#!/bin/bash
#
# $Id$
#
THEME_DIR="/usr/share/nagios-themes"
NAGIOS_CGI_CFG="/etc/nagios/cgi.cfg"
APACHE_CONFIG="/etc/apache2/conf.d/nagios.conf"
DEFAULT_THEME="nagios"
BACKUP_DIR="$THEME_DIR/backups"
APACHECTL="/etc/init.d/apache2"
DO_ECHO=""

function usage() {
	echo
	echo "Usage: $(basename $0) [OPTIONS] <theme>"
	echo "       <theme> : this is the name of the theme "
	echo "                 (same as the directory name in $THEME_DIR)"
	echo "                 (default: $DEFAULT_THEME)"
	echo
	echo " OPTIONS:"
	echo "       -h : help (this message)"
	echo
	echo "       -l : list available themes"
	echo "       -d : debug (just create backups - do nothing else)"
	echo "       -b : directory for backup files (default: $BACKUP_DIR)"
	echo "       -t : use theme directory (default: $THEME_DIR)"
	echo "       -c : use Nagios cgi.cfg  (default: $NAGIOS_CGI_CFG)"
	echo "       -a : use Apache config   (default: $APACHE_CONFIG)"
	echo
	echo " Currently available themes:"
	list_themes
	exit $1
}

function get_current_theme(){
	set -- `grep ^physical_html_path "$NAGIOS_CGI_CFG" | tail -n1 | sed 's@=@ @' | tr -d '[:cntrl:]'`
	shift # remove first ARG
	echo $*
}

function make_backup(){
	if [ ! -f "$1" ]; then
		echo "ERROR: make_backup() needs at least a valid file" >&2
		exit 1
	else
		ORGFILE="$(basename $1)"
		BACKUP=$(date +"%Y%m%d%H%M%S")
		if [ -f "$BACKUP_DIR/$ORGFILE-$BACKUP" ]; then
			echo "ERROR: $ORGFILE-$BACKUP already exists in $BACKUP_DIR" >&2
			exit 1
		else
			cp "$1" "$BACKUP_DIR/$ORGFILE-$BACKUP"
		fi
	fi
}

function list_themes(){
	CURRENT=$(basename $(get_current_theme))
	for dir in $(ls -d "$THEME_DIR"/*) $DEFAULT_THEME ; do
		dir=$(basename $dir)
		case $dir in 
			backups)
				continue;
			;;
			"$CURRENT")
				LIST="=> $dir\n$LIST"
			;;
			*)
				LIST="$dir\n$LIST"
			;;
		esac
	done
	echo -e "$LIST"
}

if [ ! "$1" ]; then
	echo "ERROR: Please define at least a theme name" >&2
    usage 1
fi

while getopts 'lha:b:c:dt:' OPTION; do
	case $OPTION in
		h) usage 0
		;;
		a) APACHE_CONFIG="$OPTARG"
		;;
		l) list_themes;
           exit 0;
		;;
		b) BACKUP_DIR="$OPTARG"
		;;
		c) NAGIOS_CGI_CFG="$OPTARG"
		;;
		d) DO_ECHO="echo "
		;;
		t) THEME_DIR="$OPTARG"
		;;
	esac
done
shift $(( OPTIND - 1 ))

THEME="$1"

if [ x"$UID" != x"0" ]; then
	echo "ERROR: Only 'root' can run this script" >&2
	ERROR=1
fi

if [ x"$THEME" == x"$DEFAULT_THEME" ]; then
	NEW_THEME="/usr/share/nagios"
else
	NEW_THEME="$THEME_DIR/$THEME"
fi

if [ ! -d "$NEW_THEME" ]; then
	echo "ERROR: $NEW_THEME is not a directory" >&2
	ERROR=1
fi

if [ ! -r "$NAGIOS_CGI_CFG" ]; then
	echo "ERROR: Could not read $NAGIOS_CGI_CFG" >&2
	ERROR=1
fi

if [ ! -r "$APACHE_CONFIG" ]; then
	echo "ERROR: Could not read $APACHE_CONFIG" >&2
	ERROR=1
fi

if [ ! -d "$BACKUP_DIR" ]; then
	install -d -m700 "$BACKUP_DIR" || ERROR=1
fi

if [ ! -x "$APACHECTL" ]; then
	echo "ERROR: Could not execute $APACHECTL" >&2
	ERROR=1
fi

if [ -n "$ERROR" ]; then
	echo "ERROR: There is at least one error - exiting" >&2
	usage 1
fi

# Switch the physical html path in cgi.cfg
# physical_html_path=/home/nagios
make_backup "$NAGIOS_CGI_CFG"
CURRENT_THEME="$(get_current_theme)"
$DO_ECHO sed -i "s|^physical_html_path.*|physical_html_path=$NEW_THEME|g" "$NAGIOS_CGI_CFG"

# (Re-)configure apache, so access to the new directory is permitted
make_backup "$APACHE_CONFIG"
$DO_ECHO sed -i "s|^Alias.*/nagios.*$CURRENT_THEME.*|Alias /nagios \"$NEW_THEME\"|; \
		s|<Directory.*$CURRENT_THEME.*|<Directory \"$NEW_THEME\">|" "$APACHE_CONFIG"

$DO_ECHO $APACHECTL restart
