#!/bin/bash
#
#  First get to main level directory for this process
#

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

#  Now let shell know where to find important executables

main=`pwd`
dirbin=$main/bin
mgdir=$main/../MadGraphII
mad2=$mgdir/mg2
helas=$main/../HELAS
mgmedir=$main/..

# Local versions to work on the cluster

tag=''
while [[ "$1" != ""  ]]; do
    if [[ "$1" == "--web" ]]; then
    mgdir=$MADGRAPH_BASE/MG_ME/MadGraphII
    mad2=$mgdir/mg2
    dirbin=$MADGRAPH_BASE/MG_ME/WebBin
    helas=$MADGRAPH_BASE/MG_ME/HELAS
    mgmedir=$MADGRAPH_BASE/MG_ME
    tag="--web"
    fi
    shift ARGV
done

gen_jpg=$dirbin/gen_jpeg-pl
gen_card=$dirbin/gen_cardhtml-pl
gen_info=$dirbin/gen_infohtml-pl
setup_model=$dirbin/setup_model-pl

echo $$ > generating
 
# Check that mg2 exists, otherwise compile
if [[ ! -d $mgdir ]]; then
  echo "Error: No MadGraphII directory found"
  exit
fi

if [[ $tag == '' && ! -e $mad2 ]]; then
  echo "No mg2 executable found. Running make."
  cd $mgdir
  make mg2
  cd $main
fi

if [[ ! -e $mad2 ]]; then
  echo "Error: No file $mad2"
  exit
fi

#	Clear out any old information/directories

rm -f *.html >& /dev/null
cp -p HTML/info-default.html HTML/info.html
rm -f bin/plot_events >& /dev/null
rm -f bin/scale_events >& /dev/null
rm -f bin/sum_html >& /dev/null
rm -f bin/select_events >& /dev/null
if [[ ! -d lib ]]; then
    echo "Error: lib directory not found"
    exit
fi
rm -f lib/* >& /dev/null
if [[ ! -d Source ]]; then
    echo "Error: Source directory not found"
    exit
fi
rm -f Source/*.o  >& /dev/null
rm -f Source/DHELAS/*.o
rm -f Source/MODEL/*.o >& /dev/null
rm -f Source/PDF/*.o >& /dev/null
rm -f Source/DECAY/*.o >& /dev/null

# Clean the log file and check the proc_card is there

if [[ -e proc_log.txt ]]; then rm -f proc_log.txt; fi

if [[ ! -e Cards/proc_card.dat ]]; then
    echo "proc_card.dat not present in directory Cards"
    echo "Prepare a card (see examples on the web) and"
    echo "copy it there."
fi

#   Copy version info
cp -p $mgdir/MGVersion.txt SubProcesses/
cp -p $mgmedir/MGMEVersion.txt .

#   Set up the model 

$setup_model $tag> proc_log.txt

# Copy helas
cp -p -r $helas/* Source/DHELAS/
rm -rf Source/DHELAS/CVS >& /dev/null
rm -rf Source/DHELAS/lib >& /dev/null
cp -p Source/DHELAS/Makefile.template Source/DHELAS/Makefile >& /dev/null

#   Now we need to get into the SubProcesses Directory

if [[ ! -d SubProcesses ]]; then
    echo "Error: SubProcesses directory not found"
    exit
fi
cd SubProcesses

#	Clear out any old information/directories

rm -r P*_* >& /dev/null
rm -f minput.mg  >& /dev/null
rm -f dname.mg  >& /dev/null
rm -f subproc.mg  >& /dev/null
rm -f *.html >& /dev/null
#
#  Below are files which are different for each subprocess and copied
#  into the subdirectory
#
mvfiles='dname.mg auto_dsig.f matrix.f matrix.ps configs.inc mg.sym props.inc xinput.mg leshouche.inc pmass.inc nexternal.inc ngraphs.inc ncombs.inc coloramps.inc decayBW.inc qmass.inc'

linkfiles='genps.f myamp.f driver.f unwgt.f coupl.inc run.inc genps.inc cuts.inc symmetry.f  cuts.f setcuts.f cluster.inc cluster.f message.inc sudakov.inc reweight.f initcluster.f addmothers.f setscales.f'

ln -sf ../Source/cuts.inc cuts.inc

cp -p ../HTML/thinking.gif ../HTML/card.jpg
echo "Started `date`" >> ../proc_log.txt
echo -n "Running..."
echo " first pass " >& status
$gen_card >& /dev/null
$mad2 < ../Cards/proc_card.dat >> ../proc_log.txt
j=1
if [[ -e matrix.ps  ]]; then    
        touch xinput.mg
        source dname.mg
        if [[ ! -e $DIRNAME ]]; then
           mkdir $DIRNAME
        else
           echo "Warning reusing directory " $DIRNAME
        fi
	cp -p pmass.inc ../Source
	cp -p nexternal.inc ../Source
	cp -p leshouche.inc ../Source
        if [[ -e color_data.inc ]]; then
          ln -sf ../makefile_mo $DIRNAME/makefile
          ln -sf ../projection.f $DIRNAME/projection.f
          mv color_data.inc $DIRNAME/color_data.inc
          g77 -O -ffixed-line-length-132 -o reduce_cfgs shrinktops.f
          ./reduce_cfgs >& /dev/null
          mv configs-1.inc configs.inc
          mv props-1.inc props.inc
          rm reduce_cfgs  >& /dev/null
          rm configs_temp.inc  >& /dev/null
          rm props_temp.inc  >& /dev/null
        else
          ln -sf ../makefile $DIRNAME/makefile
          touch qmass.inc
        fi

        mv -f $mvfiles $DIRNAME/
	for i in $linkfiles ; do
	    ln -sf ../$i $DIRNAME/$i
	done
        touch subproc.mg
        echo $DIRNAME >> subproc.mg
        pushd $DIRNAME >& /dev/null
	echo $j >& iproc.dat
        $gen_jpg 
        popd     >& /dev/null
	echo -n "."
	if [[ -e minput.mg ]]; then
	    total=`cat minput.mg|wc -l`
        else
	    total=0
	fi
        echo " "
else
    rm -f status
    echo "No Diagrams/Process " >& status
    touch done
    cp -p ../HTML/stop.jpg ../HTML/card.jpg > /dev/null
    total=0
    echo " "
    echo "No diagrams for this process!"
fi
$gen_card >& /dev/null
$gen_info >& /dev/null
total=`expr $total - 1`
total=`expr $total / 4`
total=`expr $total + 1`
remaining=0
completed=1
while [[ -e minput.mg ]]; do
    j=`expr $j + 1`
    mynum=`cat minput.mg | wc -l`
    remaining=`expr $mynum - 1`
    remaining=`expr $mynum / 4`
    completed=`expr $total - $remaining`
    completed=`expr $completed + 1`
    echo " $completed / $total " >& status
    $gen_card >& /dev/null
    mv minput.mg xinput.mg
    $mad2 < xinput.mg >> ../proc_log.txt
    if [[ -e dname.mg  ]]; then
        source dname.mg
        if [[ ! -e $DIRNAME ]]; then
            mkdir $DIRNAME
        else
            echo "Warning reusing directory " $DIRNAME
        fi
        if [[ -e color_data.inc ]]; then
          ln -sf ../makefile_mo $DIRNAME/makefile
          ln -sf ../projection.f $DIRNAME/projection.f
          mv color_data.inc $DIRNAME/color_data.inc
          g77 -O -ffixed-line-length-132 -o reduce_cfgs shrinktops.f
          ./reduce_cfgs >& /dev/null
          mv configs-1.inc configs.inc
          mv props-1.inc props.inc
          rm reduce_cfgs  >& /dev/null
          rm configs_temp.inc  >& /dev/null
          rm props_temp.inc  >& /dev/null
        else
          ln -sf ../makefile $DIRNAME/makefile
          touch qmass.inc
        fi

        mv -f $mvfiles $DIRNAME/
        for i in $linkfiles ; do
            ln -sf ../$i $DIRNAME/$i
        done
        echo $DIRNAME >> subproc.mg
        pushd $DIRNAME >& /dev/null
	echo $j >& iproc.dat
        $gen_jpg
        popd >& /dev/null
	echo -n "."
	$gen_card >& /dev/null
	$gen_info >& /dev/null
    fi
done
echo "Finished `date` " >> ../proc_log.txt
echo "`date` " >& ../CREATED
touch done
$gen_info >& /dev/null
cd ..
$dirbin/gen_crossxhtml-pl
mv *.html HTML/. >& /dev/null  # move all HTML stuff in the HTML dir
mv *.jpg  HTML/. >& /dev/null
mv HTML/index.html .  # move it back to main directory
$gen_card >& /dev/null
if [[ -e SubProcesses/subproc.mg ]]; then
   rm -f madevent.tar.gz >& /dev/null
   make 
fi
if [[  $tag == "--web"  ]]; then
   touch Online
fi
rm generating
$gen_card >& /dev/null

if [[ -e ../param_card.dat  ]]; then
  mv ../param_card.dat ./Cards/param_card.dat
fi
if [[ -e ../run_card.dat  ]]; then
  mv ../run_card.dat ./Cards/run_card.dat
fi
if [[ -e ../pythia_card.dat  ]]; then
  mv ../pythia_card.dat ./Cards/pythia_card.dat
fi
if [[ -e ../pgs_card.dat  ]]; then
  mv ../pgs_card.dat ./Cards/pgs_card.dat
fi

