#!/bin/sh
#
# UnInstall Script for UNIX Acrobat Exchange and Reader v. 8.1.7
#
# Copyright (c)1995-2007 Adobe Systems Incorporated
# All Rights Reserved
#
##########################################################################


set_lang_DEU_utf()
{
    ST_ER_AR02="FEHLER: Aktuelles Installationsverzeichnis kann nicht gefunden werden."
    ST_ER_SAME_FILE_NAME_AS_DIR="%s ist vorhanden, jedoch kein Verzeichnis. Geben Sie einen anderen Speicherort an."
    ST_UNINSTALL_SUCCESS="Adobe Reader wurde von Ihrem System entfernt."
}


set_lang_DEU()
{
    ST_ER_AR02="FEHLER: Aktuelles Installationsverzeichnis kann nicht gefunden werden."
    ST_ER_SAME_FILE_NAME_AS_DIR="%s ist vorhanden, jedoch kein Verzeichnis. Geben Sie einen anderen Speicherort an."
    ST_UNINSTALL_SUCCESS="Adobe Reader wurde von Ihrem System entfernt."
}

if echo $LANG | grep -i utf >/dev/null; then
    set_lang_DEU_utf
else
    set_lang_DEU
fi

MkTempInternal()
{
    if [ "${mktemp_count+set}" != "set" ]; then
        mktemp_count="0"
    fi

    mktemp_file="/tmp/acrobat.$$.${mktemp_count}"

    while /usr/bin/test -e "$mktemp_file"
    do
        mktemp_count="`expr $mktemp_count + 1`"
        mktemp_file="/tmp/acrobat.$$.${mktemp_count}"
    done

    touch "$mktemp_file" && chmod 600 "$mktemp_file" && echo "$mktemp_file"
}

MkTemp()
{
    template="tmp.XXXXXXXXXX";
    MKTEMP="`which mktemp 2>/dev/null`";
    if [ "$MKTEMP" != "" ]; then
	    :
    else
	    MKTEMP="MkTempInternal";
    fi

    $MKTEMP /tmp/acrobat.$template
}


AbsolutePath()
{
    OLDPWD="$PWD"
    file="$1"

    if [ -d "$file" ]; then
        cd "$file"
        /bin/pwd
    else
        cd "`dirname "$file"`"
        echo "`/bin/pwd`/`basename "$file"`"
    fi

    cd "$OLDPWD"
}


Escape_Quotes()
{
    echo $@ | sed -e 's/\\/\\\\/g' -e 's/\$/\\\$/g' -e 's/"/\\"/g' \
                  -e "s/'/\\'/g" -e 's/\^/\\\^/g' -e 's#/#\\/#g'
}


TrimSpaces()
{
    OLD_IFS_Trim_Spaces="$IFS"
    IFS=' '
    echo $@
    IFS="$OLD_IFS_Trim_Spaces"
}


remove()
{
    file="$@"

    if [ -d "$file" ]; then
        relative_directory="`echo $file | sed -ne "s#${ReadInstallDir}/*##p"`"
        if [ -n "$relative_directory" ]; then
            oldpwd="`pwd`"
            cd "$ReadInstallDir"
            rmdir -p "$relative_directory"
            if [ -d "$oldpwd" ]; then
                cd "$oldpwd"
            fi
        fi
    else
        rm -f "$file"
    fi
}


uninstall_icon()
{
    PATH="$PATH:$ReadInstallDir/Adobe/Reader8/Reader/intellinux/bin" xdg-icon-resource uninstall "$@"
}


uninstall_desktop_menu()
{
    PATH="$PATH:$ReadInstallDir/Adobe/Reader8/Reader/intellinux/bin" xdg-desktop-menu uninstall "$@"
}


uninstall_desktop_icon()
{
    PATH="$PATH:$ReadInstallDir/Adobe/Reader8/Reader/intellinux/bin" xdg-desktop-icon uninstall "$@"
}


uninstall_mime()
{
    PATH="$PATH:$ReadInstallDir/Adobe/Reader8/Reader/intellinux/bin" xdg-mime uninstall "$@"
}


ProcessConfigFile()
{
    OLD_IFS="$IFS"
    config_file="$1"
    IFS=' '

    tmpfile="`MkTemp`"
    mv -f "$config_file" "$tmpfile"

    cat "$tmpfile" | grep -v "^[ ]*$" | \
    while read op args
    do
        op="`TrimSpaces $op`"

        case "$op" in
            remove | uninstall_icon | uninstall_desktop_menu | uninstall_mime | uninstall_desktop_icon)
                "$op" $args 2>/dev/null ;;

            *)
                echo "Unknown operation: $op" ;;
        esac
    done

    rm -f "$tmpfile"
    IFS="$OLD_IFS"
}


TypeCheck() 
{
  h=`type $1 2> /dev/null`
  case " $h " in
  *' '/*/$1' '*)
    return 0
    ;;
  *)
    return 1
    ;;
  esac
}


ExpandDirPath ()
{
    var=`echo $@ |  cut -d "\"" -f 2`

    case "$var" in
    ../*|./*|..|.)
      var="`/bin/pwd`/${var}"
      ;;
    *)
      ;;
    esac

    eval echo "${var}"
}


KillSynchronizerApp()
{
    tmpfile="`MkTemp`"
    ps -ef > "$tmpfile" 2>/dev/null
    synchronizerapp="`AbsolutePath "$1"`"
    synchronizerapp_pid=`awk '{ print $8, $2 }' < "$tmpfile" | grep -w "^$synchronizerapp" | awk '{ print $NF; exit }'`

    if [ -z "$synchronizerapp_pid" ]; then
        ps auwwwxg 2>/dev/null > "$tmpfile"
        synchronizerapp_pid=`awk '{ print $11, $2 }' < "$tmpfile" | grep -w "^$synchronizerapp" | awk '{ print $NF; exit }'`

        if [ -z "$synchronizerapp_pid" ]; then
            rm -f "$tmpfile"
            return 0;
        fi
    fi

    rm -f "$tmpfile"

    if [ -n "$synchronizerapp_pid" ]; then
        kill -KILL "$synchronizerapp_pid" >/dev/null 2>&1
    fi
}

UnInstallReader()
{
    install_dir="`ExpandDirPath "$1"`"
    config_file="$install_dir/Reader/GlobalPrefs/.config"


    if [ -f "$install_dir" ]; then
        printf "$ST_ER_SAME_FILE_NAME_AS_DIR" "\"$install_dir\""
        exit 1
    fi

    if [ ! -d "$install_dir" ]; then
        printf "%s\n" "$ST_ER_AR02"
        exit 1
    fi

    cd "$install_dir"

    ## kill SynchronizerApp
    case "$OSname" in
     "Linux")
        ACRO_CONFIG="intellinux" ;;
     "SunOS")
        ACRO_CONFIG="sparcsolaris" ;;
     "HP-UX")
        ACRO_CONFIG="hppahpux" ;;
     "AIX")
        ACRO_CONFIG="rs6000aix" ;;
    *)
        ACRO_CONFIG="none" ;;
    esac

    synchronizer_app="$install_dir/Reader/$ACRO_CONFIG/bin/SynchronizerApp"
    KillSynchronizerApp "$synchronizer_app"

    if [ -f "$config_file" ]; then
        ProcessConfigFile "$config_file"
        rm -f "$config_file"
    fi

    if [ "`uname -s`" = Linux ]; then
        PATH="$PATH:$ReadInstallDir/Adobe/Reader8/Reader/intellinux/bin" xdg-icon-resource forceupdate >/dev/null 2>&1
    fi

    return 0
}


##############################################################

#
# Initialization:
#

umask 022

ScriptName="`basename "$0"`"
CurrentDirectory="`/bin/pwd`"
eval CurrentDirectory=\"${CurrentDirectory}\"

ScriptDirectory=`dirname "$0"`
eval ScriptDirectory=\"${ScriptDirectory}\"

case "${ScriptDirectory}" in
  /*) ;;
  .) ScriptDirectory="$CurrentDirectory" ;;
  *) ScriptDirectory="$CurrentDirectory"/"$ScriptDirectory" ;;
esac

ReadInstallDir="`dirname "$ScriptDirectory"`/../../"
ReadInstallDir="`AbsolutePath "$ReadInstallDir"`"
export ReadInstallDir

TypeCheck uname

if [ $? -eq 0 ] ; then
    OSname=`uname -s`
    if [ "$OSname" = "AIX" ] ; then
        OSrelease=`uname -a | ( read name host minor major foo ; echo $major.$minor )`
    else
        OSrelease=`uname -r`
    fi
else
    OSname=unknown
    OSrelease=unknown
fi


Version=8.1.7

#
# Restore the files replaced during installation
#

UnInstallReader "$ReadInstallDir/Adobe/Reader8"

if [ $? -eq 0 ]; then
    printf "%s\n" "$ST_UNINSTALL_SUCCESS"
fi

printf "\n"
exit 0
