#!/bin/sh
#
# dchub      HUB Direct Connect
#
# $Id: dchub.init.in,v 1.8 2003/10/19 15:32:52 blusseau Exp $
#

# For Redhat-ish systems
#
# chkconfig: 2345 80 30
# description: dchub is a Direct Connect HUB Clone.
#
# processname: dchub
# pidfile: /var/run/dchub.pid
# logfile: /var/log/dchub.log

# This is used in the Debian distribution to determine the proper
# location for the S- and K-links to this init file.
# The following value is extracted by debstd to figure out how to
# generate the postinst script. Edit the field to change the way the
# script is registered through update-rc.d (see the manpage for
# update-rc.d!)
#
FLAGS="defaults 21"

prog="dchub"
initdir=/etc/rc.d/init.d
DCHUB=/usr/bin/dchub
DCHUB_LIBEXECDIR=/usr/libexec/dchub
DCHUB_UID=dchub
DCHUB_LOGFILE=/var/log/dchub.log
DCHUB_DBDIR=/etc/dchub
DCHUB_DBCONF=$DCHUB_DBDIR/dchub.conf
DCHUB_DBUSERFILE=$DCHUB_DBDIR/dchub.users
DCHUB_XMLCONFFILE=$DCHUB_DBDIR/conf.xml
DCHUB_XMLUSERFILE=$DCHUB_DBDIR/users.xml
DCHUB_HUBPASSFILE=$DCHUB_DBDIR/dchub.hubpasswd
DCHUB_CRCFILE=/var/lib/dchub/dchub.crc
DCHUB_SCRIPTDIR=$DCHUB_LIBEXECDIR/scripts
DCHUB_PLUGINSDIR=$DCHUB_LIBEXECDIR/plugins
DCHUB_MAINSCRIPT=main.pl
DCHUB_EXTPROGDIR=$DCHUB_LIBEXECDIR/extprog
DCHUB_TOOLSDIR=$DCHUB_LIBEXECDIR/tools

OPTIONS="--UID=$DCHUB_UID --conf=$DCHUB_XMLCONFFILE --user=$DCHUB_XMLUSERFILE --hubpasswd=$DCHUB_HUBPASSFILE --crc=$DCHUB_CRCFILE --pscriptdir=$DCHUB_SCRIPTDIR --linkdir=$DCHUB_PLUGINSDIR --pscriptinit=$DCHUB_MAINSCRIPT --exprogdir=$DCHUB_EXTPROGDIR --logfile=$DCHUB_LOGFILE"

# Source function library.
. $initdir/functions

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

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

# Pull in sysconfig settings
[ -f /etc/sysconfig/dchub ] && . /etc/sysconfig/dchub

[ -x "$DCHUB" ] || exit 0
[ -d "$DCHUB_DBDIR" ] || exit 0

RETVAL=0

do_init_db() {
	if [ ! -f $DCHUB_XMLCONFFILE -a ! -f $DCHUB_XMLUSERFILE -a ! -f $DCHUB_HUBPASSFILE -a ! -f $DCHUB_CRCFILE ]; then
		echo -n $"Initializing the database: " 
		if $DCHUB --init $OPTIONS >/dev/null; then
			success $"DcHub database creation"
			echo
			echo $"You can now log into the hub with the account MASTER"
			echo $"password MASTER to configure the hub."
		else
			failure $"DcHub database creation"
			echo
			exit 1
		fi
	fi
}

do_migration() {
	if [ -f $DCHUB_DBUSERFILE ]; then
		echo -n $"Migration of users:"
		migrate_dchub_users="$DCHUB_TOOLSDIR/migrate_dchub_users"
		if [ ! -x "$migrate_dchub_users" ]; then
			failure $"Can't find migrate_dchub_users program."
            echo
			exit 1
	    fi
		$migrate_dchub_users $DCHUB_DBUSERFILE $DCHUB_XMLUSERFILE >/dev/null
		RETVAL=$?
		if [ $RETVAL -eq 0 ];then
			mv $DCHUB_DBUSERFILE ${DCHUB_DBUSERFILE}.bak
			chown $DCHUB_UID $DCHUB_XMLUSERFILE
			success "DcHub users migration"
			echo
		else
			failure
			echo
			exit 1
		fi
	fi
	if [ -f $DCHUB_DBCONF ]; then
		echo -n $"Migration of configuration:"
		migrate_dchub_configuration="$DCHUB_TOOLSDIR/migrate_dchub_configuration"
		if [ ! -x "$migrate_dchub_configuration" ]; then
			failure $"Can't find migrate_dchub_configuration program."
            echo
			exit 1
	    fi
		$migrate_dchub_configuration $DCHUB_DBCONF $DCHUB_XMLCONFFILE >/dev/null
		RETVAL=$?
		if [ $RETVAL -eq 0 ];then
			mv $DCHUB_DBCONF ${DCHUB_DBCONF}.bak
			chown $DCHUB_UID $DCHUB_XMLCONFFILE
			success "DcHub configuration migration"
			echo
		else
			failure
			echo
			exit 1
		fi
	fi
	
}

start() {
	# Create database if necessary
	do_init_db

	# Migrate users if necessary
	do_migration

	# Start daemon.
	if [ -n "`/sbin/pidof $prog`" ]; then
		echo $"$prog: already running"
		return 1
	fi
	echo -n $"Starting $prog: "
	OPTIONS="$OPTIONS --daemonize --missing --pidfile=/var/run/dchub.pid"
	ulimit -n 2048
	daemon $DCHUB $OPTIONS
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/dchub
    echo
	return $RETVAL
}

stop() {
	# Stop daemons.
	echo -n $"Stopping $prog: "
	killproc dchub
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/dchub
	echo
	return $RETVAL
}

restart() {
	stop
	start
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  status)
  	status dchub
	;;
  condrestart)
	[ -f /var/lock/subsys/dchub ] && restart || :
	;;
  *)
	echo "Usage: dchub {start|stop|restart|status|condrestart}"
	exit 1
esac

exit $?
