#!/usr/bin/env bash

#
# Execute small ivc demonstration/verification run
#

set -o nounset
set -o pipefail

script_dir=$(dirname $0)
root_dir=$script_dir/..
data_dir=$script_dir/data

analysis_dir=./ivcDemoAnalysis

config_script=$root_dir/configureWorkflow.pl

demo_config_file=$script_dir/ivc.ini





if [ ! -e $config_script ]; then
    cat<<END 1>&2

ERROR: The ivc workflow must be built prior to running demo.
       See installation instructions in '$root_dir/README'

END
    exit 2
fi


#
# Step 1: configure demo
#
if [ -e $analysis_dir ]; then
    cat<<END 1>&2

ERROR: Demo analysis directory already exists: '$analysis_dir' 
       Please remove/move this to rerun demo.

END
    exit 2
fi

cmd="$config_script \
--bam=$data_dir/sorted.bam \
--ref=$data_dir/main.fa \
--config=$demo_config_file \
--output-dir=$analysis_dir"

echo 1>&2
echo "**** Starting demo configuration." 1>&2
echo "**** Configuration cmd: '$cmd'" 1>&2
echo 1>&2
$cmd

if [ $? -ne 0 ]; then
    echo 1>&2
    echo "ERROR: Demo configuration step failed" 1>&2
    echo 1>&2
    exit 1
else
    echo 1>&2
    echo "**** Completed demo configuration." 1>&2
    echo 1>&2
fi


#
# Step 2: run demo (on single local core):
#
#stderrlog=$analysis_dir/make.stderr.log
cmd="make -j 12 -C $analysis_dir"
echo 1>&2
echo "**** Starting demo workflow execution." 1>&2
echo "**** Workflow cmd: '$cmd'" 1>&2
echo 1>&2
$cmd


if [ $? -ne 0 ]; then
    cat<<END 1>&2

ERROR: Workflow execution step failed

END
#        See make error log file: '$stderrlog'
    exit 1
else
    echo 1>&2
    echo "**** Completed demo workflow execution." 1>&2
    echo 1>&2
fi

echo 1>&2
echo "**** Demo/verification successfully completed" 1>&2
echo 1>&2

