#!/bin/bash
set -eu -o pipefail

#------------------------------------------------------------
# parse command line options
#------------------------------------------------------------

case $1 in
	--help)
		cat <<EOF
Usage: abyss-dida [OPTION]... QUERY... TARGET

Options:

    --help        this help message
    -d STRING     additional command line opts for DIDA
    -j            number of threads [1]
    -m            path of 'mpirun' executable [mpirun]
    -n            number of ranks in DIDA MPI job [3]
    -x VAR=value  set environment variable for MPI job
    --version     print version info and exit

Align the sequences of the files QUERY to those of the file
TARGET using DIDA.
EOF
		exit
		;;
	--version)
		cat <<EOF
abyss-dida (ABySS)
Written by Ben Vandervalk.
EOF
		dida-wrapper --version
		exit
		;;
esac

n=3
mpirun=''
dida_cmd='dida-wrapper'
dida_opt='--no-clean --se'
while getopts :d:j:l:m:n:v:x: opt; do
	case $opt in
		d) dida_opt="$dida_opt $OPTARG";;
		j) dida_cmd="$dida_cmd -j$OPTARG";;
		l) dida_cmd="$dida_cmd -l$OPTARG";;
		m) mpirun="$OPTARG";;
		n) n=$OPTARG;;
		v) ;;
		x) export "$OPTARG";;
		\?) echo >&2 "$(basename 0): invalid option: $OPTARG"; exit 1;;
	esac
done
dida_cmd="$dida_cmd $dida_opt"
shift $((OPTIND-1))

# dida-wrapper requires n >= 3
if [ $n -lt 3 ]; then
	n = 3
fi

# default mpirun cmd, if none specified
if [ -z "$mpirun" ]; then
	mpirun="mpirun -np $n"
fi

#------------------------------------------------------------
# parse command line arguments (query and target files)
#------------------------------------------------------------

# Add file arguments to dida command.  Convert all input file paths
# to absolute, since we change to a temp dir below

query=($(readlink -f "$@"))
target=${query[${#query[@]}-1]}
unset query[${#query[@]}-1]

#------------------------------------------------------------
# set up and switch to temp sandbox dir
#------------------------------------------------------------

tmpdir=$(mktemp -d --tmpdir=.)
pushd $tmpdir > /dev/null

ln -s "$target"
target_link="$(basename $target)"

#------------------------------------------------------------
# run DIDA
#------------------------------------------------------------

dida_cmd="$mpirun /bin/bash -c '$dida_cmd <(abyss-tofastq --interleave ${query[@]}) $target_link'"
echo >&2 "$dida_cmd"
# tricky: must use eval here to preserve nested quotes
# (e.g. quotes in $mpirun command)
eval "$dida_cmd"

du=$(du -hsc * | tail -1 | awk '{print $1}')
echo >&2 "dida-wrapper job used $du temp disk space"

#------------------------------------------------------------
# clean up
#------------------------------------------------------------

rm -f *
popd > /dev/null
rmdir $tmpdir
