#!/bin/sh
#
# @(#)$Id: bboksadm.sh,v 1.1.2.30.2.3 1998/09/10 13:55:00 cvsusr Exp $
#
# Copyright 1996 Dynamic Software AB
# This is unpublished proprietary source code.
#
# Description:
# 		Entry point script to administer Basic BoKS.
#
# Usage:
#		boksadm				- interactive use
#			-f startup-file
#			-b browser
#			-a			- always show entire menu
#			-v			- print startpage name
#			-d			- Let DISPLAY pass
#
#		boksadm -S cmd [args...]	- run cmd with args
#
# If browser is specified as "-", no browser is started.
#
# Also reads browser and startup-file from environment variables
# SMADM_BROWSER and SMADM_FILE (and SSMADM... for backward compatibility).
#
PROGNAME=`basename $0`

# The following line added to support japanese kanji.
LANG=C; LC_ALL=C; export LANG LC_ALL
 
USAGE="\n\t$PROGNAME [-f startfile][-b browser][-avd]\n\t$PROGNAME -S command [args]"

# The following line added to support japanese kanji.
LANG=C; LC_ALL=C; export LANG LC_ALL

# Short routine to get "absolute path" of program
# Just returns progname on fail. If program starts with ./
# or . is in path and it is found relative to that, the
# relative path is returned. (Introduced to obviate need
# of using `type`, since that is different on different
# machines).
#
ds_find () {
    pgm=$1
    # contains full path?
    if expr "$pgm" : "/*" > /dev/null 2>&1; then
        echo $pgm
        return
    fi
    # contains relative path?
    if expr "$pgm" : "\./*" > /dev/null 2>&1; then
        echo $pgm
        return
    fi
    # Have to look for it in current path
    found=0
    SAVEDIFS=$IFS
    IFS=:
    for p in $PATH; do
        if [ -x $p/$pgm ]; then
            echo $p/$pgm
	    found=1
            break
        fi
    done
    IFS=$SAVEDIFS
    if [ $found -ne 1 ]; then
	echo $pgm
    fi
}


# Locate BoKS
#
_PATH="$PATH"
for f in /etc/opt/SUNWssmi/ENV /etc/boksenv; do
    if [ -f $f ]; then
	eval `grep '^BOKS_lib=' $f`
	PATH="$BOKS_lib:$PATH"
	break
    fi
done
test -z "$BOKS_lib" && exit 1

# Get BoKS locations
for d in DIR etc sbin lib  data var tmp bin man; do
    eval BOKS_$d="`boksdir $d`"
    export BOKS_$d
done
test -z "$BOKS_lib" && { echo "Must be run in a BoKS environment" >&2; exit 1; }
PATH="$BOKS_lib:$BOKS_sbin:$BOKS_bin:$PATH"; export PATH


SSO_CRED_ROOT=$BOKS_data/sso_creds
export SSO_CRED_ROOT

PRECONFIG=

# For compatibility with SSM
# Priority if several of these are set will be
# (highest first): BOKSADM_*, SMADM_*, SSMADM_*
#
SSMADM_BROWSER="${SSMADM_BROWSER:-lynx}"
SSMADM_FILE="${SSMADM_FILE:-/tmp/ssm$$.html}"
SMADM_BROWSER="${SMADM_BROWSER:-$SSMADM_BROWSER}"
SMADM_FILE="${SMADM_FILE:-$SSMADM_FILE}"

BOKSADM_BROWSER="${BOKSADM_BROWSER:-$SMADM_BROWSER}"
BOKSADM_FILE="${BOKSADM_FILE:-$SMADM_FILE}"

RUNSHELL=false
SHOWPAGE=false
STOP_DISPLAY=true
COOKIE_ID=
while getopts af:b:Stvd c
do
    case $c in
    S)
	#Ensure that only root can get a SM shell.
	case `/bin/id` in
		uid=0*) ;;
		*)      echo "You must be root to run this program with -S"
		exit 1;
		;;
	esac
	RUNSHELL=true; break;;
    a)
	PRECONFIG= ;;
    f)
	BOKSADM_FILE="$OPTARG" ;;
    b)
	BOKSADM_BROWSER="$OPTARG" ;;
    v)
	SHOWPAGE=true;;
    d)
	STOP_DISPLAY=false;;
    *)
	printf "Usage: $USAGE\n" >&2; exit 1 ;;
    esac
done
shift `expr $OPTIND - 1`

# This one sets MLANG
#
# Set MLANG
if [ -z "$MLANG" ]; then
    case $LANG in
	sv*)
	    MLANG=swe;;
	*)
	    MLANG=eng;;
    esac
    export MLANG
fi


# Check if welcome file exists, otherwise set MLANG to eng
if [ ! -f "$BOKS_lib/gui/etc/$MLANG/welcome-page.tmpl" ]; then
    MLANG=eng
    export MLANG
fi

# Licensing reminder? 
#
BADLICFILE=$BOKS_var/.licinfo
if [ -s $BADLICFILE ]; then
    cat $BADLICFILE >&2
    echo
    echo "Please fix licensing problem."
    echo "(Sleeping 10 seconds before resuming operation.)"
    sleep 10
fi

# Just run an interacive shell or a command?
#
if $RUNSHELL; then
    MANPATH=$BOKS_man:${MANPATH:-/usr/man:/usr/share/man}; export MANPATH
    if [ -n "$1" ]; then
	exec "$@"
    fi
    eval `grep '^VERSION=' $BOKS_etc/ENV`
    if [ -n "$VERSION" ]; then
	echo "SM $VERSION" >&2
    fi
    PS1="SM ${PS1:-> }"
    export PS1
    unset ENV
    exec ${BOKS_SHELL:-/usr/bin/ksh}
fi

if test ! -f $BOKS_var/LCK/boks_init; then
    echo "SM daemons not running, have you executed setup or Boot?, exiting"
    exit 1
fi

# Read messages
eval "`msglist $BOKS_lib/mess boksadm.sh 2>/dev/null`"


if [ "X$BOKSADM_BROWSER" = "X-" ]; then
    BOKSADM_BROWSER=
fi

# another startpage for Lynx
#
if [ -z "$PRECONFIG" -a x"$BOKSADM_BROWSER" = x"lynx" ]; then
    PRECONFIG=startlynx
fi

if [ "x$BOKSADM_BROWSER" != x ]; then
    BOKSADM_BROWSER=`ds_find $BOKSADM_BROWSER`
fi

if grep '^ISMASTER=on$' $BOKS_etc/ENV > /dev/null 2>&1; then
    :
else
    echo "You must run SSMI administration gui on the master."
    exit 1
fi

LYNX_HELP_FILE="file://localhost/$BOKS_lib/gui/public/lib/lynx.html"
export BOKSADM_BROWSER BOKSADM_FILE LYNX_HELP_FILE

if $STOP_DISPLAY && test -n "$DISPLAY" -a X"$BOKSADM_BROWSER" != X ; then
    if expr "$DISPLAY" : ':[0-9].*' > /dev/null; then :
    elif expr "$DISPLAY" : "`/usr/bin/uname -n`:[0-9].*" > /dev/null; then :
    else
	s="DISPLAY is not local (use -d option to override)"
	echo "${MESS_nonlocal_display:-$s}" >&2
	exit 1
    fi
fi

cleanup() {
    if [ X"$COOKIE_ID" != X ]; then
	rm -f "$BOKSADM_FILE"    
	bcookie -r "$COOKIE_ID" > /dev/null 2>&1
    fi
}
    
rm -f "$BOKSADM_FILE"
trap cleanup 0 1 2 3 15

st=0
if [ -d $BOKS_lib/gui ]; then
    $SHOWPAGE && echo "$BOKSADM_FILE"
    tmpl="$BOKS_lib/gui/etc/$MLANG/welcome-page.tmpl"
    flags=
    x=`bcookie -f "$tmpl" $flags -s ${PRECONFIG:-start} "$BOKSADM_FILE" 2>&1`
    st=$?
    case $st in
	0) COOKIE_ID="$x";;
	2) echo "${MESS_perm_denied:-permission denied}" >&2; exit 2;;
	*) echo "$x" >&2; exit $st;;
    esac
    if [ X"$BOKSADM_BROWSER" != X ]; then
	$BOKSADM_BROWSER "file:$BOKSADM_FILE"
	st=$?
    else
	COOKIE_ID=
    fi
fi
exit $st
