#!/bin/sh
#
# Install script for Adaptec Storage Manager - Browser Edition application suite
#

SILENT_INSTALL=0
INSTALLER_ARGS=""
TEXT_SETUP=0

prompt()
{
    if [ -z "$promptOS" ]; then
        promptOS=`uname -s`
        if [ "${promptOS}" = "Linux" -o "${promptOS}" = "SCO_SV" ]; then
            noNLopt="-n"
            noNLescape=""
        elif [ "${promptOS}" = "UnixWare" -o "${promptOS}" = "OpenUNIX" ]; then
            noNLopt=""
            noNLescape="\c"
        else
            noNLopt=""
            noNLescape=""
        fi
    fi

    msg=""
    if [ ! -z "$1" ]; then
        msg=$1
    fi

    echo $noNLopt "${msg}${noNLescape}"
}

Usage()
{
	echo "Usage:"
	echo "	`basename $0` [ --text|--silent|--help|-h|--installerArgs="arguments to pass to native OS installer" ]"
	echo
	echo "	--text bypasses GUI installation and enters interactive text setup."
	echo "	--silent installs without prompting for installation type."
	echo "		Default install is chosen."
	echo "	--installerArgs=\"<native OS installer args>\" passes arguments to"
	echo "		native OS installer."
	echo "		Example: `basename $0` --installerArgs=\"--root /mnt\""
        exit 0
}

# Parse arguments
while [ "$1" != "" ]; do
	if `echo $1 | grep "\--installerArgs=" > /dev/null 2>&1`; then
		#
		# The user passed in an argument to the native OS installer using the
		# --installerArgs argument.
		# Example: ./install --installerArgs="--root /mnt"
		#
		INSTALLER_ARGS="`echo $1 | cut -d'=' -f2-`"
		continue;
	fi
	case "$1" in
		--silent )	SILENT_INSTALL=1;;
		--text	 )	TEXT_SETUP=1;;
		-h|--help)	Usage;;
	esac
	shift 1;
done

# Find the location of this install program.
CURDIR=`dirname "$0"`
cd "$CURDIR"
SETUPPATH=`pwd`
PATH=.:$PATH
export PATH

MISMATCH=0
if echo `pwd` |grep "packages" > /dev/null; then
	# Verify that the user is installing the correct version of the installer
	# for the target OS
	THISOS="`uname -s`"
	MISMATCH=1
	if [ "${THISOS}" = "Linux" ] && echo `pwd` | grep -i "linux" > /dev/null; then
		MISMATCH=0
	elif [ "${THISOS}" = "SCO_SV" ] && echo `pwd` | grep -i "openserver" > /dev/null; then
		MISMATCH=0
	elif [ "${THISOS}" = "UnixWare" -o "${THISOS}" = "OpenUNIX" ] && echo `pwd` | grep -i -e "openunix" -e "unixware" > /dev/null; then
		MISMATCH=0
	fi
fi

if [ ${MISMATCH} -eq 1 ]; then
	echo "The install that was invoked does not match the target OS."
	exit 1
fi

# Check if we are in X-Windows... if so, run the GUI if user did not specify
# text mode.
if [ "x$DISPLAY" != "x" -a $SILENT_INSTALL -eq 0 -a $TEXT_SETUP -eq 0 ]
then
	VIEW_IMAGE="stage2"
	"${SETUPPATH}/$VIEW_IMAGE" > /dev/null 2> /dev/null
        if [ $? -ne 0 ]; then
            echo "Problem encountered during install attempt." >&2
		else
			if [ "`uname -s`" = "Linux" ]; then
				/etc/init.d/adaptec-smbe stop
				cd /; /etc/init.d/adaptec-smbe start; cd -
			fi
        fi
	exit
fi

# Text setup starts from here on.

# If none of the AUTO environment variables are set and SILENT_INSTALL is not
# set, do an interactive install.
if [ "$ADPTAUTONOTIFIER" = "" -a "$ADPTAUTOWEBSERVER" = "" -a "$ADPTAUTOMANAGEDSYSTEM" = "" -a "$ADPTAUTOSNMP" = "" -a "$ADPTAUTODRIVER" = "" -a $SILENT_INSTALL -eq 0 ]
then


    webServer="Web Server"
    managedSys="Managed System"
    eventNot="Event Notification"
    snmpAgent="SNMP Agent"
    driver="Driver"
   
    enabled_components=""
    if [ "${THISOS}" = "Linux" ]; then
        valid_components="${webServer}, ${managedSys}, ${eventNot}, ${snmpAgent}, ${driver}"
    elif [ "${THISOS}" = "SCO_SV" -o "${THISOS}" = "UnixWare" -o "${THISOS}" = "OpenUNIX" ]; then
        valid_components="${managedSys}, ${eventNot}, ${snmpAgent}"
    fi


    default_components=`echo ${valid_components}|sed -e "s/, ${snmpAgent}//"`
    if [ "${managedSys}, ${eventNot}" = "${default_components}" ]; then
        do_skip_eventNot_component="yes"
    else
        do_skip_eventNot_component="no"
    fi


    while true
    do
        component_list=${valid_components}
        count=0

        count=`expr $count + 1`
        echo "    ${count}.   ${default_components} Components [Default]"

        while [ ! -z "${component_list}" ];
        do
            component=`echo ${component_list} | sed -e "s/^\([^,]*\), .*$/\1/"`
            component_list=`echo ${component_list} | sed -e "s/^${component}//" -e "s/^, //"`

            if [ "${component}" = "${eventNot}" -a "${do_skip_eventNot_component}" = "yes" ]; then
                continue
            fi

            count=`expr $count + 1`

            if [ "${component}" != "${eventNot}" ]; then
                echo "    ${count}.   ${component} Component Only"
            else
                echo "    ${count}.   ${managedSys}, ${eventNot} Components"
            fi
        done

        count=`expr $count + 1`
        echo "    ${count}.   Full - ${valid_components} Components"

        prompt "Please enter an installation type (1 - ${count}) [1]: "
        read choice
        if [ -z "$choice" ]; then
            choice=1
        fi

        if [ $choice -ge 1 -a $choice -le $count ]; then
            break;
        else
            echo "Enter a number between 1 and ${count}"
        fi
    done

    if [ $choice -eq 1 ]; then
        selected_components="${default_components}"
    elif [ $choice -eq $count ]; then
        selected_components="${valid_components}"
    else
        component_list=${valid_components}
        count=1
        while [ $count -ne $choice ];
        do
            component=`echo ${component_list} | sed -e "s/^\([^,]*\), .*$/\1/"`
            component_list=`echo ${component_list} | sed -e "s/^${component}//" -e "s/^, //"`
            if [ "${component}" = "${eventNot}" -a "${do_skip_eventNot_component}" = "yes" ]; then
                continue
            fi

            count=`expr $count + 1`
        done
        selected_components="${component}"
        if [ "${component}" = "${eventNot}" ]; then
            selected_components="${managedSys}, ${selected_components}"
        fi
    fi

    isComponentSelected()
    {
        echo "$1" | grep "$2" > /dev/null 2> /dev/null
        if [ $? -eq 0 ]; then
            echo yes
        else
            echo no
        fi
    }

    ADPTAUTOWEBSERVER=`isComponentSelected "${selected_components}" "${webServer}"`
    ADPTAUTOMANAGEDSYSTEM=`isComponentSelected "${selected_components}" "${managedSys}"`
    ADPTAUTONOTIFIER=`isComponentSelected "${selected_components}" "${eventNot}"`
    ADPTAUTOSNMP=`isComponentSelected "${selected_components}" "${snmpAgent}"`
    ADPTAUTODRIVER=`isComponentSelected "${selected_components}" "${driver}"`


fi

#
# kernelRebuildNeeded is specific to OpenServer
# NOTE: Currently this is in several places which is not good. It is
# in preinstall, the install script (install), and in osdsco5.cpp of
# the install gui.
#
kernelRebuildNeeded()
{
   result="no"

   for input  in `cat /etc/conf/sdevice.d/suds /etc/conf/cf.d/sdevice | \
                  awk '{ if ($1 == "suds") print $2 }'`
   do
      if [ $input = "N" -o $input = "n" ]; then
          result="yes"
          break
      fi
   done

   echo $result
   return
}



if [ "${THISOS}" = "SCO_SV" ]; then
    if [ "`kernelRebuildNeeded`" = "yes" ]; then
         echo The Adaptec Storage Manager - Browser Edition requires
         echo a feature of the kernel that is not currently configured
         echo into your system. Enabling this feature requires the kernel
         echo to be rebuilt and the system rebooted. If the installation
         echo process is commenced, it will rebuild the kernel and make
         echo the new kernel the default boot kernel. The kernel environment
         echo will also be rebuilt.  After the installation is complete the
         echo system will need to be rebooted.
         echo
         echo

         if [ -z "${ADPTAUTOKERNELREBUILD}" ]; then
             if [ $SILENT_INSTALL = "1" ]; then
                 echo "Cannot proceed with silent installation"
                 echo "Aborting installation"
                 exit 1
             fi

             echo Proceed with setup if it is okay to rebuild the kernel.
             prompt "Continue with installation (Y/N): "
             read ans
         else
             if [ "${ADPTAUTOKERNELREBUILD}" = "yes" ]; then
                 ans=Y
             else
                 ans=N
             fi
         fi
         if [ $ans != "Y" -a $ans != "y" ]; then
             echo "Aborting installation"
             exit 1
         fi
         ADPTAUTOKERNELREBUILD=yes
    fi
fi


if [ "${THISOS}" = "Linux" ]; then

	# Get the packages to be installed.
	SMBE_RPM=`/bin/ls |grep -i "adaptec-smbe.*rpm"`
	BROWSER_RPM=`/bin/ls |grep -i "adaptec-browser.*rpm"`
	DRIVER_RPM=`/bin/ls |grep -i "aacraid.*rpm"`

        # Do the actual installation
	if [ "$ADPTAUTODRIVER" = "yes" ]; then
		rpm -e `echo $DRIVER_RPM | cut -f1 -d'-'` 2> /dev/null
		rpm -ivh $INSTALLER_ARGS $DRIVER_RPM
	fi

	export ADPTAUTOWEBSERVER ADPTAUTOMANAGEDSYSTEM ADPTAUTOSNMP
	rpm -e `echo $SMBE_RPM | sed "s,-[0-9].*$,,"` 2> /dev/null
	rpm -ivh $INSTALLER_ARGS $SMBE_RPM
	if [ "$ADPTAUTOWEBSERVER" = "yes" ]; then
		rpm -e `echo $BROWSER_RPM | sed "s,-[0-9].*$,,"` 2> /dev/null
		rpm -ivh $INSTALLER_ARGS $BROWSER_RPM
		/etc/init.d/adaptec-smbe stop
		[ -f addcert.sh ] && sh addcert.sh
		cd /; /etc/init.d/adaptec-smbe start; cd -
	fi
elif [ "${THISOS}" = "SCO_SV" -o "${THISOS}" = "UnixWare" -o "${THISOS}" = "OpenUNIX" ]; then

	# Get the packages to be installed.
	pkgname=ADPTsmbe
	pkgresponse=/tmp/pkgresponse_$$
        smbepkgfile=`/bin/ls -1 |egrep -i "^${pkgname}.*\.pkg$"`
	if [ -f "${smbepkgfile}" ]; then
		echo "ADPTAUTOWEBSERVER=\"$ADPTAUTOWEBSERVER\"" >> ${pkgresponse}
		echo "ADPTAUTOMANAGEDSYSTEM=\"$ADPTAUTOMANAGEDSYSTEM\"" >> ${pkgresponse}
		echo "ADPTAUTOSNMP=\"$ADPTAUTOSNMP\"" >> ${pkgresponse}
		echo "ADPTAUTONOTIFIER=\"$ADPTAUTONOTIFIER\"" >> ${pkgresponse}
                if [ ! -z "${ADPTAUTOKERNELREBUILD}" ]; then
			echo "ADPTAUTOKERNELREBUILD=\"$ADPTAUTOKERNELREBUILD\"" >> ${pkgresponse}
                fi
		pkgadd -d `pwd`/${smbepkgfile} -r ${pkgresponse} -n $INSTALLER_ARGS  ${pkgname}
		stat=$?
		if [ "${stat}" != "0" ]; then
			if [ $stat -ge 10 -a $stat -le 13 ];then
    			echo "System should be rebooted when installation of all selected packages is completed."
    			stat=`expr $stat - 10`
			elif [ $stat -ge 20 -a $stat -le 23  ];then
    			echo "System should be rebooted immediately upon completing installation of the current package."
    			stat=`expr $stat - 20`
			fi

		fi
		if [ $stat -eq 0 ]; then
    		echo "Successful installation of package."
		else
    		echo "Problem encountered during installation of package."
		fi
		rm -f ${pkgresponse}
	else
		echo "Error, cannot locate package to install"
	fi

fi

