#!/bin/bash
#
# This script is a part of SRM test suite. It is a wrapper around the main
# srm_test binary, calling it with apropriate args, checking output, 
# diffing the resulting files and notifying user about result.
#
# Written by Jiri Kosina <kosina@fzu.cz>
# Modified/Improved/Updated by GG (Gilbert.Grosdidier@cern.ch)
# Last rev: 27/12/2006 (GG) + 16/11/2007
#

# $Id: srmv1Suite,v 1.14 2007/11/17 02:20:58 grodid Exp $

## Comments by GG
## Required:
## - Grid proxy
## - srmv1 server running on target machine UNDER ROOT
## > setenv GRIDMAPDIR /etc/grid-security/gridmapdir/
## > setenv GRIDMAP /etc/grid-security/grid-mapfile
## > limit coredumpsize unlimited
## > /afs/cern.ch/project/gd/grodid/DPM-MYSQL-RH73/srmv1/srmv1 -c /afs/cern.ch/user/g/grodid/private/PROC/procps-3.2.3/dpns_lxb1901.config -l /var/tmp/grodid/logs/srmv1.log
## - NO $DPNS/DPM_HOST vars
## - settings: machine, protocol

## Usage: $0 targetNode userName protocol

target=$1
tstNAME=$2
proto=$3
sfile=$4
subTree=$5
##BADsite_domain=`echo $target | perl -pe 's/(\w*?)\.(.*)/$2/'`
site_domain=`echo $target | perl -pe 's/([-\w]*?)\.(.*)/$2/'`

# uhm ... let's clean this sometime :)
##export PATH=/opt/lcg/bin:/opt/globus/bin:/opt/globus/sbin:/opt/edg/bin:/usr/local/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/cern/pro/bin:/opt/gpt/sbin:/usr/java/j2sdk1.4.1_01/bin:/software/j2re1.3.1/bin:/opt/edg/bin:/opt/edg/sbin:/opt/edg/bin:/opt/edg/sbin:$PATH
export PATH=$PATH:/opt/lcg/bin:/opt/globus/bin:/opt/globus/sbin:/opt/edg/bin:/usr/local/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/cern/pro/bin:/opt/gpt/sbin:/usr/java/j2sdk1.4.1_01/bin:/software/j2re1.3.1/bin:/opt/edg/bin:/opt/edg/sbin:/opt/edg/bin:/opt/edg/sbin

## Implies that in standalone mode, we run it from test/srmv1 directory
## Not really required: PATH redefined for rfcp in my_rfcp_in.sh & my_rfcp_out.sh ## Not valid any more
export PATH=../../rfio:$PATH

# which protocol [gsiftp/gftp/rfio/(gsi)dcap]
PROTO=gsiftp
PROTO=gftp
#PROTO=rfio
#PROTO=dcap
PROTO=$proto

if [ "$PROTO" = "gftp" ]
then
    PROTO=gsiftp
fi

if [ "$VOTEAM"X = "X" ]
then
    voteam="dteam"
else
    voteam=$VOTEAM
fi

# this for castortest
SURL=srm://castorgridtest.cern.ch:8443//castor/cern.ch/grid/dteam/srm_test_suite_file.$$.$RANDOM

# this for castorgrid
SURL=srm://castorgrid.cern.ch:8443//castor/cern.ch/grid/dteam/srm_test_suite_file.$$.$RANDOM

# this for lxb1909 (Castor - 1760
SURL=srm://lxb1909.cern.ch:8443//castor/cern.ch/home/dteam/srm_test_suite_file.$$.$RANDOM
SURL=srm://lxb1760.cern.ch:8443//castor/cern.ch/home/dteam/srm_test_suite_file.$$.$RANDOM

# this for dcache at lxb1753 (dCache
#SURL=srm://lxb1753.cern.ch:8443//pnfs/cern.ch/data/dteam/srm_test_suite_file.$$.$RANDOM
#BADSURL=srm://lxb1753.cern.ch:8443//pnfs/cern.ch/data/srm_test_suite_file.$$.$RANDOM

# this for dcache at lxn1180 (dCache
#SURL=srm://lxn1180.cern.ch:8443//pnfs/cern.ch/data/dteam/srm_test_suite_file.$$.$RANDOM

# this for dcache at lxb0740 (dCache
#SURL=srm://lxb0740.cern.ch:8443//pnfs/cern.ch/data/dteam/srm_test_suite_file.$$.$RANDOM

# this for dpm at lxb1901 (GG machine ... - not GSIFTP enabled yet
SURL=srm://lxb1901.cern.ch:8443//dpm/dteam/grodid/srm_test_suite_file.$$.$RANDOM
SURL=srm://${target}:8443//dpm/dteam/grodid/srm_test_suite_file.$$.$RANDOM
##SURL=srm://${target}:8443//dpm/dteam/grodid/otherdir/srm_test_suite_file.$$.$RANDOM

# this for dpm at lxb1727, lxb1904, lxgate22 (GG machines ...
if [ "$subTree"X = "X" ]
then
    SURL=srm://${target}:8443//dpm/${site_domain}/home/${voteam}/srmv1_Tfile.$$.$RANDOM
    SURL=srm://${target}:8443//dpm/${site_domain}/home/${voteam}/srmv1_Tfile.${sfile}.$RANDOM
else
    SURL=srm://${target}:8443//dpm/${site_domain}/home/${voteam}/${subTree}/srmv1_Tfile.$$.$RANDOM
    SURL=srm://${target}:8443//dpm/${site_domain}/home/${voteam}/${subTree}/srmv1_Tfile.${sfile}.$RANDOM
fi

# this for dpm at lxb0732 (JPB machine ... srmv1 ? RH73 - not GSIFTP enabled yet ?
#SURL=srm://lxb0732.cern.ch:8443//dpm/dteam/grodid/srm_test_suite_file.$$.$RANDOM

# this for dpm at lxb0739 (Classic - 1767 - CANNOT work - not SRM enabled !
#SURL=srm://lxb0739.cern.ch:8443//dpm/dteam/grodid/srm_test_suite_file.$$.$RANDOM

case "$target" in
# this for castorgrid
    castor)  SURL=srm://castorgrid.cern.ch:8443//castor/cern.ch/grid/dteam/srm_test_suite_file.$$.$RANDOM;;
# this for lxb1909 (Castor - 1760
    lxb1909) SURL=srm://lxb1909.cern.ch:8443//castor/cern.ch/home/dteam/srm_test_suite_file.$$.$RANDOM;;
    lxb1760) SURL=srm://lxb1760.cern.ch:8443//castor/cern.ch/home/dteam/srm_test_suite_file.$$.$RANDOM;;
# this for dcache at lxb1753 (dCache - 0740
    lxb1753) SURL=srm://lxb1753.cern.ch:8443//pnfs/cern.ch/data/dteam/srm_test_suite_file.$$.$RANDOM;;
    lxb0740) SURL=srm://lxb0740.cern.ch:8443//pnfs/cern.ch/data/dteam/srm_test_suite_file.$$.$RANDOM;;
esac

##### this for castorgrid
####if [ "$target" = "castor" ]
####    SURL=srm://castorgrid.cern.ch:8443//castor/cern.ch/grid/dteam/srm_test_suite_file.$$.$RANDOM
####
##### this for lxb1909 (Castor - 1760
####if [ "$target" = "lxb1909" ]
####    SURL=srm://lxb1909.cern.ch:8443//castor/cern.ch/home/dteam/srm_test_suite_file.$$.$RANDOM
####

##echo "[++]" ./$SRM_TEST $PROTO $SURL $SURL2
##exit 0


SURL2=$SURL.SECOND_FILE
BUG=0
SRM_TEST=srm_test
SRM_TEST=srm_testjiri_26
SRM_TEST=srm_testJiri

/bin/rm -f TESTFILE*
/bin/rm -f OUTPUT

# create file that would be used
echo "This is my test file." > TESTFILE
echo "This is my test file2." > TESTFILE2
#cp -p /boot/vmlinux-2.4.21-20.0.1.EL.cernsmp TESTFILE2
##GOODcp -p /boot/vmlinux-2.4.21-20.EL.cernsmp TESTFILE2
##??ln -sb /tmp/grodid/csec_trace.log TESTFILE2

##GG chmod +x ./my_gsiftp_in.sh
##GG chmod +x ./my_gsiftp_out.sh
##GG chmod +x ./my_rfcp_in.sh
##GG chmod +x ./my_rfcp_out.sh
##GG chmod +x ./my_dccp_in.sh
##GG chmod +x ./my_dccp_out.sh

## echo "[+] ./srm_test" $PROTO $SURL $SURL2
## chmod +x ./srm_test
## echo "***"
## ./srm_test $PROTO $SURL $SURL2
echo "[+]" ./$SRM_TEST $PROTO $SURL $SURL2
##GG chmod +x ./$SRM_TEST
echo "***"
##GG ./$SRM_TEST $PROTO $SURL $SURL2
$SRM_TEST $PROTO $SURL $SURL2
RETVAL=$?
echo "Return value from test suite is $RETVAL"
echo "***"
if [ $RETVAL -eq 1 ]; then
	BUG=1
	echo "There was some bug(s) during run of test suite."
	echo "See the output and look for lines beginning with [!]"
	echo "If error messages follow, this is probably due to previous SRM errors"
	echo ""
	echo "However if everything later passes, this doesn't mean that the SRM" 
        echo "implementation is OK - it just means that the files were correctly transmitted"
fi

# First file
BUGF=0
if ( ! [ -f TESTFILE ]; ) then
	echo "[!] TESTFILE not found!"
	BUGF=1
fi
if ( ! [ -f TESTFILE.x ]; ) then
	echo "[!] TESTFILE.x not found!"
	BUGF=1
fi
if ( ! [ -f TESTFILE.x.y.y ]; ) then
	echo "[!] TESTFILE.x.y.y not found!"
	BUGF=1
fi

if ( ! [ $BUGF -eq 1 ]; ) then

	diff TESTFILE TESTFILE.x > /dev/null 2>&1
	if [ $? -eq 1 ]; then
		echo "[!] #1: Strange differ (diff of TESTFILE and TESTFILE.x)"
		BUG=1
	else
		echo "[+] #1: PASSED (diff of TESTFILE and TESTFILE.x)"
	fi

	# this should differ
	diff TESTFILE.x TESTFILE.x.y.y > /dev/null 2>&1
	if [ $? -eq 1 ]; then
		echo "[+] #2: PASSED (diff of TESTFILE.x and TESTFILE.x.y.y)"
	else
		echo "[!] #2: Strange equality (diff of TESTFILE.x and TESTFILE.x.y.y)"
		BUG=1
	fi

	diff TESTFILE.x.y TESTFILE.x.y.y > /dev/null 2>&1
	if [ $? -eq 1 ]; then
		echo "[!] #3: Strange differ (diff of TESTFILE.x.y and TESTFILE.x.y.y)"
		BUG=1
	else
		echo "[+] #3: PASSED (diff of TESTFILE.x.y and TESTFILE.x.y.y)"
	fi
else
	BUG=1
fi

# Second file
BUGF=0
if ( ! [ -f TESTFILE2 ]; ) then
	echo "[!] TESTFILE2 not found!"
	BUGF=1
fi
if ( ! [ -f TESTFILE2.x ]; ) then
	echo "[!] TESTFILE2.x not found!"
	BUGF=1
fi
if ( ! [ -f TESTFILE2.x.y.y ]; ) then
	echo "[!] TESTFILE2.x.y.y not found!"
	BUGF=1
fi

if ( ! [ $BUGF -eq 1 ]; ) then

	diff TESTFILE2 TESTFILE2.x > /dev/null 2>&1
	if [ $? -eq 1 ]; then
		echo "[!] #1: Strange differ (diff of TESTFILE2 and TESTFILE2.x)"
		BUG=1
	else
		echo "[+] #1: PASSED (diff of TESTFILE2 and TESTFILE2.x)"
	fi

	# this should differ
	diff TESTFILE2.x TESTFILE2.x.y.y > /dev/null 2>&1
	if [ $? -eq 1 ]; then
		echo "[+] #2: PASSED (diff of TESTFILE2.x and TESTFILE2.x.y.y)"
	else
		echo "[!] #2: Strange equality (diff of TESTFILE2.x and TESTFILE2.x.y.y)"
		BUG=1
	fi

	diff TESTFILE2.x.y TESTFILE2.x.y.y > /dev/null 2>&1
	if [ $? -eq 1 ]; then
		echo "[!] #3: Strange differ (diff of TESTFILE2.x.y and TESTFILE2.x.y.y)"
		BUG=1
	else
		echo "[+] #3: PASSED (diff of TESTFILE2.x.y and TESTFILE2.x.y.y)"
	fi
else
	BUG=1
fi



# final word
if [ $BUG -eq 1 ]; then
	echo "Final result: NOT passed all tests"
else
	echo "Final result: PASSED all tests"
fi

exit $BUG

