#!/bin/bash
##
# Copyright 2009-2014 Ghent University
#
# This file is part of EasyBuild,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
# with support of Ghent University (http://ugent.be/hpc),
# the Flemish Supercomputer Centre (VSC) (https://vscentrum.be/nl/en),
# the Hercules foundation (http://www.herculesstichting.be/in_English)
# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
#
# http://github.com/hpcugent/easybuild
#
# EasyBuild is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation v2.
#
# EasyBuild is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with EasyBuild.  If not, see <http://www.gnu.org/licenses/>.
##

# EasyBuild main script
# find location of main EasyBuild script by looking in Python search path, and run it

#  this is an alternative for "python -m easybuild.main $@" which has issues, i.e.
#  * it doesn't work with Python 2.4 (which we hang on to because of it being the default version in RedHat v5)
#  * an ugly warning is printed whenever the pkg_resources module is loaded (e.g. by 3rd party modules like pygraph),
#    see http://bugs.python.org/setuptools/issue36
#    the cause of this is probably that we spread out the easybuild namespace across different locations

# @author: Stijn De Weirdt (Ghent University)
# @author: Dries Verdegem (Ghent University)
# @author: Kenneth Hoste (Ghent University)
# @author: Pieter De Baets (Ghent University)
# @author: Jens Timmerman (Ghent University)

main_script_base_path="easybuild/main.py"
python_search_path_cmd="python -c \"import sys; print ' '.join(sys.path)\""

easybuild_main=
for path in `eval $python_search_path_cmd`;
do
    test_path="$path/$main_script_base_path"
    if [ -f $test_path ]
    then
        easybuild_main=$test_path
        break;
    fi
done

if [ -z $easybuild_main ]
then
    echo "ERROR: Failed to locate EasyBuild's main script $main_script_base_path" \
         "in Python's search path, obtained with '$python_search_path_cmd' ."
    echo "Make sure you PYTHONPATH setting is correct."
    exit 1
else
    python $easybuild_main $@
fi
