#!/bin/bash

# These are the standard browser locations as found within Red Hat, Mandrake
# and SuSE Linux, and the tarball installers.
if [ $1 == "32" ]; then 
	LOCATIONS="/usr/lib/mozilla /usr/lib/mozilla-* /usr/lib/firefox-* /usr/lib/seamonkey-* /usr/lib/netscape /usr/lib/opera /usr/lib/firefox /usr/local/netscape /usr/local/mozilla /usr/local/firefox /usr/local/seamonkey /opt/mozilla /opt/netscape /opt/firefox /opt/seamonkey" 
LIBDIR=lib
else
	LOCATIONS="/usr/lib64/mozilla /usr/lib64/mozilla-* /usr/lib64/firefox-* /usr/lib64/seamonkey-* /usr/lib64/netscape /usr/lib64/opera /usr/lib64/firefox /usr/local/netscape /usr/local/mozilla /usr/local/firefox /usr/local/seamonkey /opt/mozilla /opt/netscape /opt/firefox /opt/seamonkey" 
LIBDIR=lib64
fi

deleteold() {
# Detect, Backup and Delete old global Flash plugins
# Old plugin files are saved in /root/oldflashplugins.tar.gz
# tar and gzip must be installed
if [ ! -f /root/oldflashplugins.tar.gz ]; then
    FILES="libflashplayer.so ShockwaveFlash.class flashplayer.xpt libgnashplugin.so"
    for DIR in $LOCATIONS
    do
        # Skip symlinks
        if [ -h $DIR ]; then continue; fi

        for F in $FILES
        do
            # Add old plugin files to backup and delete lists
            if [ -f $DIR/plugins/$F ]
            then
                BACKUPLIST="$BACKUPLIST $DIR/plugins/$F"
                DELETELIST="$DELETELIST $DIR/plugins/$F"
            fi
            # Add symbolic links to the delete list
            if [ -h $DIR/plugins/$F ]
            then
                DELETELIST="$DELETELIST $DIR/plugins/$F"
            fi
        done
    done

    # Backup and Delete files if delete list contains files.
    if [ "x$DELETELIST" != "x" ]
    then
        # If tar is available, backup files
        tar --version >& /dev/null
        if [ $? -eq 0 ]; then
            tar cfz /root/oldflashplugins.tar.gz $BACKUPLIST >& /dev/null
            rm -f $DELETELIST
            echo 
            echo "NOTICE:"
            echo "Files belonging to older Flash plugins have been removed from the filesystem.  For your safety these files have been saved in /root/oldflashplugins.tar.gz.  You may remove this tarball if these files are no longer required."
        else
            echo
            echo "Error: tar is unavailable."
            echo "Unable to backup old Flash plugin files.  They were deleted in order to prevent conflicts." 
        fi
    fi
fi


# Remove /etc/flash.license as it is not used anymore
[ -f /etc/flash.license ] && rm -f /etc/flash.license
}

detectbrowsers() {
# Detect Mozilla plugin compatible browsers
for DIR in $LOCATIONS
do
    # Skip symlinks
    if [ -h $DIR ]; then continue; fi
    if [ -d $DIR/plugins ]; then export LIST="$LIST $DIR"; fi
done
}

link() {
# Link for browsers on Suse 11 (W2872066) 
test -d /usr/$LIBDIR/browser-plugins && ln -sf /usr/$LIBDIR/flash-plugin/libflashplayer.so /usr/$LIBDIR/browser-plugins/libflashplayer.so
# Link Mozilla plugin compatible browsers
for DIR in $LIST
do
    ln -sf /usr/$LIBDIR/flash-plugin/libflashplayer.so $DIR/plugins/libflashplayer.so
done
}

deletelinks() {
# Delete symlinks
# Remove the link on Suse 11
test -d /usr/$LIBDIR/browser-plugins && rm -f /usr/$LIBDIR/browser-plugins/libflashplayer.so
# Remove Mozilla plugin compatible browsers
for DIR in $LIST
do
    rm -f $DIR/plugins/libflashplayer.so
done
}

#=======================
# Main Section
#=======================
# Pre-Uninstall
if [ "$2" = "preun" ]; then
    detectbrowsers
    deletelinks
    exit 0
fi

# Installation
if [ "$2" = "install" ]; then
    deleteold
    detectbrowsers
    link
    exit 0
fi

# Upgrade
if [ "$2" = "upgrade" ]; then
    detectbrowsers
    link
    exit 0
fi

# Manual Setup

detectbrowsers
link

