#!/bin/bash

#-----------------------------------------------------------

#------set args for bash: installUtil file

#-----------------------------------------------------------

script_name="$1"/Contents/Resources/installerUtil

installPath="$2"

productName="Sametime"

framwork_path="Contents/MacOS"

install_customization_path="$1"/Contents/Resources/install_customization.properties

vmargs="-Xdock:icon=\$\{rcp.home\}/../Resources/Sametime.icns"

#----------------------------------------------------------

#------upgrade: reuse previous workspace

#----------------------------------------------------------

# check if app already exists. if so, assume upgrade
INSTALLPATH="$2/${productName}.app"
RCPLAUNCHERPROPERTIES="$INSTALLPATH/$framwork_path/rcp/rcplauncher.properties"
 
if [ -e "$RCPLAUNCHERPROPERTIES" ]; then
  echo "Sametime upgrade detected. Reusing existing workspace" | logger -t Sametime 
  # get existing rcp.data from rcplauncher.properties
  OLD_RCPDATA=`grep '^rcp.data=' "$RCPLAUNCHERPROPERTIES" | tail -n 1 | awk -F= '{ print $2 }'`
  if [ "${OLD_RCPDATA}" = "" ]; then
    echo "rcp.data missing from ${RCPLAUNCHERPROPERTIES}" | logger -t Sametime 
    exit 97
  fi

  # replace RCPDATA in install_customization.properties
  INSTALL_CUSTOMIZATION_TMP=`mktemp /tmp/install_customization.ini.XXXXXXX`
  if [ $? -ne 0 ]; then
    echo "could not create install_customization.ini tmp file in /tmp" | logger -t Sametime 
    exit 98
  fi
  echo "temporary install_customization.ini file = ${INSTALL_CUSTOMIZATION_TMP}" | logger -t Sametime 
  arg=`echo "${OLD_RCPDATA}" | sed 's:[]\[\^\$\.\*\/]:\\\\&:g'`
  sed 's/RCPDATA=.*/'RCPDATA="$arg"'/g' "$install_customization_path" > $INSTALL_CUSTOMIZATION_TMP
  if [ $? -ne 0 ]; then
    echo "error replacing RCPDATA in ${INSTALL_CUSTOMIZATION_TMP}" | logger -t Sametime 
    exit 99
  fi
  
  # use the new tmp install_customization instead of the default one
  install_customization_path=$INSTALL_CUSTOMIZATION_TMP
  echo "Upgrade function complete" | logger -t Sametime 
fi; 


#-----------------------------------------------------------

#------upgrade: rename old version

#-----------------------------------------------------------



if [ -e "$2/${productName}.app" ]; then

	guid=`date "+%Y%m%d%H%M%S"`

	mv "$2/${productName}.app" "$2/.${productName}.app.DELETEME.${guid}"
fi


#-----------------------------------------------------------

#------run installUtil script with input args

#-----------------------------------------------------------



sh "$script_name"  "$productName" "$installPath" "$framwork_path" "$install_customization_path"



#-----------------------------------------------------------

#--- Create Sametime runnable file under :

#---  ${Sametime.app}/Contents/MacOS/Sametime

#

#--- This runnable file simply call rcplaucher

#-----------------------------------------------------------

# bsc - replacing with native launcher
#mkdir -p "$2/$productName.app/Contents/MacOS/"

#echo "#!/bin/bash"                                                          > "$2/$productName.app/Contents/MacOS/$productName"

#echo "\"$2/$productName.app/$framwork_path/rcp/rcplauncher\" -vmargs $vmargs"  >> "$2/$productName.app/Contents/MacOS/$productName"

#echo  "exit 0"                                                             >> "$2/$productName.app/Contents/MacOS/$productName"

#chmod 777                                                                     "$2/$productName.app/Contents/MacOS/$productName"



#-----------------------------------------------------------

#------The End

#-----------------------------------------------------------

exit 0

