#!/bin/ash
# convert a pet to slackware t?z package
# originally authored by 01micko GPL2 2013
# re-written by James B, 2014 for Fatdog64 700 (also GPL2 licensed)
# Note: requires slackdesc and makepkg

### configuration
APP_VERSION="1.1james"
PKGTYPE="txz"
PKGBUILD="PET"
PKGARCH="$(uname -m)"
PKGDESC=""
PAUSE=""
WORKDIR="/tmp/pet2txz.$$"

# root check
[ $(id -u) -ne 0 ] && echo "You must be root." && exit 1

### helpers ###

# we are compatible with original - this is the original usage notes
usage() {
	echo "Usage:"
	echo "$PROG somepet.pet -- produces a Slackware .txz file."
	echo "$PROG somepet.pet -b -- produces .tbz instead of default .txz"
	echo "$PROG somepet.pet -g -- produces .tgz instead of default .txz"
	echo -e "$PROG somepet.pet -x=<build> \n-- produces build num or description instead of default PET. \nNo spaces, alpha-numeric chars only."
	echo -e "$PROG somepet.pet -d -- pauses the script to put in a verbose description."
	echo -e "$PROG somepet.pet -a=<arch> \n-- the script tries to get the architecture however\nyou can overide this, eg: \"armhf\", \"i686\""
	echo "$PROG -v -- states version"
	echo "$PROG -h -- shows this help"
	exit
}

# adapted from silent_petget, $1-pkg
check_integrity() {
	local cksum fsize
	fsize=$(( $(stat -c %s "$1") - 32 ))
	cksum=$(dd if="$1" bs=$fsize skip=1 2> /dev/null)
	echo checksum is $cksum
	if [ $(dd if="$1" bs=$fsize count=1 2> /dev/null | md5sum | awk '{print $1}') != $cksum ]; then
		echo "bad pet: checksum does not match."
		exit 1
	fi
}

# adapted from silent_petget, $1-pkg, $2-target dir, out: PETDIR, PKGDESC
extract_pet() {
	local decompressor
	case $(file --mime-type "$1") in
		*gzip) decompressor="gunzip -c" ;;
		*bzip2) decompressor="bunzip2 -c" ;;
		*xz) decompressor="unxz -c" ;;
	esac
	
	fsize=$(( $(stat -c %s "$1") - 32 ))
	dd if="$1" bs=$fsize count=1 2> /dev/null | $decompressor | tar -x -C $2
	PETDIR=$(find $2/* -maxdepth 1 -type d | head -n1)

	# handle install/uninstall scripts
	mkdir $PETDIR/install
	mv -f $PETDIR/pinstall.sh $PETDIR/install/doinst.sh 2> /dev/null
	mv -f $PETDIR/puninstall.sh $PETDIR/install/slack-uninstall.sh 2>/dev/null

	# legacy: xpm and png files at root - delete them
	rm -f $PETDIR/*.xpm $PETDIR/*.png
	[ -e $PETDIR/pet.specs ] && PKGDESC=$( awk -F"|" '{print $10}' $PETDIR/pet.specs)
	rm -f $PETDIR/pet.specs
}

# get the description
get_desc() {
	if [ "$PAUSE" ]; then
		echo "Current desription extracted from pet:"
		echo "$PKGDESC"
		printf "Enter a short description: "
		read PKGDESC
	fi
}

# make the package: $1-PETDIR $2-PKG (to determine output dir)
repack() {
	# work out correct name components
	p=${1##*/}
	PKGVERSION=${p##*-}
	PKGNAME=${p%-${PKGVERSION}}
	[ -z $PKGVERSION ] && PKGVERSION=1
	case "$PKG" in
		*/*) OUTDIR="${PKG%/*}" ;;
		*) OUTDIR="$(pwd)" ;;
	esac
	OUT="$OUTDIR"/$PKGNAME-$PKGVERSION-$PKGARCH-$PKGBUILD.$PKGTYPE
	
	# get pkg desc
	slackdesc $PKGNAME $PKGVERSION "($PKGNAME from PET)" "$PKGDESC" > $1/install/slack-desc
	
	# build the package
	( cd "$1"; makepkg -c n -l n $OUT; )
}


### main ###

while [ "$1" ]; do # args processing
	case "$1" in
		-h|--help)
			usage; exit 0;
			;;
		-v|--version)
			echo "Version $APP_VERSION"; exit 0;
			;;
		-b) PKGTYPE="tbz"
			;;
		-g)	PKGTYPE="tgz"
			;;
		-d) PAUSE=yes
			;;
		-x=*)
			PKGBUILD="${1#*=}"
			;;
		-a=*)
			PKGARCH="${1#*=}"
			;;
		*)
			PKG="$1"
			;;
	esac
	shift
done
[ -z "$PKG" ] && echo "Nothing to process." && exit 0

# do the work
check_integrity "$PKG" 
mkdir -p $WORKDIR
extract_pet "$PKG" "$WORKDIR"
get_desc
repack "$PETDIR" "$PKG"
rm -rf $WORKDIR
