# normativeTypes 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) 2013 Helmholtz-Zentrum Berlin für Materialien und Energie GmbH
# Copyright (C) 2014-2016 ITER Organization.
# All rights reserved. Use is subject to license terms.

installTool () {
    local module=$1
    local version=$2

    wget -nv https://openepics.ci.cloudbees.com/job/${module}-${version}_Build/lastSuccessfulBuild/artifact/${module,,}-${version}.CB-dist.tar.gz
    tar -xzf ${module,,}-${version}.CB-dist.tar.gz
}

installE4 () {
    local module=$1
    local branch=$2

    wget -nv https://openepics.ci.cloudbees.com/job/e4-cpp-${module}-${branch}-build/BASE=${BASE}/lastSuccessfulBuild/artifact/${module}.CB-dist.tar.gz
    tar -xzf ${module}.CB-dist.tar.gz
}

###########################################
# Defaults for EPICS Base and parameters

BASE=3.15.4
PUBLISH=${PUBLISH:-NO}
BRANCH=${BRANCH:-master}

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

export STUFF=/tmp/stuff

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

installTool Doxygen 1.8.11

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

cd ${WORKSPACE}

installE4 normativeTypes ${BRANCH}

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/normativeTypesCPP/${PUBLISH}/

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