#!/bin/sh
LOG=/var/log/cups/cups-capture.log
DIR=/usr/local
USER=dw

if false ; then
    (   
        date >>$LOG
        echo -n "$0 "
        printf '"%s" ' "$@" 
        echo 
        set
    ) >>$LOG
fi

export CONFIG="$DIR/etc/cups-capture.yml"

doit () {
    if [ $# -eq 6 ]; then
        unset TMPDIR
        FILE="$(mktemp -tp "/tmp" cups-$USER-$1-XXXXXXXXXX)"
        echo INFO: FILE=$FILE 6=$6
        cat $6 >$FILE
        arg=( "$@" )
        arg[5]=$FILE
        chmod o+r "$FILE"
        sudo -u $USER $DIR/sbin/cups-capture "${arg[@]}"
        rm -f $FILE
    else
        sudo -u $USER $DIR/sbin/cups-capture "$@"
    fi
}

# These next 2 lines will log stderr and stdout to $LOG 
# but also send them to stderr and stdout untouched
exec 3>&1
{ doit "$@" | tee -a $LOG; } 2>&1 1>&3 | tee -a $LOG 1>&2
