#!/sbin/openrc-run

extra_commands="configdump configtest modules virtualhosts"
extra_started_commands="fullstatus graceful gracefulstop reload"

depend() {
	need net
	use mysql dns logger netmount postgresql
	after sshd firewall
}

configtest() {
	ebegin "Checking ${SVCNAME} configuration"
	checkconfig
	eend $?
}

checkconfd() {
	PIDFILE="${PIDFILE:-/run/apache2/httpd.pid}"
	TIMEOUT=${TIMEOUT:-10}

	CONFIGFILE="${CONFIGFILE:-/etc/apache2/httpd.conf}"
	[ "${CONFIGFILE#/}" = "${CONFIGFILE}" ] && CONFIGFILE="${SERVERROOT}/${CONFIGFILE}"
	if [ ! -r "${CONFIGFILE}" ]; then
		eerror "Unable to read configuration file: ${CONFIGFILE}"
		return 1
	fi
	HTTPD_OPTS="${HTTPD_OPTS} -f ${CONFIGFILE}"
	[ -n "${STARTUPERRORLOG}" ] && HTTPD_OPTS="${HTTPD_OPTS} -E ${STARTUPERRORLOG}"
	return 0

}

checkconfig() {
	checkconfd || return 1

	${HTTPD} ${HTTPD_OPTS} -t 1>/dev/null 2>&1
	ret=$?
	if [ $ret -ne 0 ]; then
		eerror "${SVCNAME} has detected an error in your setup:"
		${HTTPD} ${HTTPD_OPTS} -t
	fi

	return $ret
}

start() {
	checkconfig || return 1
	checkpath --directory $(dirname $PIDFILE)

	[ -f /var/log/apache2/ssl_scache ] && rm /var/log/apache2/ssl_scache

	ebegin "Starting ${SVCNAME}"
	start-stop-daemon --start \
		--pidfile ${PIDFILE} \
		--exec ${HTTPD} \
		-- ${HTTPD_OPTS} -k start
	eend $?
}

stop() {
	checkconfd || return 1

	ebegin "Stopping ${SVCNAME}"
	start-stop-daemon --stop --pidfile ${PIDFILE} --exec ${HTTPD} \
		--retry ${TIMEOUT}
	eend $?
}

reload() {
	RELOAD_TYPE="${RELOAD_TYPE:-graceful}"

	checkconfig || return 1
	service_started "${SVCNAME}" || return

	if [ "${RELOAD_TYPE}" = "restart" ]; then
		ebegin "Restarting ${SVCNAME}"
		${HTTPD} ${HTTPD_OPTS} -k restart
		eend $?
	elif [ "${RELOAD_TYPE}" = "graceful" ]; then
		ebegin "Gracefully restarting ${SVCNAME}"
		${HTTPD} ${HTTPD_OPTS} -k graceful
		eend $?
	else
		eerror "${RELOAD_TYPE} is not a valid RELOAD_TYPE. Please edit /etc/conf.d/${SVCNAME}"
	fi
}

graceful() {
	checkconfig || return 1
	service_started "${SVCNAME}" || return
	ebegin "Gracefully restarting ${SVCNAME}"
	${HTTPD} ${HTTPD_OPTS} -k graceful
	eend $?
}

gracefulstop() {
	checkconfig || return 1
	ebegin "Gracefully stopping ${SVCNAME}"
	start-stop-daemon --stop --pidfile ${PIDFILE} --exec ${HTTPD} \
		--retry SIGWINCH/${TIMEOUT}
	eend $?
}

modules() {
	checkconfig || return 1

	${HTTPD} ${HTTPD_OPTS} -M 2>&1
}

fullstatus() {
	LYNX="${LYNX:-lynx -dump}"
	STATUSURL="${STATUSURL:-http://localhost/server-status}"

	if ! service_started "${SVCNAME}"; then
		eerror "${SVCNAME} not started"
	elif ! command -v "$(set -- ${LYNX}; printf '%s' "$1")" 2>&1 >/dev/null; then
		eerror "lynx not installed!"
	else
		${LYNX} ${STATUSURL}
	fi
}

virtualhosts() {
	checkconfd || return 1
	${HTTPD} ${HTTPD_OPTS} -S
}

configdump() {
	LYNX="${LYNX:-lynx -dump}"
	INFOURL="${INFOURL:-http://localhost/server-info}"

	checkconfd || return 1

	if ! service_started "${SVCNAME}"; then
		eerror "${SVCNAME} not started"
	elif ! command -v "$(set -- ${LYNX}; printf '%s' "$1")" 2>&1 >/dev/null; then
		eerror "lynx not installed!"
	else
		echo "${HTTPD} started with '${HTTPD_OPTS}'"
		for i in config server list; do
			${LYNX} "${INFOURL}/?${i}" | sed '/Apache Server Information/d;/^[[:space:]]\+[_]\+$/Q'
		done
	fi
}