#!/bin/bash

set -e

TOPDIR=`pwd`

#needs to be updated by the user
export SSH_PRIVATE_KEY=/path/to/ssh/private_key

#update accordingly
STAGING_SERVER_HOSTNAME="staging.server.pegasus.isi.edu"
STAGING_SERVER_SCRATCH_DIR="/var/www/html/scratch"

# 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

# hostname for gridftp server
HOSTNAME=`hostname -f`

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

# generate the dax
java BlackDiamondDAX $HOSTNAME $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-4.0.xsd" version="4.0">

    <site  handle="local" arch="x86" os="LINUX">
        <directory type="shared-scratch" path="$TOPDIR/work">
            <file-server operation="all" url="file:///$TOPDIR/work"/>
        </directory>
        <directory type="local-storage" path="$TOPDIR/outputs">
            <file-server operation="all" url="file:///$TOPDIR/outputs"/>
        </directory>
    </site>

    <site  handle="condorpool" arch="x86" os="LINUX">
        <profile namespace="pegasus" key="style" >condor</profile>
        <profile namespace="condor" key="universe" >vanilla</profile>
    </site>

     <site  handle="staging-site" arch="x86_64" os="LINUX">
        <directory type="shared-scratch" path="${STAGING_SERVER_SCRATCH_DIR}">
            <file-server operation="put" url="scp://${STAGING_SERVER_HOSTNAME}${STAGING_SERVER_SCRATCH_DIR}"/>
            <file-server operation="get" url="scp://${STAGING_SERVER_HOSTNAME}${STAGING_SERVER_SCRATCH_DIR}"/>

        </directory>
    </site>


</sitecatalog>
EOF

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

