#!/bin/bash

# reset status
echo "failure" > status.txt

# create webpage folder
BRANCH_NAME=$( echo -e "${CI_COMMIT_REF_NAME////_}")
mkdir -p webpage/
mkdir -p webpage/recent
#for w in master develop
#do
#    if [[ "${CI_COMMIT_REF_NAME}" == "$w" ]]; then
#        mkdir -p "webpage/${BRANCH_NAME}"
#    fi
#done
mkdir -p "webpage/${BRANCH_NAME}"

cd docs  || {
    echo "no docs to build available";
    echo "incomplete" > status.txt;
    echo "no docs to build avail" > incomplete.txt;
    exit 0; }

echo "${CI_COMMIT_TAG}"
make clean
make html
IS_FAILED=$?

# copy results
cp -r ./_build/html/* "../webpage/${BRANCH_NAME}/."
cp -r ./_build/html/* ../webpage/recent/.
if [[ "${CI_COMMIT_REF_NAME}" = "master" ]]; then
    cp -r ./_build/html/* ../webpage/.
fi
cd ..

# report if job was successful
if [[ ${IS_FAILED} == 0 ]]; then
    echo "success"
    echo "success" > status.txt
    echo "build" > success.txt
    exit 0
else
    echo "failed"
    exit 1
fi
