#!/bin/bash

##
# decompress the code
Contents="$2/Adobe Acrobat Reader DC.app/Contents/"
pushd "$Contents" >> /dev/null
	chmod +x decompress
	./decompress
	result=$?
	rm -rf decompress
	popd >> /dev/null
# end of decompression
##

if [ $result != 0 ]; then
	exit $result
fi

# SUCCESS is 0
function default_python_exists()
{
    if ! command -v python &> /dev/null
    then
        return 1
    fi
    return 0
}

# SUCCESS is 0 - when machine OS version >= 12.3
function use_acro_python()
{
    current_os_major_version=$(sw_vers -productVersion 2>&1 | sed 's/\([0-9]*\)\.\([0-9]*\).*/\1/')
    current_os_minor_version=$(sw_vers -productVersion 2>&1 | sed 's/\([0-9]*\)\.\([0-9]*\).*/\2/')
    required_major_version=12
    required_minor_version=3
   if [[
            ($current_os_major_version -gt $required_major_version) ||
            (($current_os_major_version -eq $required_major_version) && ($current_os_minor_version -ge $required_minor_version))
    ]]
    then
        echo "Mac OS version [$mac_os_version] is equal/higher than 12.3."

        if [[ -e "$PYTHON_MARKER_FILE" ]];
        then
            installed_python=`command -v python`
            if ! default_python_exists ; then
                echo "Marker file found, but default python not found [$installed_python]."
            else
                echo "Marker file found, will use default python. [$installed_python]"
            fi
            return 1
        fi

        if [[ -e "$ACRO_PYTHON3_TOOL" ]]; then
            echo "Will use own copy of python3."
            return 0
        fi 
    fi

    return 1
}

function exitWithErrorCode
{
    echo "ERROR: Postinstall exited with error code $1."
    exit $1
}

function url_encode {
        local LC_ALL=C
        url=$1
        len=${#url}
        for (( i = 0 ; i < $len ; i++ )) ; do
            char=${url:i:1}
            case $char in
                [a-zA-Z0-9.-/]) encodedUrl=$encodedUrl$char;;
                *) encodedUrl=$encodedUrl%`echo -n "$char"|xxd -c1 -ps`
            esac
        done
}

ReaderPath=$2
BRANCH_NAME="DC"
BRANCH_NAME_LOWER_CASE=`echo "$BRANCH_NAME" | awk '{print tolower($0)}'`

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

# url encode it
encodedUrl=""
url_encode "$ReaderPath"

# add appropriate prefix and suffix

ReaderUrl="file://localhost$encodedUrl/Adobe%20Acrobat%20Reader%20DC.app/"

# create the plist file for the browser plug-in
defaults write /tmp/com.adobe.reader.pdfviewer.tmp ReaderInstallPath $ReaderUrl

# Install the priveleged tools in the system
ReaderAppPath="$ReaderPath/Adobe Acrobat Reader DC.app"
ProductRegistrationPlistPath="/Library/Application Support/Adobe/ARMDC/Registered Products"
FeatureLockDownInstallPath="/Library/Application Support/Adobe/Reader/DC/Preferences"

if [ ! -e "$ReaderAppPath" ]
then
    echo "ERROR: Application $ReaderAppPath not found.  Installation failure."
    exitWithErrorCode 501
fi

echo "Info: Installing ProductRegistration plist.. "
if [ ! -e "$ProductRegistrationPlistPath" ]
then
    mkdir -pv "$ProductRegistrationPlistPath"
fi


mydir=`pwd`
productRegistrationPlist="$mydir/Resources/com.adobe.reader.$BRANCH_NAME_LOWER_CASE.plist"
serviceRegistrationPlist="$mydir/Resources/com.adobe.reader.servicesupdater.$BRANCH_NAME_LOWER_CASE.plist"
serviceRegistrationPlistDestname="com.adobe.reader.servicesupdater.$BRANCH_NAME_LOWER_CASE.plist"


if [ ! -e "$productRegistrationPlist" ]
then
echo "The registration file for Reader could not be found. Cancelling installation..."
exitWithErrorCode -1
fi

if [ ! -e "$serviceRegistrationPlist" ]
then
    echo "The registration file for Reader service could not be found. Cancelling installation..."
    exitWithErrorCode -1
fi

# write the reader install location to its product-registration plist
sed -in "s|@INSTALL_DIR@|$ReaderPath|g" $productRegistrationPlist
sed -in "s|@INSTALL_DIR@|$ReaderPath|g" $serviceRegistrationPlist

cp -f "$productRegistrationPlist" "$ProductRegistrationPlistPath/"
retval=$?
if [ $retval -ne 0 ]
then
    echo "ERROR: Unable to install ProductRegistration plist for updates"
    exitWithErrorCode 506
fi

echo "Info: Installing FeatureLockDown file... "
if [ ! -e "$FeatureLockDownInstallPath" ]
then
    mkdir -pv "$FeatureLockDownInstallPath"
fi

FeatureLockDownFile="$mydir/Resources/FeatureLockDown"
cp -f "$FeatureLockDownFile" "$FeatureLockDownInstallPath/"
retval=$?
if [ $retval -ne 0 ]
then
    echo "ERROR: Unable to install FeatureLockDown file for updates"
    exitWithErrorCode 506
fi

cp -f "$serviceRegistrationPlist" "$ProductRegistrationPlistPath/$serviceRegistrationPlistDestname"
retval=$?
if [ $retval -ne 0 ]
then
    echo "ERROR: Unable to install Reader Service Registration plist for updates"
    exitWithErrorCode 506
fi


#set the default url handler
$myAlert "TitleReader" "URLSchemeHandler"
retval=$?
if [ $retval -ne 0 ]
then
    echo "Something went wrong in setting the default url scheme handler"
fi


# Adding the ARM entry in /etc/defaults/AdobeARMOverrides.plist file and at /Library/Application Support/Adobe/ARMDC/AdobeARMOverrides.plist file

OVERRIDE_FOLDER_NEW="/Library/Application Support/Adobe/ARMDC"
EXT="plist"
TMP_PLIST_FOLDER="$mydir/Resources"
MERGEPLIST_TOOL="$mydir/Tools/mergeplists.py"
OVERRID_FILE="AdobeARMOverrides"
ACRO_PYTHON3_TOOL="/Library/Application Support/Adobe/Reader/Frameworks/Python3.framework/Versions/3.8/bin/python3"
PYTHON_MARKER_FILE="/Library/Application Support/Adobe/Reader/Frameworks/python_marker"


if [[ ! -e "$MERGEPLIST_TOOL" ]]
then
    echo "ERROR: Merge Plist tool not present."
    exit -1
fi

TMP_OVERRID_FILE="$BRANCH_NAME_LOWER_CASE.ARMOverrides"

echo "Updating AdobeARMOverride.plist for $BRANCH_NAME_LOWER_CASE"
if [ -e "$TMP_PLIST_FOLDER/$TMP_OVERRID_FILE.$EXT" ]
then
    #Check if /Library/Application Support/Adobe/ARMDC folder exists. If not create it
    if [ ! -e "$OVERRIDE_FOLDER_NEW" ]
    then
        mkdir -pv "$OVERRIDE_FOLDER_NEW"
        retval=$?

        if [ $retval -ne 0 ]
        then
            echo "ERROR: Unable to create $OVERRIDE_FOLDER_NEW folder. Exiting "
            exitWithErrorCode -1
        fi
    fi

    echo "Creating AdobeARMOverride.plist file in $OVERRIDE_FOLDER_NEW"

if  use_acro_python ;
then
    "$ACRO_PYTHON3_TOOL" "$MERGEPLIST_TOOL" -i "$TMP_PLIST_FOLDER/$TMP_OVERRID_FILE.$EXT" -o "$OVERRIDE_FOLDER_NEW/$OVERRID_FILE.$EXT" -k "ARM"
else
    "$MERGEPLIST_TOOL" -i "$TMP_PLIST_FOLDER/$TMP_OVERRID_FILE.$EXT" -o "$OVERRIDE_FOLDER_NEW/$OVERRID_FILE.$EXT" -k "ARM"
fi

    if [[ $? -ne 0 ]]
    then
        echo "ERROR: Could not update the AdobeARMOverride file."
        exit -1
    fi

if  use_acro_python ;
then
    "$ACRO_PYTHON3_TOOL" "$MERGEPLIST_TOOL" -i "$TMP_PLIST_FOLDER/$TMP_OVERRID_FILE.$EXT" -o "$OVERRIDE_FOLDER_NEW/$OVERRID_FILE.$EXT" -k "com.adobe.ARMDCHelper"
else
    "$MERGEPLIST_TOOL" -i "$TMP_PLIST_FOLDER/$TMP_OVERRID_FILE.$EXT" -o "$OVERRIDE_FOLDER_NEW/$OVERRID_FILE.$EXT" -k "com.adobe.ARMDCHelper"
fi

    if [[ $? -ne 0 ]]
    then
        echo "ERROR: Could not update the AdobeARMOverride file."
        exit -1
    fi


fi
# This is temp END


#TEMP

#Removing override from /etc/defaults folder

OVERRIDE_ETC_DEFAULTS="/etc/defaults/AdobeARMOverrides.plist"
if [  -e "$OVERRIDE_ETC_DEFAULTS" ]
then
    echo "TEMP:: Removing override from /etc/defaults folder"
    rm -f "$OVERRIDE_ETC_DEFAULTS"
    retval=$?
    if [ $retval -ne 0 ]
    then
        echo "Unable to remove override from /etc/defaults folder."
    fi
fi

#TEMP


echo "SUCCESS: Postinstall exited with error code 0."
exit 0
