#!/bin/sh
# Script Name: install
# Description: ShutdownAgent Installation Program
# Platform: Linux

INSTALL_PATH="/usr/local/upsagent"
SRC_PATH="sa2012/"
RCD_PATH="/etc/init.d"

check_user()
{
	set `/usr/bin/whoami`
	if [ $1 != "root" ]; then
		echo "You must login as a root to execute the program."
		echo "Bye !"
		exit
	fi
}

check_exist()
{
	exist=`/bin/ps x | grep upsagentd | awk '$5 ~ /\x2fupsagentd/ { print $1 }'`
	if [ -n "$exist" ]; then
		echo "The ShutdownAgent(upsagentd) daemon is running, please stop it before installation."
		echo "Bye !"
		exit
	fi
}

disp_title()
{
	clear
	echo "+----------------------------------------------+"
	echo "|  ShutdownAgent 2012 for Linux                |"
	echo "|  Copyright (c) 2012 Delta Electronics, Inc.  |"
	echo "|  All Rights Reserved.                        |"
	echo "+----------------------------------------------+"
	echo
}

delay()
{
	echo -n "."
}

start_upsd()
{
	echo
	echo -n "Starting ShutdownAgent(upsagentd) ... "
	$INSTALL_PATH/upsagentd 2>/dev/null
 	echo "done"
	echo
	echo -n "Do you want to configure the ShutdownAgent right now? [y|n] "
	read start
	if [ $start = "y" ]; then
		$INSTALL_PATH/configure
	fi
}		

install_files()
{
	disp_title
	echo "The destination directory is" $INSTALL_PATH"."
	if [ ! -d $INSTALL_PATH ]; then
		mkdir -p $INSTALL_PATH
	fi
	chmod u+wrx,go+rx-w $INSTALL_PATH
	echo
	cd $SRC_PATH
	echo -n "Copying files ."
	/bin/cp ./upsagentd $INSTALL_PATH
	delay
	/bin/cp ./configure $INSTALL_PATH
	delay
	/bin/cp ./uninstall $INSTALL_PATH 
	delay
	/bin/cp ./shutdownagent $INSTALL_PATH
	delay
	/bin/cp -r ./command $INSTALL_PATH
	delay
	/bin/cp -r ./httpd $INSTALL_PATH
	delay
	/bin/cp -r ./language $INSTALL_PATH
	delay
	chmod u+wrx,go-wrx $INSTALL_PATH/upsagentd
	delay
	chmod u+wrx,go-wrx $INSTALL_PATH/configure
	delay
	chmod u+wrx,go-wrx $INSTALL_PATH/uninstall
	delay
	chmod u+wrx,go-wrx $INSTALL_PATH/shutdownagent
	delay
	chmod u+wrx,go-wrx $INSTALL_PATH/command/*
	delay
	if [ -d /etc/init.d/ ]; then
		$RCD_PATH = "/etc/init.d"
	else
		$RCD_PATH = "/etc/rc.d"
	fi
	/bin/cp -f $INSTALL_PATH/shutdownagent $RCD_PATH
	delay
	ln -s /tmp $INSTALL_PATH/httpd/htdocs/tempage
	delay
	service_link
	start_upsd
	cd ..
	echo
	echo "Installation process completed."
	echo
	echo "ShutdownAgent has been installed in the /usr/local/upsagent."
	echo
}		

ask_install()
{
	echo -n "Do you want to install the ShutdownAgent? [y|n] "
	read answer
	if [ $answer = "y" ]; then
		install_files
	else
		echo "Bye !"
		exit
	fi
}

service_link() {
	if [ -f /sbin/chkconfig ]; then
		echo
		echo "Install service link by chkconfig"
		echo
		/sbin/chkconfig --add shutdownagent 2>/dev/null
		/sbin/chkconfig shutdownagent on 2>/dev/null
	else
		if [ -f /usr/sbin/update-rc.d ]; then
			echo
			echo "Install service link by update-rc.d"
			echo
			/usr/sbin/update-rc.d shutdownagent defaults 2>/dev/null
		else
			if [ -d /etc/rc.d/rc3.d/ ]; then
				/bin/ln -s $RCD_PATH/shutdownagent /etc/rc.d/rc3.d/S99shutdownagent 2>/dev/null
				/bin/ln -s $RCD_PATH/shutdownagent /etc/rc.d/rc3.d/K99shutdownagent 2>/dev/null
				/bin/ln -s $RCD_PATH/shutdownagent /etc/rc.d/rc5.d/S99shutdownagent 2>/dev/null
				/bin/ln -s $RCD_PATH/shutdownagent /etc/rc.d/rc5.d/K99shutdownagent 2>/dev/null
			else
				if [ -d /etc/rc3.d/ ]; then
					/bin/ln -s $RCD_PATH/shutdownagent /etc/rc3.d/S99shutdownagent 2>/dev/null
					/bin/ln -s $RCD_PATH/shutdownagent /etc/rc3.d/K99shutdownagent 2>/dev/null
					/bin/ln -s $RCD_PATH/shutdownagent /etc/rc5.d/S99shutdownagent 2>/dev/null
					/bin/ln -s $RCD_PATH/shutdownagent /etc/rc5.d/K99shutdownagent 2>/dev/null
				fi
			fi
		fi
	fi
}

remove_files()
{
	rm -rf ./sa2012
	rm -f ./install
}

#######################################
# main loop
#######################################
disp_title
check_user
check_exist
ask_install
remove_files
