#!/bin/sh
set -o errexit -x

# jenkins job name
job_name="$1"

export PATH=/usr/local/bin:$PATH

if test -d build; then
    rm -fr build
fi
make builddir
cd build
make

( cd ../impatient-guide && rm -rf _build && make latexpdf && make html )

# publish
if test "${job_name}" = "master branch"; then

    ## new gh-pages stuff

    git_rev=`git rev-parse --short HEAD`
    export DIST_ROOT=`pwd`/dist

    rm -fr ${DIST_ROOT}
    git clone https://github.com/nexusformat/definitions.git --branch gh-pages ${DIST_ROOT}

    ## need to preserve CNAME file for custom github domain
	
    [ -f ${DIST_ROOT}/CNAME ] && cp -f ${DIST_ROOT}/CNAME /tmp/CNAME.$$
    rm -fr ${DIST_ROOT}/*
    [ -f /tmp/CNAME.$$ ] && mv /tmp/CNAME.$$ ${DIST_ROOT}/CNAME

    mkdir ${DIST_ROOT}/pdf
	
    cd manual
    cd source

    cp -fr examples ${DIST_ROOT}

    cd ..
    # be more deliberate about building the PDF
    # first, build all intermediate content, indices, etc.
    make latex
    cd build/latex/
    # next, build the PDF
    # extra option needed to satisfy "levels nested too deeply" error
    # expect it to fail (thus exit 0) since nexus.ind not found first time
    ( pdflatex --interaction=nonstopmode nexus.tex || exit 0 )
    # make the .ind file now
    makeindex nexus.idx
    # run again with fresh nexus.ind
    ( pdflatex --interaction=nonstopmode nexus.tex || exit 0 )
    cd ..

    #pdf
    cp -f latex/nexus.pdf ${DIST_ROOT}/pdf/NeXusManual.pdf
    wget https://github.com/nexusformat/code/raw/master/doc/api/NeXusIntern.pdf -O ${DIST_ROOT}/pdf/NeXusIntern.pdf
    wget https://github.com/nexusformat/code/raw/master/applications/NXtranslate/docs/NXtranslate.pdf -O ${DIST_ROOT}/pdf/NXtranslate.pdf

    #html
    cp -fr html/* ${DIST_ROOT}

    cd ../../..

    cp -fr /isis/www/nexus/doxygen ${DIST_ROOT}

    ( cd impatient-guide && cp -f _build/latex/NXImpatient.pdf ${DIST_ROOT}/pdf/NXImpatient.pdf && cp -rf _build/html ${DIST_ROOT}/impatient )

    cd ${DIST_ROOT}
    git add .
    git commit -m "Update docs for ${git_rev}"
    git push origin gh-pages

fi

exit 0
