# pvDatabase C++ implementation
# Jenkins @ Cloudbees documentation generation and deployment
#
# Jenkins invokes scripts with the "-ex" option. So the build is considered a failure
# if any of the commands exits with a non-zero exit code.
#
# Author: Ralph Lange <ralph.lange@gmx.de>
# Copyright (C) 2014 Helmholtz-Zentrum Berlin für Materialien und Energie GmbH
# Copyright (C) 2014-2015 ITER Organization.
# All rights reserved. Use is subject to license terms.

###########################################
# Set EPICS Base version and upload target

BASE=3.15.2
PUBLISH=${1:-DONT}

###########################################
# Fetch and unpack dependencies

export STUFF=/tmp/stuff

rm -fr ${STUFF}
mkdir -p ${STUFF}
cd ${STUFF}

wget -nv https://openepics.ci.cloudbees.com/job/Doxygen-1.8.3_Build/lastSuccessfulBuild/artifact/doxygen-1.8.3.CB-dist.tar.gz
tar -xzf doxygen-1.8.3.CB-dist.tar.gz

###########################################
# Generate

cd ${WORKSPACE}

wget -nv https://openepics.ci.cloudbees.com/job/pvDatabaseCPP_Build/BASE=${BASE},USE_MB=MB_NO/lastSuccessfulBuild/artifact/pvDatabase.CB-dist.tar.gz
tar -xzf pvDatabase.CB-dist.tar.gz

export PATH=${STUFF}/bin:${PATH}

doxygen

###########################################
# Publish

if [ "${PUBLISH}" != "DONT" ]; then
    # Upload explicit dummy to ensure target directory exists
    echo "Created by CloudBees Jenkins upload job. Should be deleted as part of the job." > DUMMY
    rsync -q -e ssh DUMMY epics-jenkins@web.sourceforge.net:/home/project-web/epics-pvdata/htdocs/docbuild/pvDatabaseCPP/${PUBLISH}/

    rsync -aqP --delete -e ssh documentation epics-jenkins@web.sourceforge.net:/home/project-web/epics-pvdata/htdocs/docbuild/pvDatabaseCPP/${PUBLISH}/
fi
