#! /bin/bash
# A script to prepare, install, and remove the Hmedia Printserver into the connected Kubernetes cluster (reachable via kubectl)
#
# Prerequisites:
# certain Linux bash tools
# Kubernetes up and running, reachable via kubectl
# helm installed
#
# version 1.0.0
# Author: Andreas Hartmann, Hmedia

BLUE=$(tput setaf 4)
GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
BOLD=$(tput bold)
ITALIC=$(tput sitm)
REVERSE=$(tput rev)
DIM=$(tput dim)
NORMAL=$(tput sgr0)
UNDERLINE=$(tput smul)

THIS_VERSION="1.0.0"
HELM_REPO="https://hmedia.de/helm"
HELM_REPO_NAME="hmediade"

########################
# check availability of tools
# mkdir, sed, awk, less, base64, helm, kubectl
########################
MKDIR_CMD="$(command -v mkdir)"
if [[ -z $MKDIR_CMD ]]
then
    echo "Command ${RED}mkdir$NORMAL couldn't be found but is required for this program."
    echo "Please grant that the package ${BOLD}coreutils$NORMAL is installed before calling ${BOLD}printserverctl$NORMAL."
    exit 127
fi
SED_CMD="$(command -v sed)"
if [[ -z $MKDIR_CMD ]]
then
    echo "Command ${RED}sed$NORMAL couldn't be found but is required for this program."
    echo "Please grant that it is available and working before calling ${BOLD}printserverctl$NORMAL."
    exit 127
fi
AWK_CMD="$(command -v awk)"
if [[ -z $MKDIR_CMD ]]
then
    echo "Command ${RED}awk$NORMAL couldn't be found but is required for this program."
    echo "Please grant that it is available and working before calling ${BOLD}printserverctl$NORMAL."
    exit 127
fi
LESS_CMD="$(command -v less)"
if [[ -z $LESS_CMD ]]
then
    echo "Command ${RED}less$NORMAL couldn't be found but is required for this program."
    echo "Please grant that it is available and working before calling ${BOLD}printserverctl$NORMAL."
    exit 127
fi
BASE64_CMD="$(command -v base64)"
if [[ -z $BASE64_CMD ]]
then
    echo "Command ${RED}base64$NORMAL couldn't be found but is required for this program."
    echo "Please grant that the package ${BOLD}coreutils$NORMAL is installed before calling ${BOLD}printserverctl$NORMAL."
    exit 127
fi
HELM_CMD="$(command -v helm)"
if [[ -z $HELM_CMD ]]
then
    echo "Command ${RED}helm$NORMAL couldn't be found but is required for this program."
    echo "Please grant that it is available and working before calling ${BOLD}printserverctl$NORMAL."
    exit 127
fi
KUBECTL_CMD="$(command -v kubectl)"
if [[ -z $KUBECTL_CMD ]]
then
    echo "Command ${RED}kubectl$NORMAL couldn't be found but is required for this program."
    echo "Please grant that it is available and working before calling ${BOLD}printserverctl$NORMAL."
    exit 127
fi
if ! $($KUBECTL_CMD get nodes > /dev/null 2>&1)
then
    echo "The ${RED}Kubernetes cluster$NORMAL seems to be offline (${BOLD}kubectl get nodes$NORMAL failed)."
    echo "Please grant that it is available and working before calling ${BOLD}printserverctl$NORMAL."
    echo "To preserve the environment when running sudo, use ${BOLD}sudo -E printserverctl$NORMAL."
    exit 1
fi

########################
#check run in sudo
########################
if [ "$(id -u)" -ne "0" ]
then
    echo "The program doesn't run with ${RED}enough privileges$NORMAL."
    echo "Please call ${BOLD}printserverctl$NORMAL as root or within sudo."
    echo "To preserve the environment when running sudo, use ${BOLD}sudo -E printserverctl$NORMAL."
    exit 126
fi

########################
# Functions
########################
function parse_yaml {
   local prefix=$2
   local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
   $SED_CMD -ne "s|^\($s\):|\1|" \
        -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
        -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $1 |
   $AWK_CMD -F$fs '{
      indent = length($1)/2;
      vname[indent] = $2;
      for (i in vname) {if (i > indent) {delete vname[i]}}
      if (length($3) > 0) {
         vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
         printf("%s%s%s=\"%s\"\n", "'$prefix'",toupper(vn), toupper($2), $3);
      }
   }'
}

function input_passwd {

    unset PASSWORD
    unset CHARCOUNT
    unset PROMPT

    stty -echo

    CHARCOUNT=0
    while IFS= read -p "$PROMPT" -r -s -n 1 CHAR
    do
        # Enter - accept password
        if [[ $CHAR == $'\0' ]] ; then
            break
        fi
        # Backspace
        if [[ $CHAR == $'\177' ]] ; then
            if [ $CHARCOUNT -gt 0 ] ; then
                CHARCOUNT=$((CHARCOUNT-1))
                PROMPT=$'\b \b'
                PASSWORD="${PASSWORD%?}"
            else
                PROMPT=''
            fi
        else
            CHARCOUNT=$((CHARCOUNT+1))
            PROMPT='*'
            PASSWORD+="$CHAR"
        fi
    done

    stty echo
    echo $PASSWORD
}

function validate_preparation {
    # values file present
    if ! [ -f "$VALUES_FILE" ]
    then
        echo "The file $VALUES_FILE is required for this step. Please call ${BOLD}sudo printserverctl prepare$NORMAL before."
        exit 1
    fi
    # helm repo has hmediade
    if ! $($HELM_CMD repo add $HELM_REPO_NAME $HELM_REPO > /dev/null)
    then
        echo "Fatal error: Can't include the repository $HELM_REPO."
        exit 1
    fi
}

function trim() {
    local var="$*"
    # remove leading whitespace characters
    var="${var#"${var%%[![:space:]]*}"}"
    # remove trailing whitespace characters
    var="${var%"${var##*[![:space:]]}"}"
    printf '%s' "$var"
}

function get_deploy_status {
    # Check if printserver is installed
    CURRENT_DEPLOY_NAME=$(trim $($HELM_CMD ls --all-namespaces | grep "printserver-" | cut -f1))
    if [ -n "$CURRENT_DEPLOY_NAME" ]
    then
        IS_INSTALLED="true"
        CURRENT_NAMESPACE=$(trim $($HELM_CMD ls --all-namespaces | grep "printserver-" | cut -f2))
    fi

}

function prepare_repo {
    echo "Checking Helm repository."
    if ! $($HELM_CMD repo ls | grep "$HELM_REPO" > /dev/null)
    then
        $HELM_CMD repo add "$HELM_REPO_NAME" "$HELM_REPO"
        if [ $? != 0 ]
        then
            echo "Couldn't add the repository $HELM_REPO. Please ensure, that https access is given to that URL."
            exit 1
        fi
    fi

    echo "Updateing Helm repositories..."
    if ! $($HELM_CMD repo update > /dev/null)
    then
        echo "Couldn't update the Helm repositories. Your charts might not be of the latest version!"
    fi
}


########################
# Required Default Variables
########################
INGRESSNGINX_INSTALL="true"
INGRESSNGINX_CLASSNAME="nginx"
INGRESSNGINX_AUTH_ENABLED="true"
INGRESSNGINX_AUTH_USERNAME="printclient_user"
INGRESSNGINX_AUTH_PASSWORD="printclient_passwd"
INGRESSNGINX_TLS_ENABLED="true"
INGRESSNGINX_TLS_SECRETNAME="printserver-tls"
INGRESSNGINX_TLS_CERTIFICATE="aW52YWxpZA== #invalid, must be injected at command line through install"
INGRESSNGINX_TLS_KEY="aW52YWxpZA== #invalid, must be injected at command line through install"
PRINTSERVER_PULLPOLICY="Always"
PRINTSERVER_DOCKERTAG="4.0.0"
PRINTSERVER_NUMBEROFINSTANCES="1"
PRINTSERVER_LICENSE="aW52YWxpZA== #invalid, must be injected at command line through install"
PRINTSERVER_INEWSCONF="aW52YWxpZA== #invalid, must be injected at command line through install "
PRINTSERVERINIT_PULLPOLICY="Always"
PRINTSERVERINIT_DOCKERTAG="1.0.0"
PRINTSTYLESOURCE_CONNECTIONTYPE="scp"
PRINTSTYLESOURCE_USERNAME="printstyles_user"
PRINTSTYLESOURCE_PASSWORD="printstyles_passwd"
PRINTSTYLESOURCE_HOST="printstyles_host"
PRINTSTYLESOURCE_PATH="path/to/printstyles"
CLUSTER_FQDN="k8s.cluster.fqdn"
SERVICE_PORT="8080"

SETTINGS_DIR="/etc/hmedia/printserver"

VALUES_FILE="$SETTINGS_DIR/values.yaml"
LICENSE_FILE="$SETTINGS_DIR/license.lic"
INEWSCONF_FILE="$SETTINGS_DIR/inewsconf.xml"
TLS_CERT_FILE="$SETTINGS_DIR/tls.crt"
TLS_KEY_FILE="$SETTINGS_DIR/tls.key"

FILEPATHS_TLSCERT="$TLS_CERT_FILE"
FILEPATHS_TLSKEY="$TLS_KEY_FILE"
FILEPATHS_INEWSCONF="$INEWSCONF_FILE"
FILEPATHS_LICENSE="$LICENSE_FILE"

DEPLOYMENT_NAME="printserver"
DEPLOYMENT_NAMESPACE="default"

ACCESS_PROTOCOL="https"
IS_INSTALLED="false"


########################
# Propagate Values from File
########################
if [ -f "$VALUES_FILE" ]
then
    eval $(parse_yaml "$VALUES_FILE")

    if [ $INGRESSNGINX_TLS_ENABLED == "false" ]
    then
        ACCESS_PROTOCOL="http"
    fi
fi


########################
# create settings folder
########################
mkdir -p "$SETTINGS_DIR"


########################
# Helper Texts
########################
USAGE="
Hmedia PrintServer Control

  Find more information on ${UNDERLINE}https://hmedia.de/solutions/products/printmanager$NORMAL.

Usage: ${BOLD}sudo printserverctl <command>$NORMAL, where command is one of
${BOLD}prepare$NORMAL   Interactive setup to enter parameters and file paths related to your environment.
${BOLD}clean$NORMAL     Removes all config files from a previous preparation - doesn't affect a running instance.
${BOLD}review$NORMAL    Show all given parameters of a previous preparation step.
${BOLD}dryrun$NORMAL    Executes a dry run template creation, and prints out potential issues and problems.
${BOLD}deploy$NORMAL    Installs or updates the printserver Helm chart into Kubernetes.
${BOLD}remove$NORMAL    Removes the Helm printserver installation from Kubernetes.
${BOLD}version$NORMAL   Prints various software versions.
${BOLD}help$NORMAL      Print this page."

USAGE_PREPARATION="
Hmedia PrintServer Preparation

Usage: ${BOLD}sudo printserverctl prepare <command>$NORMAL, where command is one of
help        Prints this page.
<any>       Starts the interactive mode to enter all config values.
            If you already finished the preparation, the default values are taken from that previous execution.
            Default values are displayed in square brackets, like [default].
            If you want to start from scratch, delete teh file $BOLD$VALUES_FILE$NORMAL before.
            You can interrupt the dialogue at any time by pressing ${BOLD}CTRL+C$NORMAL.

This interactive setup will ask you for the following parameters:


${BOLD}deployment.name$NORMAL
    Name of the Helm object.
    Default is $BOLD$DEPLOYMENT_NAME$NORMAL.

${BOLD}deployment.namespace$NORMAL
    Target Namespace in Kubernetes.
    Default is $BOLD$DEPLOYMENT_NAMESPACE$NORMAL.

${BOLD}ingressNginx.install$NORMAL
    Do you want the NGINX Ingress chart automatically installed? 
    Answer ${BOLD}true$NORMAL or ${BOLD}false$NORMAL. 
    Default is $BOLD$INGRESSNGINX_INSTALL$NORMAL.
    The PrintServer depends on the ingress controller. Only set to ${BOLD}false$NORMAL 
    if the ingress controller is already deployed in this particular Kubernetes cluster.

${BOLD}ingressNginx.className$NORMAL
    Name of the nginx ingress class.
    Default is $BOLD$INGRESSNGINX_CLASSNAME$NORMAL.

${BOLD}ingressNginx.auth.enabled$NORMAL
    Do you want the connection to PrintServer protected with HTTP Basic Auhtentication?
    Answer ${BOLD}true$NORMAL or ${BOLD}false$NORMAL. 
    Default is $BOLD$INGRESSNGINX_AUTH_ENABLED$NORMAL.

${BOLD}ingressNginx.auth.username$NORMAL
    Username for HTTP Basic Authentication.
    Default is $BOLD$INGRESSNGINX_AUTH_USERNAME$NORMAL.
    Not used when ${BOLD}ingressNginx.auth.enabled$NORMAL is set to ${BOLD}false$NORMAL.

${BOLD}ingressNginx.auth.password$NORMAL
    Password for HTTP Basic Authentication.
    Not used when ${BOLD}ingressNginx.auth.enabled$NORMAL is set to ${BOLD}false$NORMAL.

${BOLD}ingressNginx.tls.enabled$NORMAL
    Do you want the connection to PrintServer encrypted via TLS 1.3?
    Answer ${BOLD}true$NORMAL or ${BOLD}false$NORMAL. 
    Default is $BOLD$INGRESSNGINX_TLS_ENABLED$NORMAL.

${BOLD}ingressNginx.tls.secretName$NORMAL
    Name of the Secret object to store the TLS certificate.
    Default is $BOLD$INGRESSNGINX_TLS_SECRETNAME$NORMAL.

${BOLD}Filepath to TLS certificate$NORMAL
    Absolute path and filename of the TLS certificate. 
    printserverctl will copy the file to /etc/hmedia/printserver and process it further.
    No default value.
    Not used when ${BOLD}ingressNginx.tls.enabled$NORMAL is set to ${BOLD}false$NORMAL.

${BOLD}Filepath to TLS key$NORMAL
    Absolute path and filename of the TLS key. The key file must be decrypted and NOT protected by a password.
    printserverctl will copy the file to /etc/hmedia/printserver and process it further.
    No default value.
    Not used when ${BOLD}ingressNginx.tls.enabled$NORMAL is set to ${BOLD}false$NORMAL.

${BOLD}printserver.pullPolicy$NORMAL
    When should Docker retrieve the printserver image from the Docker Hub registry?
    Answers
        ${BOLD}Always$NORMAL Helm retrieves the image each time when installing that chart.
        ${BOLD}IfNotPresent$NORMAL Helm retrieves the image from the registry when it is not loacally available.
        ${BOLD}Never$NORMAL Helm doesn't retrieve the image at all. It must be already present locally. 
    Default is $BOLD$PRINTSERVER_PULLPOLICY$NORMAL.

${BOLD}printserver.dockerTag$NORMAL
    Which Docker Tag of the PrintServer container shall be used for the installation. Docker tags usually refer to the software version or variant.
    For available tags refer to ${UNDERLINE}https://hub.docker.com/r/hmediade/printserver/tags$NORMAL.
    Default is $BOLD$PRINTSERVER_DOCKERTAG$NORMAL.

${BOLD}printserver.numberOfInstances$NORMAL
    How many instances of the PrintServer shall be deployed in the Kubernetes cluster?
    Answer is an Integer.
    Default is $BOLD$PRINTSERVER_NUMBEROFINSTANCES$NORMAL.

${BOLD}Filepath to the iNEWS config file$NORMAL
    Absolute path and filename of PrintServer inewsconf.xml.
    printserverctl will copy the file to /etc/hmedia/printserver and process it further.
    No default value. Without that config file, PrintServer will not operate.

${BOLD}Filepath to your PrintServer license file$NORMAL
    Absolute path and filename of the PrintServer license.lic. You can obtain such a license from Hmedia. 
    printserverctl will copy the file to /etc/hmedia/printserver and process it further.
    No default value. Without that license, PrintServer will not operate fully functional.

${BOLD}printserverInit.pullPolicy$NORMAL
    When should Docker retrieve the printserver-init image from the Docker Hub registry?
    Answers
        ${BOLD}Always$NORMAL Helm retrieves the image each time when installing that chart.
        ${BOLD}IfNotPresent$NORMAL Helm retrieves the image from the registry when it is not loacally available.
        ${BOLD}Never$NORMAL Helm doesn't retrieve the image at all. It must be already present locally. 
    Default is $BOLD$PRINTSERVERINIT_PULLPOLICY$NORMAL.

${BOLD}printserverInit.dockerTag$NORMAL
    Which Docker Tag of the PrintServer Init container shall be used for the installation. Docker tags usually refer to the software version or variant.
    For available tags refer to ${UNDERLINE}https://hub.docker.com/r/hmediade/printserver-init/tags$NORMAL.
    Default is $BOLD$PRINTSERVERINIT_DOCKERTAG$NORMAL.

${BOLD}printstyleSource.connectionType$NORMAL
    How shall the PrintServer retrieve its printstyle files during startup?.
    Answers
        ${BOLD}SCP$NORMAL PrintServer will connect into a Linux machine via SCP.
        ${BOLD}Git$NORMAL PrintServer checks out the required files from a Git repository via HTTPS.
    Default is $BOLD$PRINTSTYLESOURCE_CONNECTIONTYPE$NORMAL.

${BOLD}printstyleSource.username$NORMAL
    Username for printstyles retrieval.
    Default is $BOLD$PRINTSTYLESOURCE_USERNAME$NORMAL.

${BOLD}printstyleSource.password$NORMAL
    Password for printstyles retrieval.
    Default is $BOLD$PRINTSTYLESOURCE_PASSWORD$NORMAL.

${BOLD}printstyleSource.host$NORMAL
    IP address, hostname, or FQDN of the printstyle source.
    Default is $BOLD$PRINTSTYLESOURCE_HOST$NORMAL.

${BOLD}printstyleSource.path$NORMAL
    The local path to read the printstyles from. 
    In case of SCP connection, the path should be an absolute path in the file system.
    In case of git clone, the path is the URL part after the server name. In total, the URL will look like: https://username:password@host/path
    Default is $BOLD$PRINTSTYLESOURCE_PATH$NORMAL.

${BOLD}cluster.fqdn$NORMAL
    FQDN of the Kubernetes cluster. The PrintServer web page and REST API will be available on the URL $ACCESS_PROTOCOL://<cluster.fqdn>/printserver.
    Default is $BOLD$CLUSTER_FQDN$NORMAL.

${BOLD}service.port$NORMAL
    The network port for the Service object (connection from the ingress to the service).
    Default is $BOLD$SERVICE_PORT$NORMAL.

Quit this view with ${BOLD}q$NORMAL."

########################
# main switch statement
########################
case $1 in

    "prepare")
        # Preparation

        case $2 in
            help)
                echo "$USAGE_PREPARATION" | $LESS_CMD -r
            ;;
            *)
                # dialogue
                CONFIRM="n"
                echo "The following dialogue guides you through all environment settings for the PrintServer."
                echo ""
                echo "Default values are displayed in square brackets, like [default]."
                echo "If you already finished the preparation before, the default values are taken from that previous execution."
                echo "If you want to start from scratch, call ${BOLD}sudo printserver clean$NORMAL before."
                echo ""
                echo "You can interrupt the dialogue at any time by pressing ${BOLD}CTRL+C$NORMAL."
                echo ""

                read -p "Continue (otherwise the help page will be printed)? (y/N): " CONFIRM
                CONFIRM=$(echo ${CONFIRM:-n} | cut -c1 | tr '[:upper:]' '[:lower:]')

                if [ "$CONFIRM" != "y" ]
                then
                    echo "$USAGE_PREPARATION" | $LESS_CMD -r
                    exit 0
                fi
                echo ""


                # DEPLOYMENT_NAME
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    echo "The Hmedia PrintServer will be installed by Helm."
                    read -p "Enter the name of the Helm object [$DEPLOYMENT_NAME]: " INPUT
                    INPUT="${INPUT:-$DEPLOY_NAME}"
                    if $(echo "$INPUT" | grep -P '[a-z]([-a-z0-9]*[a-z0-9])?' > /dev/null)
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                        echo "The name $RED$INPUT$NORMAL is not a valid K8s object label (RFC 1035)."
                        continue
                    fi
                    echo "The PrintServer will have the Helm name: $INPUT."
                    CONFIRM="y"
                done
                DEPLOYMENT_NAME="$INPUT"
                echo ""

                # DEPLOYMENT_NAMESPACE
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    echo "Which Kubernetes Namespace shall be used/created for the PrintServer?"
                    read -p "Enter the Kubernetes Namespace [$DEPLOYMENT_NAMESPACE]: " INPUT
                    INPUT=${INPUT:-$DEPLOYMENT_NAMESPACE}
                    if $(echo "$INPUT" | grep -P '[a-z]([-a-z0-9]*[a-z0-9])?' > /dev/null)
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                        echo "The name $RED$INPUT$NORMAL is not a valid K8s object label (RFC 1035)."
                        continue
                    fi
                    echo "The PrintServer will be installed into the Kubernetes namespace: $INPUT."
                    CONFIRM="y"
                done
                DEPLOYMENT_NAMESPACE="$INPUT"
                echo ""

                # INGRESSNGINX_INSTALL
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    echo "Do you want the NGINX Ingress chart automatically installed?"
                    read -p "Enter true or false [$INGRESSNGINX_INSTALL]: " INPUT
                    INPUT=$(echo ${INPUT:-$INGRESSNGINX_INSTALL} | tr '[:upper:]' '[:lower:]')
                    if [ "$INPUT" != "true" ] && [ "$INPUT" != "false" ] && [ -n "$INPUT" ]
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                        continue
                    fi
                    echo "The NGINX Ingress chart will be installed: $INPUT."
                    CONFIRM="y"
                done
                INGRESSNGINX_INSTALL="$INPUT"
                echo ""

                # INGRESSNGINX_CLASSNAME
                INPUT=""
                echo "The Ingress object belongs to an Ingress Class."
                read -p "Enter the ingressClassName [$INGRESSNGINX_CLASSNAME]: " INPUT
                INPUT=${INPUT:-$INGRESSNGINX_CLASSNAME}
                echo "The NGINX Ingress will have the ingressClassName: $INPUT."
                INGRESSNGINX_CLASSNAME="$INPUT"
                echo ""

                # INGRESSNGINX_AUTH_ENABLED
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    echo "Do you want the connection to PrintServer protected with HTTP Basic Authentication?"
                    read -p "Enter true or false [$INGRESSNGINX_AUTH_ENABLED]: " INPUT
                    INPUT=$(echo ${INPUT:-$INGRESSNGINX_AUTH_ENABLED} | tr '[:upper:]' '[:lower:]')
                    if [ "$INPUT" != "true" ] && [ "$INPUT" != "false" ] && [ -n "$INPUT" ]
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                        continue
                    fi
                    echo "The NGINX Ingress will be protected with HTTP Basic Authentication: $INPUT."
                    CONFIRM="y"
                done
                INGRESSINGRESSNGINX_AUTH_ENABLED="$INPUT"
                echo ""

                if [ "$INGRESSINGRESSNGINX_AUTH_ENABLED" == "true" ]
                then
                    # INGRESSNGINX_AUTH_USERNAME
                    INPUT=""
                    echo "The HTTP Basic Authentication requires credentials."
                    read -p "Enter the username [$INGRESSNGINX_AUTH_USERNAME]: " INPUT
                    INPUT=${INPUT:-$INGRESSNGINX_AUTH_USERNAME}
                    INGRESSNGINX_AUTH_USERNAME="$INPUT"

                    # INGRESSNGINX_AUTH_PASSWORD
                    CONFIRM="n"
                    while [ "$CONFIRM" != "y" ]; do
                        INPUT=""
                        INPUT2=""
                        echo -n "Enter the password: "
                        INPUT=$(input_passwd)
                        echo ""
                        echo -n "Retype the password: "
                        INPUT2=$(input_passwd)
                        echo ""
                        if [ -z "$INPUT" ] || [ -z "$INPUT2" ] || [ "$INPUT" != "$INPUT2" ]
                        then
                            echo -e "\r\e[1A\e[K\r\e[1A\e[K"
                            echo "Passwords must be ${RED}non-empty and identical$NORMAL."
                            continue
                        fi
                        echo "HTTP Basic Authentication credential stored."
                        CONFIRM="y"
                    done
                    INGRESSNGINX_AUTH_PASSWORD="$INPUT"
                    echo ""
                fi

                # INGRESSNGINX_TLS_ENABLED
                ACCESS_PROTOCOL="http"
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    echo "Do you want the connection to PrintServer encrypted via TLS 1.3?"
                    read -p "Enter true or false [$INGRESSNGINX_TLS_ENABLED]: " INPUT
                    INPUT=$(echo ${INPUT:-$INGRESSNGINX_TLS_ENABLED} | tr '[:upper:]' '[:lower:]')
                    if [ "$INPUT" != "true" ] && [ "$INPUT" != "false" ] && [ -n "$INPUT" ]
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                        continue
                    fi
                    echo "The connection to PrintServer will be encrypted: $INPUT."
                    CONFIRM="y"
                done
                INGRESSNGINX_TLS_ENABLED="$INPUT"

                echo ""

                if [ "$INGRESSNGINX_TLS_ENABLED" == "true" ]
                then
                    ACCESS_PROTOCOL="https"

                    # INGRESSNGINX_TLS_SECRETNAME
                    INPUT=""
                    echo "The TLS certificate is stored in a Secret object."
                    read -p "Enter the name for that object [$INGRESSNGINX_TLS_SECRETNAME]: " INPUT
                    INPUT=${INPUT:-$INGRESSNGINX_TLS_SECRETNAME}
                    echo "The TLS certificate will be stored in the Secret: $INPUT."
                    INGRESSNGINX_TLS_SECRETNAME="$INPUT"
                    echo ""

                    # FILEPATHS_TLSCERT
                    CONFIRM="n"
                    while [ "$CONFIRM" != "y" ]; do
                        INPUT=""
                        echo "The TLS requires a certificate file."
                        read -p "Enter the original filename including absolute path [$FILEPATHS_TLSCERT]: " INPUT
                        INPUT="${INPUT:-$FILEPATHS_TLSCERT}"
                        if ! [ -f "$INPUT" ] || ! $(file "$INPUT" | grep 'PEM certificate' > /dev/null)
                        then
                            echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                            echo "The file $RED$INPUT$NORMAL is not a valid TLS certificate."
                            continue
                        fi
                        echo "PrintServer will use the certificate: $INPUT."
                        CONFIRM="y"
                    done
                    FILEPATHS_TLSCERT="$INPUT"
                    echo ""

                    # FILEPATHS_TLSKEY
                    CONFIRM="n"
                    while [ "$CONFIRM" != "y" ]; do
                        INPUT=""
                        echo "The TLS requires a decrypted key file."
                        read -p "Enter the original filename including absolute path [$FILEPATHS_TLSKEY]: " INPUT
                        INPUT="${INPUT:-$FILEPATHS_TLSKEY}"
                        if ! [ -f "$INPUT" ] || ! $(file "$INPUT" | grep 'ASCII text' > /dev/null) || ! $( cat "$INPUT" | grep 'END PRIVATE KEY' > /dev/null)
                        then
                            echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                            echo "The file $RED$INPUT$NORMAL is not a valid TLS decrypted key."
                            continue
                        fi
                        echo "PrintServer will use the key file: $INPUT."
                        CONFIRM="y"
                    done
                    FILEPATHS_TLSKEY="$INPUT"
                    echo ""
                fi

                # PRINTSERVER_PULLPOLICY
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    echo "When should Docker pull the PrintServer image from the Docker Hub?"
                    read -p "Enter ${BOLD}A$NORMAL for Always, ${BOLD}I$NORMAL for IfNotPresent, ${BOLD}N$NORMAL for Never  [$PRINTSERVER_PULLPOLICY]: " INPUT
                    INPUT=$(echo ${INPUT:-$PRINTSERVER_PULLPOLICY} | cut -c1 | tr '[:upper:]' '[:lower:]')
                    if [ "$INPUT" != "a" ] && [ "$INPUT" != "i" ] && [ "$INPUT" != "n" ]
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                        continue
                    fi
                    case $INPUT in
                        "i")
                            INPUT="IfNotPresent"
                        ;;
                        "n")
                            INPUT="Never"
                        ;;
                        *)
                            INPUT="Always"
                        ;;
                    esac
                    echo "The PrintServer image will be retrieved: $INPUT."
                    CONFIRM="y"
                done
                PRINTSERVER_PULLPOLICY="$INPUT"
                echo ""

                # PRINTSERVER_DOCKERTAG
                INPUT=""
                echo "Specify the PrintServer variant to install."
                read -p "Enter the Docker Tag [$PRINTSERVER_DOCKERTAG]: " INPUT
                INPUT=${INPUT:-$PRINTSERVER_DOCKERTAG}
                echo "The PrintServer with following tag will be installed: $INPUT."
                PRINTSERVER_DOCKERTAG="$INPUT"
                echo ""

                # PRINTSERVER_NUMBEROFINSTANCES
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    echo "The PrintServer (Pods) can be scaled."
                    read -p "Enter the number of PrintServer pod instances (1 to 9) [$PRINTSERVER_NUMBEROFINSTANCES]: " INPUT
                    INPUT=${INPUT:-$PRINTSERVER_NUMBEROFINSTANCES}
                    if [ "$INPUT" != $(echo "$INPUT" | cut -c1) ] || [ -z "${INPUT##*[!1-9]*}" ] 
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                        continue
                    fi
                    echo "Number of instances for the PrintServer will be: $INPUT."
                    CONFIRM="y"
                done
                PRINTSERVER_NUMBEROFINSTANCES="$INPUT"
                echo ""

                # PRINTSERVER_LICENSE
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    echo "The PrintServer requires a license file."
                    read -p "Enter the original filename including absolute path [$FILEPATHS_LICENSE]: " INPUT
                    INPUT=${INPUT:-$FILEPATHS_LICENSE}
                    if ! [ -f "$INPUT" ] || ! $(file "$INPUT" | grep ': data' > /dev/null)
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                        echo "The file $RED$INPUT$NORMAL is not a valid license file."
                        continue
                    fi
                    echo "PrintServer will use the license: $INPUT."
                    CONFIRM="y"
                done
                FILEPATHS_LICENSE="$INPUT"
                echo ""

                # PRINTSERVER_INEWSCONF
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    echo "The PrintServer requires an iNEWS configuration xml file."
                    read -p "Enter the original filename including absolute path [$FILEPATHS_INEWSCONF]: " INPUT
                    INPUT=${INPUT:-$FILEPATHS_INEWSCONF}
                    if ! [ -f "$INPUT" ] || ! $(file "$INPUT" | grep 'XML' > /dev/null)
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                        echo "The file $RED$INPUT$NORMAL is not a valid xml file."
                        continue
                    fi
                    echo "PrintServer will use the iNEWS configuration:"
                    cat "$INPUT"
                    CONFIRM="y"
                done
                FILEPATHS_INEWSCONF="$INPUT"
                echo ""

                # PRINTSERVERINIT_PULLPOLICY
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    echo "When should Docker pull the PrintServer-Init image from the Docker Hub?"
                    read -p "Enter ${BOLD}A$NORMAL for Always, ${BOLD}I$NORMAL for IfNotPresent, ${BOLD}N$NORMAL for Never  [$PRINTSERVERINIT_PULLPOLICY]: " INPUT
                    INPUT=$(echo ${INPUT:-$PRINTSERVERINIT_PULLPOLICY} | cut -c1 | tr '[:upper:]' '[:lower:]')
                    if [ "$INPUT" != "a" ] && [ "$INPUT" != "i" ] && [ "$INPUT" != "n" ]
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                        continue
                    fi
                    case $INPUT in
                        "i")
                            INPUT="IfNotPresent"
                        ;;
                        "n")
                            INPUT="Never"
                        ;;
                        *)
                            INPUT="Always"
                        ;;
                    esac
                    echo "The PrintServer image will be retrieved: $INPUT."
                    CONFIRM="y"
                done
                PRINTSERVERINIT_PULLPOLICY="$INPUT"
                echo ""

                # PRINTSERVERINIT_DOCKERTAG
                INPUT=""
                echo "Specify the PrintServer variant to install."
                read -p "Enter the Docker Tag [$PRINTSERVERINIT_DOCKERTAG]: " INPUT
                INPUT=${INPUT:-$PRINTSERVERINIT_DOCKERTAG}
                echo "The PrintServer with following tag will be installed: $INPUT."
                PRINTSERVERINIT_DOCKERTAG="$INPUT"
                echo ""

                # PRINTSTYLESOURCE_CONNECTIONTYPE
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    echo "How shall the PrintServer retrieve its printstyle files during startup?"
                    read -p "Enter ${BOLD}s$NORMAL for scp or ${BOLD}g$NORMAL for git [$PRINTSTYLESOURCE_CONNECTIONTYPE]: " INPUT
                    INPUT=$(echo ${INPUT:-$PRINTSTYLESOURCE_CONNECTIONTYPE} | cut -c1 | tr '[:upper:]' '[:lower:]')
                    if [ "$INPUT" != "s" ] && [ "$INPUT" != "g" ]
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                        continue
                    fi
                    if [ "$INPUT" == "g" ]
                    then
                        INPUT="git"
                    else
                        INPUT="scp"
                    fi
                    echo "The PrintServer will retrieve the stylesheet files via: $INPUT."
                    CONFIRM="y"
                done
                PRINTSTYLESOURCE_CONNECTIONTYPE="$INPUT"
                echo ""

                # PRINTSTYLESOURCE_HOST
                INPUT=""
                echo "What is the server to retrieve the printstyles from?"
                read -p "Enter the IP address, hostname, or FQDN of the printstyle source [$PRINTSTYLESOURCE_HOST]: " INPUT
                INPUT=${INPUT:-$PRINTSTYLESOURCE_HOST}
                PRINTSTYLESOURCE_HOST="$INPUT"

                # PRINTSTYLESOURCE_PATH
                INPUT=""
                read -p "Enter the path of the printstyle source [$PRINTSTYLESOURCE_PATH]: " INPUT
                INPUT=${INPUT:-$PRINTSTYLESOURCE_PATH}
                PRINTSTYLESOURCE_PATH="$INPUT"

                echo "The source to read the printstyles from is:"
                if [ "$PRINTSTYLESOURCE_CONNECTIONTYPE" == "git" ]
                then
                    echo "git clone https://$PRINTSTYLESOURCE_HOST/$PRINTSTYLESOURCE_PATH"
                else
                    echo "scp -r $PRINTSTYLESOURCE_HOST:$PRINTSTYLESOURCE_PATH"
                fi
                echo ""

                # PRINTSTYLESOURCE_USERNAME
                INPUT=""
                echo "What are the credentials to read the printstyle files?"
                read -p "Enter the username [$PRINTSTYLESOURCE_USERNAME]: " INPUT
                INPUT=${INPUT:-$PRINTSTYLESOURCE_USERNAME}
                PRINTSTYLESOURCE_USERNAME="$INPUT"

                # PRINTSTYLESOURCE_PASSWORD
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    INPUT2=""
                    echo -n "Enter the password: "
                    INPUT=$(input_passwd)
                    echo ""
                    echo -n "Retype the password: "
                    INPUT2=$(input_passwd)
                    echo ""
                    if [ -z "$INPUT" ] || [ -z "$INPUT2" ] || [ "$INPUT" != "$INPUT2" ]
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K"
                        echo "Passwords must be ${RED}non-empty and identical$NORMAL."
                        continue
                    fi
                    echo "Credential for reading printstyle files stored."
                    CONFIRM="y"
                done
                PRINTSTYLESOURCE_PASSWORD="$INPUT"
                echo ""

                # CLUSTER_FQDN
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    echo "What is the domain name to reach the PrintServer web page and REST API? "
                    read -p "Enter the cluster FQDN [$CLUSTER_FQDN]: " INPUT
                    INPUT=${INPUT:-$CLUSTER_FQDN}
                    if ! $(echo "$INPUT" | grep -P '^(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)$' > /dev/null)
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                        echo "$RED$INPUT$NORMAL is not a valid FQDN."
                        continue
                    fi
                    echo "The PrintServer will be reachable on the URL: $ACCESS_PROTOCOL://$INPUT/printserver."
                    CONFIRM="y"
                done
                CLUSTER_FQDN="$INPUT"
                echo ""

                # SERVICE_PORT
                CONFIRM="n"
                while [ "$CONFIRM" != "y" ]; do
                    INPUT=""
                    echo "The PrintServer Service object utilizes a network port (internal in Kubernetes)"
                    read -p "Enter the port number (1024 - 65535) [$SERVICE_PORT]: " INPUT
                    INPUT=${INPUT:-$SERVICE_PORT}
                    if ! $(echo "$INPUT" | grep -P '^[0-9]*$' > /dev/null) || [ $INPUT -lt 1024 ] || [ $INPUT -gt 65535 ] 
                    then
                        echo -e "\r\e[1A\e[K\r\e[1A\e[K\r\e[1A\e[K"
                        echo "$RED$INPUT$NORMAL is not a valid port number."
                        continue
                    fi
                    echo "Number of instances for the PrintServer will be: $INPUT."
                    CONFIRM="y"
                done
                SERVICE_PORT="$INPUT"
                echo ""

                echo "You entered all data. They will be now processed and stored in $VALUES_FILE."
                echo "To review all entered data, call ${BOLD}sudo printserverctl review$NORMAL."
                echo "To see all artifact declarations that will be created for the PrintServer, call ${BOLD}sudo printserverctl dryrun$NORMAL."
                echo "To install or update the PrintServer with those data, call ${BOLD}sudo printserverctl deploy$NORMAL."

                # copy all files to /etc/hmedia/printserver/ and process them

                if [ "$INGRESSNGINX_TLS_ENABLED" == "true" ]
                then
                    cp "$FILEPATHS_TLSCERT" "$TLS_CERT_FILE"
                    INGRESSNGINX_TLS_CERTIFICATE=$(base64 -w 0 "$TLS_CERT_FILE")
                    cp "$FILEPATHS_TLSKEY" "$TLS_KEY_FILE"
                    INGRESSNGINX_TLS_KEY=$(base64 -w 0 "$TLS_KEY_FILE")
                fi

                cp "$FILEPATHS_INEWSCONF" "$INEWSCONF_FILE"
                PRINTSERVER_INEWSCONF=$(base64 -w 0 "$INEWSCONF_FILE")

                if [ -s "$FILEPATHS_LICENSE" ]
                then
                    cp "$FILEPATHS_LICENSE" "$LICENSE_FILE"
                    PRINTSERVER_LICENSE=$(base64 -w 0 "$LICENSE_FILE")
                fi

                # save parameters in /etc/hmedia/printserver/values.yaml
VALUES_TEMPLATE="
ingressNginx:
  install: $INGRESSNGINX_INSTALL
  className: $INGRESSNGINX_CLASSNAME
  auth:
    enabled: $INGRESSNGINX_AUTH_ENABLED
    username: $INGRESSNGINX_AUTH_USERNAME
    password: $INGRESSNGINX_AUTH_PASSWORD
  tls:
    enabled: $INGRESSNGINX_TLS_ENABLED
    secretName: $INGRESSNGINX_TLS_SECRETNAME
    certificate: $INGRESSNGINX_TLS_CERTIFICATE
    key: $INGRESSNGINX_TLS_KEY

printserver:
  pullPolicy: $PRINTSERVER_PULLPOLICY
  dockerTag: $PRINTSERVER_DOCKERTAG
  numberOfInstances: $PRINTSERVER_NUMBEROFINSTANCES
  license: $PRINTSERVER_LICENSE
  inewsconf: $PRINTSERVER_INEWSCONF 

printserverInit:
  pullPolicy: $PRINTSERVERINIT_PULLPOLICY
  dockerTag: $PRINTSERVERINIT_DOCKERTAG

printstyleSource:
  connectionType: $PRINTSTYLESOURCE_CONNECTIONTYPE
  username: $PRINTSTYLESOURCE_USERNAME
  password: $PRINTSTYLESOURCE_PASSWORD
  host: $PRINTSTYLESOURCE_HOST
  path: $PRINTSTYLESOURCE_PATH

cluster:
  fqdn: $CLUSTER_FQDN

service:
  port: $SERVICE_PORT

filePaths:
  tlscert: $FILEPATHS_TLSCERT
  tlskey: $FILEPATHS_TLSKEY
  inewsconf: $FILEPATHS_INEWSCONF
  license: $FILEPATHS_LICENSE

deployment:
  name: $DEPLOYMENT_NAME
  namespace: $DEPLOYMENT_NAMESPACE
"

                echo "$VALUES_TEMPLATE" > "$VALUES_FILE"
                exit 0
            ;;
        esac
        ;;

    "clean")
        # Clean prepared files
        CONFIRM="n"
        echo "This step will clear all data from a previous preparation."
        echo "All files in the directory $SETTINGS_DIR will be deleted!"

        read -p "Continue? (y/N): " CONFIRM
        CONFIRM=$(echo ${CONFIRM:-n} | cut -c1 | tr '[:upper:]' '[:lower:]')

        if [ "$CONFIRM" != "y" ]
        then
            exit 0
        fi
        echo ""

        cd "$SETTINGS_DIR"
        if [ "$(pwd)" != $SETTINGS_DIR ]
        then
            echo "Internal error: Couldn't enter the directory $SETTINGS_DIR."
            exit 1
        else
            rm -rf *
            echo "All preparation data removed."
            exit 0
        fi
    ;;

    "review")
        # Show preparation results
        validate_preparation

DATA="
The current parameters for the PrintServer setup are:

${BOLD}deployment.name$NORMAL
    Name of the Helm object: $BOLD$DEPLOYMENT_NAME$NORMAL$NORMAL

${BOLD}deployment.namespace$NORMAL
    Kubernetes Namespace for the printserver: $BOLD$DEPLOYMENT_NAMESPACE$NORMAL

${BOLD}ingressNginx.install$NORMAL
    The NGINX Ingress chart will be installed: $BOLD$INGRESSNGINX_INSTALL$NORMAL

${BOLD}ingressNginx.className$NORMAL
    Name of the nginx ingress class: $BOLD$INGRESSNGINX_CLASSNAME$NORMAL$NORMAL

${BOLD}ingressNginx.auth.enabled$NORMAL
    The NGINX Ingress will be protected with HTTP Basic Authentication: $BOLD$INGRESSNGINX_AUTH_ENABLED$NORMAL

${BOLD}ingressNginx.auth.username$NORMAL
    The username for HTTP Basic Authentication: $BOLD$INGRESSNGINX_AUTH_USERNAME$NORMAL

${BOLD}ingressNginx.auth.password$NORMAL
    Password for HTTP Basic Authentication (base64): $BOLD$(echo -n $INGRESSNGINX_AUTH_PASSWORD | base64)$NORMAL

${BOLD}ingressNginx.tls.enabled$NORMAL
    The connection to PrintServer will be encrypted with TLS 1.3: $BOLD$INGRESSNGINX_TLS_ENABLED$NORMAL

${BOLD}ingressNginx.tls.secretName$NORMAL
    The TLS certificate will be stored in the Secret: $BOLD$INGRESSNGINX_TLS_SECRETNAME$NORMAL

${BOLD}TLS certificate$NORMAL
    The original TLS certification file was: $BOLD$FILEPATHS_TLSCERT$NORMAL
    For processing it was copied to: $BOLD$TLS_CERT_FILE$NORMAL

${BOLD}TLS key$NORMAL
    The original TLS decrypted key file was: $BOLD$FILEPATHS_TLSKEY$NORMAL
    For processing it was copied to: $BOLD$TLS_KEY_FILE$NORMAL

${BOLD}printserver.pullPolicy$NORMAL
    The PrintServer image will be retrievedfrom Docker Hub: $BOLD$PRINTSERVER_PULLPOLICY$NORMAL

${BOLD}printserver.dockerTag$NORMAL
    The PrintServer with following tag will be installed: $BOLD$PRINTSERVER_DOCKERTAG$NORMAL

${BOLD}printserver.numberOfInstances$NORMAL
    Number of instances for the PrintServer (pod): $BOLD$PRINTSERVER_NUMBEROFINSTANCES$NORMAL

${BOLD}iNEWS config file$NORMAL
    The original inewsconf file was: $BOLD$FILEPATHS_INEWSCONF$NORMAL
    For processing it was copied to: $BOLD$INEWSCONF_FILE$NORMAL
    The content of that file is:
$BOLD$(cat $INEWSCONF_FILE)$NORMAL

${BOLD}PrintServer license file$NORMAL
    The original license file was: $BOLD$FILEPATHS_LICENSE$NORMAL
    For processing it was copied to: $BOLD$LICENSE_FILE$NORMAL

${BOLD}printserverInit.pullPolicy$NORMAL
    The PrintServer-Init image will be retrievedfrom Docker Hub: $BOLD$PRINTSERVERINIT_PULLPOLICY$NORMAL

${BOLD}printserverInit.dockerTag$NORMAL
    The PrintServer-Init with following tag will be installed: $BOLD$PRINTSERVERINIT_DOCKERTAG$NORMAL

${BOLD}printstyleSource.connectionType$NORMAL
    The PrintServer will retrieve the stylesheet files via: $BOLD$PRINTSTYLESOURCE_CONNECTIONTYPE$NORMAL

${BOLD}printstyleSource.host$NORMAL
    The server hosting the printstyle files: $BOLD$PRINTSTYLESOURCE_HOST$NORMAL

${BOLD}printstyleSource.path$NORMAL
    The path to read the printstyles from: $BOLD$PRINTSTYLESOURCE_PATH$NORMAL

${BOLD}printstyleSource.username$NORMAL
    The username for printstyles retrieval: $BOLD$PRINTSTYLESOURCE_USERNAME$NORMAL

${BOLD}printstyleSource.password$NORMAL
    The password for printstyles retrieval (base64): $BOLD$(echo -n $PRINTSTYLESOURCE_PASSWORD | base64)$NORMAL

${BOLD}cluster.fqdn$NORMAL
    The Kubernetes cluster name: $CLUSTER_FQDN
    The PrintServer will be reachable on the URL: $BOLD$ACCESS_PROTOCOL://$CLUSTER_FQDN/printserver$NORMAL

${BOLD}service.port$NORMAL
    The network port for the Service object (connection from the ingress to the service): $BOLD$SERVICE_PORT$NORMAL

To modify those data, call ${BOLD}sudo printserverctl prepare$NORMAL.
To entirely delete custom data, call ${BOLD}sudo printserverctl clean$NORMAL.
To see all artifact declarations that will be created for the PrintServer, call ${BOLD}sudo printserverctl dryrun$NORMAL.
To install or update the PrintServer with those data, call ${BOLD}sudo printserverctl deploy$NORMAL.

Quit this view with ${BOLD}q$NORMAL."

    echo "$DATA" | $LESS_CMD -r
    ;;

    "dryrun")
        # Dry Run
        # check for preparation
        validate_preparation
        # prepare Helm repo
        prepare_repo

        $HELM_CMD template $DEPLOYMENT_NAME $HELM_REPO_NAME/printserver --namespace=$DEPLOYMENT_NAMESPACE --create-namespace --dry-run --debug --values "$VALUES_FILE"
        ;;

    "deploy")
        # Install
        # check for preparation
        validate_preparation
        # check for installation
        get_deploy_status
        # prepare Helm repo
        prepare_repo

        if [ "$IS_INSTALLED" == "false" ]
        then
            # Not yet installed - so, go ahead and install!
            $HELM_CMD install $DEPLOYMENT_NAME $HELM_REPO_NAME/printserver --namespace=$DEPLOYMENT_NAMESPACE --create-namespace --values "$VALUES_FILE"
            exit $?
        else
            if [ "$CURRENT_DEPLOY_NAME" != "$DEPLOYMENT_NAME" ] || [ "$CURRENT_NAMESPACE" != "$DEPLOYMENT_NAMESPACE" ]
            then
                [ "$CURRENT_DEPLOY_NAME" != "$DEPLOYMENT_NAME" ] && HIGHLIGHT_NAME=$RED || HIGHLIGHT_NAME=$BOLD
                [ "$CURRENT_NAMESPACE" != "$DEPLOYMENT_NAMESPACE" ] && HIGHLIGHT_NAMESPACE=$RED || HIGHLIGHT_NAMESPACE=$BOLD

                # Already installed but deployment name and namespace don't match with preparation data!
                echo "There is already an installation of the PrintServer with name $HIGHLIGHT_NAME$CURRENT_DEPLOY_NAME$NORMAL in namespace $HIGHLIGHT_NAMESPACE$CURRENT_NAMESPACE$NORMAL!"
                echo "The preparation data point to an installation with name $HIGHLIGHT_NAME$DEPLOYMENT_NAME$NORMAL in namespace $HIGHLIGHT_NAMESPACE$DEPLOYMENT_NAMESPACE$NORMAL!"
                echo "Two installations of the PrintServer in the same Kubernetes cluster are NOT supported!"
                echo ""

                CONFIRM=""
                read -p "Do you want to correct the preparation data and modify the existing installation instead? (y/N): " CONFIRM
                CONFIRM=$(echo ${CONFIRM:-n} | cut -c1 | tr '[:upper:]' '[:lower:]')
                if [ "$CONFIRM" != "y" ]
                then
                    echo "Nothing changed!"
                    exit 0
                fi

                # Update name and namespace
                DEPLOYMENT_NAME="$CURRENT_DEPLOY_NAME"
                DEPLOYMENT_NAMESPACE="$CURRENT_NAMESPACE"

                # save parameters in /etc/hmedia/printserver/values.yaml
VALUES_TEMPLATE="
ingressNginx:
  install: $INGRESSNGINX_INSTALL
  className: $INGRESSNGINX_CLASSNAME
  auth:
    enabled: $INGRESSNGINX_AUTH_ENABLED
    username: $INGRESSNGINX_AUTH_USERNAME
    password: $INGRESSNGINX_AUTH_PASSWORD
  tls:
    enabled: $INGRESSNGINX_TLS_ENABLED
    secretName: $INGRESSNGINX_TLS_SECRETNAME
    certificate: $INGRESSNGINX_TLS_CERTIFICATE
    key: $INGRESSNGINX_TLS_KEY

printserver:
  pullPolicy: $PRINTSERVER_PULLPOLICY
  dockerTag: $PRINTSERVER_DOCKERTAG
  numberOfInstances: $PRINTSERVER_NUMBEROFINSTANCES
  license: $PRINTSERVER_LICENSE
  inewsconf: $PRINTSERVER_INEWSCONF 

printserverInit:
  pullPolicy: $PRINTSERVERINIT_PULLPOLICY
  dockerTag: $PRINTSERVERINIT_DOCKERTAG

printstyleSource:
  connectionType: $PRINTSTYLESOURCE_CONNECTIONTYPE
  username: $PRINTSTYLESOURCE_USERNAME
  password: $PRINTSTYLESOURCE_PASSWORD
  host: $PRINTSTYLESOURCE_HOST
  path: $PRINTSTYLESOURCE_PATH

cluster:
  fqdn: $CLUSTER_FQDN

service:
  port: $SERVICE_PORT

filePaths:
  tlscert: $FILEPATHS_TLSCERT
  tlskey: $FILEPATHS_TLSKEY
  inewsconf: $FILEPATHS_INEWSCONF
  license: $FILEPATHS_LICENSE

deployment:
  name: $DEPLOYMENT_NAME
  namespace: $DEPLOYMENT_NAMESPACE
"
                echo "$VALUES_TEMPLATE" > "$VALUES_FILE"

            fi

            # Already installed and (now) matching name and namepsace - so, go ahead and update the objects
            $HELM_CMD upgrade $DEPLOYMENT_NAME $HELM_REPO_NAME/printserver --namespace=$DEPLOYMENT_NAMESPACE --create-namespace --values "$VALUES_FILE"
            exit $?
        fi
        ;;

    "remove")
        # Uninstall

        # check for installation
        get_deploy_status

        if [ "$IS_INSTALLED" == "false" ]
        then
            echo "Can't find a Helm installation of the PrintServer in this Kubernetes cluster."
            exit 1
        fi

        # Confirmation question
        CONFIRM=""
        read -p "Do you really want to remove the installation $CURRENT_DEPLOY_NAME from namespace $CURRENT_NAMESPACE? (y/N): " CONFIRM
        CONFIRM=$(echo ${CONFIRM:-n} | cut -c1 | tr '[:upper:]' '[:lower:]')
        echo ""
        if [ "$CONFIRM" != "y" ]
        then
            echo "Nothing done!"
            exit 0
        fi

        # Want to remove namespace
        REMOVE_NAMESPACE=n
        if [ "$CURRENT_NAMESPACE" != "default" ]
        then
            read -p "Shall the namespace $CURRENT_NAMESPACE also be removed? (y/N): " CONFIRM
            REMOVE_NAMESPACE=$(echo ${CONFIRM:-n} | cut -c1 | tr '[:upper:]' '[:lower:]')
        else
            REMOVE_NAMESPACE=n
            echo "The installation $CURRENT_DEPLOY_NAME is installed in namespace default. This namespace will not be removed."
            echo ""
        fi

        # Delete PrintServer
        $HELM_CMD delete $CURRENT_DEPLOY_NAME -n $CURRENT_NAMESPACE
        if [ $? -eq 0 ]
        then
            echo "The installation $CURRENT_DEPLOY_NAME has been removed from namespace $CURRENT_NAMESPACE."
        else
            echo "The installation $CURRENT_DEPLOY_NAME in namespace $CURRENT_NAMESPACE couldn't be removed successfully."
        fi

        # Conditionally delete namespace
        if [ "$REMOVE_NAMESPACE" == "y" ]
        then
            kubectl delete ns $CURRENT_NAMESPACE
            echo "The namespace $CURRENT_NAMESPACE has been removed."
        else
            echo "The namespace $CURRENT_NAMESPACE is still available in Kubernetes." 
        fi
        exit 0
        ;;

    "version")
        # Versions for this script, printserver, chart, kubernetes, helm 
        APP_VERSION=$($HELM_CMD ls --all-namespaces | grep "printserver-" | cut -f7)
        if [ -z "$APP_VERSION" ]
        then
            APP_VERSION="not installed"
        fi
        CHART_VERSION=$($HELM_CMD ls --all-namespaces | grep "printserver-" | cut -f6)
        if [ -z "$CHART_VERSION" ]
        then
            CHART_VERSION="not installed"
        fi
        HELM_VERSION=$($HELM_CMD version --short 2>/dev/null)
        K8S_VERSION=$($KUBECTL_CMD version | grep "Server" | cut -f3 -d" ")

        echo "printserverctl                $THIS_VERSION"  
        echo "PrintServer Application       $APP_VERSION"  
        echo "PrintServer Helm Chart        $CHART_VERSION"  
        echo "Kubernetes Server Software    $K8S_VERSION"  
        echo "Helm Software                 $HELM_VERSION"  
        exit 0

    ;;

    *)
        # Print Usage
        echo "$USAGE"
        ;;

esac

exit 0