#!/bin/ksh
#
# Explorer Version:
EXP_VERSION=4.0

# Get and set the EXP_HOME and EXP_LIB variables
EXP_HOME=`pkginfo -r SUNWexplo.\*`
EXP_LIB=${EXP_HOME}/lib
EXP_ETC=/etc/opt/SUNWexplo

# Source tools functions
. ${EXP_LIB}/exp_tools


typeset -i term_cols
typeset -i term_lines
typeset -i top_response

term_lines=`tput lines`
term_cols=`tput cols`
(( top_response = (( $term_lines / 3 )) * 2 ))


# Screen Tools
clear_screen () {

	# Clear the dynamic area of the screen, leaving the top 3 lines and bottom 3 lines...
	(( v_pos = 0 ))
	(( h_pos = 0 ))
	position_cursor
	term_counter=3
	(( bottom = (( $term_cols - 3 )) ))
	while (( $term_counter < $bottom ))
	do
		tput cup $term_counter $h_pos
		clear_line
		(( term_counter = term_counter + 1 ))
	done

}

clear_response () {

	# Clear the response area of the screen: 
	(( v_pos = $top_response ))
	(( h_pos = 0 ))
	position_cursor
	term_counter=$v_pos
	(( bottom = $term_lines ))
	while (( $term_counter < $bottom ))
	do
		tput cup $term_counter $h_pos
		clear_line
		(( term_counter = term_counter + 1 ))
	done

}

screen_header () {

	# Clear the screen, start fresh!
	clear
	TEXT=$1
	TEXT2=$2
	line_length=`print ${#TEXT}`
	horiz_center
	(( v_pos = 0 ))
	position_cursor
	print ${TEXT}
	line_length=`print ${#TEXT2}`
	horiz_center
	line_increment 1
	position_cursor
	print ${TEXT2}

}

vert_center () {

	(( v_pos = (( $term_lines / 2 )) ))

}

horiz_center () {

	(( h_pos = (( $term_cols - $line_length )) / 2 ))

}

screen_footer () {

	(( v_pos = (( $term_lines - 2 )) ))

}

line_increment () {

	(( v_pos = (( v_pos + $1 )) ))

}

line_decrement () {

	(( v_pos = (( v_pos - $1 )) ))
	
}

clear_below () {
	
	tput ed

}

clear_line () {
	
	tput el

}

position_cursor () {
	
	tput cup $v_pos $h_pos

}

bold () {
	
	tput bold

}

init_scr () {
	
	tput sgr0

}

press_return () {
	screen_footer
	(( h_pos = 0 ))
	position_cursor
	clear_line
	TEXT="Press Return to Continue..."
  	line_length=`print ${#TEXT}`
	horiz_center
	position_cursor
	print -n ${TEXT}
	read ENTER

}

#
check_email_format () {
#   Test email addrss format
#
#   Return Codes:
#	10	Email address needs a @ in it.
#	20	RECIPIENT is zero length.
#	30 	DOMAINMANE is zero length.
#	31	Domain name ends in a "." .
#	32	Domain name does not have a ".".
#	33	Domain name contains a ".." string.
#	

CHECK_EMAIL=$1

if [[ -f /var/tmp/check_email ]]
  then /usr/bin/rm /var/tmp/check_email
fi

if [[ -f /var/tmp/check_email ]]
  then /usr/bin/rm /var/tmp/recipient_file
fi

if [[ -f /var/tmp/check_email ]]
  then /usr/bin/rm /var/tmp/domainname_file
fi

echo $CHECK_EMAIL > /var/tmp/check_email

#  Test for "@" in the email address
egrep -s -e  @ /var/tmp/check_email > /dev/null
if [[ ${?} != 0 ]]
  then
  clear_response
  horiz_center
  position_cursor
  print "Email address needs a @ in it."
  press_return
  clear_response
  return 10
fi

#  Parse the email address with @ as the delimiter
#  Place parts into separate files

RECIPIENT=`cut -d@ -f1 /var/tmp/check_email`
echo ${RECIPIENT} > /var/tmp/recipient_file

DOMAINMANE=`cut -d@ -f2 /var/tmp/check_email`
echo ${DOMAINMANE} > /var/tmp/domainname_file

# Test length of expected parts of the email address
# Each must be non-zero in length
if [[ -z $RECIPIENT ]]
  then
  clear_response
  horiz_center
  position_cursor
  print "RECIPIENT is zero length."
  press_return
  clear_response
  return 20
fi

if [[ -z $DOMAINMANE ]]
  then
  clear_response
  horiz_center
  position_cursor
  print "DOMAINMANE is zero length."
  press_return
  clear_response
  return 30
fi

# Check domain name format for common errors
# Check for a decimal in domain name
# Check for a single decimal point in domain name
# Check that a decimal is not the last character
# Check that multiple decimals are not consecutive

egrep -s -e '\.' /var/tmp/domainname_file  > /dev/null
if [[ ${?} = 0 ]]
  then
  egrep -s -e '\.\.' /var/tmp/domainname_file > /dev/null
  if [[ ${?} = 0 ]]
    then
    clear_response
    horiz_center
    position_cursor
    print "Domain name contains a .. string."
    press_return
    clear_response
    return 33
  fi
  egrep -s -e '\.$' /var/tmp/domainname_file > /dev/null
  if [[ ${?} = 0 ]]
    then
    clear_response
    horiz_center
    position_cursor
    print "Domain name ends in a ."
    press_return
    clear_response
    return 31
  fi
  else
  clear_response
  horiz_center
  position_cursor
  print "Domain name does not have a ."
  press_return
  clear_response
  return 32
fi
return 0
}
#
menu_intro1 () {
clear_screen
  (( v_pos = $term_lines / 3 ))
  (( h_pos = $term_cols / 5 ))
  TEXT01="SUN Microsystems wishes to improve service with the"
  position_cursor
  print ${TEXT01}

  line_increment 1
  TEXT02="Explorer script. One suggestion is to provide"
  position_cursor
  print ${TEXT02}

  line_increment 1
  TEXT03="notification to the client when Explorer output is"
  position_cursor
  print ${TEXT03}

  line_increment 1
  TEXT04="received by SUN.  To accomplish this, it is necessary"
  position_cursor
  print ${TEXT04}

  line_increment 1
  TEXT05="to add the EXP_NOTIFY variable to the Explorer"
  position_cursor
  print ${TEXT05}

  line_increment 1
  TEXT06="defaults file.  This variable will be populated"
  position_cursor
  print ${TEXT06}

  line_increment 1
  TEXT07="during thie initial installation of Explorer"
  position_cursor
  print ${TEXT07}

  line_increment 1
  TEXT08="on a server.  However, an upgrade of Explorer does"
  position_cursor
  print ${TEXT08}

  line_increment 1
  TEXT09="not create a new Explorer defaults file."
  position_cursor
  print ${TEXT09}

  line_increment 2
  TEXT10="This menu provides control of the EXP_NOTIFY"
  position_cursor
  print ${TEXT10}

  line_increment 1
  TEXT11="variable in the Explorer defaults file"
  position_cursor
  print ${TEXT11}

  press_return

}

menu_intro2 () {
  clear_screen
  (( v_pos = $term_lines / 3 ))
  (( h_pos = $term_cols / 4 ))
  

  TEXT01="The EXP_NOTIFY variable's inclusion in the servers"
  position_cursor
  print ${TEXT01}

  line_increment 1
  TEXT02="explorer defaults triggers an email to be sent when"
  position_cursor
  print ${TEXT02}
  
  line_increment 1
  TEXT03="the output from an Explorer run is received by Sun."
  position_cursor
  print ${TEXT03}
  
  line_increment 1
  TEXT04="One and only one email address can be assigned to the"
  position_cursor
  print ${TEXT04}
  
  line_increment 1
  TEXT05="EXP_NOTIFY variable."
  position_cursor
  print ${TEXT05}
  
  line_increment 1
  TEXT06="The absence of the EXP_NOTIFY variable from the defaults"
  position_cursor
  print ${TEXT06}
  
  line_increment 1
  TEXT07="file indicates that email notification is not desired."
  position_cursor
  print ${TEXT07}
  
  line_increment 1
  TEXT08="The Opt out option removes EXP_NOTIFY from the"
  position_cursor
  print ${TEXT08}
  
  line_increment 1
  TEXT08="explorer defaults file for this server."
  position_cursor
  print ${TEXT08}

  press_return
}

menu_select () {

while [[ $RESPONSE != END ]]
do
  (( v_pos = $term_lines - 2 ))
  (( h_pos = 0 ))
  TEXT="Select Option (1-5): "
  clear_line
  line_length=`print ${#TEXT}`
  horiz_center
  position_cursor
  print -n ${TEXT}
  read ANSWER
  case $ANSWER in
	1) add_exp_notify;;
	2) change_exp_notify;;
	3) check_exp_notify;;
	4) delete_exp_notify;;
	5) quit_menu
	   RESPONSE=END;;
	*) retry_msg;;
  esac
done

}


menu_proper () {
  clear_screen
  (( v_pos = $term_lines / 4 ))

  TEXT01="Add, Change, Display, or Opt out of EXP_NOTIFY"
  line_length=`print ${#TEXT01}`
  horiz_center
  position_cursor
  print ${TEXT01}

  (( h_pos = $term_cols / 4 ))
  line_increment 2
  TEXT02A="Option"
  position_cursor
  print ${TEXT02A}

  (( h_pos = h_pos + 10 ))
  TEXT02B="Action"
  position_cursor
  print ${TEXT02B}

  line_increment 1
  (( h_pos = (( $term_cols / 4 )) + 2 ))
  TEXT03A="1"
  position_cursor
  print ${TEXT03A}

  (( h_pos = h_pos + 4 ))
  TEXT03B="Add EXP_NOTIFY to the Explorer defaults file."
  position_cursor
  print ${TEXT03B}

  line_increment 1
  (( h_pos = (( $term_cols / 4 )) + 2 ))
  TEXT04A="2"
  position_cursor
  print ${TEXT04A}

  (( h_pos = h_pos + 4 ))
  TEXT04B="Change the value of EXP_NOTIFY."
  position_cursor
  print ${TEXT04B}

  line_increment 1
  (( h_pos = (( $term_cols / 4 )) + 2 ))
  TEXT05A="3"
  position_cursor
  print ${TEXT05A}

  (( h_pos = h_pos + 4 ))
  TEXT05B="Display current EXP_NOTIFY value."
  position_cursor
  print ${TEXT05B}

  line_increment 1
  (( h_pos = (( $term_cols / 4 )) + 2 ))
  TEXT06A="4"
  position_cursor
  print ${TEXT06A}

  (( h_pos = h_pos + 4 ))
  TEXT06B="Opt out of email notification."
  position_cursor
  print ${TEXT06B}

  line_increment 1
  (( h_pos = (( $term_cols / 4 )) + 2 ))
  TEXT07A="5"
  position_cursor
  print ${TEXT07A}

  (( h_pos = h_pos + 4 ))
  TEXT07B="Quit menu."
  position_cursor
  print ${TEXT07B}

}
#====================================================================================
#  Common functions
yorn () {
    (( v_pos = $top_response ))
    (( h_pos = 0 ))
    position_cursor
    clear_response
    horiz_center
    position_cursor

    VAR=$"$1"

    while :
    do
		# Display prompt
        (( v_pos = $top_response ))
    	(( h_pos = 0 ))
    	position_cursor
    	clear_line
    	(( h_pos = (( $term_cols / 6 )) ))
	clear_response
    	position_cursor
    	print "$2"
	line_increment 2
    	print -n "[y,n]: "
    		# Read response
    	read ANSWER

	# Acceptable responses are y|yes|n|no
	case `echo "${ANSWER}" | tr '[A-Z]' '[a-z]'` in
            yes | y)
		ANSWER=yes
		break
		;;
            no | n)
			ANSWER=no
			break
			;;
            *)
			line_increment 1
			print "Please answer y, yes, n or no"
			continue
			;;
        esac

    done

    # Set variable
    eval "$1=\"${ANSWER}\""
    clear_response
}

#====================================================================================
#  EXP_NOTIFY actions begin here
#--------------------------------------------------------
add_exp_notify () {

#  Does current Explorer defaults file contain the EXP_NOTIFY variable?
egrep -s -e "EXP_NOTIFY=" ${EXP_ETC}/default/explorer > /dev/null
if [[ ${?} = 0 ]]
  then
  clear_response
  TEXT="EXP_NOTIFY= is already defined in Explorer default file."
  line_length=`print ${#TEXT}`
  horiz_center
  position_cursor
  print "$TEXT"
  line_increment 2
  TEXT=`sed -n '/EXP_NOTIFY=/p' ${EXP_ETC}/default/explorer`
  line_length=`print ${#TEXT}`
  horiz_center
  position_cursor
  print "$TEXT"
  press_return
  clear_response
  return 50
fi
#  Request email address and perform some checking on it.
DONE=n
EXP_NOTIFY=" "

#  Assign cursor coordinates
while [ -n "$DONE" ] && expr "X$DONE" : "X[Nn]" > /dev/null
do
  clear_response
  (( v_pos = $top_response ))
  (( h_pos = (( $term_cols / 4 )) ))
  position_cursor
  print "Enter the email address to receive notification."
  line_increment 2
  position_cursor
  print -n "EXP_NOTIFY="
  read EXP_NOTIFY
  EMAIL_VALID=no
  while [[ ${EMAIL_VALID} != "yes" ]]
  do
    check_email_format $EXP_NOTIFY
    EMAIL_CHECK_RC=${?}
    if [[ $EMAIL_CHECK_RC = 0 ]]
      then EMAIL_VALID=yes
      else 
  	(( v_pos = $top_response ))
	(( h_pos = (( $term_cols / 4 )) ))
	position_cursor
	print "Enter the email address to receive notification."
	line_increment 2
	position_cursor
 	print -n "EXP_NOTIFY="
	read EXP_NOTIFY
    fi
  done
  yorn DONE "You have answered: $EXP_NOTIFY 	Is this correct?"
done

# Remove any lead or trailing double quotes from email address
EXP_NOTIFY=`echo ${EXP_NOTIFY} | sed "s/^\"*//g" | sed "s/\"*$//g"`

# Copy current Explorer defaults file for backup
cp ${EXP_ETC}/default/explorer ${EXP_ETC}/default_explore.pre_exp_notify

# Add variable to Explorer defaults file.
echo "# Customer Notify Email" >> ${EXP_ETC}/default/explorer
echo "EXP_NOTIFY=\"$EXP_NOTIFY\"" >> ${EXP_ETC}/default/explorer

(( v_pos = $top_response ))
(( h_pos = (( $term_cols / 4 )) ))
position_cursor
print "Receipt notification address is defined."
press_return
clear_response

}
#--------------------------------------------------------
change_exp_notify () {
check_exp_notify
if [[ ${?} != 0 ]]
  then
  clear_response
  TEXT="Please select a different option."
  line_length=`print ${#TEXT}`
  horiz_center
  position_cursor
  print "$TEXT"
  press_return
  clear_response
  return 60
fi
delete_exp_notify
add_exp_notify
}

#--------------------------------------------------------
check_exp_notify () {

#  Assign cursor coordinates
  (( v_pos = $top_response ))
  (( h_pos = (( $term_cols / 6 )) ))

#  Does current Explorer defaults file contain the EXP_NOTIFY variable?
egrep -s -e "EXP_NOTIFY=" ${EXP_ETC}/default/explorer > /dev/null
if [[ ${?} = 0 ]]
then
   position_cursor
   print "EXP_NOTIFY is in the Explorer defaults file."
   line_increment 2
   TEXT=`sed -n '/EXP_NOTIFY=/p' ${EXP_ETC}/default/explorer`
   position_cursor
   print ${TEXT}
   clear_below
   press_return
   clear_response
else
   position_cursor
   print "EXP_NOTIFY is not in the Explorer defaults file."
   clear_below
   press_return
   clear_response
   return 40
fi
}
#--------------------------------------------------------
delete_exp_notify () {
#  Delete EXP_NOTIFY variable from the Explorer defaults file.
sed '/EXP_NOTIFY=/d' ${EXP_ETC}/default/explorer > ${EXP_ETC}/default/explorer.wrk
sed '/# Customer Notify Email/d' ${EXP_ETC}/default/explorer.wrk > ${EXP_ETC}/default/explorer.new
cp ${EXP_ETC}/default/explorer ${EXP_ETC}/default/explorer.`date +%m%d%y%H%M%S`
mv ${EXP_ETC}/default/explorer.new ${EXP_ETC}/default/explorer
rm ${EXP_ETC}/default/explorer.wrk

(( v_pos = $top_response ))
(( h_pos = (( $term_cols / 6 )) ))
position_cursor
print "Removed EXP_NOTIFY from Explorer defaults file."
press_return
clear_response

}
#--------------------------------------------------------

retry_msg () {

#  Assign cursor coordinates
  (( v_pos = $top_response ))
  (( h_pos = (( $term_cols / 6 )) ))
   position_cursor
   TEXT="Selection invalid.  Press Return to retry."
   line_length=`print ${#TEXT}`
   horiz_center
   position_cursor
   print ${TEXT}
   press_return
   clear_response
}
#--------------------------------------------------------

quit_menu () {

#  Assign cursor coordinates
  (( v_pos = $top_response ))
# (( h_pos = (( $term_cols / 6 )) ))
   TEXT="Quitting explorer_notify_update menu"
   line_length=`print ${#TEXT}`
   horiz_center
   position_cursor
   print ${TEXT}
   line_increment 1
   clear_below
}
#--------------------------------------------------------
# Main script starts here


# explorer_notify_update must be run as root 
# Attempt to use /usr/xpg4/bin/id, otherwise use /bin/id
TEXT=`gettext "Must be run as root"`
if [ -x "/usr/xpg4/bin/id" ]
then
    require "test `/usr/xpg4/bin/id -u` -eq 0" "${TEXT}"
else
    require '/bin/id | egrep -s -e "uid=0\("' "${TEXT}"
fi

DONE=N

# Clear the Screen
clear
screen_header "Sun Microsystems" "Explorer Email Notification Update Menu"

#  Verify SUNWexplo is installed and get install path
vert_center
horiz_center
position_cursor
pkginfo -q SUNWexplo.\*
if [ ${?} = 0 ]
then
   print "Explorer is installed on `hostid`"
else
   print "The SUNWexplo package for Explorer is not installed on `hostid`"
fi

EXP_INSTALL_DIR=`pkginfo -r SUNWexplo.\*`
line_increment 3
position_cursor
print "Explorer install directory is: ${EXP_INSTALL_DIR}"
press_return

menu_intro1

menu_intro2

menu_proper

menu_select
