#!/bin/bash

#Script to uninstall and deactivate installed serialized versions of Acrobat
#This should be used as a reference script for NUD migration to clean previous installs on Acrobat before deploying Acrobat DC on NUD.
#This script will check presence of Acrobat based on their default install location and must be changed if install location is changed.
# The script should be run with admin privileges

acrobatAppPathList=(
	"/Applications/Adobe Acrobat X Pro/Adobe Acrobat Pro.app"
	"/Applications/Adobe Acrobat XI Pro/Adobe Acrobat Pro.app"
	"/Applications/Adobe Acrobat 2015/Adobe Acrobat.app"
	"/Applications/Adobe Acrobat 2017/Adobe Acrobat.app"
	"/Applications/Adobe Acrobat DC/Adobe Acrobat.app"
     )

acrobatLEIDList=(
	"AcrobatPro-AS1-Mac-GM"
	"V6{}AcrobatPro-AS2-Mac-GM"
	"V7{}AcrobatESR-12-Mac-GM"
	"V7{}AcrobatETLA-12-Mac-GM"
	"V7{}AcrobatESR-17-Mac-GM"
	"V7{}AcrobatCont-12-Mac-GM"
     )

# Check sudo access
if [ "$EUID" -ne 0 ]; then
	echo "This script needs admin privileges. Please run it as sudo."
	exit
fi

# Invoke the uninstaller
for ((i = 0; i < ${#acrobatAppPathList[@]}; i++))
do
	appPath=${acrobatAppPathList[$i]}
	printf "\nProcessing $appPath\n"
	if [ -e "$appPath" ]; then
		echo "$appPath found installed."
		if [ -e  "$appPath/Contents/Helpers/Acrobat Uninstaller.app/Contents/Library/LaunchServices/com.adobe.Acrobat.RemoverTool" ]; then
			echo "Removing $appPath now..."
			sudo "$appPath/Contents/Helpers/Acrobat Uninstaller.app/Contents/Library/LaunchServices/com.adobe.Acrobat.RemoverTool" &>/dev/null
			retval=$?
			if [ $retval -ne 0 ]
			then
				echo "Error: Failed to uninstall $appPath"
			else					
				echo "Removed $appPath successfully."
			fi

		elif [ -e "$appPath/Contents/Helpers/Acrobat Uninstaller.app/Contents/MacOS/RemoverTool" ]; then
			echo "Removing $appPath now..."
			sudo "$appPath/Contents/Helpers/Acrobat Uninstaller.app/Contents/MacOS/RemoverTool" "Uninstall" "$appPath" &>/dev/null
			retval=$?
			if [ $retval -ne 0 ]
			then
				echo "Error: Failed to Uninstall $appPath"
			else				
				echo "Removed $appPath successfully."
			fi
        elif [ -e "$appPath/Contents/Support/Acrobat Uninstaller.app/Contents/MacOS/RemoverTool" ]; then
            echo "Removing $appPath now..."
            sudo "$appPath/Contents/Support/Acrobat Uninstaller.app/Contents/MacOS/RemoverTool" "Uninstall" "$appPath" &>/dev/null
            retval=$?
            if [ $retval -ne 0 ]
            then
                echo "Error: Failed to Uninstall $appPath"
            else
                echo "Removed $appPath successfully."
            fi
        elif [ -e "$appPath/Contents/Support/Acrobat Uninstaller.app/Contents/Library/LaunchServices/com.adobe.Acrobat.RemoverTool" ]; then
            echo "Removing $appPath now..."
            sudo "$appPath/Contents/Support/Acrobat Uninstaller.app/Contents/Library/LaunchServices/com.adobe.Acrobat.RemoverTool" "Uninstall" "$appPath" &>/dev/null
            retval=$?
            if [ $retval -ne 0 ]
            then
                echo "Error: Failed to Uninstall $appPath"
            else
                echo "Removed $appPath successfully."
            fi

		else
			echo "Error: Uninstaller not found. Installation seems to be corrupt."
		fi
	else
		echo "$appPath is not installed on this machine."
	fi
done

# Deserialize now
bash_sourcedir=`dirname ${BASH_SOURCE[0]}`
for ((i = 0; i < ${#acrobatLEIDList[@]}; i++))
do	
	if [ -e "$bash_sourcedir/adobe_prtk" ]; then
		echo ${acrobatLEIDList[$i]}
		$bash_sourcedir/adobe_prtk --tool=UnSerialize --leid=${acrobatLEIDList[$i]} --deactivate –-force
	else
		echo "Error: adobe_prtk tool could not be found at path [${PWD}/adobe_prtk]. Please place the adobe_prtk tool next to this script and then run it again."
		exit;
	fi
done

echo "Script execution completed."
