#!/bin/bash

# Written: 2009, Michiel van Wessem, Manchester, United Kingdom.
#  - With thanks to Robby Workman for polishing the script.
# Orginally based on the rpm2tgz script created by Patrick Volkerding
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "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 COPYRIGHT
# OWNER OR CONTRIBUTORS 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.

# FOR PEOPLE RUNNING SLACKWARE-12.2 (xz is of course part of -current)
# NOTE: This script requires you to have xz installed n your
#      system. The best way is to download the source/a/xz/ 
#      slackbuild and source from a slackware-current mirror 
#      and build xz from scratch.
#
#      You also may want to edit /etc/DIR_COLORS, so that 
#      txz files show up properly as archives.

trap "do_cleanup ; exit 0" EXIT;
trap "do_cleanup ; exit 1" SIGINT SIGTERM;

if [ "$1" = "" ]; then
  printf "$(basename $0):  Converts a standard slackware package (.tgz) to \n"
  printf "          the new standard of GNU tar + xz (LZMA) format. \n\n"
  printf "Usage:      $(basename $0) <file.tgz> \n"
  printf "            (Outputs \"file.txz\")\n"
fi

# First, let's make sure xz is installed.
if ! xz --help 1> /dev/null 2> /dev/null; then
  printf "ERROR: the \"xz\" utility has not been found. (did you install it?)\n"
  exit 127
fi

find_pkg_dirname() {
  PKG_DIR=$(dirname $i)
}

find_pkg_basename() {
  PKG_BASE=$(basename $i .tgz)
}

do_cleanup() {
  rm -rf $TGZ2TXZ_TEMP 
}

# Create a new temporary directory with a secure filename:
make_temp_dir() {
  TMP=${TMP:-/tmp}
  
  if [ -x "$(which mcookie)" ]; then
    TGZ2TXZ_TEMP=${TMP}/tgz2txz.$(mcookie)
    mkdir -p -m 0755 ${TGZ2TXZ_TEMP}
  elif [ -x "$(which openssl)" ]; then
    TGZ2TXZ_TEMP=${TMP}/tgz2txz.$(dd if=/dev/urandom bs=1k count=1 2> /dev/null | openssl dgst -md5)
    mkdir -p -m 0755 ${TGZ2TXZ_TEMP}
  elif [ -x "$(which md5)" ]; then
    TGZ2TXZ_TEMP=${TMP}/tgz2txz.$(dd if=/dev/urandom bs=1k count=1 2> /dev/null | md5)
    mkdir -p -m 0755 ${TGZ2TXZ_TEMP}
  elif [ -x "$(which mktemp)" ]; then
    TGZ2TXZ_TEMP=$(mktemp -p ${TMP} -d tgz2txz.XXXXXX 2>/dev/null)
	chmod 0755 ${TGZ2TXZ_TEMP}
  fi
  # success, return the name of the directory:
  if [ -d ${TGZ2TXZ_TEMP} ]; then
    echo ${TGZ2TXZ_TEMP}
  else
    temp_failure
  fi
}

temp_failure() {
  printf "Failed to create a secure temporary working directory. \n"
  printf "Be sure you have either the \"mcookie\", \"openssl\", \"md5\", \n"
  printf "or the \"mktemp\" package installed. \n"
  exit 1
}

convert_failure() {
  printf "Failed to convert ${PKG_BASE}..."
}

# Create a temporary directory:
TGZ2TXZ_TEMP=$(make_temp_dir) || temp_failure

for i in $*; do
  if [ ! -e $i ]; then
    printf "WARNING: ${PKG_BASE}.tgz not found - skipping...\n"
  else
    # Unset PKG_DIR and PKG_BASE just to be sure
    unset PKG_DIR PKG_BASE

    # Get the dirname and basename of the package
    find_pkg_dirname
    find_pkg_basename

    # Do the conversion into the temporary working directory
    gunzip -dc ${PKG_DIR}/${PKG_BASE}.tgz | \
      xz -zc > ${TGZ2TXZ_TEMP}/${PKG_BASE}.txz
    [ $? = 0 ] && SUCCESS=1

    # If the conversion worked, then wipe the old and move the new over
    if [ $SUCCESS = 1 ]; then
      rm -f ${PKG_DIR}/${PKG_BASE}.tgz
      mv ${TGZ2TXZ_TEMP}/${PKG_BASE}.txz ${PKG_DIR}
    else
      convert_failure
    fi
  fi
done

