#!/bin/sh
#
# Copyright (C) 2005 Lasse Collin <lasse.collin@slackware.fi>
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Last modified: Mon May 2 16:46:28 EEST 2005

# Exit status:
# 0 - All OK
# 1 - File not found
# 2 - Unknown package format
# 3 - Specified --outdir does not exist
# 99 - Invalid parameters

# Load shared functions:
. /usr/share/pkgtools/shared_functions.sh

# Do not overwrite existing files with '>'. This is to avoid symlink attacks:
set -C

usage() {
  cat << EOF

Convert packages between different compression formats. Packages that
are already in specified format are skipped. If you convert multiple
packages with one command you can also specify the new format multiple
times if you like.

Usage: convertpkg [options] package [[options2] package2 ...]

Options:  -o dir, --outdir dir  Set output directory, defaults to the same
                                directory as the source package is in.

    Target package format:
          -g, --tgz, --gzip
          -l, --tlz, --lzma  (default)
          -b, --tbz, --bzip2
          -a, --tar

EOF
  exit 0
}

convert() {
  echo -n "$1: "
  PKG_TYPE=$(package_type "$1")
  if [ "$OUT_DIR" = "" ]; then
    TARGET_FILE="$(dirname "$1")/$(package_fullname "$1").$TARGET_FORMAT"
  else
    TARGET_FILE="$OUT_DIR/$(package_fullname "$1").$TARGET_FORMAT"
  fi
  if [ ! -r "$1" ]; then
    echo "File not found."
    EXITSTATUS=1
    return
  elif [ "$PKG_TYPE" = "" ]; then
    echo "Unknown package format."
    EXITSTATUS=2
    return
  elif [ "$PKG_TYPE" = "$TARGET_FORMAT" ]; then
    echo "Already in requested format."
    return
  elif [ -e "$TARGET_FILE" ]; then
    echo "Already converted."
    return
  fi
  uncompress_pkg "$1" | compress_pkg "$TARGET_FORMAT" > "$TARGET_FILE"
  if [ $? != 0 ]; then
    echo FAILED.
  else
    echo "Done."
  fi
}

check_dir() {
  [ "$1" = "" ] && usage
  if [ ! -d "$1" ]; then
    echo "Specified directory does not exist: $1"
    exit 3
  fi
  echo "$1"
}

EXITSTATUS=0
TARGET_FORMAT=tlz
unset OUT_DIR

[ $# = 0 ] && usage
ARGS=$(getopt -n convertpkg -o -glbao:h \
  -l tgz,gzip,tlz,lzma,tbz,bzip2,tar,outdir:,help -- "$@")
[ $? != 0 ] && exit 99
eval set -- "$ARGS"
while [ $# != 0 ]; do
  case "$1" in
    -g|--tgz|--gzip)   TARGET_FORMAT=tgz ;;
    -l|--tlz|--lzma)   TARGET_FORMAT=tlz ;;
    -b|--tbz|--bzip2)  TARGET_FORMAT=tbz ;;
    -a|--tar)          TARGET_FORMAT=tar ;;
    -o|--outdir)       OUT_DIR=$(check_dir "$2"); shift 1 ;;
    -h|--help)         usage ;;
    --)                exit $EXITSTATUS ;;
    *)                 convert "$1" ;;
  esac
  shift 1
done
