#!/bin/bash

set -e


#######################################################################
#
#  Settings
#
CLUSTER_NAME="CCG"
CLUSTER_HOSTNAME="obelix.isi.edu"
CLUSTER_GATEKEEPER_PORT="2119"
CLUSTER_GRID_TYPE="gt5"
CLUSTER_SCHEDULER="condor"
CLUSTER_WORK_DIR="/nfs/ccg1/90-day-scratch"
CLUSTER_GLOBUS_LOCATION="/ccg/software/globus/default"
#######################################################################


TOPDIR=`pwd`

# figure out where Pegasus is installed
export PEGASUS_BIN_DIR=`pegasus-config --bin`
if [ "x$PEGASUS_BIN_DIR" = "x" ]; then
    echo "Please make sure pegasus-plan is in your path"
    exit 1
fi 

# download rosetta binary - this is to save space in the Pegasus distribution
if [ ! -e rosetta.exe ]; then
   wget http://pegasus.isi.edu/wms/example-workflows/rosetta/rosetta.exe
   chmod 755 rosetta.exe
fi

# do we have the required minirosetta_database?
if [ ! -e minirosetta_database ]; then
   wget http://pegasus.isi.edu/wms/example-workflows/rosetta/minirosetta_database.tar.gz
   tar xzf minirosetta_database.tar.gz
   rm minirosetta_database.tar.gz
fi

# what about the required pdbs?
if [ ! -e pdbs ]; then
   wget http://pegasus.isi.edu/wms/example-workflows/rosetta/pdbs.tar.gz
   tar xzf pdbs.tar.gz
   rm pdbs.tar.gz
fi

# build the dax generator
export CLASSPATH=.:`pegasus-config --classpath`
javac RosettaDAX.java

# generate the dax
java RosettaDAX dax.xml

# site catalog
cat >sites.xml <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<sitecatalog xmlns="http://pegasus.isi.edu/schema/sitecatalog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pegasus.isi.edu/schema/sitecatalog http://pegasus.isi.edu/schema/sc-3.0.xsd" version="3.0">
    <site  handle="local" arch="x86" os="LINUX">
        <grid  type="gt2" contact="localhost/jobmanager-fork" scheduler="Fork" jobtype="auxillary"/>
        <head-fs>
            <scratch>
                <shared>
                    <file-server protocol="file" url="file://" mount-point="$TOPDIR/outputs"/>
                    <internal-mount-point mount-point="$TOPDIR/outputs" free-size="100G" total-size="30G"/>
                </shared>
            </scratch>
            <storage>
                <shared>
                    <file-server protocol="file" url="file://" mount-point="$TOPDIR/outputs"/>
                    <internal-mount-point mount-point="$TOPDIR/outputs" free-size="100G" total-size="30G"/>
                </shared>
            </storage>
        </head-fs>
        <replica-catalog  type="LRC" url="rlsn://dummyValue.url.edu" />
        <profile namespace="env" key="PEGASUS_BIN_DIR" >$PEGASUS_BIN_DIR</profile>
        <profile namespace="env" key="GLOBUS_LOCATION" >$GLOBUS_LOCATION</profile>
        <profile namespace="env" key="LD_LIBRARY_PATH" >$GLOBUS_LOCATION/lib</profile>
    </site>
    <site  handle="$CLUSTER_NAME" arch="x86" os="LINUX">
        <grid  type="$CLUSTER_GRID_TYPE" contact="$CLUSTER_HOSTNAME:$CLUSTER_GATEKEEPER_PORT/jobmanager-fork" scheduler="Fork" jobtype="auxillary"/>
        <grid  type="$CLUSTER_GRID_TYPE" contact="$CLUSTER_HOSTNAME:$CLUSTER_GATEKEEPER_PORT/jobmanager-$CLUSTER_SCHEDULER" scheduler="unknown" jobtype="compute"/>
        <head-fs>
            <scratch>
                <shared>
                    <file-server protocol="gsiftp" url="gsiftp://$CLUSTER_HOSTNAME" mount-point="$CLUSTER_WORK_DIR"/>
                    <internal-mount-point mount-point="$CLUSTER_WORK_DIR"/>
                </shared>
            </scratch>
            <storage>
                <shared>
                    <file-server protocol="gsiftp" url="gsiftp://$CLUSTER_HOSTNAME" mount-point="$CLUSTER_WORK_DIR"/>
                    <internal-mount-point mount-point="$CLUSTER_WORK_DIR"/>
                </shared>
            </storage>
        </head-fs>
        <replica-catalog  type="LRC" url="rlsn://dummyValue.url.edu" />
        <profile namespace="env" key="GLOBUS_LOCATION" >$CLUSTER_GLOBUS_LOCATION</profile>
    </site>
</sitecatalog>
EOF

echo
echo
echo "Planning and submitting the workflow..."
pegasus-plan \
    --conf pegasusrc \
    --sites $CLUSTER_NAME \
    --cluster horizontal \
    --dir work \
    --output local \
    --dax dax.xml \
    --submit


