#!/bin/csh -f

# simple date to day of week converter
# Donald Watrous, watrous@cs.rutgers.edu; 4/5/01

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	default:
		if (! $?DATE) then
			if ($#argv < 3) goto usage
			set M = $1
			shift
			set D = $1
			shift
			set Y = $1
			shift
			breaksw
		endif
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

if (! $?Y ) goto usage

switch ("$M")
case Jan:
	set MI = 1
	breaksw
case Feb:
	set MI = 2
	breaksw
case Mar:
	set MI = 3
	breaksw
case Apr:
	set MI = 4
	breaksw
case May:
	set MI = 5
	breaksw
case Jun:
	set MI = 6
	breaksw
case Jul:
	set MI = 7
	breaksw
case Aug:
	set MI = 8
	breaksw
case Sep:
	set MI = 9
	breaksw
case Oct:
	set MI = 10
	breaksw
case Nov:
	set MI = 11
	breaksw
case Dec:
	set MI = 12
	breaksw
default:
	set DQ = '"'
	echo ${0}: "What do I do with $DQ$M$DQ?"
	goto usage
endsw

goto gotdate

usage:
	echo "usage: $0 <mon day year>"
	exec echo "    (eg, "\"$0 Apr 5 2001\"")"

gotdate:

if ($?VERBOSE) echo `date +%T` DATE = $M $D $Y

if ($Y < 1901) exec echo $0 only works for years after 1900

@ Y1000 = $Y / 1000
if ($?DEBUG) echo Y1000 = $Y1000

@ Y100 = $Y / 100
if ($?DEBUG) echo Y100 = $Y100

@ Y4 = $Y / 4
if ($?DEBUG) echo Y4 = $Y4

@ Ym4 = $Y - (($Y / 4) * 4)
if ($?DEBUG) echo Ym4 = $Ym4

# so every year, the day of the week marches forward one day

@ I = $Y

# except every leapyear, where it goes ahead two days

@ I += $Y4

# but every hundred years is not a leapyear

@ I -= $Y100

# but every thousand is

@ I += $Y1000

# don't adjust for leapyear if not after Feb 29

if ($MI < 3 && $Ym4 == 0) @ I -= 1

# now we have year adjusted figure
# to that, add day and fudge for month

set FUDGE = ( 2 5 5 1 3 6 1 4 0 2 5 0 )

@ I += $D + $FUDGE[$MI]

# reduce this to index of day of week

@ I = ($I - (($I / 7) * 7)) + 1
if ($?DEBUG) echo I = $I

# and print it out

set DAY = ( "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" )

if ($?VERBOSE) then
	set MSG = "$M $D $Y is a"
else
	set MSG
endif

echo $MSG $DAY[$I]
