#! /bin/sh
#
#       @(#)phone (SunForum 3.2)
#
# Phone script file
#
# This utility should be run from a terminal window.
#
# (C) Copyright(c) Data Connection Ltd 2000-2001
##########################################################################
#
# Changes
#
#   23Aug00 MWR   SFR6717        Created
#   25Aug00 MWR   SFR6717        Change comments
#    8Nov00 NPL   SFR6812        Made whattable
#    5Dec00 ADR   SFR7054        Internationalization
#    8Jan01 ADR   SFR7108        Set pwd as env variable
#
##########################################################################
#
#       This script is used to run the phone command line tool.
#       It is the intended interface for users.
#
#       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.  Execute pcl which is assumed to be in ../lib relative to
#           where the SunForum binaries were found. All arguments are passed.
#
##########################################################################


SFPATH=
PATHERR=1
DEBUG=0

GREP=/usr/bin/grep
AWK=/usr/bin/awk
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

STARTPATH=`$PWD`

#
#       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/sfcc -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 ../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
            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
}

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

DCS_ORIG_PWD=`$PWD`
export DCS_ORIG_PWD

#
#       Set TEXTDOMAINDIR appropriately for SunKeeper messaging.
#       This must be done before using gettext.
#

if [ $TEXTDOMAINDIR ]; then
    TEXTDOMAINDIR="$TEXTDOMAINDIR:"
fi

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

export TEXTDOMAINDIR

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

if [ $PATHERR = 1 ]; then
   echo `$GETT DC-Phone "Error: Could not find phone script"`
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

#Move to the calculated directory.  PCL should be in lib/pcl from here.
cd $SFPATH

ii=1
prev=$0

#We only want the exe name, not the entire path
temp=`$ECHO $0 | $AWK -F"\/" '{print $'$ii'}'`
while [ "$temp" != "" ];
do
   prev=$temp
   ii=`expr $ii + 1`
   temp=`$ECHO $0 | $AWK -F/ '{print $'$ii'}'`
done

#Run the program, and then change back to the starting directory.
lib/pcl $prev $*

cd $STARTPATH

