#!/bin/sh
# Copyright 2009  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# 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.
#
# Sun Apr  27 02:09:00 CET 2009
# First version.  <titopoquito>

SCRIPT_ALIAS=$(basename $0)
TAR=tar-1.13
CWD=$(pwd)
umask 022
$TAR --help 1> /dev/null 2> /dev/null
if [ ! $? = 0 ]; then
  TAR=tar
fi
if [ ! "$(LC_MESSAGES=C $TAR --version)" = "tar (GNU tar) 1.13

Copyright (C) 1988, 92,93,94,95,96,97,98, 1999 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Written by John Gilmore and Jay Fenlason." ]; then
  echo "WARNING: pkgtools are unstable with tar > 1.13."
  echo "         You should provide a \"tar-1.13\" in your \$PATH."
  sleep 5
fi

if [ "$1" = "" ]; then
  echo "${SCRIPT_ALIAS}:  Converts Slackware packages to another compression format."
  echo "          Slackware packages can be build using different compressions: tar"
  echo "          (without compression), tgz or tar.gz (gzip compressed, the standard"
  echo "          up till Slackware 12.2, tbz or tar.bz2 (b2zip compression), tlz or"
  echo "          tar.lzm (lzma compression) and last but not least txz or tar.xz"
  echo "          (xz compression)."
  echo "          This script can be called as {tgz|tbz|tlz|txz}2{tgz|tbz|tlz|txz}."
  echo "          The new package will be saved in the current directory."
  echo
  echo "Usage:    $0 <file.{tgz|tbz|tlz|txz}>"
  exit 1
fi


# Create a new temporary directory with a secure filename:
make_temp_dir() {
  if [ -x "$(which mcookie)" ]; then
    tempd=/tmp/tmp.$(mcookie)
    mkdir -p -m 0755 $tempd
  elif [ -x "$(which openssl)" ]; then
    tempd=/tmp/tmp.$(dd if=/dev/urandom bs=1k count=1 2> /dev/null | openssl dgst -md5)
    mkdir -p -m 0755 $tempd
  elif [ -x "$(which md5)" ]; then
    tempd=/tmp/tmp.$(dd if=/dev/urandom bs=1k count=1 2> /dev/null | md5)
    mkdir -p -m 0755 $tempd
  elif [ -x "$(which mktemp)" ]; then
    tempd=$(mktemp -d)
    chmod 755 $tempd
  ## Uncomment for insecure use, but don't blame me:
  #else
  #  tempd=/tmp/tmp.$$
  #  mkdir -p -m 0755 $tempd
  fi
  if [ -d $tempd ]; then # success, return the name of the directory:
    echo $tempd
  else
    echo "ERROR:  Could not find mcookie, openssl, or md5."
    echo "        Exiting since a secure temporary directory could not be made."
    exit 1
  fi
}


IN_FORMAT=$(echo $SCRIPT_ALIAS | cut -d "2" -f 1)
OUT_FORMAT=$(echo $SCRIPT_ALIAS | cut -d "2" -f 2)

for i in $* ; do

  # test if package name has right extension
  if [ $(basename "$i" .${IN_FORMAT}) = $(basename "$i") ]; then
    echo "ERROR: package "$i" does not look like a ${IN_FORMAT} package. Skipping package."
    continue
  else

    # Create a temporary directory:
    TMPDIR=$(make_temp_dir)

    # explode source package
    ( cd $TMPDIR 
      if [ $(dirname "$i") = "." ]; then
        explodepkg "$CWD/$i" # argument was only filename, no path
      else
        explodepkg "$i"      # argument has a path and a filename
      fi
    )
  
    # Make sure external compression utility is available:
    if [ $IN_FORMAT = "tgz" -o $OUT_FORMAT = "tgz" ]; then
      if ! which gzip 1> /dev/null 2> /dev/null ; then
	echo "ERROR:  gzip compression utility not found in \$PATH."
	exit 3
      fi
    fi
    if [ $IN_FORMAT = "tbz" -o $OUT_FORMAT = "tbz" ]; then
      if ! which bzip2 1> /dev/null 2> /dev/null ; then
	echo "ERROR:  bzip2 compression utility not found in \$PATH."
	exit 3
      fi
    fi
    if [ $IN_FORMAT = "tlz" -o $OUT_FORMAT = "tlz" ]; then
      if ! which lzma 1> /dev/null 2> /dev/null ; then
	echo "ERROR:  lzma compression utility not found in \$PATH."
	exit 3
      fi
    fi
    if [ $IN_FORMAT = "txz" -o $OUT_FORMAT = "txz" ]; then
      if ! which xz 1> /dev/null 2> /dev/null ; then
	echo "ERROR:  xz compression utility not found in \$PATH."
	exit 3
      fi
    fi

    # repackage it
    ( cd $TMPDIR
      case $OUT_FORMAT in
	'tgz' )
	  $TAR cvf - . | gzip -9c > ${CWD}/$(basename "$i" .${IN_FORMAT}).${OUT_FORMAT}
	  ERRCODE=$?
	  if [ ! $? = 0 ]; then
	      echo "ERROR:  gzip returned error code $? -- $SCRIPT_ALIAS failed."
	      echo "ERROR:  Package "$i" could not be converted."
	  fi
	  ;;
	'tbz' )
	  $TAR cvf - . | bzip2 -9c > ${CWD}/$(basename "$i" .${IN_FORMAT}).${OUT_FORMAT}
	  ERRCODE=$?
	  if [ ! $ERRCODE = 0 ]; then
	      echo "ERROR:  bzip2 returned error code $ERRCODE -- $SCRIPT_ALIAS failed."
	      echo "ERROR:  Package "$i" could not be converted."
	  fi
	  ;;
	'tlz' )
	  $TAR cvf - . | lzma -c > ${CWD}/$(basename "$i" .${IN_FORMAT}).${OUT_FORMAT}
	  ERRCODE=$?
	  if [ ! $ERRCODE = 0 ]; then
	      echo "ERROR:  lzma returned error code $ERRCODE -- $SCRIPT_ALIAS failed."
	      echo "ERROR:  Package "$i" could not be converted."
	  fi
	  ;;
	'txz' )
	  $TAR cvf - . | xz -c > ${CWD}/$(basename "$i" .${IN_FORMAT}).${OUT_FORMAT}
	  ERRCODE=$?
	  if [ ! $ERRCODE = 0 ]; then
	      echo "ERROR:  xz returned error code $ERRCODE -- $SCRIPT_ALIAS failed."
	      echo "ERROR:  Package "$i" could not be converted."
	  fi
	  ;;
	esac
      )
    # Remove temporary directory:
    rm -rf $TMPDIR
  fi

done