#! /bin/sh
#  Copyright 2007, One Laptop per Child
#  Author: John Watlington
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

#  idmgr        This is the script that starts up the
#               Identity service on the XS School server
#
# chkconfig: 35 96 8
# description: provides the OLPC laptop identity service

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

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PID_FILE="/var/run/idmgr.pid"
OPTS="$PID_FILE "
prog=idmgr
SERVER=/usr/bin/registration-server
RETVAL=0
SYS_DOMAIN_FILE=/etc/sysconfig/xs_domain_name

check_domain_configured() {
    if [ ! -e $SYS_DOMAIN_FILE ]; then
	echo "Domain not configured yet" > /dev/stderr
	exit 1;
    fi

    domain=`cat "$SYS_DOMAIN_FILE" `
    if [ "$domain" == "random.xs.laptop.org" ]; then
	echo "Domain not configured yet" > /dev/stderr
	exit 1;
    fi
}  


start() {
	# Start daemons.
	echo -n "Starting $prog: "
	check_domain_configured
        daemon --pidfile=${PID_FILE} $SERVER $OPTS
	RETVAL=$?
	return $RETVAL
}

stop() {
	# Stop daemons.
	echo -n "Shutting down $prog: "
	killproc -p ${PID_FILE} -d 10 $prog
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f ${PID_FILE}
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  condrestart)
	if [ -f $PID_FILE ] ; then
		stop
		start
	        RETVAL=$?
	fi
	;;
  status)
        status idmgr $PID_FILE
	RETVAL=$?
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|condrestart|status}"
	exit 1
esac

exit $RETVAL
