#!/bb/ash
# (c) Robert Shingledecker 2013
# Contributions by Jason Williams
. /etc/init.d/tc-functions

useBusybox
checknotroot
PROG_NAME=$(basename $0)
KERNELVER=$(uname -r)
unset BOOTING ONDEMAND SUPPRESS
FORCE="n"  # Overwrite system files default to no. Use -f to force overwrite.
SAVED_DIR=`pwd`

#ONBOOTNAME="$(getbootparam lst 2>/dev/null)"

[ -f /tmp/select.ans ] && sudo rm /tmp/select.ans

[ -n "$ONBOOTNAME" ] || ONBOOTNAME="onboot.lst"

[ -f /tmp/.installed ] || touch /tmp/.installed

abort(){
	echo "Version `version`";
	echo "Usage: ${PKG_NAME}"
	echo " Example:"
	echo "  loadsce nano"
	exit 2
}

abort_to_saved_dir(){
	cd "$SAVED_DIR"
	exit 1
}

choose(){
	TMP=/tmp/select.$$
	SCE=/tmp/sce.$$
	MOUNTED=/tmp/mounted.$$
	ls -1 /etc/sysconfig/tcedir/sce/*.sce|awk 'BEGIN{FS="/"}{size=length($6)-4; print substr($6,0,size)}' > "$SCE"
	mount | awk '$3 ~ /tcloop/{print substr($3,13)}' > "$MOUNTED"

	cat "$SCE" "$MOUNTED" "$MOUNTED" | sort | uniq -u > "$TMP"

	rm "$SCE"
	rm "$MOUNTED"

	if [ ! -s "$TMP" ]; then
		echo "Nothing found to load."
		exit 1
	fi
	select "dCore - Select SCE to load" "$TMP"
	TARGETAPP="$(cat  /tmp/select.ans)"
	rm "$TMP"
	[ "$TARGETAPP" == "q" ] && exit 0
	echo "Loading $TARGETAPP"

}

processAPP(){
	APPNAME=${TARGETAPP%%.sce}
	if [ ${TARGETAPP} == ${APPNAME} ]; then TARGETAPP=${TARGETAPP}.sce; fi
	APPNAME="${APPNAME/-KERNEL/-${KERNELVER}}"
	TARGETAPP="${TARGETAPP/-KERNEL.sce/-${KERNELVER}.sce}"

	#THISAPP=`basename $APPNAME .sce`
	if grep "^$APPNAME:" /tmp/.debinstalled > /dev/null 2>&1; then
		if [ "$BOOTING" ]; then
			continue
		else
			echo ""$APPNAME" is already installed!"
			continue
		fi
	fi
	if [ -d /tmp/tcloop/"$APPNAME" ]; then
		if [ "$BOOTING" ]; then
			continue
		else
			echo ""$APPNAME" is already installed!"
			continue
		fi
	fi
	if [ -f "$TARGETAPP" ]; then
		:
	elif [ -f "$TCEDIR"/sce/"$TARGETAPP" ]; then
		TARGETAPP="$TCEDIR"/sce/"$TARGETAPP"
	else
		echo "$TARGETAPP not found!"
		abort_to_saved_dir
	fi
	#FROMWHERE=`dirname "$TARGETAPP"` && cd "$FROMWHERE"
	EXTENSION=`basename "$TARGETAPP"`
	install "$TARGETAPP"
	[ "$SUPPRESS" ] || echo "$TARGETAPP: OK"
}

while getopts bdsf OPTION
do
	case ${OPTION} in
		b) BOOTING=TRUE ;;
		s) SUPPRESS=TRUE ;;
		f) FORCE="y" ;;
		d) DEBUG=TRUE ;;
		*) abort ;;
	esac
done
shift `expr $OPTIND - 1`

startupscript_check() {
	APPSSS_D="/tmp/tcloop/${APPNAME}/usr/local/tce.installed"
	if [ -d "$APPSSS_D" ]
	then
		for SS in $(ls "$APPSSS_D" )
		do
			if [ ! -e "$TCEINSTALLED"/"$SS" ]
			then
				[ -x "$APPSSS_D"/"$SS" ] && echo "$TCEINSTALLED"/"$SS" >> /tmp/ss$$.lst
			fi
		done
	else
		touch "$TCEINSTALLED"/${APPNAME}
	fi
}

update_system() {
	if [ "$BOOTING" ]; then
		[ "$MODULES" ] && sudo touch /etc/sysconfig/newmodules
	else
		if [ "$MODULES" ]; then
			sudo depmod -a 2>/dev/null
			sudo /sbin/udevadm trigger
		fi
		sudo /sbin/ldconfig 2>/dev/null
	fi
	if [ -s /tmp/ss$$.lst ]; then
		if [ "$BOOTING" ] ; then
			cat /tmp/ss$$.lst >> /tmp/setup.lst
		else
			for SS in $(cat /tmp/ss$$.lst | grep -v "tce.installed/$APPNAME$")
			do
				APP=`basename "$SS"`
				if ! grep "^$APP:" /tmp/.installed > /dev/null 2>&1; then
					if [ "$DEBUG" == "TRUE" ]; then
					  echo "$APP:" >> /tmp/.installed
					  sudo echo "$APP:" | sudo tee -a /var/log/sce.log > /dev/null 2>&1
					  sudo $SS 2>&1 | sudo tee -a /var/log/sce.log > /dev/null 2>&1
					else
					  sudo $SS >> /dev/null 2>&1 
					  echo "$APP:" >> /tmp/.installed
					fi
					
				fi
			done
			APP=`basename /usr/local/tce.installed/"$APPNAME"`
			if [ "$DEBUG" == "TRUE" ]; then
				echo "$APP:" >> /tmp/.installed
				sudo echo "$APP:" 2>&1 | sudo tee -a /var/log/sce.log > /dev/null 2>&1
				sudo /usr/local/tce.installed/"$APPNAME" 2>&1 | sudo tee -a /var/log/sce.log > /dev/null 2>&1
			else	
				echo "$APP:" >> /tmp/.installed
				sudo /usr/local/tce.installed/"$APPNAME" >> /dev/null 2>&1 
			fi
		   FREEDESKTOP="/tmp/tcloop/"$APPNAME"/usr/share/applications"
		   if [ "$(ls -A $FREEDESKTOP 2>/dev/null)" ]; then
		    	for F in $(ls "$FREEDESKTOP"/*.desktop | grep -v "tinycore-"| grep -Ev '(~[1-9][1-9]*)'.desktop); do
		     		if ! grep "OnlyShowIn" "$F" > /dev/null 2>&1 && ! grep "NoDisplay=true" "$F" > /dev/null 2>&1; then
		       			EXTNAME="${F%.desktop}"
		      			EXTNAME="${EXTNAME##*/}"
		       			desktop.sh "$EXTNAME" > /dev/null 2>&1
		     		fi
		    	done
		   fi
		   
		   FREEDESKTOP="/tmp/tcloop/"$APPNAME"/usr/local/share/applications"
		   if [ "$(ls -A $FREEDESKTOP 2>/dev/null)" ]; then
		    	for F in $(ls "$FREEDESKTOP"/*.desktop | grep -v "tinycore-"| grep -Ev '(~[1-9][1-9]*)'.desktop); do
		     		if ! grep "OnlyShowIn" "$F" > /dev/null 2>&1 && ! grep "NoDisplay=true" "$F" > /dev/null 2>&1; then
		       				EXTNAME="${F%.desktop}"
		       				EXTNAME="${EXTNAME##*/}"
		       				desktop.sh "$EXTNAME" > /dev/null 2>&1
		     		fi
		    	done
		   fi
		   
		fi
		
		rm /tmp/ss$$.lst
	fi		
}

install(){
	unset MODULES EMPTYEXT

	if [ "$LANG" != "C" ]; then
		LOCALEEXT="${1%.sce}-locale.sce"
		[ -f "$LOCALEEXT" ] && install "$LOCALEEXT"
	fi

	THISAPP="$1"
	APPNAME=$(getbasefile "$THISAPP" 1)

	[ -d /tmp/tcloop/"$APPNAME" ] || sudo mkdir -p /tmp/tcloop/"$APPNAME"
	awk -v appname="/tmp/tcloop/$APPNAME" ' { if ( $2 == appname )  exit 1 }' /etc/mtab
	[ "$?" == 1 ] || sudo mount "$THISAPP" /tmp/tcloop/"$APPNAME" -t squashfs -o loop,ro,bs=4096 2>&1
	[ "$?" == 0 ] || abort_to_saved_dir
	[ -z "`ls /tmp/tcloop/${APPNAME}`" ] && EMPTYEXT=1
	[ "`sudo find /tmp/tcloop/${APPNAME} -mindepth 1 -maxdepth 2 | wc -l`" -le 1 ] && EMPTYEXT=1

	if [ -z "$EMPTYEXT" ]; then
		startupscript_check
		yes "$FORCE" | sudo cp -ais /tmp/tcloop/"$APPNAME"/opt / 2>/dev/null
		yes "$FORCE" | sudo cp -ais /tmp/tcloop/"$APPNAME"/usr / 2>/dev/null
		yes "$FORCE" | sudo cp -ais /tmp/tcloop/"$APPNAME"/bin / 2>/dev/null
		yes "$FORCE" | sudo cp -ais /tmp/tcloop/"$APPNAME"/sbin / 2>/dev/null
		yes "$FORCE" | sudo cp -ais /tmp/tcloop/"$APPNAME"/lib / 2>/dev/null
		yes "$FORCE" | sudo cp -ais /tmp/tcloop/"$APPNAME"/dev / 2>/dev/null
		yes "$FORCE" | sudo cp -ai /tmp/tcloop/"$APPNAME"/etc / 2>/dev/null
		yes "$FORCE" | sudo cp -ai /tmp/tcloop/"$APPNAME"/var / 2>/dev/null
		[ ! -z "`sudo find /tmp/tcloop/"$APPNAME" -type f -name *.ko.gz`" ] && MODULES=TRUE
		update_system "$THISAPP" "$APPNAME"
		if [ ! "$BOOTING" ]; then
			[ -s /etc/sysconfig/desktop ] && desktop.sh "$APPNAME" > /dev/null 2>&1
		fi
	else
		umount -d /tmp/tcloop/"$APPNAME"
		update_system "$THISAPP" "$APPNAME"
	fi
	#grep "^$APPNAME:" /tmp/.installed > /dev/null 2>&1 || echo "$APPNAME:" >> /tmp/.installed
	if [ -f /tmp/tcloop/"$APPNAME"/usr/local/sce/"$APPNAME"/"$APPNAME".md5sum ]; then
		for I in `cat /tmp/tcloop/"$APPNAME"/usr/local/sce/"$APPNAME"/"$APPNAME".md5sum | cut -f1 -d:`; do
			grep "^$I:" /tmp/.debinstalled > /dev/null 2>&1 || echo "$I:" >> /tmp/.debinstalled
		done
	fi
	[ "$BOOTING" ] && [ "$SHOWAPPS" ] && echo -n "${YELLOW}$APPNAME ${NORMAL}"
}

# Main
TCEDIR=/etc/sysconfig/tcedir
[ -d "$TCEDIR" ] || exit 1
if [ -z "$1" ] && [ -z "$BOOTING" ]; then
	choose
fi
[ -f /etc/sysconfig/showapps ] && SHOWAPPS=TRUE && SUPPRESS=TRUE
TCEINSTALLED=/usr/local/tce.installed

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

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



if [ -n "$TARGETAPP" ]
#FROMWHERE=`dirname "$TARGETAPP"` && cd "$FROMWHERE"
then
	THISSCENAME=${TARGETAPP%%.sce}
		if [ ${TARGETAPP} == ${THISSCENAME} ]; then THISSCE=${TARGETAPP}.sce; fi
				
		if [ -f "$THISSCE" ]; then
			TARGETDIR=`dirname "$THISSCE"`
		else
			TARGETDIR="$TCEDIR"/sce/
		fi
		if [ -f "$TARGETDIR"/"$THISSCE".md5.txt ] && ! cat /proc/cmdline | grep " nomd5 " > /dev/null 2>&1; then
			cd "$TARGETDIR"
			md5sum -c "$THISSCE".md5.txt > /dev/null 2>&1
			if [ "$?" != 0 ]; then
				echo "Md5sum error on "$TARGETDIR"/"$THISSCE".  Exiting.."
				cd "$SAVED_DIR"
			fi
		fi
		
		if [ -f "$TARGETDIR"/"$THISSCE".dep ]; then
			DEPLIST=" $THISSCENAME "
			for I in `cat "$TARGETDIR"/"$THISSCE".dep`; do 
				getDeps "$I"
			done
		else
			DEPLIST=" $THISSCENAME "
		
		fi
		for I in `echo "$DEPLIST"`; do	
			TARGETAPP=`echo "$I" | tr -d ' '`
			#APPNAME=${TARGETAPP%%.sce}
			#if [ ${TARETAPP} == ${APPNAME} ]; then TARGETAPP=${TARGETAPP}.sce; fi
			processAPP
		done
else
	for TARGETAPP in $@; do
		THISSCENAME=${TARGETAPP%%.sce}
		if [ ${TARGETAPP} == ${THISSCENAME} ]; then THISSCE=${TARGETAPP}.sce; fi
		
		if [ -f "$THISSCE" ]; then
			TARGETDIR=`dirname "$THISSCE"`
		else
			TARGETDIR="$TCEDIR"/sce/
		fi

		if [ -f "$TARGETDIR"/"$THISSCE".md5.txt ] && ! cat /proc/cmdline | grep " nomd5 " > /dev/null 2>&1; then
			cd "$TARGETDIR"
			md5sum -c "$THISSCE".md5.txt > /dev/null 2>&1
			if [ "$?" != 0 ]; then
				echo "Md5sum error on "$TARGETDIR"/"$THISSCE".  Exiting.."
				cd "$SAVED_DIR"
			fi
		fi

		if [ -f "$TARGETDIR"/"$THISSCE".dep ]; then
			DEPLIST=" $THISSCENAME "
			for I in `cat "$TARGETDIR"/"$THISSCE".dep`; do 
				getDeps "$I"
			done
		else
			DEPLIST=" $THISSCENAME "
		
		fi

	done # Finish the for-loop for multiple extensions 
		for I in `echo "$DEPLIST"`; do
			TARGETAPP=`echo "$I" | tr -d ' '`
			#APPNAME=${TARGETAPP%%.sce}
			#if [ ${TARGETAPP} == ${APPNAME} ]; then TARGETAPP=${TARGETAPP}.sce; fi
			processAPP
		done # for loop for dependencies
fi


cd "$SAVED_DIR"

[ "$BOOTING" ] && exit 0
if [ -n "$DISPLAY" ]
then
	[ $(which "$DESKTOP"_restart) ] && "$DESKTOP"_restart > /dev/null 2>&1
	[ $(which wbar) ] && pkill wbar && wbar.sh > /dev/null 2>&1
fi

exit 0
