#!/bin/bash
##
# decompress: run from the postinstall script to decompress the 7z archives
##
# track the 7zz status and return it at the end of the script
## Nikgupta: 12/01/2023: Use 7zz's Fat binary (intel and ARM). ADC-4378271
result=0

path_app="$(dirname "$PWD")" 

##
# [PSIRT-23307] / ADC-4380659: Check the valdiity of any presexisting Adobe Reader bundle. 
# Return true [1], only if any preexisting Reader app is root owned and a directory- other wise return 0. This is because we always create a fresh Adobe Reader bundle in full installer and this is created with root user.
function validateFolderOwnership
{	
	if [[ -d "$1" ]]
	then
		stat_info=`stat  "$1"`
		fileOwnerName="$(echo $stat_info | awk '{ print $5 }')"
		filePermission="$(echo $stat_info | awk '{ print $3 }')"

		if [[ "$fileOwnerName" == "root" ]] 
		then  
			if [[ "$filePermission" == d* ]] && [[ ${filePermission: -2:1} == - ]]  ; then return 1; fi
		fi

	fi 
	return 0
}

validateFolderOwnership "$path_app"
retval=$?
if [ $retval -ne 1 ]
then
	"ERROR: Adobe Reader app bundle has unexpected permissions. Installer will quit with ERROR."
	exit -1
fi
##
# extract the (de) compressor
Z=7zz
rm -rf setX.txt    # remove old copy of setX.txt
rm -rf $Z          # remove old copy of 7zz
unzip  $Z.zip      # unzip a new copy of 7zz

##
# decompress the folders (and remove the compressed archives)
for folder in MacOS AcroCEF Plugins Helpers Frameworks Plug-ins Resources Services Support; do
	if [ $result == 0 ]; then 
		echo decompressing $folder/
		./$Z  x -y -snld $folder.7z	# extracts $folder/ from $folder.7z
		result=$?				# remember if we failed!
		rm -rf     $folder.7z	# remove the compressed archive
        if [ "$folder" = "MacOS" ]; then
            echo "Will touch the app icon"
            touch "$path_app" # touch Reader app-  upon extracting MacOs contents , we refresh its icon
        fi
	fi
done

##
# set the permissions (as prescribed by Apple)
find . -name "*" -type d -depth                    -exec chmod 0775 {} ";"
find . -name "*" -type f -depth                    -exec chmod 0664 {} ";"

# 2684939 and 2690875.  Set the X bit by reading setX.txt
{
	while read filename; do
		if [ -e "$filename" ]; then 
  			chmod 775 "$filename"
		fi
	done
} < "setX.txt"

##
# 2717261
chown root MacOS
chown root Resources


##
# cleanup:  remove the compressor (postinstall removes -this- script)
rm -rf setX.txt    # remove old copy of setX.txt
rm -rf $Z          # remove old copy of 7zz
rm -rf $Z.zip

exit $result

# That's all Folks!
##
