#!/bin/ksh
#
# $Id: DCWinstall.sh,v 1.1.6.1 2005/05/27 20:56:18 nrao Exp $ 
#
# All rights reserved.
#

#
# The following are set during the build process
#
copyright="Copyright (c) 1990-2005, LEGATO Software, a division of EMC."

#
# Initialize the variables required by this Linux
# installation script.
#
pkg="lgtowizd"
pkgver="lgtowizd-1.0-1"
package="lgtowizd-1.0-1.i686.rpm"
bsd_echo=false

# Here is the product description
description[1]="The device configuration wizard lets you detect and configure"
description[2]="SCSI device resources for backups on a specified NetWorker server."
description[3]="Those resources can then be edited by using the NetWorker"
description[4]="Administrator program."

# Here is the post installation instructions
postinstall[1]="### Information for Completing your Wizard Installation ###"
postinstall[2]="--------------------------------------------------------------------"
postinstall[3]="Please follow the remaining steps in the Installation Guide to complete"
postinstall[4]="the installation."

# Calculate the number if lines for each text array
desc_lines=`eval echo ${#description[*]}`
post_lines=`eval echo ${#postinstall[*]}`

# Start with the default install option
rpmoptions="-i"

#
# echo_n function
#
if [ $bsd_echo = true ]; then
	echo_n()
	{

		echo -n "$*"
	}
else
	echo_n()
	{

		echo "$*\c"
	}
fi

#
# yesno function
#
# Generic function to get yes/no answer
#
# inputs:
#	prompt=		string to prompt with (excluding default value)
#	default=	name of default value if no response
#
# outputs:
#	result=		'y' or 'n'
#
yesno()
{
	echo ""
	while true
	do
		if [ -z "${default}" ]; then
			echo_n "${prompt}? "
			read ans
		else
			echo_n "${prompt} [${default}]? "
			read ans
		fi
		if [ "X${ans}" = X ]; then
			result="${default}"
		else
			result="${ans}"
		fi

		if [ `expr "X${result}" : 'X[yY]'` -ne 0 ]; then
			result=y
			break
		elif [ `expr "X${result}" : 'X[nN]'` -ne 0 ]; then
			result=n
			break
		else
			echo "Please respond \`yes' or \`no'"
		fi
	done
}

#
# Check for the existence of NetWorker Storage Node
#
check_networker_client () {
  clnt=`rpm -qa | grep lgtonode`
  if [ X$clnt != X ]; then
    # see if nsrmm was installed as part of the NW client
    nsrmmloc=`rpm -qs $clnt | grep nsrmm`
    # we should have the full path to nsrmm
    nsrmmpath=`echo $nsrmmloc | tr -s ' ' | cut -d' ' -f2`
    if [ X$nsrmmpath != X ]; then
      reloc=`echo "$nsrmmpath" | sed "s@/sbin/nsrmmd@@g"`
      if [ X$reloc != X/usr ]; then
        # Add the relocation to the rpm command list
        rpmoptions=`echo $rpmoptions --relocate /usr=$reloc`
      fi
    fi
  else
    echo "This product has a dependency on the lgtonode"
    echo "Please install the Legato NetWorker Node using NWinstall"
    exit 1
  fi
}

#
# See if package is already installed
#
check_package_installation () {
  lnm=`rpm -qa | grep $pkg`
  if [ X$lnm != X ]; then
    if [ X$lnm = X$pkgver ]; then
      echo "This package has already been installed"
      echo "If you need to re-install the package first remove it."
      echo "rpm -e "$lnm
      exit 1
    else
      prompt="$lnm already installed, would you like to upgrade"
	  default="y"
	  yesno
	  if [ "${result}" = "y" ]; then
        # Add the upgrade option to rpm command list
	    rpmoptions=`echo $rpmoptions -U`
	  else
		exit 1
	  fi
    fi
  fi
}

#
# Some versions of Linux require the --nodeps option
# Caldera is on of those versions
#
check_nodeps_option () {
# In the future we might want to look at /etc/issue file
# for distribution information
  kernel=`rpm -qa | grep kernel | head -1`
  nodeps=0
  if [ X$kernel != X ]; then
    vendor=`rpm -qi $kernel | grep "Vendor:"`
    if [ X"$vendor" != X ]; then
      caldera=`echo $vendor | grep -i caldera`
      if [ X"$caldera" != X ]; then
        nodeps=1
      fi
    fi
  fi
  if [ $nodeps -eq 1 ]; then
    # Add the do not check depandencies before installation
	# to the rpm command list
    rpmoptions=`echo $rpmoptions --nodeps`
  fi
}

#
# This function displays the product description during
# the pre-installation phase.
#
echo_description () {
  let count=1
  let lnm_lines=$desc_lines+1
  while (( $count < $lnm_lines )); do
    echo "`eval echo '${description[$count]}'`"
    let count="count+1"
  done
}

#
# This function displays the post-installation instructions.
#
echo_postinstall_message () {
  let count=1
  let lnm_lines=$post_lines+1
  while (( $count < $lnm_lines )); do
    echo "`eval echo '${postinstall[$count]}'`"
    let count="count+1"
  done
}

#
# This function drives the post-installation clean-up
#
post_installation () {
  echo_postinstall_message
  echo " "
  echo "Please update the MANPATH environment variable to"
  echo "include the path" $reloc"/share/man"

}

#
# This function drives the pre-installation requirements
#
pre_installation () {
  echo $copyright
  echo_description
  check_networker_client
  check_package_installation
  check_nodeps_option
}

#
# We are now ready to begin the package installation process
#
pre_installation
echo "rpm" $rpmoptions $package
rpm $rpmoptions $package
if [ $? -eq 0 ]; then
  post_installation
fi
