#!/bin/bash
INSTALL_DIR="/usr/bin"
CONF_DIR="/etc/pandora"
AGENT_CONF_DIR="/etc/pandora/conf"

# Get distro and version
function get_distro {
	if [ -f "/etc/SuSE-release" ]
	then
		DISTRO="SUSE"
	elif [ -f "/etc/lsb-release" ] && [ ! -f "/etc/redhat-release" ]
	then
		DISTRO="UBUNTU"
	elif [ -f "/etc/debian_version" ]
	then
		DISTRO="DEBIAN"
	elif [ -f "/etc/fedora-release" ]
	then
		DISTRO="FEDORA"
	elif [ `uname -s` = "Darwin" ]
	then
		DISTRO="Darwin"
	elif [ `uname -s` = "AIX" ]
	then
		DISTRO="AIX"
	elif [ `uname -s` = "SunOS" ]
	then
		DISTRO="Solaris"
	elif [ `uname -s` = "Linux" ]
	then
		DISTRO="GENERIC"
	elif [ `uname -s` = "FreeBSD" ]
	then
		DISTRO="FreeBSD"
	elif [ `uname -s` = "NetBSD" ]
	then
		DISTRO="NetBSD"
	else
		DISTRO=`uname -s`
	fi
}

# Check that the given binary is in the PATH. Print a warning message if it is not.
function check_binary {
	BINARY="$1"
	
	which $BINARY >/dev/null 2>&1
	if [ $? != 0 ]
	then
		echo "ERROR: $BINARY not found. Either install it or update satellite_server.conf if it is installed."
		exit 1
	fi
}

# Guess the distro we are running on.
get_distro
echo "Pandora FMS Satellite Server installer for $DISTRO. (c) 2014-2023 Pandora FMS."
echo

# Install.
if [ "$1" == "--install" ]
then
	
	check_binary "nmap"
	check_binary "fping"
	
	echo
	echo ">Installing the Pandora FMS Satellite Server binary to $INSTALL_DIR..."
	cp -f satellite_server "$INSTALL_DIR" || exit 2
	
	echo ">Installing the tentacle_client binary to $INSTALL_DIR..."
	cp -f bin/tentacle_client "$INSTALL_DIR" || exit 2
	
	echo ">Installing the braa binary to $INSTALL_DIR..."
	cp -f bin/braa "$INSTALL_DIR" || exit 2

	echo ">Installing the pandorafsnmp binary to $INSTALL_DIR..."
	cp -f bin/pandorafsnmp "$INSTALL_DIR" || exit 2
	
	echo ">Installing the wmic binary to $INSTALL_DIR..."
	cp -f bin/wmic "$INSTALL_DIR" || exit 2
	
	echo ">Installing the pandorawmic binary to $INSTALL_DIR..."
	cp -f bin/pandorawmic "$INSTALL_DIR" || exit 2

	echo ">Copying configuration file to $CONF_DIR..."
	mkdir -p "$CONF_DIR" 2>/dev/null || exit 3
	CONF_FILE=$CONF_DIR"/satellite_server.conf"
	if [ -f $CONF_FILE ]
	then
		cp -f satellite_server.conf "$CONF_DIR/"satellite_server.conf.new || exit 3
	else
		cp -f satellite_server.conf "$CONF_DIR/"satellite_server.conf || exit 3
	fi

	echo ">Copying satellite plugins"
	mkdir -p "$CONF_DIR/satellite_plugins" 2>/dev/null || exit 3
	cp -f plugins/* "$CONF_DIR/satellite_plugins/" 2>/dev/null # Allow this command to fail if there are no plug-ins for the current architecture.
	
	echo ">Creating agent configuration directory $AGENT_CONF_DIR..."
	mkdir -p "$AGENT_CONF_DIR" 2>/dev/null || exit 4
	
	echo ">Copying startup script to /etc/init.d..."
	cp -f satellite_serverd /etc/init.d/ || exit 5
	if [ "$DISTRO" = "UBUNTU" ] || [ "$DISTRO" = "DEBIAN" ]
	then
		update-rc.d satellite_serverd defaults
	elif [ "$DISTRO" = "SUSE" ]
	then
		insserv satellite_serverd
	else
		INITLV=`cat /etc/inittab | grep "[0-9]\:initdefault" | cut -f 2 -d ":"`
		if [ -z "$INITLV" ]; then
			INITLV=2
		fi
		echo ">Linking startup script to /etc/rc.d/rc$INITLV.d"
		ln -s /etc/init.d/satellite_serverd /etc/rc.d/rc$INITLV.d/S90satellite_server
	fi
	
	if [ -d /etc/logrotate.d ]
	then
		[ -d $DESTDIR/etc/logrotate.d ] && mkdir -p $DESTDIR/etc/logrotate.d
		echo "Creating logrotate.d entry for Pandora FMS log management"
		cp pandora_satellite_logrotate $DESTDIR/etc/logrotate.d/pandora_satellite
	else
		echo "Please add a log rotation schedule manually to your log rotation daemon (if any)"
	fi
	
	echo
	echo "Edit the file $CONF_DIR/satellite_server.conf and manually configure the Satellite Server."
	echo

# Uninstall
elif [ "$1" == "--uninstall" ]
then
	echo
	echo ">Uninstalling the Pandora FMS Satellite Server binary from $INSTALL_DIR..."
	rm -f "$INSTALL_DIR/satellite_server"
	
	echo ">Removing configuration file from $CONF_DIR..."
	rm -f "$CONF_DIR/satellite_server.conf"
	
	echo ">Removing agent configuration directory $AGENT_CONF_DIR..."
	rm -rf "$AGENT_CONF_DIR"
	
	echo ">Removing startup script from /etc/init.d..."
	rm -f /etc/init.d/satellite_serverd
	rm -f  $DESTDIR/etc/logrotate.d/pandora_satellite
	if [ "$DISTRO" = "UBUNTU" ] || [ "$DISTRO" = "DEBIAN" ]
	then
		update-rc.d -f satellite_serverd remove
	elif [ "$DISTRO" = "SUSE" ]
	then
		insserv -f satellite_serverd
	else
		INITLV=`cat /etc/inittab | grep "[0-9]\:initdefault" | cut -f 2 -d ":"`
		if [ -z "$INITLV" ]; then
			INITLV=2
		fi
		echo ">Unlinking startup script /etc/rc.d/rc$INITLV.d/S90satellite_server"
		rm -f /etc/rc.d/rc$INITLV.d/S90satellite_server
	fi
else
	echo "Usage: $0 [--install|--uninstall]"
fi
