#!/bin/sh
# Copyright (C) 2004 Networks Associates Technology Inc. All rights reserved.

cd "`dirname $0`" || exit
installdir=`/bin/pwd`

ExitUnfinished()
{
    echo "To complete the WebShield client installation it will be" >&2
    echo "necessary to find or install the Java interpreter and re-run $0" >&2
    exit 1
}
trap 'ExitUnfinished' 1 2 3 13 15

rpmlist=`type rpm >/dev/null 2>&1 && rpm -qa | grep '^j2'`
javalist=`
    IFS=':'
    [ -n "$rpmlist" ] && rpm -ql $rpmlist | grep '/java$'
    for d in /usr/java/*/bin $PATH; do
	[ -x "$d/java" ] && echo "$d/java"
    done
`

if [ -n "$javalist" ]; then
    echo ''
    echo "List of java interpreters found:"
    echo "$javalist"
    echo "... and there may be others installed"
fi

javaint=`echo "$javalist" | head -1`
echo ""
echo "The WebShield client requires a Java interpreter"
while :; do
    echo ""
    if [ -z "$javaint" ]; then :
    elif [ -x "$javaint" ]; then
	echo "Version of $javaint:"
	"$javaint" -version ||
	    echo "Error: '$javaint -version' failed"
    else
	echo "$javaint is not executable"
    fi
    printf "Pathname of Java interpreter to use? [$javaint] "
    read ans
    if [ -n "$ans" ]; then javaint=$ans
    else break
    fi
done

if [ ! -x "$javaint" ]; then
    echo "$javaint is not executable" >&2
    ExitUnfinished
fi

classpath=''
for f in *.jar; do
    classpath=${classpath:+$classpath:}$installdir/$f
done
scriptform='#!/bin/sh
ji="%s"
[ -x "$ji" ] &&
    exec "$ji" -classpath "%s" com.nai.webshield.WSMainApp -j no -s "%s" "$@"
echo "Java interpreter not present, use %s to set it up" >&2
exit 1'
rm -f ws_ui_client
printf "$scriptform" "$javaint" "$classpath" "`pwd`" "$0" >ws_ui_client

chmod a=rx ws_ui_client

echo ""
echo "Setup completed."
echo "To run the Webshield client use: $installdir/ws_ui_client"
echo "It is recommended to soft link this to some directory in your \$PATH"
echo "(To change Java interpreter run $0 again.)"
exit 0
