#!/bin/sh
#
#  AMC (Automatic Message Creator) installation script
#    (c) 2004/11/06  -  J/D.H. Beekhuizen
#

#  Version number
VERSION=0.99.5/Linux

declare -i Count

# some useful functions

#  Request displays the first parameter, and asks a new value for 
#  variable, which name is # in the second parameter

Request ()
{
qvar=$2
eval oldvar=\$$qvar
eval echo -n \""$1 [$oldvar] : "\" 
read $qvar
eval test -z \"\$$qvar\" && eval $qvar=\'$oldvar\'
}

#  YesNo gives you value 1 on "no", value 0 on "yes"
#  The parameter specifies the text to display

YesNo ()
{
	while true
		do
		echo -n "$1 (Y/n) ? "
		read qans
		case Q$qans in
			Q|Qy|QY) return 0;;
			Qn|QN)   return 1;;
		esac
	done
}

#  Well...
#

Sorry ()
{
	echo
	echo "I'm sorry I couldn't be of any help."
	echo
	exit 5
}

#  Things could go wrong...
#

InstallProblem ()
{
	echo
	echo "The 'install' utility returned an error."
	echo "Unable to proceed!"
	echo
	if [ "" = "True" ]
		then
		rm -rf  2>&1 > /dev/null
	fi
	exit 7
}

LinkProblem ()
{
	echo
	echo "The symbolic link could not be made."
	echo "Please investigate this and do it manually."
	echo
}
	
#  You must be root to use this script
#

if [ "`id -un`" != "root" ]
    then
    echo
	echo "You must be root to install AMC for general use!"
	echo "Please read 00INSTALL for instructions on installation."
	echo
	exit 3
fi


#  Blurb
#

clear
echo
echo "This script will install"
echo "Automatic Message Creator  v$VERSION"
echo "for general use."
echo

#  The default is to install in /opt/amc

PREFIX=/opt

#  Get the installation directory

Request "Please specify the directory to install AMC in" PREFIX

AMC_ROOT=$PREFIX/amc
AMC_BIN=$AMC_ROOT/bin
AMC_OLDLANG=$AMC_BIN/lang
AMC_LIB=$AMC_ROOT/lib
AMC_LANG=$AMC_LIB/lang
AMC_ETC=$AMC_ROOT/etc
AMC_DOC=$AMC_ROOT/doc
AMC_LOG=$AMC_ROOT/log  
AMC_LOGfile=$AMC_ROOT/log/amc.log

#  Check if it exists already

if [ -d $AMC_ROOT ]
	then
	echo
	echo "There already exists a directory $AMC_ROOT."
	echo "If you choose to overwrite it, any existing default configuration files"
	echo "and language resource files will be saved."
	if YesNo "Do you want to overwrite it"
		then
		if YesNo "Are you absolutely sure"
			then
			AMC_New=False
			Count=10
			while [ $Count -gt 0 ]
				do
				echo -en "Starting in $Count seconds; use <Ctrl>C to bail out "\\r
				sleep 1
				Count=$(($Count-1))
			done
			echo
			echo
		else
			Sorry
		fi
	else
		Sorry
	fi
else
	AMC_New=True
fi
	
#  OK, now do the installation
#

#  Make the directories
#

install -o root -g root -m 755 -d $AMC_ROOT      ||  InstallProblem
install -o root -g root -m 755 -d $AMC_BIN       ||  InstallProblem
install -o root -g root -m 755 -d $AMC_LANG      ||  InstallProblem
install -o root -g root -m 755 -d $AMC_DOC       ||  InstallProblem
install -o root -g root -m 755 -d $AMC_ETC       ||  InstallProblem
install -o root -g root -m 755 -d $AMC_LOG       ||  InstallProblem

#  Save old default configuration and language resource files
#

if [ "$AMC_New" = "False" ]
	then
	if [ -d $AMC_OLDLANG ]
		then
		mv $AMC_OLDLANG/* $AMC_LANG/  > /dev/null 2>&1
		rm -r $AMC_OLDLANG             > /dev/null 2>&1
		echo "NOTICE: the language resource files have been moved"
		echo "            from $AMC_OLDLANG"
		echo "              to $AMC_LANG"
	fi
	for ll in en nl de
		do
		LFILE=amc_messages.$ll.po
		if [ -f $AMC_LANG/$LFILE	]
			then
			mv $AMC_LANG/$IFILE $AMC_LANG/$LFILE.sav  > /dev/null 2>&1
			echo "NOTICE: file $AMC_LANG/$LFILE has been saved as"
			echo "             $AMC_LANG/$LFILE.sav"
		fi
	done
	for ff in amc.cfg amc.txt amcbirth.day
		do
		if [ -f $AMC_ETC/$ff	]
			then
			mv $AMC_ETC/$ff $AMC_ETC/$ff.sav  > /dev/null 2>&1
			echo "NOTICE: file $AMC_ETC/$ff has been saved as"
			echo "             $AMC_ETC/$ff.sav"
		fi
	done
fi

#  Install the contents			
#

install -o root -g root -m 755 -s bin/* $AMC_BIN
install -o root -g root -m 666 lib/lang/* $AMC_LANG
install -o root -g root -m 644 doc/* $AMC_DOC
install -o root -g root -m 644 etc/* $AMC_ETC
install -o root -g root -m 666 /dev/null $AMC_LOGfile

chmod 444 $AMC_LANG/README
chmod 444 $AMC_LANG/amc_messages.00.po

#  Installation done
#

echo
echo "The installation is finished."
echo "The executable file is:                  " $AMC_BIN
echo "The language resource files are in:      " $AMC_LANG
echo "The default configuration files are in:  " $AMC_ETC
echo "The default log file is:                 " $AMC_LOGfile
echo "Documentation is in:                     " $AMC_DOC

#  Make a link?
#

echo
echo "It is possible to create a symbolic link to AMC"
echo "either in /usr/local/bin or in /usr/bin"
echo

if YesNo "Do you want to create a symbolic link in /usr/local/bin"
	then
	ln -sf $AMC_BIN/amc /usr/local/bin/amc  ||  LinkProblem
else
	if YesNo "Do you want to create a symbolic link in /usr/bin"
		then
		ln -sf $AMC_BIN/amc /usr/bin/amc    ||  LinkProblem
	fi
fi

echo
echo "AMC is now installed and ready for use."
echo "Please change the configuration to your own needs."
echo
echo "Have fun with it!"
echo
