#!/bin/sh
#
# HighPoint ATA RAID Management Software Installation Script
#

[ $# -lt 1 ] && exit 1

HERE=`pwd`
#GTKVER=`gtk-config --version`
GTKVER="1.2.6"

if [ $? -ne 0 ] ; then
	echo "gtk library not installed"
	exit 1
fi

HPTRAID=`echo -n hptraid;echo $GTKVER`

case $1 in
-i) 
	pidof hptraid > /dev/null
	if [ $? -eq 0 ] ; then
	  echo "The software is running. Please stop it first."
	  exit 1
	fi 

	if [ ! -f $HERE/hptraid.tar.gz ] ; then
		echo "Software package not found"
		exit 1
	fi

	if [ -f /usr/bin/hptraid ] ; then
		while true
		do
			echo -n "The software has been installed. Do you want reinstall it?(Y/N)"
		 	read yorn
		 	case $yorn in
		 	[yY])
		 		break
		 		;;
		 	[nN])
		 		exit 1
		 		;;
		 	*)
		 		continue
		 		;;
		 	esac
		done
		rm -f /usr/bin/hptraid
		rm -rf /usr/bin/res
		rm -rf /usr/share/doc/hpt/help
		rm -f /usr/bin/hptsvr
		rm -rf /usr/share/gnome/apps/Applications/hptraid.desktop
		rm -rf /usr/share/applnk/Applications/hptraid.desktop						
		rm -f /etc/rc.d/rc3.d/S99hptsvr
		rm -f /etc/rc.d/rc4.d/S99hptsvr
		rm -f /etc/rc.d/rc5.d/S99hptsvr
		rm -f /etc/rc.d/rc1.d/K99hptsvr
		rm -f /etc/rc.d/rc2.d/K99hptsvr
		rm -f /etc/rc.d/rc6.d/K99hptsvr
		rm -f /usr/bin/hpthelp
		/etc/rc.d/init.d/hptdaemon stop
		rm -f /etc/rc.d/init.d/hptdaemon
	fi
	
	cd /tmp
	gzip -dc $HERE/hptraid.tar.gz | tar -xv >/dev/null 2>/dev/null
	if [ -f $HPTRAID ] ; then
		cp -f $HPTRAID /usr/bin/hptraid
	else
		echo "There is no suitable gtk library on your system."
		echo "The software may still work, but unexpected error may occur."
		cp -f hptraid1.2.6 /usr/bin/hptraid
	fi
	chmod 544 /usr/bin/hptraid >/dev/null 2>/dev/null
	mkdir -p /usr/share/gnome/apps/Applications/ >/dev/null 2>/dev/null
	mkdir -p /usr/share/applnk/Applications/ >/dev/null 2>/dev/null
	mkdir -p /usr/share/doc/hpt/ >/dev/null 2>/dev/null
	cp -f hptraid.desktop /usr/share/gnome/apps/Applications/
	cp -f hptraid.desktop /usr/share/applnk/Applications/
	cp -rf res /usr/bin/
	cp -rf help /usr/share/doc/hpt/
	cp -f hptsvr /usr/bin/
	cp -f $HERE/hptinstall.sh /usr/bin/
	chmod a+x /usr/bin/hptinstall.sh
	cp -f hptdaemon /etc/rc.d/init.d/
	chmod a+x /etc/rc.d/init.d/hptdaemon
	ln -s /etc/rc.d/init.d/hptdaemon /etc/rc.d/rc3.d/S99hptsvr
	ln -s /etc/rc.d/init.d/hptdaemon /etc/rc.d/rc4.d/S99hptsvr
	ln -s /etc/rc.d/init.d/hptdaemon /etc/rc.d/rc5.d/S99hptsvr
	ln -s /etc/rc.d/init.d/hptdaemon /etc/rc.d/rc1.d/K99hptsvr
	ln -s /etc/rc.d/init.d/hptdaemon /etc/rc.d/rc2.d/K99hptsvr
	ln -s /etc/rc.d/init.d/hptdaemon /etc/rc.d/rc6.d/K99hptsvr  
	if [ -f /usr/bin/netscape ] ; then
		ln /usr/bin/netscape /usr/bin/hpthelp
	elif [ -f /usr/bin/mozilla ] ; then
		ln /usr/bin/mozilla /usr/bin/hpthelp
	else 
		echo "Warning: Can not find a HTML browser."
		echo "To view help, please make the symbol link /usr/bin/hpthelp to"
		echo "your browser manually."
	fi                

	rm -f hptraid	      
	rm -f hptraid.desktop
	rm -f hptsvr
	rm -rf res
	rm -rf help
	rm -f hptdaemon
	/etc/rc.d/init.d/hptdaemon start
	echo "HighPoint ATA RAID Management Software has been installed successfully!"
	cd $HERE
	;;
-u)
	pidof hptraid > /dev/null
	if [ $? -eq 0 ] ; then
	  echo "The software is running. Please stop it first."
	  exit 1
	fi 
	if [ -f /usr/bin/hptraid ] ; then	
		while true
		do
			echo -n "Are you sure to uninstall HighPoint ATA RAID Management Software?(Y/N)"
			read yesorno
			case $yesorno in
			[yY])
				rm -rf /usr/bin/hptraid
				rm -rf /usr/bin/res
				rm -rf /usr/share/doc/hpt/help
				rm -f /usr/bin/hptsvr
				rm -f /usr/bin/hptinstall.sh	 
				rm -f /usr/share/gnome/apps/Applications/hptraid.desktop
				rm -f /usr/share/applnk/Applications/hptraid.desktop				
				rm -f /etc/rc.d/rc3.d/S99hptsvr
				rm -f /etc/rc.d/rc4.d/S99hptsvr
				rm -f /etc/rc.d/rc5.d/S99hptsvr
				rm -f /etc/rc.d/rc1.d/K99hptsvr
				rm -f /etc/rc.d/rc2.d/K99hptsvr
				rm -f /etc/rc.d/rc6.d/K99hptsvr	
				rm -f /usr/bin/hpthelp 
				/etc/rc.d/init.d/hptdaemon stop
				rm -f /etc/rc.d/init.d/hptdaemon
				echo "Uninstall fininshed!" 
				break
				;;
			[nN])
				break
				;;
			*)
				continue
				;;
			esac
		done 
	fi 	
    ;;
*)
	exit 1
	;; 
esac
exit 0
