#!/bin/bash

# Wrapper for the 'at' package's 'atrm' program.  The wrapper calls
# the real program after deleting the 'email-at' temporary file, if
# applicable.

set -o errexit
set -o nounset
set -o pipefail

declare -r real_me="/usr/bin/atrm"

function zap () {
    local jobid="$1"
    local tmpfile="$(at -c $jobid | grep -o '/var/tmp/email-at[^"]\+' | head -1)"
    if [[ -n "$tmpfile" ]]; then
        rm "$tmpfile"
    fi
    $real_me $jobid
    return
}
#------------------------------------------------------------------------------

while getopts ":V" opt
do
   case $opt in
       V ) printf "$0 is a wrapper for\n"; $real_me "$@"; exit 0 ;;
       * ) $real_me -h ;;
   esac
done
shift $(($OPTIND - 1))
#------------------------------------------------------------------------------

while [[ $# > 0 ]]; do
    zap "$1"
    shift
done

exit 0
