#!/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`

[ -f /tmp/.nosceexist ] && sudo rm /tmp/.nosceexist

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
        echo
	echo "${YELLOW}sce-sce2gz - Create a *.gz initrd file from SCE(s) (*.sce) and any dependency"
	echo "             SCE(s) located in /etc/sysconfig/tcedir/sce/.${NORMAL} The resulting *.gz"
	echo "             file can be manually moved into dCore's boot directory alongside"
	echo "             the primary dCore-*.gz file. The boot loader's boot parameters"
	echo "             must be appended with this *.gz file to load it at boot. Note"
	echo "             dCore is capable of booting numerous *.gz initrd files."
	echo " "
	echo "             Possible reasons to create and use *.gz initrd file(s):"
	echo "             * Load files directly into RAM, not symlinked."
	echo "             * Load files earlier in the boot cycle."
	echo "             * Avoid using a persistent TCE directory."
	echo "             * Combine numerous SCEs into a mega-initrd."
        echo " "
	echo "Usage:"
        echo " "
	echo "${YELLOW}"sce-sce2gz SCE"${NORMAL}  Create a *.gz file from the specified SCE and any dependency"
	echo "                SCE(s) located in /etc/sysconfig/tcedir/sce/. Prompted to"
	echo "                name the *.gz file, creates a *.gz and *.gz.md5.txt file. "
	echo "${YELLOW}"sce-sce2gz"${NORMAL}      Menu prompt, create a *.gz file from one or more selected"
	echo "                SCE(s) and any dependency SCE(s)."
        echo " "
exit 1
fi


[ -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
	continue
fi

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." "-"
		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 " "
	echo -n "Enter desired name of resulting .gz initrd: "
	read TARGET
	echo " "
	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 "* Unpacking: ${YELLOW}"$D".sce${NORMAL}."
		sudo unsquashfs -f -d rootfs "$SCE"
	done
	cd rootfs
	echo -n "* Creating initrd: ${NORMAL}${YELLOW}$TARGET.gz${NORMAL}."
	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 " "
	echo "* Success."
else
	echo " "
	echo -n "Enter desired name of resulting .gz initrd: "
	read TARGET
	echo " "
	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 ' '`
		THISAPPNAME=${TARGETAPP%%.sce}
		grep "$THISAPPNAME:" /tmp/.sce2gzunpacked > /dev/null 2>&1 && continue
		if [ ${TARGETAPP} == ${THISAPPNAME} ]; then
			THISSCE=${TARGETAPP}.sce
		else
			THISSCE=${TARGETAPP}
		fi
		
		
		#if [ ! -f "$SCEDIR"/"$THISSCE" ]; then
		#	echo "${MAGENTA}$THISSCE${NORMAL} does not exist in the SCE directory, not using.."
		#	continue
		#fi
	
		if [ -f "$SCEDIR"/"$THISSCE".dep ]; then
			DEPLIST="$THISAPPNAME $DEPLIST"
			for I in `cat "$SCEDIR"/"$THISSCE".dep`; do
				if [ -f "$SCEDIR"/"$THISSCE".dep ]; then
					for I in `cat "$SCEDIR"/"$THISSCE".dep`; do
							getDeps "$I"
					done
				fi
			done
		else
			DEPLIST=" "$DEPLIST" $THISAPPNAME"
		fi
	done

	for I in `echo "$DEPLIST" | sort | uniq`; do
		if [ ! -f "$SCEDIR"/"$I".sce ]; then
			echo "$I" >> /tmp/.nosceexist
			
		fi
	done

	if [ -s /tmp/.nosceexist ]; then
		echo "The below SCEs don't exist as SCEs or in SCE.dep files, exiting.."
		for I in `cat /tmp/.nosceexist | sort | uniq`; do
			echo "${MAGENTA}"$I"${NORMAL}"
		done
		exit 1
	fi

	for I in `echo "$DEPLIST" | sort | uniq`; do
		echo "* Unpacking: ${YELLOW}"$I".sce${NORMAL}."
		cd "$WORK"
		sudo unsquashfs -f -d rootfs "$SCEDIR"/"$I".sce
		cd "$HERE"
		echo "$I:" >> /tmp/.sce2gzunpacked
	done
	cd "$WORK"
	cd rootfs
	echo -n "* Creating initrd: ${NORMAL}${YELLOW}$TARGET.gz${NORMAL}."
	sudo 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 " "
	echo "* Success."
fi

cleanup

