#!/bin/bash

p=$1

rm -f combine.log > /dev/null
rm -f combine.err > /dev/null
rm -f combine.out > /dev/null
if [ "$p" = "1" ] 
then
echo '#!/bin/bash' > combine.pbs
echo 'if [[ "$PBS_O_WORKDIR" != "" ]]; then' >> combine.pbs
echo '    cd $PBS_O_WORKDIR' >> combine.pbs
echo 'fi' >> combine.pbs
echo "../bin/combine_events;touch combine.out" >> combine.pbs
chmod u+x combine.pbs
qsub -N combine_run -e combine.err -o combine.log -V -q madgraph combine.pbs >> ../running_jobs 
while [ ! -e combine.out ]
do
  sleep 1
done  
rm -f combine.pbs
cat combine.log
cat combine.err

else
../bin/combine_events 
fi

# Remove huge scratch file
cp P1_f-n+_f-n+x/G1/{events,_SAVE}.lhe
cp P1_f-n+_f-n+x/G2/{events,_SAVE}.lhe
echo "REMEMBER TO DELETE THE SCRATCH FILE!!"
rm -f scratch
