#!/bin/bash

set -e
set -v

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

TOPDIR=`pwd`

# generate the dax
export PYTHONPATH=`pegasus-config --python`
${testdir}/dax-generator.py >dax.xml

# create the site catalog
echo "" >f.a
echo "Hello!" >f.b0
echo "Hello world!" >f.c12

# 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_64" 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_64" os="LINUX">
        <head-fs>
            <scratch />
            <storage />
        </head-fs>
        <profile namespace="pegasus" key="style" >condor</profile>
        <profile namespace="condor" key="universe" >vanilla</profile>
    </site>
</sitecatalog>
EOF

cat > rc.data <<EOF
f.a file://$TOPDIR/f.a pool="local"
"f\\.([x])" file://$TOPDIR/f.a pool="local" regex="true"
fa([x]) file://$TOPDIR/f.a pool="local" regex="true"
dir/file.x file://$TOPDIR/f.a pool="local"
dir/file.y file://$TOPDIR/f.a pool="local" regex="true"

f.b0 file://$TOPDIR/f.b0 pool="local"
f.c12 file://$TOPDIR/f.c12 pool="local"
f\.c2([0-9]) file://$TOPDIR/f.c12 pool="local" regex="true"
EOF

# plan and submit the  workflow
pegasus-plan \
    --conf ${testdir}/pegasusrc \
    --sites condorpool \
    --dir work/submit \
    --output-site local \
    --dax dax.xml \
    --cleanup leaf \
    -vvvvv \
    --submit | tee $TOPDIR/plan.out
