#!/bin/sh
#****************************************************************
# *	Copyright (C) 2003, Wistron Inc.  All Rights Reserved.
# *	File Name:		
# *	Author:			Jerry Zhou
# *	Create Time: 	2003-05-15 15:06:12
# *	Last Update: 	2003-05-29 13:50:04 by Jerry Zhou 
#****************************************************************
pkg_key=pegasus-2.2-5
pkg_mid="rh73"
pkg_append=i386.rpm
build_file=buildrepository
pegasus_exec=/usr/local/share/pegasus/cim_server
pegasus_usertool=/usr/local/share/pegasus/cimauthconf
pega_daemon=pegasusd
pega_agent=pegasusdagent
init_path=/etc/init.d
rc3_path=/etc/rc3.d
tool_path=/usr/bin
pega_shellfile=pgpkg

case $1 in
	install)
		kernelVersionValid=`uname -r | sed "s/-[0-9a-zA-Z]*//g" | awk -F. '{
			if ($1 > 2)
			{ print 3 }
			else
			{
				if ($2 > 4)
				{ print 3 }
				else
				{
					if ($3 > 18)
					{ print 3 }
					else
					{
						if ($3 == 18)
						{	print 2 }
						else
						{	print 1 }
					}
				}
			}
		}'`
		if [ $kernelVersionValid = 3 ] ; then
			pkg_mid="rh80"
		else
			if [ $kernelVersionValid = 2 ] ; then
				kernelVersionValid=`uname -r | sed "s/[0-9a-zA-Z\.]*-//g" | sed "s/[a-zA-Z]//g"`
				if [ $kernelVersionValid -ge 14 ] ; then
					pkg_mid="rh80"
				fi
			fi
		fi
		if uname -r | grep "2\.4\.20-8" 1>/dev/null 2>/dev/null ; then
			pkg_mid="rh90"
		fi
		pkg_name=$pkg_key"."$pkg_mid"."$pkg_append
		rpm -i --force $pkg_name
		echo "Config pegasus server now ..."
		./$build_file
		echo "Configuration complete!"
		cp -f $pega_agent $tool_path/
		chmod 755 $tool_path/$pega_agent
		cp -f $pega_daemon $init_path/
		chmod 755 $init_path/$pega_daemon
		ln -s $init_path/$pega_daemon $rc3_path/S99$pega_daemon
		$init_path/$pega_daemon start
		echo -n "Enable user authentication (Y)es/(N)o? [Y]"
		read addauthuser
		if [ -z $addauthuser ] || [ $addauthuser = y ] || [ $addauthuser = Y ] || [ $addauthuser = Yes ] || [ $addauthuser = yes ] ; then
			addauthuser=y
		else
			addauthuser=n
		fi
		cp -f ./cimauthconf $pegasus_usertool
		chmod a+x $pegasus_usertool
		if [ ! -z $addauthuser ] && [ $addauthuser = y ]; then
			while true
			do
				./cimauthconf add
				echo -n "Add another user name (Y)es/(N)o? [Y]"
				read anotheruser
				if [ -z $anotheruser ] || [ $anotheruser = y ] || [ $anotheruser = Y ] || [ $anotheruser = Yes ] || [ $anotheruser = yes ] ; then
					anotheruser=y
				else
					anotheruser=n
				fi
				if [ ! -z $anotheruser ] && [ $anotheruser = y ]; then
					continue
				else
					break
				fi
			done
			./cimauthconf enableauth
			echo "Restart pegasus server now ..."
			$pegasus_exec stop
			$pegasus_exec start
		else
			echo "NOTICE: You can use \""$pegasus_usertool" enableauth\" to enable user authentication. Please type "$pegasus_usertool" help\" for more information."
			echo -n "Start pegasus server now (Y)es/(N)o? [Y]"
			read runnow
			if [ -z $runnow ] || [ $runnow = y ] || [ $runnow = Y ] || [ $runnow = Yes ] || [ $runnow = yes ] ; then
				runnow=y
			else
				runnow=n
			fi
			if [ ! -z $runnow ] && [ $runnow = y ] ; then
				$pegasus_exec start
			else
				echo ""
				echo "NOTICE: You can use command \""$pegasus_exec" start\" to start pegasus server at once or reboot machine."
			fi
		fi
		cp -f $pega_shellfile $tool_path/
		echo ""
		;;
	uninstall)
		if rpm -qa | grep $pkg_key 1>/dev/null 2>/dev/null ; then
			installedpkgname=`rpm -qa | grep $pkg_key`
			rpm -e $installedpkgname
			$init_path/$pega_daemon stop 2>/dev/null
			rm -f $tool_path/$pega_agent $rc3_path/S99$pega_daemon $init_path/$pega_daemon
			rm -f $tool_path/$pega_shellfile
		fi
		;;
	*)
		echo "Usage: pgpkg {install | uninstall}"
		exit 1
esac

exit 0