#!/bin/dash
# Convert a TGZ/TBZ/TXZ package to SFS
# Copyright (C) James Budiono 2017
# License: GNU GPL Version 3 or later
#

### configuration
COMPRESSION=${COMPRESSION:--comp xz}
WORKDIR=/tmp/pkg2sfs.$$
APPTITLE="pkg2sfs"

trap 'rm -rf $WORKDIR; exit' 0 INT HUP TERM

### make sure we run as root - input $@
run_as_root() {
	if [ $(id -u) -ne 0 ]; then
		if [ $DISPLAY ]; then
			exec gtksu "$APPTITLE" "$0" "$@"
		else
			exec su -c "$0 $*"
		fi
	fi
}

# $1-msg
info_bg() {
	if [ "$DISPLAY" ]; then
		Xdialog --title "$APPTITLE" --no-buttons --infobox "$1" 0 0 999999999 &
	else
		dialog --title "$APPTITLE" --infobox "$1" 0 0 &
	fi
}

# $1-msg
msg() {
	if [ "$DISPLAY" ]; then
		Xdialog --title "$APPTITLE" --msgbox "$1" 0 0
	else
		dialog --title "$APPTITLE" --msgbox "$1" 0 0
	fi
}

# $1-msg
yesno() {
	if [ "$DISPLAY" ]; then
		Xdialog --title "$APPTITLE" --yesno "$1" 0 0
	else
		dialog --title "$APPTITLE" --yesno "$1" 0 0
	fi
}

# $1-msg
die() {
	msg "$1"
	exit 1
}

### main

# 1. sanity checks
run_as_root "$@"
case "$1" in
	*tgz|*tbz|*txz) ;; # good
	*) die "Usage: ${0##*/} /path/to/package.{txz,tbz,tgz}"
esac

SRC=$(readlink -f "$1")
! [ -e "$SRC" ] && die "$1 does not exist."
! [ -r "$SRC" ] && die "$1 is not readable."
TARGET=${SRC%.t?z}.sfs
[ -e "$TARGET" ] && die "$TARGET already exist."

# 2. show wait dialog
info_bg "Please wait, converting..."
PID=$!

# 3. extract and do the work
mkdir $WORKDIR
tar -xf "$SRC" -C $WORKDIR

# 4. handle possible install/uninstall scripts and also preserve all other metadata (Jake)
if [ -d $WORKDIR/install ]; then
	mkdir -p $WORKDIR/tmp/sfs/
	cp -a $WORKDIR/install/ $WORKDIR/tmp/sfs/
	rm -r $WORKDIR/install
	
	# get the slackdesc and show it up on load
	if [ -f $WORKDIR/tmp/sfs/install/slack-desc ]; then
		# get the package name
		prefix=$(awk -F: '{ print $1; exit; }' $WORKDIR/tmp/sfs/install/slack-desc)
		sed "s/^$prefix://; s/^[ \t]*//; /^$/d" $WORKDIR/tmp/sfs/install/slack-desc > $WORKDIR/tmp/sfs/msg
	fi	
	echo "SFS automatically created by pkg2sfs" >> $WORKDIR/tmp/sfs/msg	
	
	# check if there is an install script
	if [ -f $WORKDIR/tmp/sfs/install/doinst.sh ]; then
		# either run it later
		if yesno "\
Install script detected. 
Do you want to auto-run this when the SFS is loaded?"; 
		then
			cat << "EOF" > $WORKDIR/tmp/sfs/autorun.sh
#!/bin/sh
#Automatically created by pkg2sfs
case "$1" in
	systemload|load)
		if [ -f ${2}/tmp/sfs/install/doinst.sh ]; then
			(cd / ; sh ${2}/tmp/sfs/install/doinst.sh -install; )
		fi
		;;
	systemunload|unload)
		if [ -f ${2}/tmp/sfs/install/slack-uninstall.sh ]; then
			(cd / ; sh ${2}/tmp/sfs/install/slack-uninstall.sh -uninstall; )
		fi
		;;
esac
EOF
			chmod +x $WORKDIR/tmp/sfs/autorun.sh
		else
			# or run it now
			if yesno "\
Do you want to run the install script now, to have the effect
baked into the SFS, instead? (un-install script will be ignored)";
			then
				( cd $WORKDIR; sh $WORKDIR/tmp/sfs/install/doinst.sh -install; )
				sed -i -e '1i#This script has already been run when this SFS was created by pkg2sfs' \
					$WORKDIR/tmp/sfs/install/doinst.sh
			fi
			# or not at all
		fi
	fi
fi

# x. final step
chmod 755 $WORKDIR
mksquashfs "$WORKDIR" "$TARGET" $COMPRESSION
kill -0 $PID 2>/dev/null && kill $PID
msg "Done. Created $TARGET."
