#!/bin/bash

# Pandora FMS Server Upgrader (c) 2008-2010 Artica ST
# Linux Version (generic), for SuSe and Debian/Ubuntu only
# other Linux distros could not work properly without modifications
# Please see http://www.pandorafms.org
# v3.2 Build 101122
# This code is licensed under GPL 2.0 license.
# **********************************************************************

MODE=$1

install () {

	echo "Installing new libraries and server executables...."
	perl Makefile.PL
	make
	make install

	echo "Setting permissions..."

	chmod 750 bin/*
	chmod 750 pandora_* 
	chmod 750 util/*.pl
	chmod 750 util/*.sh
	chmod 750 util/tentacle_serverd
	chmod 750 util/pandora_server
	chmod 750 util/plugin/*

	echo "Copying utilities...."
	cp -R util /usr/share/pandora_server
	
	echo "Rebuilding links /usr/local/bin -> /usr/bin"
	#rm /usr/local/bin/pandora_server 2> /dev/null
	#rm /usr/local/bin/pandora_exec 2> /dev/null
	ln -s /usr/local/bin/pandora_server /usr/bin/pandora_server 2> /dev/null
	ln -s /usr/local/bin/pandora_exec /usr/bin/pandora_exec 2> /dev/null

	if [ -d /etc/logrotate.d ]; then
		echo "Creating logrotate.d entry for Pandora FMS log management"
		cp util/pandora_logrotate /etc/logrotate.d/pandora
	else
		echo "Please add a log rotation schedule manually to your log rotation daemon (if any)"
	fi

	echo "Copying startup scripts to /etc/init.d...."
	cp util/pandora_server /etc/init.d
	cp util/tentacle_serverd /etc/init.d

	if [ ! -e /etc/cron.hourly/pandora_db ]
	then
		echo "You don't have a /etc/cron.hourly/pandora_db file, you should consider"
		echo "to setup the Pandora FMS Database Maintance script. Create with 755 perms a file in /etc/cron.hourly "
		echo "called 'pandora_db', with following contents: "
		echo " "
		echo "	perl /usr/share/pandora_server/util/pandora_db.pl /etc/pandora/pandora_server.conf"
	fi

	echo " "
	echo "DONE!. Don't forget to restart all your Pandora FMS servers running in this machine"
	echo " "
}

help () {
	echo "	--upgrade	To upgrade Pandora FMS Servers on this system (You have to be root)"
	echo " "
}

# Script banner at start
echo " "
echo "Pandora FMS 5.0 Server Upgrade (c) 2009-2013 Artica ST"
echo "This program is licensed under GPL2 Terms. http://pandorafms.org"
echo " "

case "$MODE" in

'--upgrade')
	install
	exit
	;;

*)
	help
esac