#!/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=/home/panstarrs/bills/psconfig

WORK_DIR=/data/ipp004.0/pstamp-work

#### 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 debug
status=$?
if [[ $status != 0 ]] ; then
    echo error setting up IPP environment >&2
    exit $status
fi

#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

# make sure other users can modify files
# todo: this is of course quite dangerous, avoid this by using proper group permissions
umask 0

$*
