#! /bin/sh

# File:        npsqueakregister
# Author:      Bert Freudenberg 
# Description: Script to register the npsqueak plugin with 
#              various browsers. 
#              Rerun after you installed a new browser!
# Parameters:  -u unregister

NPSQUEAK_SO_PATH=/usr/share/Etoys.app/Contents/Linux686/npetoys.so
NPSQUEAK_SO=npetoys.so

BROWSERS="netscape mozilla mozilla-firefox firefox opera MozillaFirefox/lib"
BROWSER_DIRS="/usr/local/lib /usr/lib /opt"
SYSTEM_BROWSER_DIR="/usr/lib/browser-plugins"

ACTION=register

usage() {
    echo "Usage: $0 [-u] [PLUGIN]"
    echo "Registers the Squeak browser plugin for known browsers"
    echo "  -u         unregister plugin"
    echo "  PLUGIN     absolute path to npetoys.so"
    echo "             (default is /usr/share/Etoys.app/Contents/Linux686/npetoys.so)"
    exit
}

register() {
    echo Registering ${1}
    rm -f ${1}
    ln -sv $NPSQUEAK_SO_PATH ${1}
}

unregister() {
    echo Unregistering ${1}
    rm -f ${1}
}

case "$1" in
    -u) 
         ACTION=unregister
         ;;
    "")   
         ACTION=register
         ;;
    /*.so)
	NPSQUEAK_SO_PATH="$1"
	;;
    *)   usage
         exit 1
         ;;
esac

if [ ! -e "$NPSQUEAK_SO_PATH" ] ; then
    echo File not found: $NPSQUEAK_SO_PATH
    echo Aborting.
    exit 1 
fi

for browser in $BROWSERS
do 
    for bdir in $BROWSER_DIRS
    do
        dirs=`ls -d ${bdir}/${browser}* 2>/dev/null`
        for dir in $dirs
        do
            pdirs=`find -H $dir -name plugins -print`
            for pdir in $pdirs
            do
	        $ACTION ${pdir}/$NPSQUEAK_SO
            done
        done
    done
done


if [ -e $SYSTEM_BROWSER_DIR ]; then
    $ACTION $SYSTEM_BROWSER_DIR/$NPSQUEAK_SO
fi
