#!/bin/bash

###############################################################################################
# install repo-proxy
#
# TODO make this more interactive!
###############################################################################################
THIS_VER='v0.01'
PATHLIST="/usr/local/sbin /usr/sbin /usr/local/bin /usr/bin"

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

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

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

SERVER_URL='localhost'
SERVER_PORT=10001
SERVER_SRC="$SERVER_URL:$SERVER_PORT/"

PREV_INSTALL=0
INSTALLED=0

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
}

INSTALL_REPO_PATH() {
# $1 pathname
	echo "search $1"
	if [ -d "$1" ] ; then
		REPOPATH="$1/repo-proxy"
		if [ -f "$REPOPATH" ] ; then
			REPOVER=$($REPOPATH -v)
			echo -n " found existing repo-proxy $REPOVER"
			if [ "$REPOVER" \> "$THIS_VER" -o "$REPOVER" == "$THIS_VER" ] ; then
				echo " (current version or better)"
				PREV_INSTALL=1
				INSTALLED=1
				return 1
			fi
			echo " (replace previous version)"
		else
			echo " install $REPOPATH"
		fi
		echo -n " copy: "
		cp -fv ./repo-proxy "$REPOPATH"
		# be smarter! the old config may be exactly the same as the new
		# FIXME get port from old config
		if [ -f "$CONFIG_DEST" ] ; then
			echo -n " rename: "
			mv -fv "$CONFIG_DEST" "$CONFIG_DEST.old"
		fi
		echo -n " copy: "
		cp -fv "$CONFIG_SOURCE" "$CONFIG_DEST"
		INSTALLED=1
		return 1
	fi
	echo " not found"
	return 0
}

echo "checking previous install"

# is repo-proxy already installed?
for x in $PATHLIST ; do
	if [ -f "$x/repo-proxy" ] ; then
		INSTALL_REPO_PATH $x
		break
	fi
done

if [ $INSTALLED -eq 0 ] ; then
	for x in $PATHLIST ; do
		INSTALL_REPO_PATH $x
		if [ $INSTALLED -eq 1 ] ; then
			echo "successful install $INSTALLED"
			break
		fi
	done
	if [ $INSTALLED -eq 0 ] ; then
		echo "failed to install repo-proxy"
		exit 1
	fi
fi

XINETD=0
XINETD_INSTALL=0
echo
echo -n 'checking xinetd installed: '
if [ -d "/etc/xinetd.d" ] ; then
	XINETD=1
	echo 'yes '
	if [ -f "$XINETD_DEST" ] ; then
		XINETD_INSTALL=1
		# check path, turn on, restart xinetd
		CURRENT_STATUS=$(sed -n 's:^[ \t]*disable[ \t]*=[ \t]*\([^ \t]*\):\1:p' "$XINETD_DEST")
		CURRENT_PORT=$(sed -n 's:^[ \t]*port[ \t]*=[ \t]*\([^ \t]*\):\1:p' "$XINETD_DEST")
		CURRENT_PATH=$(sed -n 's:^[ \t]*server[ \t]*=[ \t]*\([^ \t]*\):\1:p' "$XINETD_DEST")
# 		echo "current status $CURRENT_STATUS"
# 		echo "current port $CURRENT_PORT"
# 		echo "current path $CURRENT_PATH"
		if [ $CURRENT_STATUS == 'no' - o "$CURRENT_PATH" != "$REPOPATH" -o "$CURRENT_PORT" != "$SERVER_PORT" ] ; then
			# could be smarter about this?
			echo " replace: \`$XINETD_SOURCE' -> \`$XINETD_DEST'"
			sed -e 's:{PATH}:'"$REPOPATH"':' -e 's:{PORT}:'"$SERVER_PORT"':' "$XINETD_SOURCE" > "$XINETD_DEST"
			/etc/init.d/xinetd restart
		else
			# NOTE we don't check if it's actually running!
			echo " configuration OK"
		fi
		echo
	fi
else
	echo 'no'
fi

SERVICE_INSTALL=0
echo -n 'checking repo-proxy service installed: '
if [ -f "$SERVICE_DEST" ] ; then
	echo 'yes '
	echo
	# FIXME maybe we should check some values like path and port
	if [ $XINETD_INSTALL == 1 ] ; then
		echo "Found repo-proxy running from xinetd and as a service."
		echo "Removing service"
		$SERVICE_DEST stop
		chkconfig --del repo-proxy
		rm -fv $SERVICE_DEST
	else
		SERVICE_INSTALL=1
		chkconfig --add repo-proxy
		$SERVICE_DEST restart
	fi
else
	echo 'no'
	echo
	if [ $XINETD == 1 -a $XINETD_INSTALL == 0 ] ; then
		WAIT_KEY "Run repo-proxy from xinetd (y/n)?" "'y' or 'n'" "y n"
		if [ $YN == 'y' ] ; then
			XINETD_INSTALL=1
			echo " copy: \`$XINETD_SOURCE' -> \`$XINETD_DEST'"
			sed -e 's:{PATH}:'"$REPOPATH"':' -e 's:{PORT}:'"$SERVER_PORT"':' "$XINETD_SOURCE" > "$XINETD_DEST"
			/etc/init.d/xinetd restart
		fi
	fi
	if [ $XINETD_INSTALL == 0 ] ; then
		echo "Run repo-proxy as a service"
#		WAIT_KEY "Run repo-proxy as a service (y/n)?" "'y' or 'n'" "y n"
#		if [ $YN == 'y' ] ; then
			SERVICE_INSTALL=1
			echo " copy: \`$SERVICE_SOURCE' -> \`$SERVICE_DEST'"
			sed -e 's:{PATH}:'"$REPOPATH"':' -e 's:{PORT}:'"$SERVER_PORT"':' "$SERVICE_SOURCE" > "$SERVICE_DEST"
			chmod -v "--reference=$SERVICE_SOURCE" "$SERVICE_DEST"
			chkconfig --add repo-proxy
			$SERVICE_DEST start
#		fi
	fi
fi


echo
echo "check sources"
if [ -f "/etc/apt/sources.list" ] ; then
	APTGET="/etc/apt/sources.list"
	if [ "$(grep 'http://' "$APTGET")" ] ; then
		echo -n " found apt-get sources (repo-proxy "
		if [ "$(grep "http://$SERVER_SRC" "$APTGET")" ] ; then
			echo "configured)"
		else
			echo "not configured)"
			WAIT_KEY "Update apt-get sources (y/n)?" "'y' or 'n'" "y n"
			if [ $YN == 'y' ] ; then
				echo " found these entries to change:"
				echo
				sed -n 's!http://\(.[^:]*\)$!http://\1!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://\(.[^:]*\)$!http://'"$SERVER_SRC"'\1!p' "$APTGET.save"
					sed 's!http://\(.[^:]*\)$!http://'"$SERVER_SRC"'\1!' "$APTGET.save" > "$APTGET"
					echo 'update apt-get'
					apt-get update
				fi
			fi
			if [ $YN != 'y' ] ; then
				echo
				echo "Your apt sources must be manually configured."
				echo "Edit '$APTGET'."
				echo "Find the repository source you want to use with repo-proxy"
				echo "and change the source as follows:"
				echo
				echo " from:   http://some.domain..."
				echo "   to:   http://${SERVER_SRC}some.domain..."
				echo
			fi
		fi
	else
		echo " found apt-get sources but no http repositories"
	fi
	echo
fi
if [ -f "/var/lib/smart/config" ] ; then
	echo " found smart sources"
	SMART="/var/lib/smart/config"
	if [ "$(grep 'http://' "$SMART")" ] ; then
		echo -n " found smart sources (repo-proxy "
		if [ "$(grep "http://$SERVER_SRC" "$SMART")" ] ; then
			echo "configured)"
		else
			echo "not configured)"
			echo
			echo "Smart must be manually configured."
			echo "Start Smart, select edit->channels from the main menu."
			echo "Select the channel you want to use repo-proxy and change"
			echo "the 'Base URL' property as follows:"
			echo
			echo " from:   http://some.domain..."
			echo "   to:   http://${SERVER_SRC}some.domain..."
			echo
		fi
	else
		echo " found smart sources but no http repositories"
	fi
fi

echo
echo "Installation complete!"
if [ $XINETD_INSTALL == 1 ] ; then
	echo "Repo-proxy is running from xinetd."
else
	if [ $SERVICE_INSTALL == 1 ] ; then
		echo "Repo-proxy is running as a service."
	else
		echo "Repo-proxy must be started manually."
	fi
fi
echo "Please see the README.txt file for more information."

