#!/bin/bash

set -e
set -v


if [ X${testdir} = "X" ]; then
    testdir=`dirname  $0`
    export testdir
fi

TOPDIR=`pwd`

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

# output directory
mkdir -p outputs

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

# generate the dax
java BlackDiamondDAX /usr 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-4.0.xsd" version="4.0">
    <site handle="local" arch="x86_64" os="LINUX" osrelease="deb" osversion="8">
        <directory type="shared-scratch" path="$TOPDIR/scratch">
            <file-server operation="all" url="file://$TOPDIR/scratch"/>
        </directory>
        <directory type="local-storage" path="$TOPDIR/outputs">
            <file-server operation="all" url="file://$TOPDIR/outputs"/>
        </directory>
        <profile namespace="env" key="PATH">$PEGASUS_BIN_DIR:/ccg/software/irods/3.3/bin:/usr/bin:/bin</profile>
    </site>
    <site handle="condorpool" arch="x86_64" os="LINUX" >
        <profile namespace="pegasus" key="style" >condor</profile>
        <profile namespace="condor" key="universe" >vanilla</profile>
        <profile namespace="env" key="PATH">/ccg/software/irods/3.3/bin:/usr/bin:/bin</profile>
    </site>

    <site handle="cartman-data" arch="x86" os="LINUX">
         <directory type="shared-scratch" path="${TOPDIR}/staging-site/scratch">
            <file-server operation="all" url="gsiftp://cartman.isi.edu/${TOPDIR}/staging-site/scratch"/>
        </directory>

    </site>

</sitecatalog>
EOF

# plan and submit the  workflow
pegasus-plan \
    --conf $testdir/pegasusrc \
    --sites condorpool \
    --staging-site cartman-data \
    --dir work \
    --output-site local \
    --dax blackdiamond.dax  | tee $TOPDIR/plan.out

WORK_DIR=`cat plan.out | grep pegasus-run | sed -E 's/^pegasus-run[ ]+//'`
cd $WORK_DIR

echo
echo
echo "Starting the workflow..."
pegasus-run $WORK_DIR


