#!/bin/bash

#---------------------------------------------
# IBM Confidential
#-
#- SFA Source Materials
#-
#- (C) Copyright IBM Corp. 2012, 2015
#
#- The source code for this program is not published or otherwise
#- divested of its trade secrets, irrespective of what has been
#- deposited with the U.S. Copyright Office
#
#---------------------------------------------------------------------------
#- A Helper Library, which provides a number of helper functions to facilitate 
#  functionality of UCOMMAND Setup script.
#
#- LIST of Current Functions in this HELPER 
#    1. check_and_prepare_java_home
#    2. check_php_and_java
#    3. check_status_or_exit
#    4. check_url_format
#    5. download_and_prepare_udczip
#    6. get_alias
#    7. get_and_validate_user_inputs
#    8. get_env_app
#    9. get_java_home_directory
#    10. prepare_auth_token
#    11. validate_user_login
#
#- Author: 
#- Kostadin Ivanov   kostadii@ie.ibm.com
#-----------------------------------------

# Check and Prepare JAVA_HOME
function check_and_prepare_java_home () {
    
    local RETURN_STATUS=0
    local DEFAULT_JAVA_HOME=''
    # Switch used to prevent duplicate request for input to Java Home directory
    # 0: Request Input; 1: Do not Request Input;
    local REQUEST_INPUT=0
    
    echo -e "\n[INFO] Setting JAVA_HOME..."
    
    # Get the JAVA_HOME directory
    DEFAULT_JAVA_HOME=$(get_java_home_directory 2>&1)
    
    echo -e "\n    [ACTION] Specify Java Home directory to be used in this setup:\n"
    if [ ! -z "${DEFAULT_JAVA_HOME// /}" ]; then 
        read -r -p "        * Please enter Java Home directory.    [Default: '${DEFAULT_JAVA_HOME}']: " NEW_JAVA_HOME
        REQUEST_INPUT=1
    else
        read -r -p "        * Please enter Java Home directory: " NEW_JAVA_HOME
        REQUEST_INPUT=1
    fi
    
    
    # Validate the entered NEW_JAVA_HOME or DEFAULT it to: DEFAULT_JAVA_HOME
    if [ ! -z "${DEFAULT_JAVA_HOME// /}" ] && [ -z "${NEW_JAVA_HOME// /}" ]; then
        echo -e "\n    [INFO] Defaulting Java Home to: '${DEFAULT_JAVA_HOME}'\n"
        JAVA_HOME_UCOMMAND="${DEFAULT_JAVA_HOME}"
    fi
    
    # JAVA_HOME_UCOMMAND is stil NOT Set
    # i.e. DEFAULT_JAVA_HOME was empty OR NEW_JAVA_HOME is empty or wrong
    while [ -z "${JAVA_HOME_UCOMMAND// /}" ] || [ ! -f "${JAVA_HOME_UCOMMAND}/bin/java" ]; 
    do
        # Read User input
        if [ ! -z "${NEW_JAVA_HOME// /}" ]; then
            JAVA_HOME_UCOMMAND="${NEW_JAVA_HOME}"
            # Clear the NEW_JAVA_HOME
            NEW_JAVA_HOME=''
            REQUEST_INPUT=0
        elif [ $REQUEST_INPUT -eq 0 ]; then
            echo ''
            read -r -p "        * Please enter Java Home directory: " JAVA_HOME_UCOMMAND
        fi
        
        if [ -z "${JAVA_HOME_UCOMMAND}" ]; then
            echo '          [WARNING] JAVA_HOME is REQUIRED!'
            REQUEST_INPUT=0
            
        # Validate User input
        elif [ ! -f "${JAVA_HOME_UCOMMAND}/bin/java" ]; then
            echo -e "          [WARNING] Specified JAVA_HOME: '${JAVA_HOME_UCOMMAND}' "\
                    "\n              is NOT a valid Directory OR Java executable cannot be accessed!"
            JAVA_HOME_UCOMMAND=''
            REQUEST_INPUT=0
        fi  
    done
    
    echo -e "\n[DONE]"

    # Make Sure the NEW Java Home is correct in Global: JAVA_HOME
    JAVA_HOME="${JAVA_HOME_UCOMMAND}"
    
    # Return
    return $RETUN_STATUS
}

# Check if PHP and JAVA are installed
function check_php_and_java () {
    
    local RETURN_STATUS=0

    # Check if PHP is installed
    echo -e "\n[INFO] Checking PHP..."
    PHP_CHECK=$(which php)
    if [ $? -ne 0 ]; then
      RETURN_STATUS=1
      echo -e "\n[ERROR] PHP package is REQUIRED!"
    else
      echo '       [OK]'
    fi

    # Check is JAVA is set
    echo -e "\n[INFO] Checking Java..."
    JAVA_CHECK=$(which java)
    if [ $? -ne 0 ]; then
      RETURN_STATUS=1
      echo -e "\n[ERROR] JAVA is REQUIRED!"
    else
      echo '       [OK]'
    fi

    # Return
    return $RETURN_STATUS
}


# Function to check an Exit Code, returned by another function:
#  - return 0 when OK (Status = 0) 
#  - exit with STATUS to check when Error (Status > 0)
#  - exit with Status: 1 in case when Status to Check is not provided
function check_status_or_exit () {

    # REQUIRED Parameter
    if [ -z "${1// /}" ]; then
        echo -e "\n[ERROR] Exit Code to Check is REQUIRED!"
        exit 1
    # Status: OK
    elif [ $1 -eq 0 ]; then
        return $1
    # Status: ERROR
    elif [ $1 -gt 0 ];then
        exit $1
    fi
}


# Validate if specified URL is in correct format
function check_url_format () {
    local RETURN_STATUS=0
    local URL_REGEXP='^(https:\/\/)?([a-zA-Z0-9]\.)?([a-zA-Z0-9_\-\.])+(\.{1})([a-zA-Z])+(:{1}[0-9]{1,5})?$'

    if [[ ! "${1}" =~ $URL_REGEXP ]]; then
      echo -e "\n[WARNING] Specified URL: '${1}' IS NOT in CORRECT FORMAT:"\
              "\n    Possible formats:"\
              "\n        1. www.udeploy.domain.com"\
              "\n        2. www.udeploy.domain.com:12345"\
              "\n        3. https://www.udeploy.domain.com"\
              "\n        4. https://www.udeploy.domain.com:12345"
      RETURN_STATUS=1
    fi

    # Return
    return $RETURN_STATUS
}


# Download and Unzip the udclient.zip
function download_and_prepare_udczip () {
    
    local RETURN_STATUS=0
    
    echo -e "\n[INFO] Downloading udclient from: "\
            "'${UCD_WEB_URL}/tools/udclient.zip' ...\n"
            
    curl -k "${UCD_WEB_URL}/tools/udclient.zip" -o udclient.zip
    RETURN_STATUS=$?
    if [ $RETURN_STATUS -ne 0 ]; then
        echo -e "\n[ERROR] CURL request FAILED with Exit Code: '${RETURN_STATUS}'\n"
    else
        # Prompt and continue to unzip step
        echo -e "[DONE]\n"\
                "\n[INFO] Extracting udclient.zip ...\n"
        unzip -q udclient.zip
        RETURN_STATUS=$?
        if [ $RETURN_STATUS -ne 0 ]; then
          echo -e "\n[ERROR] UNZIP command FAILED with Exit Code: '${RETURN_STATUS}'\n"
        else
          echo '[DONE]'
        fi
    fi

    # Return
    return $RETURN_STATUS
}

# Get Alias
function get_alias () {
    ALIAS=""
    SERVERS_DIR=$1
    PATTERN=" |'"
    
    while [ -z $ALIAS ]; 
    do
        echo -e "\n[ACTION] Choose a unique alias for your configuration"
        read -r -p "    * Alias (no spaces): " ALIAS
        if [[ $ALIAS =~ $PATTERN ]]; then
            ALIAS=""
            echo "    [ERROR] Please choose an alias without spaces"
        else
            SERVER_FILE="${SERVERS_DIR}/${ALIAS}.server.php"
            if [ -f $SERVER_FILE ]; then
                echo "    [WARNING] Alias '${ALIAS}' is already in use"
                echo "    Do you want to override the exising configuration?  " 
                read -r -p "    * [Y]es: Override .   [N]o: Set a different alias. [Default: 'N']: " OVERRIDE
                case "${OVERRIDE}" in
                    "y" | "Y")
                        return 0
                    ;;
                    # Default: do not override
                    *)
                        ALIAS=""
                    ;;
                esac
            fi
        fi
    done
    
    return 0
}


# Get and Valildate user inputs
function get_and_validate_user_inputs () {
    
    echo -e "\n[ACTION] Enter your UrbanCode Deploy details:"
            
    # Step1: Get and prepare UCD URL 
    while [ -z "${UCD_WEB_URL// /}" ]; 
    do
        echo ''
        read -r -p "    * uDeploy Server URL: " UCD_WEB_URL
        if [ -z "${UCD_WEB_URL// /}" ]; then
            echo '      [WARNING] uDeploy Server URL is REQUIRED!'
        else
            # Validate provided URL
            check_url_format "${UCD_WEB_URL}"
            if [ $? -ne 0 ]; then
                UCD_WEB_URL=''
            else
                # Make sure that the UCD_WEBURL is in the form: https://URL:PORT
                # Check and set https:// part
                if [[ ! "${UCD_WEB_URL}" =~ ^(https:\/\/){1} ]]; then
                    echo "      [INFO] Prepending 'https://' part to URL: '${UCD_WEB_URL}'"
                    UCD_WEB_URL="https://${UCD_WEB_URL}"
                fi
                
                # Check and add the PORT part in case if it is missing 
                if [[ ! "${UCD_WEB_URL}" =~ (:{1}[0-9]{1,5})$ ]]; then
                    # 2.4 Get Port
                    echo ''
                    read -r -p "    * Enter Port.    [Default: 8443]: " UCD_PORT
                    # Check and Default PORT
                    if [[ ( -z "${UCD_PORT// /}" ) || ( ! "${UCD_PORT}" =~ ^([0-9]{1,5})$ ) ]]; then
                        echo "      [INFO] Defaulting PORT to: '8443' for URL: '${UCD_WEB_URL}'"
                        UCD_PORT="8443"
                    fi
                    # Add the PORT
                    echo "      [INFO] Appending PORT: '${UCD_PORT}' to URL: '${UCD_WEB_URL}'"
                    UCD_WEB_URL="${UCD_WEB_URL}:${UCD_PORT}"
                fi
            fi
        fi 
    done
    # Step2: Get UCD Username
    while [ -z "${UCD_USER// /}" ]; 
    do
        echo ''
        read -r -p "    * uDeploy Username: " UCD_USER
        if [ -z "${UCD_USER}" ]; then
            echo '      [WARNING] uDeploy Username is REQUIRED!'
        fi 
    done
    # Step3: Get UCD Password
    while [ -z "${UCD_PASSWORD// /}" ]; 
    do
        echo ''
        read -r -s -p "    * uDeploy Password: " UCD_PASSWORD
        if [ -z "${UCD_PASSWORD// /}" ]; then
            echo -e "\n      [WARNING] uDeploy PASSWORD is REQUIRED"
        else
            echo ''
        fi 
    done

    # Return
    return 0
}

# Get environment and application
function get_env_app () {
    ENVIRONMENT=""
    APPLICATION=""
    
    echo -e "\n[INFO] Provide default values for Application and Environment"
    read -r -p "    * Application name: " APPLICATION
    read -r -p "    * Environment name: " ENVIRONMENT
    
    return 0
}

# Function to get the path to JAVA HOME directory
# which holds the JAVA executable
# WILL: check the GLOBAL: JAVA_HOME and return its value
#       if JAVA_HOME is missing will use $(which java)
#       and find its real HOME directory (which holds the /bin/java executable
# Returns: 
# - on SUCCESS: the Path to the JAVA HOME directory
# - Any other case: EMPTY_STRING
function get_java_home_directory () {
    
    local RETURN_STATUS=0
    local JAVA_HOME_DIR=''
    local JAVA_PATH=''
    local LINK_LOCATION=''
    
    
    # If JAVA_HOME is setup
    if [ ! -z "${JAVA_HOME}" ];then
        JAVA_HOME_DIR="${JAVA_HOME}"
        
    # If Java is installed -=> gets its Home directory
    else
        # Check if JAVA is installed
        #JAVA_PATH='/usr/lib/j2sdk1.6-ibm' 
        JAVA_PATH=$(which java 2>&1)
        if [ $? -eq 0 ]; then
            # Get the REAL Path to JAVA Home directory
            while [ -z "${JAVA_HOME_DIR// /}" ];
            do
                #echo -e "\n[debug] processing JAVA_PATH: '${JAVA_PATH}'"
                # If JAVA_PATH is a link
                if [ -L "${JAVA_PATH}" ]; then
                    # Get the Link to which Java Path is pointing To 
                    LINK_LOCATION=$(ls -ld "${JAVA_PATH}" | awk '{print$NF}')
                    #echo -e "\n[debug] JAVA_PATH: '${JAVA_PATH}' is link to: '${LINK_LOCATION}'"
                    # If pointed location holds the /bin/java executable
                    if [[ "${LINK_LOCATION}" = *"/bin/java" ]]; then
                        # Set JAVA_HOME_DIR
                        JAVA_HOME_DIR="${LINK_LOCATION//'/bin/java'/}"
                    else
                        # Update JAVA_PATH and continue search
                        JAVA_PATH="${LINK_LOCATION}"
                    fi
                    
                # If JAVA_PATH is a File ending with: /bin/java
                elif [ -f "${JAVA_PATH}" ] && [[ "${JAVA_PATH}" = *"/bin/java" ]]; then
                    #echo -e "\n[debug] JAVA_PATH: '${JAVA_PATH}' is file"
                    # Get the Parent directory of the Java Path
                    #echo -e "\n[debug] dirname of JAVA_PATH: '${JAVA_PATH}' is: '$(dirname $JAVA_PATH)'"
                    JAVA_HOME_DIR="${JAVA_PATH//'/bin/java'/}"
                
                # If JAVA_PATH is set to Directory which holds the '/bin/java' executable
                elif [ -d "${JAVA_PATH}" ] && [ -f "${JAVA_PATH}/bin/java" ]; then
                    #echo -e "\n[debug] we are done! \n        JAVA_PATH: '${JAVA_PATH}' is correct path to JAVA_HOME"
                    JAVA_HOME_DIR="${JAVA_PATH}"
                    
                else
                    # We should not get to this point - break the loop to return
                    echo -e "\n[debug] JAVA_PATH: '${JAVA_PATH}' was NOT correct JAVA location!"
                    RETURN_STATUS=1
                    break
                fi
            done
        fi
        # ELSE Java was not installed. 
        # THIS should be caught by function: check_php_and_java
        # return EMPTY_STRING
    fi
    
    # Return
    echo "${JAVA_HOME_DIR}"
    return $RETURN_STATUS
}


# Prepare: Get Existent or Generate NEW AUTHTOKEN
function prepare_auth_token () {
    
    # Token Format is: 5 groups of AlphaNumeric characters joined by '-'
    # 1st group      : 8 AlphaNumeric characters
    # 2nd - 4th group: 4 AlphaNumeric characters
    # 5th group      : 12 AlphaNumeric characters
    local TOKEN_FORMAT='^([a-z0-9]{8}-){1}(([a-z0-9]{4})-){3}([a-z0-9]{12}){1}$'
    
    # Get System type
    local OS_SYS=$(uname)
    
    # IP v4 Format
    local IPV4_FORMAT='^([0-9]{1,3}\.){3}([0-9]{1,3}){1}$'
    # IP v6 Format
    local IPV6_FORMAT='^([0-9a-fA-F]{1,4}\:){7}([0-9a-fA-F]{1,4}){1}$'
    
    # Set Current System IP Address
    local IP=$(host $(hostname) | sed -n 's/.*\(\([0-9]\{1,3\}\.\)\{3\}\([0-9]\{1,3\}\)\{1\}\).*/\1/p')
    
    # Date format: yyyy-dd-mm
    local DATE_FORMAT='^([0-9]{4}){1}-([0-9]{2}){1}-([0-9]{2}){1}$'
    
    # Set Default Expiry Date - 1 month from now
    if [ "${OS_SYS}" = 'AIX' ]; then
        # AIX workaround: Get Date (744 hours or 31 days from now)
        local DEFAULT_TOKEN_EXP_DATE=$(TZ=GMT-744; date +"%Y-%m-%d";)
    else
        local DEFAULT_TOKEN_EXP_DATE=$(date --date='next month' +"%Y-%m-%d")
    fi 
    
    # Prepare uCommand Authentication Token and setup uCommand Config
    echo -e "\n[ACTION] Generate an 'authtoken' for uCommand?"
            
    # Turns on and off the: '[N]o, I use an existing token...' message
    PROMPT_USAGE=0
    while [ -z "${TOKEN// /}" ];
    do
        # Get Action from User whether to generate or use existent Token
        if [ $PROMPT_USAGE -eq 0 ]; then
            echo ''
            read -r -p "    * [Y]es: Generate Token.   [N]o: I have an existing token.   [H]elp.    [Default: 'Y']: " GENERATE
            PROMPT_USAGE=1
        fi
    
        case "${GENERATE}" in
            # Print Help
            "h" | "H")
                echo -e "\n### [HELP][AUTHTOKEN] ###"\
                        "\n    Authtoken is the most secure way to connect to UrbanCode Deploy via CLI."\
                        "\n    It is used in combination with it's related username."\
                        "\n    To generate an authtoken, you can either:"\
                        "\n        - use this tool"\
                        "\n        - or from your browser go to '${UCD_WEB_URL}/#security/tokens'"\
                        "\n          (NOTE: you will need to have administrator privileges)"\
                        "\n###\n"
                PROMPT_USAGE=0
            ;;
     
            # Get User Token
            "n" | "N")
            
                echo ''
                read -r -p "    * TOKEN: " TOKEN
                
                # Check input
                if [ -z "${TOKEN// /}" ]; then
                    echo -e "      [WARNING] TOKEN is REQUIRED!"
                    # skip Prompt for usage
                # Vaalidate TOKEN format
                elif [[ ! "${TOKEN}" =~ $TOKEN_FORMAT ]]; then
                    echo -e "      [WARNING] Provided TOKEN: '${TOKEN}' is NOT in CORRECT FORMAT!"\
                            "\n\n      [INFO] Correct Format is: "\
                            "[8 AlphaNumeric characters]-[4 AlphaNum chars]-[4 AlphaNum chars]-[4 AlphaNum chars]-[12 AlphaNum chars]"\
                            "\n\n      [EXAMPLE] 'abcd1234-ab12-cd34-ef56-abcdef123456'"
                    TOKEN=''
                    PROMPT_USAGE=0
                fi
            ;;
    
            # DEFAULT: Generate Token:
            *)
                echo -e "\n[INFO] Generating AUTHTOKEN...\n"\
                        "\n    [ACTION] Enter details for the AUTHTOKEN:"
                # Get and set Expiry Date
                read -r -p "        * Expiration date (YYYY-MM-DD).    [Default: '${DEFAULT_TOKEN_EXP_DATE}']: " DATE
                # Default DATE
                if [ -z "${DATE// /}" ] || [[ ! "${DATE}" =~ $DATE_FORMAT ]]; then
                    if [ ! -z "${DATE// /}" ]; then
                        echo -e "\n          [WARNING] Specified Date: '${DATE}' not in correct format!"
                    fi
                    echo -e "\n          [INFO] Defaulting Token Expiry Date to: '${DEFAULT_TOKEN_EXP_DATE}'"
                    DATE="${DEFAULT_TOKEN_EXP_DATE}"
                fi
    
                # Get HOST
                echo -e "\n    [ACTION] Setup IP HOST to be used in your Token:\n"
                read -r -p "        * A[ll] Allow Token usage from ANY IP.    [Default: your current IP: '${IP}']: " HOST
                if [[ "${HOST// /}" =~ ^([aA]{1})([lL]{0,2})$ ]] || [ "${HOST// /}" = '0.0.0.0' ]; then
                    echo -e "\n[INFO] Setting Token HOST to: 'All'"
                    HOST=''
                else
                    # Validate user input to be in IPV4_FORMAT
                    if [[ "${HOST// /}" =~ $IPV4_FORMAT ]]; then
                        echo -e "\n[INFO] Setting Token HOST to: '${IP}'"
                        HOST="${IP}"
                    else
                        echo -e "\n[WARNING] Defaulting Provided HOST: '${HOST}' to current IP: '${IP}'"
                        HOST="${IP}"
                    fi
                fi
    
                # Retrieve user ID from UCD_WEB_URL
                echo -e "\n[INFO] Retrieving User ID for User: '${UCD_USER}' ..."
                OUT=$(udclient/udclient -username "${UCD_USER}" -password "${UCD_PASSWORD}" -weburl "${UCD_WEB_URL}" getUser -user "${UCD_USER}" 2>&1)
                if [ $? -ne 0 ]; then
                    echo -e "\n[ERROR] Failed to retrieve ID for User: '${UCD_USER}'."\
                            "\n        Exit Code: '$?'"\
                            "\n[ERROR] Message: \n###\n${OUT}\n###\n"
                    return 1
                else
                    # Extract the User ID from the uDeploy response
                    ID=$(php INIT/jsonGet.php "id" "${OUT}" 2>&1)
                    ID_GET_STATUS=$?
                    if [ $ID_GET_STATUS -ne 0 ]; then 
                        echo -e "\n[ERROR] Failed to retrieve ID for User: '${UCD_USER}'."\
                                "\n        Exit Code: '$ID_GET_STATUS'"
                        return $ID_GET_STATUS
                    fi
                fi
    
                # Generate Token
                echo -e "\n[INFO] Generating AUTH TOKEN for User: '${UCD_USER}'..."
                EXP=$(php INIT/strtotime.php "${DATE}")
                JSON='{"description":"uCommand Setup Authtoken","expDate":"'${DATE}'T00:00:00.000Z","expTime":"'${DATE}'T00:00:00.000Z","host":"'${HOST}'","expiration":"'${EXP}'000","userId":"'${ID}'"}'
                UCD_AUTHTOKEN_URL="${UCD_WEB_URL}/security/authtoken"
                TOKEN_GET=$(curl -u "${UCD_USER}:${UCD_PASSWORD}" -H "Content-Type: application/json" -k -s -X PUT -d "${JSON}" "${UCD_AUTHTOKEN_URL}" 2>&1)
                TOKEN_GET_STATUS=$?
                TOKEN_ERROR=$(echo $TOKEN_GET | grep -i "Error")
                TOKEN_ERROR_DATE=$(echo $TOKEN_GET | grep -i "The expiration date must be after the current date")
                if [ $TOKEN_GET_STATUS -ne 0 ] || [ ! -z "$TOKEN_ERROR" ]; then
                    echo -e "\n[ERROR] Failed to generate an authtoken for User: '${UCD_USER}'"\
                            "\n        Exit Code: '${TOKEN_GET_STATUS}'"\
                            "\n[ERROR] Message: \n###\n${TOKEN_ERROR}:${TOKEN_ERROR_DATE}\n###\n"\
                            "\n[ACTION] Please provide an existing authtoken to continue the setup."\
                            "\n    OR Press [Enter]: to retry AUTH TOKEN Creation."\
                            "\n    Otherwise, CTRL+C to quit\n"
                    read -r -p "         * TOKEN: " TOKEN
                    PROMPT_USAGE=1
                else 
                    TOKEN=$(php INIT/jsonGet.php "token" "$TOKEN_GET")
                    TOKEN_GET_STATUS=$?
                    if [ $TOKEN_GET_STATUS -ne 0 ]; then
                        echo -e "\n[ERROR] Failed to generate an authtoken for User: '${UCD_USER}'"\
                                "\n        Exit Code: '${TOKEN_GET_STATUS}'"
                        return $TOKEN_GET_STATUS
                    else
                        # Token will be printed in 
                        # Step #7: Setup uCommand Config of the setup script
                        echo -e "\n[DONE]"
                        return 0
                    fi
                fi
            ;;
        esac
    done
}


# Validate UserName and Password
function validate_user_login () {
    
    local RETURN_STATUS=0
    
    echo -e "\n[INFO] Validating username and password...\n"
    
    # Make sure specified user is logged out
    # Usually when Logout was successful we should get message: 'Operation succeeded.' ans Status: 0
    LOGOUT_MSG=$(./udclient/udclient -weburl $UCD_WEB_URL -username $UCD_USER -password $UCD_PASSWORD logout 2>&1)
    LOGOUT_STATUS=$?
    # however we are not going to validate this here 
    # as the 'logout' is not returning any message in case of an error (even with --verbose oprion)

    # Send login request to validate user access
    ./udclient/udclient -weburl $UCD_WEB_URL -username $UCD_USER -password $UCD_PASSWORD login
    RETURN_STATUS=$?
    if [ ! "$RETURN_STATUS" -eq 0 ]; then
        echo -e "\n[ERROR] There was a problem with User Login check!";
        return $RETURN_STATUS
    else
        echo '       [OK]'
        return $RETURN_STATUS
    fi
}