#!/bin/ksh
#<lastwkmon: run something on the last workday of the month
#
# Last workday means:
# 1. it's Monday-Thursday and the following day is the first, or
# 2. it's a Friday and the following Sunday is either the first
#    or the second.
#
# If no arguments, exit true if last workday of the month.

PATH=/bin:/usr/sbin:/usr/bin:/usr/local/bin
export PATH

# Print the current version and source location.

myuuid () {
    lsedscr='s/UUID: //
    s/\$//g'
    lid='$UUID: 7c7f6be5-61e5-35ba-858e-5c2a5e642ebb $'
    echo "$lid" | sed -e "$lsedscr"
}

version () {
    lsedscr='s/RCSfile: //
    s/.Date: //
    s/,v . .Revision: /  v/
    s/\$//g'

    lrevno='$RCSfile: lastwkdom,v $ $Revision: 1.5 $'
    lrevdate='$Date: 2009/06/08 18:00:24 $'
    echo "$lrevno $lrevdate" | sed -e "$lsedscr"
}

where () {
    lsedscr='s/Source: //
    s/\$//g'
    lsrc='$Source: /src/opt/lastday/RCS/lastwkdom,v $'
    echo "$lsrc" | sed -e "$lsedscr"
}

# Handle command line arguments.

while getopts uvw c
do
    case $c in
        u)  myuuid; exit 0 ;;
        v)  version; exit 0 ;;
        w)  where; exit 0 ;;
        \?) exit 0 ;;
    esac
done
shift `expr $OPTIND - 1`

# Real work starts here.

timezone=`date +%Z`                   # time zone
DoW=`date +%w`                        # day of week
tomorrow=`TZ=$timezone-24 date +%d`   # today's date (e.g., 27)
aft3days=`TZ=$timezone-72 date +%d`   # three days after tomorrow

case $DoW in
  [1-4]) test "$tomorrow" == 1  && lastworkday=true ;;
  5)     test "$aft3days" -le 3 && lastworkday=true ;;
esac

case "$#" in
  0) test $lastworkday && exit 0 ;;
  *) test $lastworkday && exec ${1+"$@"} ;;
esac

exit 1
