#!/bin/bash -e
usage='loadTracks [options] trackDb hgFindSpec db0 ...

options:
   -release=release
   -strict
   -local
   -raName=trackDb.ra  - base name of ra file
   -grpSql=grp.sql - create a grp table using this sql file
   -sqlDir=../../lib - directory containg trackDb.sql and hgFindSpec.sql'

UNAME_N=`uname -n`
UNAME_N=${UNAME_N/.cse.ucsc.edu/}
release=''
settings=''
strict=''
raName=''
grpSql=''
sqlDir='../../lib'
while [[ $1 == -* ]] ; do
    case $1 in
        -release=*)
            release=$(echo $1 | sed 's/-release=//') ;;
        -strict)
            strict=yes;;
        -settings)
            settings="yes";;
        -local)
            local=yes;;
        -raName=*)
            raName=$(echo $1 | sed 's/-raName=//') ;;
        -grpSql=*)
            grpSql=$(echo $1 | sed 's/-grpSql=//') ;;
        -sqlDir=*)
            sqlDir=$(echo $1 | sed 's/-sqlDir=//') ;;
        *)
            echo "invalid option: $1" >&2
            exit 1 ;;
    esac
    shift
done

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

trackDb="$1"; shift
hgFindSpec="$1" ; shift
dbs="$@"

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

# load trackDb for a database
loadDbTracks() {
    local db="$1"
    local dbpath=$(ls -1 -d */$db)
    local org=$(echo $dbpath | sed -e 's/\/.*//')
    local topts='' fopts='' qopts='-check'
    if [ -f $dbpath/visibility.ra ] ; then
        topts="$topts -visibility=$dbpath/visibility.ra"
    elif [ -f $org/visibility.ra ] ; then
        topts="$topts -visibility=$org/visibility.ra"
    fi
    if [ -f $dbpath/priority.ra ] ; then
        topts="$topts -priority=$dbpath/priority.ra"
    elif [ -f $org/priority.ra ] ; then
        topts="$topts -priority=$org/priority.ra"
    fi
    if [ -n "$release" ] ; then
        topts="$topts -release=$release"
        qopts="$qopts -release=$release"
    fi
    if [ -n "$strict" ] ; then
        topts="$topts -strict"
        fopts="$fopts -strict"
        qopts="$qopts -strict"
    fi
    if [ -n "$settings" ] ; then
        topts="$topts -settings"
    fi
    if [ -n "$local" ] ; then
        topts="$topts -local"
        fopts="$fopts -local"
    fi
    if [ -n "$raName" ] ; then
        topts="$topts -raName=$raName"
        fopts="$fopts -raName=$raName"
    fi
    # don't use set -x, since the autoload scripts e-mail stderr.
    local cmd="hgTrackDb $topts $org $db $trackDb ${sqlDir}/trackDb.sql ."
    echo $cmd
    eval $cmd

    cmd="tdbQuery $qopts 'select count(*) from $db' -root=`pwd`"
    echo $cmd
    eval $cmd

    cmd="hgFindSpec $fopts $org $db $hgFindSpec ${sqlDir}/hgFindSpec.sql ."
    echo $cmd
    eval $cmd
    if [ -z "$strict" -a -f $dbpath/description.html ] ; then
	if [ "X${UNAME_N}Y" = "XhgwdevY" ]; then
	    if [ ! -L /gbdb/$db/html -a -d /hive/data/genomes/$db/html ]; then
		rm -f /gbdb/$db/html/description.html
		rmdir /gbdb/$db/html
		ln -s /hive/data/genomes/$db/html /gbdb/$db/html
	    fi
	    if [ -f /gbdb/$db/html/description.html ]; then
		rm -f /gbdb/$db/html/description.html
	    fi
	fi
        rm -f /gbdb/$db/html/description.html
        cp -p $dbpath/description.html /gbdb/$db/html/description.html
    fi
    if [ -n "$grpSql" ] ; then
        cmd="hgsql $db < $grpSql"
        echo $cmd
        eval $cmd
    fi
}

# load for all specified databases
for db in $dbs ; do
    if dbExists $db ; then
        loadDbTracks $db
    fi
done

