#! /bin/sh
#
#       @(#)registrar Version 1.0 00/12/08
#
#       registrar:  This script is used to initiate a stub process
#       for starting and controlling Sun Registrar.  It is the intended
#       interface for users.
#
#       Program Steps
#
#       1.  Look in /opt/SUNWkeep for Sun Registrar application and libraries
#       2.  If Sun Registrar can't be found in /opt/SUNWkeep then use the path
#           to this script to find Sun Registrar.  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 Sun Registrar messaging based on location of
#           Sun Registrar installation.  Assume /opt/SUNWkeep if unknown.
#       4.  If unable to locate installation, give up and print an error
#           message

SKPATH=
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 Sun Registrar 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/skcore -a -f $1/lib/skstub ]; then
                if [ $DEBUG = 1 ]; then
                    $ECHO "CheckRoot:   Sun Registrar found in $1"
                fi
                SKPATH=$1
                PATHERR=0
            fi
        fi
    fi
}

#
#       Check Binary Path:  Use the path to this script as the starting
#       point in the search for the Sun Registrar 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
}

#
#       Start of main script
#

#
#       Check for Sun Registrar in /opt/SUNWkeep.  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/SUNWkeep
if [ $PATHERR = 1 ]; then
    CheckBinPath $0
fi

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

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

if [ $SKPATH ]; then
    TEXTDOMAINDIR="$TEXTDOMAINDIR$SKPATH/lib/locale"
else
    TEXTDOMAINDIR="$TEXTDOMAINDIR/opt/SUNWkeep/lib/locale"
fi

export TEXTDOMAINDIR

#
#       If couldn't find the Sun Registrar binaries or libraries anywhere
#       flag an error
#

if [ $PATHERR = 1 ]; then
    $ECHO
    $ECHO `$GETT SunRegistrar "Cannot locate Sun Registrar installation - Please check installation"`
    exit 1
fi
#
#       Record the current directory in case we change to another
#       directory below.
#

#
#       Start the user session
#

cd $SKPATH
./lib/skstub "$@";

#
#    Remove all temporary files from /tmp that were created by this
#    instance of Sun Registrar, but leave the trace files behind.
#
if [ .$1 = ".stop" ]; then
   $LS -a /tmp/.dcgk.* | sed -e "/trc/d" | xargs rm > /dev/null 2>&1
fi

