#!/bin/bash
#
# Startup script for the Apache Web Server
#
# chkconfig: 234 99 16
# description: Virtuozzo power panel 
# processname: pvapp

# Source function library.
if [ $(uname -s) != Darwin ];
then
       if [[ -e /etc/rc.d/init.d/functions ]];
       then
               . /etc/rc.d/init.d/functions
       else
               #Support of SLES10 and other by Linux Standart Base
               . /lib/lsb/init-functions
       fi
fi

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

. /etc/opt/pva/pp/globals.conf

RETVAL=0

# join some XML files to output
join_xml_files() {
	if [ $# -gt 1 ]; then
		(
				echo '<files>'
				for i in "$@"; do echo "<file href=\"$i\"/>"; done
				echo '</files>'
        	) | /opt/pva/pp/libexec/pvappcon -nx /var/opt/pva/pp/xsl/utils/join-trees.xsl -
	else
		cat "$1"
	fi
}

# list plugins
list_plugins() {
  echo "core "`cd $plugins_dir && ls | while read d; do if [ -d "$d" -a "$d" != "core" ]; then echo "$d"; fi; done`
}

# perform action on plugin
# action is performed by script with the same name in plugin /etc directory
do_plugins_action() {
	action=$1
	plugins=$2
	
	if [ -z "$plugins" ]; then
		plugins=`list_plugins`
		shift 1
	else
		shift 2
	fi
	
	for p in $plugins; do
		plugin_action $p $action $@  || { ret=$? ; echo "[ERROR] Failed to $action plugin $p" 1>&2; exit $ret; }
	done
}

plugin_action(){
	plugin=$1
	action=$2
	ret=0

	if [ -e $plugins_dir/$plugin/$action.sh ]; then
		(
			if [ -e $plugins_dir/$plugin/defines.conf ]; then
				. $plugins_dir/$plugin/defines.conf
			fi
			shift 2
			. $plugins_dir/$plugin/$action.sh $@
		)
		ret=$?
	fi

    return $ret
}

# generate plugin-based common files
generate_plugins() {
	set -e

	configs=`cd $plugins_dir/core && find * -name '*.xml'`
	plugins=`list_plugins`

	for cf in $configs; do 
		common_file="$config_dir/$cf"

		files=""
		for p in $plugins; do
			plugin_file="$plugins_dir/$p/$cf"
			if [ -f "$plugin_file" ]; then
				files="$files $plugin_file"
			fi
		done

		tmp_file="$common_file.tmp"
		echo "Updating configuration: $common_file"
		join_xml_files $files > $tmp_file
		mv $tmp_file $common_file
	done
}

# rebuild all configuraiton files
reconfigure() {
	generate_plugins 
	do_plugins_action configure 
	echo "Reset configuration flag"
	touch "${configure_flag}"
}

list_port_forwards() {
	xsl=${xsldir}/utils/dump-forwards.xsl
	if [ ! -f ${xsl} ]; then
		return -1
	else
		/opt/pva/pp/libexec/pvappcon -nx ${xsl} ${config_dir}/pvacc.conf
		return $?
	fi
}

get_pva_pid() {
	if [ -f /var/run/pvappd.pid ]; then
		pid=`cat /var/run/pvappd.pid`
		if [ -z "$pid" ]; then
			return 3
		else
			pids=`ps x -o "pid ppid" | grep $pid | awk '{print $1}' | xargs echo`
			if [ -z "$pids" ]; then
				return 3
			else
				echo "$pids"
			fi
		fi
	else 
		return 3
	fi
}

usage() {
	echo $"Usage: $prog {start|stop|restart|reload|status|help|listports|reconfigure}" 1>&2
	return 1
}

# See how we were called.
case "$1" in
	status)
		if [ $(uname -s) == Darwin  ] || [ ! -e /etc/rc.d/init.d/functions ]; then	
			pid=`get_pva_pid`
			RETVAL=$?
			if [ -z "$pid" ]; then
					echo "pvapp is not running"
			else
					echo "pvapp (pid $pid) is running..."
			fi
		else
			status /opt/pva/cc/libexec/pvappd
			RETVAL=$?
		fi
	;;
	listports)
		list_port_forwards
		RETVAL=$?
	;;
	help)
		usage
		RETVAL=$?
	;;
	stop)
		do_plugins_action stop  
	;;
	reconfigure)
		reconfigure
	;;
	start)
		do_plugins_action start
	;;
	restart)
		do_plugins_action stop
		do_plugins_action start
	;;
	reload)
		do_plugins_action reload
	;;
	*)
		usage
		RETVAL=$?
	;;
esac

exit $RETVAL
