#! /bin/tcsh -f

#aquire names of lists

if ( $#argv != 1 ) then
    echo "Usage:" $0 " [*.lis(SUP file names)]" 
    exit 1
endif

if ( !(-r $1) ) then
    echo "namechange.csh: Cannot open "$1": No such file or directory"
    exit 1
endif

set list = $1

rm tmp1 tmp2
foreach file (`cat $list`)
    getkey2.pl S_UFNAME $file >> tmp1
    getkey2.pl DATE-OBS $file >> tmp2
end

@ x = ` wc $list | awk '{print $1}' `
@ y = ` wc tmp1 | awk '{print $1}' `
@ z = ` wc tmp2 | awk '{print $1}' `

echo number of input files : $x
echo number of file names  : $y
echo number of file dates  : $z

if ( $x != $y ) then
    echo "Some errors seem to happen"
    echo "number of input names:" $x " number of output names:" $y 
    echo "somehow getheader dose not work"
    exit 1
endif

if ( $x != $z ) then
    echo "Some errors seem to happen"
    echo "number of input names:" $x " number of output dates:" $z 
    echo "somehow getheader dose not work"
    exit 1
endif

sed 's/-/ /g' tmp2 | awk '$1>=2000 && $1<=2010 {print "0"$1-2000""" "$2" "$3} $1>2010 {print $1-2000""" "$2" "$3} $1<2000 {print $1-1900""" "$2" "$3}' > tmp3

awk '$3!=1 && $3<=10 {print $1""$2"0"$3-1} $3!=1 && $3>10 {print $1""$2""$3-1} $3==1 && ($2==2 || $2==4 || $2==6 || $2==8 || $2==9) {print $1"0"$2-1"""31"} $3==1 && $2==11 {print $1""$2-1"""31"} $3==1 && $2==1 && $1>0 {print $1-1"1231"} $3==1 && $2==1 && $1==0 {print "991231"}  $3==1 && ($2==5 || $2==7 || $2==10) {print $1"0"$2-1"""30"} $3==1 && $2==3 {print $1"02UR"} $3==1 && $2==12 {print $1""$2-1"""30"}' tmp3 > tmp4

paste $list tmp4 tmp1 | awk '{ print "mv "$1" H"$2""$3} ' > tmp5
cat tmp5
echo "processing..."

#paste $list tmp4 tmp1 | awk '{ print "mv "$1" H"$2""$3} ' | csh -f
csh -f tmp5




