#!/bin/bash

project=$1
host=$2
shift;
shift;
rest_args=$@

swat_ini_file='swat.ini'

# apply home directory swat settings
test -f ~/$swat_ini_file && source ~/$swat_ini_file

test -f $project/host && host=${host:=`cat $project/host`}

if [ -n "${host}"  ] ; then

    # host valiadtion; it should not contain '../'
    if perl -e '$l=shift(); $l=~/\.\.\// or exit 1' "${host}"; then
        echo "bad host: ${host}"
        exit 1
    fi

    rm -rf ~/.swat/.cache/$host
    mkdir -p ~/.swat/.cache/$host
    swat_environment_settings_file=~/.swat/.cache/$host/$swat_ini_file
    session_file=~/.swat/.cache/$host/session.ini
else
    echo "usage: swat project HOST:port prove-options"
    exit 1
fi

packages_dir=~/.swat/.packages/

if [ \(  -n "${project}"  \) -a \( -d "${project}" \) ]; then
    is_swat_package=0
elif perl -M$project -MFile::ShareDir -e "print(File::ShareDir::module_dir($project))"  ; then
    is_swat_package=1
    project=`perl -M$project -MFile::ShareDir -e "print(File::ShareDir::module_dir($project))"`;
else
    echo "usage: swat project HOST:port prover-options"
    exit 1
fi


# save swat environmetal settings to re-apply later

echo > $swat_environment_settings_file

if [ -n "${debug}" ]; then
    echo "debug=$debug" >> $swat_environment_settings_file
fi

if [ -n "${debug_bytes}" ]; then
    echo "debug_bytes=$debug_bytes" >> $swat_environment_settings_file
fi

if [ -n "${try_num}" ]; then
    echo "try_num=$try_num" >> $swat_environment_settings_file
fi

if [ -n "${ignore_http_err}" ]; then
    echo "ignore_http_err=$ignore_http_err" >> $swat_environment_settings_file
fi

if [ -n "${curl_connect_timeout}" ]; then
    echo "curl_connect_timeout=$curl_connect_timeout" >> $swat_environment_settings_file
fi

if [ -n "${curl_max_time}" ]; then
    echo "curl_max_time=$curl_max_time" >> $swat_environment_settings_file
fi


if [ -n "${curl_params}" ]; then
    echo "curl_params=$curl_params" >> $swat_environment_settings_file
fi

if [ -n "${port}" ]; then
    echo "port=$port" >> $swat_environment_settings_file
fi


# re-apply swat environmetal settings
source $swat_environment_settings_file

# sets defaults
debug=${debug:=0}
debug_bytes=${debug_bytes:=500}
ignore_http_err=${ignore_http_err:=0}
try_num=${try_num:=2}
curl_connect_timeout=${curl_connect_timeout:=20}
curl_max_time=${curl_max_time:=20}
swat_use_proxy=${swat_use_proxy:=0}


# save `merged' result to session file

echo "debug=$debug" > $session_file
echo "debug_bytes=$debug_bytes" >> $session_file
echo "try_num=$try_num" >> $session_file
echo "ignore_http_err=$ignore_http_err" >> $session_file
echo "curl_connect_timeout=$curl_connect_timeout" >> $session_file
echo "curl_max_time=$curl_max_time" >> $session_file


if [ -n "${curl_params}" ]; then
    echo "curl_params=$curl_params" >> $session_file
fi

if [ -n "${port}" ]; then
    echo "port=$port" >> $session_file
fi


safe_project=`perl -MFile::Basename -e '$i=$ARGV[0]; s{\/$}[], chomp for $i; print $i' $project`

reports_dir=~/.swat/reports/$host/
rm -rf $reports_dir
mkdir -p $reports_dir

for f in `find $safe_project/ -type f -name get.txt -o -name post.txt`; do

    test_dir=`perl -e '$sp=$ARGV[0]; s{\w+\.txt$}[] for $sp; chomp $sp; print $sp' $f`;

    unset try_num
    unset ignore_http_err
    unset curl_connect_timeout
    unset curl_max_time
    unset curl_params
    unset port

    test -f $safe_project/$swat_ini_file && source $safe_project/$swat_ini_file
    test -f $test_dir/$swat_ini_file && source $test_dir/$swat_ini_file
    source $session_file

    path=`perl -e '$sp=$ARGV[0]; $p=$ARGV[1]; s{^$sp}[], s{\w+\.txt}[], s{/$}[] for $p; chomp $p; $p = "/"  unless $p; print $p' $safe_project $f`;
    mkdir -p "${reports_dir}/${path}";

    http_meth=`perl -e '$p=$ARGV[0]; $p=~/(\w+)\.txt$/ and print uc($1)' $f`;

    if [ -n "${port}" ]; then
        http_url="$host:$port"
    else
        http_url=$host
    fi

    curl_cmd="curl -X $http_meth"

    curl_cmd="${curl_cmd} -k --connect-timeout $curl_connect_timeout -m $curl_max_time -D - -L --stderr -"

    if [ "$ignore_http_err" -eq '0' ]; then
        curl_cmd="$curl_cmd -f"
    fi

    if [ -n "${curl_params}" ]; then
       curl_cmd="$curl_cmd ${curl_params}"
    fi

    real_path=`perl -e  '$p=shift(); s{:(\w+)}($ENV{$1}||":$1")ge for $p; print $p' $path`

    curl_cmd="$curl_cmd $http_url$real_path"

    if [ "$http_meth" = 'GET' ]; then
        tfile="${reports_dir}/${path}/00.t"
    else
        tfile="${reports_dir}/${path}/00.post.t"
    fi

    echo 'BEGIN { push @INC, q{'$safe_project'/lib}; }'  > $tfile
    echo >> $tfile


    echo "use Test::More q{no_plan};"  >> $tfile
    echo $\content_file = q{"${reports_dir}${path}/content.${http_meth}.txt};"  >> $tfile
    echo $\path = q{"${path}};"  >> $tfile
    echo $\project = q{"${safe_project}};"  >> $tfile
    echo $\http_meth = q{"${http_meth}};"  >> $tfile
    echo $\url = q{"${http_url}};"  >> $tfile
    echo $\debug = $debug';'  >> $tfile
    echo $\debug_bytes = $debug_bytes';'  >> $tfile
    echo $\ignore_http_err = $ignore_http_err';'  >> $tfile
    echo $\try_num = $try_num';'  >> $tfile
    echo $\curl_cmd = q{"${curl_cmd}};"  >> $tfile
    echo $\is_swat_package = $is_swat_package';'  >> $tfile
    echo >> $tfile

    echo "require swat;"  >> $tfile

    test -f $safe_project/hook.pm && echo "require '"`pwd`"/${safe_project}/hook.pm';"  >> $tfile
    test -f $test_dir/hook.pm && echo "require '"`pwd`"/${test_dir}/hook.pm';"  >> $tfile

    echo >> $tfile

    echo "SKIP: {" >> $tfile
    echo -e "\tgenerate_asserts(q{$f},1)" >> $tfile;
    echo >> $tfile
    echo "}" >> $tfile

done;

if [ -z "${rest_args}" ] && [ -n "${prove_options}" ]; then
    rest_args=("$prove_options")
    prove -m -r $rest_args  $reports_dir;
elif [ -n "${rest_args}"  ]; then
    prove -m -r  $rest_args $reports_dir;
else
    prove -m -r -v $reports_dir;
fi





