#!/bin/bash

set -e
set -x

TOP_DIR=`pwd`

DEGREES=1.0
MONTAGE_HOME=/ccg/software/montage/Montage_v3.3_patched_2

# unique directory for this run
RUN_ID=`/bin/date +'%F_%H%M%S'`
RUN_DIR=$TOP_DIR/work/$RUN_ID
mkdir -p $RUN_DIR

echo
echo "Work directory: $RUN_DIR"
echo

cd $RUN_DIR

cp $TOP_DIR/pegasusrc .

# create the transformation catalog (tc)
echo
echo "Creating the transformation catalog..."
for BINARY in `(cd $MONTAGE_HOME/bin && ls)`; do
    name=$BINARY:3.3
    if [ "${BINARY}" = "mFitplane" ] || [ "$BINARY" = "mDiff" ]; then
        name=$BINARY
    fi

    cat >>tc <<EOF
tr $name {
    site local {
        pfn "$MONTAGE_HOME/bin/$BINARY"
        arch "x86_64"
        os "linux"
        type "STAGEABLE"
        profile pegasus "clusters.size" "20"
        profile env "MONTAGE_BIN" "."
    }
}
EOF
    
done
    
echo
echo "Creating 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">
        <grid  type="gt2" contact="localhost/jobmanager-fork" scheduler="Fork" jobtype="auxillary"/>
        <head-fs>
            <scratch>
                <shared>
                    <file-server protocol="file" url="file://" mount-point="$RUN_DIR/scratch"/>
                    <internal-mount-point mount-point="$RUN_DIR/scratch"/>
                </shared>
            </scratch>
            <storage>
                <shared>
                    <file-server protocol="file" url="file://" mount-point="$RUN_DIR/outputs"/>
                </shared>
            </storage>
        </head-fs>
        <replica-catalog  type="LRC" url="rlsn://dummyValue.url.edu" />
        <profile namespace="env" key="MONTAGE_HOME" >$MONTAGE_HOME</profile>
        <profile namespace="env" key="PEGASUS_HOME" >/usr</profile>
    </site>

    <site handle="glideinwms" arch="x86_64" os="LINUX">
        <head-fs>
            <scratch />
            <storage />
        </head-fs>
        <replica-catalog  type="LRC" url="rlsn://dummyValue.url.edu" />
        <profile namespace="pegasus" key="style">condor</profile>
        <profile namespace="condor" key="universe">vanilla</profile>
        <profile namespace="condor" key="requirements" >(Target.Arch == "X86_64")</profile>
    </site>
</sitecatalog>
EOF

echo
echo "Running mDAG (finding input images, generating DAX, ...)..."
mDAG 2mass j M17 $DEGREES $DEGREES 0.0002777778 . "file://$RUN_DIR" "gsiftp://$RUN_DIR/inputs"

echo
echo "Adding input images to the replica catalog..."
echo "  " `cat cache.list | wc -l` "images found"
cat cache.list | grep -v ".fits " | sed 's/ipac_cluster/local/' >rc
cat url.list | sed 's/ http:.*ref=/ http:\/\/obelix.isi.edu\/irsa-cache/' >>rc

echo
echo "Planning and submitting the workflow..."
pegasus-plan \
    --conf pegasusrc \
    --sites glideinwms \
    --dir work \
    --output local \
    --nocleanup \
    --dax dag.xml \
    --cluster horizontal \
    --submit

