#!/bin/sh
# publish-acc-to-console	publish alterator web interface on console and alt-notes
#
# chkconfig: 35 99 99
# description: publish alterator web interface on console and alt-notes

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions
unset LANG LANGUAGE LC_ALL
SourceIfNotEmpty /etc/sysconfig/i18n

. alterator-net-functions

RETVAL=0
label='Web administration console URLs:'
label_tr=$(LANG="$LANG" gettext publish-acc-to-console "$label")

ahttpd_port()
{
	netlist -a |
		awk '$3=="ahttpd" && $8=="LISTEN" {split($6,a,/:/); print a[2];}'
}

ahttpd_addresses()
{
    local port="$1";shift

    [ "$port" = "443" ] && port="" || port=":$port"

    list_eth |
	xargs -rn1 ip addr show |
	sed -n 's,.*[[:space:]]inet[[:space:]]\+\([^/]\+\)/.*,https://\1'"$port"',p'
}

start()
{
	local port="$(ahttpd_port)"
	local addresses=

	[ -z "$port" ] ||
	    addresses="$(ahttpd_addresses "$port")"

	write_issue "$addresses"
	write_splash "$addresses"
	update_splash
	action 'Publishing web administration console addresses' true
	echo verbose >/proc/splash
	return 0
}

write_issue()
{
	local addresses="$1";shift

    cat > /etc/issue <<_EOF_
$label
$addresses
Welcome to \R / \l
_EOF_
}

write_splash()
{
	local addresses="$1";shift
	imgdir=/etc/bootsplash/themes/current/images
	[ -f $imgdir/orig-bootsplash-800x600.jpg ] ||
		mv $imgdir/bootsplash-800x600.jpg $imgdir/orig-bootsplash-800x600.jpg
	 convert -font '/usr/share/fonts/ttf/liberation/LiberationSans-Regular.ttf' \
	 -fill blue \
	 -pointsize 32 \
	 -gravity center \
	 -draw "text 0,-100 \"$label_tr\"" \
	 -draw "text 0,0 \"$addresses\"" \
	 $imgdir/orig-bootsplash-800x600.jpg \
	 $imgdir/bootsplash-800x600.jpg
}

update_splash()
{
	splash -s -u 0 /etc/bootsplash/themes/current/config/bootsplash-800x600.cfg
}

stop()
{
	true	
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload|restart)
		restart
		;;
	condreload|condrestart|condstop)
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condreload|condrestart|condstop}"
		RETVAL=1
esac

exit $RETVAL 
