#!/bin/ksh -p
#
# Explorer Version:
#

# Source tools functions
. ${EXP_LIB}/exp_tools

# System installation information
# Script will accomplish three tasks.
#  1. Create file "instinfo" that contains installation information.
#  2. Run Explorer.
#  3. Copy this Explorer output to a second directory.

# Run only if specified to do so

#
# Step 1. Create instinfo file
#
set_globals () {
	# EXP_INSTINFO_OUTDIR the directory in which to place output
	EXP_INSTINFO_OUTDIR=${EXP_ETC}/instinfo

	# File containing info gathered by this script.
	EXP_INFO_TEXT_FILE=${EXP_INSTINFO_OUTDIR}/install_info

	# Some useful variables
	HOSTNAME=`uname -n 2>/dev/null`
	MARCH=`uname -m 2>/dev/null`
	PLATFORM=`/usr/platform/$MARCH/sbin/prtdiag -v | head -1`
}
###############

announce () {
   #
   # Introduce the program
   #

cat <<EOF

Explorer has been invoked with the INSTINFO option. It will:

a) Prompt for installation specific data such as installer's name.

b) Insert additional file into explorer output which when returned
   to the Sun database will trigger alerts and other actions

EOF
}

################################################
getvalue () {
#
# Matches lines in the explorer defaults
# file to the supplied string (anchored to beginning of line) 
# and extracts the remainder of the line following the first '"'.
#
	VALUE=`grep -s "^[ 	]*$1" $2 | awk -F'"' '{print $2}'`
	VALUE=${VALUE##'[ 	]*'} # Remove spaces and tabs from left
	VALUE=${VALUE%%'[ 	]*'} # Remove spaces and tabs from the right
	echo $VALUE
}

##################################################

preset_values () {
echo "preset_values started"
	#
	# Function to read in the existing explorer default file 
	# and to prompt user on whether to accept them.
	# Function also determines values for fields derived from 
	# OS commands.
	#
 
        # Collect system type from banner-name line in the prtconf -vp output
        # the line we are after looks like this:
        # banner-name:  'Sun Enterprise 420R (2 X UltraSPARC-II 450MHz)'
        #
        # First grep out *one* banner line (some prtconf have two) 
        # remove the quotes, strip out the system name (after the :) 
        # and delete from (  to end of line
        # 
        # The value is assigned to BANNER_NAME 
        
        SYSTYPE=`prtconf -vp | grep "^[ 	]*banner-name:" | head -1 | tr -d "\'"`
        
        # Check if EISCD was used and get version from release file
        if [ -f "/var/sun/.release-EIS-CD" ]
           then 
             EXP_INSTINFO_EISCD_RELEASE=`head -1 /var/sun/.release-EIS-CD`
        fi  
        if [ ! "$EXP_INSTINFO_EISCD_RELEASE" ]
         then
             EXP_INSTINFO_EISCD_RELEASE="NONE"
        fi

	CUST_BANNER_NAME=`echo ${SYSTYPE} | awk -F: '{print $2}' | awk -F\( '{print $1}'`
	EXP_INSTINFO_SYSTYPE=${CUST_BANNER_NAME}
	CUST_HOSTID=`/usr/bin/hostid`
	CUST_SYSTEM_SERIAL=`getvalue EXP_SERIAL_${CUST_HOSTID}= ${EXP_ETC}/default/explorer`  
	CUST_NAME=`getvalue EXP_CUSTOMER_NAME= ${EXP_ETC}/default/explorer`  
	CUST_ADDR_LINE1=`getvalue EXP_ADDRESS_1= ${EXP_ETC}/default/explorer`  
	CUST_ADDR_LINE2=`getvalue EXP_ADDRESS_2= ${EXP_ETC}/default/explorer`  
	CUST_CONTACT_1=`getvalue EXP_USER_NAME= ${EXP_ETC}/default/explorer`  
	CUST_PHONE_1=`getvalue EXP_PHONE= ${EXP_ETC}/default/explorer`  
	CUST_CITY=`getvalue EXP_ADDRESS_CITY= ${EXP_ETC}/default/explorer`
	CUST_POSTCODE=`getvalue EXP_ADDRESS_ZIP= ${EXP_ETC}/default/explorer`
	CUST_COUNTRY=`getvalue EXP_ADDRESS_COUNTRY= ${EXP_ETC}/default/explorer`  
	CUST_GEO=`getvalue EXP_GEO= ${EXP_ETC}/default/explorer`
	EXP_INSTINFO_SYSTEM_ENGINEER=`getvalue EXP_INSTINFO_SYSTEM_ENGINEER= ${EXP_ETC}/instinfo/install_info`
	EXP_INSTINFO_ENGINEER_EMAIL=`getvalue EXP_INSTINFO_ENGINEER_EMAIL= ${EXP_ETC}/instinfo/install_info`
	EXP_INSTINFO_SERVICE_TYPE=`getvalue EXP_INSTINFO_SERVICE_TYPE= ${EXP_ETC}/instinfo/install_info`
	EXP_INSTINFO_INSTALL_ORG=`getvalue EXP_INSTINFO_INSTALL_ORG= ${EXP_ETC}/instinfo/install_info`
	EXP_INSTINFO_INSTALL_REF=`getvalue EXP_INSTINFO_INSTALL_REF= ${EXP_ETC}/instinfo/install_info`
	EXP_INSTINFO_RESELLER_NAME=`getvalue EXP_INSTINFO_RESELLER_NAME= ${EXP_ETC}/instinfo/install_info`
	EXP_INSTINFO_SALES_ORDER=`getvalue EXP_INSTINFO_SALES_ORDER= ${EXP_ETC}/instinfo/install_info`
	EXP_INSTINFO_SALES_ORDER=`getvalue EXP_INSTINFO_SALES_ORDER= ${EXP_ETC}/instinfo/install_info`
	EXP_INSTINFO_CUST_NAME=`getvalue EXP_INSTINFO_CUST_NAME= ${EXP_ETC}/instinfo/install_info`
	EXP_INSTINFO_CUST_ADD_LINE_1=`getvalue EXP_INSTINFO_CUST_ADD_LINE_1= ${EXP_ETC}/instinfo/install_info`
	EXP_INSTINFO_CUST_ADD_LINE_2=`getvalue EXP_INSTINFO_CUST_ADD_LINE_2= ${EXP_ETC}/instinfo/install_info`
}


##############################################################
yorn () {
    VAR=$"$1"

    while :
    do
        # Display prompt
        echo "$2"
        echo "[y,n] \c"

        # 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
                ;;
            *)
                echo "Please answer y, yes, n or no"
                continue
                ;;
        esac

    done

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

##############################################################
write_preset_values() {
# writes the preset values (obtained from the explorer template file)
# to the output file which is read by other programs later.

echo "EXP_INSTINFO_HOSTID=\"${HOSTID}\""			> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_NODENAME=\"${HOSTNAME}\""			>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_ARCH=\"${MARCH}\""				>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_BANNER=\"${CUST_BANNER_NAME}\"" 		>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_EISCD_RELEASE=\"$EXP_INSTINFO_EISCD_RELEASE\"" >> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_SERVICE_TYPE=\"$EXP_INSTINFO_SERVICE_TYPE\""	>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_SYSTEM_TYPE=\"$EXP_INSTINFO_SYSTYPE\""	>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_SERIAL=\"${CUST_SYSTEM_SERIAL}\""  		>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_SYSTEM_ENGINEER=\"$EXP_INSTINFO_SYSTEM_ENGINEER\""	>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_ENGINEER_EMAIL=\"$EXP_INSTINFO_ENGINEER_EMAIL\""	>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_INSTALL_ORG=\"$EXP_INSTINFO_INSTALL_ORG\""	>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_INSTALL_REF=\"$EXP_INSTINFO_INSTALL_REF\""	>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_RESELLER_NAME=\"$EXP_INSTINFO_RESELLER_NAME\""	>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_SALES_ORDER=\"$EXP_INSTINFO_SALES_ORDER\""	>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_CUST_NAME=\"${CUST_NAME}\""  		>> $EXP_INFO_TEXT_FILE 
echo "EXP_INSTINFO_CUST_ADD_LINE_1=\"${CUST_ADDR_LINE1}\""  	>> $EXP_INFO_TEXT_FILE 
echo "EXP_INSTINFO_CUST_ADD_LINE_2=\"${CUST_ADDR_LINE2}\""   	>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_CUST_CITY=\"${CUST_CITY}\""   		>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_CUST_POSTCODE=\"${CUST_POSTCODE}\""		>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_CUST_COUNTRY=\"${CUST_COUNTRY}\""  		>> $EXP_INFO_TEXT_FILE
echo "EXP_INSTINFO_CUST_CONTACT_1=\"${CUST_CONTACT_1}\"" 	>> $EXP_INFO_TEXT_FILE 
echo "EXP_INSTINFO_CUST_PHONE_1=\"${CUST_PHONE_1}\"" 		>> $EXP_INFO_TEXT_FILE 

# if the machine is an ssp, write out its hostname.
if [ -f /etc/ssphostname ]
then
	SSP_HOSTNAME=`cat /etc/ssphostname`
	echo "EXP_INSTINFO_SSP_HOSTNAME=$SSP_HOSTNAME"    	>> $EXP_INFO_TEXT_FILE
fi
}

################################################

echoe () {
      #
      #  Echo string if non-null
      #  Args: string, [prefix]
      #
	case X$1 in
	X)	;;
	*)	echo "    $2$1" ;;
	esac
}

################################################
ask () {
#  Prompt for input, supplying default value.
#  Args: variable, prompt, [default]

	while true
	do
		echo "$2 [$3]: \c"
		read ans
		case X$ans in
		X\?)	help ;;
		X)	eval $1="\"$3\""   ; break ;;
		X-)	eval $1=""         ; break ;;
		X*)	eval $1="\"$ans\"" ; break ;;
		esac
	done
}

################################################

errexit ()
{
       #
       # This function sets ERRVAL to be used as the exit value of the
       # program prints an error message supplied as $1 and then 
       # exits Explorer and instinfo
       #
        ERRVAL=1
	echo ERROR: "$1"
        exit 
}

################################################

collect_info () {
	#
	# Prompt the user for specific information assigning to 
        # variables 
	# checks validity of some answers

        while true
	do
          PS3="Type number: "
	  echo "\n\nWhat type of installation is this?\n"
          select installtype in DeskStart "ServerStart only" "ArrayStart only" "ServerStart+ArrayStart" "SunCluster" 
          do
            case "$installtype" in
              DeskStart) EXP_INSTINFO_SERVICE_TYPE=DeskStart ; break  ;;
              "ServerStart only") EXP_INSTINFO_SERVICE_TYPE=ServerStart ; break ;;
              "ArrayStart only") EXP_INSTINFO_SERVICE_TYPE=ArrayStart ; break ;;
              "ServerStart+ArrayStart") EXP_INSTINFO_SERVICE_TYPE=ServerStart+ArrayStart ; break ;;
              "SunCluster") EXP_INSTINFO_SERVICE_TYPE=SunCluster ; break ;;
              Other) EXP_INSTINFO_SERVICE_TYPE=Other ; break ;;
              * ) echo "Invalid selection"
            esac
          done

	echo "\n"
	echo "SYSTEM INFORMATION (use ? for assistance)"
	echo "Questions marked with * must have values"
	echoe "$EXP_INSTINFO_SYSTYPE"     "System Type:      "
	echoe "$CUST_SYSTEM_SERIAL"   "Serial Number:    "
	echoe "$EXP_INSTINFO_SYSTEM_ENGINEER" "Engineer:         "
	echo

	ask EXP_INSTINFO_SYSTYPE "*System Type" "$EXP_INSTINFO_SYSTYPE"
	ask CUST_SYSTEM_SERIAL "*Serial Number" "$CUST_SYSTEM_SERIAL"
	ask EXP_INSTINFO_SYSTEM_ENGINEER "*Installing Engineer" "$EXP_INSTINFO_SYSTEM_ENGINEER"
	ask EXP_INSTINFO_ENGINEER_EMAIL "*Installing Engineers email address" "$EXP_INSTINFO_ENGINEER_EMAIL"
        echo

        yorn RESPONSE "Are you a reseller?\c "
        if [[ ${RESPONSE} = yes ]]
          then
	  EXP_INSTINFO_RESELLER_FLAG=Y
          else
	  EXP_INSTINFO_RESELLER_FLAG=N
        fi    

        if [[ "${EXP_INSTINFO_RESELLER_FLAG}" = "Y" ]]
          then 
          # if Reseller ask for their reference and Sun Sales Order
          EXP_INSTINFO_INSTALL_ORG="Reseller"
	  ask EXP_INSTINFO_RESELLER_NAME "Reseller Name" "$EXP_INSTINFO_RESELLER_NAME"
	  ask EXP_INSTINFO_INSTALL_REF "$EXP_INSTINFO_INSTALL_ORG Installation Reference" "$EXP_INSTINFO_INSTALL_REF"
	  ask EXP_INSTINFO_SALES_ORDER "Sun Sales Order" "$EXP_INSTINFO_SALES_ORDER"

          else
       	   PS3="Type number: "
	  echo "\nWhat is your organisation?\n"
          select org in "Sun Support Services" "Sun Professional Services" 
          do
            case "$org" in
              "Sun Support Services") EXP_INSTINFO_INSTALL_ORG="Sun Support Services" ; break ;;
              "Sun Professional Services") EXP_INSTINFO_INSTALL_ORG="Sun Professional Services" ; break ;;
               * ) echo "Invalid selection"

            esac
          done

          case "$EXP_INSTINFO_INSTALL_ORG" in
            "Sun Support Services")       # if Support Services ask for Radiance reference number
	        ask EXP_INSTINFO_INSTALL_REF "*Radiance case number" "$EXP_INSTINFO_INSTALL_REF"
                ;;
            "Sun Professional Services") # if SunPS ask for SunPS reference number
		ask EXP_INSTINFO_INSTALL_REF "$EXP_INSTINFO_INSTALL_ORG Installation Reference" "$EXP_INSTINFO_INSTALL_REF"
                ;;
          esac
        fi

	echo "\n"
	echo "CUSTOMER INFORMATION"
	echoe "$CUST_NAME" "Customer Name:         "
	echo

	ask CUST_NAME "*Customer Name" "$CUST_NAME"

	echo "\n"
	echo "ADDRESS INFORMATION"
	echoe "$CUST_ADDR_LINE1"
	echoe "$CUST_ADDR_LINE2"
	echoe "$CUST_CITY"
	echoe "$CUST_POSTCODE"
	echoe "$CUST_COUNTRY"
	echo

	ask CUST_CITY "*Town or City" "$CUST_CITY"
	ask CUST_ADDR_LINE1 "*Address line 1" "$CUST_ADDR_LINE1"
	ask CUST_ADDR_LINE2 "Address line 2" "$CUST_ADDR_LINE2"
	ask CUST_POSTCODE "Postcode" "$CUST_POSTCODE"
	# ask CUST_COUNTRY "Country" "$CUST_COUNTRY"

	 # Get the geo.
	 yorn RESPONSE "instinfo has detect that your geo is ${CUST_GEO}. Is this correct?"
	 if [[ "${RESPONSE}" = "no" ]] # the anwser was no 
	 then
		get_geo
	 else
		get_country
	 fi

	
	 echo "\n"
	 echo "CONTACT INFORMATION"
	 echoe "$CUST_CONTACT_1" "Contact #1:  "
	 case X$CUST_CONTACT_1 in
	 X)	;;
	 *)	echoe "$CUST_PHONE_1"   "Phone for Contact #1:  "
		echo
	 esac
	 echo

	 ask CUST_CONTACT_1 "*Contact #1" "$CUST_CONTACT_1"
	 case X$CUST_CONTACT_1 in
	 X)	CUST_PHONE_1="" ;;
	 *)	ask CUST_PHONE_1 "*Phone for $CUST_CONTACT_1" "$CUST_PHONE_1"
	 esac

         tput clear

	 echo "     **** PLEASE CHECK THE FOLLOWING DETAILS ARE CORRECT BEFORE CONTINUING ***"
	 echo
	echoe "$EXP_INSTINFO_SERVICE_TYPE"     "        Service Type: "
	echoe "$EXP_INSTINFO_SYSTYPE"      "         System Type: "
	echoe "$CUST_SYSTEM_SERIAL"    "       Serial Number: "
        echo
	echoe "$EXP_INSTINFO_INSTALL_ORG"      "Install Organisation: "
	echoe "$EXP_INSTINFO_SYSTEM_ENGINEER"  "            Engineer: "
	echoe "$EXP_INSTINFO_ENGINEER_EMAIL"       "      Engineer Email: "
	echoe "$EXP_INSTINFO_INSTALL_REF"      "    Radiance Ref: "
	echoe "$EXP_INSTINFO_RESELLER_NAME"    "       Reseller name: "
	echoe "$EXP_INSTINFO_SALES_ORDER"      "     Sun Sales Order: "
        echo
	echoe "$CUST_NAME"        "       Customer Name: "
	echoe "$CUST_ADDR_LINE1"  "      Address Line 1: "
	echoe "$CUST_ADDR_LINE2"  "      Address Line 2: "
	echoe "$CUST_CITY"        "                City: "
	echoe "$CUST_POSTCODE"    "            Postcode: "
	echoe "$CUST_COUNTRY"     "             Country: " 
        echo
	echoe "$CUST_CONTACT_1"   "        Contact Name: "
	echoe "$CUST_PHONE_1"     "     Contact Phone #: "
	 echo

	 ans=

         until [ X$ans != X ] ; do
            ask ans "Repeat, Continue or Exit? [rce]"
            case $ans in
            [rR]*)  ;;
            [eE]*)  echo "Edit cancelled. Please restart instinfo to continue"
                    clean_exit
                    ;;
            [cC]*) # check mandatory fields are defined
                     errs=
                     case X$EXP_INSTINFO_SYSTYPE in
                     X)      errs="$errs|System Type" 
                     esac

                     case X$CUST_SYSTEM_SERIAL in
                     X)      errs="$errs|Serial Number"
                     esac

                     case X$EXP_INSTINFO_SYSTEM_ENGINEER in
                     X)      errs="$errs|Installing Engineer"
                     esac

                     case X$CUST_NAME in
                     X)      errs="$errs|Customer Name"
                     esac

                     case X$CUST_ADDR_LINE1 in
                     X)      errs="$errs|Address line 1"
                     esac

                     case X$CUST_CITY in
                     X)      errs="$errs|City"
                     esac

                     case X$CUST_CONTACT_1 in
                     X)      errs="$errs|Primary Contact"
                     esac

                     case X$CUST_PHONE_1 in
                     X)      errs="$errs|Primary Contact's phone number"
                     esac

                     case "$EXP_INSTINFO_INSTALL_ORG" in
                     "Sun Support Services")
                           #
                           # detect if call reference meets 
                           # requirement of 8 digits (Radiance)
                           #

                           case X$EXP_INSTINFO_INSTALL_REF in
                 	   X) errs="$errs|Installation Reference" ;;
                       	   *)
                              # check Radiance is 8 digits
			      echo $EXP_INSTINFO_INSTALL_REF | egrep -s "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$"

			      if [ "$?" -ne 0 ] && [ ! -z "$EXP_INSTINFO_INSTALL_REF" ] 
                              then
       			        errs="$errs|Radiance call ref# must be 8 digits"
		   	      fi
                           esac
                          ;;                      
                     "Sun Professional Services")
                         case X$EXP_INSTINFO_INSTALL_REF in
                 	 X) errs="$errs|SunPS Installation Reference" ;;
                         esac
                         ;;
                     "Reseller")
                         case X$EXP_INSTINFO_RESELLER_NAME in
                 	 X) errs="$errs|Reseller Name:" ;;
                         esac

                         case X$EXP_INSTINFO_INSTALL_REF in
                 	 X) errs="$errs|Reseller Installation Reference" ;;
                         esac

                         case X$EXP_INSTINFO_SALES_ORDER in
                 	 X) errs="$errs|Sun Sales Order No:" ;;
                         esac

                         ;;
                     esac

            case X$errs in
	        X) break 2 ;;
       	        *) echo;echo "** ERROR ** Insufficient information specified"
                   echo "The following are missing:\c"
                   echo $errs | tr '|' '\012' | sed 's/^/   ==> /'
            esac
         esac
	 done
done
}

########################################################################################
get_country () {
   case "$CUST_GEO" in
      "AMERICAS" ) get_usa_country; break ;;
      "APAC" )  get_apac_country; break ;;
      "EMEA" )  get_emea_country; break ;;
	* ) errexit "Trouble identifying geo"
   esac
}
########################################################################################
get_usa_country() 
{
	 echo "\n*Country (Please enter number)\n"
	 select instinfo_cust_country in "Canada Central" "Canada East" "Canada Quebec" "Canada West" "LA Argentina" "LA Brazil" "LA Chile" "LA Colombia" "LA Mexico" "LA Venezuela" "SAS Client Account" "US Federal" "US MidAmerica" "US NorthEast" "US South" "US West" "Other Country"
         do
           case "$instinfo_cust_country" in
        	"Canada Central") CUST_COUNTRY="Canada Central" ; break ;;
        	"Canada East") CUST_COUNTRY="Canada East" ; break ;;
        	"Canada Quebec") CUST_COUNTRY="Canada Quebec" ; break ;;
        	"Canada West") CUST_COUNTRY="Canada West" ; break ;;
        	"LA Argentina") CUST_COUNTRY="LA Argentina" ; break ;;
        	"LA Brazil") CUST_COUNTRY="LA Brazil" ; break ;;
        	"LA Chile") CUST_COUNTRY="LA Chile" ; break ;;
        	"LA Colombia") CUST_COUNTRY="LA Colombia" ; break ;;
        	"LA Mexico") CUST_COUNTRY="LA Mexico" ; break ;;
        	"LA Venezuela") CUST_COUNTRY="LA Venezuela" ; break ;;
        	"SAS Client Account") CUST_COUNTRY="SAS Client Account" ; break ;;
        	"US Federal") CUST_COUNTRY="US Federal" ; break ;;
        	"US MidAmerica") CUST_COUNTRY="US MidAmerica" ; break ;;
        	"US NorthEast") CUST_COUNTRY="US NorthEast" ; break ;;
        	"US South") CUST_COUNTRY="US South" ; break ;;
        	"US West") CUST_COUNTRY="US West" ; break ;;
        	"Other Country") CUST_COUNTRY="Other Country" ; break ;;
           esac
         done
}
##############################################################
get_apac_country() 
{
	 echo "\n*Country (Please enter number)\n"
	 select instinfo_cust_country in "Australia" "China" "India" "Japan" "Korea" "Malaysia" "New Zealand" "Singapore" "Thailand" "Other Country"
         do
           case "$instinfo_cust_country" in
        	"Australia") CUST_COUNTRY="Australia" ; break ;;
        	"China") CUST_COUNTRY="China" ; break ;;
        	"India") CUST_COUNTRY="India" ; break ;;
        	"Japan") CUST_COUNTRY="Japan" ; break ;;
        	"Korea") CUST_COUNTRY="Korea" ; break ;;
        	"Malaysia") CUST_COUNTRY="Malaysia" ; break ;;
        	"New Zealand") CUST_COUNTRY="New Zealand" ; break ;;
        	"Singapore") CUST_COUNTRY="Singapore" ; break ;;
        	"Thailand") CUST_COUNTRY="Thailand" ; break ;;
        	"Other Country") CUST_COUNTRY="Other Country" ; break ;;
           esac
         done
}
##############################################################
get_emea_country() 
{
	 echo "\n*Country (Please enter number)\n"
	select instinfo_cust_country in "Belgium" "Czech Republic" "Denmark" "Dubai" "Egypt" "Finland" "France" "Germany" "Greece" "Holland" "Hungary" "Israel" "Italy" "Kazakstan" "Kuwait" "Norway" "Poland" "Portugal" "Russia" "Saudi Arabia" "Slovakia" "South Africa" "Spain" "Sweden" "Switzerland" "Turkey" "United Arab Emirates" "United Kingdom" "Other Country"
         do
	   case $instinfo_cust_country in
        	"Belgium") CUST_COUNTRY="Belgium" ; break ;;
        	"Czech Republic") CUST_COUNTRY="Czech Republic" ; break ;;
        	"Denmark") CUST_COUNTRY="Denmark" ; break ;;
        	"Dubai") CUST_COUNTRY="Dubai" ; break ;;
        	"Egypt") CUST_COUNTRY="Egypt" ; break ;;
        	"Finland") CUST_COUNTRY="Finland" ; break ;;
        	"France") CUST_COUNTRY="France" ; break ;;
        	"Germany") CUST_COUNTRY="Germany" ; break ;;
        	"Greece") CUST_COUNTRY="Greece" ; break ;;
        	"Holland") CUST_COUNTRY="Holland" ; break ;;
        	"Hungary") CUST_COUNTRY="Hungary" ; break ;;
        	"Israel") CUST_COUNTRY="Israel" ; break ;;
        	"Italy") CUST_COUNTRY="Italy" ; break ;;
        	"Kazakstan") CUST_COUNTRY="Kazakstan" ; break ;;
        	"Kuwait") CUST_COUNTRY="Kuwait" ; break ;;
        	"Norway") CUST_COUNTRY="Norway" ; break ;;
        	"Poland") CUST_COUNTRY="Poland" ; break ;;
        	"Portugal") CUST_COUNTRY="Portugal" ; break ;;
        	"Russia") CUST_COUNTRY="Russia" ; break ;;
        	"Saudi Arabia") CUST_COUNTRY="Saudi Arabia" ; break ;;
        	"Slovakia") CUST_COUNTRY="Slovakia" ; break ;;
        	"South Africa") CUST_COUNTRY="South Africa" ; break ;;
        	"Spain") CUST_COUNTRY="Spain" ; break ;;
        	"Sweden") CUST_COUNTRY="Sweden" ; break ;;
        	"Switzerland") CUST_COUNTRY="Switzerland" ; break ;;
        	"Turkey") CUST_COUNTRY="Turkey" ; break ;;
        	"United Arab emirates") CUST_COUNTRY="United Arab Emirates" ; break ;;
        	"United Kingdom") CUST_COUNTRY="UK" ; break ;;
        	"Other Country") CUST_COUNTRY="Other Country" ; break ;;
           esac
         done
}
##############################################################
get_geo () {
  # display the list of geos
  cat <<!

Please select your geo from this list -

1) AMERICAS 
2) EMEA
3) APAC

[EMEA] 
!

  # get the geo from the user ( set the default for the return key )
  read geo 
  if [ -z $geo ]
  then
        geo="EMEA"
  fi

  # validate the user input and put up the corresponding country list
  # to get the country.
  ok="no"
  while [ $ok == "no" ]
  do 
    case "$geo" in 
      "1" | "AMERICAS" ) ok="yes" ; get_usa_country;  break ;;
      "2" | "EMEA"     ) ok="yes" ; get_emea_country; break ;;
      "3" | "APAC"     ) ok="yes" ; get_apac_country; break ;;
                      *) get_geo
    esac
  done
}

##########################################################
process_platform_type()
{
	# we are going to decide whether to edit the scinput.txt file and which
	# flags to run the explorer with. First we will set the defaults.
	EDIT_SCINPUT_FILE="false"
	EXP_INSTINFO_COMMAND="${EXP_HOME}/bin/explorer -q -f ${INFO_TEXT_FILE} "

	# now we will check the sun fire machines.
	echo $PLATFORM | grep 'Sun Fire 3800' > /dev/null
	if [ $? -eq 0 ]
	then
		EDIT_SCINPUT_FILE="true"
		EXP_INSTINFO_COMMAND="${EXP_HOME}/bin/explorer -q -f ${INFO_TEXT_FILE} "
	fi

	echo $PLATFORM | grep 'Sun Fire 4800' > /dev/null
	if [ $? -eq 0 ]
	then
		EDIT_SCINPUT_FILE="true"
		EXP_INSTINFO_COMMAND="${EXP_HOME}/bin/explorer -q -f ${INFO_TEXT_FILE} "
	fi

	echo $PLATFORM | grep 'Sun Fire 4810' > /dev/null
	if [ $? -eq 0 ]
	then
		EDIT_SCINPUT_FILE="true"
		EXP_INSTINFO_COMMAND="${EXP_HOME}/bin/explorer -q -f ${INFO_TEXT_FILE} "
	fi

	echo $PLATFORM | grep 'Sun Fire 6800' > /dev/null
	if [ $? -eq 0 ]
	then
		EDIT_SCINPUT_FILE="true"
		EXP_INSTINFO_COMMAND="${EXP_HOME}/bin/explorer -q -f ${INFO_TEXT_FILE} "
	fi

	echo $PLATFORM | grep 'Sun Fire 15000' > /dev/null
	if [ $? -eq 0 ]
	then
		EDIT_SCINPUT_FILE="false"
		EXP_INSTINFO_COMMAND="${EXP_HOME}/bin/explorer -q -f ${INFO_TEXT_FILE} "
	fi

	echo $PLATFORM | grep 'CP 1500' > /dev/null
	if [ $? -eq 0 ]
	then
		EDIT_SCINPUT_FILE="false"
		EXP_INSTINFO_COMMAND="${EXP_HOME}/bin/explorer -q -f ${INFO_TEXT_FILE} "
	fi


	# now we'll edit the scinput.txt file if necessary.
	if [ ${EDIT_SCINPUT_FILE} == "true" ]   
	then
		initialise_scinput_file
		NO_MORE_SSP_CONTROLLERS=false
		loop_count=0
		while true
		do
			(( loop_count = $loop_count + 1 ))
			get_sc_hostname  
			if [ $NO_MORE_SSP_CONTROLLERS == "true" ] 
			then
				break
			fi
			get_sc_password
			update_scinput_file
			if [ $loop_count == 2 ]
			then
				break
			fi
		done
	fi	

	## set correct permissions of ${EXP_ETC}/scinput.txt
	## just to be sure.
	if [ -f ${EXP_ETC}/scinput.txt ]
	then
		chmod 600 ${EXP_ETC}/scinput.txt
	fi
}
########################################################################################
initialise_scinput_file() {
	echo "Instinfo has detected that this is a Sun Fire."
	echo
	# if ${EXP_ETC}/scinput.txt isnt there, create it.
	if [ ! -f ${EXP_ETC}/scinput.txt ]
	then
		echo "Creating ${EXP_ETC}/scinput.txt ..."
		touch ${EXP_ETC}/scinput.txt
		echo "# Input file for extended data collection" >> ${EXP_ETC}/scinput.txt
		echo "# Format is HOST PASSWORD"  >> ${EXP_ETC}/scinput.txt
	fi
}
########################################################################################
get_sc_hostname() {
# keep requesting hostname until you get
# one that responds to a ping request
validhost=false
while [ $validhost == "false" ] 
do
	echo "Enter hostname for controller (press return if there are no more controllers.)"
       	read host_name
	if [ -z $host_name ]
	then
		NO_MORE_SSP_CONTROLLERS=true
		break
	fi
       	/usr/sbin/ping "$host_name" > /dev/null
       	if [ $? -ne 0 ]
       	then
		echo
       		echo "Host $host_name not responding to ping requests. Please enter a valid hostname."
       	else
       		validhost=true 
       	fi
done
}
########################################################################################
get_sc_password() {
# request password for that host until they enter it the same way twice
validpass=false
while [ $validpass == "false" ]
do
	echo "Please enter root password for $host_name"
	/usr/bin/stty -echo
	read pass_first
	/usr/bin/stty echo
	echo "Please re-enter password for $host_name to confirm"
	/usr/bin/stty -echo
	read pass_second
	/usr/bin/stty echo
	if [ $pass_first == $pass_second ] 
	then
		validpass=true
	else
		echo
		echo "The passwords were not the same. Please try again."
	fi
done
pass=$pass_second
}
#######################################################################################
update_sc_inputfile() {
# see if the string "$host $pass" is in etc/scinput.txt file
grep -s "^$host _name" ${EXPLORER_DIR}/etc/scinput.txt | grep -s " $pass$" > /dev/null
if [ $? -eq 1 ]
then
	grep -s  "^$host_name " ${EXPLORER_DIR}/etc/scinput.txt > /dev/null
       	if [ $? -eq 0 ]
       	then
       		# the hostname is there but the password is different so we'll overwrite that line
		# in etc/scinput.txt.
		touch /tmp/INSTINFO.$$
		chmod 600 /tmp/INSTINFO.$$
       		grep  -v "^$host_name " ${EXPLORER_DIR}/etc/scinput.txt >> /tmp/INSTINFO.$$
       		cp /tmp/INSTINFO.$$ ${EXPLORER_DIR}/etc/scinput.txt
       		rm /tmp/INSTINFO.$$
       		echo $host_name     $pass >> ${EXPLORER_DIR}/etc/scinput.txt
       	else
		# the hostname and password arent in etc/scinput.txt so we'll
		# append them to it.
       		echo $host_name     $pass >> ${EXPLORER_DIR}/etc/scinput.txt
       	fi
fi
}
########################################################################################
#
#  Main script
#
########################################################################################

# Set variable
set_globals

# Announce the script and ask to continue
announce
TEXT="continue?"
yorn RESPONSE "${TEXT} \c"
if [[ "${RESPONSE}" = no ]]
  then
    echo "\n\nQuitting Explorer Instinfo!"
    exit 1
fi 

#
# Verify the directory for storing the initial
# Explorer output exits and is empty
#
echo "instinfo_outdir is ${EXP_INSTINFO_OUTDIR}"
if [[ ! -d ${EXP_INSTINFO_OUTDIR} ]]
then
 # if it doesn't exist, make it
 mkdir -p $EXP_INSTINFO_OUTDIR 
#else
 # if it does clear it out. 
# echo "Clearing out the instinfo output directory of files from previous run ...\n " 
# rm -rf ${EXP_INSTINFO_OUTDIR}/explorer.${HOSTID}.*
# rm -rf ${EXP_INSTINFO_OUTDIR}/install_report_html
 # and remove the info file
# rm -rf ${INFO_TEXT_FILE} 
fi
###########################################################
# Stage 1 # Collect information and write it to info file
###########################################################

# Load variables from Explorer defaults file and OS derived
# values
preset_values
echo "preset_values complete"

# prompt the user for information and assign to variables
collect_info
echo "collect_info complete"

# Determine if server is a Sun Fire and needs SSP Controller
# hostname and password stored before scextended module can run.
process_platform_type
echo $? from process platform type

# write INSTINFO variables to info file
write_preset_values
echo "write_preset_values complete"

echo "instinfo complete"

echo "Starting Explorer Data Collection"
