#!/bin/sh
#
# conserver -- serial-port console daemon
#
# chkconfig: 2345 92 08
# description: conserver is a serial-port console daemon
# config: /etc/conserver.cf
#

DAEMON=/usr/sbin/conserver

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[  ${NETWORKING} = "no" ] && exit 0

# make sure conserver is installed and executable
[ -x $DAEMON  ] || exit 1

# the logfiles are root:uucp owned
umask 002

# See how we were called.
case "$1" in
  start)
    echo -n "Starting conserver: "

mkdir -p /var/state/conserver
CMDLIST=/var/state/conserver/commands.allow

	if [ -z "$CONSERVER_PRINCIPAL" ]; then
		CONSERVER_PRINCIPAL=$( hostname --fqdn )
		CONSERVER_PRINCIPAL="host/${CONSERVER_PRINCIPAL}"
	fi

	if [ -n "$CONSERVER_PRINCIPAL" -a -z "$KRB5_KTNAME" ]; then
		if [ -f /etc/krb5.keytab ]; then
			KRB5_KTNAME=/var/state/conserver/krb5.keytab
			cp /etc/krb5.keytab $KRB5_KTNAME
			chown uucp $KRB5_KTNAME
			chmod 0400 $KRB5_KTNAME
		else
			echo "CONSERVER_PRINCIPAL is set, KRB5_KTNAME is empty and /etc/krb5.keytab not found!"
			exit 0
		fi
	fi
    # suppress warning about the UUCP user not having a home directory
	# launch with 500 consoles on a single child -- this prevents the opening of too many ports
	# and we have memory in abundance
    daemon --user uucp "KRB5_KTNAME=$KRB5_KTNAME" /usr/sbin/conserver -m 500 -d -L /var/log/console/__server.log 
    echo
    touch /var/lock/subsys/conserver
    ;;
  stop)
    echo -n "Shutting down conserver: "
    killproc conserver
    echo
    rm -f /var/lock/subsys/conserver
    ;;
  status)
    status conserver
    ;;
  restart)
    echo -n "Restarting conserver: "
    status conserver >/dev/null && killproc conserver -HUP || $0 start
    echo
    ;;
  reinit)
    echo -n "Reopening dead consoles: "
    killproc conserver -USR1
    ;;
  reopen)
    # for logrotate
    killproc conserver -USR2 >/dev/null 2>&1
    ;;
  *)
    echo "Usage: conserver {start|stop|restart|status}"
    exit 1
esac

exit 0
