#!/bin/sh

# -
# Copyright (c) 2002, Ramsey G. Brenner <rgbrenner@myrealbox.com>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Redistributions of products derived from this software must obtain
#    the specific prior written permission of the author.
# 4. Neither the name of the author nor the names of its contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
# -

unknown_answer()
{
	printf "\
Please answer with 'Y', 'y', 'N', or 'n'.\n\n"
}

still_no_answer()
{
	printf "\
This script cannot understand answers other than 'Y',
'y', 'N', or 'n'\n\n."
}

manual_install()
{
	printf "\
You can go into the 'src' directory and type './configure && make &&
make install && make man && make clean' to manually install.\n"
}


case "$1" in
install)
	case "$USER" in
	root)
		;;
	*)
		printf "\
You must be root to install.\n"
		exit 1
		;;
	esac

	for cnt in 1 2 3
	{
		printf "\
Install manual pages? (y|n) "
		read tmp
		printf "\n"
		case "$tmp" in
		Y | y)
			MAN_PAGES=YES
			break
			;;
		N | n)
			MAN_PAGES=NO
			break
			;;
		*)
			case "$cnt" in
			3)
				printf "\
Assuming you do want to install the manual pages.\n\n"
				MAN_PAGES=YES
				break
				;;
			*)
				unknown_answer
				continue
				;;
			esac
			;;
		esac
	}

# Start installation
	cd src/
	./configure 2>../install.log >../install.log && \
		make 2>>../install.log >>../install.log && \
		make install 2>>../install.log >>../install.log
	case "$?" in
	0)
		case "$MAN_PAGES" in
		YES)
			make man 2>>../install.log >>../install.log
			case "$?" in
			0)
				;;
			*)
				make uninstall clean 2>/dev/null >/dev/null
				printf "\
Sorry, the installation failed. There will be an 'install.log' file
in the current directory which will tell you where it failed.\n\n"
				manual_install
				exit 1
				;;
			esac
			;;
		esac

		printf "\
Installation success. Cleaning up..."
		make clean 2>>../install.log >>../install.log
		rm -f ../install.log
		printf "\
Finished.\n"
		printf "\
If you ever want to uninstall, run XORUninstall.\n"
		;;
	*)
		make uninstall clean 2> /dev/null >/dev/null
		printf "\
Sorry, the installation failed. There will be an 'install.log' file
in the current directory which will tell you where it failed.\n\n"
		manual_install
		exit 1
		;;
	esac
	;;
uninstall)
	case "$USER" in
	root)
		;;
	*)
		printf "\
You must be root to uninstall.\n"
		exit 1
		;;
	esac

	cd src/
	./configure 2>../uninstall.log >../uninstall.log && \
		make 2>>../uninstall.log >>../uninstall.log && \
		make uninstall 2>>../uninstall.log >>../uninstall.log
	case "$?" in
	0)
		make clean 2>>../uninstall.log >>../uninstall.log
		rm -f ../uninstall.log
		printf "\
XORCrypt has been removed.\n"
		exit 0
		;;
	*)
		make clean 2>/dev/null >/dev/null
		printf "\
XORCrypt could not be removed! An error log called 'uninstall.log'
has been create which should indicate the error.\n"
		exit 1
		;;
	esac
	;;
upgrade)
	case "$USER" in
	root)
		;;
	*)
		printf "\
You must be root to upgrade.\n"
		exit 1
		;;
	esac

#	XORUninstall -f
	./start uninstall
	case "$?" in
	0)
		;;
	*)
		exit 1
		;;
	esac
	./start install
	;;
help)
	printf "\
Usage: ./start <option>
Options:
	install:    Install the program
	upgrade:    Upgrade from previous version
	help:       This screen\n"
	;;
*)
	echo "Usage: $0 [install, upgrade, help]"
	;;
esac
exit 0
