#!/bin/bash

function MoveReaderToTrash {

	newReaderApp=$readerApp
	while [ 1 ]
	do
		if [ -e ~/.Trash/"$newReaderApp" ]
		then
			echo "$newReaderApp exists in Trash. Retrying with a new name."
			sleep 1
			time_to_be_attached=`date "+%k-%M-%S"`
			newReaderApp="Adobe Reader""$time_to_be_attached"".app"
		else
			echo "Moving existing $readerApp to Trash as $newReaderApp"
			mv "$installation_path/$readerApp" ~/.Trash/"$newReaderApp"
			if [ $? -ne 0 ]
			then
				echo "ERROR: Could not move existing $readerApp to Trash."
				exit 0    #ashkuma: allowing installation to proceed even if MoveToTrash fails. 
			else
				exit 0
			fi
		fi
	done	
}

mydir=`pwd`
myAlert="$mydir/Tools/AcroInstallAlert.app/Contents/MacOS/AcroInstallAlert"

if [ ! -e $myAlert ]
then
	echo "ERROR: $myAlert does not exist. Installation failure."
	exit -1
fi

CLI_mode=0	#0 indicates GUI, 1 indicates CLI installation
if [ $COMMAND_LINE_INSTALL 2>/dev/null ]
then
	CLI_mode=1
fi

if [ $CLI_mode -eq 1 ]
then
	$myAlert TitleReader CheckAppRunning $CLI_mode
	retval=$?		
else
	while [ 1 ]
	do
		$myAlert TitleReader CheckAppRunning $CLI_mode
		retval=$?

		if [ $retval -ne 1 ]
		then
			break
		fi
	done
fi

if [ $retval -eq 2 ]	#if Cancel is clicked in alert in GUI installation
then
	echo "User cancelled the installation."
	exit -1
fi

if [ $retval -eq 255 ]	#this is logged in CLI installation only
then
	echo "Adobe Acrobat  or Adobe Acrobat Reader were found running."
	exit -1
fi

installation_path=$2

currentMajor="23"
currentFirstMinor="001"
currentSecondMinor="20177"
readerApp="Adobe Acrobat Reader DC.app"
readerAppDCMoniker="Adobe Acrobat Reader.app"
installed_version_higher="false"
installedVersion="23.001.20177"
if [ -e "$installation_path/$readerAppDCMoniker" ]
then
	readerApp=$readerAppDCMoniker		
fi

if [ -e "$installation_path/$readerApp" ]
then
	if [ -e "$installation_path/$readerApp/Contents/Info.plist" ]
	then
		installedVersion=`defaults read "$installation_path/$readerApp/Contents/Info" CFBundleVersion`
		major=`echo $installedVersion | cut -d "." -f 1`
		
		if [ $major -eq $currentMajor ]
		then
			firstMinor=`echo $installedVersion | cut -d "." -f 2`
			
			if [ $currentFirstMinor -eq $firstMinor ]
			then
				secondMinor=`echo $installedVersion | cut -d "." -f 3`
				
				if [ $currentSecondMinor -eq $secondMinor ]
				then
					exit 0
				else
					if [ $currentSecondMinor -lt $secondMinor ]
					then
						installed_version_higher="true"
					else
						installed_version_higher="false"
					fi
				fi
			else
				if [ $currentFirstMinor -lt $firstMinor ]
				then
					installed_version_higher="true"
				else
					installed_version_higher="false"
				fi
			fi
		else
			if [ $currentMajor -lt $major ]
			then
				installed_version_higher="true"
			else
				installed_version_higher="false"
			fi
		fi
	fi
else
    #check for the upgrade scenario and remove the old app if present
    $myAlert TitleReader Upgrade
    if [ $? -ne 0 ]; then
        echo "ERROR: Could not remove the old copy of Reader."
        exit -1
    else
        exit 0
    fi
fi

if [ $CLI_mode -eq 1 ]
then
	if [ $installed_version_higher == "true" ]
	then
		echo "ERROR: Existing $readerApp at $installation_path has a higher version: $installedVersion. Installation failure."
		exit -1	
	else
		echo "Existing $readerApp at $installation_path has a lower version: $installedVersion. Moving it to Trash."
		MoveReaderToTrash
	fi
else
	if [ $installed_version_higher == "true" ]
	then
		echo "ERROR: existing $readerApp at $installation_path has a higher version: $installedVersion."		
	else
		echo "Existing $readerApp at $installation_path has a lower version: $installedVersion."
	fi

	mydir=`pwd`
    myAlert="$mydir/Tools/AcroInstallAlert.app/Contents/MacOS/AcroInstallAlert"
		
	if [ -e $myAlert ]
	then
		if [ $installed_version_higher == "true" ]
		then			
			$myAlert TitleReader ReaderExistsHigher
		else
			$myAlert TitleReader ReaderExistsLower
			retval=$?
	
			if [ $retval -eq 0 ]
			then
				echo "User chose to trash the existing Reader."
				MoveReaderToTrash
			else
				echo "User chose to retain the existing Reader. Canceling installation."
				exit -1
			fi
		fi
	else
		echo "ERROR: $myAlert not found. Cannot confirm movement of $readerApp to Trash. Canceling installation."
		exit -1
	fi
fi
