#!/bin/bash

#################################################################
# Silicon Software Marathon VCX QP Applets Linux Installer Script
#################################################################

die() {
	echo "Error!"; exit 1;
}

print_usage() {
cat << EOF
Usage: $0 options

OPTIONS:
 -d <dir>     installation target directory   (default: /opt/SiliconSoftware/Runtime5.6.0/dll/mE5-MA-VCX-QP)
 -y <yes|no>  assume YES on questions         (default: no)
 -h           print this help

EOF
}

if [ "$USER" != "root" ]; then
	echo "Installer needs to be run as 'root'!"
	exit 1
fi

cat << EOF
#################################################################
# Silicon Software Marathon VCX QP Applets Linux Installer
#################################################################

EOF

INSTALLATION_DIRECTORY=/opt/SiliconSoftware/Runtime5.6.0/dll/mE5-MA-VCX-QP
ASSUME_YES=no

while getopts "d:e:s:y:h" OPTION
do
	case $OPTION in
		d)
			INSTALLATION_DIRECTORY=$OPTARG
			;;
		y)
			ASSUME_YES=$OPTARG
			;;
		h)
			print_usage
			exit
			;;
		?)
			print_usage
			exit
			;;
	esac
done

cat << EOF
INSTALLATION PARAMETERS:
    Target directory            : $INSTALLATION_DIRECTORY

EOF

echo -n "Are you sure to proceed (yes/no)? "

if [ "$ASSUME_YES" == "yes" ]; then
	echo "yes"
else
	read
	if [ "$REPLY" != "yes" ]; then
		exit
	fi
fi

cat << EOF

#################################################################
# Now Installing ....
#################################################################

EOF

BASEPATH="$(cd "$(dirname "$0")"; pwd)"

INSTALLARCHIVE=setup-mE5-MA-VCX-QP-ACQ-Applets-5.6.0-linux-ia32.tar.bz2

echo -n "Creating target directory ... "
mkdir -p $INSTALLATION_DIRECTORY || die
cd $INSTALLATION_DIRECTORY || die
INSTALLATION_DIRECTORY_ABS=$(pwd)
echo "Done"

(
	echo -n "Checking installation archive \"$INSTALLARCHIVE\" ... "
	if [ -e "$BASEPATH/$INSTALLARCHIVE" ]; then
		echo "Done"
	else
		echo "Missing"
		exit 1
	fi
		
	echo -n "Unpacking Silicon Marathon ACX QP Applets to directory \"$INSTALLATION_DIRECTORY_ABS\" ... "
	tar -xvf "$BASEPATH/$INSTALLARCHIVE" > /dev/null || die
	echo "Done"

) | tee $INSTALLATION_DIRECTORY_ABS/install_log.txt 2>&1

exit 0

