#!/bin/csh -f
#***********************************************************************************
#*                                                                                 *
#*  uninstall-acroread - Linux Fedora Uninstaller for Adobe Acrobat Reader 9.5.5-1 *
#*                                                                                 *
#***********************************************************************************
#
# 

onintr CLEAN_UP

set INSTALL_DIR = "M_INSTALL_DIR"

set MY_NAME = "${INSTALL_DIR}/uninstall-acroread"

set TMP_DIR  = /tmp/Acroread_47_tmp_dir.$$
set TMP_FILE = /tmp/Acroread_47_tmp_file.$$

umask 022

alias check_yes 'if ($ANSWER == y || $ANSWER == Y || $ANSWER == yes || $ANSWER == YES) set ANSWER = y'
alias check_no  'if ($ANSWER == n || $ANSWER == N || $ANSWER == no  || $ANSWER == NO ) set ANSWER = n'

alias check_answer 'check_yes ; check_no'

# --------------------------------------------------- #
# First make sure we are running this script as root. #
# --------------------------------------------------- #

if ( -x /usr/bin/whoami ) then
	if ( `/usr/bin/whoami` != "root" ) then
		echo ""
		echo "*** ERROR: You must be root to run this script."
		echo "Please change your user id to root. Then try again."
		goto CANCEL
	endif
else
	echo ""
	echo "*** ERROR: Could not find the command: /usr/bin/whoami"
	echo "Please install /usr/bin/whoami. Then try again."
	goto CANCEL
endif

# -----------------------------------------------------------------------------
echo ""
echo "Acrobat Reader will be removed (uninstalled) from ${INSTALL_DIR}"
echo ""

set ANSWER = x
while ($ANSWER != y && $ANSWER != n)
	echo -n "Continue (y/n)? "
	set ANSWER = $<
	check_answer
end
if ($ANSWER == n) then
	echo ""
	echo "OK. Uninstallation cancelled."
	goto CANCEL
endif

echo ""
echo "OK. Continuing with uninstallation ..."
echo ""
echo "Removing ${INSTALL_DIR}/Reader9 ..."
sleep 1


# ---------------------------------------------------------
chmod 777 ${INSTALL_DIR}/Reader9               >& /dev/null
chmod 777 ${INSTALL_DIR}/Reader9/*             >& /dev/null
chmod 777 ${INSTALL_DIR}/Reader9/*/*           >& /dev/null
chmod 777 ${INSTALL_DIR}/Reader9/*/*/*         >& /dev/null
chmod 777 ${INSTALL_DIR}/Reader9/*/*/*/*       >& /dev/null
chmod 777 ${INSTALL_DIR}/Reader9/*/*/*/*/*     >& /dev/null
chmod 777 ${INSTALL_DIR}/Reader9/*/*/*/*/*/*   >& /dev/null
chmod 777 ${INSTALL_DIR}/Reader9/*/*/*/*/*/*/* >& /dev/null
rm -f -r  ${INSTALL_DIR}/Reader9
# ---------------------------------------------------------
if ( -e ${INSTALL_DIR}/Reader9 ) then
	echo ""
	echo "*** ERROR: Could not remove ${INSTALL_DIR}/Reader9"
	echo "Please check the permissions of the directory, then try again."
	echo ""
	echo "Uninstallation cancelled."
	goto CANCEL
endif

# ------------------------------------------------------------------
# Skipping (not removing):
#
# /usr/bin/gvfs-*
#
# /usr/local/share/applications/defaults.list
#
# /usr/share/mime/application/vnd.*.xml
# /usr/share/mime/application/vnd.adobe.pdx.xml
# /usr/share/mime/application/vnd.adobe.xdp+xml.xml
# /usr/share/mime/application/vnd.adobe.xfdf.xml
# /usr/share/mime/application/vnd.fdf.xml
#
# /usr/share/mime/types
# /usr/share/mime/aliases
# 
# /usr/share/icons/hicolor/*/mimetypes/application-fdf.png
# /usr/share/icons/hicolor/*/mimetypes/application-pdf.png
# /usr/share/icons/hicolor/*/mimetypes/application-pdx.png
# /usr/share/icons/hicolor/*/mimetypes/application-xdp+xml.png
# /usr/share/icons/hicolor/*/mimetypes/application-xfdf.png
# 
# /usr/share/icons/hicolor/*/mimetypes/gnome-application-fdf.png
# /usr/share/icons/hicolor/*/mimetypes/gnome-application-pdf.png
# /usr/share/icons/hicolor/*/mimetypes/gnome-application-pdx.png
# /usr/share/icons/hicolor/*/mimetypes/gnome-application-xdp+xml.png
# /usr/share/icons/hicolor/*/mimetypes/gnome-application-xfdf.png
# ------------------------------------------------------------------

# ---------------------------
# Set list of files to remove
#

# ----------------------------------------------------- #
set FILE_LIST = (										\
														\
	/usr/bin/acroread									\
	/usr/share/mime/packages/AdobeReader.xml			\
	/usr/lib/mozilla/plugins/nppdf.so					\
	/usr/lib64/mozilla/plugins/nppdf.so					\
	/usr/lib/opera/plugins/nppdf.so						\
														\
	/etc/bash_completion.d/acroread.sh					\
	/usr/share/man/man1/acroread.1.gz					\
	/root/Desktop/AdobeReader.desktop					\
	/usr/local/share/applications/AdobeReader.desktop	\
														)
# ----------------------------------------------------- #

# ---------------------------------------------------------------
# Here we could use the :q variable modifier to be able to handle
# a file_list with wild cards, like: 'libidn*'. See this article:
#
# https://stackoverflow.com/questions/30849512/
# cshell-cannot-set-array-of-strings-with-spaces
#
# However, in this case we don't have to use the :q modifier
# bec ause this list does not include any wild cards.
# ---------------------------------------------------------------

# foreach file ( $FILE_LIST:q )

foreach file ( $FILE_LIST )

	echo "Removing ${file} ..."

	# Right here we are redirecting output to /dev/null because chmod on
	# /usr/bin/acroread (which is a sym-link) creates  the error message:
	#
	# "chmod: cannot operate on dangling symlink '/usr/bin/acroread'"
	#
	chmod 777 ${file} >& /dev/null
	rm -f     ${file}
	if ( -e ${file} ) then
		echo ""
		echo "*** ERROR: Could not remove ${file}"
		echo "Please check the permissions of the file, then try again."
		echo ""
		echo "Uninstallation cancelled."
		goto CANCEL
	endif

end

# ---------------------------------------------------------------------
# Right here, one could think that there is a potential race condition:
#
# We are removing this script (the uninstaller itself) but we are still
# executing a few more lines in this script before it exits. How can we
# make sure that the script doesn't disappear before it completes and
# exits?
#
# Fortunately, the script is loaded into memory so the instructions
# following the 'rm' statement are still in memory and can be executed.
# Moreover, the rm command just unlinks the file, but the file remains
# open and available as long as the shell has it opened. When the shell
# exits, then the blocks of the file are available to the system. See:
#
# https://stackoverflow.com/questions/7834667/self-deleting-bash-script
#
# ---------------------------------------------------------------------
echo "Removing ${MY_NAME} ..."
chmod 777 ${MY_NAME}
rm -f ${MY_NAME}

REALLY_DONE:

	echo ""
	echo "Done."
	echo ""
	exit 0

# Error Exit:
#
ERROR_EXIT:

	cd /
	chmod 777 $TMP_DIR  >& /dev/null
	rm -f -r  $TMP_DIR  >& /dev/null

	chmod 777 $TMP_FILE >& /dev/null
	rm -f     $TMP_FILE >& /dev/null

	exit 1

CLEAN_UP:

	echo ""
	echo "Received interrupt, cleaning up ..."
	echo ""

CANCEL:

	cd /
	chmod 777 $TMP_DIR  >& /dev/null
	rm -f -r  $TMP_DIR  >& /dev/null

	chmod 777 $TMP_FILE >& /dev/null
	rm -f     $TMP_FILE >& /dev/null

	echo ""
	exit 1

# We will never get here

exit 0
          
# === End of uninstall-acroread

