#!/bin/bash

###############################################################################################
# remove repo-proxy
#
# TODO make this more interactive!
###############################################################################################
PATHLIST="/usr/local/sbin /usr/sbin /usr/local/bin /usr/bin"

CONFIG_DEST='/etc/repo-proxy.conf'

XINETD_DEST='/etc/xinetd.d/repo-proxy'

SERVICE_DEST='/etc/init.d/repo-proxy'

# should get these directly from the config file!!
SERVER_URL='localhost'
SERVER_PORT=10001
SERVER_SRC="$SERVER_URL:$SERVER_PORT/"
CACHEDIR="/var/cache/repo-proxy"

WAIT_KEY() {
	INITIAL_TXT=$1
	OPTIONS_TXT=$2
	OPTIONS_LIST=$3
	okToProceed=0

	while [ $okToProceed -eq 0 ]; do
		read -e -n1 -p " $INITIAL_TXT " YN

		for option in $OPTIONS_LIST ; do
			if [ $option == "$YN" ] ; then
				okToProceed=1
				break
			fi
		done

		if [ $okToProceed -eq 0 ] ; then
			echo ""
			echo " Please enter $OPTIONS_TXT."
		fi
	done
}

echo "checking previous install"
echo "check sources"
if [ -f "/etc/apt/sources.list" ] ; then
	APTGET="/etc/apt/sources.list"
	if [ "$(grep "http://$SERVER_SRC" "$APTGET")" ] ; then
		echo " found these entries to change:"
		echo
		sed -n 's!http://'"$SERVER_SRC"'!http://'"$SERVER_SRC"'!p' "$APTGET"
		echo
		WAIT_KEY "Are you sure you want to update these (y/n)?" "'y' or 'n'" "y n"
		if [ $YN == 'y' ] ; then
			echo -n " copy: "
			mv -fv "$APTGET" "$APTGET.save"
			# once for the terminal
			sed -n 's!http://'"$SERVER_SRC"'!http://!p' "$APTGET.save"
			sed 's!http://'"$SERVER_SRC"'!http://!' "$APTGET.save" > "$APTGET"
			echo 'update apt-get'
			apt-get update
		else
			echo
			echo "Your apt sources must be manually configured."
			echo "Edit '$APTGET'."
			echo "Find the repository source you want to remove repo-proxy"
			echo "from and change the source as follows:"
			echo
			echo " from:   http://${SERVER_SRC}some.domain..."
			echo "   to:   http://some.domain..."
			echo
		fi
	else
		echo " found apt-get sources but no repo-proxy entries"
	fi
	echo
fi
if [ -f "/var/lib/smart/config" ] ; then
	if [ "$(grep "http://$SERVER_SRC" "$SMART")" ] ; then
		echo
		echo "Smart must be manually configured."
		echo "Start Smart, select edit->channels from the main menu."
		echo "Select the channel you want to remove repo-proxy from and"
		echo "change the 'Base URL' property as follows:"
		echo
		echo " from:   http://${SERVER_SRC}some.domain..."
		echo " to:     http://some.domain..."
		echo
	else
		echo " found smart sources but no repo-proxy entries"
	fi
fi

echo
if [ -f "$XINETD_DEST" ] ; then
	echo "found xinetd install"
	echo " removing"
	rm -fv $XINETD_DEST
	/etc/init.d/xinetd restart
fi

if [ -f "$SERVICE_DEST" ] ; then
	echo "found service install"
	echo " removing"
	$SERVICE_DEST stop
	chkconfig --del repo-proxy
	rm -fv $SERVICE_DEST
fi

for x in $PATHLIST ; do
	if [ -f "$x/repo-proxy" ] ; then
		echo "found repo-proxy $x/repo-proxy"
		echo " removing"
		"$x/repo-proxy" -K
		rm -fv "$x/repo-proxy"
	fi
done

echo
if [ -f "$CONFIG_DEST" ] ; then
	echo "removing config file"
	rm -fv $CONFIG_DEST
	echo
fi

if [ -d "$CACHEDIR" ] ; then
	echo "found cache directory $CACHEDIR"
	WAIT_KEY "Do you want to delete it (y/n)?" "'y' or 'n'" "y n"
	if [ $YN == 'y' ] ; then
		echo "removing cache directory"
		rm -fr "$CACHEDIR"
	fi
fi

echo
echo "Removal complete!"
