#!/bin/bash -e
usage='buildTrix [options] trixName metaDbName path_to_cv.ra outputMachine outputPath ${DBS}

options:
   -alsoTo=machine:path -- copy the same files to another machine and path as well.
                           May be given more than once.  Using this instead of a second
                           invocation avoids rebuilding the index from scratch.'

dests=()
while [[ $1 == -* ]] ; do
    case $1 in
        -alsoTo=*)
            dests+=("${1#-alsoTo=}") ;;
        *)
            echo "invalid option: $1" >&2
            exit 1 ;;
    esac
    shift
done

if [ $# -lt 6 ] ; then
    echo "wrong # args: $usage" >&2
    exit 1
fi

trixName="$1"; shift
metaDbName="$1" ; shift
cvRaPath="$1" ; shift
outMachine="$1" ; shift
outPath="$1" ; shift
dbs="$@"

dests=("$outMachine:$outPath" "${dests[@]}")

# check if a database exists, print note and return non-zero if it doesn't
dbExists() {
    local db="$1"
    local dbChk=$(/cluster/bin/x86_64/hgsql -Ne 'show databases like "'$db'"')
    if [ -z "$dbChk" ] ; then
        echo "Note: database $db does not exist, skipping"
        return 1
    else
        return 0
    fi
}

buildDbTrix() {
    local db="$1"
    local trixName="$2"
    local metaDbName="$3"
    local cvRaPath="$4"
    # Build into a temporary directory using the final file names, so that each
    # destination takes one rsync instead of one per file.  That is five fewer ssh
    # connections per destination, which matters when this make runs in parallel.
    local tmpDir=`mktemp -d`;
    local base="$tmpDir/${db}_${trixName}"
    /cluster/bin/x86_64/makeTrackIndex $db $metaDbName $cvRaPath > $base.txt;
    if test -s $base.txt; then
        /cluster/bin/x86_64/ixIxx -maxWordLength=64 $base.txt $base.ix $base.ixx
        # trixContextIndex makes the $db_$trixName.offsets and $db_$trixName.offsets.ixx files:
        /cluster/bin/x86_64/trixContextIndex $base.txt $base
        chmod 664 $base.txt $base.ix $base.ixx $base.offsets $base.offsets.ixx
        # the .txt, .offsets and .offsets.ixx files are for snippets on search pages:
        # Name the files explicitly rather than rsyncing $tmpDir/ itself.  With a
        # directory as the source, rsync also applies -a to the destination
        # directory, and setting its times fails unless we own it.
        local dest
        for dest in "${dests[@]}"; do
            rsync -a $base.txt $base.ix $base.ixx $base.offsets $base.offsets.ixx \
                "$dest/"
        done
    fi
    rm -rf $tmpDir
}

for db in $dbs ; do
    if dbExists $db ; then
        buildDbTrix $db $trixName $metaDbName $cvRaPath
    fi
done
