#!/bin/sh 
################################################################################
#this is a first try on a script to join all GridMaAnova workflows.
# Based on ideas from the gridjob submission (GJS) by ....
# 
#Author: Marcia A. Inda.
################################################################################

PATH=.:$PATH; export PATH 

#------------------------------------------------------------------------------
#set default parameters
#------------------------------------------------------------------------------
#templates
templ_obs=template-matest-obs.xml
templ_perm=template-matest-perm.xml
templ_concat=template-matest-concat.xml

#VLAM command line
vlam_sh='/home/vleroot/wsvlam/development/composer/bin/wsvlam-composer-start.sh'
vlam_sh_options='-nogui'

#default parameters for GridMaAnova script
nperm=2000                         #number of permutations for full job
critical=1                         #critical
seed=1	    	    	    	   #rng seed
rng=default                        #rng type

#from the old gridjobsubmission scripts
#pmt=3                              #start column intensity data
#id=1                               #column spot identifiers
#these are optional. 
#contrast.file   <- "contrast.txt"  #contrast file for maanova
#This are the default value's
#flag      	 <- FALSE           #splot flag 

clusters="fs2.das3.science.uva.nl"       #clusters on which the job will run 
refresh=60          #60 minutes, frequency at which jobstatus is checked 

#------------------------------------------------------------------------------
#functions
#------------------------------------------------------------------------------
#checks if a file is present
check_for_file()
{
    if tty -s; then # is this a terminal?
    	echo Please enter $1 file name:
	read filename_
    	until [ $filename_ -a -r $filename_ ]; do
    	    echo file \'$filename_\' not readable. Please enter $1 file name:
	    read filename_
	done
    else
    	echo 1>&2 Fatal: file \'$1\' file not readble or does not exist"!"
    	exit 127 # exits with a non-zero error.  
    fi
    return 0
}


read_required_parameter()
{
    if tty -s; then # is this a terminal?
    	echo "Please enter $1 value:"
	read parameter_
    	until [ $parameter_ ]; do
    	    echo Parameter \'$parameter_\' not valid. Please enter $1 value:
	    read parameter_
	done
    else
    	echo 1>&2 "Fatal: parameter \'$1\' not set!"
    	exit 127 # exits with a non-zero error.  
    fi
    return 0
}

read_optional_parameter()
{
    if tty -s; then # is this a terminal?
    	echo Please enter $1 value:
	read parameter_
    fi
    if [ ! $parameter_ ]; then
    	echo Using \'$parameter_\' default value.
	return 127
    fi
    return 0
}

#------------------------------------------------------------------------------
echo Starting GridMaAnova
#------------------------------------------------------------------------------
date 


#------------------------------------------------------------------------------
#check command line arguments
#------------------------------------------------------------------------------
if [ $# -le 0 ]; then # no arguments given?
    if tty -s; then # is this a terminal?
    	echo Please enter parameters file name:
	read parameters_file
    else
    	echo 1>&2 "Fatal: Parameters file missing!"
    	exit 127 # exits with a non-zero error.  
    fi
else
    parameters_file=$1
fi

if [ ! -r $parameters_file ]; then
    check_for_file parameters
    parameters_file=$filename_
fi


#TO DO: add support for command line in which we can specify the templates
#------------------------------------------------------------------------------
echo "Checking if all needed files are present"
#------------------------------------------------------------------------------
if [ ! -r $templ_obs ]; then
    check_for_file "matest-obs template"
    templ_obs=$filename_
fi

if [ ! -r $templ_perm ]; then
    check_for_file "matest-perm template"
    templ_perm=$filename_
fi

if [ ! -r $templ_concat ]; then
    check_for_file "matest-concat template"
    templ_concat=$filename_
fi

if [ ! -x $vlam_sh ]; then
    check_for_file "WSVLAM executable shell"
    vlam_sh=$filename_
fi

#------------------------------------------------------------------------------
source $parameters_file
#------------------------------------------------------------------------------
if [ ! $experiment_name ]; then
    read_required_parameter "experiment name"
    experiment_name=$parameter_
fi
if [ ! $formula ]; then
    read_required_parameter "formula"
    formula=$parameter_
fi
if [ ! $random ]; then
    read_required_parameter "random term"
    random=$parameter_
fi
if [ ! $term ]; then
    read_required_parameter "term to be tested"
    term=$parameter_
fi
if [ ! $data_file ]; then
    read_required_parameter "data file"
    data_file=$parameter_
fi
if [ ! $design_file ]; then
    read_required_parameter "design file"
    design_file=$parameter_
fi

#------------------------------------------------------------------------------
echo
echo Template workflows:
#------------------------------------------------------------------------------
echo "    templ_obs    = " $templ_obs
echo "    templ_perm   = " $templ_perm
echo "    templ_concat = " $templ_concat

#------------------------------------------------------------------------------
echo
echo "VLAM command line script: " $vlam_sh
#------------------------------------------------------------------------------
echo
echo "Parameters:"
#------------------------------------------------------------------------------
echo "    experiment_name = " $experiment_name

echo "    data_file       = " $data_file	
echo "    design_file     = " $design_file

echo "    temp_url        = " $temp_url

echo "    formula         = " $formula
echo "    random          = " $random
echo "    term            = " $term
echo "    nperm           = " $nperm
echo "    critical        = " $critical
echo "    seed            = " $seed
echo "    rng             = " $rng
#echo "    pmt             = " $pmt #start column intensity data
#echo "    id              = " $id  #column spot identifiers
echo
echo Optional parameters:
echo "    contrast_file   = " $contrast_file
#echo "    flag      	   = " $flag #splot flag 
echo "    cluster         = " $clusters
#echo "    refresh         = " $refresh

#------------------------------------------------------------------------------
echo
echo Executed workflows:
#------------------------------------------------------------------------------
echo "    " $experiment_name.obs.xml
echo "    " $experiment_name.perm.xml
echo "    " $experiment_name.concat.xml

#------------------------------------------------------------------------------
echo
echo Creating credentials
#maybe it is nice to add verbose command to print the following variables?
if [ ! $GRID_SERVICE_HOST ]; then
    read_required_parameter "GRID_SERVICE_HOST"
    GRID_SERVICE_HOST=$parameter_
fi
if [ ! $GRID_SERVICE_PORT ]; then
    read_required_parameter "GRID_SERVICE_PORT"
    GRID_SERVICE_PORT=$parameter_
fi
if [ ! $GRID_SERVICE_URL ]; then
    read_required_parameter "GRID_SERVICE_URL"
    GRID_SERVICE_URL=$parameter_
fi
if [ ! $GLOBUS_LOCATION ]; then
    read_required_parameter "GLOBUS_LOCATION"
    GLOBUS_LOCATION=$parameter_
fi
if [ ! $JAVA_HOME ]; then
    read_required_parameter "JAVA_HOME"
    JAVA_HOME=$parameter_
fi
if [ ! $VLAM_HOME ]; then
    read_required_parameter "VLAM_HOME"
    VLAM_HOME=$parameter_
fi
export GRID_SERVICE_HOST GRID_SERVICE_PORT GRID_SERVICE_URL
export GLOBUS_LOCATION JAVA_HOME VLAM_HOME

for i in $GLOBUS_LOCATION/lib/*.jar
 do CLASSPATH=$i:$CLASSPATH
done
for i in $VLAM_HOME/lib/*.jar
 do CLASSPATH=$i:$CLASSPATH
done
export CLASSPATH

#init the proxy ##ask Dmitry what are the correct commads
#java org.globus.tools.DefaultProxyInit

#prints proxy info 
java org.globus.tools.ProxyInfo
#proxytime= `(set \`java org.globus.tools.ProxyInfo\`; echo $1)`
#echo $proxytime

TODAY=`(set \`date\`; echo $1)`

echo $TODAY
#------------------------------------------------------------------------------
madata_file=$temp_url/$experiment_name.madata.Rdata
matestobs_file=$temp_url/$experiment_name.matest_obs.Rdata

matesttime_file=$log_url/$experiment_name.matest_time.log
local_matesttime_file=$log_url/$experiment_name.matest_time.log

matestobs_logfile=$log_url/$experiment_name.matest_obs.log
local_matestobs_logfile=$log_url/$experiment_name.matest_obs.log



#set matest-obs parameters using sed
sed -e "s#madatafile_value_#$madata_file#g" \
    -e "s#designfile_value_#$design_file#g" \
    -e "s#datafile_value_#$data_file#g" \
    -e "s#matestobsfile_value_#$matestobs_file#g" \
    -e "s#obstimefile_value_#$matesttime_file#g" \
    -e "s#obslogfile_value_#$matestobs_logfile#g" \
    -e "s#formula_value_#$formula#g" \
    -e "s#random_value_#$random#g" \
    -e "s#term_value_#$term#g" \
    -e "s#critical_value_#$critical#g" \
    -e "s#seed_value_#$seed#g" \
    -e "s#rng_value_#$rng#g" \
    -e "s#madatafile_host_#$clusters#g" \
    -e "s#designfile_host_#$clusters#g" \
    -e "s#datafile_host_#$clusters#g" \
    -e "s#matestobsfile_host_#$clusters#g" \
    -e "s#obstimefile_host_#$clusters#g" \
    -e "s#obslogfile_host_#$clusters#g" \
    -e "s#matestobs_host_#$clusters#g" \
    $templ_obs > $experiment_name.obs.xml

if [ ! -r $experiment_name.obs.xml ]; then
    echo 1>&2 Fatal: error creating $experiment_name.obs.xml "!"
    exit 127 # exits with a non-zero error.  
fi

#------------------------------------------------------------------------------
#run matest-obs
echo +++++++++++++++++++++++++++++++++++++++++++
echo Executing $vlam_sh $experiment_name.obs.xml
$vlam_sh $vlam_sh_options $experiment_name.obs.xml
echo +++++++++++++++++++++++++++++++++++++++++++
echo $log_url
echo $local_matestobs_logfile
echo +++++++++++++++++++++++++++++++++++++++++++

if [ ! "`grep "R script run-grid-matest.obs.R finished" $local_matestobs_logfile`" ]; then
    echo "something wrong!"
else
    echo "GridMaTest-obs finished succesfully"
fi
#compute njobs


fpermRdata_file=$perm_url/$experiment_name.fperm.Rdata
maperm_log=$log_url/$experiment_name.maperm.log

#set matest-perm parameters
# madata_file and the $matestobs_file
# are used as input file to the second workflow
# 
#
sed -e "s#Rdata_file_value_#$madata_file#g" \
    -e "s#matestRdata_value_#$matestobs_file#g" \
    -e "s#fpermRdata_value_#$fpermRdata_file#g" \
    -e "s#mapermlog_file_value_#$maperm_log#g" \
    -e "s#gridMatest_perm_host_#$clusters#g" \
    -e "s#dataRdata_host_#$clusters#g" \
    -e "s#fpermRdata_host_#$clusters#g" \
    -e "s#matestRdata_host_#$clusters#g" \
    -e "s#mapermlog_host_#$clusters#g" \
     $templ_perm > $experiment_name.perm.xml

#run matest-perm
echo Executing $vlam_sh $experiment_name.perm.xml
$vlam_sh $vlam_sh_options $experiment_name.perm.xml
echo +++++++++++++++++++++++++++++++++++++++++++
echo $log_url
echo $local_matestobs_logfile
echo +++++++++++++++++++++++++++++++++++++++++++


fconcatRdata_file=$temp_url/$experiment_name.ftest-results.Rdata
maconcat_log=$log_url/$experiment_name.maconcat.log

#set matest-concat parameters
sed -e "s#perm_url_#$perm_url#g" \
    -e "s#matest_Rdata_file_value_#$matestobs_file#g" \
    -e "s#ftest_results_Rdata_file_value_#$fconcatRdata_file#g" \
    -e "s#maconcat_log_file_value_#$maconcat_log#g" \
    -e "s#gridmatest_concat_host_#$clusters#g" \
    -e "s#ftest_results_Rdata_host_#$clusters#g" \
    -e "s#matest_Rdata_host_#$clusters#g" \
    -e "s#maconcat_log_host_#$clusters#g" \
    -e "s#tarfiles_host_#$clusters#g" \
    $templ_concat > $experiment_name.concat.xml


#run matest-concat
echo Executing $vlam_sh $experiment_name.concat.xml
$vlam_sh $vlam_sh_options $experiment_name.concat.xml
#echo +++++++++++++++++++++++++++++++++++++++++++
#echo $log_url
#echo $local_matestobs_logfile
#echo +++++++++++++++++++++++++++++++++++++++++++
