#!/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.3-0
pkg_mid="rhe3"
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
ldconf=/etc/ld.so.conf


arch_=`arch`
if [ $arch_ = "x86_64" ]|| [ $arch_ = "ia64" ] || !(uname -r | grep -sqE  '2\.4\.20-8|2\.4\.21-4\.EL|2\.4\.21-15\.EL')
then
	echo -e "ASM agent only support the below Linux distributions:\n\nRed Hat Linux release 9\nRed Hat Enterprise Linux AS release 3\nRed Hat Enterprise Linux AS release 3 Update 2 (32 bit)\n\nAbort!"
	exit 1
fi


case $1 in
        install)
                tmp=`uname -r`
                if echo $tmp | grep "2\.4\.18-3" 1>/dev/null 2>/dev/null ; then
                        pkg_mid="rh73"
                fi
                if echo $tmp | grep "2\.4\.18-14" 1>/dev/null 2>/dev/null ; then
                        pkg_mid="rh80"
                fi
                if echo $tmp | grep "2\.4\.20-8" 1>/dev/null 2>/dev/null ; then
                        pkg_mid="rh90"
                fi
                if echo $tmp | grep "2\.4\.21-4" 1>/dev/null 2>/dev/null ; then
                        pkg_mid="rhe3"
                fi
                pkg_name=$pkg_key"."$pkg_mid"."$pkg_append
                rpm -i --force $pkg_name
                echo "/usr/local/share/pegasus/lib" >> $ldconf
                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
                        mv -f $ldconf $ldconf.bak
                        sed '/\/usr\/local\/share\/pegasus\/lib/d' $ldconf.bak > $ldconf
                fi
                ;;
        *)
                echo "Usage: pgpkg {install | uninstall}"
                exit 1
esac

exit 0
