#!/bin/sh
# (c) Jason Williams 2015
# Create sce from Debian package
# Usage: $ sce-deb2sce debianpackagename.deb 

if [ "`id -u`" != "0" ]; then
	echo "Please run as root or with the sudo command".
	exit 1
fi

if [ -z "$1" ]; then
	echo "You must specify a deb file."
	exit 1
fi


HERE=`pwd`
PATH="/bb:/bin:/sbin:/usr/bin:/usr/sbin"
export PATH

setupStartupScript() {
	[ -d "$PKG"/usr/local/tce.installed ] || mkdir -p "$PKG/usr/local/tce.installed/"
	chmod 775 "$PKG/usr/local/tce.installed/"
	chown root.staff "$PKG/usr/local/tce.installed/"
}

make_sce() {
	CFG="$PKG"/cfg
	mkdir -p "$CFG"
	DATA_TAR=`ar t "$FILE" data.tar.*`
	CONFIG_TAR=`ar t "$FILE" control.tar.*`
	mkdir -p "$PKG"
	ar p "$FILE" "$DATA_TAR" > "$PKGDIR"/"$DATA_TAR" || exit 1
	ar p "$FILE" "$CONFIG_TAR" > "$PKGDIR"/"$CONFIG_TAR" || exit 1
	tar xf "$PKGDIR"/"$DATA_TAR" -C "$PKG" || exit 1
	tar xf "$PKGDIR"/"$CONFIG_TAR" -C "$CFG" || exit 1
	if [ "$KEEPDOC" != "TRUE" ]; then
		[ -d "$PKG"/usr/share/doc ] && rm -r "$PKG"/usr/share/doc
		[ -d "$PKG"/usr/share/man ] && rm -r "$PKG"/usr/share/man
	fi
	
	if [ -f "$HERE"/"$BASENAME".deb2sce ]; then
		#[ -d "$PKG"/usr/local/sce/"$BASENAME" ] || mkdir -p "$PKG"/usr/local/sce/"$BASENAME"
		echo Merging Tiny Core custom start script for ${BASENAME}: "${BASENAME}.deb2sce"
		setupStartupScript
		cat "$HERE"/"$BASENAME".deb2sce > "$PKG/usr/local/tce.installed/${BASENAME}"
		echo " " >> "$PKG/usr/local/tce.installed/${BASENAME}"
		echo " " >> "$PKG/usr/local/tce.installed/${BASENAME}"
		#echo ""$BASENAME"-deb2sce: `md5sum ${BASENAME}.deb2sce | cut -f1 -d" "`" >> "$PKG"/usr/local/sce/"$TARGET"/"$TARGET"-data.md5sum
		chmod -R 775 "$PKG/usr/local/tce.installed/"
		chown -R root.staff "$PKG/usr/local/tce.installed/"
	fi
	cd "$PKG"
	if [ -f "$CFG"/postinst ]; then
		mkdir -p "$PKG/usr/local/postinst"
		cp "$CFG/postinst" "$PKG/usr/local/postinst/${BASENAME}"
		SCRIPT='/usr/local/postinst/'${BASENAME}' configure'
		setupStartupScript
		echo "${SCRIPT}" >> "$PKG/usr/local/tce.installed/${BASENAME}"
		echo " " >> "$PKG/usr/local/tce.installed/${BASENAME}"
		chmod 775 "$PKG/usr/local/tce.installed/${BASENAME}"
		chown -R root.staff "$PKG/usr/local/tce.installed/"
	fi
	[ -d "$CFG" ] && rm -r "$CFG"
	cd "$PKGDIR"
	mksquashfs pkg "$HERE"/"$APPNAME" -noappend
	cd "$HERE"
	rm -r "$PKGDIR"
}

while getopts rk OPTION
do
	case ${OPTION} in
		r) RAM=TRUE ;;
		k) KEEPDOC=TRUE ;;
		*) exit 1 ;;
	esac
done

shift `expr $OPTIND - 1`

FILE="$1"
echo $FILE
INPUT=${FILE##*.}

BASENAME="${FILE%%_*}"
APPNAME="$BASENAME".sce

if [ "$RAM" == "TRUE" ]; then
	PKGDIR=/tmp/deb2sce.1234
else 	
	PKGDIR=/etc/sysconfig/tcedir/import/deb2sce.1234
fi

[ -d /tmp/deb2sce.1234 ] && rm -r /tmp/deb2sce.1234
[ -d /etc/sysconfig/tcedir/import/deb2sce.1234 ] && rm -r /etc/sysconfig/tcedir/import/deb2sce.1234

[ -d "$PKGDIR" ] || mkdir -p "$PKGDIR"

PKG="$PKGDIR"/pkg



if [ ! "$INPUT" == "deb" ] ; then
	echo "Only Debian packages work with this." 
	exit 1
else
	make_sce
fi

[ -d /tmp/deb2sce.1234 ] && rm -r /tmp/deb2sce.1234
[ -d /etc/sysconfig/tcedir/import/deb2sce.1234 ] && rm -r /etc/sysconfig/tcedir/import/deb2sce.1234
	
if [ -f "$APPNAME" ]; then
	echo "Success."
else
	echo "Something went wrong."
fi
