#! /bin/sh
DIALOG=${DIALOG=dialog}

DEVICES="0e11a0f0 0e11b203 0e1100d7"

tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/hpasmconfig$$

#messages
TITLE="hpasm Configuration"

ESC_MSG="\n\n\n<ESC> will terminate this installation program. Settings will be lost."

WELCOME_MSG="  Welcome to the hp Advance Server Management(hpasm) package installation. This package contains the hp Advanced Server Management Application(hpasmd) and hp's SNMP agents.  This package is intended to only work on hp servers  with the Advanced Server Management (ASM) ASIC.  This application will work with both the ProLiant ASM (0x0E11A0F2) and the ProLiant iLO Advance Server Management (0x0E11B203) ASICs. $ESC_MSG"

GPL_MSG="The startup scripts in this package also control the loading of modules in other hp management packages(i.e. hprsm).  Those management packages contain modules that are non-GPL.  When these modules are loaded the kernels \"tainted\" flag (see /proc/sys/kernel/tainted) may be set.   If you choise not to load them, some management functionality will be lost\n\n\n\nDo you want to load the hp module even though they may \"taint\" your kernel? $ESC_MSG"

SNMP_AGENTS_MSG="  This package contains data gathering agents that provide data to clients via SNMP.  verbage...verbage...verbage. If you chose not to load all of them some data will not be provided.\n\n\n\nDo you want to configure the hp snmp agents? $ESC_MSG"

AGENT_CONFIG_MSG="  Configure each item in the menu below.  You will come back to this screen when the confiration of the selected item is done. When all required configuration is done the <Continue> button will be available.  You can chose by using the UP/DOWN arrow keys or by using the first letter of the choice as a hot key. $ESC_MSG"

RW_TITLE="Configure SNMP Read/Write Community"
RWCOMSYS_MSG="Enter the IP or DNS of the system that will be using the SNMP data from this system.  The entered system will have both read and write permissions.  It is important for security purposes that this information be kept confidential.$ESC_MSG"
RWCOMSTR_MSG="Enter the Read Write Community string below. This will be the string used by SNMP to authinticate that the system making the request has permissions to do so.  It is important for security purposes that this information be kept confidential.$ESC_MSG"
LHDEFCOMSTR="com`dd if=/dev/urandom bs=1 count=100 2>/dev/null | md5sum | cut -b1-12`"

RO_TITLE="Configure SNMP Read Only Community"
ROCOMSYS_MSG="Enter the IP or DNS of the system that will be using the SNMP data from this system.  The entered system will have read only permissions.  It is important for security purposes that this information be kept confidential.$ESC_MSG"
ROCOMSTR_MSG="Enter the Read Only Community string below. This will be the string used by SNMP to authinticate that the system making the request has permissions to do so.  It is important for security purposes that this information be kept confidential.$ESC_MSG"


TRAP_TITLE="Configure TRAP Community"
TRAPCOMSYS_MSG="Enter the IP or DNS of the system that will be recieving SNMP traps from this system.  When the agents determine that certain conditions warant a trap will be sent to the system you enter.$ESC_MSG"
TRAPCOMSTR_MSG="Enter the TRAP Community string below. This will be the string used by SNMP on the system recieving the TRAP to authinticate that the this system has permissions to do so.  It is important for security purposes that this information be kept confidential.$ESC_MSG"

SYSCONTACT_TITLE="Configure the SNMP System Contact Info"
SYSCONTACT_MSG="Enter the desired contact info for this system. This can be person, phone number, address, room number, etc. SNMP will ... verbage verbage. whenever verbage verbage. $ESC_MSG"

SYSLOCATION_TITLE="Configure the SNMP System Location Info"
SYSLOCATION_MSG="Enter the desired location information for this system. This can be an address, room number, etc. SNMP will ... verbage verbage. whenever verbage verbage. $ESC_MSG"

AGENTS_TITLE="Configure SNMP collection agents"
AGENTS_MSG="Select the collection agent that you with to load. The one without the <x> will be excluded from loading.  The data that those excluded agents provide will not be available through hp's management system.  The description indicates the type of data the corresponding agent gathers"
AGENTS_DESC="cmahealthd - \"the health of the system. e.i fans, temp, etc.\"\ncmastdeqd - \"The physical configuration of the system.\"\ncmaperfd - \"The general performance data of the system.\"\ncmathreshd - \"TODO TODO.\"\ncmahostd - \"the software running on the system.\"\ncmaidad - \"the Intelegent Disk Array subsystem configuration.\"\ncmafcad - \"the Fibre Channel subsysem configuration.\"\ncmaided - \"the IDE subsystem configuration.\"\ncmascsid - \"the SCSI subsystem configuration.\""

AGENTS_LIST="cmahealthd cmaperfd cmastdeqd cmathreshd cmahostd cmaidad cmafcad cmaided cmascsid"

#defaults
NO_TAINTED_KERNEL="NO"
SNMPCONFIG="YES"
RWCOMLIST="localhost $LHDEFCOMSTR"
ROCOMLIST=""
TRAPCOMLIST=""
SYSCONTACT=""
SYSLOCATION=""
AGENTS_EXCLUDED=""

#################################FUNCTIONS#########################

###################################################################
#GetCommunity
# input RW for Read/Write Community
#       RO for Read Only Community
#       TRAP for trapsink Community
# Output RWCOMLIST ROCOMLIST and TRAPCOMLIST will be set to the 
#        users configuration.  They will be lists of 
#        System/community string pairs
###################################################################
GetCommunity()
{
   TMP_MSG="ERROR"

   case $1 in
      RW)
         TMP_TITLE="$RW_TITLE"
         TMP_SYSMSG="$RWCOMSYS_MSG"
         TMP_STRMSG="$RWCOMSTR_MSG"
         TMP_COMLIST="$RWCOMLIST"
      ;;
      RO)
         TMP_TITLE="$RO_TITLE"
         TMP_SYSMSG="$ROCOMSYS_MSG"
         TMP_STRMSG="$ROCOMSTR_MSG"
         TMP_COMLIST="$ROCOMLIST"
      ;;
      Trap)
         TMP_TITLE="$TRAP_TITLE"
         TMP_SYSMSG="$TRAPCOMSYS_MSG"
         TMP_STRMSG="$TRAPCOMSTR_MSG"
         TMP_COMLIST="$TRAPCOMLIST"
      ;;
   esac

   DONE="NO"
   while [ "$DONE" = "NO" ];
   do
      #If we already see some configuration, we need to find
      #out want the user wants to do
      if [ -n "$TMP_COMLIST" ]; then
         $DIALOG --clear --ok-label "DELETE" \
             --cancel-label "ADD" --help-button --help-label "Back" --title "$TMP_TITLE"  --menu \
             "Listed below is this community's current configuration.  Select the appropriate action below\n\n" $ROWS $COLS 7 \
             $TMP_COMLIST 2> $tempfile
         RET=$?
         OPTION=`cat $tempfile`
         [ $RET = 0 ] && ACTION="DELETE"
         [ $RET = 1 ] && ACTION="ADD"
         [ $RET = 255 ] && exit 255 
         [ ! -z "`echo $OPTION | grep HELP`" ] && DONE="YES" && break
      else
         ACTION="ADD"
      fi
   
      echo $ACTION $OPTION $DONE

      if [ "$ACTION" = "ADD" ]; then
         #Get the IP of the system in this community
         $DIALOG  --title "$TMP_TITLE" --clear --inputbox "$TMP_SYSMSG" $ROWS $COLS 2> $tempfile
         RET=$?
         SYS=`cat $tempfile`
         [ $RET -eq 255 ] && exit 255 
         [ $RET -eq 1 -a -z "$TMP_COMLIST" ] && break
         [ $RET -eq 1 ] && continue
      
         #TODO: we may want to actualy ping the system
      
         FRESP="1"
         SRESP="2"
      
         #Now get the community string for the entered system
         while [ "$FRESP" != "$SRESP" ]; 
         do
           $DIALOG  --title "$TMP_TITLE" --clear --inputbox "$TMP_STRMSG" $ROWS $COLS 2> $tempfile
           RET=$?
           FRESP=`cat $tempfile`
           [ $RET -eq 1 ] && break
           [ $RET -eq 255 ] && exit 255 
           
           #Ask for a conformation
           $DIALOG  --title "$TMP_TITLE" --clear --inputbox "Please Confirm by retyping your previous response below" $ROWS $COLS 2> $tempfile
           RET=$?
           SRESP=`cat $tempfile`
           [ $RET -eq 1 ] && break
           [ $RET -eq 255 ] && exit 255 
      
           #Do a little filtering
           if [ "$FRESP" != "$SRESP" ]; then
              $DIALOG --title "ERROR" --msgbox "Inputs did not match!!! Press OK to try again" $ROWS $COLS
              continue
           fi
      
           if [ "$FRESP" = "" ]; then
              $DIALOG --title "ERROR" --msgbox "You entered an invalid community string. Press OK to try again" $ROWS $COLS
              FRESP="1"
              SRESP="2"
              continue
           fi
         done
      
         #check if we go something
         if [ "$FRESP" = "$SRESP" ]; then
            TMP_COMLIST="${TMP_COMLIST} $SYS $FRESP"
         fi
      elif [ "$ACTION" = "DELETE" ]; then
         #TODO we may need add a warning here is this 
         #is RW and OPTION=localhost

         #Remove the system and the com string from the list
         TMP_COMLIST=`echo "${TMP_COMLIST}" | sed -e "s/$OPTION [a-zA-Z0-9]*[ ]*//"`

         #check to see if there are anymore entries
         [ -z "$TMP_COMLIST" ] && DONE="YES"
         continue  #need to go back to the previous screen
      fi 

   done 

   # if they pressed DONE then they are accepting the changes
   if [ "$DONE" = "YES" ]; then
    
      #strip off any leading spaces, just to be neat
      TMP_COMLIST=`echo $TMP_COMLIST  | sed -e 's/^[ ]*//'`

      case $1 in
         RW)
            RWCOMLIST="$TMP_COMLIST"
         ;;
         RO)
            ROCOMLIST="$TMP_COMLIST"
         ;;
         Trap)
            TRAPCOMLIST="$TMP_COMLIST"
         ;;
       esac 
   fi
   
   return 0
}

###################################################################
#GetSystemContact
# inputs: none
# outputs: SYSCONTACT will be populated with the user contact info
###################################################################
GetSystemContact()
{
  if [ ! -z "$SYSCONTACT" ]; then
     $DIALOG --clear  --title "$SYSCONTACT_TITLE"  --yesno "The SNMP system contact information is already set to:\n\n$SYSCONTACT\n\nDo you wish to change this configuration? $ESC_MSG" $ROWS $COLS
     RET=$?
     [ $RET = 1 ] && return 0    # the answer was no
     [ $RET = 255 ] && exit 255 
  fi
 
  $DIALOG  --title "$SYSCONTACT_TITLE" --clear \
            --inputbox "$SYSCONTACT_MSG" $ROWS $COLS 2> $tempfile
  RET=$?
  TMP_CON=`cat $tempfile`
  [ $RET -eq 255 ] && exit 255 
  [ $RET -eq 0 ] && SYSCONTACT="$TMP_CON" 

  return 0
}

###################################################################
#GetSystemLocation
# inputs: none
# outputs: SYSLOCATION will be populated with the user contact info
###################################################################
GetSystemLocation()
{
   if [ ! -z "$SYSLOCATION" ]; then
      $DIALOG --clear  --title "$SYSLOCATION_TITLE"  --yesno "The SNMP system location information is already set to:\n\n$SYSLOCATION\n\nDo you wish to change this configuration? $ESC_MSG" $ROWS $COLS
      RET=$?
      [ $RET = 1 ] && return 0    # the answer was no
      [ $RET = 255 ] && exit 255 
   fi
 
   $DIALOG  --title "$SYSLOCATION_TITLE" --clear \
             --inputbox "$SYSLOCATION_MSG" $ROWS $COLS 2> $tempfile
   RET=$?
   TMP_LOC=`cat $tempfile`
   [ $RET -eq 255 ] && exit 255 
   [ $RET -eq 0 ] && SYSLOCATION="$TMP_LOC"
 
   return 0
}

################################################################
#
################################################################
GetAgentConfig()
{
   CHECKLIST=""

   #Construct the current config
   for a in $AGENTS_LIST; do
      TMP_ONOFF="on"
      [ -n "`echo $AGENTS_EXCLUDED | grep $a 2>/dev/null`" ] && TMP_ONOFF="off"
      CHECKLIST="$CHECKLIST \"$a\" `echo -e $AGENTS_DESC | grep $a | sed -e "s/$a -//"` $TMP_ONOFF"
   done
 

   CMD="$DIALOG --clear  --title \"$AGENTS_TITLE\"  --checklist \"$AGENTS_MSG\" $ROWS $COLS 10 $CHECKLIST 2>$tempfile"
   eval $CMD
   RET=$?
   [ $RET -eq 255 ] && exit 255 

   if [ $RET -eq 0 ]; then
      AGNTSELECTED=`cat $tempfile`
      rm $tempfile
      AGENTS_EXCLUDED=""
      for a in $AGENTS_LIST; do
         #if this agent is NOT in the AGENTSSELECTED list then 
         #put it on the excluded list
   
         [ -z "`echo $AGNTSELECTED | grep $a 2>/dev/null`" ] && AGENTS_EXCLUDED="$AGENTS_EXCLUDED $a"
      done

      #strip off any leading spaces, just to be neat
      AGENTS_EXCLUDED=`echo $AGENTS_EXCLUDED | sed -e 's/^[ ]*//'`
   fi
   
   return 0
}

###################################################################
# Set the front end variables to reflex the current configuration
###################################################################
setFrontEndVariables()
{
#   SNMPCONFIG="YES"
#   RWCOMLIST="localhost $LHDEFCOMSTR"
#ROCOMLIST=""
#TRAPCOMLIST=""
#SYSCONTACT=""
#SYSLOCATION=""
#AGENTS_EXCLUDED=""
}


###################################MAIN############################

. /opt/compaq/hpasm/etc/config-handler


# Get pre-config setting if any
getPreConfigSettings

# Set the internal vars to reflex the exist configuration
setvariables

# Get the screen size
ROWS=`stty size| cut -d ' ' -f1`
COLS=`stty size| cut -d ' ' -f2`

ROWS=`expr $ROWS - 4`
COLS=`expr $COLS - 4`

if [ $ROWS -lt 5 -o $COLS -lt 10 ]; then
   echo "Too small for screen"
   exit 0
fi

[ $ROWS -gt 21 ] && ROWS=21 
[ $COLS -gt 80 ] && COLS=80 

MGMT_DEVICE_PRESENT="NO"
for d in $DEVICES; do
   if [ ! -z "`cat /proc/bus/pci/devices | grep -e $d`" ]; then
     MGMT_DEVICE_PRESENT = "YES"
     break
   fi
done

if [ $MGMT_DEVICE_PRESENT = "NO" ]; then
   echo "************************************************"
   echo "* WARNING: TODO: No hp management device present"
   echo "************************************************"
   exit -1
fi

#Put up the Welcome screen
$DIALOG --clear --ok-label "Continue" --title "$TITLE" --msgbox "$WELCOME_MSG" $ROWS $COLS
RET=$?
[ $RET -eq 255 ] && exit 255 

if [ SNMP_SUITABLE = "NO" ]; then
   echo "*******************************************"
   echo "* WARNING: TODO: SNMP not suitable"
   echo "*******************************************"
   exit -1;
fi

#Do you care if we "taint" your kernel
$DIALOG --clear --title "$TITLE" --yesno "$GPL_MSG" $ROWS $COLS
RET=$?
[ $RET -eq 1 ] && NO_TAINTED_KERNEL="YES"
[ $RET -eq 255 ] && exit 255 


if [ $SNMP_SUITABLE = "YES" ]; then

   #Do you want hp's SNMP agents
   $DIALOG --clear --title "$TITLE" --yesno "$SNMP_AGENTS_MSG" $ROWS $COLS
   RET=$?
   [ $RET -eq 1 ] && SNMPCONFIG="NO"
   [ $RET -eq 255 ] && exit 255 

   if [ $SNMPCONFIG = "YES" ]; then
      while true; 
      do
         #SNMP agents need to be configured
         $DIALOG --clear  --help-button --help-label "Finish" \
            --title "$TITLE"  --menu "$AGENT_CONFIG_MSG" $ROWS $COLS 7 \
            "Agents"  "Select the desired data gathering agents (default all)" \
            "Read/Write" "Configure SNMP Read/Write community (Default localhost)" \
            "ReadOnly"  "Configure SNMP Read Only community (No Default)" \
            "Trap" "Configure SNMP trapsink community (No Default)" \
            "Contact" "Set SNMP system contact (optional)" \
            "Location" "Set the SNMP system location (optional)" 2> $tempfile
            RET=$?
            OPTION=`cat $tempfile`
            [ $RET -eq 1 ] && break 
            [ $RET -eq 255 ] && exit 255 
            [ ! -z "`echo $OPTION | grep HELP`" ] && SNMPDONE="YES" && break
         
         case "$OPTION" in
            Agents)
               GetAgentConfig
               echo "Agents"
            ;;
            Read/Write)
               GetCommunity "RW"
               echo "R/W"
            ;;
            ReadOnly)
               GetCommunity "RO"
               echo "ReadOnly"
            ;;
            Trap)
               GetCommunity "Trap"
               echo "Trap"
            ;;
            Contact)
               GetSystemContact
               echo "SysContact"
            ;;
            Location)
               GetSystemLocation
               echo "SystenLocation"
            ;;
         esac
      done
   fi
fi
echo "NO_TAINTED_KERNEL=$NO_TAINTED_KERNEL SNMPCONFIG=$SNMPCONFIG RWCOMLIST=$RWCOMLIST ROCOMLIST=$ROCOMLIST TRAPCOMLIST=$TRAPCOMLIST SYSCONTACT=$SYSCONTACT SYSLOCATION=$SYSLOCATION AGENTS_EXCLUDED=$AGENTS_EXCLUDED"
