#!/bin/bash

appname=""
cellname=""
clustername=""
earfile=""
hostname=""
nodename=""
password=""
servername=""
username=""
virtualhost=""
webservername=""
showhelp=""
cluserOrServerName=""

cmdname=${0##*/}
usage()
{
cat << EOS >&2
Usage:
    $cmdname
    -a | --appname              The name of the application to deploy, such as "MAXIMO".
    -c | --cellname             The WebSphere cell name, for example "ctgCell01".   
    --clustername               The name of the cluster to deploy the application to, for example "MXUICluster". 
    -e | --earfile              The path to the Maximo EAR file to deploy, for example "D:\IBM\SMP\maximo\deployment\default\maximo.ear".
    -h | --hostname             The name of the WebSphere deployment manager host.                                                                
    -n | --nodename             The WebSphere Node name that hosts the web server, for example "ctgNode01".                                       
    -p | --password             The WebSphere user password.                                                                                      
    -s | --servername	        The application server name to deploy the application to, for non-clustered environments, for example "MXServer". 
    -u | --username	            The WebSphere username.                                                                                           
    -v | --virtualhost	        The name of the virtual host to deploy the application to, for example "maximo_host".                             
    -w | --webservername        The name of the web server to bind the application to, for example "webserver1".
    --help                      Prints this help message
EOS
}


while (( "$#" )); do    
    case "$1" in
        -a|--appname)
            if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
                
                appname=$2
                shift 2
            else
                printf "\nError: Argument for $1 is missing an application name, a value must be provided.\n" >&2
                exit 1
            fi
        ;;
        -c|--cellname)
            if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
                cellname=$2
                shift 2
            else
                printf "\nError: Argument for $1 is missing a cell name, a value must be provided.\n" >&2
                exit 1
            fi
        ;;    
        --clustername)
            if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
                clustername=$2
                shift 2
            else
                printf "\nError: Argument for $1 is missing a cluster name, a value must be provided.\n" >&2
                exit 1
            fi
        ;;   
        -e|--earfile)
            if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
                earfile=$2
                shift 2
            else
                printf "\nError: Argument for $1 is missing a path to the EAR file, a value must be provided.\n" >&2
                exit 1
            fi
        ;; 
        -h|--hostname)
            if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
                hostname=$2
                shift 2
            else
                printf "\nError: Argument for $1 is missing a host name, a value must be provided.\n" >&2
                exit 1
            fi
        ;;    
        -n|--nodename)
            if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
                nodename=$2
                shift 2
            else
                printf "\nError: Argument for $1 is missing a node name, a value must be provided.\n" >&2
                exit 1
            fi
        ;;    
        -p|--password)
            if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
                password=$2
                shift 2
            else
                printf "\nError: Argument for $1 is missing a password, a value must be provided.\n" >&2
                exit 1
            fi
        ;;  
        -s|--servername)
            if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
                servername=$2
                shift 2
            else
                printf "\nError: Argument for $1 is missing a server name, a value must be provided.\n" >&2
                exit 1
            fi
        ;;                                                         
        -u|--username)
            if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
                username=$2
                shift 2
            else
                printf "\nError: Argument for $1 is missing a username, a value must be provided.\n" >&2
                exit 1
            fi
        ;; 
        -v|--virtualhost)
            if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
                virtualhost=$2
                shift 2
            else
                printf "\nError: Argument for $1 is missing a virtual host name, a value must be provided.\n" >&2
                exit 1
            fi
        ;;         
        -w|--webservername)
            if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
                webservername=$2
                shift 2
            else
                printf "\nError: Argument for $1 is missing a web server name, a value must be provided.\n" >&2
                exit 1
            fi
        ;;           

        --help) 
            usage               
            exit 0
        ;;     
        -*|--*=) # unsupported flags
            printf "\nError: Unsupported flag $1.\n" >&2
            exit 1
        ;;
        *) # preserve positional arguments
            PARAMS="$PARAMS $1"
            shift
        ;;
    esac
done


get_password() {
while [ -z "$password" ]; do
    read -s -p "Enter Password: " password

    if [ -z "$password" ]; then
        printf "\nA password is required\n"
    fi
done
echo ""
}

validate(){
    if [ -z "$appname" ]; then
        echo "Error: The application name is required."
        exit 1
    fi

    if [ -z "$cellname" ]; then
        echo "Error: The cell name is required."
        exit 1
    fi

    if [ -z "$clustername" ] && [ -z "$servername" ]; then
        echo "Error: Either a server name or a cluster name is required."
        exit 1
    fi

    if [ -z "$earfile" ]; then
        echo "Error: The path to the Maximo EAR file is required."
        exit 1
    fi    

    if [ ! -f $earfile ]; then 
        echo "Error: The EAR file $earfile does not exist."
        exit 1
    fi   

    if [ -z "$hostname" ]; then
        echo "Error: The WebSphere Deployment Manager host name is required."
        exit 1
    fi    

    if [ -z "$nodename" ]; then
        echo "Error: The WebSphere node name is required."
        exit 1
    fi

    if [ -z "$username" ]; then
        echo "Error: The WebSphere user name is required."
        exit 1
    fi

    if [ -z "$virtualhost" ]; then
        echo "Error: The WebSphere virtual host name is required."
        exit 1
    fi    

    if [ -z "$webservername" ]; then
        echo "Error: The web server name is required."
        exit 1
    fi

}

validate

get_password

if [ ! -z "$servername" ]; then    
    clusterOrServerName="$servername"
else
    clusterOrServerName="$clustername"
fi

echo "The cluster or server name is $clusterOrServerName"

/bin/bash $(pwd)/wasclient/ThinWsadmin.sh -username $username -password $password -host $hostname -f $(pwd)/scripts/deploy-application.py $earfile $appname $virtualhost $cellname $nodename $clusterOrServerName $webservername
