#!/bin/sh

NCLIENTS=100
KILLMODE=service

PIDDIR=/var/samba/run
BINDIR=/liu/bin

if [ "$1" != "" ]; then
    KILLMODE="$1"
fi

while true; do
    case $KILLMODE in
	restart)
	    service samba_server restart
	    ;;

	stop-start)
	    service samba_server stop
	    service samba_server start
	    ;;
	
	stop-wait-start)
	    service samba_server stop
	    if pgrep -q smbd; then
		echo "Waiting for all smbd to die:"
		while pgrep -q smbd; do
		    echo -n "."
		done
		echo ""
	    fi
	    if pgrep -q winbindd; then
		echo "Waiting for all winbindd to die:"
		while pgrep -q winbindd; do
		    echo -n "."
		done
		echo ""
	    fi
	    service samba_server start
	    ;;
	
	pkill-smbd-winbindd)
	    echo "Terminating smbd:"
	    pkill smbd
	    while pgrep -q smbd; do
		echo -n "."
	    done
	    echo ""
	    echo "Terminating winbind:"
	    pkill winbind
	    while pgrep -q winbindd; do
		echo -n "."
	    done
	    echo ""
	    echo "Restarting"
	    service samba_server start
	    ;;

	pkill-winbindd-smbd)
	    echo "Terminating winbind:"
	    pkill winbind
	    while pgrep -q winbindd; do
		echo -n "."
	    done
	    echo ""
	    echo "Terminating smbd:"
	    pkill smbd
	    while pgrep -q smbd; do
		echo -n "."
	    done
	    echo ""
	    echo "Restarting"
	    service samba_server start
	    ;;

	pidfiles-smbd-winbindd)
	    echo "Terminating smbd:"
	    test -f $PIDDIR/smbd.pid && kill `cat $PIDDIR/smbd.pid`
	    while pgrep -q smbd; do
		echo -n "."
	    done
	    echo ""
	    echo "Terminating winbind:"
	    test -f $PIDDIR/winbindd.pid && kill `cat $PIDDIR/winbindd.pid`
	    while pgrep -q winbindd; do
		echo -n "."
	    done
	    echo ""
	    echo "Restarting"
	    service samba_server start
	    ;;

	pidfiles-winbindd-smbd)
	    echo "Terminating winbind:"
	    test -f $PIDDIR/winbindd.pid && kill `cat $PIDDIR/winbindd.pid`
	    while pgrep -q winbindd; do
		echo -n "."
	    done
	    echo ""
	    echo "Terminating smbd:"
	    test -f $PIDDIR/smbd.pid && kill `cat $PIDDIR/smbd.pid`
	    while pgrep -q smbd; do
		echo -n "."
	    done
	    echo ""
	    echo "Restarting"
	    service samba_server start
	    ;;

	*)
	    echo "$0: Error: $1: Invalid killmode" >&2
	    exit 1
	    ;;
    esac

    sleep 1
    echo "Waiting for clients to terminate"
    wait
    
    echo "Starting $NCLIENTS clients..."
    for N in `seq 1 $NCLIENTS`; do
	/liu/bin/smbclient -k //filur00/peter86 -c "notify foo" >/dev/null &
    done
  sleep 10
done
