#!/bin/bash
#  
#   First we need to get into the main directory
#
if [[  ! -d ./bin  ]]; then
    cd ../
    if [[ ! -d ./bin ]]; then
	echo "Error: store must be executed from the main, or bin directory"
	exit
    fi
fi
if [[ ! -d SubProcesses ]]; then
    echo "Error: SubProcesses directory not found"
    exit
fi
cd SubProcesses
if [[  "$1" == ""  ]]; then
    echo 'Enter you must specify a name to store files under. (eg store TeV)'     
    exit
fi
#$i=1;
#$max_store = 99;
#while(-e "SubProcesses/".$t.$i."_results.html" && $i < $max_store){
#    $i++;
#}

if [[ -e $1_results.html ]]; then
    rm -f $1_results.html
fi
sed s/results.html/$1_results.html/g results.html | sed s/log.txt/$1_log.txt/g | sed s/results.dat/$1_results.dat/g | sed s/plots.html/$1_plots.html/g | sed s/pyplots.html/$1_pyplots.html/g > $1_results.html
mv -f results.dat $1_results.dat  >& /dev/null
for i in P*_* ; do
    cd $i
    echo $i
    if [[ -e $1_results.html ]]; then
	rm -f $1_results.html
    fi    
    mv -f results.dat $1_results.dat  >& /dev/null
    sed s/results.html/$1_results.html/g results.html | sed s/log.txt/$1_log.txt/g | sed s/results.dat/$1_results.dat/g > $1_results.html
    for k in G* ; do
	cd $k
	if [[ -e events.lhe ]]; then
	    rm -f events.lhe
#	    cp -f events.lhe $1_events.lhe
#	    rm -f $1_events.lhe.gz >& /dev/null
#	    gzip $1_events.lhe
	fi
	for j in results.dat log.txt ; do
	    if [[ -e $j ]]; then
		mv -f $j $1_$j
	    fi
	done
	for j in ftn26 ; do
	    if [[ -e $j ]]; then
		mv -f $j $1_$j
		rm -f $1_$j.gz >&/dev/null
		gzip $1_$j
	    fi
	done
	cd ../
    done
    cd ../
done
cd ..
./bin/gen_cardhtml-pl
if [[ -e Events/events.lhe ]]; then
    mv -f Events/events.lhe Events/$1_events.lhe 
    rm -f Events/$1_events.lhe.gz >& /dev/null
    gzip -f Events/$1_events.lhe
fi
if [[ -e Events/unweighted_events.lhe ]]; then
    ./bin/extract_banner-pl Events/unweighted_events.lhe  Events/$1_banner.txt 
    mv -f Events/unweighted_events.lhe Events/$1_unweighted_events.lhe 
    rm -f Events/$1_unweighted_events.lhe.gz >& /dev/null
    gzip -f Events/$1_unweighted_events.lhe 
fi
cd Events
if [[ -e plots.html ]]; then
    mv -f plots.html $1_plots.html
fi
if [[ -e pyplots.html ]]; then
    mv -f pyplots.html $1_pyplots.html
fi
if [[ -e ./$1/events.list ]]; then
    sed s/unweighted_events.root/$1_unweighted_events.root/g ./$1/events.list >  ./$1/events.list.temp
    mv -f ./$1/events.list.temp ./$1/events.list
fi
if [[ -e unweighted_events.root ]]; then
    mv -f unweighted_events.root $1_unweighted_events.root
fi
if [[ -e pythia_events.hep ]]; then
    mv -f pythia_events.hep $1_pythia_events.hep
    gzip -f $1_pythia_events.hep
    mv -f pythia.log $1_pythia.log
# pythia_card.dat
   echo "<MGPythiaCard>" >>$1_banner.txt
   if [[ -e ../Cards/pythia_card.dat ]]; then cat ../Cards/pythia_card.dat   >> $1_banner.txt; fi
   echo "</MGPythiaCard>" >>$1_banner.txt
fi

if [[ -e pythia_events.lhe ]]; then
# write the pythia banner
    echo \<LesHouchesEvents version=\"1.0\"\> > temp.lhe
    echo \<\!-- >> temp.lhe
    cat $1_banner.txt >> temp.lhe
    sed /'<LesHouchesEvents version=\"1.0\">'/s//'-->'/ pythia_events.lhe >> temp.lhe
    rm -f pythia_events.lhe
    mv -f temp.lhe $1_pythia_events.lhe
    gzip -f $1_pythia_events.lhe
fi

if [[ -e pythia_lhe_events.root ]]; then
    mv -f pythia_lhe_events.root $1_pythia_lhe_events.root
fi

if [[ -e events.tree ]]; then
    mv -f events.tree $1_events.tree
    gzip -f $1_events.tree
    mv -f xsecs.tree $1_xsecs.tree
    mv -f beforeveto.tree $1_beforeveto.tree
    gzip -f $1_beforeveto.tree
fi
if [[ -e pgs_events.lhco ]]; then
# pgs_card.dat
   echo "<MGPGSCard>" >>$1_banner.txt
   if [[ -e ../Cards/pgs_card.dat ]]; then cat ../Cards/pgs_card.dat   >> $1_banner.txt; fi
   echo "</MGPGSCard>" >>$1_banner.txt
# write the pgs banner
    sed -e "s/^/#/g" $1_banner.txt > temp.dat
    cat pgs_events.lhco >> temp.dat
    rm -f pgs_events.lhco
    mv -f temp.dat $1_pgs_events.lhco
    gzip -f $1_pgs_events.lhco
    mv -f pgs.log $1_pgs.log
fi
if [[ -e pgs_events.root ]]; then
    mv -f pgs_events.root $1_pgs_events.root
fi
if [[ -e pythia_events.root ]]; then
    mv -f pythia_events.root $1_pythia_events.root
fi
cd ..
