#!/bin/sh
set -u  # force better programming and ability to use check for not set
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

# bin/$RCSfile$  $Revision$

node=`uname -n| sed -e 's/\([^\.]\)\..*/\1/'`
gang=`gang`
N=`date +"%j"` #day of year
case $gang in
  stken) namespace=/pnfs/eagle/backups
         weak_port=24126
	 strong_port=24127
         date_string_d() { date +"%Y/%m" --date="1 week ago" ; }
         place=$namespace/`date_string_d`
         file=`find $place -type f -print | head -n 1`
         f2=`echo $file | sed -e 's%/pnfs%%'`
         f3=`echo $file | sed -e 's%/pnfs/eagle%%'`
         w=/pnfs/eagle/dcache-tests/$N.dcache
         w2=`echo $w | sed -e 's%/pnfs%%'`
         w3=`echo $w | sed -e 's%/pnfs/eagle%%'`
         dcache=fndca
         do_active=1
         do_passive=1
         do_nonkerb_active=1
         do_nonkerb_passive=1
         do_kftp_write=1
         do_dccp=1
         do_double=1
         do_dccpwrite=1
         do_readback=1
          ;;
  cdfen) namespace=/pnfs/cdfen/filesets
         weak_port=25126
	 strong_port=25127
         # new file once/day, asumming the head of directory is constant (bad assumption)
         file=`find $namespace -type f -size +0 -print | head -n $N | tail -n 1` 2>/dev/null
         f2=`echo $file | sed -e 's%/pnfs%%'`
         f3=`echo $file | sed -e 's%/pnfs/cdfen%%'`
         w=/pnfs/cdfen/jon/fcdfsgi2/$N.dcache
         w2=`echo $w | sed -e 's%/pnfs%%'`
         w3=`echo $w | sed -e 's%/pnfs/cdfen%%'`
         dcache=cdfendca3
	 #dcache=orchard
         do_active=1
         do_passive=1
         do_nonkerb_active=0
         do_nonkerb_passive=0
         do_kftp_write=1
         do_dccp=1
         do_double=1
         do_dccpwrite=1
         do_readback=1
          ;;
  d0en) namespace=/pnfs/sam/dzero
	 weak_port=24126
	 strong_port=24127
         #date_string_d() { date +"%Y/%m" --date="1 week ago" ; }
	 #place=$namespace/`date_string_d`
         #file=`find $place -type f -print | head -n 1`
	 # new file once/day, asumming the head of directory is constant (bad assumption)
       echo "Finding file"
         file=`find $namespace -type f -size +0 -print | head -n $N | tail -n 1` 2>/dev/null
	echo "found file"
         f2=`echo $file | sed -e 's%/pnfs%%'`
         #f3=`echo $file | sed -e 's%/pnfs/archive%%'`
         f3=`echo $file | sed -e 's%/pnfs/sam%%'`
         w=/pnfs/archive/dcache-tests/$N.dcache
         w2=`echo $w | sed -e 's%/pnfs%%'`
         #w3=`echo $w | sed -e 's%/pnfs/archive%%'`
         w3=`echo $w | sed -e 's%/pnfs%%'`
         dcache=d0endca3a
         do_active=1
         do_passive=1
         do_nonkerb_active=0
         do_nonkerb_passive=0
         do_kftp_write=1
         do_dccp=1
         do_double=1
         do_dccpwrite=1
         do_readback=1
          ;;
       *) namespace="UNKNOWN"
          ;;
esac
echo FILE $file $f2 $f3 $w $w2 $w3
test_file=/tmp/dcache_test_file
read_back=/tmp/dcache_read_back

rm -f $test_file
cp /var/log/messages $test_file

source /usr/local/etc/setups.sh
if [ $? -gt 0 ]; then
    echo "Unable to source /usr/local/etc/setups.sh."
    exit 9
fi
setup kftp
if [ $? -gt 0 ]; then
    echo "Unable to setup kftp."
    exit 8
fi
setup dcap
if [ $? -gt 0 ]; then
    echo "Unable to setup dcap."
    exit 8
fi

# get a file that is old.  As time moves on, choose a different
# file.  But keep old choice for a reasonable amount of time.
if [ ! -r $file ]; then
  echo "Can not read $file from $place.  Trouble with script"
  exit 9
fi

# Use Eileen's enrsh method to get a ticket
krbdir="/usr/krb5/bin"
defaultDomain=".fnal.gov"
host=`uname -n`
if expr $host : '.*\.' >/dev/null;then
   thisHost=$host;
else
   thisHost=${host}${defaultDomain};
fi
KRB5CCNAME=/tmp/krb5cc_enstore_$$
export KRB5CCNAME
${krbdir}/kinit -k -t /local/ups/kt/enstorekt enstore/cd/${thisHost}
# ${krbdir}/klist -fea


wfile=${w3}_$$_1
# Active kerberized kftp read
if [ $do_active = 1 ]; then
  echo "================================================================================================================================"
  echo "`date` Active read kftpcp -p $strong_port -v enstore@$dcache:$f3 /dev/null"
                           kftpcp -p $strong_port -v enstore@$dcache:$f3 /dev/null
  stat1=$?
  if [ $stat1 -ne 0 ]; then
     echo enstore alarm --raise --severity "A" --root-error "Active read kftpcp -p $strong_port -v enstore@$dcache:$f3 /dev/null  STAT=$stat1"
  fi
  echo
else
  stat1=0
fi

# Active kerberized kftp write
if [ $do_active = 1 -a $do_kftp_write = 1 ]; then
  echo "================================================================================================================================"
  echo "`date` Active write kftpcp -p $strong_port -v $test_file enstore@$dcache:$wfile"
                            kftpcp -p $strong_port -v $test_file enstore@$dcache:$wfile
  stat1a=$?
  if [ $stat1a -ne 0 ]; then
     echo enstore alarm --raise --severity "A" --root-error "Active write kftpcp -p $strong_port -v $test_file enstore@$dcache:$wfile STAT=$stat1a"
  fi
  echo
else
  stat1a=0
fi

# Active kerberized kftp readback
if [ $do_active = 1 -a $do_kftp_write = 1 -a $do_readback = 1 ]; then
  echo "================================================================================================================================"
  echo "rm -f $read_back"
        rm -f $read_back
  echo "`date` Active readback kftpcp -p $strong_port -v enstore@$dcache:$wfile $read_back"
                               kftpcp -p $strong_port -v enstore@$dcache:$wfile $read_back

  stat1b=$?
  if [ $stat1b -ne 0 ]; then
     echo enstore alarm --raise --severity "A" --root-error "Active readback kftpcp -p $strong_port -v $test_file enstore@$dcache:$wfile STAT=$stat1b"
     stat1c=0
  else
     cmp $test_file $read_back
     stat1c=$?
     if [ $stat1c -ne 0 ]; then
         echo enstore alarm --raise --severity "A" --root-error "Active readback kftpcp. cmp fails STAT=$stat1c"
     fi
  fi
  echo
else
  stat1b=0
  stat1c=0
fi


# Passive kerberized read
if [ $do_passive = 1 ]; then
  echo "================================================================================================================================"
  echo "`date` Passive kftpcp -p $strong_port -m p -v enstore@$dcache:$f3 /dev/null"
                       kftpcp -p $strong_port -m p -v enstore@$dcache:$f3 /dev/null
  stat2=$?
  if [ $stat1 -ne 0 ]; then
     echo enstore alarm --raise --severity "A" --root-error "Passive kftpcp -p $strong_port -m p -v enstore@$dcache:$f3 /dev/null  STAT=$stat2"
  fi
  echo
else
  stat2=0
fi

if /bin/false; then

wfile=${w3}_$$_2

# Passive kerberized kftp write
if [ $do_passive = 1 -a $do_kftp_write = 1 ]; then
  echo "================================================================================================================================"
  echo "`date` Passive write kftpcp -p $strong_port -m p -v $test_file enstore@$dcache:$wfile"
                             kftpcp -p $strong_port -m p -v $test_file enstore@$dcache:$wfile
  stat2a=$?
  if [ $stat2a -ne 0 ]; then
     echo enstore alarm --raise --severity "A" --root-error "Passive write kftpcp -p $strong_port -m p -v $test_file enstore@$dcache:$wfile STAT=$stat2a"
  fi
  echo
else
  stat2a=0
fi


# Passive kerberized kftp readback
if [ $do_passive = 1 -a $do_kftp_write = 1 -a $do_readback = 1 ]; then
  echo "================================================================================================================================"
  echo "rm -f $read_back"
        rm -f $read_back
  echo "`date` Passive readback kftpcp -p $strong_port -m p -v enstore@$dcache:$wfile $read_back"
                                kftpcp -p $strong_port -m p -v enstore@$dcache:$wfile $read_back

  stat2b=$?
  if [ $stat2b -ne 0 ]; then
     echo enstore alarm --raise --severity "A" --root-error "Passive readback kftpcp -p $strong_port -m p -v $test_file enstore@$dcache:$wfile STAT=$stat2b"
     stat2c=0
  else
     cmp $test_file $read_back
     stat2c=$?
     if [ $stat2c -ne 0 ]; then
        echo enstore alarm --raise --severity "A" --root-error "Passive readback kftpcp. cmp fails STAT=$stat2c"
     fi
  fi
  echo
else
  stat2b=0
  stat2c=0
fi

else
  stat2a=0
  stat2b=0
  stat2c=0
fi

# read the same file twice - Active kerberized read
if [ $do_active = 1 -a $do_double = 1 ]; then
  echo "================================================================================================================================"
  echo "`date` Double Active  kftpcp -p $strong_port -v enstore@$dcache:$f3 /dev/null& kftpcp -p $strong_port -v enstore@$dcache:$f3 /dev/null&"
                              kftpcp -p $strong_port -v enstore@$dcache:$f3 /dev/null& j1=$!; kftpcp -p $strong_port -v enstore@$dcache:$f3 /dev/null& j2=$!
  ps axuwww | grep kftpcp | grep -v grep
  (sleep 900; echo "after 900 seconds killing $j1 and $j2"; kill $j1 $j2)& j3=$!
  echo waiting for process $j1 and $j2
  wait $j1
  wait $j2
  kill $j3 2>/dev/null
  echo "both completed. unknown status"
  echo
fi

# read the same file twice - Passive kerberized read
if [ $do_passive = 1 -a $do_double = 1 ]; then
  echo "================================================================================================================================"
  echo "`date` Double Passive kftpcp -p $strong_port -m p -v enstore@$dcache:$f3 /dev/null& kftpcp -p $strong_port -m p -v enstore@$dcache:$f3 /dev/null&"
                              kftpcp -p $strong_port -m p -v enstore@$dcache:$f3 /dev/null& j1=$!; kftpcp -p $strong_port -m p -v enstore@$dcache:$f3 /dev/null& j2=$!
  ps axuwww | grep kftpcp | grep -v grep
  (sleep 900; echo after 900 seconds killing $j1 and $j2; kill $j1 $j2)&j3=$!
  echo waiting for process $j1 and $j2
  wait $j1
  wait $j2
  kill $j3 2>/dev/null
  echo "both completed. unknown status"
  echo
fi

${krbdir}/kdestroy
# ${krbdir}/klist -fea


# Active nonkerberized read
if [ $do_nonkerb_active = 1 ]; then
  echo "================================================================================================================================"
  echo "`date` anonymous ftp read enstore-test@$dcache:$f3 /dev/null"
  python -c '
def x(dum):
 return
from ftplib import FTP
import sys
ftp=FTP()
ftp.connect("'$dcache'",'$weak_port')
ftp.login("enstore-test","anon-read")
print ftp.retrbinary("RETR '$f3'",x)
'
  stat3=$?
  echo "`date`: Non-kerberized door check finished. stat = $stat3"

  if [ $stat3 -ne 0 ]; then
  echo enstore alarm --raise --severity "A" --root-error "anonymous ftp read enstore-test@$dcache:$f3 /dev/null.  STAT=$stat3"
  fi
  echo
else
  stat3=0
fi

stat4=0


# dccp read
if [ $do_dccp = 1 ]; then
  echo "================================================================================================================================"
  echo "`date` dccp read dccp -d 2 $file /dev/null"
                         dccp -d 2 $file /dev/null
  stat5=$?
  echo
else
  stat5=0
fi


# double dccp read
if [ $do_dccp = 1 -a $do_double = 1 ]; then
  echo "================================================================================================================================"
  echo "`date` Double dccp read dccp -d 2 $file /dev/null"
                                dccp -d 2 $file /dev/null& j1=$!; dccp -d 2 $file /dev/null& j2=$!
  ps axuwww | grep dccp | grep -v grep
  (sleep 900; echo after 900 seconds killing $j1 and $j2; kill $j1 $j2)&j3=$!
  echo waiting for process $j1 and $j2
  wait $j1
  wait $j2
  kill $j3 2>/dev/null
  echo "both completed. unknown status"
  echo
fi


wfile=${w}_$$_3
# dccp write
if [ $do_dccpwrite = 1 ]; then
  echo "================================================================================================================================"
  echo "`date`dccp write  dccp -d 2 $test_file $wfile"
                          dccp -d 2 $test_file $wfile
  stat6=$?
  echo
else
  stat6=0
fi


# dccp readback
if [ $do_dccpwrite = 1 -a $do_readback = 1 ]; then
  echo "================================================================================================================================"
  echo "rm -f $read_back"
        rm -f $read_back
  echo "`date` dccp readback dccp -d 2 $wfile $read_back"
                             dccp -d 2 $wfile $read_back
  stat7=$?
  if [ $stat7 -ne 0 ]; then
     echo enstore alarm --raise --severity "A" --root-error "dccp readback dccp -d 2 $wfile $read_back"
     stat7c=0
  else
     cmp $test_file $read_back
     stat7c=$?
     if [ $stat7c -ne 0 ]; then
        echo enstore alarm --raise --severity "A" --root-error "dccp readback cmp fails STAT=$stat7c"
     fi
  fi
  echo
else
  stat7=0
  stat7c=0
fi


stat=`expr $stat1 + $stat1a + $stat1b + $stat1c + $stat2 + $stat2a + $stat2b + $stat2c + $stat3 + $stat4 + $stat5 + $stat6 + $stat7  + $stat7c`
echo `date` Final status $stat
exit $stat
