#!/bin/sh
#
# Copyright (c) [2013-2014] Novell, Inc.  All Rights Reserved.
#
# THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.  IT
# MAY NOT BE USED, COPIED, DISTRIBUTED, DISCLOSED, ADAPTED, PERFORMED, DISPLAYED,
# COLLECTED, COMPILED, OR LINKED WITHOUT NOVELL'S PRIOR WRITTEN CONSENT.  USE OR
# EXPLOITATION OF THIS WORK WITHOUT AUTHORIZATION COULD SUBJECT THE PERPETRATOR
# TO CRIMINAL AND CIVIL LIABILITY.
#
# NOVELL PROVIDES THE WORK "AS IS," WITHOUT ANY EXPRESS OR IMPLIED WARRANTY,
# INCLUDING WITHOUT THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE, AND NON-INFRINGEMENT. NOVELL, THE AUTHORS OF THE WORK, AND
# THE OWNERS OF COPYRIGHT IN THE WORK ARE NOT LIABLE FOR ANY CLAIM, DAMAGES, OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING
# FROM, OUT OF, OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS IN
# THE WORK.
# This script is executed at the end of appliance creation.  Here you can do
# one-time actions to modify your appliance before it is ever used, like
# removing files and directories to make it smaller, creating symlinks,
# generating indexes, etc.
#
# The 'kiwi_type' variable will contain the format of the appliance (oem =
# disk image, vmx = VMware, iso = CD/DVD, xen = Xen).
#

readonly GREEN='\033[0;32m'
readonly RED='\033[0;31m'
readonly NOCOLOR='\033[0m'

printOKMessage() {
    printf "[  ${GREEN}OK${NOCOLOR}  ] CSP: $1\n" > /dev/console
}

printErrorMessage() {
    printf "[${RED}ERROR${NOCOLOR} ] CSP: $1\n" > /dev/console
}

displayStatus() {
    #Display info to help us
    cspctl status | tee /dev/console
    echo "Pods Status:" | tee /dev/console
    kubectl get pods -A | tee /dev/console
}

firewall() {
    ## Firewall Configurations
    /opt/opentext/csp/bin/cspctl verify --fixall &> /dev/null
    /opt/novell/base_config/firewall/firewall_config.sh reload
}

symlinks() {
     ## Sym Links
    ln -s /opt/opentext/csp/bin/kubectl /usr/bin/kubectl
}

bashprofile() {
     ## Bash Profile
    ln -s /opt/opentext/csp/bin/env.sh /etc/profile.d/csp-env.sh
}

removeRepos() {
    #Remove all zypper repos
    zypper rr 1
    zypper rr 1
    zypper rr 1
    zypper rr 1

    zypper lr
}

initcsp() {
    echo "" > /dev/console 
    cspctl enable > /dev/console
    cspctl verify > /dev/console
    echo "" > /dev/console
    cspctl start > /dev/console

    #in minutes
    sleepcounter=0
    isready=1
    while [ "$isready" -eq 1 ] && [ "$sleepcounter" -lt 8 ] ; do
        kubectl wait --for=condition=Ready nodes --all --timeout=1s &> /dev/null
        isready=$?
        sleepcounter=$(($sleepcounter+1))
        sleep 60
        printOKMessage "Waiting for node to be ready..."
    done

    if [ $isready -eq 1 ]; then
        printErrorMessage "Timed out waiting for node to be ready - $(date +%H:%M:%S)"
        displayStatus
        exit 1
    else
        printOKMessage "Node is ready - $(date +%H:%M:%S)"
    fi
}

clearConsole() {
    printf "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" > /dev/console
    printOKMessage "Initialization may take about 5 minutes - $(date +%H:%M:%S)"
}

registerGPGKeys() {
    rpmkeys --import /opt/novell/va_install/emc.pub
    rpmkeys --import /opt/novell/va_install/Rocket.package-sign.pub
}

main() {
    clearConsole
    firewall
    bashprofile
    symlinks
    registerGPGKeys
    removeRepos

    initcsp
}

main "$@"