#!/bin/sh
#====================================================================
# File Name   : install
# Purpose     : Install InSoft Software
#
# Developed by: InSoft, Incorporated
#               (c) Copyright 1995
#
#====================================================================

# Some needed variables...
# a generic header
HEADER="===== InSoft Package Installer Version 1.55ct ================================="
# INSTALL_ALL=0 means: use menus to select packages to install
# INSTALL_ALL=1 means: automatically install all packages (no menus)
# note: can be overridden by -a and -q command line options
# note: OR when there is a "./packages" directory it will assume -q
#       unless there was a -q or -a on the command line
INSTALL_ALL=1

# KNOW24=0 means: this install script sees sol24 as mtag solaris
# KNOW24=1 means: this install script sees sol24 as a separate mtag
KNOW24=1

#
# Figure out which platform we're running on
#
generate_mtag () 
   {
   arch=`/bin/uname -s`
   #sanity
   os_vers=0
   retval=0
   if [ "$arch" = "SunOS" ]; then
      os_vers=`/bin/uname -r`
      if [ $os_vers -ge 5 ]; then
         if [ "$KNOW24" -eq 1 ]; then
            if [ "$os_vers" = "5.3" ]; then
               mtag=solaris
            else
               mtag=sol24
            fi
         else
            mtag=solaris
         fi
         os_vers=5
      else
         mtag=sunos
         os_vers=4
      fi
   elif [ "$arch" = "HP-UX" ]; then
      mtag=hpux
   elif [ "$arch" = "OSF1" ]; then
      mtag=axp
   elif [ "$arch" = "AIX" ]; then
      mtag=aix
   elif [ "$arch" = "IRIX" ]; then
      mtag=irix
   else
      retval=1
   fi
   return $retval
   }

#
# Set up default path for this shell
#
setup_path () {
   PATH="/bin"
   if [ -d "/usr/bin" ]; then
      PATH="$PATH:/usr/bin"
   fi
   if [ -d "/usr/ucb" ]; then
      PATH="$PATH:/usr/ucb"
   fi
   if [ -d "/usr/bsd" ]; then
      PATH="$PATH:/usr/bsd"
   fi ; }

#
# Make sure we use the right executables for each architecture
#
setup_arch_cmds () {
   case "`echo 'x\c'`" in
      'x\c')  necho="echo -n"      nnl="" ;;         # BSD
      x)      necho="echo"         nnl="\c" ;;       # SYSV
      *)      necho="echo -n"      nnl="" ;;         # BSD
   esac

   case "$mtag" in
     sunos )   ps="/bin/ps -auxww"
               du="/usr/5bin/du"
               df="/bin/df" ;;
     solaris ) ps="/bin/ps -ea"
               du="/bin/du"
               df="/bin/df -k" ;;
     hpux )    ps="/bin/ps -ea"
               du="/bin/du"
               df="/usr/bin/bdf" ;;
     hpux10 )  ps="/bin/ps -ea"
               du="/bin/du"
               df="/usr/bin/bdf" ;;
     axp )     ps="/bin/ps -ea"
               du="/bin/du"
               df="/bin/df -k" ;;
     aix )     ps="/bin/ps -ea"
               du="/bin/du"
               if [ `uname -v` = 4 ] ; then
                  df="/bin/df -k"
               else
                  df="/bin/df"
               fi ;;
     irix )    ps="/bin/ps -ea"
               du="/bin/du"
               df="/usr/sbin/df -k" ;;
   esac ; }

#
# The normal exit routine
#
exit_good () {
   stop_spin
   echo ""
   echo "InSoft Package Installer Exiting."
   exit 0 ; }

#
# The "bad" exit routine
#
exit_bad () {
   stop_spin
   echo ""
   echo "InSoft Package Installer Aborting."
   exit 1 ; }

#
# The user aborted exit routine
#
exit_user () {
   stop_spin
   echo ""
   echo "InSoft Package Installer Aborted by User."
   exit 1 ; }

#
# Check for Quit
#
check_quit () {
   if [ "`echo $1 | cut -c1`" = "Q" -o "`echo $1 | cut -c1`" = "q" ]; then
      exit_user
   fi ; }

#
# Resolves a directory path
# Note: This does not permanently change the current directory, it only
#       attempts to discover the real path name of the incoming string.
# Note: Make sure that the function is called like: val=`resolve_path "$path"`,
#       just in case there are any spaces in the path.
# in:  $1 = path to resolve
# out: 0  = success (resolved path is returned)
#      1  = failure (original path is returned)
#
resolve_path()
   { (
   savepath="`pwd`"
   if [ -d "$1" ]; then
      cd "$1" > /dev/null 2>&1
      if [ $? -eq 0 ]; then
         echo "`pwd`"
         cd "$savepath"
         return 0
      fi
   fi
   echo "$1"
   cd "$savepath"
   return 1
   ) }

#
# Try to figure out where the product is loaded.
#
find_install() {
   if [ -d "$COMM_HOME/OpenDVE" ]; then
      DEFAULT_LOC="$COMM_HOME"
   elif [ -d "/opt/insoft/$PRODUCT_DIR/OpenDVE" ]; then
      DEFAULT_LOC="/opt/insoft/$PRODUCT_DIR"
   elif [ -d "/home/insoft/$PRODUCT_DIR/OpenDVE" ]; then
      DEFAULT_LOC="/home/insoft/$PRODUCT_DIR"
   elif [ -d "/usr/insoft/$PRODUCT_DIR/OpenDVE" ]; then
      DEFAULT_LOC="/usr/insoft/$PRODUCT_DIR"
   elif [ -d "/user/insoft/$PRODUCT_DIR/OpenDVE" ]; then
      DEFAULT_LOC="/user/insoft/$PRODUCT_DIR"
   else
      DEFAULT_LOC="/opt/insoft/$PRODUCT_DIR"
   fi } ;

#
# adjusts the filename to match the CD filesystem
#
# $1 = filename to adjust
# returns the new value
fix_filename() {
   if [ $UPPER -eq 1 ]; then
      FFN=`echo "$1" | tr "[a-z]" "[A-Z]"`
   else
      FFN=$1
   fi
   echo "$FFN$POST" ; }

#
# adjusts a directory name to match the CD filesystem
#
# $1 = dirname to adjust
# returns new value
fix_dirname() {
   if [ $UPPER -eq 1 ]; then
      echo "$1" | tr "[a-z]" "[A-Z]"
   else
      echo "$1"
   fi ; }

#
# Print header
#
print_header() {
cat << EOF
$HEADER
EOF
}

#
# returns a textual description for the os, long style
#
get_os_long_string() {
   case "$1" in
      sunos)   echo "Sun SPARCstation running Solaris 1" ;;
      solaris) if [ "$KNOW24" -eq  1 ]; then
                  echo "Sun SPARCstation running Solaris 2.3"
               else
                  echo "Sun SPARCstation running Solaris 2"
               fi ;;
      sol24)   echo "Sun SPARCstation running Solaris 2.4" ;;
      hpux)    echo "HP 9000 Series 700 running HP-UX 9" ;;
      hpux10)  echo "HP 9000 Series 700 running HP-UX 10" ;;
      axp)     echo "Digital Alpha AXP running Digital Unix" ;;
      aix)     echo "RS/6000 compatible running AIX" ;;
      irix)    echo "SGI running IRIX" ;;
   esac ; }

#
# returns a textual description for the os, short style
#
get_os_short_string() {
   case "$1" in
      sunos)   echo "Solaris 1" ;;
      solaris) if [ "$KNOW24" -eq  1 ]; then
                  echo "Solaris 2.3"
               else
                  echo "Solaris 2"
               fi ;;
      sol24)   echo "Solaris 2.4" ;;
      hpux)    echo "HP-UX 9" ;;
      hpux10)  echo "HP-UX 10" ;;
      axp)     echo "Digital Unix" ;;
      aix)     echo "AIX" ;;
      irix)    echo "IRIX" ;;
   esac ; }

#
# Find out how much space (in KB) is on the device where dir is
# $df must be set so that it returns KB results
# $1 = the directory path to use
check_space () { (
   dir="$1"
   last=""
   if [ -d "$dir" ]; then
      list=`$df $dir | grep '[0-9]%'`
      for TOKEN in $list
      do
         filter=`echo "$TOKEN" | sed -n 's/\(.%\)/\1/p'`
         if [ ! -z "$filter" ]; then
            echo "$last"
            return 0
         fi
         last="$TOKEN"
      done
   fi
   return 1 ; ) }

#
# echo the size in bytes of the specified file
# $1 = filename
get_file_size()
   { (
   if [ ! -f "$1" ]; then
      return 1
   fi
   # sunos /bin/ls doesn't show group
   if [ "$mtag" = "sunos" ]; then
      file_size=`/bin/ls -al $1 | awk '{ print $4 }'`
   else
      file_size=`/bin/ls -al $1 | awk '{ print $5 }'`
   fi
   echo "$file_size"
   return 0 
   ) }

#
# MENU_TITLE = header title
# MENU_HEADER = goes above MENU_TITLE if set, otherwise a space is added
# MENU_LINES = number of entries
# MENU_ITEMS_[x] = menu entry for [x]
# MENU_SIZE_[x] = size for menu entry [x]
# MENU_SIZE_COMPUTE = 0 don't compute, 1 compute
# MENU_SIZE_DIR = dirname of selected partition, if computing
# MENU_SIZE_DISK = available disk space in selected partition, if computing
# MENU_SIZE_TAG = tag to use if computing, like "MB" (partially used)
# MENU_SEL_VAR = array variable to use in the selection process that is set
#                for the return function. 
#                0 means NOT selected, 1 is selected.
#                2 or anything else means "Required" and is not changable
#                IMPORTANT: The caller must set up these values.
# MENU_INFO_CALL = the name of the function to call when processing an 
#                  info request from the user
# MENU_ITEMS_SELECTED = the final number of selected (and required) items
menu() 
   {
   $necho "Working...$nnl"
   start_spin
   # adjust line lengths for menu items and sizes
   menu_count=1
   while [ "$menu_count" -le "$MENU_LINES" ]
   do
      eval menu_tmp=\`$necho \"\$MENU_ITEMS_${menu_count}$nnl\" \| wc -c\`
      if [ "$menu_tmp" -gt 40 ]; then
         eval menu_tmp=\""\$MENU_ITEMS_${menu_count}"\"
         menu_tmp=`echo "$menu_tmp" | awk '{ printf("%-40s\n", substr($0,0,40)) }'`
      else
         eval menu_tmp=\""\$MENU_ITEMS_${menu_count}"\"
         menu_tmp=`echo "$menu_tmp" | awk '{ printf("%-40s\n", $0) }'`
      fi
      eval MENU_ITEMS_${menu_count}=\"\$menu_tmp\"
      eval menu_tmp=\""\$MENU_SIZE_${menu_count}"\"
      menu_tmp=`echo "$menu_tmp" | awk '{ printf("%4s\n", $0) }'`
      eval MENU_SIZE_${menu_count}=\"\$menu_tmp\"
      menu_count=`expr $menu_count + 1`
   done
   # compute size for all currently selected entries
   if [ "$MENU_SIZE_COMPUTE" -eq 1 ]; then
      menu_total=0
      menu_count=1
      while [ "$menu_count" -le "$MENU_LINES" ]
      do
         eval menu_tmp=\""\$MENU_SIZE_${menu_count}"\"
         menu_tmp=`echo "scale=2 ; $menu_tmp / 1000000" | bc | awk '{printf("%10s", $0)}'`
         # if too small than "abbreviate"
         if [ "$menu_tmp" = "         0" ]; then
            eval menu_size_item_${menu_count}=\""       .01"\"
         else
            eval menu_size_item_${menu_count}=\""$menu_tmp"\"
         fi
	 # add to overall total if selected
         eval menu_tmp=\""\$${MENU_SEL_VAR}${menu_count}"\"
         if [ "$menu_tmp" -eq 1 -o "$menu_tmp" -eq 2 ]; then
            eval menu_tmp=\""\$menu_size_item_${menu_count}"\"
            menu_total=`echo "scale=2; $menu_total + $menu_tmp" | bc`
         fi
         menu_count=`expr $menu_count + 1`
      done
   fi
   # limit menu title to 52 chars and header to 70
   MENU_TITLE=`echo "$MENU_TITLE" | awk '{printf("%-52s", substr($0,0,52))}'`
   MENU_HEADER=`echo "$MENU_HEADER" | awk '{printf("%-70s", substr($0,0,70))}'`
   # calculate total lines for size listings, limit length if necessary
   if [ "$MENU_SIZE_COMPUTE" -eq 1 ]; then
      menu_spacer=`$necho "$MENU_SIZE_DIR$nnl" | wc -c`
      if [ "$menu_spacer" -gt 45 ]; then
         MENU_SIZE_DIR="... `echo "$MENU_SIZE_DIR" | awk '{print substr($0,(length-40))}'"
         menu_spacer=45
      fi
      if [ ! -z "$MENU_DIR_DIR" ]; then
         eval menu_space_line=\`echo \"\" \| awk \'\{printf\(\"Available Space in \[${MENU_SIZE_DIR}\] : %3s ${MENU_SIZE_TAG}\\\n\", ${MENU_SIZE_DISK}\)\}\'\`
      fi
   fi
   # determine whether we can use "get info"
   if [ ! -z "$MENU_INFO_CALL" ]; then
      menu_tmp=`type "$MENU_INFO_CALL" | grep "not found"`
      menu_info_avail=$?
   else
      menu_info_avail=0
   fi
   stop_spin
   $necho "$nnl"

   start=1
   menu_pages=`expr \( \( $MENU_LINES - 1 \) / 10 \) + 1`
   menu_cpage=1
   no_next=0
   no_prev=0
   MENU_DONE=0
   # main loop starts here
   while [ "$MENU_DONE" -eq 0 ]
   do
      if [ "$start" -le 1 ]; then
         start=1
         no_prev=1
      else
         no_prev=0
      fi
      menu_xlines=0
      end=`expr $start + 9`
      if [ "$end" -ge "$MENU_LINES" ]; then
         menu_xlines=`expr $end - $MENU_LINES`
         end="$MENU_LINES"
         no_next=1
      else
         no_next=0
      fi
      menu_tmp=`echo "$menu_total" | sed 's/\.*//'`
      if [ "$menu_tmp" -eq 0 ]; then
         menu_total_mb=0
      else
         menu_tmp=`echo "$menu_total + 0.500" | bc`
         menu_total_mb=`echo "$menu_tmp" | sed 's/\..*//'`
         if [ "$menu_total_mb" -lt 1 ]; then
            menu_total_mb=1
         fi
      fi

      clear
      #print_header
      if [ -z "$MENU_HEADER" ]; then
         echo ""
      else
         echo "   ${MENU_HEADER}"
      fi
      echo ""
      echo "   ${MENU_TITLE}         Page $menu_cpage of $menu_pages"
      if [ "$MENU_SIZE_COMPUTE" -eq 1 ]; then
         echo "        Description                               Selected        Size"
      else
         echo "        Description                               Selected"
      fi
      echo " ------------------------------------------------------------------------------"
      menu_count=$start
      while [ "$menu_count" -le "$end" ]
      do
         # get the selected/non-selected flag
         eval menu_tmp=\""\$${MENU_SEL_VAR}${menu_count}"\"
         if [ "$menu_tmp" -eq 0 ]; then
            menu_sel="     [ ]"
         elif [ "$menu_tmp" -eq 1 ]; then
            menu_sel="     [x]"
         else
            menu_sel="Required"
         fi
         # adjust for spacing
         if [ "$menu_count" -lt 10 ]; then
            menu_tmp="  "
         elif [ "$menu_count" -lt 100 ]; then 
            menu_tmp=" "
         else 
            menu_tmp=""
         fi
         if [ "$MENU_SIZE_COMPUTE" -eq 1 ]; then
            eval echo \""   ${menu_tmp}${menu_count}) \$MENU_ITEMS_${menu_count}  $menu_sel  \$menu_size_item_${menu_count} $MENU_SIZE_TAG"\"
         else
            eval echo \""   ${menu_tmp}${menu_count}) \$MENU_ITEMS_${menu_count}  $menu_sel"\"
         fi
         menu_count=`expr $menu_count + 1`
      done

      menu_count=1
      while [ "$menu_count" -le "$menu_xlines" ]
      do
         echo ""
         menu_count=`expr $menu_count + 1`
      done
      echo ""
      
      if [ "$MENU_SIZE_COMPUTE" -eq 1 ]; then
         eval echo \"\" \| awk \'\{printf\(\"Total Size Selected%${menu_spacer}s\ \ \ : %3s ${MENU_SIZE_TAG}\\\n\", \"\", \"${menu_total_mb}\"\)\}\'
         if [ ! -z "$MENU_SIZE_DIR" ]; then
            echo "$menu_space_line"
         else
            echo ""
         fi
         echo ""
      else 
         echo ""
         echo ""
         echo ""
      fi
      $necho "[D]-Done with Selections    $nnl"
      if [ "$no_prev" -eq 0 ]; then
         echo "[P]-Prev Page  [#]-Toggle Item     [A]-Select ALL"
      else
         echo "               [#]-Toggle Item     [A]-Select ALL"
      fi
      $necho "[Q]-Quit Program  [?]-Help  $nnl"
      if [ "$no_next" -eq 0 ]; then
         $necho "[N]-Next Page  $nnl"
      else
         $necho "               $nnl"
      fi
      if [ "$menu_info_avail" -eq 1 ]; then
         echo "[I #]-Info on Item  [U]-Unselect ALL"
      else
         echo "                    [U]-Unselect ALL"
      fi
      echo ""
  
      $necho "Action: $nnl"
      read Action
      Action=`echo "$Action" | tr -d '!@#%^&*()~_=+{}|:;",.<>/\044\133\134\135\140\055'`
      AChar=`echo "$Action" | cut -c1`
   
      if [ "$AChar" = "n" -o "$AChar" = "N" ]; then
         if [ "$no_next" -eq 1 ]; then
            continue
         fi
         menu_tmp=`expr $start + 10`
         if [ "$menu_tmp" -gt "$MENU_LINES" ]; then
            continue
         fi
         menu_cpage=`expr $menu_cpage + 1`
         start=`expr $start + 10`
         continue
      elif [ "$AChar" = "p" -o "$AChar" = "P" ]; then
         if [ "$no_prev" -eq 1 ]; then
            continue
         fi
         menu_cpage=`expr $menu_cpage - 1`
         start=`expr $start - 10`
         continue
      elif [ "$AChar" = "q" -o "$AChar" = "Q" ]; then
         MENU_DONE=1
         exit_user
         break
      elif [ "$AChar" = "d" -o "$AChar" = "D" ]; then
         if [ ! -z "$MENU_SIZE_DIR" ]; then
            if [ "$MENU_SIZE_COMPUTE" -eq 1 -a "$menu_total_mb" -gt "$MENU_SIZE_DISK" ]; then
               clear
               print_header
               echo ""
               echo "WARNING: It appears that the disk you specified may not have enough free"
               echo "         space to install all the packages you have selected."
               echo ""
               echo "The directory you specified was:"
               echo "   $MENU_SIZE_DIR"
               echo "It appears to have $MENU_SIZE_DISK $MENU_SIZE_TAG of free space available"
               echo ""
               echo "The packages you selected would require $menu_total_mb $MENU_SIZE_TAG"
   
               echo ""
               echo "Although it is not recommended, you may continue. If you continue,"
               echo "however, you may not be able to fully install the software."
               echo ""
               echo "Continue?"
               $necho "  Yes, No, Quit (Y/N/Q)? [N] -> $nnl"
               read TempAnswer
               check_quit $TempAnswer
               if [ "$TempAnswer" = "" ]; then
                  TempAnswer="N"
               fi
               TempAnswer=`echo $TempAnswer | cut -c1`
               if [ "$TempAnswer" = "Y" -o "$TempAnswer" = "y" ]; then
                  MENU_DONE=1
                  break
               elif [ "$TempAnswer" = "N" -o "$TempAnswer" = "n" ]; then
                  MENU_DONE=0
                  continue
               fi
            fi
         fi
         MENU_DONE=1
         break
      elif [ "$AChar" = "i" -o "$AChar" = "I" ]; then
         if [ "$menu_info_avail" -eq 1 ]; then
            Action=`echo "$Action" | cut -c2- | tr -d " ?[a-z][A-Z]"`
            if [ "$Action" -lt 1 -o "$Action" -gt "$MENU_LINES" ]; then
               continue
            fi
            eval $MENU_INFO_CALL \""$Action\""
            continue
         fi
      elif [ "$AChar" = "a" -o "$AChar" = "A" ]; then
         menu_count=1
         menu_total=0
         while [ "$menu_count" -le "$MENU_LINES" ]
         do
            eval menu_tmp=\""\$${MENU_SEL_VAR}${menu_count}"\"
            if [ "$menu_tmp" -eq 0 ]; then
               eval ${MENU_SEL_VAR}${menu_count}=1
               if [ "$MENU_SIZE_COMPUTE" -eq 1 ]; then
                  eval menu_tmp=\""\$menu_size_item_${menu_count}"\"
                  menu_total=`echo "scale=2; $menu_total + $menu_tmp" | bc`
               fi
            fi
            menu_count=`expr $menu_count + 1`
         done
         continue
      elif [ "$AChar" = "u" -o "$AChar" = "U" ]; then
         menu_count=1
         while [ "$menu_count" -le "$MENU_LINES" ]
         do
            eval menu_tmp=\""\$${MENU_SEL_VAR}${menu_count}"\"
            if [ "$menu_tmp" -eq 1 ]; then
               eval ${MENU_SEL_VAR}${menu_count}=0
            fi
            menu_count=`expr $menu_count + 1`
         done
         menu_total=0
         continue
      elif [ "$AChar" = "?" ]; then
         clear
         print_header
         echo ""
         echo "    Action     Description"
         echo "-------------------------------------------------------------------------------"
         echo "    #          Toggle the selection of Item #"
         echo "                - Item # must be on the current screen"
         echo "                - Items marked "Required" cannot be toggled"
         echo "    I #        Get Information on Item #"
         echo ""
         echo "    P          Go to Previous Page"
         echo "    N          Go to Next Page"
         echo ""
         echo "    D          Done with Selections - go on to next screen"
         echo "    Q          Quit the Program"
         echo ""
         echo "    A          Select All Items"
         echo "                - This includes ALL items on ALL pages"
         echo "    U          Unselect All Items"
         echo "                - This includes ALL items on ALL pages"
         echo ""
         echo " Note that not all Actions may be available on all screens"
         echo ""
         $necho " Press [ENTER] to continue$nnl"
         read Action
         continue
      elif [ "$Action" -lt "$start" -o "$Action" -gt "$end" ]; then
         continue
      else 
         eval menu_tmp=\""\$${MENU_SEL_VAR}${Action}"\"
         if [ "$menu_tmp" = 2 ]; then
            continue
         elif [ "$menu_tmp" = 1 ]; then
            eval ${MENU_SEL_VAR}${Action}=0
            if [ "$MENU_SIZE_COMPUTE" -eq 1 ]; then
               eval menu_tmp=\""\$menu_size_item_${Action}"\"
               menu_total=`echo "scale=2; $menu_total - $menu_tmp" | bc`
            fi
         else
            eval ${MENU_SEL_VAR}${Action}=1
            if [ "$MENU_SIZE_COMPUTE" -eq 1 ]; then
               eval menu_tmp=\""\$menu_size_item_${Action}"\"
               menu_total=`echo "scale=2; $menu_total + $menu_tmp" | bc`
            fi
         fi
      fi
   done

   menu_count=1
   MENU_ITEMS_SELECTED=0
   MENU_SIZE_TOTAL="$menu_total"
   while [ "$menu_count" -le "$MENU_LINES" ]
   do
      eval menu_tmp=\""\$${MENU_SEL_VAR}${menu_count}"\"
      if [ "$menu_tmp" -ne 0 ]; then
         MENU_ITEMS_SELECTED=`expr $MENU_ITEMS_SELECTED + 1`
      fi
      menu_count=`expr $menu_count + 1`
   done

   unset menu_info_avail
   unset AChar
   unset Action
   unset menu_spacer
   unset menu_space_line
   unset menu_xlines
   unset menu_pages
   unset menu_cpage
   unset MENU_DONE
   unset menu_tmp
   unset menu_sel
   unset menu_total
   unset menu_count
   }

# set_group initializes an array named $1 of size $2 to the value $3
set_group()
   {
   set_group_count=1
   while [ "$set_group_count" -le "$2" ]
   do
      eval "$1${set_group_count}"=\""$3"\"
      set_group_count=`expr $set_group_count + 1`
   done

   unset set_group_count
   }

# display information on packages 
handle_package_info()
   {
   clear 
   eval handle_package_id=\""\$package_pointers_${1}"\"
   handle_package_id=`expr $handle_package_id + 0`
   print_header
   echo ""
   eval echo \""\$PACKAGE_NAME_${handle_package_id}"\"
   echo ""
   eval echo \""\$PACKAGE_ABOUT_${handle_package_id}"\"
   echo ""
   $necho "Press [ENTER] to Continue$nnl"
   read tmp
   }


#
# get the specified config line from the specified file
# $1 = file to search
# $2 = config value to find
get_info_line()
   { (
   line=`grep "^$2" "$1"`
   count=`echo "$line" | wc -c`
   if [ "$count" -le 1 ]; then
      return 1
   fi
   line=`echo "$line" | sed 's/.*=//'`
   line=`echo "$line" | sed 's/^[ 	]*//'`
   echo "$line"
   return 0
   ) }

# 
# return the number of items in the list
# $1 = variable list to count
count_list() 
   {
   eval count_list_list=\""\$$1"\"
   count_list_count=0
   for loop in $count_list_list
   do
      count_list_count=`expr $count_list_count + 1`
   done
   unset count_list_list
   echo "$count_list_count"
   return 0
   }

# 
# determine if the specified value is in the list
# $1 = variable list to search
# $2 = string to find
search_list() 
   {
   eval search_list_list=\""\$$1"\"
   for loop in $search_list_list
   do
      if [ "$loop" = "$2" ]; then
         unset search_list_list
         return 1
      fi
   done
   unset search_list_list
   return 0
   }

# 
# add the specified value to the list if it is not there already
# $1 = variable list to search
# $2 = string to add
add_to_list() 
   {
   eval add_to_list_list=\""\$$1"\"
   for loop in $add_to_list_list
   do
      if [ "$loop" = "$2" ]; then
         unset add_to_list_list
         return 1
      fi
   done
   eval "$1"=\""\$$1 $2"\"
   unset add_to_list_list
   return 0
   }

#
# remove the specified value from the list if it is there
# $1 = variable list to search
# $2 = string to remove
remove_from_list()
   {
   remove_new_list=""
   remove_result=0
   eval remove_from_list_list=\""\$$1"\"
   for loop in $remove_from_list_list
   do
      if [ "$loop" = "$2" ]; then
         remove_result=1
      else
         remove_new_list="$remove_new_list $loop"
      fi
   done
   eval "$1"=\""$remove_new_list"\"
   unset remove_new_list
   unset remove_from_list_list
   return $remove_result
   }

# checks the specified array to see whether the value exists
# returns the first element in the array which contains the value, 0 if none
# $1 = the variable array to search
# $2 = the value to find
# $3 = the number of elements in the array
check_array()
   {
   check_array_count=1
   check_array_size="$3"
   while [ "$check_array_count" -le "$check_array_size" ]
   do
      eval check_array_tmp=\""\$$1${check_array_count}"\"
      if [ "$check_array_tmp" = "$2" ]; then
         unset check_array_tmp
         unset check_array_size
         return $check_array_count
      fi
      check_array_count=`expr $check_array_count + 1`
   done
   unset check_array_tmp
   unset check_array_count
   unset check_array_size
   return 0
   }

run_spin() 
   {
   trap "STOP=1" 2 15

   result=`echo '\\\x' | wc -c`

   STOP=0
   if [ "$STOP" -eq 0 ]; then
      $necho "|$nnl"
      sleep 1
   fi
   while [ 1 ]
   do
      if [ "$STOP" -ne 1 ]; then
         $necho "/$nnl"
         sleep 1
      fi
      if [ "$STOP" -ne 1 ]; then
         $necho "-$nnl"
         sleep 1
      fi
      if [ "$STOP" -ne 1 ]; then
         if [ "$result" -eq 4 ]; then
            if [ "$necho" = "echo" ]; then
               echo '\\c'
            else
               echo -n '\'
            fi
         else
            if [ "$necho" = "echo" ]; then
               echo '\\\c'
            else
               echo -n '\\'
            fi
         fi
         sleep 1
      fi
      if [ "$STOP" -ne 1 ]; then
         $necho "|$nnl"
         sleep 1
      fi
      if [ "$STOP" -eq 1 ]; then
         $necho " $nnl"
         exit
      fi
   done
   }

start_spin() 
   {
   run_spin &
   SPIN_PID="$!"
   }

# make sure that trap 2 for the main shell is set to something 
# that calls stop_spin before exiting
stop_spin() 
   {
   kill $SPIN_PID 2> /dev/null
   wait $SPIN_PID
   }

# does nothing
do_nothing() 
   {
   return 0
   }

# return field in string
get_field()
   {
   get_field_loc="$1"
   shift
   eval echo \""\$$get_field_loc"\"
   }

# uses the strings in $1 and adds the corresponding size listings in $2
add_arch_sizes()
   {
   add_arch_string="0"
   add_next=0
   for add_arch_arch in $1
   do
      for add_arch_list in $2
      do
         if [ "$add_next" -eq 1 ]; then
            add_arch_string="$add_arch_string + $add_arch_list"
            add_next=0
            continue
         fi
         if [ "$add_arch_list" = "$add_arch_arch" ]; then
            add_next=1
         fi
      done
   done
   unset add_arch_arch
   unset add_arch_list
   unset add_next
   add_arch_string=`expr $add_arch_string`
   echo "$add_arch_string"
   }

# find out if the specified directory exists, asks user if they want to 
# create it if not, checks it for writeability, and assures that there is
# enough space in that directory
# $1 is the directory to check
# $2 is the amount to install, in MB (if 0, skip check)
# out: 0 = directory created
#      1 = user does not want to make directory or couldn't create
ask_create_dir()
   {
   ask_dir_dir="$1"
   ask_dir_size="$2"
   if [ ! -d "$InstallLoc" ]; then
      while [ 1 ]
      do
         echo ""
         echo "The directoy you specified:"
         echo "   $ask_dir_dir"
         echo "does not exist."
         echo ""
         echo "Create this directory?"
         $necho "  Yes, No, or Quit (Y/N/Q)? [Y] -> $nnl"
         read TempAnswer
         if [ "$TempAnswer" = "" ]; then
            TempAnswer="Y"
         fi
         check_quit "$TempAnswer"
         TempAnswer=`echo "$TempAnswer" | cut -c1`
         if [ "$TempAnswer" = "y" -o "$TempAnswer" = "Y" ]; then
            break
         elif [ "$TempAnswer" = "n" -o "$TempAnswer" = "N" ]; then
            return 1
         fi
      done
      echo ""
      echo "Creating $ask_dir_dir"
      mkdir -p "$ask_dir_dir" > /dev/null 2>&1
      if [ "$?" -ne 0 ]; then
         echo "An error occured while creating the directory:"
         echo "   $ask_dir_dir"
         echo ""
         echo "This could be because the directory is on a mounted file system,"
         echo "and root does not have permission to write to this remote file"
         echo "system."
         echo ""
         echo "You MUST specify a writeable directory."
         echo ""
         $necho "Press [ENTER] to Continue$nnl"
         read TempAnswer
         return 1
      fi
   else
      touch "$ask_dir_dir/install.check" > /dev/null 2>&1
      if [ "$?" -eq 1 ]; then
         echo ""
         echo "Unable to write to the directory:"
         echo "   $ask_dir_dir"
         echo ""
         echo "This could be because the directory is on a mounted file system,"
         echo "and root does not have permission to write to this remote file"
         echo "system."
         echo ""
         echo "You MUST specify a writeable directory."
         echo ""
         $necho "  Press [ENTER] to Continue$nnl"
         read TempAnswer
         return 1
         continue
      fi
      rm -f "$ask_dir_dir/install.check" > /dev/null 2>&1
   fi
   if [ "$ask_dir_size" -ne 0 ]; then
      tmp=`check_space "$ask_dir_dir"`
      tmp=`expr $tmp / 1000`
      if [ "$tmp" -lt "$ask_dir_size" ]; then
         while [ 1 ]
         do 
            echo ""
            echo "WARNING: It appears that the disk you specified may not have enough free"
            echo "         space to install all the packages you have selected."
            echo ""
            echo "The directory you specified was:"
            echo "   $ask_dir_dir"
            echo "It appears to have $tmp MB of free space available"
            echo ""
            echo "The packages you selected would require $ask_dir_size MB"
   
            echo ""
            echo "Although it is not recommended, you may continue. If you continue,"
            echo "however, you may not be able to fully install the software."
            echo ""
            echo "Continue?"
            $necho "  Yes, No, Quit (Y/N/Q)? [N] -> $nnl"
            read TempAnswer
            check_quit $TempAnswer
            if [ "$TempAnswer" = "" ]; then
               TempAnswer="N"
            fi
            TempAnswer=`echo $TempAnswer | cut -c1`
            if [ "$TempAnswer" = "Y" -o "$TempAnswer" = "y" ]; then
               return 0
            elif [ "$TempAnswer" = "N" -o "$TempAnswer" = "n" ]; then
               return 1
            fi
         done
      fi
   fi
   return 0
   }

#
# What to when we get an interrupt (usually a CTRL-C)
#
trap " exit_user " 2

##############################
# Main start here...
#

# check for the command line options:
#   -d for alternate dir locations
#   -a for install ALL (no menus)
#   -q for use menus
# note: the last -a or -q on line takes precidence

# if there is a "packages" directory in the current dir, then
# we assume -q unless there was a -q or -a on the command line
if [ -d "./packages" ]; then
   INSTALL_ALL=0
fi
   
tmp=""
SEARCH_DIR=""
for ARG in "$@"
do
   if [ "$tmp" = "-d" ]; then
      if [ -d "$ARG" ]; then
         SaveDir="`pwd`"
         cd "$ARG"
         SEARCH_DIR="`pwd`"
         cd "$SaveDir"
         unset SaveDir
      else 
         echo ""
         echo "WARNING: The directory you specified for the package locations:"
         echo "         $ARG"
         echo "         does not exist."
         exit_bad
      fi
   elif [ "$ARG" = "-a" ]; then
      INSTALL_ALL=1
   elif [ "$ARG" = "-q" ]; then
      INSTALL_ALL=0
   fi
   tmp="$ARG"
done

setup_path

SPIN_PID=65000

#
# Set up some machine/platform specific variables
#
generate_mtag
if [ -z "$mtag" ]; then
   echo ""
   echo "Unknown machine architecture."
   exit_bad
fi

#
# Get the program name and determine where we are running from
#
progname="`basename $0`"
SaveDir="`pwd`"
cd "`dirname $0`" > /dev/null 2>&1
if [ $? -eq 0 ]; then
   basepath="`pwd`"
else
   echo "ERROR: Unable to determine program location for $progname."
   exit_bad
fi
cd "$SaveDir"
unset SaveDir

# if -d wasn't specified then look in the current directory
if [ "$SEARCH_DIR" = "" ]; then
   # check to see if there are any packages in the current directory
   for prod_loop in `ls -d * 2> /dev/null`
   do
      if [ ! -d "$prod_loop" ]; then
         continue
      elif [ ! -r "$prod_loop" ]; then
         continue
      elif [ "`ls $prod_loop | wc -l 2> /dev/null`" -eq 0 ]; then
         continue
      elif [ -f "$prod_loop/info.ipi" -o -f "$prod_loop/INFO.IPI" ]; then
         SEARCH_DIR="`pwd`"
         break
      elif [ -f "$prod_loop/info.ipi;1" -o -f "$prod_loop/INFO.IPI;1" ]; then
         SEARCH_DIR="`pwd`"
         break
      else
         continue
      fi
   done
fi

# if nothing was found in the current directory, look in the packages dir
if [ "$SEARCH_DIR" = "" ]; then
   if [ -d "packages" -o -d "PACKAGES" ]; then
      if [ -d "packages" ]; then
         cd "packages"
      else
         cd "PACKAGES"
      fi
      for prod_loop in `ls -d * 2> /dev/null`
      do
         if [ ! -d "$prod_loop" ]; then
            continue
         elif [ ! -r "$prod_loop" ]; then
            continue
         elif [ "`ls $prod_loop | wc -l 2> /dev/null`" -eq 0 ]; then
            continue
         elif [ -f "$prod_loop/info.ipi" -o -f "$prod_loop/INFO.IPI" ]; then
            SEARCH_DIR="`pwd`"
            break
         elif [ -f "$prod_loop/info.ipi;1" -o -f "$prod_loop/INFO.IPI;1" ]; then
            SEARCH_DIR="`pwd`"
            break
         else
            continue
         fi
      done
   fi
fi
   
# if we didn't find anything in either directory, then warn the user 
if [ "$SEARCH_DIR" = "" ]; then
   echo ""
   echo "No products found in directory: `pwd`"
   echo "No products found in directory: `pwd`/packages"
   echo "No products found in directory: `pwd`/PACKAGES"
   echo ""
   echo "You can specify where to look for products by using:"
   echo "   $progname -d <directory>"
   exit_bad
fi

LOCALHOST=`/bin/uname -n`

setup_arch_cmds

runner=`whoami`
#if [ ! "$runner" = "root" ]; then
#   echo "You must be logged in as root to run this install script."
#   exit_bad
#fi

# set up umask in case root has their's set funny
umask 022

num_products_found=0
num_packages_found=0
num_arches_found=0
num_postinst_found=0
LOG_FILE=""
PRODUCT_LIST=""
ARCHES_LIST=""
UPPER=0
POST=""

# we should let the user know what is going on
clear
print_header
echo ""
echo "This is InSoft's package installer."
echo ""
echo "It will allow you to install any package that was included with your"
echo "product media."
echo ""
if [ "$INSTALL_ALL" -eq 0 ]; then
   echo "You will first be presented with a list of products that were found in"
   echo "the specified install directory. After selecting the products to install,"
   echo "you must then select the machine architectures to install support for."
   echo "Finally, you must choose the componants of the product to install."
   echo ""
   echo "Note that each componant may have it's own licensing requirements, and"
   echo "you should install only the componants for which you have purchased"
   echo "licenses."
else
   echo "This script will install all the packages that are present in the"
   echo "specified directory."
fi
echo ""
$necho "Press [ENTER] to Continue$nnl"
read TempAnswer

# now search for the products and packages
cd "$SEARCH_DIR"
clear
print_header
echo ""
echo "Searching for products in: $SEARCH_DIR"
echo ""
$necho "Working...$nnl"
start_spin

# search the specified directory for packages
for prod_loop in `ls -d * 2> /dev/null`
do
   if [ ! -d "$prod_loop" ]; then
      continue
   elif [ ! -r "$prod_loop" ]; then
      continue
   elif [ "`ls $prod_loop | wc -l 2> /dev/null`" -eq 0 ]; then
      continue
   elif [ -f "$prod_loop/info.ipi" ]; then
      UPPER=0
      POST=""
   elif [ -f "$prod_loop/INFO.IPI" ]; then
      UPPER=1
      POST=""
   elif [ -f "$prod_loop/info.ipi;1" ]; then
      UPPER=0
      POST=";1"
   elif [ -f "$prod_loop/INFO.IPI;1" ]; then
      UPPER=1
      POST=";1"
   else
      continue
   fi
   
   num_packages_found=`expr $num_packages_found + 1`

   # zero out all the known variables so that an old existing value
   # doesn't get set for the current package if it doesn't have that value
   MFG=""
   NAME=""
   IPFNAME=""
   VERSION=""
   REVISION=""
   RELDATE=""
   DESCRIPTION=""
   BASE_PRODUCT_TAG=""
   BASE_PRODUCT_DESC=""
   BASE_PRODUCT_SUBFILE=""
   BASE_PRODUCT_SUBDIR=""
   DEFAULT_DIR=""
   BASE_DIR=""
   SIZE=""
   PRECIDENCE=""
   DEPEND=""
   ARCHES=""
   LICENSE=""
   ABOUT=""
   CONTENTS=""
   SOURCE=""
   PREINSTALL=""
   POSTINSTALL=""

   tmp=`fix_filename info.ipi`
   . "$prod_loop/$tmp"

   eval tmp=\""$BASE_PRODUCT_TAG"\"
   check_array "PRODUCT_TAG_" "$tmp" "$num_products_found"
   result=$?
   if [ "$result" -eq 0 ]; then
      num_products_found=`expr $num_products_found + 1`
      eval PRODUCT_TAG_${num_products_found}=\""$BASE_PRODUCT_TAG"\"
      eval PRODUCT_DESC_${num_products_found}=\""$BASE_PRODUCT_DESC"\"
      eval PRODUCT_DEF_DIR_${num_products_found}=\""$DEFAULT_DIR"\"
      eval PRODUCT_BASE_DIR_${num_products_found}=\""$BASE_DIR"\"
      # set up menu entries now, also
      eval MENU_ITEMS_${num_products_found}=\""$BASE_PRODUCT_DESC"\"
      eval product_selections_${num_products_found}=1
      # add any new arches to the list
      for tmp in $ARCHES
      do
         eval add_to_list \""PRODUCT_ARCHES_${num_products_found}"\" \""$tmp"\"
      done
   else
      # add any new arches to the list
      for tmp in $ARCHES
      do
         eval add_to_list \""PRODUCT_ARCHES_${result}"\" \""$tmp"\"
      done
   fi
      
   eval PACKAGE_NAME_${num_packages_found}=\""$NAME"\"
   eval PACKAGE_PROD_TAG_${num_packages_found}=\""$BASE_PRODUCT_TAG"\"
   eval PACKAGE_DESC_${num_packages_found}=\""$BASE_PRODUCT_DESC"\"
   eval PACKAGE_IPFNAME_${num_packages_found}=\""$IPFNAME"\"
   eval PACKAGE_VERS_${num_packages_found}=\""$VERSION"\"
   eval PACKAGE_SIZE_${num_packages_found}=\""$SIZE"\"
   eval PACKAGE_PREC_${num_packages_found}=\""$PRECIDENCE"\"
   eval PACKAGE_ARCHES_${num_packages_found}=\""$ARCHES"\"
   eval PACKAGE_ABOUT_${num_packages_found}=\""$ABOUT"\"
   eval PACKAGE_DONE_${num_packages_found}=0
   eval PACKAGE_POSTINST_${num_packages_found}=\""$POSTINSTALL"\"
done
stop_spin
$necho "$nnl"

if [ "$num_products_found" -eq 0 ]; then
   echo ""
   echo "No products found in directory: $SEARCH_DIR"
   echo ""
   echo "You can specify where to look for products by using:"
   echo "   $progname -d <directory>"
   exit_bad
fi

# if user input is required
if [ "$INSTALL_ALL" -eq 0 ]; then
   # set up the rest of the menu for the "products" screen 
   MENU_TITLE="Install packages for which products:"
   MENU_HEADER="Packages for use with the following product(s) were found:"
   MENU_LINES="$num_products_found"
   MENU_SIZE_COMPUTE=0
   MENU_INFO_CALL=""
   MENU_SEL_VAR="product_selections_"
   
   LOOP_DONE=0
   while [ "$LOOP_DONE" -eq 0 ]
   do
      menu
      if [ "$MENU_ITEMS_SELECTED" -eq 0 ]; then
         echo ""
         echo "You must select at least one product to install."
         echo ""
         $necho "Press [ENTER] to Continue$nnl"
         read tmp
      else 
         LOOP_DONE=1
      fi
   done
else
   # all products selected automatically
   loop_prod_count=0
   while [ "$loop_prod_count" -lt "$num_products_found" ]
   do
      loop_prod_count=`expr $loop_prod_count + 1`
      eval product_selections_${loop_prod_count}=1
   done
fi

# product loop starts here
loop_prod_count=0
while [ "$loop_prod_count" -lt "$num_products_found" ]
do
   cd "$SEARCH_DIR"
   loop_prod_count=`expr $loop_prod_count + 1`
   # skip if we are not installing this product
   eval tmp=\""\$product_selections_${loop_prod_count}"\"
   if [ "$tmp" -eq 0 ]; then
      continue
   fi

   eval PRODUCT_CURRENT=\""\$PRODUCT_TAG_${loop_prod_count}"\"
   eval PROD_CURR_DESC=\""\$PRODUCT_DESC_${loop_prod_count}"\"
   eval PROD_DEFAULT=\""\$PRODUCT_DEF_DIR_${loop_prod_count}"\"
   eval PROD_BASE=\""\$PRODUCT_BASE_DIR_${loop_prod_count}"\"
   eval ARCHES_MENU=\""\$PRODUCT_ARCHES_${loop_prod_count}"\"

   # set up the menu for the "arches" screen 
   MENU_TITLE="Select the architectures to install support for:"
   MENU_HEADER="$PROD_CURR_DESC supports the following architectures."
   MENU_SIZE_COMPUTE=0
   MENU_INFO_CALL=""
   MENU_SEL_VAR="arches_selections_"
   MENU_LINES=0
   CORE_PRESENT=0

   for tmp in $ARCHES_MENU
   do
      # don't put core in arches menu
      if [ "$tmp" = "core" ]; then
         CORE_PRESENT=1
         continue
      fi
      MENU_LINES=`expr $MENU_LINES + 1`
      loop_tmp=`get_os_long_string "$tmp"`
      eval MENU_ITEMS_${MENU_LINES}=\""$loop_tmp"\"
      if [ "$tmp" = "$mtag" ]; then
         eval arches_selections_${MENU_LINES}=1
      else
         eval arches_selections_${MENU_LINES}=0
      fi
   done
   # if ONLY core, skip arches screen
   if [ "$MENU_LINES" -eq 0 -a "$CORE_PRESENT" -eq 1 ]; then
      ARCHES_INSTALL="core"
   else 
      # skip menu section if installing all
      if [ "$INSTALL_ALL" -eq 0 ]; then
         LOOP_DONE=0
         while [ "$LOOP_DONE" -eq 0 ]
         do
            menu
            if [ "$MENU_ITEMS_SELECTED" -eq 0 ]; then
               echo ""
               echo "You must select at least one platform to install support for."
               echo ""
               $necho "Press [ENTER] to Continue$nnl"
               read tmp
            else 
               LOOP_DONE=1
            fi
         done
      else
         # skipping menus, so select everything
         count=1
         while [ "$count" -le "$MENU_LINES" ]
         do
            eval arches_selections_${count}=1
            count=`expr $count + 1`
         done
      fi
      # create a list of the arches we actually want to install, by mtag
      # core is always selected if present
      if [ "$CORE_PRESENT" -eq 1 ]; then
         ARCHES_INSTALL="core"
      else
         ARCHES_INSTALL=""
      fi
      count=1
      for tmp in $ARCHES_MENU
      do
         if [ "$tmp" = "core" ]; then
            continue
         fi
         eval loop_tmp=\""\$arches_selections_${count}"\"
         if [ "$loop_tmp" -ne 0 ]; then
            ARCHES_INSTALL="$ARCHES_INSTALL $tmp"
         fi
         count=`expr $count + 1`
      done
   fi

   # create a list of the packages that are available for the selected arches
   $necho "Working...$nnl"
   start_spin
   count=0
   MENU_LINES=0
   while [ "$count" -lt "$num_packages_found" ]
   do
      count=`expr $count + 1`
      eval tmp=\""\$PACKAGE_PROD_TAG_${count}"\"
      if [ "$tmp" != "$PRODUCT_CURRENT" ]; then
         continue
      fi

      # if we are installing everything, always select
      if [ "$INSTALL_ALL" -eq 1 ]; then
         # tag for install
         MENU_LINES=`expr $MENU_LINES + 1`
         eval MENU_ITEMS_${MENU_LINES}=\""\$PACKAGE_NAME_${count}"\"
         eval package_pointers_${MENU_LINES}=$count
         eval tmp=\""\$PACKAGE_SIZE_${count}"\"
         tmp=`add_arch_sizes "$ARCHES_INSTALL" "$tmp"`
         eval MENU_SIZE_${MENU_LINES}=$tmp
         eval package_selections_${MENU_LINES}=1
         continue
      else
         # set up menus
         eval loop_tmp=\""\$PACKAGE_ARCHES_${count}"\"
         pack_has_core=0
         for loop_arch in $ARCHES_INSTALL
         do
            if [ "$loop_arch" = "core" ]; then
               pack_has_core=1
               continue
            fi
            search_list "loop_tmp" "$loop_arch"
            result=$?
            if [ "$result" -ne 0 ]; then
               # tag for install
               MENU_LINES=`expr $MENU_LINES + 1`
               eval MENU_ITEMS_${MENU_LINES}=\""\$PACKAGE_NAME_${count}"\"
               eval package_pointers_${MENU_LINES}=$count
               eval tmp=\""\$PACKAGE_SIZE_${count}"\"
               tmp=`add_arch_sizes "$ARCHES_INSTALL" "$tmp"`
               eval MENU_SIZE_${MENU_LINES}=$tmp
               eval package_selections_${MENU_LINES}=1
               continue 2
            fi
         done
         tmp=`count_list "loop_tmp"`
         if [ "$pack_has_core" -eq 1 -a "$tmp" -eq 1 ]; then
            # tag for install (this is for when there is core only)
            MENU_LINES=`expr $MENU_LINES + 1`
            eval MENU_ITEMS_${MENU_LINES}=\""\$PACKAGE_NAME_${count}"\"
            eval package_pointers_${MENU_LINES}=$count
            eval tmp=\""\$PACKAGE_SIZE_${count}"\"
            tmp=`add_arch_sizes "$ARCHES_INSTALL" "$tmp"`
            eval MENU_SIZE_${MENU_LINES}=$tmp
            eval package_selections_${MENU_LINES}=1
            continue 
         fi
      fi
   done
   stop_spin
   $necho "$nnl"

   # finish setting up the menu for the "packages" screen 
   MENU_TITLE="Select the packages you wish to install:"
   MENU_HEADER="You are currently selecting packages for: $PROD_CURR_DESC"
   MENU_SIZE_COMPUTE=1
   MENU_INFO_CALL="handle_package_info"
   MENU_SIZE_DIR=""
   MENU_SIZE_DISK="$product_install_space"
   MENU_SIZE_TAG="MB"	
   MENU_SEL_VAR="package_selections_"
   
   # if we are installing everything, always select
   if [ "$INSTALL_ALL" -eq 1 ]; then
      # skipping menus, so select everything
      count=1
      while [ "$count" -le "$MENU_LINES" ]
      do
         eval package_selections_${count}=1
         count=`expr $count + 1`
      done
   else
      # otherwise, go ask the user what to install
      LOOP_DONE=0
      while [ "$LOOP_DONE" -eq 0 ]
      do
         menu
         if [ "$MENU_ITEMS_SELECTED" -eq 0 ]; then
            echo ""
            echo "You must select at least one package to install."
            echo ""
            $necho "Press [ENTER] to Continue$nnl"
            read tmp
         else 
            LOOP_DONE=1
         fi
      done
   fi

   # ask the user where they want the product installed
   LOOP_DONE=0
   while [ "$LOOP_DONE" -eq 0 ]
   do
      clear
      print_header
      echo ""
      echo "You must now select the location where $PROD_CURR_DESC will be installed."
      echo ""
      echo "The files for $PROD_CURR_DESC will be installed in a directory named"
      echo "$PROD_BASE under the specified install directory."
      echo ""
      echo "The default install directory is: $PROD_DEFAULT"
      echo ""
      echo "Note that this may overwrite some older versions of software."
      echo ""
      echo "Press [ENTER] for $PROD_DEFAULT"
      echo ""
      $necho "Install Location or Quit ? -> $nnl"
      read InstallLoc
      check_quit $InstallLoc
      if [ "$InstallLoc" = "" ]; then
         InstallLoc="$PROD_DEFAULT"
      fi
      
      InstallLoc=`resolve_path "$InstallLoc"`
      ask_create_dir "$InstallLoc" "$MENU_SIZE_TOTAL"
      if [ "$?" -eq 1 ]; then
         continue
      fi

      # check for a link
      InstallLoc="$InstallLoc/$PROD_BASE"
      if [ -d "$InstallLoc" ]; then
         tmp=`ls -l $InstallLoc | cut -c1`
         if [ "$tmp" = "l" ]; then
            echo ""
            echo "The following directory was really a link:"
            echo "$InstallLoc"
            echo ""
            echo "This link will be renamed to:"
            echo "$InstallLoc.old"
            mv -f "${InstallLoc}" "${InstallLoc}.old"
            echo "Creating $InstallLoc"
            mkdir -p "$InstallLoc" > /dev/null 2>&1
         fi
      else
         echo ""
         echo "Creating $InstallLoc"
         mkdir -p "$InstallLoc" > /dev/null 2>&1
      fi
      product_install_space=`check_space "$InstallLoc"`
      product_install_space=`expr $product_install_space \/ 1000`
      if [ "$product_install_space" -lt 1 ]; then
         product_install_space=1
      fi
      echo ""
      echo "$PROD_CURR_DESC files will be installed in:"
      echo "$InstallLoc"
      echo ""
      $necho "  Press [ENTER] to Continue$nnl"
      read TempAnswer
      LOOP_DONE=1
   done
   
   clear
   print_header
   echo ""
   echo "The software you have selected is about to be installed."
   echo ""
   echo "A log of all the files that are installed will be placed in:"
   echo "$InstallLoc/etc/install.log"
   echo ""
   $necho "Press [ENTER] to Begin Installation, Q to Quit -> $nnl"
   read TempAnswer
   check_quit "$TempAnswer"

   # actually start the install process
   clear
   print_header
   echo ""
   cd "$InstallLoc"
   mkdir "etc" > /dev/null 2>&1
   mkdir "etc/packages" > /dev/null 2>&1
   touch "etc/install.log"
   echo "Installing: $PROD_CURR_DESC"
   echo "Installing from: $SEARCH_DIR"
   echo "Installing to  : $InstallLoc"
   echo ""
   $necho "Working...$nnl"
   start_spin
   echo "Installing: $PROD_CURR_DESC" >> "etc/install.log"
   echo "Installing from: $SEARCH_DIR" >> "etc/install.log"
   echo "Installing to  : $InstallLoc" >> "etc/install.log"
   count=1
   while [ "$count" -le "$MENU_LINES" ]
   do
      eval tmp=\""\$package_selections_${count}"\"
      eval curr_pack=\""\$package_pointers_${count}"\"
      if [ "$tmp" -eq 0 ]; then
         # mark the package as processed, even though we're not installing it
         eval PACKAGE_DONE_${curr_pack}=1
         count=`expr $count + 1`
         continue
      fi
      eval tmp=\""\$PACKAGE_NAME_${curr_pack}"\"
      eval pack_arches=\""\$PACKAGE_ARCHES_${curr_pack}"\"
      eval loop_dir=\""\$PACKAGE_IPFNAME_${curr_pack}"\"
      loop_dir=`fix_dirname "$loop_dir"`
      for curr_arch in $ARCHES_INSTALL
      do
         search_list "pack_arches" "$curr_arch"
         result=$?
         if [ "$result" -eq 1 ]; then
            echo "Saving a copy of the info.ipi file in $InstallLoc/etc/packages" >> "etc/install.log"
            eval tmp=\""\$PACKAGE_IPFNAME_${curr_pack}"\"
            tmp2=`fix_filename "info.ipi"`
            cp "${SEARCH_DIR}/${loop_dir}/${tmp2}" "./etc/packages/${tmp}.ipi"
            tmp="${curr_arch}.tar"
            tmp=`fix_filename "$tmp"`
            if [ -f "${SEARCH_DIR}/${loop_dir}/${tmp}" ]; then
               echo "Extracting: ${SEARCH_DIR}/${loop_dir}/${tmp}" >> "etc/install.log"
               tar xpvf "${SEARCH_DIR}/${loop_dir}/${tmp}" >> "etc/install.log" 2>&1
               # extract tar file
            else
               tmp="${curr_arch}.taz"
               tmp=`fix_filename "$tmp"`
               if [ -f "${SEARCH_DIR}/${loop_dir}/${tmp}" ]; then
                  echo "Extracting: ${SEARCH_DIR}/${loop_dir}/${tmp}" >> "etc/install.log"
                  cat "${SEARCH_DIR}/${loop_dir}/${tmp}" | zcat | tar xpvf - >> "etc/install.log" 2>&1
                  # extract taz file
               fi
            fi
         fi
      done
      # check for postinstall script and add to postinstall list
      eval tmp3=\""\$PACKAGE_POSTINST_${curr_pack}"\"
      if [ ! -z "$tmp3" ]; then
         num_postinst_found=`expr $num_postinst_found + 1`
         eval postinst_pointer_${num_postinst_found}=\""\$curr_pack"\"
      fi
      # mark the package as processed
      eval PACKAGE_DONE_${curr_pack}=1
      count=`expr $count + 1`
   done
   stop_spin

   echo "Done"

   # check for postinstall scripts for current product's packages and run
   if [ "$num_postinst_found" -gt 0 ]; then
      echo ""
      echo "Some of the packages you just installed have postinstall scripts."
      echo ""
      echo "If you do not run these scripts now, you may encounter problems"
      echo "when running $PROD_CURR_DESC."
      echo ""
      $necho "Press [ENTER] to Start the Postinstall Script(s) ->$nnl"
      read tmp
      echo ""
      count=1
      while [ "$count" -le "$num_postinst_found" ]
      do
         eval tmp=\""\$postinst_pointer_${count}"\"
         eval tmp2=\""\$PACKAGE_IPFNAME_${tmp}"\"
         tmp2=`fix_filename "$tmp2"`
         eval tmp3=\""\$PACKAGE_POSTINST_${tmp}"\"
         tmp3=`fix_filename "$tmp3"`
         if [ -f "${SEARCH_DIR}/${tmp2}/${tmp3}" ]; then
            (. "${SEARCH_DIR}/${tmp2}/${tmp3}")
         fi
         count=`expr $count + 1`
      done
      echo ""
      echo "The postinstall script(s) have completed."
      $necho "Press [ENTER] to Continue$nnl"
      read tmp
   fi

   # let them know we are done with this product
   echo ""
   echo "$PROD_CURR_DESC is done installing."
   echo ""
   $necho "Press [ENTER] to Continue$nnl"
   read tmp
done

echo ""
echo "Installation Complete"
echo ""
echo "If you would like to install further products or packages, you should"
echo "run this script again."
