#!/bin/bash


TOPDIR=`pwd`

function timed_transfer
{
    min=$1
    max=$2
    cmd=$3

    echo
    echo
    echo
    echo
    echo "-----------------------------------------"
    echo

    start_ts=`/bin/date +'%s'`

    echo "Test command: $cmd"
    echo 

    $cmd
    rc=$?
    if [ "x$rc" != "x0" ]; then
        echo "Error: Command returned $rc"
        exit 1
    fi

    end_ts=`/bin/date +'%s'`
    duration=$(($end_ts - $start_ts))
    duration_minutes=$(($duration / 60))
    echo
    echo "Test took $duration_minutes minutes"
    echo "The upper limit was $max minutes"
    echo "The lower limit was $min minutes"
    echo

    if [ $duration_minutes -gt $max ]; then
        echo "Error: Limit exceeded!"
        exit 1
    fi

    if [ $duration_minutes -lt $min ]; then
        echo "Error: Test was faster than lower limit - time to lower limits!"
        exit 1
    fi

    echo "Test passed!"
}

# make sure we use the same g-u-c every time
export GLOBUS_LOCATION=/ccg/software/globus/default
. $GLOBUS_LOCATION/etc/globus-user-env.sh

# S3 creds
export S3CFG=~/.s3cfg

echo
echo
echo "================================================================="
echo 
echo
echo "General tests..."
echo
echo

timed_transfer 0 30 "pegasus-transfer -f mixed-protocols.in"

echo
echo
echo "================================================================="
echo 
echo
echo "S3 test..."
echo
echo

timed_transfer 0 30 "pegasus-transfer -f s3-step1.in"

timed_transfer 0 30 "pegasus-transfer -f s3-step2.in"

timed_transfer 0 30 "pegasus-cleanup -f s3-cleanup.in"

echo
echo
echo "================================================================="
echo 
echo
echo "3rd party gridftp test..."
echo
echo


timed_transfer 0 20 "pegasus-transfer -f 3rdparty.in"

timed_transfer 0 20 "pegasus-transfer -f 3rdparty-step2.in"

timed_transfer 0 10  "pegasus-cleanup -f 3rdparty-cleanup.in"

timed_transfer 0 40 "pegasus-transfer -f many-small-files-3rdparty.in"

timed_transfer 0 10 "pegasus-transfer -f medium-files-push-3rdparty.in"


echo
echo
echo "================================================================="
echo 
echo
echo "Starting NAT/Firewall simulation..."
echo
echo

# NAT/Firewall simulation - set the tcp port range to something that is firewalled 
export GLOBUS_TCP_PORT_RANGE=5000,6000

timed_transfer 0 20 "pegasus-transfer -f nat.in"

timed_transfer 0 30 "pegasus-transfer -f medium-files-pull.in"

timed_transfer 0 30 "pegasus-transfer -f medium-files-push.in"


exit 0

