#!/bin/bash

set -e

TOPDIR=`pwd`

# pegasus bin directory is needed to find keg
BIN_DIR=`pegasus-config --bin`

# 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 $BIN_DIR 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">
        <head-fs>
            <scratch>
                <shared>
                    <file-server protocol="file" url="file://" mount-point="$TOPDIR/work"/>
                    <internal-mount-point mount-point="$TOPDIR/work"/>
                </shared>
            </scratch>
            <storage>
                <shared>
                    <file-server protocol="file" url="file://" mount-point="$TOPDIR/outputs"/>
                    <internal-mount-point mount-point="$TOPDIR/outputs"/>
                </shared>
            </storage>
        </head-fs>
    </site>
    <site  handle="condorpool" arch="x86" os="LINUX">
        <head-fs>
            <scratch>
                <shared>
                    <file-server protocol="gsiftp" url="gsiftp://obelix.isi.edu" mount-point="/nfs/ccg3/scratch"/>
                    <internal-mount-point mount-point="/nfs/ccg3/scratch"/>
                </shared>
            </scratch>
            <storage />
        </head-fs>
        <profile namespace="pegasus" key="style" >condor</profile>
        <profile namespace="condor" key="universe" >vanilla</profile>
        <profile namespace="condor" key="requirements" >TARGET.FileSystemDomain != &quot;&quot;</profile>
        <profile namespace="condor" key="should_transfer_files">Yes</profile>
        <profile namespace="condor" key="when_to_transfer_output">ON_EXIT</profile>
    </site>
</sitecatalog>
EOF

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

