#!/bin/bash

set -e

TOP_DIR=`pwd`

# download rosetta binary - this is to save space in the Pegasus distribution
if [ ! -e rosetta.exe ]; then
   wget -q http://pegasus.isi.edu/wms/example-workflows/rosetta/rosetta.exe
   chmod 755 rosetta.exe
fi

# do we have the required minirosetta_database?
if [ ! -e minirosetta_database ]; then
   wget -q http://pegasus.isi.edu/wms/example-workflows/rosetta/minirosetta_database.tar.gz
   tar xzf minirosetta_database.tar.gz
   rm minirosetta_database.tar.gz
fi

# what about the required pdbs?
if [ ! -e pdbs ]; then
   wget -q http://pegasus.isi.edu/wms/example-workflows/rosetta/pdbs.tar.gz
   tar xzf pdbs.tar.gz
   rm pdbs.tar.gz
fi

# 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 

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

# generate the dax
java RosettaDAX dax.xml

# 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="/nfs/ccg4/scratch-purge-no-backups/http-scratch/bamboo">
            <file-server operation="get" url="http://obelix.isi.edu/shared-scratch/bamboo"/>
            <file-server operation="put" url="file:///nfs/ccg4/scratch-purge-no-backups/http-scratch/bamboo"/>
        </directory>
        <directory type="local-storage" path="$TOP_DIR/outputs">
            <file-server operation="all" url="file://$TOP_DIR/outputs"/>
        </directory>
        <profile namespace="env" key="PEGASUS_BIN_DIR" >$PEGASUS_BIN_DIR</profile>
    </site>
    <site  handle="condor_pool" arch="x86" os="LINUX">
        <profile namespace="pegasus" key="style" >condor</profile>
        <profile namespace="condor" key="universe" >vanilla</profile>
    </site>
</sitecatalog>
EOF

echo
echo
echo "The site catalog is:"
cat sites.xml

echo
echo
echo "Planning and submitting the workflow..."
pegasus-plan \
    --conf pegasusrc \
    --dir work \
    --dax dax.xml \
    --sites condor_pool \
    --staging-site local \
    --output-site local \
    --submit | tee $TOP_DIR/plan.out


