#! /bin/csh -f

set date = $1

set yr  = `echo $date | cut -c1-4`
set doy = `echo $date | cut -c5-7 | sed 's/0*//'`

set leap = 0

@ i = ($yr % 4)
if ($i == 0) then
    set leap = 1
endif

@ i = ($yr % 100)
if ($i == 0) then
    set leap = 0
endif

@ i = ($yr % 400)
if ($i == 0) then
    set leap = 1
endif

#echo leap: $leap

if ($leap == 0) then
    set nday = (31 28 31 30 31 30 31 31 30 31 30 31)
endif

if ($leap == 1) then
    set nday = (31 29 31 30 31 30 31 31 30 31 30 31)
endif


@ mn = 1
@ dy = $doy

while ($mn < 13)
    @ dy = $dy - $nday[$mn]
    if ($dy <= 0) then
	@ dy = $dy + $nday[$mn]
	break
    endif

    @ mn++
end

date -d $mn/$dy/$yr +%Y-%m-%d

exit

