#! /bin/bash -e
#
#	MOSIX $Id: mosix.init,v 1.21 2001/04/05 17:46:44 amnons Exp $
# 
# Description: MOSIX is an extension to the Linux kernel to
#	       support scalable and transparent cluster computing.
#
# mosix		Script to stop/start MOSIX
#
# Author:       Amnon Shiloh
#
# Modified for Debian by: Viral Shah <viral@debian.org>
#

# Read the mosix variables to make decisions
. /etc/default/mosix

stop_mosix() {
    if [ -f /proc/mosix/admin/mospe ]; then
	mosctl expel
	mosctl bring
        echo 0 > /proc/mosix/admin/mospe
    else
        echo Error: this is not MOSIX!
        exit 0
    fi
}

start_mosix() {
	if [ -s /etc/mosix/overheads -a -f /proc/mosix/admin/overheads ]
	then
		 /bin/grep -v '^#' /etc/mosix/overheads > /proc/mosix/admin/overheads
	fi
	if [ -s /etc/mosix/mfscosts -a -f /proc/mosix/admin/mfscosts ]
	then
		 /bin/grep -v '^#' /etc/mosix/mfscosts > /proc/mosix/admin/mfscosts
	fi
	a1=
	[ -s /etc/mosix/mospe ] && a1="-p `cat /etc/mosix/mospe`"
	a2=
	[ -s /etc/mosix/mosgates ] && a2="-g `cat /etc/mosix/mosgates`"
	/sbin/setpe -W $a1 $a2 -f /etc/mosix/mosix.map

        if [ $? = 1 ]; then
            exit 0;
        fi

        if [ $MIGRATE == "yes" ]; then
            /usr/bin/mosctl -lstay
            /usr/bin/mosctl -stay
        else
            /usr/bin/mosctl lstay 
        fi

        if [ $BLOCK == "yes" ]; then
            /usr/bin/mosctl block
        else
            /usr/bin/mosctl -block
	fi    

        if [ $MFS == "yes" ]; then
            /usr/bin/mosctl mfs
        else
            /usr/bin/mosctl nomfs
        fi

}

alarm() {
	t=$2
	while :
	do
		case "$t" in 0) break ;; esac
		sleep 1
		t=$(($t-1))
	done
	kill -1 $1
	exit 0
}

# First check if we have a mosix kernel and are a MOSIX node.

if [ ! -d /proc/mosix ]; then
    echo "Starting mosix node: Kernel not mosix enabled. Aborting."
    exit 0;
fi

if [ $MOSIX_NODE != "yes" ]; then
    exit 0;
fi

# See how we were called.
case "$1" in
  start)
	echo -n "Starting mosix node: "
	if [ ! -f /etc/mosix/mosix.map ]
	then
		echo You have no MOSIX configuration - please edit yours now:
		echo -n "Editor to use [q to quit] - [editor] :- "
		trap 'echo Timed-Out ; exit 1' 1
		alarm $$ 120 &
		killer=$!
		read editor
		kill $killer
		trap ''
		case "$editor" in [qQ]) exit ;;
			"") editor=/usr/bin/editor
			    [ -f $editor ] || editor=/usr/bin/ae
				;;
		esac
		echo "# MOSIX CONFIGURATION" > /etc/mosix/mosix.map
		echo "# ===================" >> /etc/mosix/mosix.map
		echo "#" >> /etc/mosix/mosix.map
		echo "# Each line should contain 3 fields, mapping IP addresses to MOSIX node-numbers:" >> /etc/mosix/mosix.map
		echo "# 1) first MOSIX node-number in range." >> /etc/mosix/mosix.map
		echo "# 2) IP address of the above node (or node-name from /etc/hosts)." >> /etc/mosix/mosix.map
		echo "# 3) number of nodes in this range." >> /etc/mosix/mosix.map
		echo "#" >> /etc/mosix/mosix.map
		echo "# MOSIX-#  IP  number-of-nodes" >> /etc/mosix/mosix.map
		echo "# ============================" >> /etc/mosix/mosix.map
		$editor /etc/mosix/mosix.map
		echo
		while :
		do
			echo "If this node's standard IP address is not part of the table that"
			echo "you just edited, because MOSIX uses a different or separate network,"
			echo "you need to type this node's MOSIX-number now."
			echo
			/bin/echo -n "Otherwise please press only <Enter> :- "
			read me
			case "$me" in "") /bin/rm -f /etc/mosix/mospe ; break ;;
				[1-9] | [1-9][0-9] | [1-9][0-9][0-9] | \
				[1-9][0-9][0-9][0-9] | \
				[1-6][0-9][0-9][0-9][0-9])
					echo $me > /etc/mosix/mospe ; break ;;
				*) echo Improper MOSIX number - please try again: ;;
			esac
		done
	fi
	start_mosix 
        echo "mosix."
        ;;
  stop)
	echo -n "Stopping mosix node: "
	stop_mosix 
        echo "mosix."
        ;;
  status)
	/sbin/setpe -r ;;
  restart|force-reload)
	echo -n "Restarting mosix node: "
	stop_mosix
	start_mosix 
        echo "mosix."
        ;;
  *)
	echo "Usage: /etc/init.d/mosix {start|stop|restart|force-reload|status}"
	exit 1
esac

exit 0
