#!/bin/bash

if [[ -z "${BITBUCKET_CLONE_DIR}" ]]; then
   YS_LOCAL=1;
   BITBUCKET_CLONE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/..";
fi

PUBLISH_SSH_USER="ysqwgcje"
PUBLISH_URL="download.y-square.de"
PUBLISH_PATH="~/download.y-square.de"

MODULE_NAME="ys_giftcert"
MODULE_TAR_NAME="YS-GIFTCERT"


# Get latest Tag on branch
LATEST_TAG=$(git describe --abbrev=0)

# Check if we are in Release Mode (Tag available) or in branch mode
if [[ -z "${BITBUCKET_TAG}" && -z "${LATEST_TAG}" ]]; then
    echo "==============="
    echo "=             ="
    echo "= Local Mode  ="
    echo "=             ="
    echo "===============";
elif [[ -z "${BITBUCKET_TAG}" ]]; then
    echo "==============="
    echo "=             ="
    echo "= Branch-Mode ="
    echo "=             ="
    echo "===============";
    # increase version number by one as dev is the next version and not the current
    LATEST_TAG=$(echo ${LATEST_TAG} | perl -spe 's/(\d+).(\d+).(\d+)/$1.${\($2 + 1)}.$3/g')
    VERSION="${LATEST_TAG}-dev";
else
    echo "================"
    echo "=              ="
    echo "=   Tag-Mode   ="
    echo "=              ="
    echo "================";
    VERSION=${BITBUCKET_TAG};
fi

echo
echo "========================== Setting Versions =========================="
echo "= VERSION: ${VERSION} ="
cd ${BITBUCKET_CLONE_DIR}

if [[ -z "${YS_LOCAL}" ]]; then
    sed -i "s/'version'.*=>.*'.*',/'version' => '${VERSION}',/" src/metadata.php
    sed -i "s/\"version\".*:.*\".*\",/\"version\": \"${VERSION}\",/" src/composer.json
    sed -i "s/version.*=.*\'.*\',/version = \'${VERSION}\'/" docs/source/conf.py
    sed -i "s/release.*=.*\'.*\',/release = \'${VERSION}\'/" docs/source/conf.py
fi

echo
echo "==========================  Building docs  ==========================="
cd ${BITBUCKET_CLONE_DIR}/docs
make html

echo
echo "========================== Building Package =========================="
echo "NPM"
echo "---"
cd ${BITBUCKET_CLONE_DIR}/src/build/.npm
npm install
npm start
echo "NPM END"
echo "-------"

cd ${BITBUCKET_CLONE_DIR}
mkdir -p ${BITBUCKET_CLONE_DIR}/release/ysquare/${MODULE_NAME}
cp -R ${BITBUCKET_CLONE_DIR}/src/* ${BITBUCKET_CLONE_DIR}/release/ysquare/${MODULE_NAME}
rm -rf ${BITBUCKET_CLONE_DIR}/release/ysquare/${MODULE_NAME}/build
mkdir -p ${BITBUCKET_CLONE_DIR}/release/documentation/${VERSION}
cp -R docs/build/html ${BITBUCKET_CLONE_DIR}/release/documentation/${VERSION}

echo
echo "==========================    Packaging     =========================="
if [[ -z "${YS_LOCAL}" ]]; then
    cd ${BITBUCKET_CLONE_DIR}/release/ysquare
    tar cfz ${MODULE_TAR_NAME}-${VERSION}.tgz ${MODULE_NAME}

    echo
    echo "==========================    Publishing    =========================="
    cd ${BITBUCKET_CLONE_DIR}/release/ysquare
    ssh ${PUBLISH_SSH_USER}@${PUBLISH_URL} "mkdir -p ${PUBLISH_PATH}/packages/${MODULE_NAME}"
    scp ${BITBUCKET_CLONE_DIR}/release/ysquare/${MODULE_TAR_NAME}-${VERSION}.tgz ${PUBLISH_SSH_USER}@${PUBLISH_URL}:${PUBLISH_PATH}/packages/${MODULE_NAME}/
    rsync -azh -e ssh --ignore-times --delete --stats ${BITBUCKET_CLONE_DIR}/release/documentation/${VERSION} ${PUBLISH_SSH_USER}@${PUBLISH_URL}:${PUBLISH_PATH}/docs/${MODULE_NAME}
fi