#!/bin/sh
###
#
# pstamp_runcommand.sh: 
#
# Set up the ipp environment to run a ipp commands on behalf of the postage stamp
# server and execute the command given by the argument list 
# This script is used by cgi or php scripts.

if [[ $# == 0 ]] ;then
    echo "usage $0 command" >&2
    #  EINVAL = 22
    exit 22
fi

#### BEGIN LOCAL_CONFIGURATION

# These variables need to be customized for a particular installation
# XXX: why not pass these on on the command line or in the environment?
export PSCONFDIR=/export/data0/bills/psconfig

WORK_DIR=/export/data1/bills/pstamp/work
CMD_DIR=/export/data0/bills/src/ipp/pstamp/scripts:/export/data0/bills/src/ipp/DataStoreServer/web/cgi

#### END LOCAL_CONFIGURATION

export HOME=$WORK_DIR

cd $WORK_DIR
status=$?
if [[ $status != 0 ]] ; then
    echo $0 cannot cd to WORK_DIR: $WORK_DIR status: $status >&2
    exit $status
fi

###
### configure the IPP
###

source $PSCONFDIR/psconfig.bash default
status=$?
if [[ $status != 0 ]] ; then
    echo error setting up IPP environment >&2
    exit $status
fi

###
### Add our command directory to the path. 
### XXX: This won't be necessary once the scripts get installed as part of the IPP
###
PATH=${CMD_DIR}:${PATH}
#echo path: $PATH >&2
#echo command: $* >&2

## make sure we are able to run the desired command
## XXX: This test is sort of redundant. 
## We'd get a more useful error by just going ahead and trying the command
## or by not suppressing the error output of which
if [[ ! -x `which $1 2> /dev/null` ]] ;then
    status=$?
    echo command $1 not found >&2
    exit $status
fi

#### Finally, execute the command given by the command line arguments

umask 0

$*
