#!/bin/bash
#
# ------------------------------------------------------
# Client install/uninstall script for Linux/Unix
# ------------------------------------------------------

#
# CLIENT_INSTALL needs to point to the directory
# where the .jar file is located.
#
CLIENT_INSTALL=/usr/share/PowerFolder/


############################################
### Don't change anything below this line! #
############################################

BASEDIR=$(dirname $0)

#
# Export variables
#
export CLIENT_INSTALL

#
# Check if the user is not root
#
if [ "$(id -u)" != "0" ]; then
	echo "This script must be run as root!" 1>&2
	exit 1
fi

#
# Action
#

usage()
	{
		echo "Usage: $0 {install|uninstall}"
		exit 1
	}
	[ $# -gt 0 ] || usage

if [ `getconf LONG_BIT` = "64" ]
then
  echo
else
  echo "Running on 32-bit systems are no longer supported. Please run on a 64-bit architecture."
  exit 1
fi

ACTION=$1
case "$ACTION" in
	install)

		read -p "Please close PowerFolder if it is still running and press [Enter] to continue with the installation!"
	
		echo "Installing PowerFolder $DIST_VER"

		if [ ! -d "$CLIENT_INSTALL" ]
		then
			mkdir $CLIENT_INSTALL
		fi
		
		cp --recursive --force --preserve=mode $BASEDIR/jre/ $CLIENT_INSTALL/jre
		cp $BASEDIR/PowerFolder.jar $CLIENT_INSTALL/
		sed 's:CLIENT_INSTALL=.:CLIENT_INSTALL=/usr/share/PowerFolder:g' $BASEDIR/PowerFolder-Client.sh > $CLIENT_INSTALL/PowerFolder-Client.sh
		chmod +x $CLIENT_INSTALL/PowerFolder-Client.sh

		cp -f $BASEDIR/install-files/PowerFolder.png /usr/share/icons/hicolor/128x128/apps/
		cp -f $BASEDIR/install-files/PowerFolder.desktop /usr/share/applications/
		
		if [ ! -d "/usr/share/doc/PowerFolder" ]
		then
			mkdir /usr/share/doc/PowerFolder
		fi
		
		cp -f $BASEDIR/LICENSE /usr/share/doc/PowerFolder/
		cp -f $BASEDIR/README /usr/share/doc/PowerFolder/
		cp -f $BASEDIR/VERSION /usr/share/doc/PowerFolder/
		
		echo "Installing PowerFolder completed!"
	;;

	uninstall)
	
		read -p "Please close all open PowerFolder processes running on your machine and press [Enter] to continue with the uninstallation!"
		
		echo "Uninstall PowerFolder"

		rm -Rf /usr/share/PowerFolder
		rm -Rf /usr/share/doc/PowerFolder
		
		rm /usr/share/applications/PowerFolder.desktop
		rm /usr/share/icons/hicolor/128x128/apps/PowerFolder.png
		
		echo "Uninstall PowerFolder completed!"

	;;
  
	*)
		usage
	;;
esac
	
exit 0
fi
