#!/bin/busybox ash
# (c) Jason Williams 2016

. /etc/init.d/tc-functions
checknotroot
TCEDIR=/etc/sysconfig/tcedir
SCEDIR="/etc/sysconfig/tcedir/sce"
WORK="$TCEDIR"/import/sce2gz
TARGETAPP="$1"
HERE=`pwd`

[ -d "$WORK" ] || mkdir "$WORK"
cleanup() {
	[ -f /tmp/select.ans ] && sudo rm /tmp/select.ans
	[ -f /tmp/.scelistchoose ] && sudo rm /tmp/.scelistchoose
	[ -f /tmp/.scegzchoose ] && sudo rm /tmp/.scegzchoose
	[ -f /tmp/.sce2gzunpacked ] && sudo rm /tmp/.sce2gzunpacked
}
	

cleanup

getDeps() {
DEPLIST=" $1 $DEPLIST "

if [ -f "$SCEDIR"/"$1".sce.dep ]; then
	for E in `cat "$SCEDIR"/"$1".sce.dep`; do
		H=" $E "
		if echo "$DEPLIST" | grep "$H" > /dev/null 2>&1; then
			continue
		else
			getDeps "$E"
		fi
	done
fi
}

if [ -z "$1" ]; then
	cd "$SCEDIR"
	ls *.sce | sed 's:.sce$::' | sort > /tmp/.scelistchoose
	while true; do
		[ ! -s /tmp/.scelistchoose ] && break
		cat /tmp/.scelistchoose | select2 "Choose SCE(s) to convert to a *.gz initrd, press Enter with no selection to proceed." "-"
		read ANS < /tmp/select.ans
		if [ "$ANS" == "" ]; then
			break
		fi
		grep "^$ANS$" /tmp/.scegzchoose > /dev/null 2>&1 || echo "$ANS" >> /tmp/.scegzchoose
		sed -i "/^$ANS$/d" /tmp/.scelistchoose
	done
	for I in `cat /tmp/.scegzchoose`; do 
		if [ -f "$SCEDIR"/"$I".sce.dep ]; then
			getDeps "$I"
			for I in `echo "$DEPLIST"`; do
				grep "^$I$" /tmp/.scegzchoose > /dev/null 2>&1 || echo "$I" >> /tmp/.scegzchoose
			done
		fi
	done
	echo -n "Enter desired name of resulting .gz initrd: "
	read TARGET
	TARGET=`basename "$TARGET" .gz`
	if [ -z "$TARGET" ]; then
		echo "You must choose name, exiting.."
		exit 1
	fi
	cd "$WORK"
	for D in `cat /tmp/.scegzchoose`; do
		SCE="$SCEDIR"/"$D".sce
		echo "${YELLOW}/etc/sysconfig/tcedir/sce/"$D".sce${NORMAL}: Unpacking.."
		sudo unsquashfs -f -d rootfs "$SCE"
	done
	cd rootfs
	echo "${NORMAL}${YELLOW}/etc/sysconfig/tcedir/sce/$TARGET.gz${NORMAL} creating initrd.."
	find | sudo cpio -o -H newc | sudo gzip -2 > "$SCEDIR"/"$TARGET".gz 
	cd "$SCEDIR"
	sudo md5sum "$TARGET".gz > "$TARGET".gz.md5.txt
	chown "$USER":staff "$TARGET".gz*
	cd "$WORK"
	sudo rm -r rootfs
	echo "${NORMAL}${YELLOW}$TARGET.gz${NORMAL} is now in your SCE directory."
else
	echo -n "Enter desired name of resulting .gz initrd: "
	read TARGET
	TARGET=`basename "$TARGET" .gz`
	if [ -z "$TARGET" ]; then
		echo "You must choose name, exiting.."
		exit 1
	fi
	for TARGETAPP in `echo "$@"`; do
		TARGETAPP=`echo "$TARGETAPP" | tr -d ' '`
		THISSCENAME=${TARGETAPP%%.sce}
		THISSCENAME=`basename "$THISSCENAME"`
		grep "$THISSCENAME:" /tmp/.sce2gzunpacked > /dev/null 2>&1 && continue
		if [ ${TARGETAPP} == ${THISSCENAME} ]; then
			THISSCE=${TARGETAPP}.sce
		else
			THISSCE=${TARGETAPP}
		fi
		if [ -f "$THISSCE" ] || [ -f "$TCEDIR"/"$THISSCE" ]; then
			TARGETDIR=`dirname "$THISSCE"`
		else
			TARGETDIR="$TCEDIR"/sce
		fi
	
		if [ "$TARGETDIR" == "." ]; then
			TARGETDIR="`pwd`"
		fi
		if [ -f "$TARGETDIR"/"$THISSCE".dep ]; then
			DEPLIST="$TARGETAPP $DEPLIST"
			for I in `cat "$TARGETDIR"/"$THISSCE".dep`; do 
				for I in `cat "$TARGETDIR"/"$THISSCE".dep`; do
						getDeps "$I"
				done
			done
		else
			DEPLIST=" "$DEPLIST" $TARGETAPP"
		fi
	done
	for I in `echo "$DEPLIST"`; do
		if [ ! -f "$TARGETDIR"/"$I".sce ]; then
				echo "${YELLOW}"$TARGETDIR"/"$I".sce${NORMAL} does not exist, continuing.."
				continue
		fi
		echo "${YELLOW}"$TARGETDIR"/"$I".sce${NORMAL}: Unpacking.."
		cd "$WORK"
		sudo unsquashfs -f -d rootfs "$TARGETDIR"/"$I".sce
		cd "$HERE"
		echo "$I:" >> /tmp/.sce2gzunpacked
	done
	cd "$WORK"
	cd rootfs
	echo "${NORMAL}${YELLOW}$SCEDIR/$TARGET.gz${NORMAL} creating initrd.."
	find | sudo cpio -o -H newc | sudo gzip -2 > "$SCEDIR"/"$TARGET".gz 
	cd "$SCEDIR"
	sudo md5sum "$TARGET".gz > "$TARGET".gz.md5.txt
	chown "$USER":staff "$TARGET".gz*
	cd "$WORK"
	sudo rm -r rootfs
	echo "${NORMAL}${YELLOW}$TARGET.gz${NORMAL} is now in your SCE directory."
fi

cleanup
