#!/bin/sh

scriptname="radical-pilot-create-static-ve"

reqs="pymongo==2.8 python-hostlist netifaces==0.10.4 setproctitle ntplib pyzmq"
reqs="$reqs apache-libcloud colorama backports.ssl-match-hostname msgpack-python"
reqs="$reqs future"

setuptools="setuptools==0.6c11"
pip="pip==1.4.1"

# ------------------------------------------------------------------------------
#
help(){

    err="$1"
    ret=0
    
    if ! test -z "$err"
    then
        ret=1
        printf "\n    ERROR: $err\n"
    fi

    cat <<EOT

    usage: $0 <target> [-h]

    This script creates a virtualenv at the given target location.  That
    virtualenv should be suitable to be used as static VE for a radical.pilot
    target resource, and can be specified in a resource configuration for RP.

EOT
    exit $ret
}


# ------------------------------------------------------------------------------
#
prefix=$1

if test "$prefix" = "-h"
then
    help 
fi

if test -z "$prefix"
then
    help "missing target"
fi

# We don't want to overwrite VE's -- the hack to get the namespace import in
# place is too invasive to be applied to an existing VE.
if test -e "$prefix"
then
    help "target '$prefix' exists"
fi

# Ensure we install in an absolute path -- pip seems to like that better...
case $prefix in
    /*)
        ;;
    *)
        prefix="`pwd`/$prefix"
        ;;
esac
echo "using install prefix $prefix"

# create the ve, install bare necessities
mkdir -p "$prefix"
mkdir -p "$prefix/rp_install"

virtualenv "$prefix"
.          "$prefix"/bin/activate
pip install --upgrade $setuptools || exit 1
pip install --upgrade $pip        || exit 1
pip install --upgrade $reqs       || exit 1

# install the radical stack (utils, saga, pilot) into a separate tree
# ($prefix/rp_install(, so that any local install can use the ve *w/o* 
# the radical stack, by re-routing PYTHONPATH
python_version=`python -c 'import distutils.sysconfig as sc; print sc.get_python_version()'`
ve_mod_prefix=` python -c 'import distutils.sysconfig as sc; print sc.get_python_lib()'`
rp_mod_prefix=`echo $ve_mod_prefix | sed -e "s|$prefix|$prefix/rp_install/|"`

# we actually stop at this point.  RP has other dependencies nowadays, which are
# not always solved by the simple script above, namely ORTE, and
# morespecifically (and indirectly) libffi.  radical-pilot-deploy-ompi.sh needs
# to run before rp installation has a chance to succeed (usually).
exit


# NOTE: the code below is left in, as it might get reactivated at some point.
PYTHONPATH="$rp_mod_prefix:$PYTHONPATH"
export PYTHONPATH

PATH="$prefix/rp_install/bin:$PATH"
export PATH

# we need to add the radical name __init__.py manually here --
# distutil is broken and will not install it.  That file is necessary to have
# the 'radical' namespace searchable by python's import.
mkdir -p   "$rp_mod_prefix/radical/"
ru_ns_init="$rp_mod_prefix/radical/__init__.py"
echo                                              >  $ru_ns_init
echo 'import pkg_resources'                       >> $ru_ns_init
echo 'pkg_resources.declare_namespace (__name__)' >> $ru_ns_init
echo                                              >> $ru_ns_init

PIP_INSTALL="pip install --install-option='--prefix=$prefix/rp_install'"
$PIP_INSTALL radical.utils || exit 1
$PIP_INSTALL saga-python   || exit 1
$PIP_INSTALL radical.pilot || exit 1

OLD_SAGA_VERBOSE=$SAGA_VERBOSE
OLD_RADICAL_VERBOSE=$RADICAL_VERBOSE
OLD_RADICAL_PILOT_VERBOSE=$RADICAL_PILOT_VERBOSE

SAGA_VERBOSE=WARNING
RADICAL_VERBOSE=WARNING
RADICAL_PILOT_VERBOSE=WARNING

# print the ve information and stack versions for verification
echo
echo "---------------------------------------------------------------------"
echo
echo "PYTHONPATH: $PYTHONPATH"
echo "python: `which python` (`python --version`)"
python -c 'print "utils :",; import radical.utils as ru; print ru.version_detail,; print ru.__file__'
python -c 'print "saga  :",; import saga          as rs; print rs.version_detail,; print rs.__file__'
python -c 'print "pilot :",; import radical.pilot as rp; print rp.version_detail,; print rp.__file__'
echo
echo "---------------------------------------------------------------------"
echo

SAGA_VERBOSE=$OLD_SAGA_VERBOSE
RADICAL_VERBOSE=$OLD_RADICAL_VERBOSE
RADICAL_PILOT_VERBOSE=$OLD_RADICAL_PILOT_VERBOSE

