#!/bin/sh
#
# configd          Start/Stop alterator specific httpd (firsttime mode)
#
# chkconfig: - 95 05
#
# processname: ahttpd
# config: /etc/httpd-alterator
# description: alterator specific httpd
# pidfile: /var/run/httpd-alterator/httpd.pid

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

. cert-sh-functions
. avahi-sh-functions
. shell-config

CONFIGFILE=/etc/ahttpd/ahttpd-firsttime.conf
PIDFILE=/var/run/ahttpd-firsttime.pid
LOCKFILE=/var/lock/subsys/ahttpd-firsttime
CERTCONF=/etc/ahttpd/ahttpd.cnf
RETVAL=0

ahttpd_port()
{
    shell_config_get "$CONFIGFILE" server-port '[[:space:]]\+'
}

start()
{
	ssl_generate "ahttpd"

	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user _ahttpd --name ahttpd --displayname ahttpd -- ahttpd -c "$CONFIGFILE"
	RETVAL=$?

	[ "$RETVAL" != "0" ] || publish_service ahttpd 'Initial system setup at %h' '_https._tcp' "$(ahttpd_port)"

	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user _ahttpd --name ahttpd --displayname ahttpd -- ahttpd
	RETVAL=$?

	[ "$RETVAL" != "0" ] || unpublish_service ahttpd

	return $RETVAL
}

restart()
{
	stop
	start
}


# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart|reload)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart|condreload)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status --lockfile "$LOCKFILE" --pidfile "$PIDFILE" --expect-user _ahttpd --name ahttpd --displayname ahttpd -- ahttpd
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
