#!/bin/bash

#################################################################
# Silicon Software Runtime 5.7.0.76321 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.7.0
 -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 Runtime 5.7.0 Linux Installer
#################################################################

EOF

INSTALLATION_DIRECTORY=/opt/SiliconSoftware/Runtime5.7.0
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=siso-rt5-5.7.0.76321-linux-amd64.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 Software Runtime 5.7.0 to directory \"$INSTALLATION_DIRECTORY_ABS\" ... "
	tar -xvf "$BASEPATH/$INSTALLARCHIVE" > /dev/null || die
	echo "Done"

	echo -n "Setting group of directory \"bin/log\" to \"video\" ... "
	chgrp video bin/log
	echo "Done"

	echo -n "Setting permissions of directory \"bin/log\" to 0775 ... "
	chmod 0775 bin/log
	echo "Done"

	echo -n "Setting group of directory \"genicam/cache\" to \"video\" ... "
	chgrp video genicam/cache
	echo "Done"

	echo -n "Setting permissions of directory \"genicam/cache\" to 0775 ... "
	chmod 0775 genicam/cache
	echo "Done"

	echo -n "Modifying SISODIR5 in \"setup-siso-env.sh\" ... "
	sed -i "s|export SISODIR5=/opt/siso|export SISODIR5=$INSTALLATION_DIRECTORY_ABS|" setup-siso-env.sh || die
	echo "Done"

	echo -n "Modifying environment in \"bin/gs\" ... "
	sed -i "s|. /opt/siso/setup-siso-env.sh|. $INSTALLATION_DIRECTORY_ABS/setup-siso-env.sh|" bin/gs || die
	echo "Done"

	echo -n "Modifying environment in \"bin/GenICamExplorer\" ... "
	sed -i "s|. /opt/siso/setup-siso-env.sh|. $INSTALLATION_DIRECTORY_ABS/setup-siso-env.sh|" bin/GenICamExplorer || die
	echo "Done"

	echo -n "Modifying environment in \"bin/microDisplay\" ... "
	sed -i "s|. /opt/siso/setup-siso-env.sh|. $INSTALLATION_DIRECTORY_ABS/setup-siso-env.sh|" bin/microDisplay || die
	echo "Done"

	echo -n "Modifying environment in \"bin/microDiagnostics\" ... "
	sed -i "s|. /opt/siso/setup-siso-env.sh|. $INSTALLATION_DIRECTORY_ABS/setup-siso-env.sh|" bin/microDiagnostics || die
	echo "Done"

	echo "Installation complete."
	echo
) | tee $INSTALLATION_DIRECTORY_ABS/install_log.txt 2>&1
chmod 755 $INSTALLATION_DIRECTORY_ABS/doc -R
exit 0

