#!/bin/bash
#
################################################################################
#                                                                             ##
#                    MadGraph/MadEvent                                        ##
#                                                                             ##
# FILE : compile                                                              ##
# VERSION : 1.0                                                               ##
# DATE : 23 Septembre 2007                                                    ##
# AUTHOR : MH - MadGraph team                                                 ##
#                                                                             ##
# DESCRIPTION : script to compile madevent                                    ##
# USAGE : ./make_package                                                      ##
################################################################################

# set nonomatch

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


#
# If argument is equal to 'd' use dynamic libraries
#
echo $PWD
if [[  "$1" == ""  ]]; then
    echo "Using default makefiles"
elif [[ "$1" == "dynamic" ]]; then
    echo "Changing makefiles for the generation of dynamic libraries (might not work under MacOsX)"
    if [[ -e ./Source/makefile_dynamic ]]; then
	mv ./Source/makefile ./Source/makefile_static
        cp ./Source/makefile_dynamic ./Source/makefile
    else
        echo "Error: Could not replace Source/makefile"
        exit
    fi
    if [[ -e SubProcesses/makefile_dynamic ]]; then
	mv SubProcesses/makefile SubProcesses/makefile_static
        cp SubProcesses/makefile_dynamic SubProcesses/makefile
    else
        echo "Error: Could not replace SubProcesses/makefile"
        exit
    fi
    if [[ -e Source/DHELAS/Makefile_dynamic ]]; then
	mv Source/DHELAS/Makefile Source/DHELAS/Makefile_static
        cp Source/DHELAS/Makefile_dynamic Source/DHELAS/Makefile
    else
        echo "Error: Could not replace Source/DHELAS/Makefile"
        exit
    fi
    if [[ -e Source/MODEL/makefile_dynamic ]]; then
	mv Source/MODEL/makefile Source/MODEL/makefile_static
        cp Source/MODEL/makefile_dynamic Source/MODEL/makefile
    else
        echo "Error: Could not replace Source/MODEL/makefile"
        exit
    fi
    if [[ -e Source/PDF/makefile_dynamic ]]; then
	mv Source/PDF/makefile Source/PDF/makefile_static
        cp Source/PDF/makefile_dynamic Source/PDF/makefile
    else
        echo "Error: Could not replace Source/PDF/makefile"
        exit
    fi
else
    echo Error: invalid argument $1
    exit
fi

#
#  Now let shell know where to find important executables
#

if [[ -d Source ]]; then
    cd Source
    make ../bin/sum_html
    make ../bin/gen_ximprove
    make all
    make ../bin/combine_events
    cd ..
else
    echo 'Error Source directory not found'
    exit
fi

if [[ -d SubProcesses ]]; then
    cd SubProcesses
    for i in P*_* ; do
	cd $i
	echo $i
	rm -f ajob* >& /dev/null
	rm -f wait.ajob* >& /dev/null
	rm -f run.ajob* >& /dev/null
	rm -f done.ajob* >& /dev/null
#        if [[ -e ajob1 ]]; then
#	   chmod +x ajob*
        make madevent > /dev/null
#        fi
	cd ..
     done
    cd ..
else
    echo "Error could not find SubProcesses"
    exit
fi

if [[ -d ../DECAY ]]; then
    echo "DECAY directory found, compiling..."
    cd ../DECAY
    if [[  "$1" == ""  ]]; then
	sed -i.bak "s|HELAS/lib/libdhelas3.a|madevent/lib/libdhelas3.a|g" makefile
    elif [[ "$1" == "dynamic" ]]; then
	sed -i.bak "s|HELAS/lib/libdhelas3.a|madevent/lib/libdhelas3.so|g" makefile
    else
	echo Error: invalid argument $1
	exit
    fi
    make
fi

echo ""

