#!/bin/bash

set -e

TEST_DIR=$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

# Extract the ctools archive

# Generate dax
export PYTHONPATH=`pegasus-config --python`
python daxgen.py dax.xml

MPIEXEC=`which mpiexec`

cat > sites.xml <<END
<?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="$TEST_DIR/exec"/>
                    <internal-mount-point mount-point="$TEST_DIR/exec"/>
                </shared>
            </scratch>
            <storage>
                <shared>
                    <file-server protocol="file" url="file://" mount-point="$TEST_DIR/output"/>
                    <internal-mount-point mount-point="$TEST_DIR/output"/>
                </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="MPIEXEC">$MPIEXEC</profile>
    </site>
</sitecatalog>
END

cat > tc.data <<END
tr pegasus::mpiexec { 
    site local {
        pfn "$TEST_DIR/pegasus-mpi-cluster-wrapper"
        arch "x86"
        os "linux"
        type "INSTALLED"
        profile globus "maxwalltime" "240"
        profile pegasus "cluster.arguments" "--host-memory 1024"
    }
}
END

echo "Planning and submitting the workflow..."
pegasus-plan \
    --conf pegasusrc \
    --dir work \
    --dax dax.xml \
    --sites local \
    --cleanup leaf \
    --cluster label \
    --submit -v

