#!/bin/bash

# Based on the Kadu install script (www.kadu.net)

DEFAULT_DIR=`kde-config --prefix`
if [ "$DEFAULT_DIR" == "" ]; then
	DEFAULT_DIR=/usr
fi;

INSTALL_DIR=$DEFAULT_DIR

while [ 1 ]; do

	clear
	echo
	echo "  KShutDown Installer"
	echo "  -------------------"
	echo
	echo "  [i] Install (./configure --prefix=${INSTALL_DIR}, make, su -c 'make install')"
	echo "  [p] Change Prefix"
	echo
	echo "  [c] Changes (q - quit)"
	echo "  [l] License (GPL)"
	echo "  [r] Readme"
	echo
	echo "  [q] Quit"

	read -n 1 -s

	# install
	if [[ ("$REPLY" == "i") || ( "$REPLY" == "I" ) ]]; then

		# configure
		if ./configure "--prefix=${INSTALL_DIR}"; then
			# make
			if make; then
				# make install via su
				echo "----"
				echo "Enter root password to install (make install):";
				if su -c "make install"; then
					echo "----"
					echo "Command Line:"
					echo "KShutDown Classic: kshutdown"
					echo "KShutDown Wizard: kshutdown --wizard"
					echo "Options: kshutdown --help"
					echo "K Menu: Utilities -> KShutDown"
					echo "----"
					echo -e "\a"
					exit
				else
					echo "----"
					echo "Setup: make install failed!"
					exit 1
				fi;
			else
				echo "----"
				echo "Setup: make failed!"
				exit 1
			fi;
		else
			echo
			echo "----"
			echo "Setup: configure failed!"
			echo "Setup: Would you like to see config.log? (y/n)"
			read -n 1 -s
			if [[ ("$REPLY" == "y") || ("$REPLY" == "Y") ]]; then
				less config.log
			fi;
			exit 1
		fi;

		exit;
	fi;

	# prefix
	if [[ ("$REPLY" == "p") || ( "$REPLY" == "P" ) ]]; then
		echo "----"
		echo "Setup: Enter a new prefix [${DEFAULT_DIR}]: "
		read -e
		if [ "$REPLY" == "" ]; then
			REPLY=$DEFAULT_DIR
		fi;
			INSTALL_DIR="$REPLY"
	fi;

	# changes
	if [[ ("$REPLY" == "c") || ( "$REPLY" == "C" ) ]]; then
		less "ChangeLog"
	fi;

	# license
	if [[ ("$REPLY" == "l") || ( "$REPLY" == "L" ) ]]; then
		less "COPYING"
	fi;

	# readme
	if [[ ("$REPLY" == "r") || ( "$REPLY" == "R" ) ]]; then
		less "README"
	fi;

	# quit
	if [[ ("$REPLY" == "q") || ( "$REPLY" == "Q" ) ]]; then
		exit
	fi;

done
