#! /bin/bash

# Max. no of Questions
maxQuestions=7

# Current Choice
choice=""

# DFM IP Address
dfmIpAddress=""

# DFM user name
dfmUserName=""

# SMVI IP Address
smviIpAddress=""

# SMVI User name
smviUserName=""

# Command Line for config_set
cmd_line_args=""

# Did you configure DFM
didConfigureDFM=false

# Did you configure SMVI
didConfigureSMVI=false

# Are we doing this only for NFS
onlyNfs=false

# Is the transport-protocol iscsi
isISCSI=false

# Ask if the user wants to restart the daemon
shouldRestart="yes"

# Ask if the user wants to continue or not before setting up the variables
shouldContinue="yes"

# See if the multipath software is selected
isMultipath=false
#ports
httpDfmPort="8088"
sslDfmPort="8488"
httpsToDfm="on"

smviPort="8043"
httpsToSMVI="on"

# Function to accept dfm address
function acceptDfmIpAddress() {
    read -p "Enter the DFM Server Name/Ip Address:" dfmIpAddress
    if [ $? -ne 0 ] ; then
        echo "Error in  accepting the DFM Server Ip Address"
    fi
}

# Function to accept smvi address
function acceptSmviIpAddress() {
    read -p "Enter the SMVI Server Ip Address:" smviIpAddress
    if [ $? -ne 0 ] ; then
        echo "Error in  accepting the SMVI Server Ip Address"
    fi
}

# Function to accept dfm username
function acceptDfmUserName() {
    read -p "Enter the DFM Server User Name:" dfmUserName
    if [ $? -ne 0 ] ; then
        echo "Error in  accepting the DFM Server User name"
    fi
}

# Function to accept SMVI username
function acceptSMVIUserName() {
    read -p "Enter the SMVI Server User Name:" smviUserName
    if [ $? -ne 0 ] ; then
        echo "Error in  accepting the SMVI Server User name"
    fi
}

# Function to accept HttpDfmPort
function acceptHttpDfmPort()   {
    read -p "Enter the HTTP server port to contact to access the DFM [$httpDfmPort]:" inputPort
    if [ $? -ne 0 ] ; then
        echo "Error in  accepting the HTTP server port to contact to access the DFM"
    fi
    if [ "$inputPort" ] ; then
       httpDfmPort=$inputPort
    fi
}

# Function to accept SSL Dfm port
function acceptSslDfmPort()     {
    read -p "Enter the SSL server port to contact to access the DFM [$sslDfmPort]:" inputPort
    if [ $? -ne 0 ] ; then
        echo "Error in  accepting the SSL server port to contact to access the DFM"
    fi
    if [ "$inputPort" ] ; then
        sslDfmPort=$inputPort
    fi
}

# Function to accept SMVI Port
function acceptViAdminPort()    {
    read -p "Enter the HTTP/HTTPS port to contact to access the virtual interface admin [$smviPort]:" inputPort
    if [ $? -ne 0 ] ; then
         echo "Error in  accepting the  HTTP/HTTPS port to contact to access the virtual interface admin"
    fi
    if [ "$inputPort" ] ; then
        smviPort=$inputPort
    fi
}

# Function to enable Https/Http to Dfm
function acceptIfHttpsOrHttpToDfm {
    invalidEntry=true
    while $invalidEntry ; do
        read -p "Enable https for communicating with DFM? [yes, no]:" choice
        if [ $? -ne 0 ] ; then
            echo "Error in  accepting the input"
        fi
        case $choice in
            "yes"|"YES"|"Y"|"y"|"Yes")      httpsToDfm="on"
                                            invalidEntry=false ;;
            "no"|"NO"|"No"|"N"|"n")         httpsToDfm="off"
                                            invalidEntry=false ;;
            *)                              echo "Please enter an proper value"
                                            invalidEntry=true ;;
        esac
    done
}

# Function to enable Https/Http to SMVI Product
function acceptIfHttpsOrHttpToSMVI()    {
    invalidEntry=true
    while $invalidEntry ; do
        read -p "Enable https for communicating with SMVI Product? [yes, no]:" choice
        if [ $? -ne 0 ] ; then
            echo "Error in  accepting the input"
        fi
        case $choice in
            "yes"|"YES"|"Y"|"y"|"Yes")      httpsToSMVI="on"
                                            invalidEntry=false ;;
            "no"|"NO"|"No"|"N"|"n")         httpsToSMVI="off"
                                            invalidEntry=false ;;
            *)                              echo "Please enter an proper value"
                                            invalidEntry=true ;;
        esac
    done
}

# Function to accept user input
function acceptInput {
    read -p "Please enter your choice:" choice
    if [ $? -ne 0 ] ; then
        echo "Error in accepting the input"
        exit
    fi
}

# Function to set Variables
function setVariables() {
    varlist=`cat /opt/NetApp/snapdrive/setup/variables/$1`
    for i in $varlist; do
        cmd_line_args="$cmd_line_args $i"
    done
}

# Function to set the ports
function setPorts() {
    value=2
    val=""
    varlist=`cat /opt/NetApp/snapdrive/setup/variables/$1`
    for i in $varlist; do
        eval val="$"$value
        cmd_line_args="$cmd_line_args $i=$val"
        ((value++))
    done
}

# Function to print the file
function PrintFile()    {
    cat /opt/NetApp/snapdrive/setup/questions/$1
}

# Function to set the variables
function Finalize()     {
    echo -e "\n\nThe following configuration variables will be changed\n"

    echo $cmd_line_args | tr " " "\n"
    invalidEntry=true
    while $invalidEntry ; do

        read -p "Do you want to continue? [yes, no]:" shouldContinue
        if [ $? -ne 0 ] ; then
            echo "Error in accepting the input"
        exit
        fi
        case $shouldContinue in
            "yes"|"YES"|"Y"|"y"|"Yes")      /opt/NetApp/snapdrive/bin/config_set $cmd_line_args
                                            restartDaemon
                                            invalidEntry=false ;;
            "no"|"NO"|"No"|"N"|"n")         callExit
                                            invalidEntry=false ;;
            *)                              echo -e "Enter proper value,(yes or no)"
                                            invalidEntry=true ;;
        esac
    done

}

# Function to clean up everything if "exit" is the input
function callExit()     {
    if $didConfigureDFM ; then
        echo -e "Removing your dfm configuration"
        /opt/NetApp/snapdrive/bin/snapdrive config delete $dfmIpAddress
    fi
    if $didConfigureSMVI ; then
        echo -e "Removing your smvi configuration"
        /opt/NetApp/snapdrive/bin/snapdrive config delete $smviIpAddress
    fi
    PrintFile warning.txt
    exit
}


function restartDaemon() {
    invalidEntry=true
    if $isMultipath ; then
        echo -e "SDU is configured for using multipathing.The multipathd daemon should be configured correctly and running, before restarting the snapdrive daemon"
    fi
    while $invalidEntry ; do

        #Ask if the daemon needs to be restarted
        read -p "Do you want the snapdrive daemon to be restarted now? [yes, no]:" shouldRestart
        if [ $? -ne 0 ] ; then
            echo "Error in accepting the input"
        exit
        fi
        case $shouldRestart in
            "yes"|"YES"|"Y"|"y"|"Yes")      echo -e "\n\nRestarting the snapdrive daemon..."
                                            snapdrived restart
                                            invalidEntry=false ;;
            "no"|"NO"|"No"|"N"|"n")         echo -e "\n\nPlease restart the snapdrive daemon manually for the changes to take place"
                                            invalidEntry=false ;;
            *)                              echo -e "Enter proper value,(yes or no)"
                                            invalidEntry=true ;;
        esac
    done
}

function acceptDfmPorts()   {
    acceptHttpDfmPort
    acceptSslDfmPort
    acceptIfHttpsOrHttpToDfm
    setPorts "DFMPORTS.txt" $httpDfmPort $sslDfmPort $httpsToDfm
}

function acceptSMVIPorts()  {
    acceptViAdminPort
    acceptIfHttpsOrHttpToSMVI
    setPorts "SMVIPORTS.txt" $smviPort $httpsToSMVI
}


function questionOne() {
    PrintFile 1.txt
    acceptInput
    case $choice in
        "nfs"|"NFS"|"Nfs")                  setVariables "NFS.txt"
                                            onlyNfs=true
                                            count=4 ;;
        "san"|"SAN"|"San")                  setVariables "SAN.txt"
                                            onlyNfs=false
                                            count=2 ;;
        "mixed"|"Mixed"|"MIXED")            setVariables "mixed.txt"
                                            onlyNfs=false
                                            count=2 ;;
        "exit"|"Exit"|"EXIT")               callExit ;;
        *)                                  echo "Please Enter proper value"
                                            count=1 ;;
    esac
}

function questionTwo() {
    PrintFile 2.txt
    acceptInput
    case $choice in
        "fcp"|"FCP"|"Fcp")                  setVariables "FCP.txt"
                                            isISCSI=false
                                            count=3 ;;
        "iscsi"|"ISCSI"|"Iscsi")            setVariables "ISCSI.txt"
                                            isISCSI=true
                                            count=3 ;;
        "exit"|"EXIT"|"Exit")               callExit ;;
        *)                                  echo "Please Enter proper value"
                                            count=2 ;;
    esac
}

function questionThree() {
    PrintFile 3.txt
    acceptInput
    case $choice in
        "veritas"|"Veritas"|"VERITAS")      if [ "linux" = "solaris" ] || [ "linux" = "aix" ] ; then
                                                if $isISCSI ; then
                                                    echo -e "Veritas is not supported for ISCSI transport protocol.Select native/none"
                                                    count=3
                                                else
                                                    setVariables "VERITAS.txt"
                                                    isMultipath=true
                                                    count=4
                                                fi
                                            else
                                                    echo -e "Veritas not supported on linux.Enter proper choice"
                                                    count=3
                                            fi
                                            ;;
        "native"|"NATIVE"|"Native")         setVariables "NATIVE.txt"
                                            isMultipath=true
                                            count=4 ;;
        "none"|"None"|"NONE")               setVariables "NONE.txt"
                                            isMultipath=false
                                            count=4 ;;
        "exit"|"EXIT"|"Exit")               callExit ;;
        *)                                  echo "Please Enter proper value"
                                            count=3 ;;
    esac
}

function questionFour() {
    PrintFile 4.txt
    acceptInput
    case $choice in
        "yes"|"YES"|"Y"|"y"|"Yes")          setVariables "PMIYES.txt"
                                            # Accept Parameters for snapdrive config set
                                            acceptDfmIpAddress
                                            acceptDfmUserName
                                            /opt/NetApp/snapdrive/bin/snapdrive config set -dfm $dfmUserName $dfmIpAddress
                                            if [ $? -eq 0 ] ; then
                                                didConfigureDFM=true
                                                acceptDfmPorts
                                            fi
                                            count=5 ;;
        "no"|"No"|"NO"|"N"|"n")             setVariables "PMINO.txt"
                                            count=5 ;;
        "exit"|"EXIT"|"Exit")               callExit ;;
        *)                                  echo "Please Enter proper value"
                                            count=4 ;;
    esac
}


function questionFive() {
    PrintFile 5.txt
    acceptInput
    case $choice in
        "dfm"|"DFM"|"Dfm")                  setVariables "RBACDFM.txt"
                                            if ! $didConfigureDFM ; then
                                                echo "Warning: The RBAC method is selected as DFM without enabling Protection Manager Integration"
                                            fi
                                            count=6
                                            ;;
        "native"|"Native"|"NATIVE")         setVariables "RBACNATIVE.txt"
                                            count=6 ;;
        "exit"|"EXIT"|"Exit")               callExit ;;
        *)                                  echo "Please Enter proper value"
                                            count=5 ;;
    esac
}


function questionSix() {
    if [ "linux" = "linux" ] || [ "linux" = "solaris_x86" ] && ! $onlyNfs  && ! $isISCSI ; then
        PrintFile 6.txt
        acceptInput
        case $choice in
            "yes"|"YES"|"Y"|"y"|"Yes")      setVariables "SMVIYES.txt"
                                            # Accept Parameters for snapdrive config set
                                            acceptSmviIpAddress
                                            acceptSMVIUserName
                                            /opt/NetApp/snapdrive/bin/snapdrive config set -viadmin $smviUserName $smviIpAddress
                                            if [ $? -eq 0 ] ; then
                                                didConfigureSMVI=true
                                                acceptSMVIPorts
                                            fi
                                            count=7 ;;
            "no"|"No"|"NO"|"N"|"n")         setVariables "SMVINO.txt"
                                            count=7 ;;
            "exit"|"EXIT"|"Exit")           callExit ;;
            *)                              echo "Please Enter proper value"
                                            count=6 ;;
        esac
    else
        count=7
    fi
}

function questionSeven() {
    PrintFile 7.txt
    acceptInput
    case $choice in
        "http"|"Http"|"HTTP")               setVariables "HTTP.txt"
                                            count=`expr $maxQuestions + 1` ;;
        "https"|"HTTPS"|"Https")            setVariables "HTTPS.txt"
                                            count=`expr $maxQuestions + 1` ;;
        "done"|"Done"|"DONE")               count=`expr $maxQuestions + 1` ;;
        "exit"|"EXIT"|"Exit")               callExit ;;
        *)                                  echo "Please Enter proper value"
                                            count=7 ;;
    esac
}

function Main() {

    # Print the Introduction screen
    PrintFile introduction.txt

    # Questions are not serial as we allow back and forth
    count=1
    while [ $count -le $maxQuestions ]
    do
        case $count in
            1)questionOne ;;
            2)questionTwo ;;
            3)questionThree ;;
            4)questionFour ;;
            5)questionFive ;;
            6)questionSix ;;
            7)questionSeven ;;
    esac
    done

    # Set the Variables in one shot
    Finalize
}

Main
