#!/bin/sh
#
# Copyright (c) 1999-2006 Novell, Inc.
# All rights reserved.
#
# Startup for namcd
#

# chkconfig: 2345 76 54
# description: Cache daemon for Novell's Linux User Management.
# processname: namcd
# pidfile: 
# config: 

### BEGIN INIT INFO
# Provides: namcd 
# Required-Start: $local_fs $syslog $network
# Should-Start: ndsd
# Required-Stop: 
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Cache daemon for LUM
# Description: Cache daemon for LUM
### END INIT INFO

ulimit -c unlimited

# Template configuration variables

prefix=/usr
exec_prefix=/usr
sbindir=/usr/sbin
bindir=/usr/bin
initdir=/etc/init.d

userID=`id`
userID=`echo $userID | sed -e "s/(.*//" | sed -e "s/.*=//"`
if [ "$userID" -ne 0 ]; then
	echo "$0: must be run as root."
	exit 2
fi

. /etc/rc.status

NAMCD_BIN=/usr/sbin/namcd
test -x $NAMCD_BIN || { echo "$NAMCD_BIN not installed";
    if [ "$1" = "stop" ]; then exit 0; else exit 5; fi; }

NAMCD_CONFIG=/etc/nam.conf
test -r $NAMCD_CONFIG || { echo "$NAMCD_CONFIG does not exist";
    if [ "$1" = "stop" ]; then exit 0; else exit 6; fi; }

NAMCD_PID_FILE="/var/run/novell-lum/namcd.pid"

rc_reset


rc=0

case "$1" in
'start')
        rm -rf /var/lib/novell-lum/.namcdloaded > /dev/null 2>&1

	printf "Starting NAM Cache Daemon ..."
        PID=`ps alxw | awk ' /namcd/ { if ( $4 == 1 ) print $3 }'`
	if [ ! -z "$PID" ] ; then
	    echo "        NAMCD is running already"
            exit 0
	else
	    if [ -f /var/lib/novell-lum/.flush_check_file ] ; then
                rm -f /var/lib/novell-lum/.group_info.*
                rm -f /var/lib/novell-lum/.user_info.*
                rm -f /var/lib/novell-lum/.refresh_info
                rm -f /var/lib/novell-lum/.flush_check_file
                rm -f $NAMCD_PID_FILE
            fi
            PIDNDSD=`ps alxw | awk ' /ndsd/ { if ( $4 == 1 ) print $3 }'`
	    if [ ! -z "$PIDNDSD" ] ; then
                y=0
	        printf "\nWaiting for LDAP server to be ready ...\n"
                while [ $y -lt 60 ]
                do
                    $bindir/namldapprobe 
                    if [ "$?" == 0 ]
                    then
                        break
                    fi    
                    #
                    # CA certificate is missing, no point to wait
                    #
                    if [ "$?" == 2 ]
                    then
                        exit 2;
                    fi    
                    printf "."
		    sleep 1
		    y=$((y+1))
                done    
	        /sbin/startproc -p $NAMCD_PID_FILE $sbindir/namcd 
            else
	        /sbin/startproc -p $NAMCD_PID_FILE $sbindir/namcd 
            fi        
            rc=$?
        fi    
        z=0
	printf "\nWaiting for namcd initialization to complete ...\n"
        while [ $z -lt 120 ]
        do
            if [ -f /var/lib/novell-lum/.namcdloaded ]
            then
                break
            fi    
            printf "."
            sleep 1
	    z=$((z+1))
        done    
        printf "\n"
        rc_status -v
	;;
'stop')
	printf "Stopping NAM Cache Daemon ..."
        rm -rf /var/lib/novell-lum/.namcdloaded > /dev/null 2>&1
        touch /var/lib/novell-lum/.flush_check_file
	PID=`ps alxw | awk ' /namcd/ { if ( $4 == 1 ) print $3 }'`
	if [ ! -z "$PID" ] ; then
		# /sbin/killproc  -p $NAMCD_PID_FILE -TERM $sbindir/namcd
		# /sbin/killproc -TERM $sbindir/namcd
                # rc_status -v
                /bin/kill -TERM ${PID} >/dev/null 2>&1
	else
	    echo " Daemon is not running"
	    rm -f /var/lib/novell-lum/.flush_check_file
	    exit 0;
	fi 

	if kill -0 $PID > /dev/null 2>&1 
        then 
            # Wait for proper shutdown
	    x=0
            PID=0
	    printf "\nPlease Wait. Flashing cache to the file \n"
	    if [ -f /var/lib/novell-lum/.flush_check_file ]
            then
	    	while [ -f /var/lib/novell-lum/.flush_check_file ] 
		do
			printf "."
			sleep 1
			x=$((x+1))

                        # Check on process again. This is needed for upgrade case.

                       	PID=`ps alxw | awk ' /namcd/ { if ( $4 == 1 ) print $3 }'`
	                if [ -z "$PID" ] 
                        then
	                    rm -f /var/lib/novell-lum/.flush_check_file
                            rm -f $NAMCD_PID_FILE
                            # printf "\nDone.\n"
                            rc_status -v
	                    exit 0;
	                fi 

                        # If namcd has big cache to flash to the file
			# and server is under heavy load
                        # it may take up to 4 minutes to flash 6000 users
                        # we will wait then kill the main thread
			if [ $x -gt 240 ] 
                        then
			    /bin/kill -KILL ${PID} >/dev/null 2>&1
                            rm -f /var/lib/novell-lum/.flush_check_file
                            rm -f $NAMCD_PID_FILE
			    exit 1;
                        fi
       	        done	
	    fi
        fi 
        rc_status -v
        rm -f $NAMCD_PID_FILE
	;;
'status')
	echo -n "Checking for LUM NAMCD daemon "
        /sbin/checkproc -p $NAMCD_PID_FILE  $sbindir/namcd
        rc_status -v
	;;
'restart')
	$0 stop
        sleep 1
	$0 start
        rc_status
	;;
*)
	echo "Usage: $initdir/namcd { start | stop | restart | status}"
	;;
esac
rc_exit $rc

