#
# Template for generalized overnight OR background task driver script:
#

echo                        # Announce the nature of the application
echo "(Name of report)"
echo

debug=Y                     # to support testing

AppId=report                # AppId is short description of application
Ltmp=/tmp                   # 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 any additional user-specified parameters here)
#

if [ $onite = N ]; then
    bgrun.sh $outlog $LOCKOPT <<END
#   *** insert command here ***
    [ $UseLock = Y ] && rm $LOCKFILE
END
    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` <<END
#   *** insert command here ***
END
    echo
    echo This process has been scheduled for overnight processing.
    echo Check for your output tomorrow morning at the selected printer.
    echo
fi
