#!/usr/bin/ksh
#
# Copyright (c) 2002-2003 Sun Microsystems, Inc. All rights reserved
#
#  @(#)config	1.16 03/03/31 
#
#  This program is a wrapper around the useradd/usermod Solaris utilities
#  used for Diagnostic Expert server configuration, and the ras_install 
#  script used for Diagnostic Expert data-host agent configuration.
#

#---------------------------------------
# Globals
#---------------------------------------
PROGNAME=$0

#
# source the libaries
#
base=`pkgparam SUNWstm BASEDIR` > /dev/null 
esmlib=$base/SUNWstm/lib/

if [[ ! -d $esmlib ]]; then
    echo "$PROGNAME: ERROR: $esmlib is not found. Please verify the install of SUNWstm. Exiting."
    exit 1
fi

. $esmlib/esm_global.ksh
. $esmlib/esm_pkgs.ksh
. $esmlib/esm_lib.ksh

ESM_LOG=$ESM_DE_LOG

#---------------------------------------
#  Display the usage for the main program
# Arguments: 
#  none
#---------------------------------------

usage () {

    echo $PROGNAME: "INFO: usage: $PROGNAME [options]

    where options are:

  -A | --all                  config both the SSDE Server and Data-Host Agent
  -s | --server               config only the SSDE Server
  -a | --agent                config only the SSDE data-host agent
  -S | --silent               perform the config silently
  -p | --print                print the current configuration information
  -u | --useradd username     add "username" to the SA_Admin role
  -d | --userdel username     delete "username" from the SA_Admin role
  -? | -h | --help
  
"
    config=""
}


#---------------------------------------
# Arguments: 
#  none
#---------------------------------------
print_current_config () {
    useronly=$1
    echoto=vecho
   [ $ESM_SILENT -eq 1 ] && echoto=secho

    # Check to verify that the SSDE server package exists
	/bin/pkgparam SUNWsade BASEDIR > /dev/null 2>&1
    if [ $? -eq 0 ]; then 
        if [ -z "$useronly" ]; then
            secho "============================================="
            $echoto ""
            $echoto "SSDE Server Parameters"
        fi
        $echoto ""
        $echoto "    Administrators:"
        awk -F":" -e '/roles=.*SA_Admin/{ print $1}' /etc/user_attr | cat -n > /tmp/.adminusers

        [ $ESM_SILENT -eq 0 ] && cat /tmp/.adminusers > /dev/stderr
        cat /tmp/.adminusers >> $ESM_DE_LOG
        $echoto " "
    fi
 
    if [ -z $useronly ]; then
    # Check to verify that the SSDE data host agent package exists
	/bin/pkgparam SUNWstadh BASEDIR > /dev/null 2>&1
	[ $? -ne 0 ] && return 1
        path=$(pkgparam SUNWstadh BASEDIR)
        path=$path/SUNWstadh/DATA/rasagent.conf
	if [ -f $path ]; then
	    master=$(awk -F"=" '/hostname/{ print $2 }' $path)
	else
	    master="not available"
	fi	

        $echoto "SSDE Data Host Agent Parameters"
        $echoto " "
        $echoto "    SSDE Master:            $master"

        # The following three parameters are saved on the SSDE Master
        # so are not available.

        $echoto "    Probing Frequency:      not available"
        $echoto "    Message File Location:  not available"
        $echoto ""
        secho "============================================="
    fi
}

#---------------------------------------
# Arguments: 
#  none
#---------------------------------------
server_check () {
    if [ ! $DEBUG ]; then
        SSDEBASE=$(pkgparam SUNWsade BASEDIR)
        if [ $? -eq 1 ]; then
            vecho
            vecho "$PROGNAME: ERROR: Sun StorEdge Diagnostic Expert"
            vecho "                  is not installed on this system."
            exit 1
        fi
    fi
}

#---------------------------------------
# Arguments: 
#  none
#---------------------------------------
agent_check () {
    if [ ! $DEBUG ]; then
        SSSABASE=$(pkgparam SUNWstadh BASEDIR)
        if [ $? -eq 1 ]; then
            vecho
            vecho "$PROGNAME: ERROR: Storage Automated Diagnostic Environment"
            vecho "                  is not installed on this system."
            exit 1
        fi
    fi
}

#---------------------------------------
# Arguments: 
#  none
#---------------------------------------
server_display_menu () {
    sdpmn_intro="\n
Sun StorEdge Diagnostic Expert (SSDE) requires that\n 
one role, SA_Admin be assigned to a user id.  The SSDE install \n
adds the SA_Admin role to the system.  Use the following to \n 
manage the user id's that belong to the SA_Adimin role.\n"
    sdpmn_menu="
          (a) Add a user to SA_Admin role
          (d) Delete a user from SA_Admin role
          (q) Quit\n
Your Choice: "

    secho ""
    if [ "$first_time" = "yes" ]; then
        vecho "Configure Diagnostic Expert Server"
        vecho ""
        vecho $sdpmn_intro
    fi
    print_current_config usersonly
    vecho "${sdpmn_menu}\c"
    read choice
    echo $choice
    secho $choice
}

#---------------------------------------
# Arguments: 
#  none
#---------------------------------------
server_useradd () {

    tmp=1
    if [ "$1" = "" ]; then
        vecho "Enter Solaris user name: \c"
        read lusername
        secho $lusername
    else
        lusername=$1
    fi
    
    /bin/grep "^$lusername:" /etc/passwd > /dev/null
    if [ $? -eq 0 ]; then
        # User exists
        vecho ""
        vecho "$PROGNAME: ERROR: user $lusername already exists on this system."
        vecho ""
        tmp=0
    else
	ypcat passwd | grep "^$lusername:" > /dev/null
	if [ $? = 0 ]; then
	    # User exists
	    vecho ""
	    vecho "$PROGNAME: ERROR: user $lusername already exists on the network."
	    vecho "                  SSDE administrators must be local to this host"
	    vecho ""
	    tmp=0
	else
	    # Add user
	    # (Note: useradd always returns an exit status of 0,
	    #  so as a work around the output of useradd is used
	    #  to trap errors.
	    uacatch=$(useradd -s /bin/pfcsh $lusername 2>&1)
	    if [ "$uacatch" = "" ]; then
	       passwd $lusername
	    else
	       # Note: Even though useradd says it failed it still
	       #       added the user to the /etc/passwd and /etc/shadow
	       #       files, so delete the invalid user.
	       vecho ""
	       vecho $uacatch
	       vecho ""
	       $(userdel $lusername)
	       tmp=0
	    fi
	fi
    fi

    if [ $tmp -eq 1 ]; then
        # Set SA_Admin as one of the user's roles. If the user already has
        # SA_Admin as a role, then the following command does nothing.

        usermod -R SA_Admin $lusername
    fi
    changed=1
}

#---------------------------------------
# Arguments: 
#  none
#---------------------------------------
server_userdel () {
    prompt="     (q) Quit\n
Your Choice: "


    if [ "$1" = "" ]; then
        tmpusername=.
        while [ $tmpusername != q ];
        do
            print_current_config usersonly
            vecho "${prompt}\c"            
            read tmpusername
            secho $tmpusername

            lusername=$(awk -e "/$tmpusername/{print \$2}" /tmp/.adminusers)
            if [ "$lusername" != "" ]; then
                userdel $lusername
            fi
        done
    else
        lusername=$1
        userdel $lusername
    fi
    changed=1
}

#---------------------------------------
# Arguments: 
#  none
#---------------------------------------
server_config () {
    first_time=yes

    server_check

    choice=.
    while [ $choice != q ];
    do
        choice=$(server_display_menu $first_time)
        choice=$(echo $choice | tr "\n" "")
        first_time=""

        case $choice in
        a|A)
            server_useradd $username
        ;;
        d|D)  
            server_userdel $username
        ;;
        esac
    done
}

#---------------------------------------
# Arguments: 
#  none
#---------------------------------------
agent_display_menu () {
    adpmn_intro="\n
Sun StorEdge Diagnostic Expert (SSDE) Data-Host\n
Agent requires that you configure the following information:\n
\n
- Hostname:port or IP Address:port of the SSDE Master\n
- Probing Frequency (5-120)\n
- Message File Location (default=/usr/adm/messages)\n
- Start/Stop of the Agent cron job"
    adpmn_menu="
Are you ready to configure the Data-Host Agent? ([y],n) "

    vecho $adpmn_intro
    vecho "${adpmn_menu}\c"
    read choice
    secho $choice
    echo $choice
}

#---------------------------------------
# Arguments: 
#  none
#---------------------------------------
agent_config () {

    echo "Configure Diagnostic Expert Data-Host Agent"

    agent_check

    choice=$(agent_display_menu)

    if [ "$choice" != "n" ]; then
        $SSSABASE/SUNWstadh/bin/ras_install
        changed=1
    fi
}

#---------------------------------------
# Arguments: 
#  none
#---------------------------------------
display_menu () {
    sdpmn_intro="\n
Sun StorEdge Diagnostic Expert (SSDE) Configuration:"
    sdpmn_menu="
          (1) Add, Edit, Delete SSDE Administrators
          (2) Configure the Data Host Agent
          (p) Print the Current Configuration
          (q) Quit\n
Your Choice: "

    secho ""
    vecho $sdpmn_intro
    vecho "${sdpmn_menu}\c"
    read choice
    [ "$choice" = "" ] && choice=1
    secho $choice
    echo $choice
}


#---------------------------------------
# Arguments: 
#  $1 action
#---------------------------------------
optaction () {
    case $1 in
	all)
        config_server=1
        config_agent=1
    ;;
	server)
        config_server=1  
        config_agent=0  
    ;;
	agent)
        config_agent=1
        config_server=0  
    ;;
	silent)
        verbose=0
    ;;
	print)
        print_current_config
        config_agent=0
        config_server=0  
    ;;
	useradd)
        if [ $# -gt 1 ]; then
            username=$2
            server_useradd $username
        else
            vecho ""
            vecho "$PROGNAME: ERROR: Missing user name."
            vecho ""
            usage
            exit 1
        fi
        config_server=0
        config_agent=0
    ;;
	userdel)
        if [ $# -gt 1 ]; then
            username=$2
            server_userdel $username
        else
            vecho ""
            vecho "$PROGNAME: ERROR: Missing user name."
            vecho ""
            usage
            exit 1
        fi
        config_server=0
        config_agent=0
    ;;
    none)
    ;;
	*)
        config_server=0
        config_agent=0
        usage
    ;;
    esac
}

#---------------------------------------
# Arguments: 
#  $1 set of compound arguments (-aSAa)
#---------------------------------------
lgetopts () {
    while getopts :AsaSplud gopt
    do
        case $gopt in
	    A)
            action=all
        ;;
	    s)
            action=server
        ;;
	    a)
            action=agent
        ;;
	    S)
            action=silent
        ;;
	    p)
            action=print
        ;;
	    u)
            action=useradd
        ;;
	    d)
            action=userdel
        ;;
        *)
            action=help
        ;;
        esac
        optaction $action $optarg
    done
}

set_env () {
    lopt=$1
    case $lopt in
	1)  config_server=1
        start=1
	    ;;
	2)  config_agent=1
        start=1
        ;;
    p)  print_config=1
        config_agent=0
        config_server=0
        ;;
    q)  update=0
        ;;
	*)  vecho ""
        vecho "Error: $lopt is an invalid selection. "
        vecho ""
	    ;;
    esac
}

reset_state () {
    config_server=0
    config_agent=0
    print_config=0
}

#---------------------------------------
# Start SSDE if it is not already running.
# Arguments:
#  none
#---------------------------------------
start () {
    ps -ef | grep java | grep SADE > /dev/null 2>& 1
    if [ $? -ne 0 ]; then
	vecho " "
	vecho "Starting SSDE."
	/etc/init.d/init.ssde start
    fi
}

#---------------------------------------
# Arguments: 
#  $1 command line arguments (see usage function above)
#---------------------------------------
main () {
    start=0
    verbose=1
    reset_state
    update=1
    action=""
    sum=0
    changed=0
	
    [ $# -ne 0 ] && sum=1
    while [ $# -ne 0 ];
    do
        opt=$1
        optarg=$2
        echo $opt | grep '\-\-' > /dev/null
        if [ $? -eq 0 ]; then 
		    optaction $(echo $opt | tr -d '-') $optarg
        else
            lgetopts $opt
        fi
        shift
    done

    secho "Original SSDE Configuration"
    ESM_SILENT=1
    print_current_config
    ESM_SILENT=0
    
    while [ $update -eq 1 ]; do
        if [ $sum -eq 0 ]; then
            menu=1
            retval=$(display_menu)
            set_env $retval
        else
            update=0
        fi
        [ $config_server -eq 1 ] && server_config

        [ $config_agent -eq 1 ] && agent_config

        [ $print_config -eq 1 ] && print_current_config

        reset_state
    done

    secho ""
    secho "New SSDE Configuration"
    ESM_SILENT=1
    print_current_config
    ESM_SILENT=0

    # Start SSDE if it is not already running.
    start
}

generic_log_title SSDE "$DE_VERSION" "Start of Configuration"

main $@

generic_log_title SSDE "$DE_VERSION" "End of Configuration"

if [ "$DEBUG" != "" ]; then
    echo "config_server |$config_server|"
    echo "config_agent  |$config_agent|"
    echo "verbose       |$verbose|"
    echo "username      |$username|"
fi

exit 0

