#! /bin/sh

##
# 
# improv-env:  a script to be used by the provisioning client programs
# upon startup to create the appropriate execution environment
# 
# $Id: improv-env,v 1.41 2003/09/22 23:30:27 root Exp shaug $
# 
##

## Important environmental variables
##

# Critical for RCS commands and any other shell command that makes use
# of temp files!
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/adm/improv:/usr/local/adm/libexec
PLATFORM=`uname`
case $PLATFORM in
FreeBSD)
  ;;
SunOS)
  PATH=/usr/xpg4/bin:${PATH}:/usr/ucb
  if [ -d /usr/hol ]; then
     p=/usr/hol/adm/libexec:/usr/hol/sbin:/usr/hol/bin
     p=${p}:/usr/hol/vpop/bin
     PATH=${p}:${PATH}
     LD_LIBRARY_PATH=/usr/hol/lib:${LD_LIBRARY_PATH}
  elif [ -d /usr/common ]; then
     p=/usr/common/adm/libexec:/usr/common/sbin
     PATH=${p}:/usr/common/bin:${PATH}
     MANPATH=/usr/common/man:${MANPATH}
     LD_LIBRARY_PATH=/usr/common/lib:${LD_LIBRARY_PATH}
  fi
  ;;
esac
TMP=/tmp/.improv; test -d $TMP || { mkdir -p $TMP; chmod 0700 $TMP; }
export PATH LD_LIBRARY_PATH TMP


# where are mail files kept?
MAILFILTER_DIR=/usr/local/etc/maildroprcs
MAILDROP_RC=.mailfilter
FORWARD_FILE=.forward


## Exit code definitions:
##
## successful completion of the request
##

EX_OK=0
export EX_OK

## All other exit values indicate a failure of some type
##

# bogus syntax (e.g., wrong number of args, etc)
EX_USAGE=64

# bogus input data
EX_DATAERR=65

# the requested service does not exist
EX_UNAVAILABLE=69

# a system error occurred
EX_OSERR=71

# error connecting to server
EX_CONNECT=74

# IP or user not authorized to run script or connect to server
EX_NOPERM=77

# target user/address/condtion does not exist
EX_NOEXISTS=79

# target user/address/condition already exists
EX_EXISTS=80

# the target user/address/service is protected
EX_PROTECTED=81

# an unexpected, as-yet-undefined error
EX_MISCERR=99

export EX_USAGE EX_DATAERR EX_PROTECTED EX_EXISTS EX_NOEXISTS EX_OSERR
export EX_MISCERR EX_UNAVAILABLE EX_NOPERM EX_CONNECT 


## Useful utility functions
##

# die - Fatal exit with diagnostic info
# arg 1: exit code
# arg n: all other arguments are messages to be printed out
die () {
    code="$1"; shift
    #for msg in "$@"; do echo "$ME: $msg"; done;
    for msg in "$@"; do echo $msg |sed "s/^/$ME: /g"; done;
    exit $code
}

# warn - Print a message
# args: each arg is printed to stdout, prefixed with the name of the program
warn () {
    for msg in "$@"; do echo "$ME: $msg"; done;
}

# sigdie - Signal handler that reports the cause of death (barely)
sigdie () {
    die $EX_OSERR "killed"
}

# find_caller - Simple function to try and identify who's running the program
find_caller () {
    real_user=`whoami`
    #real_user=${SUDO_USER:-`who am i | cut -d' ' -f1`}
    remote_host=${REMOTE_HOST:-${REMOTE_ADDR:-localhost}}
    remote_user=${REMOTE_USER:-$real_user}
    export real_user remote_host remote_user
}

# check_out - wrapper around RCS check-out
# arg 1: name of file to check out and lock
check_out () {
    file="$1"

    for d in 3 2 1; do
        co -q -l $file >/dev/null 2>&1 && return 0
        sleep 1
    done

    #warn "$file could not be locked."
    return $EX_OSERR
}

# check_in - wrapper around RCS check-in
# arg 1: name of file to check in and unlock
# arg 2: message to record in the RCS history
check_in () {
    file="$1"
    note="$2"

    for d in 3 2 1; do
        ci -q -u "-m$note" $file >/dev/null 2>&1 && return 0
        sleep 1
    done

    #warn "$file could not be unlocked."
    return $EX_OSERR
}
