#!/bin/ksh
USAGE='usage: [-e envy1 envy2 ...] [-s <shell cmd> ...]'

if [ x$ENVY_VERSION != x ]; then
  echo
  echo '*** ERROR: Envy is already running!'
  echo
  echo 'To test, please use this idiom:'
  echo "  'env -i $0 ...'"
  echo
  exit
fi

EDITOR=vi; export EDITOR
LD_LIBRARY_PATH=/usr/lib; export LD_LIBRARY_PATH
MANPATH=/usr/man; export MANPATH
PATH=/usr/bin:/usr/sbin:/usr/ucb; export PATH


if [ x$LOGNAME == x  ]; then
  # cron sets up LOGNAME automatically but 'env -i' does not.
  # Here we try to set it to something reasonable:
  LOGNAME=`/usr/bin/whoami`; export LOGNAME
fi

typeset -i i
typeset -i j
if [[ $# = 0 ]]; then
    echo $USAGE
    break
fi
  
ENVY=`dirname $0`/envy.pl
test -x $ENVY || echo Envy not found at $ENVY: PANIC

toload=test

while [[ $1 = -* ]];
do
case $1 in

-s) while [[ $2 != -* && $# > 1 ]];
    do 
	commands[$i]=${commands[$i]}" "$2
	shift
    done
    i=$i+1;;

-m|-e) while [[ $2 != -* && $# > 1 ]];
    do 
	if [ "$2" != test ]; then
	    toload="$toload $2"
	fi
	shift
    done;;

*)  echo "ILLEGAL OPTION -"$1 
    echo $USAGE
    break
esac

if [[ $# > 0 ]];  then
    shift
fi
done

ENVY_CONTEXT=${commands[0]}
export ENVY_CONTEXT
eval `$ENVY load $toload`

j=$i
i=0
while [[ $i < $j ]];
do
    ${commands[$i]} 
    i=$i+1
done

# set_defaults.PL wrapper.IN -> wrapper
