#!/bin/sh
# chkconfig: 235 99 10
# description: Start or stop the Webmin server

start=/etc/webmin/start
stop=/etc/webmin/stop
lockfile=/var/lock/subsys/webmin
confFile=/etc/webmin/miniserv.conf
userFile=/etc/webmin/miniserv.users
pidFile=/var/webmin/miniserv.pid
name='Webmin'
crypt=`grep "^root:" /etc/shadow | cut -f 2 -d :`
export crypt

case "$1" in
'start')
	if [ -f $userFile ]; then
		if [ ! -z "`grep 'root:XXUp2ozpdysrQ:0' $userFile`" ]; then
			# Oops! webmin password is null, 
			# so we replace with new root password.
			/usr/bin/perl -pi -e 's/root:XXUp2ozpdysrQ:0/root:$ENV{crypt}:0/g' $userFile
		fi
	fi
	if [ -f $confFile ]; then
		if [ -z "`grep 'allow=' $confFile`" ]; then
			echo "allow=127.0.0.1" >> $confFile
		fi
	fi
	$start
	RETVAL=$?
	if [ "$RETVAL" = "0" ]; then
		touch $lockfile >/dev/null 2>&1
	fi
	;;
'stop')
	$stop
	RETVAL=$?
	if [ "$RETVAL" = "0" ]; then
		rm -f $lockfile
	fi
	;;
'status')
	pidfile=`grep "^pidfile=" $confFile | sed -e 's/pidfile=//g'`
	if [ "$pidfile" = "" ]; then
		pidfile=$pidFile
	fi
	if [ -s $pidfile ]; then
		pid=`cat $pidfile`
		kill -0 $pid >/dev/null 2>&1
		if [ "$?" = "0" ]; then
			echo "$name (pid $pid) is running"
			RETVAL=0
		else
			echo "$name is stopped"
			RETVAL=1
		fi
	else
		echo "$name is stopped"
		RETVAL=1
	fi
	;;
'restart')
	$stop && $start
	RETVAL=$?
	;;
*)
	echo "Usage: $0 { start | stop | restart }"
	RETVAL=1
	;;
esac
exit $RETVAL

