#!/bin/bash

if [[  ! -d ./bin  ]]; then
    cd ../
    if [[ ! -d ./bin ]]; then
        echo "Error: it must be executed from the main, or bin directory"
        exit
    fi
fi

echo $$ > myprocid

main=`pwd`
dirbin=$main/bin
ERAdir=$main/../ExRootAnalysis
MAdir=$main/../MadAnalysis
td=$main/../td

p=0
n=''
t='default'
nrun=5
add=''

if [[  "$4" == ""  ]]; then
    echo 'Enter 1 for parallel 0 for serial run' 
    read p
    if [[  $p -gt 0 ]]; then
    echo 'Enter name for jobs on pbs queue'
    read n
    fi
    echo 'Enter run name'
    read t
    echo 'Enter number of wanted runs'
    read nrun 
else
     p=$1
     n=$2
     if [[  $p == 0  ]]; then
       n=' '
     fi
     t=$3
     nrun=$4
     if [[  $# -gt 5  ]]; then
       add=$5
       ERAdir="$MADGRAPH_BASE/MG_ME/ExRootAnalysis"
       MAdir="$MADGRAPH_BASE/MG_ME/MadAnalysis"
       td="$MADGRAPH_BASE/MG_ME/td"
     fi
fi

#$dirbin/generate_events $p $n $t\_1 $add
#cp -p $main/Events/$t\_1\_unweighted_events\.lhe\.gz $main/Events/$t\_unweighted_events\.lhe\.gz
#cp -p $main/Events/$t\_1\_banner.txt $main/Events/$t\_banner.txt
i=1

while [[  $i -le $nrun  ]]; do 
    echo "Running generate_events for run ${t}_$i (see ${t}_$i.log)"
    $dirbin/generate_events $p $n $t\_$i $add >& $t\_$i.log
#  $dirbin/merge.pl $main/Events/$t\_unweighted_events\.lhe\.gz $main/Events/$t\_$i\_unweighted_events\.lhe\.gz $main/Events/$t\_unweighted_events\.lhe\.gz $main/Events/$t\_banner.txt
  i=`expr $i + 1`
done 

#set allfiles = `ls $main/Events/$t\_\*\_unweighted_events\.lhe\.gz`
touch RunWeb

echo "Merging LHE files"
echo "Merging LHE files" >& ../status
$dirbin/merge.pl $main/Events/$t\_*\_unweighted_events\.lhe\.gz $main/Events/$t\_unweighted_events\.lhe\.gz $main/Events/$t\_banner.txt

$dirbin/gen_crossxhtml-pl $t

#
#  Create root file and plots
#

cd $main/Events

if [[ (-e $t\_unweighted_events.lhe.gz) ]]; then
    gunzip $t\_unweighted_events.lhe.gz
fi

if [[ (-x $ERAdir/ExRootLHEFConverter) && (-e $t\_unweighted_events.lhe) ]]; then
    echo "Creating Root File"
    echo "Creating Root File" >& ../status
    $dirbin/gen_crossxhtml-pl $t
    $ERAdir/ExRootLHEFConverter $t\_unweighted_events.lhe $t\_unweighted_events.root
fi

#if [[ (-x $ERAdir/ExRootMain) && (-e $t\_unweighted_events.root) && (-e ../Cards/plot_card.dat) ]]; then
#    echo "Creating Root Plots" >& ../status
#    $dirbin/gen_crossxhtml-pl
#    mkdir $t
#    cd $t
#    echo "../$t""_unweighted_events.root" > events.list
#    $dirbin/plot $ERAdir > plot.log
#    cd ..
#    $dirbin/plot_page-pl $t
#    mv plots.html $t\_plots.html
#    $dirbin/gen_crossxhtml-pl
#fi

if [[ (-x $MAdir/plot_events) && (-e $t\_unweighted_events.lhe) && (-e ../Cards/plot_card.dat) ]]; then
    echo "Creating Plots"
    echo "Creating Plots" >& ../status
    $dirbin/gen_crossxhtml-pl $t
    mkdir $t
    cd $t
    echo "../${t}_unweighted_events.lhe" > events.list
    $dirbin/plot $MAdir $td > plot.log
    cd ..
    $dirbin/plot_page-pl $t
    mv plots.html ${t}_plots.html
fi

if [[ -e $t\_unweighted_events.lhe ]]; then
    gzip $t\_unweighted_events.lhe
fi

cd ..
rm RunWeb
rm status

$dirbin/gen_crossxhtml-pl 
