# Usage examples:
# first you need to source this script, so that you can use it's methods:
# $ source releaseSilecs.sh
#
# release a new version v1.0.0
# $ release 1.0.0 /common/usr/cscofe/silecs /home/bel/schwinn/lnx/git/silecs
#
# patch 2 packages of an existing version 1.2.3
# $ patch silecs-codegen 1.2.4 1.2.3 /common/usr/cscofe/silecs /home/bel/schwinn/lnx/git/silecs
# $ patch silecs-model 1.2.4 1.2.3 /common/usr/cscofe/silecs /home/bel/schwinn/lnx/git/silecs
#
# release a new version of the silecs-eclipse plugin
# $ plugin_release /common/usr/cscofe/silecs /home/bel/schwinn/lnx/git/silecs-plugin

# List of silecs packages which is released or patched
PACKAGES="silecs-codegen silecs-model silecs-communication-cpp silecs-diagnostic-cpp snap7"

# Check that folder exists, print proper error if not
checkFolderExists()
{
    FOLDER=$1
    if [ ! -d ${FOLDER} ]; then
        echo "Error: The folder '${FOLDER}' is needed in order to finish the installation."
        echo "Installation cancelled."
        return 1
    fi
}

# releases all silecs-packages to release-dir
release()
{
    VERSION=$1
    RELEASE_DIR_BASE=$2
    WORKSPACE=$3
    checkFolderExists ${WORKSPACE}
    
    for PACKAGE in $PACKAGES; do
        INSTALL_DIR=${RELEASE_DIR_BASE}/${PACKAGE}/${VERSION}
        if [ -d ${INSTALL_DIR} ]; then 
            echo "Error: Silecs version ${VERSION} is already installed in ${INSTALL_DIR}. Exiting now."
            return 1
        else
            checkFolderExists ${WORKSPACE}/${PACKAGE}
            echo "installing ${INSTALL_DIR} from ${WORKSPACE}/${PACKAGE}"
            ${WORKSPACE}/${PACKAGE}/install.sh ${INSTALL_DIR}
        fi
    done
    return 0
}

plugin_release()
{
    (
        set -e
        RELEASE_DIR_BASE=$1
        WORKSPACE=$2
        PACKAGE=silecs-eclipse-plugin-update-site
        INSTALL_DIR=${RELEASE_DIR_BASE}/${PACKAGE}
        checkFolderExists ${WORKSPACE}/${PACKAGE}
        echo "installing ${INSTALL_DIR} from ${WORKSPACE}/${PACKAGE}"
        ${WORKSPACE}/${PACKAGE}/install.sh ${INSTALL_DIR}
        return 0
    )
}
    
# releases a specific package to release-dir. For all other packages symlinks to the base-version are generated
patch()
{
    PACKAGE_TO_PATCH=$1
    NEW_VERSION=$2
    BASE_VERSION=$3
    RELEASE_DIR_BASE=$4
    WORKSPACE=$5
    checkFolderExists ${WORKSPACE}

    for PACKAGE in $PACKAGES; do
        INSTALL_DIR=${RELEASE_DIR_BASE}/${PACKAGE}/${NEW_VERSION}
        BASE_DIR=${RELEASE_DIR_BASE}/${PACKAGE}/${BASE_VERSION}
        checkFolderExists ${BASE_DIR}
        checkFolderExists ${BASE_DIR}
        checkFolderExists ${INSTALL_DIR}
        if [ "$PACKAGE" == "$PACKAGE_TO_PATCH" ]; then
            if [ -d ${INSTALL_DIR} ]; then
               if [ -L ${INSTALL_DIR} ]; then
                  checkFolderExists ${WORKSPACE}/${PACKAGE}
                  # Replace Sym-Link with patch-version ... needed if more than one package is patched for the same version
                  unlink ${INSTALL_DIR}
                  checkFolderExists ${WORKSPACE}/${PACKAGE}
                  ${WORKSPACE}/${PACKAGE}/install.sh ${INSTALL_DIR}
               else
                  echo "Error: Silecs version ${NEW_VERSION} of package ${PACKAGE} is already installed in ${INSTALL_DIR}. Exiting now."
                  return 1
               fi
            else
               checkFolderExists ${WORKSPACE}/${PACKAGE}
               echo "installing ${INSTALL_DIR} from ${WORKSPACE}/${PACKAGE}"
               ${WORKSPACE}/${PACKAGE}/install.sh ${INSTALL_DIR}
            fi
        else
           if [ ! -d ${INSTALL_DIR} ]; then
                # no patched version for this package available --> link to base-version
                echo "Symbolic link to base version '${BASE_VERSION}' created for package '${PACKAGE}' of version '${NEW_VERSION}'"
                ln -s ${BASE_VERSION} ${INSTALL_DIR}
            fi
        fi
    done
    return 0
}
