#! /bin/sh
#
#       @(#)sunforum Version 3.2 00/08/04
#
#       sunforum:  This script is used to initiate a SunForum
#       collaboration session.  It is the intended interface for
#       users.  Should the conference manager exit abnormally, this
#       script will stop the t120 daemons that were running in
#       support of the conference session.
#
#       Program Steps
#
#       1.  Use the path to this script to find SunForum.  This assumes that
#           the binaries and the libraries are located in ../lib relative to
#           this script.
#       2.  If SunForum can't be found there, look in /opt/SUNWdat.
#       3.  Set TEXTDOMAINDIR for SunForum messaging based on location of
#           SunForum installation.  Assume /opt/SUNWdat if unknown.
#       4.  If unable to locate installation, give up and print an error
#           message
#       5.  If found and not in /opt/SUNWdat prepend path to LD_LIBRARY_PATH.

SFPATH=
SF3DPATH=
PATHERR=1
DEBUG=${SF3D_DEBUG:-0}

GREP=/usr/bin/grep
LS=/usr/bin/ls
PWD=/usr/bin/pwd
UNAME=/usr/bin/uname
FILE=/usr/bin/file
ECHO=/usr/bin/echo
GETT=/usr/bin/gettext
PS=/usr/bin/ps
WHOAMI=/usr/ucb/whoami
PKGINFO=/bin/pkginfo
SHOWREV=/usr/bin/showrev

LogDebug()
{
    if [ $DEBUG -eq 1 ] ; then
	$ECHO "$*"
    fi
}

#
#       Check Root: Determine if path specified by $1 points to root
#       of the Sunforum application hierarchy.
#
CheckRoot()
{
    if [ $1 ]; then
	LogDebug "CheckRoot:   examining $1"

        if [ -d $1/bin -a -d $1/lib ]; then
            if [ -f $1/lib/sfcc -a -f $1/lib/libdcsapp.so ]; then
		LogDebug "CheckRoot:   SunForum found in $1"
                SFPATH=$1
                PATHERR=0
            fi
        fi
    fi
}

#
#       Check Root: Determine if path specified by $1 points to root
#       of the Sunforum 3D addon hierarchy.
#
CheckRoot3D()
{
    PATHERR=1

    if [ $1 ]; then
	LogDebug "CheckRoot3D:   examining $1"
        if [ -d $1/bin -a -d $1/lib ]; then
            if [ -x $1/bin/Transmitter -a \
		 -x $1/bin/Receiver    -a \
		 -f $1/lib/libSF3D_interposer.so ]; then
		LogDebug "CheckRoot3D:   SunForum found in $1"
                SF3DPATH=$1
                PATHERR=0
            fi
        fi
    fi
}

CheckNM30Mode()
{
    nm30mode=
    cfgfile=${HOME}/.sunforum/.user-config

    if [ -f $cfgfile ] ; then
	nm30mode=`$GREP "Command Centre" $cfgfile | $GREP "NM30Mode" | awk '{print $3}' | tr "[a-z]" "[A-Z]"`
    fi

    if [ "$nm30mode" = "" ] ; then

	cfgfile=/opt/SUNWdat/config/system-config

	if [ -f $cfgfile ] ; then
	    nm30mode=`$GREP "Command Centre" $cfgfile | $GREP "NM30Mode" | awk '{print $3}' | tr "[a-z]" "[A-Z]"`
	fi
    fi

    if [ "$nm30mode" = "FALSE" ] ; then
	return 0
    fi

    return 1
}

CheckOpenGLVersion()
{
    bits=$1
    if [ $bits -eq 32 ] ; then
	pkg=SUNWglrt
	patchnum=113886
    else
	pkg=SUNWglrtx
	patchnum=113887
    fi

    patchrev=07
    patchver=${patchnum}-${patchrev}

    $PKGINFO -q $pkg > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
	$ECHO "    - ($bits-bit) `$GETT SUNW_SUNFORUM_sunforum 'libraries not installed - FAILED'`"
	return 1
    fi

    oglfile=/tmp/sf3d_ogl_$$
    /bin/rm -f $oglfile

    #
    # First check that the OpenGL version is 1.3 or higher
    #
    ( LC_ALL=C ; export LC_ALL ; $PKGINFO -l $pkg ) > $oglfile

    oglver=`$GREP "VERSION:" $oglfile | awk '{print $2}' | cut -d, -f1`

    /bin/rm -f $oglfile

    major=`$ECHO $oglver | cut -d. -f1`
    minor=`$ECHO $oglver | cut -d. -f2`
    dotdot=`$ECHO $oglver | cut -d. -f3`

    #
    # If OpenGL version is 1.4 or higher we are fine.
    # no need to do even patch checks.
    #
    if [ $major -gt 1 -o $minor -gt 3 ] ; then
	return 0
    fi

    #
    # Incorrect OpenGL version < 1.3
    #
    if [ $minor -lt 3 ] ; then
	$ECHO "    - ($bits-bit) `$GETT SUNW_SUNFORUM_sunforum 'libraries incorrect version - FAILED'`"
	return 2
    fi

    #
    # If we are OpenGL 1.3.x no need to do patch checks.
    #
    if [ "$dotdot" != "" ] ; then
	return 0
    fi

    #
    # Now if OpenGL 1.3, do the patch checks.
    #
    $SHOWREV -p | $GREP "^Patch: ${patchnum}-[0-9][0-9]*" >/dev/null 2>&1

    if [ $? -ne 0 ]
    then # req'd patch is not installed, see if an obsoleting one is:

	LogDebug "    - Required patch $patchver is not installed."

	OBSPATCH=`$SHOWREV -p | \
	    sed -n "s/^Patch: \([0-9][0-9]*-[0-9][0-9]*\).*Obsoletes:.*\(${patchnum}-[0-9][0-9]*\).*Requires.*/\1:\2/p"`

	if [ "$OBSPATCH" = "" ] ; then 
	    # no installed patch is obsoleting this patch, nor is it installed,
	    # so add it to the list:
	    LogDebug "    - No patch obsoleting $patchver was found"
	    $ECHO "    - ($bits-bit) `$GETT SUNW_SUNFORUM_sunforum 'libraries patch not installed - FAILED'`"
	    return 3
	else
	    OBSOLETING=`echo $OBSPATCH | cut -d: -f1`
	    OBSOLETED=`echo $OBSPATCH | cut -d: -f2`

	    LogDebug "    - $OBSOLETING lists $OBSOLETED as obsoleted, so it need not be installed."
	fi

    else
	# some version of this patch is installed - make sure its not an 
	# older version of the patch:
	CURRPATCHNUM=`$SHOWREV -p | \
		sed -n "s/^Patch: \(${patchnum}-[0-9][0-9]*\).*/\1/p" | \
		sort -n | tail -1`

	# if the installed rev is lower than the required rev, add the required
	# rev to the list:
	CURRPATCHREV=`echo $CURRPATCHNUM | cut -d- -f2`
	if [ "${CURRPATCHREV}" -lt "${patchrev}" ]
	then
	    LogDebug "    - Down-rev patch $CURRPATCHNUM is installed - upgrade needed."
	    $ECHO "    - ($bits-bit) `$GETT SUNW_SUNFORUM_sunforum 'libraries incorrect patch version - FAILED'`"
	    return 3
	else
	    LogDebug "    - Patch $CURRPATCHNUM is installed"
	fi 

    fi # if ! $SHOWREV -p
}

    

#
#	Perform all the checks needed for the SunForum3D features
#	to be available (note, this assumes that SunForum itself
#	is properly installed)
#
CheckSF3D()
{
    exitval=0

    $ECHO "`$GETT SUNW_SUNFORUM_sunforum 'Running SunForum3D checks...'`"

    #
    # First see if the package is installed...
    #
    $PKGINFO -q SUNWsf3d > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
	$ECHO "`$GETT SUNW_SUNFORUM_sunforum '  - SUNWsf3d package check - not installed - FAILED'`"
	exitval=1
    else
	if [ "$SF3DPATH" = "" ] ; then
	    $ECHO "`$GETT SUNW_SUNFORUM_sunforum '  - SUNWsf3d package check - not properly installed - FAILED'`"
	    exitval=1
	else
	    $ECHO "`$GETT SUNW_SUNFORUM_sunforum '  - SUNWsf3d package check - succeeded'`"
	fi
    fi

    #
    # Now check the OS version
    #
    if [ `uname -r | cut -d. -f2` -lt 8 ] ; then
	$ECHO "`$GETT SUNW_SUNFORUM_sunforum '  - Solaris version check - not Solaris 8 or higher - FAILED'`"
	exitval=1
    else
	$ECHO "`$GETT SUNW_SUNFORUM_sunforum '  - Solaris version check - Solaris 8 or higher - succeeded'`"
    fi

    #
    # Now check that compatibility mode is set to T.120
    #
    CheckNM30Mode > /dev/null 2>&1

    if [ $? -ne 0 ] ; then
	$ECHO "`$GETT SUNW_SUNFORUM_sunforum '  - Compatibility mode check - is not T.120 - FAILED'`"
	exitval=1
    else
	$ECHO "`$GETT SUNW_SUNFORUM_sunforum '  - Compatibility mode check - is T.120 - succeeded'`"
    fi

    #
    # Check OpenGL version (just doing the main runtime libs check here)
    #
    oglerrval=0

    $ECHO "`$GETT SUNW_SUNFORUM_sunforum '  - OpenGL version/patch checks...'`"
    $ECHO "`$GETT SUNW_SUNFORUM_sunforum '    + version 1.3 or higher required'`"
    $ECHO "`$GETT SUNW_SUNFORUM_sunforum '    + 1.3 needs patches 113886-07 & 113887-07 or higher'`"

    CheckOpenGLVersion 32
    if [ $? -ne 0 ] ; then
	oglerrval=1
    fi

    CheckOpenGLVersion 64
    if [ $? -ne 0 ] ; then
	oglerrval=1
    fi

    if [ $oglerrval -eq 0 ] ; then
	$ECHO "`$GETT SUNW_SUNFORUM_sunforum '    - succeeded'`"
    else
	exitval=1
    fi

    #
    # Final error message
    #
    if [ $exitval -eq 0 ] ; then
	$ECHO "`$GETT SUNW_SUNFORUM_sunforum 'SunForum3D checks PASSED!'`"
    else
	$ECHO "`$GETT SUNW_SUNFORUM_sunforum 'SunForum3D checks FAILED!'`"
    fi

    $ECHO
    exit $exitval
}

#
#       Check Binary Path:  Use the path to this script as the starting
#       point in the search for the SunForum binaries.  They should
#       be located in ../lib relative to this script.
#
#       Program steps:
#               1.  Set fullpath =  current working directory
#               2.  While not done
#                       a) Parse current path
#                       b) If a relative path
#                           then append to full path
#                           else replace full path
#                       c) Save command
#                       d) If command is a link
#                           then set current path to link path
#                           else done
#               3.  Parse fullpath
#               4.  Remove "bin" directory from path
#               5.  Call CheckRoot to see if libraries can be found
#
CheckBinPath ()
{
    if [ $1 ]; then
        fpath=`$PWD`
        cpath=$1
        while [ $cpath ]; do
            tmpifs=$IFS
            IFS=/
            set $cpath
            IFS=$tmpifs
	    LogDebug "CheckBinPath:    parsed cpath: $*"
            case $cpath in
                /*) rel=0;;
                *) rel=1;;
            esac
            if [ $rel = 0 ]; then
                cpath=""
            else
                cpath="$fpath"
                if [ $1 = "." ]; then
                    shift
                fi
            fi
            while [ $2 ]; do
                cpath="${cpath}/$1"
                shift
            done
	    LogDebug "CheckBinPath:    fpath: $fpath"
	    LogDebug "CheckBinPath:    cpath: $cpath"
            fpath=$cpath
            CMD="$cpath/$1"
            cpath=
            if [ -h $CMD ]; then
                set `$LS -l $CMD`
                while [ $# -ge 2 ]; do
                    if [ $1 = '->' ]; then
                        cpath=$2
                    fi
                    shift
                done
            fi
        done
        IFS=/
        set $fpath
        IFS=$tmpifs
	LogDebug "CheckBinPath:        parsed fpath: $*"
        fpath=""
        while [ $2 ]; do
            fpath="$fpath/$1"
            shift
        done
        CheckRoot $fpath
        unset fpath cpath tmpifs rel
    fi
}

#
#       Add Path:  Add to the path variable named by $1 the component $2.  $3
#       must be "append" or "prepend" to indicate where the component is added.
#
addpath ()
{
    eval value=\"\$$1\"
    case "$value" in
        *:$2:*|*:$2|$2:*|$2)
            result="$value"
            ;;
        "")
            result="$2"
            ;;
        *)
            case "$3" in
                p*)
                    result="$2:${value}"
                    ;;
                *)
                    result="${value}:$2"
                    ;;
            esac
    esac
    eval $1=$result
    unset result value
}

#
#       Update the users config file.  Grep out the useful information
#
UpdateConfig()
{
        $ECHO `$GETT SUNW_SUNFORUM_sunforum "Updating existing user configuration file"`

	#
	# if SF3D addon is installed do not automatically turn ON
	# NetMeeting mode.
	#
	if [ "$SF3DPATH" = "" ] ; then
	    sed -e "/AutoReceive/d;/NM30Mode/d" ${HOME}/.sunforum/.user-config > /tmp/.dcgx$$
	else
	    sed -e "/AutoReceive/d" ${HOME}/.sunforum/.user-config > /tmp/.dcgx$$
	fi

        echo "VERSION: $CURVERSION"          >  ${HOME}/.sunforum/.user-config
        $GREP "Message"            /tmp/.dcgx$$ >> ${HOME}/.sunforum/.user-config
        $GREP "Whiteboard.Left"    /tmp/.dcgx$$ >> ${HOME}/.sunforum/.user-config
        $GREP "Whiteboard.Top"     /tmp/.dcgx$$ >> ${HOME}/.sunforum/.user-config
        $GREP "Whiteboard.Width"   /tmp/.dcgx$$ >> ${HOME}/.sunforum/.user-config
        $GREP "Whiteboard.Height"  /tmp/.dcgx$$ >> ${HOME}/.sunforum/.user-config
        $GREP "Whiteboard.Printer" /tmp/.dcgx$$ >> ${HOME}/.sunforum/.user-config
        $GREP "Whiteboard.GSPath"  /tmp/.dcgx$$ >> ${HOME}/.sunforum/.user-config
        $GREP "H323"               /tmp/.dcgx$$ >> ${HOME}/.sunforum/.user-config

#       If upgrading from build 4515 with use asian naming enabled,
#       swap first and last name order.
        DOSWAP=

        if [ -n "$OLDVERSION" ]; then
        if [ $OLDVERSION = "4515" ]; then
            if [ -f $SFPATH/lib/locale/$LANG/XDcs ]; then
                   DOSWAP=`$GREP XDcs.useAsianNaming $SFPATH/lib/locale/$LANG/XDcs`
               fi
            fi
        fi

        if [ -n "$DOSWAP" ]; then
                $GREP "Command Centre"  /tmp/.dcgx$$   > /tmp/.dcgxcc$$
                $GREP -v "ilsFirstName" /tmp/.dcgxcc$$ > /tmp/.dcgxcd$$
                $GREP -v "ilsLastName"  /tmp/.dcgxcd$$ >> ${HOME}/.sunforum/.user-config
                rm /tmp/.dcgxcc$$
                rm /tmp/.dcgxcd$$
                SN=`$GREP "Site Name" /tmp/.dcgx$$`
                NSN=`echo $SN | awk '{print $1 " " $2 " " $3 " " $5 " " $4}'`
                echo $NSN >> ${HOME}/.sunforum/.user-config
                FN=`$GREP "ilsFirstName" /tmp/.dcgx$$ | awk '{print $3}'`
                LN=`$GREP "ilsLastName"  /tmp/.dcgx$$ | awk '{print $3}'`

                echo "Command Centre.ilsFirstName: $LN" >> ${HOME}/.sunforum/.user-config
                echo "Command Centre.ilsLastName: $FN" >> ${HOME}/.sunforum/.user-config
            else
                $GREP "Site Name"      /tmp/.dcgx$$ >> ${HOME}/.sunforum/.user-config
                $GREP "Command Centre" /tmp/.dcgx$$ >> ${HOME}/.sunforum/.user-config
            fi

        rm /tmp/.dcgx$$
}

#
# Only kill those processes that have been reparented to 1
# (i.e. haven't quit when sfcc did)
#
KillProcess ()
{
    $PS -ef | $GREP $SUNFORUM_USER   | $GREP "$1"	\
	    | awk '$3==1 {print $2}' | xargs kill -9 > /dev/null 2>&1
}

#
# Remove all temporary files from /tmp that were created by this
# instance of SunForum.
# if ($1 == 0) leave behind trace files
# if ($1 == 1) delete trace files also
#
RemoveTmpFiles()
{
   if [ -n "$DISPLAYSET" ]; then
      INSTANCENAME=`$ECHO $DISPLAYSET | sed 's/.*-display //' | awk '{print $1}'`
   else
      DISPLAYNAME=`set | grep "^DISPLAY=" | sed 's/DISPLAY=//' | sed 's/\..*$//' `
      HOSTNAME=`$ECHO $DISPLAYNAME | sed 's/:.*//' `
      if [ $HOSTNAME ]; then
         INSTANCENAME=$DISPLAYNAME
      else
         HOSTNAME=`hostname`
         INSTANCENAME=$HOSTNAME$DISPLAYNAME
      fi
   fi

   #
   # Confirm that the Phone UI isn't still running before deleting anything
   #
   SF_TMP_FILE=/tmp/.dcs.$INSTANCENAME.touch_pid_$$
   /bin/touch -f $SF_TMP_FILE

   PHONEUI_EXISTS=`$LS -a /tmp/.dcs.$INSTANCENAME.* | $GREP PICore`
   if [ -z "$PHONEUI_EXISTS" ]; then

       if [ $1 -eq 0 ]; then
	   $LS -a /tmp/.dcs.$INSTANCENAME.* | sed -e "/trc/d" | xargs rm > /dev/null 2>&1
       else
	   $LS -a /tmp/.dcs.$INSTANCENAME.* | xargs rm > /dev/null 2>&1
       fi
   fi

   /bin/rm -f $SF_TMP_FILE
}

usage ()
{
        $ECHO `$GETT SUNW_SUNFORUM_sunforum "usage: sunforum [options]"`
        $ECHO
        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\toptions:'`"

        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\t\t-visual 0xnn'`"
        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\t\t use visual 0xnn for shadow windows and video windows'`"

        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\t\t-install'`"
        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\t\t install a private colormap using visual specified'`"

        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\t\t-noinstall'`"
        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\t\t do not install a private colormap'`"

        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\t\t-runinbackground'`"
        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\t\t start running in the background'`"

        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\t\t-speeddial <filename>'`"
        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\t\t process the speed dial file <filename>'`"

        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\t\t-check3d'`"
        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\t\t check if SunForum3D features are available'`"

        $ECHO
        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\tBy default SunForum will use a 24bpp TrueColor visual if available.'`"
        $ECHO "`$GETT SUNW_SUNFORUM_sunforum '\tIf not the default visual and colormap are used.'`"
    $ECHO
}

#
#       Start of main script
#

#
#     It is assumed that the binaries, libraries, and
#       messages will be found relative to the the directory that
#     contains this script.  If they can't be found there, look in /opt/SUNWdat.
#

CheckBinPath $0
if [ $PATHERR = 1 ]; then
CheckRoot /opt/SUNWdat
fi

#
#       If couldn't find the SunForum binaries or libraries anywhere
#       flag an error
#
if [ $PATHERR = 1 ]; then
    $ECHO
    $ECHO `$GETT SUNW_SUNFORUM_sunforum "Cannot locate Sunforum installation - Please check installation"`
    exit 1
fi

#
# Check if the 3D addon is available
#
CheckRoot3D /opt/SUNWsf3d
if [ $PATHERR = 1 ]; then
    LogDebug "SunForum 3D add on not available"
fi

#
#       Set TEXTDOMAINDIR appropriately for SunForum messaging.
#
if [ $TEXTDOMAINDIR ]; then
    TEXTDOMAINDIR="$TEXTDOMAINDIR:"
fi

if [ $SFPATH ]; then
    TEXTDOMAINDIR="$TEXTDOMAINDIR$SFPATH/lib/locale"
else
    TEXTDOMAINDIR="$TEXTDOMAINDIR/opt/SUNWdat/lib/locale"
fi

export TEXTDOMAINDIR


#
#       Check if the user is asking for help and if so give it to them.
#
if [ .$1 = ".-help" ]; then
    $ECHO
    usage
    exit 1
fi

#
#       Check if the SunForum3D checks need to be performed.
#
if [ .$1 = ".-check3d" ]; then
    $ECHO
    CheckSF3D
fi

#
#       Check to see if this is the first time in. If so we want to run ourselves in the
#       background.
#
if [ "$SF3D_RUNFG" = "" ] ; then
    if [ .$1 != ".bg" ]; then
	$0 bg "$@" &
	exit 0
    fi
fi


#
#       SunForum found!  Set LD_LIBRARY_PATH appropriately if necessary
#
if [ $SFPATH != "/opt/SUNWdat" ]; then
    newpath="$SFPATH/lib:$OPENWINHOME/lib"
    addpath LD_LIBRARY_PATH $newpath prepend
    export LD_LIBRARY_PATH
    LogDebug "LD_LIBRARY_PATH: $LD_LIBRARY_PATH"
fi

#
#       Set XFILESEARCHPATH appropriately
#
newpath="$SFPATH/lib/locale/%L/%N%S:$SFPATH/lib/locale/C/%N%S"
addpath XFILESEARCHPATH $newpath prepend
export XFILESEARCHPATH
LogDebug "XFILESEARCHPATH: $XFILESEARCHPATH"

#
#       Set environment variables needed for video
#

newpath="$SFPATH/lib"
eval H261HOME=$newpath
export H261HOME
LogDebug "H261HOME: $H261HOME"

newpath="/opt/MMACopi/lib"
addpath LD_LIBRARY_PATH $newpath prepend
export LD_LIBRARY_PATH


#
# XXX: To test using the new version of the thread libraries
#
newpath="/usr/lib/lwp"
addpath LD_LIBRARY_PATH $newpath prepend
export LD_LIBRARY_PATH


#
#       Determine if the user has a config file which is for an earlier
#       version of the product.  If so, update it.
#
#       If there's no config file then create a new one, stamped with
#       the correct version number.
#
CURVERSION=`awk '/VERSION/ {print $2}' $SFPATH/config/system-config`
if [ ! -d ${HOME}/.sunforum ]; then
    mkdir ${HOME}/.sunforum
fi
if [ ! -f ${HOME}/.sunforum/.user-config ]; then
    if [ -f ${HOME}/.sunforum/.dcgx ]; then
        mv ${HOME}/.sunforum/.dcgx ${HOME}/.sunforum/.user-config
    elif [ -f ${HOME}/.dcgx ]; then
        mv ${HOME}/.dcgx ${HOME}/.sunforum/.user-config
    fi
fi
if [ -f ${HOME}/.sunforum/.user-config ]; then
    OLDVERSION=`awk '/VERSION/ {print $2}' ${HOME}/.sunforum/.user-config`
    if [ -n "$CURVERSION" ]; then
        if [ -z "$OLDVERSION" ]; then
            UpdateConfig
        elif [ $OLDVERSION -lt $CURVERSION ]; then
            UpdateConfig
        fi
    fi
else
    echo "VERSION: $CURVERSION"          >  ${HOME}/.sunforum/.user-config
fi

#
#       Record the current directory in case we change to another
#       directory below.
#

DCS_ORIG_PWD=`$PWD`
export DCS_ORIG_PWD

#
#       Did the user specify the display to use.
#
DISPLAYSET=`$ECHO $@ | grep -- '-display'`

#
#       Remember who we are
#
SUNFORUM_USER=`$WHOAMI`

#
#       Clean up tmp files from previous sessions. Do this only for
#	SunForum3D
if [ "$SF3DPATH" != "" ] ; then
    RemoveTmpFiles 1
fi

#
#       Start the user session
#
cd $SFPATH
ulimit -n 128

if [ "$SF3D_DBX" != "" ] ; then
    if [ -n "$DISPLAYSET" ]; then
	SF3D_RUNARGS="$@"
    else
	SF3D_RUNARGS="$@ -display $DISPLAY"
    fi

    echo "$SF3D_DBX -c "runargs $SF3D_RUNARGS" ./lib/sfcc"
    $SF3D_DBX -c "runargs $SF3D_RUNARGS" ./lib/sfcc;

elif [ -n "$DISPLAYSET" ]; then
   ./lib/sfcc "$@";

else
   ./lib/sfcc "$@" -display $DISPLAY;
fi

#
#       Remember the return code from sfcc
#
SFCCRC=$?


#
#
#       Now clean up
#
#
#    The AV processes and T120 stack should no longer be running, but wait a
#    second to allow them to exit, and clean up to be sure. Only clean up
#    those processes that have been reparented to 1 (i.e. haven't quit when
#    sfcc did).
#
sleep 1
KillProcess mcsd
KillProcess gccd
KillProcess sfav
#
# If SunForum3D also clean-up the Transmitter & Receiver processes
#
if [ "$SF3DPATH" != "" ] ; then
    KillProcess $SF3DPATH/bin/Transmitter
    KillProcess $SF3DPATH/bin/Receiver
fi

#
# Remove all temporary files from /tmp that were created by this
# instance of SunForum, but leave the trace files behind.
#
if [ $SFCCRC != 255 ]; then
   RemoveTmpFiles 0
fi
