#!/bin/bash

set -e


#######################################################################
#
#  Settings
#
CLUSTER_NAME="Obelix"
CLUSTER_HOSTNAME="obelix.isi.edu"
CLUSTER_SCHEDULER="condor"
CLUSTER_WORK_DIR="/nfs/ccg3/scratch"
CLUSTER_PEGASUS_HOME="/ccg/software/pegasus/dev/trunk"
CLUSTER_GLOBUS_LOCATION="/ccg/software/globus/default"
#######################################################################


TOPDIR=`pwd`

# pegasus bin directory is needed to find keg
BIN_DIR=`pegasus-config --bin`
# figure out where Pegasus is installed

if [ "x$GLOBUS_LOCATION" = "x" ]; then
    echo "Pelase set GLOBUS_LOCATION to the location of your Pegasus install"
    exit 1
fi 

# generate the input file
echo "This is sample input to KEG" >f.a

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

# generate the dax
java BlackDiamondDAX $CLUSTER_NAME $CLUSTER_PEGASUS_HOME blackdiamond.dax

# create the 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"/>
        <grid  type="gt2" contact="localhost/jobmanager-fork" scheduler="unknown" jobtype="compute"/>
        <head-fs>
            <scratch>
                <shared>
                    <file-server protocol="file" url="file://" mount-point="$TOPDIR/outputs"/>
                    <internal-mount-point mount-point="$TOPDIR/work/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/work/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" >$PEGASUS_BIN_DIR</profile>
        <profile namespace="env" key="GLOBUS_LOCATION" >$GLOBUS_LOCATION</profile>
    </site>
    <site  handle="$CLUSTER_NAME" arch="x86" os="LINUX">
        <grid  type="gt2" contact="$CLUSTER_HOSTNAME/jobmanager-fork" scheduler="Fork" jobtype="auxillary"/>
        <grid  type="gt2" contact="$CLUSTER_HOSTNAME/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="PEGASUS_HOME" >$CLUSTER_PEGASUS_HOME</profile>
        <profile namespace="env" key="GLOBUS_LOCATION" >$CLUSTER_GLOBUS_LOCATION</profile>
    </site>
</sitecatalog>
EOF

# plan and submit the  workflow
pegasus-plan \
    --conf pegasusrc \
    --sites $CLUSTER_NAME \
    --dir work \
    --output local \
    --dax blackdiamond.dax \
    --submit

