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

. /etc/init.d/tc-functions

TCEDIR="/etc/sysconfig/tcedir"
SCEDIR=""$TCEDIR"/sce"
SCE=${SCE%%.sce}
cd "$SCEDIR"

cleanup () {
[ -f /tmp/.keeplist ] && sudo rm /tmp/.keeplist
[ -f /tmp/.scermlist ] && sudo rm /tmp/.scermlist
[ -f /tmp/select.ans ] && sudo rm /tmp/select.ans
}

cleanup

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
	echo " "
	echo "${YELLOW}sce-remove - Remove SCE(s) upon shutdown${NORMAL}. Removing an SCE that other SCE(s)"
	echo "             depend on for shared dependencies will also flag these for removal."
	echo "             SCE(s) flagged for removal are listed in /tmp/.removesce prior to"
	echo "             shutdown. Applicable SCE(s) will also be removed from OnDemand."
	echo " "
	echo "Usage:"
	echo " "
	echo "${YELLOW}"sce-remove"${NORMAL}          Select SCE(s) for removal from list."
	echo "${YELLOW}"sce-remove SCE"${NORMAL}      Mark SCE and any SCE(s) that depend on it for removal."
	echo "${YELLOW}"sce-remove -k"${NORMAL}       Select SCE(s) for removal from list, keep .lst and .dep"
        echo "                    files, useful for future re-imports."
	echo "${YELLOW}"sce-remove -k SCE"${NORMAL}   Mark SCE and any SCE(s) that depend on it for removal,"
        echo "                    keep .lst and .dep files, useful for future re-imports."
	echo " "
exit 1
fi


while getopts k OPTION
do
	case ${OPTION} in
		k) KEEPLIST=TRUE ;;
		*) echo "Run  sce-remove --help  for usage information."
		   exit 1 ;;
	esac
done
shift `expr $OPTIND - 1`
SCE="$1"

if [ -n "$2" ]; then
	echo " "
	echo "${YELLOW}Warning:${NORMAL} Only one SCE can be entered in command line for removal."
	echo "         Run 'sce-remove' to select more than one SCE, exiting.."
	exit 1
fi

if [ "$KEEPLIST" == "TRUE" ]; then
	echo "* Using the -k option."
fi

if grep -i "^KEEPLIST=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1; then
	touch /tmp/.keeplist
elif [ "$KEEPLIST" == "TRUE" ]; then
	touch /tmp/.keeplist
fi

if [ -z "$SCE" ]; then
	## Select sces to remove on shutdown.
	cd "$SCEDIR"
	ls *.sce | sed 's:.sce$::' | sort > /tmp/.scermlist
	while true; do
		[ ! -s /tmp/.scermlist ] && break
		cat /tmp/.scermlist | select2 "Choose SCE(s) to remove on shutdown, press Enter without selection to proceed." "-"
		read ANS < /tmp/select.ans
		if [ "$ANS" == "" ]; then
			break
		fi
		grep "^$ANS$" /tmp/.removesce > /dev/null 2>&1 || echo "$ANS" >> /tmp/.removesce
		sed -i "/^$ANS$/d" /tmp/.scermlist
	done
else
	cd "$SCEDIR"
	if [ -f "$SCE".sce ]; then
		echo "$SCE" > /tmp/.removesce
	else
		echo "'"$SCE".sce' does not exist, exiting.."
		exit 1
	fi
fi 

rmdep() {
if ls *.dep > /dev/null 2>&1; then
	for D in `grep "^$1$" *.dep`; do 
		EXT0=`echo "$D" | cut -f1 -d:`
		EXT=`basename "$EXT0" .sce.dep`
		grep "^$EXT$" /tmp/.removesce > /dev/null 2>&1 || echo "$EXT" >> /tmp/.removesce
		rmdep "$EXT"
	done
fi	
}
if [ -s /tmp/.removesce ]; then
	for I in `cat /tmp/.removesce`; do
		rmdep "$I"
	done
	echo " "
	cat /tmp/.removesce
	echo " "
	echo "${YELLOW}Warning:${NORMAL} Selected SCE(s) and any that require the dependencies, listed above,"
	echo "         will be removed at shutdown, any OnDemand scripts will now be removed."
	echo " "
	echo -n "Press Enter to confirm removal, (q)uit to exit without changes: "
	read ans
	if [ "$ans" == "q" ] || [ "$ans" == "quit" ]; then
		rm /tmp/.removesce
		cleanup
		exit 1
	fi
	for I in `cat /tmp/.removesce`; do 
		ondemand -r "$I"
	done
	echo " "
	echo "These SCE(s) will be removed during shutdown, exiting.."
else
	echo "No SCE(s) selected for removal, exiting.."
fi

cleanup
