#!/bin/bash -e

# align-genbank [-copy|-finish] [-keep] [-workdir dir]

# Script to do alignment part of build.  This is normally called by
# genbank-build, however it can be run by hand if the alignment needs to
# be manually completed.  Edit this file to add new genomes and assemblies.
#
# -copy - continue a file alignment at the copy phase
# -finish - Finish processing an alignment that was completed by hand.
# -workdir dir - set the workdir, need with -finish if the date has
#  changed since the alignment was started.
# -keep - don't delete working directory
#

# errors terminate with message
set -e
trap "echo Error: genbank-align failed on `hostname` >&2; exit 1" ERR
exec </dev/null

# initialize
gbRoot=/cluster/data/genbank
cd $gbRoot
. $gbRoot/lib/gbCommon.sh
databases=`gbGetDatabases etc/align.dbs`
maxParallel=16

buildTime=var/build/build.time

finish=""
keep=""
while [[ $1 == -* ]] ; do
    opt=$1
    shift
    case "$opt" in
        -finish) 
            finish="-continue=finish" ;;
        -copy) 
            finish="-continue=copy" ;;
        -keep) 
            keep="-keep" ;;
        -workdir)
            workdir="$1"
            shift ;;
        -*) echo "Error: invalid option $opt" >&2
            exit 1 ;;
    esac
done

# setup workdir unless specified on command line
if [ "x$workdir" = "x" ] ; then
    date=`date +"%Y.%m.%d"`
    workdir="work/daily/align.$date"
fi

nice -14 gbAlignStep -maxParallel=$maxParallel $finish $keep -workdir=$workdir $databases

gbMkTimeFile $buildTime

