#!/bin/sh
#****************************************************************
# *	Copyright (C) 2003, Wistron Inc.  All Rights Reserved.
# *	File Name:		asmsetup
# *	Author:			Jerry Zhou
# *	Create Time: 	2003-07-07 09:40:37
# *	Last Update: 	2003-07-17 10:09:34 by Jerry Zhou 
#****************************************************************
#must be root
if [ `/usr/bin/whoami` != "root" ] ; then
    echo "Abort! Must have root privilege to perform the operation!"
	exit 1
fi

shellfile=asmsetup
startfile=asmagent
procfile=wprocd
procfileagent=wprocdagent
init_path=/etc/init.d
rc3_path=/etc/rc3.d
tool_path=/usr/bin
asmagent_path=/usr/local/share/asmagent
compilepkginfo=$asmagent_path/wsma/createrpminfo
compulsory_pkg_list="wsmbdrv wconf wsma asm6p armc"
optional_pkg_list="hma rca"
modedetect=/usr/local/share/asmagent/wsmbdrv/smbiosinfo
drvfile=/etc/init.d/wsmbdrv_server
cimserverfile=/usr/local/share/pegasus/cim_server
osaagentfile=osaagentpkg
osaagentname=asm6agent
anypkginstall=no
case $1 in
	install)
		if rpm -qa|grep pegasus- 1>/dev/null 2>/dev/null; then
			echo ""
		else
			echo "pegasus package has not been installed yet! Please install it first! Abort!"
			exit 1
		fi
		if rpm -qa|grep ucd-snmp-4.2.3 1>/dev/null 2>/dev/null; then
			echo ""
		else
			echo "ucd-snmp-4.2.3 package has not been installed yet! Please install it first! Abort!"
			exit 1
		fi
		if rpm -qa|grep ^tcl- 1>/dev/null 2>/dev/null; then
			echo ""
		else
			echo "tcl-* package has not been installed yet! Please install it first! Abort!"
			exit 1
		fi

		clear
		echo ""
		echo " 		Welcome to ASM Setup"
		echo "		Copyright (c) 2003, Acer Inc."
		echo ""
		compulsory_pkg_installed=n
		for j in $optional_pkg_list
		do
			case $j in
				wsma)
					echo -n "Install System Monitor Agent (Y)es/(N)o? [Y]"
					;;
				hma)
					if [ -e ./wsmbdrvpkg ] ; then
						wsmbpkgfile=./wsmbdrvpkg
					else
						if [ -e $tool_path/wsmbdrvpkg ] ; then
							wsmbpkgfile=$tool_path/wsmbdrvpkg
						else
							continue
						fi
					fi
					$wsmbpkgfile install 1>/dev/null 2>/dev/null
					detectrst=NONE
					if [ -e $drvfile ] ; then
						$drvfile start 1>/dev/null 2>/dev/null
						if [ -e $modedetect ] ; then
							detectrst=`$modedetect -t 2>/dev/null`
						fi
						$drvfile stop 1>/dev/null 2>/dev/null
					fi
					$wsmbpkgfile uninstall 1>/dev/null 2>/dev/null
					#if [ -z $detectrst ] || [ $detectrst = NONE ] ; then
					#	continue #current machine doesn't support healthmon
					#fi
					echo -n "Install Health Monitor Agent (Y)es/(N)o? [Y]"
					;;
				armc)
					echo -n "Install ARMC Agent (Y)es/(N)o? [Y]"
					;;
				rca)
					echo -n "Install Remote Console Agent (Y)es/(N)o? [Y]"
					;;
			esac
			read installpkg
			if [ -z $installpkg ] || [ $installpkg = y ] || [ $installpkg = Y ] || [ $installpkg = Yes ] || [ $installpkg = yes ] ; then
				installpkg=y
			else
				installpkg=n
			fi
			if [ $installpkg = y ] ; then
				if [ $compulsory_pkg_installed = n ] ; then
					echo "Searching for dependent packages ... "
					if [ ! -e $init_path/$procfile ] ; then
						cp -f $procfileagent $tool_path/
						chmod 755 $tool_path/$procfileagent
						cp -f $procfile $init_path/
						chmod 755 $init_path/$procfile
						ln -s $init_path/$procfile $rc3_path/S99$procfile
						$init_path/$procfile start
					fi
					for i in $compulsory_pkg_list
					do
						if rpm -qa|grep $i 1>/dev/null 2>/dev/null ; then
							pkg_name=`rpm -qa|grep $i 2>/dev/null`
						else
							tool=$i"pkg"
							if [ -e $tool ] ; then
								./$tool install 1>/dev/null
								cp -f $tool $tool_path/
							fi
						fi
					done
					compulsory_pkg_installed=y
				fi
				tool=$j"pkg"
				if [ -e $tool ] ; then
					case $j in
						wsma)
							if rpm -qa|grep wsma- 1>/dev/null 2>/dev/null ; then
								continue #because wsma is a default installation package
								echo -n "System Monitor Agent has been installed. Overwrite it (Y)es/(N)o? [N]"
								read rewwsma
								if [ -z $rewwsma ] ; then
									rewwsma=n
								fi
								if [ $rewwsma = Y ] || [ $rewwsma = Yes ] || [ $rewwsma = yes ] ; then
									rewwsma=y
								fi
								if [ -z $rewwsma ] || [ $rewwsma != y ] ; then
									continue
								fi
							fi
							echo "Install System Monitor Agent ..."
							;;
						hma)
							if rpm -qa|grep hma- 1>/dev/null 2>/dev/null ; then
								echo -n "Health Monitor Agent has been installed! Please uninstall current package first! Abort!"
								echo ""
								continue
								echo -n "Health Monitor Agent has been installed. Overwrite it (Y)es/(N)o? [N]"
								read rewhma
								if [ -z $rewhma ] ; then
									rewhma=n
								fi
								if [ $rewhma = Y ] || [ $rewhma = Yes ] || [ $rewhma = yes ] ; then
									rewhma=y
								fi
								if [ -z $rewhma ] || [ $rewhma != y ] ; then
									continue
								fi
							else
								if rpm -qa|grep asm6agent- 1>/dev/null 2>/dev/null ; then
									echo -n "Health Monitor Agent has been installed! Please uninstall current package first! Abort!"
									echo ""
									continue
									echo -n "Health Monitor Agent has been installed. Overwrite it (Y)es/(N)o? [N]"
									read rewosahma
									if [ -z $rewosahma ] ; then
										rewosahma=n
									fi
									if [ $rewosahma = Y ] || [ $rewosahma = Yes ] || [ $rewosahma = yes ] ; then
										rewosahma=y
									fi
									if [ -z $rewosahma ] || [ $rewosahma != y ] ; then
										continue
									fi
								fi
							fi
							echo "Install Health Monitor Agent ..."
							if [ -e $drvfile ] ; then
								$drvfile start 1>/dev/null 2>/dev/null
								if [ -e $modedetect ] ; then
									if $modedetect -m | grep "Altos G310" -i 1>/dev/null 2>/dev/null; then
										tool=$osaagentfile
									fi
								fi
								$drvfile stop 1>/dev/null 2>/dev/null
							fi
							;;
						armc)
							if rpm -qa|grep armcagent- 1>/dev/null 2>/dev/null ; then
								continue #because armc is a default installation package
								echo -n "ARMC Agent has been installed! Please uninstall current package first! Abort!"
								echo ""
								continue
								echo -n "ARMC Agent has been installed. Overwrite it (Y)es/(N)o? [N]"
								read rewarmc
								if [ -z $rewarmc ] ; then
									rewarmc=n
								fi
								if [ $rewarmc = Y ] || [ $rewarmc = Yes ] || [ $rewarmc = yes ] ; then
									rewarmc=y
								fi
								if [ -z $rewarmc ] || [ $rewarmc != y ] ; then
									continue
								fi
							fi
							echo "Install ARMC Agent ..."
							;;
						rca)
							if rpm -qa|grep rc-agent- 1>/dev/null 2>/dev/null ; then
								echo -n "Remote Console Agent has been installed! Please uninstall current package first! Abort!"
								echo ""
								continue
								echo -n "Remote Console Agent has been installed. Overwrite it (Y)es/(N)o? [N]"
								read rewrca
								if [ -z $rewrca ] ; then
									rewrca=n
								fi
								if [ $rewrca = Y ] || [ $rewrca = Yes ] || [ $rewrca = yes ] ; then
									rewrca=y
								fi
								if [ -z $rewrca ] || [ $rewrca != y ] ; then
									continue
								fi
							fi
							echo "Install Remote Console Agent ..."
							;;
					esac
					./$tool install
					cp -f $tool $tool_path/
					anypkginstall=yes
				fi
			fi
		done
		if [ $anypkginstall = yes ] ; then
			cp -f $shellfile $tool_path/
			cp -f $startfile $tool_path/
			if [ -e $compilepkginfo ] ; then
				$compilepkginfo
			fi
			echo ""
			echo -n "Start agents now (Y)es/(N)o? [Y]"
			read startnow
			if [ -z $startnow ] || [ $startnow = y ] || [ $startnow = Y ] || [ $startnow = Yes ] || [ $startnow = yes ] ; then
				startnow=y
			else
				startnow=n
			fi
			if [ ! -z $startnow ] && [ $startnow = y ] ; then
				cd $tool_path
				$startfile start
			else
				echo "NOTICE: You can use command \""$startfile" start\" to start agents at once or reboot machine."
			fi
		fi
		echo ""
		;;
	uninstall)
		clear
		echo ""
		echo " 		Welcome to ASM Setup"
		echo "		Copyright (c) 2003, Acer Inc."
		echo ""
		echo "Searching ... "
		if [ -e $cimserverfile ] ; then
			echo "Stop pegasus server now ..."
			$cimserverfile stop
		fi
		for i in $optional_pkg_list
		do
			if [ $i = "armc" ] ; then
				pkg_name_tmp="^"$i"agent-"
			else
				if [ $i = "rca" ] ; then
					pkg_name_tmp="^rc-agent"
				else
					pkg_name_tmp="^"$i"-"
				fi
			fi
			if rpm -qa|grep $pkg_name_tmp 1>/dev/null 2>/dev/null ; then
				installed_pkg_name=`rpm -qa|grep $pkg_name_tmp`
				case $i in
					wsma)
						echo -n "System Monitor Agent has been installed! Remove it (Y)es/(N)o? [Y]"
						;;
					hma)
						echo -n "Health Monitor Agent has been installed! Remove it (Y)es/(N)o? [Y]"
						;;
					armc)
						echo -n "ARMC Agent has been installed! Remove it (Y)es/(N)o? [Y]"
						;;
					rca)
						echo -n "Remote Console Agent has been installed! Remove it (Y)es/(N)o? [Y]"
						;;
				esac
				read uninstall_pkg
				if [ -z $uninstall_pkg ] || [ $uninstall_pkg = y ] || [ $uninstall_pkg = Y ] || [ $uninstall_pkg = Yes ] || [ $uninstall_pkg = yes ] ; then
					uninstall_pkg=y
				else
					uninstall_pkg=n
				fi
				if [ $uninstall_pkg = y ] ; then
					echo "Checking for uninstall script ..."
					uninstall_script=$tool_path"/"$i"pkg"
					if [ -e $uninstall_script ] ; then
						$uninstall_script uninstall
						rm -f $uninstall_script
					else
						case $i in
							wsma)
								echo "Uninstall: script file "$uninstall_script" does not exist! Stop uninstalling system monitor agent."
								;;
							hma)
								echo "Uninstall: script file "$uninstall_script" does not exist! Stop uninstalling health monitor agent."
								;;
							armc)
								echo "Uninstall: script file "$uninstall_script" does not exist! Stop uninstalling armc agent."
								;;
							rca)
								echo "Uninstall: script file "$uninstall_script" does not exist! Stop uninstalling remote console agent."
								;;
						esac
					fi
				fi
			fi
		done
		still_exist=n
		for i in $optional_pkg_list
		do
			if [ $i = "wsma" ] || [ $i = "hma" ] ; then
				pkg_name_tmp="^"$i"-"
			else
				if [ $i = "rca" ] ; then 
					pkg_name_tmp="^rc-agent-"
				else
					continue
				fi
			fi
			if rpm -qa|grep $pkg_name_tmp 1>/dev/null 2>/dev/null ; then
				still_exist=y
				break
			fi
		done
		if rpm -qa|grep $osaagentname 1>/dev/null 2>/dev/null ; then
			echo -n "Health Monitor Agent has been installed! Remove it (Y)es/(N)o? [Y]"
			read osaaction
			if [ -z $osaaction ] || [ $osaaction = y ] || [ $osaaction = Y ] || [ $osaaction = Yes ] || [ $osaaction = yes ] ; then
				osaaction=y
			else
				osaaction=n
			fi
			if [ $osaaction = y ] ; then
				echo "Checking for uninstall script ..."
				uninstall_script=$tool_path"/"$osaagentfile
				if [ -e $uninstall_script ] ; then
					$uninstall_script uninstall
					if rpm -qa|grep $osaagentname 1>/dev/null 2>/dev/null ; then
						still_exist=y
					else
						rm -f $uninstall_script
					fi
				else
					echo "Uninstall: script file "$uninstall_script" does not exist! Stop uninstalling health monitor agent."
					still_exist=y
				fi
			else
				still_exist=y
			fi
		fi
		if [ $still_exist = n ] ; then
			echo "Prepare to remove dependent packages ..."
			find_depend_pkg=no
			for j in $compulsory_pkg_list
			do
				if [ $j = "armc" ] ; then
					grep_pkg_name="^"$j"agent-"
				else
					grep_pkg_name=$j
				fi
				if rpm -qa|grep $grep_pkg_name 1>/dev/null 2>/dev/null ; then
					find_depend_pkg=yes
					tool=$tool_path"/"$j"pkg"
					if  [ -e $tool ] ; then
						$tool uninstall
						rm -f $tool
					else
						echo "Uninstall: script file "$tool" does not exist! Stop uninstalling "$j" agent."
					fi
				fi
			done
			rm -f $tool_path/$shellfile
			rm -f $tool_path/$startfile
			rm -rf $asmagent_path
			if [ -e $init_path/$procfile ] ; then
				$init_path/$procfile stop
			fi
			rm -f $init_path/$procfile $rc3_path/S99$procfile $tool_path/$procfileagent
			if [ $find_depend_pkg = no ] ; then
				echo "Uninstall: none of installed packages was found!"
			fi
		fi
		if [ -e $cimserverfile ] ; then
			echo "Start pegasus server now ..."
			$cimserverfile start
		fi
		;;
	*)
		echo "Usage: asmsetup {install | uninstall}"
		exit 1
esac

exit 0	