#!/bin/sh 
#######################################################################
#
# qsinstall_header
#
# The header of a shell script to install a Quadrics product
#
#
#######################################################################

# exit handler, for clean exit on ctrl-C
trap 'exit 1' SIGINT
# qsinstall_functions.OSF1
#
#######################################################################
#
# $Id: qsinstall_functions.OSF1,v 1.2.4.1 2004/07/20 16:34:29 fegan Exp $
#
#######################################################################

#
# variables
#

forOS="OSF1"
compstr="subset"

#
# OSF1 only variables
#

KITDB="/usr/.smdb." # where Compaq keep their installed kits information

#
# functions
#

get_admdir() {
    printf "/var/rms/adm\n"
}

get_rmspath() {
    printf "/usr/bin\n"
}

get_core_components() {
    printf "RMSELAN RMSJTAG RMSD RMSBASE RMSPANDORA RMSHTTPD\n"
}

check_user() {
    user=`whoami`
    if [ "$user" != "root" ]; then 
        errorexit "you must be root to run $0"
    fi
}

check_product() {
    imagefile=`(cd ${productbase}; ls *.image 2>&1)`
    if [ $? -ne 0 ]; then
        errorexit "no .image file found - need to be in a kit directory"
    fi
}

get_product_name() {
    printf "$imagefile\n" | gawk -F. '{print$1}'
}

get_componentbase() {
    printf "${productbase}\n"
}

get_productname_from_product() {
    printf "$1\n" | sed 's/[0-9]\{1,3\}.*//'
}

get_productvers_from_product() {
    printf "$1\n" | sed 's/^[A-Z]*//'
}

get_componentname_from_component() {
    printf "$1\n" | sed 's/[0-9]\{1,3\}.*//'
}

get_componentvers_from_component() {
    printf "$1\n" | sed 's/^[A-Z]*//'
}

check_rmsenv() {    

   #
   # See if rms user and group exist - create them if not
   #

   # Don't use nis if on a cluster

   /usr/sbin/clu_get_info >/dev/null 2>&1
   nocluster=$?

   if [ $nocluster -eq 1 ]; then
      distributed=1
   else
      distributed=0
   fi

   # Check rms group
    
   rms_group_exists=0

   if [ $distributed -eq 1 ]; then
      if ypcat group | grep "^rms:" > /dev/null ; then
	 rms_group_exists=1
      fi
   else
       if grep "^rms:" /etc/group > /dev/null ; then
	 rms_group_exists=1
       fi
   fi       
   if [ $rms_group_exists -eq 1 ]; then
      printverbose "'rms' group already exists"
   else
      printmsg "adding 'rms' group ..."
      groupdel -x local=0 rms 2>&-
      groupdel -x local=1 rms 2>&-
      groupadd -x distributed=$distributed rms
      usermod -G `groups root | sed 's/ /,/g' `,rms root
   fi

   # Create rms user
    
   rms_user_exists=0

   if [ $distributed -eq 1 ]; then
      if ypcat passwd | grep "^rms:" > /dev/null ; then
         rms_user_exists=1
      fi
   else
      if grep "^rms:" /etc/passwd > /dev/null; then
         rms_user_exists=1
      fi
   fi
   if [ $rms_user_exists -eq 1 ]; then
     printverbose "'rms' user already exists"
   else
      printmsg "adding 'rms' user ..."
      userdel -x local=1 rms 2>&-
      userdel -x local=0 rms 2>&-
      useradd -x distributed=$distributed -g rms rms

      #
      # unfortunately useradd doesn't actually add 'rms' as a user to the
      # groups file even though rms is the primary group for user rms.  So,
      # we need to do that explicitly.
      #

      if [ $distributed -eq 1 ]; then
         grpfile=/var/yp/src/group
      else
         grpfile=/etc/group
      fi
      tmpfile=/tmp/qsinstall_$$
      rmsline=`grep "^rms:" $grpfile`

      # it's an error if we can't find the group because we should have
      # just added it above
      if [ ! "$rmsline" ]; then
         errorexit "group 'rms' not present in group file $grpfile"
      fi

      grpmembers=`printf "$rmsline\n" | gawk -F: '{ print $4 }'`
      grplist=`printf "$grpmembers\n" | gawk -F, '{ for (i = 1; i <= NF; i++) { printf "%s ", $i }}'`
      found=""
      for member in $grplist; do
         if [ "$member" = "rms" ]; then 
            found=1
         fi
      done
      if [ ! "$found" ]; then
         printmsg "adding user 'rms' to group 'rms' in $grpfile"
         sed "/^rms:/d" $grpfile > $tmpfile
         printf "${rmsline},rms\n" >> $tmpfile
         cp $tmpfile $grpfile
         rm $tmpfile 
	 if [ $distributed -eq 1 ]; then
            cd /var/yp
            make
            cd $here
	 fi
      fi
   fi
}

get_local_components() {
    subsets=`cat ${productbase}/$imagefile | gawk '{print $3}'`
    printf "$subsets\n"	
}

# Added for compatibility - not used at the moment
get_componentattr() {
    printf ""
}


get_componenttype() {
    subset=$1
    ctrlfile=${componentbase}/instctrl/${subset}.ctrl
    flag=`sed -n "/^FLAGS=/ p" $ctrlfile | sed "s/FLAGS=//"`

    if [ $flag -gt 255 ]; then
        printf "opt\n"
    else
	printf "root\n"
    fi
}

#
# look through a list of subsets to install and see if we have to remove
# any first - once we've found one to remove we must remove them all so 
# can stop looking.  'remove' is set to indicate result.
#
# 
check_remove() {
	
    subsets=$1

    remove=""
    for subset in $subsets; do
	subsetname=`get_componentname_from_component $subset`
	subsetvers=`get_componentvers_from_component $subset`

	# existence of <subset>.lk file in the kit database means subset is
	# installed

	isubsets=`ls ${KITDB}/${subsetname}*.lk 2> /dev/null | sed "s?${KITDB}/??g" | sed "s?.lk??g"`

	for isubset in $isubsets; do
	    isubsetname=`get_componentname_from_component $isubset`
	    isubsetvers=`get_componentvers_from_component $isubset`

	    if [ "$isubsetname" = "$subsetname" ]; then
		if [ "$isubsetvers" != "$subsetvers" ]; then
		    if [ "$verbose" ]; then
		       printmsg "will remove subsets as version (${subsetvers}) is different"
	               printf "            from installed version (${isubsetvers}) for subset $subset\n"
		    fi
		    remove="1"
	            return
		else
		    if [ "$force" ]; then
		       printverbose "will remove installed subsets as 'force' is set"
		       remove="1"
	               return
		    fi
		fi
	    fi
	done
    done
}

#
# removecomponent <subset> - remove the subset
#                         Will remove any versions of the subset - the
#                         subset parameter can be with or without version
remove_component() {
    rsubset=$1
    rname=`get_componentname_from_component $rsubset`
    rvers=`get_componentvers_from_component $rsubset`

    # see if the subset is installed

    isubsets=`ls ${KITDB}/${rname}*.lk 2> /dev/null | sed "s?${KITDB}/??g" | sed "s?.lk??g"`

    found=""

    for isubset in $isubsets; do

	ivers=`get_componentvers_from_component $isubset`
	iname=`get_componentname_from_component $isubset`

	if [ "$iname" = "$rname" ]; then
	    printmsg "removing $isubset"
	    found="1"
	    if [ "$doit" ]; then
		/usr/sbin/setld -d $isubset
		if [ $? -ne 0 ]; then
		    errorexit "failed to /usr/sbin/setld -d $isubset"
		fi
	    fi
	fi
    done
    if [ "$removeonly" -a ! "$found" ]; then
	printmsg "subset $rsubset not installed"
    fi
}

#
# install_component <subset> - install the subset if not already installed    
#
install_component() {
    isubset=$1

    #
    # Check if subset already installed - if the subset is already installed
    # installation not necessary because if the -f option was used the
    # installed subset will have been removed already.
    #
    # Where the -n flag was used need to look in the revsubsets list to see if 
    # the subset would have been removed by now.
    #

    if [ -f ${KITDB}/${isubset}.lk ]; then
	if [ ! "$doit" ]; then 
	    found=""
	    for rsubset in $revcomponents; do
		if [ "$isubset" = "$rsubset" ]; then	    
		    found="1"
		    break
		fi
	    done
	    if [ ! "$found" ]; then
		printmsg "subset $isubset already installed"
		return
	    fi
	else
	    printmsg "subset $isubset already installed"
	    return
	fi
    fi

    #
    # subsets get installed according to the flag value in the image file 
    # for the subset
    #
    # if bit 8 of flag is set then install subset on rmshost only
    # otherwise install everywhere
    #

    ctrlfile=${componentbase}/instctrl/${isubset}.ctrl
    flag=`sed -n "/^FLAGS=/ p" $ctrlfile | sed "s/FLAGS=//"`

    if [ $flag -gt 255 ]; then
	if [ "$isrmshost" ]; then
	    printmsg "installing opt subset $isubset"
	    if [ "$doit" ]; then
		/usr/sbin/setld -l ${productbase} $isubset
		if [ $? -ne 0 ]; then
		    echo "$sname: ERROR : Failed to /usr/sbin/setld -l ${productbase} $isubset"
		    exit 1
		fi
                # log that we've installed this component to tmp file
		subsetname=`get_componentname_from_component $isubset`
		subsetvers=`get_componentvers_from_component $isubset`
	        printf "$subsetname $subsetvers\n" >> $tmpcompfile
	    fi
	else
	    printmsg "NOT installing opt subset $isubset"
	fi
    else
	# root package - install everywhere
	printmsg "installing root subset $isubset"
	if [ "$doit" ]; then
	    /usr/sbin/setld -l ${productbase} $isubset
	    if [ $? -ne 0 ]; then
	        errorexit "failed to /usr/sbin/setld -l ${productbase} $isubset"
	    fi
            # log that we've installed this component to tmp file
            subsetname=`get_componentname_from_component $isubset`
	    subsetvers=`get_componentvers_from_component $isubset`
	    printf "$subsetname $subsetvers\n" >> $tmpcompfile
	fi
    fi
}

#
# for OSF1 rmshost must be defined so we always return zero
# this function is required because it doesn't need to be if 
# installing open source products on Linux
#

rmshostneeded() {
    exit 0
}
#######################################################################
#
# qsinstall.generic
#
#
#######################################################################
#
# $Id: qsinstall_generic,v 1.4.2.2 2004/07/20 16:34:29 fegan Exp $
#
#######################################################################

#
# make sure we use gawk
#

if [ -x /bin/gawk ]; then
    AWK="/bin/gawk"
elif [ -x /usr/bin/gawk ]; then
        AWK="/usr/bin/gawk"
else
    AWK="/usr/local/bin/gawk"
fi


#
# common functions
#

#
# errorexit
#
errorexit() {
    printerror "$1" "$2"
    exit 1
}

printerror() {
    printf "$sname: $nodename : ERROR : $1\n"
    if [ "$2" ]; then
        usage
    fi
}

printmsg() {
    printf "$sname: $nodename : $1\n"
}

printverbose() {
    if [ "$verbose" ]; then
        printmsg "$1"
    fi
}

warning() {
    printf "$sname: $nodename : WARNING : $1\n"
}

#
# usage
#
usage() {
    printf "${usageStr}\n"
}

help() {
    usage
#
# all nodes option not implemented
#   printf "       -a\t  All nodes (default is just this node)\n"
    if [ "$OS" = "OSF1" ]; then 	
        printf "       -c\t  Install on all cluster leaders\n"
    fi
    printf "       -f\t  Force reinstallation even if ${compstr} versions match\n"
    printf "       -h\t  This help message\n"
    printf "       -n\t  No execution mode - just display 'what if'\n"
    printf "       -r\t  Remove installed ${compstr}s - no installation\n"
    printf "       -v\t  Verbose\n"
    printf "       -N <nodes> Run on <nodes> - eg. '-N clust[1-7]'\n"
    if [ "$OS" = "Linux" ]; then 	
	printf "\n"
        printf "       -rpmeflags \"<flags>\"\tFlags for 'rpm -e' (removal)\n"
        printf "       -rpmiflags \"<flags>\"\tFlags for 'rpm -i' (installation)\n"
    fi
}

check_OS() {
    if [ "$OS" != "$forOS" ]; then
	errorexit "can only use this script on a $forOS system"
    fi
}

check_rmshost() {    
#
# check rmshost is defined and set 'isrmshost' if this node is it
# 
    printverbose "checking for rmshost"

    rmshost=`$RMSHOSTSCRIPT 2>&1`
    rc=$?
    if [ $rc -ne 0 ]; then
	# rmshost exits with 1 and outputs nothing if rmshost is undefined
	# if the string is not empty an error must have occurred and the
	# string will contain the error message
	if [ "$rmshost" != "" ]; then
	    errorexit "'rmshost' returned '$rmshost'"
        fi
	rmshostundefined="1"
    fi

    if [ "$rmshost" = "$nodename" ]; then
        isrmshost="1"
    fi
}

check_rmsquery() {    
#
# check rmsquery is available and working (set usermsquery if it is)
# 
    printverbose "checking for rmsquery"

    if [ -x $RMSQUERY ]; then
	output=`$RMSQUERY -e "select * from attributes" 2>&1`
	if [ $? -ne 0 ]; then 
            warning "$RMSQUERY not responding"
	else
	    usermsquery=1
	fi
    fi	
}

#
# Copy the release notes into place
#
copy_relnotes ()
{    

    if [ -d $srcrelnotesdir ]; then
        if [ ! -d $destrelnotesdir ]; then
            mkdir -p $destrelnotesdir
	    chmod 775 $destrelnotesdir
        fi
        for file in `ls $srcrelnotesdir`; do
	    /bin/cp -f ${srcrelnotesdir}/$file $destrelnotesdir/$file
	    chmod a+r $destrelnotesdir/$file
        done
    fi
}

#
# copy_productdef : Copy the product definition into place.
#                   Existing ones for the same product are moved to .<file>
#
copy_productdef()
{ 
    if [ "$doit" ]; then
        # Can only do this if we were successful in extracting product name
        if [ "$productdefname" ]; then
            if [ ! -d $swprod_dir ]; then
                mkdir -p $swprod_dir
            fi
            if [ ! -f $swprod_dir/$productdeffile ]; then
	        oldlist=`ls $swprod_dir`
   	        if [ "$oldlist" != "" ]; then
  	            for oldfile in $oldlist; do
	                oproduct=`get_productname_from_product $oldfile`
	                overs=`get_productvers_from_product $oldfile`
	                if [ "$oproduct" = "$productdefname" ]; then
		            mv $swprod_dir/$oldfile $swprod_dir/"."$oldfile
	                fi
                    done
                fi
            fi
            cp -f $productdefdir/$productdeffile $swprod_dir
        fi
    fi
}

#
# makecomplist <list> - Sorts a list of components into the correct
#			order for installing.
#			First looks for a productdeffile to use.
#			If no productdeffile and the product is RMS 
#			use a a hardwired 'core' component list.
#
#			When the list has been sorted any components
#			which wouldn't be installed on this sort of node
#			are discarded from the list.
#
#	usage : makecomplist <components_being_installed>      
#     

makecomplist() {
    installcomponents=$1
    components=""    		# will store the sorted list to return
    orderedcomponents=""

    if [ "$productdeffile" ]; then
	orderedcomponents=`cat ${productdefdir}/$productdeffile | $AWK '{ print $1 }'`
    else
        # if this is the 'RMS' product use hardwired list
	if [ "$product" = "RMS" ]; then
	    orderedcomponents=`get_core_components`
	fi
    fi

    for comp in $orderedcomponents; do
	for installcomp in $installcomponents; do
            # the ordered components are component names only so get just 
	    # the names from the components being checked before
	    # comparing

	    checkcomp=`get_componentname_from_component $installcomp`
	    
	    if [ "$checkcomp" = "$comp" ]; then
		components=$components" "$installcomp
		break
	    fi
	done
    done

    #
    # now add any in the list which weren't in 'orderedcomponents'
    #

    for installcomp in $installcomponents; do
	found=""
	for comp in $components; do
	    if [ "$comp" = "$installcomp" ]; then
		# this one already present
		found="1"
		break
	    fi
	done
	if [ ! "$found" ]; then
	    components=$components" "$installcomp
	fi
    done
    
    finalcomponents="$components"
    printf "$finalcomponents\n"
}

#
# update_productTable : Insert the product definition into the machine database
#

update_productTable()
{ 
    if [ "$doit" -a "$usermsquery" ]; then
      printverbose "updating Software Products table"
      # Can only do this if we were successful in extracting product name
      if [ "$productdefname" ]; then
         str="DELETE from software_products WHERE name='$productdefname' AND version='$productdefversion'"
	 output=`$RMSQUERY "$str" 2>&1`
	 if [ $? -ne 0 ]; then 
	     warning "$RMSQUERY $str FAILED : output <${output}>"
         fi
	 cat $productdefdir/$productdeffile | 
         while read comp_name comp_type comp_vers comp_attr; do
            str="INSERT into software_products VALUES ('$productdefname', '$productdefversion', '$comp_name', '$comp_type', '$comp_vers', '$comp_attr')"
	    output=`$RMSQUERY "$str" 2>&1`
            if [ $? -ne 0 ]; then 
	        warning "$RMSQUERY $str FAILED : output <${output}>"
	       # don't carry on after an error - chances are if one failed
	       # all will fail
	       break
            fi
	 done
      fi
   fi
}

#
# update_installedTable : Insert the installed components into the machine 
#                         database
#

update_installedTable()
{ 
   if [ "$doit" -a "$usermsquery" -a -x $RMSGETTIME ]; then
      printverbose "updating Installed Components table"
      # Can only do this if we were successful in extracting product name
      if [ "$productdefname" -a -s $tmpcompfile ]; then
         cat $tmpcompfile |
         while read comp_name comp_vers; do
            str="DELETE from installed_components WHERE hostname='$nodename' AND product='$productdefname' AND prodversion='$productdefversion' AND component='$comp_name' AND compversion='$comp_vers'"
	    output=`$RMSQUERY "$str" 2>&1`
	    if [ $? -ne 0 ]; then 
                warning "$RMSQUERY $str FAILED : output <${output}>"
            fi
	    tm=`$RMSGETTIME`
	    str="INSERT into installed_components VALUES ('$nodename', '$productdefname', '$productdefversion', '$comp_name', '$comp_vers', $tm)"
	    output=`$RMSQUERY "$str" 2>&1`
            if [ $? -ne 0 ]; then 
               warning "$RMSQUERY $str FAILED : output <${output}>"
	       # don't carry on after an error - chances are if one failed
	       # all will fail
	       break
            fi
	 done
      fi
   fi
}    


#
# parsenodelist : takes the nodelist argument and uses awk to parse it
#                 and turn it into a list of nodes.
#
# nodelist is global
#

parsenodelist()
{ 
    output=`echo "$1" | $AWK -f $PARSENODELIST`
    rc=$?
    if [ $rc -eq 0 ]; then
	nodelist=$output
    else
        errorexit "error parsing nodelist - $output"
    fi    
}

#
# main : start here
#

OS=`uname`
ARCH=`uname -m`
sname="qsinstall"
if [ "$OS" = "OSF1" ]; then
    usageStr="usage: qsinstall [-cfhnrv] [-N <nodelist>] [ <$compstr> ... ]"
else
    usageStr="usage: qsinstall [-fhnrv][-N <nodelist>][-rpmiflags \"<flags>\"]\n\t\t [-rpmeflags \"<flags>\"] [ <$compstr> ... ]"
fi
prog=$0
progdir=`dirname $prog`
here=`pwd`
docluster=""
force=""
removeonly=""
rmshostundefined=""
isrmshost=""
selcomponents=""
revcomponents=""
doit="yes"
verbose=""
admdir=`get_admdir`
destrelnotesdir=${admdir}/relnotes
swprod_dir=/usr/.rms/products
tmpcompfile="/tmp/qsinstall.tmpcomp.$$"
usermsquery=""
RMSPATH=`get_rmspath`
RMSQUERY=${RMSPATH}/rmsquery
RMSGETTIME=${RMSPATH}/rmsgettime
productdefname=""
productdefversion=""
child=""
nodelist=""
multipleproducts=""

#
# nodename - turn <name>.<domain> into <name>
#
nodename=`uname -n | $AWK -F. '{print $1}'`

check_OS

#
# work out where the components to install are - based on how 
# this script was invoked
#

if [  `printf "$prog\n" | grep -v "^/"` ]; then
   # ie. not called with absolute pathname
   if [ "$progdir" = "" -o "$progdir" = "." ]; then
      productbase=$here
   else
      productbase=$here/$progdir
   fi
else
   productbase=$progdir
fi

#
# qsinstall can be called from a higher level directory to 
# do multiple products (Linux only).  if this is the case
# just loop through the list of products passing on the args
#
if [ "$OS" = "Linux" ]; then 
    if [ ! -d $productbase/RPMS ]; then
        # if no RPMS directory look for products
	filelist=`ls $productbase 2>&1`
	if [ "$filelist" = "" ]; then
	    errorexit "no products or rpms present"
	else
	    products=""
	    # get the products to install into the right order
	    for qsproduct in $qsproducts; do
	        for file in $filelist; do
	            # only do this on directories (ie. not the tarfiles!)
		    if [ -d ${productbase}/$file ]; then
	                prodname=`echo $file | $AWK -F. '{ print $1 }'`
	                if [ "$prodname" = "$qsproduct" ]; then
		            products="${products}${file} "	          
	                    break
		        fi
	            fi
	        done
	    done
	    # for any not in the qsproduct list tack them on the end
	    for file in $filelist; do
	        # only look for genuine products 
	        if [ ! -d ${file}/RPMS ]; then
	            continue
	        fi	        
	        found=""
	        for product in $products; do
	            if [ "$file" = "$product" ]; then
	                found=1
	                break
	            fi
	        done
	        if [ ! "$found" ]; then
		    products="${products}${file} "
	        fi
	    done
	    if [ "$products" = "" ]; then
		errorexit "no products or rpms present"
	    fi
	    multipleproducts="1"
	fi
    fi
fi

srcrelnotesdir=$productbase/relnotes
productdefdir=$productbase/productdef
RMSHOSTSCRIPT=$productbase/tools/rmshost
componentbase=`get_componentbase`
PARSENODELIST=$productbase/tools/parsenodelist.awk

#
# SunOS only
#
if [ "$OS" = "SunOS" ]; then 
    optpatches=$productbase/optpatches
    patches=$productbase/patches
fi


#
# parse the options
#

#
# save all the arguments for passing on
#

arguments="$*"

while [ $# -gt 0 ]; do
    option=`printf "$1\n" | sed "s/^-//"`
    if [ "$option" = "$1" ]; then
        break
     fi
     if [ "$option" = "c" ]; then
	if [ "$OS" = "OSF1" ]; then
	    if [ "$nodelist" ]; then
	        errorexit "-N and -c options are mutually exclusive"
	    fi
            if [ "$usermsquery" ]; then
	        str="SELECT name from nodes where member=1 order by name"
		output=`$RMSQUERY "$str" 2>&1`
	        if [ $? -ne 0 ]; then 
                    printerror "$RMSQUERY $str FAILED : can't find cluster leaders"
                    errorexit "output was <${output}>"
	        fi
	        nodelist=$output
	        docluster=1
	    else
                errorexit "no $RMSQUERY : can't find cluster leaders"
            fi	    
	else
	    errorexit "invalid option $1" "usage"		    
	fi
     elif [ "$option" = "f" ]; then
         force=1
     elif [ "$option" = "h" ]; then
         help
         exit 0
     elif [ "$option" = "n" ]; then
         doit=""
     elif [ "$option" = "r" ]; then
         removeonly=1
     elif [ "$option" = "v" ]; then
         verbose=1
     elif [ "$option" = "N" ]; then
         if [ "$nodelist" ]; then
	     errorexit "-N and -c options are mutually exclusive"
	 fi
	 shift
	 if [ $# -gt 0 ]; then
	     # don't actually parse the node list until you're
	     # within a product 
	     if [ ! "$multipleproducts" ]; then
	         parsenodelist $1
	     fi
	 else
             errorexit "-N option must have <nodelist> argument"
	     exit 1
	 fi
     elif [ "$option" = "k" ]; then
	child=1
     elif [ "$OS" = "SunOS" -a "$option" = "nopatches" ]; then
         nopatches=1
     elif [ "$OS" = "SunOS" -a "$option" = "jumpstart" ]; then
         # only do jumpstart if installing RMS product
         if [ ! -d ${packages}/QSWrms ]; then
             errorexit "can only do jumpstart if installing RMS product"
	     exit 1
         fi
         # Don't do the jumpstart copying if you're actually running 
	 # qsinstall from the jumpstart directory - you end up deleting 
         # the packages if you do
         if [ "$productbase" = "$jumpstartdir" ]; then
             errorexit "can't do jumpstart from $jumpstartdir"
	     exit 1
         fi
         dojumpstart="yes"
    elif [ "$OS" = "SunOS" -a "$option" = "rcinstall" ]; then
        rcinstall="1"
    elif [ "$OS" = "Linux" -a "$option" = "rpmiflags" ]; then
	 shift
	 if [ $# -gt 0 ]; then
	     tmp=$1
	 else
             errorexit "-rpmiflags option must have an argument"
	     exit 1
	 fi
	 if (checkrpmflags "$tmp" "-U"); then
	     rpmiflags="$tmp"
	 else
	    rpmiflags="-U $tmp"
         fi
     elif [ "$OS" = "Linux" -a "$option" = "rpmeflags" ]; then
	 shift
	 if [ $# -gt 0 ]; then
	     tmp=$1
	 else
             errorexit "-rpmeflags option must have an argument"
	     exit 1
	 fi
	 if (checkrpmflags "$tmp" "-e"); then
	     rpmeflags="$tmp"
	 else
	    rpmeflags="-e $tmp"
         fi

    else
        errorexit "invalid option $1" "usage"
    fi
    shift
done

#
# check your rights
#

check_user

if [ "$multipleproducts" ]; then 	
    cmdmsg="Installing"
    # if removing reverse the order of the products
    if [ "$removeonly" ]; then
	tmpproducts=$products
	products=`printf "$tmpproducts\n" | \
		$AWK '{ for (i = NF; i > 0; --i) printf ("%s ", $i ) }'`
	cmdmsg="Removing"
    fi	
    for product in $products; do
	printf "$sname: $cmdmsg product $product\n"
	${productbase}/${product}/qsinstall $arguments
	rc=$?
	if [ $rc -ne 0 ]; then
	    exit $rc
        fi
    done
    exit 0
fi

#
# if nodelist set use rsh to install on remote nodes
#

if [ "$nodelist" -a ! "$child" ]; then
    if [ "$docluster" ]; then
	printverbose "Installing on cluster leaders"
    fi
    for node in $nodelist; do
	# see if I'm in the list - no need to rsh if so
	if [ "$node" = "$nodename" ]; then
            cmd="${productbase}/qsinstall -k $arguments"
	else
            cmd="rsh $node ${productbase}/qsinstall -k $arguments"
	fi
        printverbose "executing '$cmd'\n"
	$cmd
	rc=$?
        if [ $rc -ne 0 ]; then
	    printmsg "ERROR : '$cmd' failed"
        fi
    done
    exit $rc
fi

#
# remaining args are the list of components to install
#

selcomponents=$*

#
# Check that this looks like a product - this needs doing even if we're
# removing if no components have been entered because that's how we get
# the list of components to remove
#

if [ ! "$removeonly" -o ! "$selcomponents" ]; then
    check_product
fi

#
# Extract product name
#

product=`get_product_name`

#
# See if rmshost is defined and if this node is it (sets rmshostundefined
# and isrmshost)
#    

check_rmshost

#
# see if rmsquery is available and working
#
check_rmsquery
	
# Show a message about the node

str="$sname: "
if [ "$removeonly" ]; then
   str=$str"Removing ${compstr}s from node $nodename "
else
   str=$str"Installing ${compstr}s on node $nodename "
fi
if [ "$isrmshost" ]; then
   str=$str"(rmshost)"
fi

printf "$str\n"
printf "------------------------------------------------------------\n"

if [ -d $productdefdir ]; then
    productdeffile=`ls $productdefdir`
    if [ "$productdeffile" ]; then
	productdefversion=`get_productvers_from_product $productdeffile`
	productdefname=`get_productname_from_product $productdeffile`
    else
        warning "no product definition file in $productdefdir\n"
    fi
else
    warning "no product definition directory $productdefdir\n"
fi

if [ ! "$removeonly" ]; then
    #
    # Installation
    #
    #
    # Keep a list of all components installed in a temporary file
    # to use to update the database at the end of the installation
    #

    touch $tmpcompfile
   
    #
    # set up 'components'  - the potential list of components to install
    #

    localcomponents=`get_local_components`

    if [ "$selcomponents" ]; then
        # components to install has been entered by user

	# check that selected components are present in the local product
	# the entered component name doesn't have to have a version

	for selcomponent in $selcomponents; do
	    found=""
	    for localcomponent in $localcomponents; do
	        # components can be entered with or without versions 
		# where this is appropriate to the OS
	        localname=`get_componentname_from_component $localcomponent`
	        localvers=`get_componentvers_from_component $localcomponent`

		if [ "$localcomponent" = "$selcomponent" -o \
		      "$localname"     = "$selcomponent" ]; then
		    tmpcomponents=${tmpcomponents}$localcomponent" "
		    found="1"
		    break
		fi
	    done
	    if [ ! "$found" ]; then
		errorexit "$compstr $selcomponent not found in $componentbase"
	    fi
	done
	# send the component list off to makecomplist to sort it if necc.

	componentstoinstall=`makecomplist "$tmpcomponents"`
    else
	# set up the list based on the product contents

	componentstoinstall=`makecomplist "$localcomponents"`
    fi

    #
    # see if we need to know rmshost
    #

    if (rmshostneeded "$componentstoinstall"); then
        # won't return from check_rmsenv if anything wrong
        check_rmsenv
    fi

    if [ "$verbose" ]; then
        printmsg "will install the following ${compstr}s ..."
        for component in $componentstoinstall; do
	    printf "\t$component\n"
        done
    fi    

    # components will be removed in reverse order

    revcomponents=`printf "$componentstoinstall\n" | \
	$AWK '{ for (i = NF; i > 0; --i) printf ("%s ", $i ) }'`

    remove=""

    # See if we have to remove any installed components first. Because of 
    # potential problems with the order of removals and installations 
    # if we have to remove any component we remove all in the list.

    check_remove "$componentstoinstall"

    if [ "$remove" ]; then
	if [ "$verbose" ]; then 
	    printmsg "prior to installation - will remove the installed versions"
            echo "            of the following ${compstr}s ..."
	    # only display the component names - no version numbers
	    for component in $revcomponents; do
	        compname=`get_componentname_from_component $component`
	        printf "\t $compname\n"
	    done
	fi
        for component in $revcomponents; do
	    remove_component $component
	done
    else
       revcomponents=""
    fi

    #
    # now do the installations ...
    #

    for component in $componentstoinstall; do
	install_component $component
    done

    #
    # Release notes and product definitions copied
    #
    if [ "$isrmshost" ]; then
       copy_relnotes
    fi

    copy_productdef

    if [ "$isrmshost" ]; then
       update_productTable
    fi

    update_installedTable

    /bin/rm -f $tmpcompfile
else
    #
    # Removal
    #

    # set up list of components to remove

    if [ "$selcomponents" ]; then
	revcomponents=$selcomponents
    else
	#
	# get the list of subsets in the product, sort and reverse it
	#
	localcomponents=`get_local_components`
	components=`makecomplist "$localcomponents"`
	revcomponents=`printf "$components\n" | $AWK '{ for (i = NF; i > 0; --i) printf ("%s ", $i ) }'`
    fi

    if [ "$verbose" ]; then
        echo "$sname : Will remove the following ${compstr}s ..."
        for rcomponent in $revcomponents; do
	    printf "\t$rcomponent\n"
        done
    fi
    for rcomponent in $revcomponents; do
	remove_component $rcomponent
    done

fi

printmsg "all done"

exit 0
