#! /bin/bash
# We use some bashisms (arrays), so /bin/sh won't work.
# I hope this is policy-compliant; I couldn't find anything different,
# but if so, I'm gonna need help here to find a better way of handling
# this.
#
# skeleton	example file to build /etc/init.d/ scripts.
#		This file should be used to construct scripts for /etc/init.d.
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian GNU/Linux
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
# 		Modified for the nbd-client package
#		by Wouter Verhelst <wouter@debian.org>
#
# Version:	@(#)skeleton  1.8  03-Mar-1998  miquels@cistron.nl
#TODO: find a better way to figure out what nbd-devices need to be
#disconnected (the for-loop works, but the kernel does
#printk("NBD_DISCONNECT") after each request).

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON="/bin/nbd-client"
NAME="nbd-client"
DESC="NBD client process"

test -f /etc/nbd-client && . /etc/nbd-client

test -x $DAEMON || exit 0

case "$1" in
    connect)
	# I don't use start-stop-daemon:the invoking nbd-client
	# process exits immediately, while a running nbd-client
	# process can't be stopped with kill(1), since the kernel uses
	# it.
	modprobe -k nbd
	echo -n 'Connecting...'
	i=0
	while [ ! -z ${NBD_DEVICE[$i]} ]
	  do
	  $DAEMON ${NBD_HOST[$i]} ${NBD_PORT[$i]} ${NBD_DEVICE[$i]}
	  echo "connected ${NBD_DEVICE[$i]}"
	  i=`expr $i + 1`
	done
	;;
    start)
	echo -n "Starting $DESC: "
	$0 connect
	echo 'Activating...'
	i=0
	while [ ! -z ${NBD_DEVICE[$i]} ]
	  do
	  case ${NBD_TYPE[$i]} in
	      "s")
		  /sbin/swapon ${NBD_DEVICE[$i]}
		  echo "${NBD_DEVICE[$i]}: swap activated."
		  ;;
	      "f")
		  /sbin/fsck -a ${NBD_DEVICE[$i]}
		  if [ $? -lt 2 ]
		      then
		      /bin/mount ${NBD_DEVICE[$i]}
		      echo "${NBD_DEVICE[$i]}: filesystem mounted."
		  else
		      echo "fsck of ${NBD_DEVICE[$i]} failed. Not mounting."
		  fi
		  ;;
	      "r")
		  # Nothing needs to be done
		  echo "${NBD_DEVICE[$i]}: raw selected. doing nothing."
		  ;;
	      *)
		  echo "Error: NBD_TYPE[$i] contains unknown value ${NBD_TYPE[$i]}"
		  ;;
	  esac
	  i=`expr $i + 1`
	done
	echo "$NAME."
	;;
    stop)
	echo "Stopping $DESC: "
	# We want to make sure that *all* nbd-client processes are
	# stopped.
	echo "umounting all filesystems for nbd-blockdevices..."
	for i in `mount | cut -d " " -f 1 | grep nb`
	  do
	  umount $i 2>/dev/null
	  if [ $? -eq 1 ]
	      then
	      echo -n "umount of $i failed! Data loss may occur! will continue in 10 seconds..."
	      sleep 1
	      for i in 9 8 7 6 5 4 3 2 1
		do
		echo -n $i" "
		sleep 1
	      done
	      echo "ok, going on..."
	  fi
	  echo $i
	done
	if [ -d /dev/nbd ]
	    then
	    echo "Invoking swapoff on all files in /dev/nbd..."
	    swapoff /dev/nbd/* 2>/dev/null
	    echo "Disconnecting $DESCes..."
	    for i in /dev/nbd/*
	      do
	      $DAEMON -d $i 2>/dev/null >/dev/null
	    done
	else
	    echo "Invoking swapoff on all 'nb*' files in /dev..."
	    swapoff /dev/nb* 2>/dev/null
	    echo "Disconnecting $DESCes..."
	    for i in /dev/nb*
	      do
	      $DAEMON -d $i
	    done
	fi
	rmmod nbd
	echo "$NAME."
	;;
    restart|force-reload)
        $0 stop
	sleep 10
	$0 start
	;;
    *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|connect|stop|restart|force-reload|}" >&2
	exit 1
	;;
esac

exit 0
