#!/bin/csh -f

if ($#argv != 1) then
    echo "Usage: ./test.csh <test_nr>"
    exit 1
endif

set test_nr=$1
set test_input_dir="/u/c/s/cs537-1/ta/p6/test_data/"
set test_out_dir="/u/c/s/cs537-1/ta/p6/p6/output/"
set metadata_dir="metadata"
set test_files_dir="test_files"

if( ! -d $metadata_dir) then
    mkdir $metadata_dir
endif

switch($test_nr)
case 1:
    if ( -f makefile || -f Makefile || -f MAKEFILE ) then
        if ( -f README || -f Readme || -f readme ) then
           goto pass
        else
           echo "ERROR: README file not present"
           goto fail
        endif
    else
        echo "ERROR: makefile not present"
        goto fail
    endif
breaksw
case 2:
    rm -f libfsprotect.so
    make >& /dev/null
    if (! -f libfsprotect.so) then
           echo "ERROR: not compilable using make"
           goto fail
    endif
breaksw
case 5:
    if(! -f libfsprotect.so) then
       echo "Please compile libfsprotect.so before running this Test"
       goto fail
    endif
    setenv FSPROTECT_HOME ${metadata_dir}
    rm -f ${metadata_dir}/* >& /dev/null
    cp -f ${test_input_dir}/file2 .
    rm -f myprog
    gcc -o myprog ${test_input_dir}test${test_nr}.c 
    if(! -f myprog ) then
       goto fail
    endif
    setenv LD_PRELOAD ./libfsprotect.so
    ./myprog 
    set exit_code=$status
    unsetenv LD_PRELOAD
    rm  -f ${metadata_dir}/*
    rm -f file2
    rm -f myprog
    if($exit_code != 0) then
       goto fail
    endif
breaksw
case 3:
case 4:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
case 26:
case 29:
    if(! -f libfsprotect.so) then
       echo "Please compile libfsprotect.so before running this Test"
       goto fail
    endif
    setenv FSPROTECT_HOME ${metadata_dir}
    rm -f ${metadata_dir}/* >& /dev/null
    cp -f ${test_input_dir}/file5 .
    rm -f myprog
    gcc -o myprog ${test_input_dir}test${test_nr}.c -ldl
    if(! -f myprog ) then
       goto fail
    endif
    setenv LD_PRELOAD ./libfsprotect.so
    ./myprog 
    set exit_code=$status
    unsetenv LD_PRELOAD
    rm -f ${metadata_dir}/*
    rm -f file5
    rm -f myprog
    if($exit_code != 0) then
       goto fail
    endif
breaksw
case 27:
    if(! -f libfsprotect.so) then
       echo "Please compile libfsprotect.so before running this Test"
       goto fail
    endif
    setenv FSPROTECT_HOME ${metadata_dir}
    rm -f ${metadata_dir}/* >& /dev/null
    cp -f ${test_input_dir}gen_test_file .
    rm -f myprog
    gcc -o myprog ${test_input_dir}test${test_nr}.c -ldl
    if(! -f myprog ) then
       goto fail
    endif
#create 100 files
    mkdir ${test_files_dir}
    set i=0
    while ( $i < 100 ) 
       ./gen_test_file ${test_files_dir}/file$i 4
       set i=`expr $i + 1`
    end
    setenv LD_PRELOAD ./libfsprotect.so
    ./myprog 100 4
    set exit_code=$status
    unsetenv LD_PRELOAD
    rm -f ${metadata_dir}/*
    rm -f myprog
#delete the 100 files
    set i=0
    while ( $i < 100 ) 
       rm -f ${test_files_dir}/file$i
       set i=`expr $i + 1`
    end
    rm -rf ${test_files_dir}
    rm -f gen_test_file
    if($exit_code != 0) then
       goto fail
    endif
breaksw
case 28:
    if(! -f libfsprotect.so) then
       echo "Please compile libfsprotect.so before running this Test"
       goto fail
    endif
    setenv FSPROTECT_HOME ${metadata_dir}
    rm -f ${metadata_dir}/* >&  /dev/null
    cp -f ${test_input_dir}/gen_test_file .
    rm -f myprog
    gcc -o myprog ${test_input_dir}test${test_nr}.c -ldl
    if(! -f myprog ) then
       goto fail
    endif
#create a large file of 4 MB
    ./gen_test_file file1 1000 
    setenv LD_PRELOAD ./libfsprotect.so
    ./myprog 1000
    set exit_code=$status
    unsetenv LD_PRELOAD
    rm -f ${metadata_dir}/*
    rm -f myprog
#delete the file
    rm -f file1
    rm -f gen_test_file
    if($exit_code != 0) then
       goto fail
    endif
breaksw
default:
   echo "Invalid <test_nr> option used"
   exit 1
breaksw
endsw

pass:
   echo "Test ${test_nr}: Pass"
   exit 0
fail:
   echo "Test ${test_nr}: Fail"
   exit 1
 
