#
# Actual generalized background/overnight script driver, for
# generating a set of Informix reports with different parameters:
#
# The user is prompted for a printer name (printer), and a series of
# magazine (mag) and source code (sc) pairs.  For each such pair, the
# report generation command:
#   sacego -q swkpay $mag $sc | lp -d$printer
# is generated, and all such commands are collected in a script file
# named $list.
#
# Finally, this script file is submitted to either spoolonite.sh or
# bgrun.sh, as specified by the user.
#

echo
echo Reports of Subscription Payment History by Source Code
echo

debug=Y                     # to support testing

AppId=orig                  # AppId is short description of application
Ltmp=/u3/Srcrep/Ltmp        # Work/Log area for the app, publicly writeable
UseLock=Y                   # Y to use lockfiles, N not to

if [ $UseLock = Y ]; then
    LOCKFILE=$Ltmp/$AppId.LOCK  # Name of lockfile
    LOCKOPT="-l $LOCKFILE"      # bgrun.sh lockfile option
fi

onite=`ask.sh "Do you want to run these reports overnight"`     # get Y/N

if [ $onite = N ]; then                 # If requesting background execution,
    if [ $UseLock = Y ]; then           # and using a lock file,
        if [ -r $LOCKFILE ]; then       # then check for lock file
            echo "\nSorry, a related report is running. Please try later."
            exit 1
        else                            # no active lockfile found.
            trap "rm $LOCKFILE; exit 1" 1 2 3 9 14 15       
            touch $LOCKFILE             # create the lockfile
        fi
    fi
fi

if [ $debug = Y ]
then                                    # If debugging, create output file 
    outlog=$AppId.out                   # in the current directory  
else
    outlog=$Ltmp/$AppId.log             # else put in the Temp/Log area
fi

#
# Get parameters specific to this report program:
#

printer=`getptr p`      # prompt user for printer selection (internal utility)
list=`tmpname src`      # script file to recieve report generation commands

echo
echo You will now be given the opportunity to enter a list of 
echo source/magazine pairs. Enter an empty source code when all desired
echo reports have been entered.
echo

while true
do
    echo
    echo "Please enter the source code to profile (Return alone when done) -> \c"
    read sc
    [ "$sc" = "" ] && break     # null code terminates report list
    mag=`getmag`                # prompt for publication code (internal utility)
    cat >>$list <<-END          # append the report generation command to script
        sacego -q swkpay $mag $sc | lp -d$printer
END
done

if [ $onite = N ]; then
    [ $UseLock = Y ] && echo "rm $LOCKFILE" >>$list
    bgrun.sh $outlog $LOCKOPT < $list
    echo "This process is now running in the background."
    echo "The reports will be printed as they are generated"
else
    spoolonite.sh `oname.sh $AppId` <$list
    echo
    echo This process has been scheduled for overnight processing.
    echo Check for your output tomorrow morning at the selected printer.
    echo
fi

[ $debug = N ] && rm $list
