#!/bin/bash

echo -e "\n"
echo Setting autoconfiguration for SDA installer

echo "started in `pwd`"
cd `dirname "$0"`
echo "changed directory to `pwd`"

if [ -z "$TMP"]
then
	export TMP="$TEMP"
fi

if [ -z "$TMP"]
then
	export TMP="$TMPDIR"
fi

if [ -z "$TMP"]
then
	export TMP="/tmp"
fi

export SRC_PROPERTIES_FILE="$(pwd)/SIERRA_DESKTOP_APP_RUN.properties"
export DEST_PROPERTIES_FILE="$TMP/SIERRA_DESKTOP_APP_RUN.properties"

echo -e "\n"
echo Using variables:
echo "SRC_PROPERTIES_FILE - $SRC_PROPERTIES_FILE"
echo "DEST_PROPERTIES_FILE - $DEST_PROPERTIES_FILE"

#STEP 1 - check if source properties file is founded
echo -e "\n"
echo "Checking if autoconfiguration properties file exists $SRC_PROPERTIES_FILE..."
if [[ -f "$SRC_PROPERTIES_FILE" ]]
then
	echo "Source config file exists"
else
	echo "Source config file does not exist. Check if you have successfully extracted archive and location of SIERRA_DESKTOP_APP_RUN.properties in the same directory as this script"
	read -p "Error has occured. Press enter to finally close script..."
	exit 1
fi

#STEP 2 - check if destination properties file is founded
echo -e "\n"
echo "Checking if autoconfiguration properties files has already been added to be used by installer..."
if [[ -f "$DEST_PROPERTIES_FILE" ]]
then
	echo "Properties have already been set to be used by installer in $DEST_PROPERTIES_FILE"
	echo "Properties will be overrided!!!"
	read -p "Press enter if you agree to override current SDA installer autoconfiguration properties or close script if you dont want to"
else
	echo "No previous installer autoconfiguration files found"
fi


#STEP 3 - copying property file in temp directory
echo -e "\n"
echo "Copying file to be used by SDA installer..."
echo "Copying $SRC_PROPERTIES_FILE to $SRC_PROPERTIES_FILE"
cp "$SRC_PROPERTIES_FILE" "$DEST_PROPERTIES_FILE"
if [ "$?" -eq 0 ]
then
	echo "All operations have been executed succesfully. SDA installer autoconfiguration is done"
	echo "Note: Autoconfiguration could be removed by the system in some time"
else
	echo "Unexpected error happened during copying file"
	echo "Try to relaunch script or to copy file $SRC_PROPERTIES_FILE to $DEST_PROPERTIES_FILE manually"
	read -p "Error has occured. Press enter to finally close script..."
	exit 1
fi


#STEP 4 - start installer
echo -e "\n"
echo "Starting SDA installer..."
./install.bin
