#!/bin/bash 
#
# Script to update UCSC shared data tables.  The idea is to update, but if
# the update fails, not replace current data/tables with error
# messages.
set -e

# setup directories
BINDIR="/cvmfs/data.galaxyproject.org/managed/bin"
BROWSER="http://genome-test.cse.ucsc.edu/cgi-bin/das/dsn"
LENGTHS="/cvmfs/data.galaxyproject.org/managed/len/ucsc"
LOCATION="/cvmfs/data.galaxyproject.org/managed/location"

if [ -z "$WORKDIR" ]; then
    export WORKDIR=$(mktemp -t -d ucsc_builds_XXXXXXXX)
    cp -p $0 $WORKDIR
    echo "Re-executing $0 from ${WORKDIR}/$(basename $0)"
    exec bash ${WORKDIR}/$(basename $0)
fi

mkdir ${WORKDIR}/chrom ${WORKDIR}/build


# the only dependency is six
. ${HOME}/ucsc_update/bin/activate

echo "Working directory: $WORKDIR"

echo "Updating builds.txt"
python ${BINDIR}/parse_builds.py ${BROWSER} > ${WORKDIR}/build/builds.txt
echo "Updating ucsc_build_sites.txt"
python ${BINDIR}/parse_builds_3_sites.py > ${WORKDIR}/build/ucsc_build_sites.txt
echo "Building chromInfo tables"
python ${BINDIR}/build_chrom_db.py ${WORKDIR}/chrom/ ${WORKDIR}/build/builds.txt
echo "Adding Manual Builds"
curl https://raw.githubusercontent.com/galaxyproject/galaxy/dev/tool-data/shared/ucsc/manual_builds.txt.sample \
    | python ${BINDIR}/add_manual_builds.py /dev/stdin ${WORKDIR}/build/builds.txt ${WORKDIR}/chrom/

cvmfs_server transaction data.galaxyproject.org

for f in builds.txt ucsc_build_sites.txt; do
    echo "Comparing ${WORKDIR}/build/$f with ${LOCATION}/$f"
    diff -u ${WORKDIR}/build/$f ${LOCATION}/$f || { echo "Installing new $f"; cp ${WORKDIR}/build/$f ${LOCATION}/$f; }
done

new_len_flag=0
echo "Comparing length files"
for f in ${WORKDIR}/chrom/*.len; do
    f=$(basename $f)
    diff -q ${WORKDIR}/chrom/$f ${LENGTHS}/$f || { echo "Installing new $f"; cp ${WORKDIR}/chrom/$f ${LENGTHS}/$f; new_len_flag=1; }
done

if [[ $new_len_flag -eq 1 ]]; then
    echo '.len file changes detected'
    ${BINDIR}/ucsc_regenerate_dbkeys.sh
fi

cvmfs_server publish -a "ucsc-builds-$(date +%s)" -m "Update UCSC builds $(date +%Y-%m-%d)" data.galaxyproject.org

rm -rf ${WORKDIR}
echo "Update complete"
