#! /bin/sh
#
#       @(#)sunforum Version 3.0 99/05/12
#
#       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.  Look in /opt/SUNWdat for Sunforum application and libraries
#       2.  If SunForum can't be found in /opt/SUNWdat then use the path
#           to this script to find SunForum.  This assumes that the binaries
#           are located in the same directory as this script and the libraries
#           are located in ../lib relative to this script.
#       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=
PATHERR=1
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

#
#       Check Root: Determine if path specified by $1 points to root
#       of the Sunforum application hierarchy.
#

CheckRoot() {
    if [ $1 ]; then
        if [ $DEBUG = 1 ]; then
            $ECHO "CheckRoot:   examining $1"
        fi
        if [ -d $1/bin -a -d $1/lib ]; then
            if [ -f $1/lib/xdc_s -a -f $1/lib/libdcsapp.so ]; then
                if [ $DEBUG = 1 ]; then
                    $ECHO "CheckRoot:   SunForum found in $1"
                fi
                SFPATH=$1
                PATHERR=0
            fi
        fi
    fi
}

#
#       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 the same directory as 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
            if [ $DEBUG = 1 ]; then
                $ECHO "CheckBinPath:    parsed cpath: $*"
            fi
            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
            if [ $DEBUG = 1 ]; then
                $ECHO "CheckBinPath:    fpath: $fpath"
                $ECHO "CheckBinPath:    cpath: $cpath"
            fi
            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
        if [ $DEBUG = 1 ]; then
            $ECHO "CheckBinPath:        parsed fpath: $*"
        fi
        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"`
        mv ${HOME}/.sunforum/.dcgx /tmp/.dcgx
        echo "VERSION: $CURVERSION"          >  ${HOME}/.sunforum/.dcgx
        $GREP "Message"            /tmp/.dcgx >> ${HOME}/.sunforum/.dcgx
        $GREP "Whiteboard.Left"    /tmp/.dcgx >> ${HOME}/.sunforum/.dcgx
        $GREP "Whiteboard.Top"     /tmp/.dcgx >> ${HOME}/.sunforum/.dcgx
        $GREP "Whiteboard.Width"   /tmp/.dcgx >> ${HOME}/.sunforum/.dcgx
        $GREP "Whiteboard.Height"  /tmp/.dcgx >> ${HOME}/.sunforum/.dcgx
        $GREP "Whiteboard.Printer" /tmp/.dcgx >> ${HOME}/.sunforum/.dcgx
        $GREP "Whiteboard.GSPath"  /tmp/.dcgx >> ${HOME}/.sunforum/.dcgx
        $GREP "H323"               /tmp/.dcgx >> ${HOME}/.sunforum/.dcgx
        if [ $OLDVERSION = "4515" ]; then
            if [ -f $SFPATH/lib/locale/$LANG/XDcs ]; then
                AN=`$GREP XDcs.useAsianNaming $SFPATH/lib/locale/$LANG/XDcs`
            else
                AN=
            fi
            if [ -n "$AN" ]; then
                $GREP "Command Centre"  /tmp/.dcgx   > /tmp/.dcgxcc
                $GREP -v "ilsFirstName" /tmp/.dcgxcc > /tmp/.dcgxcd
                $GREP -v "ilsLastName"  /tmp/.dcgxcd >> ${HOME}/.sunforum/.dcgx
                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/.dcgx
                FN=`$GREP "ilsFirstName" /tmp/.dcgx | awk '{print $3}'`
                LN=`$GREP "ilsLastName"  /tmp/.dcgx | awk '{print $3}'`
                echo "Command Centre.ilsFirstName: $LN" >> ${HOME}/.sunforum/.dcgx
                echo "Command Centre.ilsLastName: $FN" >> ${HOME}/.sunforum/.dcgx
            else
                $GREP "Site Name"      /tmp/.dcgx >> ${HOME}/.sunforum/.dcgx
                $GREP "Command Centre" /tmp/.dcgx >> ${HOME}/.sunforum/.dcgx
            fi 
        else
            $GREP "Site Name"      /tmp/.dcgx >> ${HOME}/.sunforum/.dcgx
            $GREP "Command Centre" /tmp/.dcgx >> ${HOME}/.sunforum/.dcgx
        fi
        rm /tmp/.dcgx
}

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
        $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
#

#
#       Check for the SunForum in /opt/SUNWdat.  If it can't be found
#       there then use the path to this script as a hint to there
#       location.  It is assumed that the binaries, libraries, and
#	messages will be found relative to the the directory that 
#	contains this script.
#

CheckRoot /opt/SUNWdat
if [ $PATHERR = 1 ]; then
    CheckBinPath $0
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
#
#       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 to see if this is the first time in. If so we want to run ourselves in the
#       background.
#

if [ .$1 != ".bg" ]; then
    $0 bg "$@" &
    exit 0
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
    if [ $DEBUG = 1 ]; then
        $ECHO "LD_LIBRARY_PATH: $LD_LIBRARY_PATH"
    fi
fi

#
#       Set XFILESEARCHPATH appropriately
#

newpath="$SFPATH/lib/locale/%L/%N%S:$SFPATH/lib/locale/C/%N%S"
addpath XFILESEARCHPATH $newpath prepend
export XFILESEARCHPATH
if [ $DEBUG = 1 ]; then
    $ECHO "XFILESEARCHPATH: $XFILESEARCHPATH"
fi

#
#       Set environment variables needed for video
#

newpath="$SFPATH/lib"
eval H261HOME=$newpath
export H261HOME
if [ $DEBUG = 1 ]; then
    $ECHO "H261HOME: $H261HOME"
fi

newpath="/opt/MMACopi/lib"
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}' /opt/SUNWdat/config/dcgx`
if [ ! -d ${HOME}/.sunforum ]; then
    mkdir ${HOME}/.sunforum
fi
if [ ! -f ${HOME}/.sunforum/.dcgx ]; then
    if [ -f ${HOME}/.dcgx ]; then
        mv ${HOME}/.dcgx ${HOME}/.sunforum
    fi
fi
if [ -f ${HOME}/.sunforum/.dcgx ]; then
    OLDVERSION=`awk '/VERSION/ {print $2}' ${HOME}/.sunforum/.dcgx`
    if [ -n "$CURVERSION" ]; then
        if [ -z "$OLDVERSION" ]; then
            UpdateConfig
        elif [ $OLDVERSION -lt $CURVERSION ]; then
            UpdateConfig
        fi
    fi
else
    echo "VERSION: $CURVERSION"          >  ${HOME}/.sunforum/.dcgx
fi

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

DCS_ORIG_PWD=`$PWD`
export DCS_ORIG_PWD

#
#       Start the user session
#

cd $SFPATH
ulimit -n 128
./lib/xdc_s "$@"
#
#    if error occurred and still running after a couple of seconds
#    then stop the T120 stack and the audio/video process.
#
if [ $? -ne 255 ]; then
    sleep 2
    DCT120=`$PS -e | $GREP gccd`
    if [ -n "$DCT120" ]; then
      $PS -e | awk '/gccd/ {print $1}' | xargs kill -9
    fi
    DCT120=`$PS -e | $GREP mcsd`
    if [ -n "$DCT120" ]; then
      $PS -e | awk '/mcsd/ {print $1}' | xargs kill -9
    fi
    DCGAV=`$PS -e | $GREP dcgav`
    if [ -n "$DCGAV" ]; then
      $PS -e | awk '/dcgav/ {print $1}' | xargs kill -9
    fi
fi

