# SCENARI app installer script
# LICENCE[[
# Version: MPL 1.1/GPL 2.0/LGPL 2.1/CeCILL 2.O
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is kelis.fr code.
#
# The Initial Developer of the Original Code is
# samuel.monsarrat@kelis.fr
#
# Portions created by the Initial Developer are Copyright (C)  2006
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
#
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# or the CeCILL Licence Version 2.0 (http://www.cecill.info/licences.en.html),
# in which case the provisions of the GPL, the LGPL or the CeCILL are applicable
# instead of those above. If you wish to allow use of your version of this file
# only under the terms of either the GPL, the LGPL or the CeCILL, and not to allow
# others to use your version of this file under the terms of the MPL, indicate
# your decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL, the LGPL or the CeCILL. If you do not
# delete the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL, the LGPL or the CeCILL.
# ]]LICENCE

# Init variables
appName="SCENARIbuilder"
appClientName="scBuilder"
appPrefixDefaultRoot="/opt"
linkPrefixDefaultRoot="/usr"
appPrefixDefaultUser=$HOME
linkPrefixDefaultUser=$HOME
logFile="/tmp/"$appName"-install_"$(date +%Y.%m.%d-%H.%M.%S)".log"
ecoLine=""
# Start...
echo "$appName installer log" >> $logFile
echo " _______________________________________________________________________________"
echo "|"
echo "| Welcome to the $appName installer"
echo "|"

# Check if the script is being run as root or as a simple user.
if [ "$UID" -ne "0" ]
then
	echo "| You do not have root access. $appName will be installed in your"
	echo "| home directory by default."
	installType="user"
	appPrefixDefault=$appPrefixDefaultUser
	linkPrefixDefault=$linkPrefixDefaultUser
else
	echo "|"
	echo "| You have root access. $appName will be installed system wide by default"
	installType="root"
	appPrefixDefault=$appPrefixDefaultRoot
	linkPrefixDefault=$linkPrefixDefaultRoot
fi
echo "!_______________________________________________________________________________"
echo

# Display license.
if [ -f "license.txt" ] ; then 
	echo "Please read and accept the following end user license before"
	echo "installing $appName."
	echo -n "Press Enter to continue"
	read userRes
	more license.txt
	echo -n "Do you agree to the above license terms? [yes/NO] "
	read userRes
	userRes=`echo $userRes | tr 'A-Z' 'a-z'`
	if [ "$userRes" != "y" ] && [ "$userRes" != "yes" ] ; then 
		echo "If you don't agree to the license you can't install $appName."
		echo "Installation aborted, $appName was not installed."
	  	exit 1
	fi
fi

# Look for OpenOffice.
ooLocation=`locate soffice.bin` 
if [ "$ooLocation" = "" ] && [ ! -d "/usr/lib/openoffice/program" ] ; then 
	ooLocation="found"
fi
if [ "$ooLocation" = "" ] && [ ! -d "/usr/lib/openoffice.org/program" ] ; then 
	ooLocation="found"
fi
if [ "$ooLocation" = "" ] && [ ! -d "/usr/lib/openoffice1.9.104/program" ] ; then 
	ooLocation="found"
fi
if [ "$ooLocation" = "" ] && [ ! -d "/usr/lib/openoffice2/program" ] ; then 
	ooLocation="found"
fi
if [ "$ooLocation" = "" ] && [ ! -d "/usr/lib/openoffice.org2.0/program" ] ; then 
	ooLocation="found"
fi
if [ "$ooLocation" = "" ] ; then 
	echo
	echo "WARNING: The installer cannot find OpenOffice on this system."
	echo "Although OpenOffice is not mandatory in order to use $appName,"
	echo "certain features may be affected."
	echo "It is therefore advisable to install OpenOffice (www.openoffice.org)."
fi

# Prompt for install location.
echo
echo -n "Please give the installation location [$appPrefixDefault]: "
read appPrefix
if [ "$appPrefix" = "" ]; then
	appPrefix=$appPrefixDefault
fi
if [ ! -w $appPrefix ]; then
	echo "You do not have write permission for $appPrefix."
	echo "Installation aborted, $appName was not installed."
  	exit 1
fi
appPath=$appPrefix"/"$appName

# Delete previous installation if it exists
if [ -d $appPath ]; then
	echo
	echo "WARNING: $appName is already installed in $appPrefix."
	echo "This previous intallation will be deleted before proceding."
	echo -n "Do you want to continue? [Y/n]: "
	read userRes
	if [ "$userRes" = "N" ] || [ "$userRes" = "n" ]; then 
		echo "Installation aborted, $appName was not installed."
  		exit 1
	fi
	echo -n "Delete previous installation..." >> $logFile
	rm -Rf $appPath >> $logFile 2>&1
	echo "OK" >> $logFile
fi

#==============================================================================
# Unpack the application
#==============================================================================
#Unpack SCx to the right location in appPrefix
echo -n "Unpacking  $appName..."
echo -n "Unpack $appName to $appPath..." >> $logFile
mkdir -p $appPath >> $logFile 2>&1
tar -x -z --no-same-owner --no-same-permissions -f $appName".tar.gz"  -C $appPath >> $logFile 2>&1
#Unpack xulrunner...
tar -x -z --no-same-owner --no-same-permissions -f $appPath"/bin/xulrunner.tar.gz" -C $appPath"/bin/" >> $logFile 2>&1
rm -f $appPath"/bin/xulrunner.tar.gz" >> $logFile 2>&1
echo "OK"
echo "OK" >> $logFile
#Unpack and setup jre if it exists...
if [ -f $appPath"/bin/scServer/jre/jre.tar.gz" ]; then
	echo -n "Unpacking the embedded java runtime environment..."
	echo -n "Unpacking the embedded java runtime environment..." >> $logFile
	tar -x -z --no-same-owner --no-same-permissions -f $appPath"/bin/scServer/jre/jre.tar.gz" -C $appPath"/bin/scServer/jre/" >> $logFile 2>&1
	rm -f $appPath"/bin/scServer/jre/jre.tar.gz" >> $logFile 2>&1
	echo "OK"
	echo "OK" >> $logFile
fi

#==============================================================================
# Setup the application
#==============================================================================
#TEMP workaround to xulrunner bug...
echo -n "TEMP workaround to xulrunner bug: (chmod a+w $appPath/bin/xulrunner)..." >> $logFile
chmod a+w $appPath"/bin/xulrunner" >> $logFile 2>&1
mkdir -p $appPath"/bin/xulrunner/updates/0" >> $logFile 2>&1
echo "OK" >> $logFile

#Link the java plugin to xulrunner (in order for applets to work in SCENARI)...
if [ -d $appPath"/bin/scServer/jre" ]; then
	ln -s $appPath"/bin/scServer/jre/plugin/i386/ns7/libjavaplugin_oji.so" $appPath"/bin/xulrunner/plugins/libjavaplugin_oji.so" >> $logFile 2>&1
fi

#Replace ==appPath== in launcher script
echo -n "Set up launcher script..." >> $logFile
appPathEsc=$(echo $appPath | sed -e 's/\//\\\//g')
sed -e "s/==app.path==/$appPathEsc/g" < $appPath"/"$appName > $appPath"/"$appName".tmp"
rm -f $appPath"/"$appName >> $logFile 2>&1
mv $appPath"/"$appName".tmp" $appPath"/"$appName >> $logFile 2>&1

#Set user rights
chmod a+x $appPath"/"$appName >> $logFile 2>&1
chmod a+x $appPath"/bin/scServer/nativlib/lame" >> $logFile 2>&1

echo "OK" >> $logFile

echo "$appName has been installed in $appPath"

#==============================================================================
# Setup the enviroment
#==============================================================================
# Enviroment integration (links menus etc)
echo "Enviroment integration..." >> $logFile
echo
echo -n "Do you want incorprate $appName into your enviroment? [Y/n]: "
read userRes
if [ "$userRes" = "" ] || [ "$userRes" = "y" ] || [ "$userRes" = "Y" ] ; then 
	# Add links
	if [ "$installType" = "root" ] ; then
		echo -n "Please specify the path prefix for your system [$linkPrefixDefault]: "
		read linkPrefix
		if [ "$linkPrefix" = "" ] ; then
			linkPrefix=$linkPrefixDefault
		fi
	else
		linkPrefix=$linkPrefixDefault 
	fi
	rm -f $linkPrefix"/bin/"$appName >> $logFile 2>&1
	ln -s $appPath"/"$appName $linkPrefix"/bin/"$appName >> $logFile 2>&1
	echo "The link $linkPrefix/bin/$appName has been added"
	echo "The link $linkPrefix/bin/$appName has been added" >> $logFile

	# Add menu item if possible (root and existing menu structure)...
	if [ "$installType" = "root" ] && [ -d $linkPrefix"/share/applications" ]; then
		appDesktopFilePath=$linkPrefix"/share/applications/"$appName".desktop"
		echo "[Desktop Entry]" > $appDesktopFilePath
		echo "Name=$appName" >> $appDesktopFilePath
		echo "Encoding=UTF-8" >> $appDesktopFilePath
		echo "Exec=$appName" >> $appDesktopFilePath
		echo "Terminal=false" >> $appDesktopFilePath
		echo "StartupNotify=true" >> $appDesktopFilePath
		echo "Icon=$appPath/bin/$appClientName/chrome/icons/default/scenaricms.png" >> $appDesktopFilePath
		echo "Type=Application" >> $appDesktopFilePath
		echo "Categories=Office;" >> $appDesktopFilePath
		chmod 644 $appDesktopFilePath  >> $logFile 2>&1

		echo "$appName has been added to the Office sub-menu of the Applications menu."
		echo "The file $appDesktopFilePath has been created" >> $logFile
	fi
else
	echo "$appName has not been incorporated into your enviroment"
fi
echo
echo "$appName installation finished"
echo "$appName installation finished" >> $logFile
