#!/bin/bash

set -e
set -v

TOPDIR=`pwd`

CONFIG=`basename $1`
DAX=horizontal-cluster.dax

# Unique directory for this run
RUN_ID=`/bin/date +'%Y%m%d_%H%M%S%N'`

# Read a property from ${CONFIG}/test.config file
properties ()
{
        eval $1=\"`grep "^[\s]*[^#]*$2\s*=" ${CONFIG}/test.config | cut -d"=" -f2 | sed -e 's/^\s*//g' -e 's/\s*$//g'`\"

        local i=\$$1
        eval local temp=$i

        # If property not set or is empty, then check if default value is provided. If Yes set property to default value.
        if [[ -z $temp && ! -z $3 ]]; then
                eval $1=$3
        fi
}

# Read the physical directory where the input file is located.
properties input_file input_file

if [ -z ${input_file} ]; then
        input_file='./f.a'
else
	mkdir -p ${input_file}/$USER/inputs
        input_file=${input_file}/$USER/inputs/f.a
fi

# generate the input file
echo "This is sample input to KEG" > ${input_file}

mkdir -p staging-site

# build the dax generator
export PYTHONPATH=`pegasus-config --python`
./cluster.py /usr $CONFIG > ${DAX}

properties local_site_protocol local_site_protocol file://
properties local_site_url local_site_url 

# 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="${local_site_protocol}" url="${local_site_url}" mount-point="$TOPDIR/work"/>
                    <internal-mount-point mount-point="$TOPDIR/work"/>
                </shared>
            </scratch>
            <storage>
                <shared>
                    <file-server protocol="${local_site_protocol}" url="${local_site_url}" 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 />
            <storage />
        </head-fs>
        <profile namespace="pegasus" key="style">condor</profile>
        <profile namespace="condor" key="universe">vanilla</profile>
    </site>
    <site handle="sharedfs" arch="x86" os="LINUX">
        <head-fs>
            <scratch>
                <shared>
                    <file-server protocol="file" url="file://" mount-point="/nfs/ccg3/scratch/$USER/scratch/$RUN_ID"/>
                    <internal-mount-point mount-point="/nfs/ccg3/scratch/$USER/scratch/$RUN_ID"/>
                </shared>
            </scratch>
            <storage>
                <shared>
                    <file-server protocol="file" url="file://" mount-point="/nfs/ccg3/scratch/$USER/storage/black-diamond-output/$RUN_ID"/>
                    <internal-mount-point mount-point="/nfs/ccg3/scratch/$USER/storage/black-diamond-output/$RUN_ID"/>
                </shared>
            </storage>
        </head-fs>
        <replica-catalog type="LRC" url="rlsn://dummyValue.url.edu" />
        <profile namespace="condor" key="should_transfer_files">Yes</profile>
        <profile namespace="condor" key="when_to_transfer_output">ON_EXIT</profile>
        <profile namespace="env" key="PEGASUS_HOME" >/usr</profile>
        <profile namespace="pegasus" key="style">condor</profile>
        <profile namespace="condor" key="universe">vanilla</profile>
    </site>
    <site handle="cartman-data">
        <head-fs>
            <scratch>
                <shared>
                    <file-server protocol="gsiftp" url="gsiftp://cartman.isi.edu" mount-point="${TOPDIR}/staging-site/scratch"/>
                    <internal-mount-point mount-point="${TOPDIR}/staging-site/scratch"/>
                </shared>
            </scratch>
            <storage />
        </head-fs>
        <replica-catalog type="LRC" url="rlsn://dummyValue.url.edu" />
        <profile namespace="env" key="PEGASUS_HOME" >/usr</profile>
    </site>
</sitecatalog>
EOF

# plan and submit the workflow
properties execution_site execution_site local
properties staging_site staging_site local
properties output_site output_site local
properties planner_args planner_args

set -x

pegasus-plan \
    --conf ${CONFIG}/pegasusrc \
    --sites ${execution_site} \
    --staging-site $staging_site \
    --output $output_site \
    --dir work \
    --nocleanup \
    --dax ${DAX} \
    --cluster horizontal \
    --submit ${planner_args}

set +x

