#
# install.sh
#
# $Id: //poco/1.4/Installer/install.sh#2 $
#
# Generic Unix install script for Applied Informatics
# C++ Libraries and Tools.
#
# The installer first displays the EULA
# and prompts the user for acceptance.
# Then, the user is asked for a path where
# the software is to be installed.
# Finally, the software is copied to the
# specified directory.
#

installerName=$0
installerPath=`dirname $installerName`
licensePath=$installerPath/license.txt
packagePath=$installerPath/product.tar
specPath=$installerPath/product.txt
preinstallPath=$installerPath/preinstall.sh
postinstallPath=$installerPath/postinstall.sh
defaultInstallDir=/opt/appinf
product=`cat $specPath`

echo ""
echo ""
echo "***"
echo "*** Welcome to the"
echo "***"
echo "***     $product"
echo "***"
echo "*** installer program!"
echo "***"
echo ""
echo "This program will guide you through the installation of $product."

if [ -f $licensePath ] ; then
	echo ""
	echo "Please review and accept the license terms before the installation begins."
	echo "Press RETURN to review the License Agreement."
	read dummy
	more $licensePath
	echo ""
	echo "======== END OF LICENSE AGREEMENT ========"
	echo ""
	echo "Do you accept the terms in the License Agreement (please answer with 'Yes' to accept)? "
	echo -n "===> "
	read answer
	if [ "$answer" != "Yes" ] ; then
		if [ "$answer" != "yes" ] ; then
			echo "Installation cancelled."
			exit 1
		fi
	fi
fi

echo ""
echo "Please specify the directory where $product shall be installed (default: '$defaultInstallDir')."
echo -n "===> "
read installDir
if [ "$installDir" = "" ] ; then
	installDir=$defaultInstallDir
fi

echo ""

if [ ! -d "$installDir" ] ; then
	echo "The directory '$installDir' does not exist and will be created."
	echo "Press RETURN to continue, or CTRL-C to cancel."
	read dummy
	mkdir -p $installDir
	if [ $? != 0 ] ; then
		echo "Cannot create directory ${installDir}. Exiting."
		exit 1
	fi
fi

if [ ! -w $installDir ] ; then
	echo "Directory $installDir is not writeable. Exiting."
	exit 1
fi

echo ""
echo "The software is now being installed. This may take some time."

if [ -x $preinstallPath ] ; then
	$preinstallPath $installDir
fi

(cd $installDir && tar xf -) <$packagePath

if [ -x $postinstallPath ] ; then
	$postinstallPath $installDir
fi

echo ""
echo "The software has been successfully installed."
echo "Thank you for installing $product."
echo ""

