#!/bin/bash

# Pandora FMS 4.0dev Console Upgrade (c) 2009-2011 Artica ST
# Please see http://www.pandorafms.com
# This code is licensed under GPL 2.0 license.
# **********************************************************************

PI_VERSION=4.0dev
USER=`whoami`

if [ $USER != "root" ]
then
	echo "Upgrade process need to be executed by root"
	exit -1
fi

MODE=$1

pandora_upgrade () {

	if [ ! -e "$PANDORAPATH/index.php" ]
	then
		echo "ERROR: Provided path for current Pandora FMS console, do not exist ($PANDORAPATH)"
		exit -1
	fi	
	echo "Installing new console code in $PANDORAPATH"
	cp -R * $PANDORAPATH

	# This makes the old-style config.php file usable with 3.x

	line=$(grep "?>" -n $PANDORAPATH/include/config.php | head -1 | cut -f1 -d:)
	line=$(($line - 1))
	head -$line $PANDORAPATH/include/config.php > /tmp/asdf
	echo "require_once ('config_process.php');" >> /tmp/asdf
	echo "?>" >> /tmp/asdf
	mv /tmp/asdf $PANDORAPATH/include/config.php
	
	# Upgrade Database ?
	
	if [ "$UPGRADEDB" == "1" ]
	then
		echo "Setting database schema changes"
		DBUSER=`cat $PANDORAPATH/include/config.php | grep dbuser | grep -v "^\/" | grep -o "\=\"[a-zA-Z0-9]*\"" | grep -o "[A-Za-z0-9]*"`
		DBPASS=`cat $PANDORAPATH/include/config.php | grep dbpass | grep -v "^\/" | grep -o "\=\"[a-zA-Z0-9]*\"" | grep -o "[A-Za-z0-9]*"`	
		DBHOST=`cat $PANDORAPATH/include/config.php | grep dbhost | grep -v "^\/" | grep -o "\=\"[a-zA-Z0-9]*\"" | grep -o "[A-Za-z0-9]*"`
		DBNAME=`cat $PANDORAPATH/include/config.php | grep dbname | grep -v "^\/" | grep -o "\=\"[a-zA-Z0-9]*\"" | grep -o "[A-Za-z0-9]*"`	
		cat extras/pandoradb_migrate_v3.1_to_v3.2.sql | mysql -f -u $DBUSER -p$DBPASS -h $DBHOST -D $DBNAME 	
	fi

	WWWUSER=`ls -la $PANDORAPATH/index.php | awk '{ print $3 }'` 
	echo "Setting permissions for $WWWUSER in $PANDORAPATH"
	chown -R $WWWUSER $PANDORAPATH
	if [ "$?" -ne 0 ]
	then
		echo "ERROR (Cannot change $PANDORAPATH to $WWWUSER ownership)!"
		return 1
	fi

	WWWGROUP=`ls -la $PANDORAPATH/index.php | awk '{ print $4 }'` 
	echo "Setting permissions for $WWWUSER in /var/spool/pandora/data_in"
	chgrp -R $WWWGROUP /var/spool/pandora/data_in
	if [ "$?" -ne 0 ]
	then
		echo "ERROR (Cannot change group ownership to /var/spool/pandora/data_in to $WWWGROUP)!"
		return 1
	fi

	# Remove installer file

	[ -f $PANDORAPATH/install.php ] && rm -f $PANDORAPATH/install.php

	# Only root should have read/write access to config.php

	chmod 600 $PANDORAPATH/include/config.php
	if [ "$?" -ne 0 ]
	then
		echo "ERROR (Cannot do chmod 600 $PANDORAPATH/include/config.php)!"
		return 1
	fi

	# Remove unnecessary files (optional, set variable to activate)

	REMOVEFILES=0
	if [ "${REMOVEFILES}" -eq 1 ]
	then
		rm -f $PANDORAPATH/AUTHORS
		rm -f $PANDORAPATH/COPYING
		rm -f $PANDORAPATH/ChangeLog
		rm -f $PANDORAPATH/pandora_console_install
		rm -f $PANDORAPATH/pandora_console_upgrade
		rm -f $PANDORAPATH/pandoradb.sql
		rm -f $PANDORAPATH/pandoradb_data.sql
	fi

	echo " "
	echo "DONE!"
	echo " "
	return 0
}

help () {
	echo "     -p <path>  Upgrade Pandora FMS Console in path (pe: /var/www/pandora_console)"
	echo "     -d         Upgrade also Database (by default not upgrade Database)           "
	echo " "
	exit 0
}

# Script banner at start
echo " "
echo "Pandora FMS $PI_VERSION Console Upgrade (c) 2009-2011 Artica ST"
echo "This program is licensed under GPL2 Terms. http://pandorafms.com"
echo " "

UPGRADEDB=0
UPGRADE=0

if [ $# -eq 0 ]
then
	help
fi

# Main parsing code

while getopts ":hdp:" optname
	do
		case "$optname" in
		"h")
			help
			;;
		"d")
			UPGRADEDB=1
			;; 
		"p")
			PANDORAPATH=$OPTARG
			UPGRADE=1
			;;
		?)
			help
			;;
		default) 
			help
			;; 
		esac
	done

if [ "$UPGRADE" == "1" ]
then
	pandora_upgrade
fi

exit