#!/bin/sh

# Pandora FMS Console Installer (c) 2008-2011 Artica ST
# Linux/FreeBSD Version (generic), for SuSe, Debian/Ubuntu, RHEL/CentOS,
# Fedora and FreeBSD only
# other Linux distros could not work properly without modifications
# Please see http://www.pandorafms.org
# v4.0dev Build 110203
# This code is licensed under GPL 2.0 license.
# **********************************************************************

PI_VERSION=4.0
FORCE=0
DESTDIR=""
LOG_TIMESTAMP=`date +"%Y/%m/%d %H:%M:%S"`
MODE=$1

#
# set_global_vars
#	Check platform and set DISTRO, OS_VERSION, WWWUSER, WWWGROUP, WWWROOT,
#	PANDORA_HOME and PANDORA_HOME_GROUP.
#
set_global_vars () {
	DISTRO="GENERIC"

	case `uname -s` in
	Linux)
		# Get Linux Distro type and version
		if [ -f "/etc/SuSE-release" ]
		then
			OS_VERSION=`cat /etc/SuSE-release | grep VERSION | cut -f 3 -d " "`
			DISTRO=SUSE
		elif [ -f "/etc/lsb-release" ] && [ ! -f "/etc/redhat-release" ]
		then
			OS_VERSION=`cat /etc/lsb-release | grep DISTRIB_RELEASE | cut -f 2 -d "="`
			DISTRO=UBUNTU
			OS_VERSION="UBUNTU $OS_VERSION"
		elif [ -f "/etc/debian_version" ]
		then
			OS_VERSION=`cat /etc/debian_version`
			OS_VERSION="DEBIAN $OS_VERSION"
			DISTRO=DEBIAN
		elif [ -f "/etc/fedora-release" ]
		then
			OS_VERSION=`cat /etc/fedora-release | cut -f 4 -d " "`
			OS_VERSION="FEDORA $OS_VERSION"
			DISTRO=FEDORA
		elif [ -f "/etc/redhat-release" ]
		then
			DISTRO=RHEL_CENTOS
		fi
		case $DISTRO in
		SUSE)
			WWWUSER=wwwrun
			WWWGROUP=www
			PANDORA_HOME_GROUP=root
			WWWROOT=/srv/www/htdocs
			;;
		UBUNTU|DEBIAN)
			WWWUSER=www-data
			WWWGROUP=www-data
			WWWROOT=/var/www
			;;
		FEDORA|RHEL_CENTOS)
			WWWUSER=apache
			WWWGROUP=apache
			WWWROOT=/var/www/html
			;;
		esac
		;;
	FreeBSD)
		DISTRO=FreeBSD
		WWWUSER=www
		WWWGROUP=www
		WWWROOT=/usr/local/www

		local apache
		for apache in apache24 apache22
		do
			[ ! -d $WWWROOT/$apache ] && continue
			WWWROOT=$WWWROOT/$apache/data
			break
		done
		;;
	NetBSD)
		DISTRO=NetBSD
		WWWUSER=www
		WWWGROUP=www
		WWWROOT=/usr/pkg/share/httpd/htdocs
		;;
	esac

	# backward compatible defaults (Assuming SUSE)
	if [ "$DISTRO" = GENERIC ]
	then
		WWWUSER=wwwrun
		WWWGROUP=www
		WWWROOT=/srv/www/htdocs
		PANDORA_HOME_GROUP=root
	fi
	# Use WWWGROUP as default for PANDORA_HOME_GROUP
	: ${PANDORA_HOME_GROUP:=$WWWGROUP}

	OS_VERSION=`uname -r`
	PANDORA_HOME="$WWWROOT/pandora_console"
}

uninstall () {
	set_global_vars

	echo "Removing Pandora FMS Console"
	rm -Rf $PANDORA_HOME
	echo "You need to drop manually pandora database from your Database server"
	echo "Done"
}

install () {
	set_global_vars

	OLDFILENAMETMP=`date +"%Y-%m-%d"`
	PANDORA_CONF=$PANDORA_HOME/include/config.php

	echo "Detecting operating system: $DISTRO"
	
	if [ -f $DESTDIR$PANDORA_HOME ] && [ "$FORCE" = "0" ]
	then
		echo "Seems that default dir already exists. Please use --force to"
		echo "force installer to install on $DESTDIR$PANDORA_HOME"
		exit
	else
		echo "Checking default dir $DESTDIR$PANDORA_HOME..."
	fi

	# Check and create 'pandora' user if needed
	id pandora 2> /dev/null
	if [ $? -eq 0 ]; then
		echo " "
		echo "User pandora does exist, skipping this step"
	elif [ "$DESTDIR" ]
	then
		# don't create user with "fakeroot" installation
		echo "User 'pandora' does not exist. All chown operations may fail."
		echo "You should manualy set proper ownership to $DESTDIR$PANDORA_HOME and $DESTDIR$PANDORA_SPOOL if it's required."
		echo
	else
		echo "Are you sure we can create a standard 'pandora' user locally? [y/N]"
		read AREYOUSURE
		if [ "$AREYOUSURE" != "y" ]
		then
			echo "Please create the 'pandora' user manually according to your authentication scheme, then start again the installation"
			echo "Aborting..."
			exit 1
		fi
		# creating user
		if [ "$DISTRO" = "FreeBSD" ]
		then
			echo "pandora:41121:::::Pandora FMS:/home/pandora:/usr/sbin/nologin:" | adduser -f - -w no 2> /dev/null
		else
			useradd pandora 
			mkdir /home/pandora 2> /dev/null
			mkdir /home/pandora/.ssh 2> /dev/null
			chown -R pandora /home/pandora 
		fi
	fi

	# Create directories
	echo "Creating Pandora FMS Console home directory at $DESTDIR$PANDORA_HOME ..."
	mkdir -p $DESTDIR$PANDORA_HOME 2> /dev/null

	# Copying Pandora FMS console
	echo "Copying Pandora FMS Console to $DESTDIR$PANDORA_HOME.."
	cp -R * $DESTDIR$PANDORA_HOME
	chmod -R u+rwX,g+rX,g-w,o-rwx $DESTDIR$PANDORA_HOME

	# prepare /var/spool/pandora/data_in and sub directories
	for subdir in collections conf md5 netflow
	do
		[ ! -d $DESTDIR/var/spool/pandora/data_in/$subdir ] && mkdir -p $DESTDIR/var/spool/pandora/data_in/$subdir
	done

	#Ownership
	chown -R $WWWUSER:$PANDORA_HOME_GROUP $DESTDIR$PANDORA_HOME 2> /dev/null
	chown -R pandora:$WWWGROUP $DESTDIR/var/spool/pandora/	2> /dev/null

	echo "Setting secure permissions for Pandora FMS spool dir..."
	chmod -R u+rwX,g+rwX,o-rwx $DESTDIR/var/spool/pandora/

	echo "Done."
	echo " "
	echo "You have your Pandora FMS console installed on $DESTDIR$PANDORA_HOME."
	echo " "
	echo "Now you can setup your Pandora FMS console and install"
	echo "database using a browser and point to: "
	echo " " 
	echo "	http://ip_address_of_this_server/pandora_console/install.php"
	echo " "
	echo " "
}

help () {
	echo "Syntax":
	echo
	echo "    ./pandora_console_install < --mode > [ --option ]"
	echo " "
	echo "Modes:"
	echo
	echo "    --force-install     To force installation if already installed on this system"
	echo "    --install           To install Pandora FMS Console on this system"
	echo "    --uninstall         To uninstall/remove Pandora FMS Console on this System"
	echo
	echo "Option:"
	echo
	echo "	  --destdir DIR	      Specify root directory for \"fakeroot\" installation"
	echo
}

# Script banner at start
echo " "
echo "Pandora FMS Console Installer $PI_VERSION (c) 2008-2014 ArticaST"
echo "This program is licensed under GPL2 Terms. http://pandorafms.com"
echo " "

# parse option
if [ "$2" = "--destdir" ]
then
	if [ -z "$3" ]
	then
		echo '"--datadir" option requires an argument'
		help
		exit 1
	fi
	DESTDIR="$3"
fi

case "$MODE" in

'--force-install')
	FORCE=1
	install
	exit
	;;

'--install')
	install
	exit
	;;

'--uninstall')
	uninstall
	exit
	;;

*)
	help
esac