#!/bin/bash
## install script for sonicwall sslvpn Linux client

VERSION="8.5.0.50"
echo "--- SonicWALL Virtual Assist $VERSION Installer ---"

USRLIB='/usr/lib'
LIB='/lib'

## FUNCTIONS ###########################################################
function check_64bit
{
	# Are we on 64-bit architecture?
	if [[ "`uname -m`" =~ '64' ]]
	then
		# Is this NX package also 64-bit?
		if [[ (! `file -b libVirtualAssist.so` =~ '64-bit') && ($FORCE_INSTALL != 1) ]]
		then
			echo "ERROR: This copy of Virtual Assist is intended for 32-bit systems."
			echo "       Please install a copy of the 64-bit version of Virtual Assist."
			exit
		fi

		# Is /usr/lib64 a directory?
		if [ -d /usr/lib64 -a ! -L /usr/lib64 ]
		then
			USRLIB='/usr/lib64'
		fi

		# Is /lib64 a directory?
		if [ -d /lib64 -a ! -L /lib64 ]
		then
			LIB='/lib64'
		fi
	elif [[ `file -b libVirtualAssist.so` =~ '64-bit' && $FORCE_INSTALL != 1 ]]
	then
		echo "ERROR: This copy of Virtual Assist is intended for 64-bit systems."
		echo "       Please install a copy of the 32-bit version of Virtual Assist."
		exit
	fi
}

function exit_dependency_failure
{
	echo
	echo "-------------------------- INSTALLATION FAILED -------------------------"
	echo
	echo "Library dependencies must be resolved before Virtual Assist will be able to"
	echo "operate correctly.  Please resolve any dependencies listed above, then"
	echo "try installing again."
	echo
	exit 1
}

function exit_success
{
	echo
	echo "------------------------ INSTALLATION SUCCESSFUL -----------------------"
	echo
	echo "To launch VirtualAssist, do one of the following:"
	echo
	echo "  1. Click the VirtualAssist icon under the Applications menu"
	echo "     (look under the 'Internet' or 'Network' category)"
	echo "     or"
	echo "  2. Type 'VirtualAssistGui'"
	
	echo
	exit 0
}

function assert_running_as_root
{
	if [ "`id -u`" != "0" ]
	then
		echo "Please run the Virtual Assist installer as root."
		echo "On many systems, you can use the sudo command:"
		echo
		echo "  sudo ./install"
		echo
		exit 1
	fi
}

function check_library_dependencies
{
	echo "Checking library dependencies..."
	MISSINGLIBS=`ldd libVirtualAssist.so | grep 'not found' | awk '{print $1}'`

	if [ "$MISSINGLIBS" != "" ]
	then
		for i in $MISSINGLIBS
		do
			echo "  Missing library: $i"
			FULLNAME="$i"
			LIBNAME=`echo $i | awk -F. '{print $1 }'`
			ALT=`find $LIB -maxdepth 1 -name "$LIBNAME.so*" -type f | sed 's/\*//g' | sort -r | head -1`

			if [ "$ALT" == "" ]
			then
				# Didn't find anything in $LIB ; try $USRLIB
				ALT=`find $USRLIB -maxdepth 1 -name "$LIBNAME.so*" -type f | sed 's/\*//g' | sort -r | head -1`
			fi

			if [ "$ALT" != "" ]
			then
				echo "  Found likely compatible version: $ALT"
				REPLY='0'

				while [ "$REPLY" != "y" -a "$REPLY" != "n" -a "$REPLY" != "Y" -a "$REPLY" != "N" ]
				do
					read -p '  Attempt auto-repair [Y/n]? ' -n1
					if [ "$REPLY" == "" ]
					then
						REPLY='y'
					else
						echo ""
					fi
				done

				if [ "$REPLY" == 'y' -o "$REPLY" == 'Y' ]
				then
					pushd . >/dev/null
					cd `dirname $ALT`
					ln -sv `basename $ALT` $FULLNAME
					popd >/dev/null
				else
					exit_dependency_failure
				fi
			else
				# No compatible version found
				echo "  No compatible version found."
				exit_dependency_failure
			fi

			echo ""
		done
	fi
}

function install_files
{
	echo 'Copying files...'
	mkdir -p /usr/share/VirtualAssist/icons
	install -m 755 VirtualAssistGui /usr/bin
	install -m 755 vasac /usr/bin

	install -m 755 libVirtualAssist.so $USRLIB
	install -m 644 VirtualAssist.jar /usr/lib
	install -m 644 icons/* /usr/share/VirtualAssist/icons
	install -m 755 uninstall /usr/share/VirtualAssist/uninstallVirtualAssist
	install -m 664 VirtualAssist.desktop /usr/share/VirtualAssist
	
	chcon -t textrel_shlib_t "$USRLIB/libVirtualAssist.so"
	
	install_menu_shortcut
}

function install_menu_shortcut
{
	cp /usr/share/VirtualAssist/VirtualAssist.desktop /usr/share/applications/sonicwall-virtualassist.desktop
	chown -f root /usr/share/applications/sonicwall-virtualassist.desktop
	chgrp -f root /usr/share/applications/sonicwall-virtualassist.desktop
	chmod -f 644 /usr/share/applications/sonicwall-virtualassist.desktop
}

## MAIN INSTALLATION ###################################################
assert_running_as_root

check_64bit
check_library_dependencies

# If we make it here, libraries should all be good to go
install_files

exit_success
