#! /bin/sh
# Copyright (c) 1995-1998 SuSE GmbH Nuernberg, Germany.
#
# Author: adrian@suse.de
#
# /etc/init.d/bsworker
#   and its symbolic  link
# /usr/sbin/rcbsworker
#
### BEGIN INIT INFO
# Provides:          bsworker
# Required-Start:    $time $network $syslog bsmd
# Required-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 4 6
# Description:       openSUSE build service worker
### END INIT INFO

. /etc/rc.status

# Determine the base and follow a runlevel link name.
base=${0##*/}
link=${base#*[SK][0-9][0-9]}
hostip=storage
screenrc=/etc/screenrc.worker
bsopt=""
lvm="yes"

if [ -f /etc/buildhost.config ];then
	. /etc/buildhost.config
	bsopt="$bsopt --xen"
fi

rc_reset
case "$1" in
    start)
                # Initial lvm setup
                if [ "$lvm" == "yes" ]; then
                   lvm lvremove -f abuild
                fi

		# create /etc/screenrc.worker
		echo "zombie on"        > $screenrc
		echo "caption always"  >> $screenrc
		echo "scrollback 1000" >> $screenrc

		# sync latest code state from server
		rsync -avHL $hostip::opensuse-bsworker /root/bs
	
		cd /root/bs
		NUM=`grep ^processor /proc/cpuinfo  | wc -l`
		I=0
		ROOT="/abuild"
		echo "Run $NUM bsworker using $ROOT"
		while test "$NUM" -gt "$I"; do
			I=$(( $I + 1 ))
			title="$HOSTNAME/$I"
			# prepare xen build setup...
			# requires the file /etc/buildhost.config which contains
			# the following variables:
			# ---
			# XEN_ROOT=[n]
			# XEN_SWAP=[n]
			# XEN_MEMX=[n]
			# ---
			if [ ! -d $ROOT/root_$I ];then
				mkdir -p $ROOT/root_$I
			fi
			if [ ! -z "$bsopt" ];then
                           if [ "$lvm" == "yes" ]; then
                                xen_opts="--xendevice /dev/abuild/${I}_root --xenswap /dev/abuild/${I}_swap"
				lvdisplay abuild >& /dev/null || exit
                                num_pv=`vgs | grep abuild |awk '{ print $2 }'`
                                lvm lvcreate -n ${I}_root -i$num_pv -L ${XEN_ROOT}M abuild || exit
                                lvm lvcreate -n ${I}_swap -i$num_pv -L ${XEN_SWAP}M abuild || exit
                           else
                                xen_opts="--xendevice $ROOT/root_$I/root --xenswap $ROOT/root_$I/swap"
				dd if=/dev/zero of=$ROOT/root_$I/root bs=1M seek=$XEN_ROOT count=1
				dd if=/dev/zero of=$ROOT/root_$I/swap bs=1M seek=$XEN_SWAP count=1
                           fi
			   echo $XEN_MEMX > $ROOT/root_$I/memory
			fi
			mkdir -p /var/run/bsworker_$I
			# prepare bsworker startup in screen...
			echo "screen -t $title ./bs_worker $bsopt $xen_opts --root $ROOT/root_$I --statedir /var/run/bsworker_$I --id $HOSTNAME/$I" \
				>> $screenrc
		done
		screen -m -d -c $screenrc
	;;
	stop)
		echo -n "NOT IMPLEMENTED "
	rc_status -v
	;;
	restart)
		echo -n "NOT IMPLEMENTED "
		## If first returns OK call the second, if first or
		## second command fails, set echo return value.
		#$0 stop &&  $0 start
		rc_status
	;;
	try-restart)
		$0 status
		if test $? = 0; then
			$0 restart
		else
			rc_reset        # Not running is not a failure.
		fi
		# Remember status and be quiet
		rc_status
	;;
	reload)
	;;
	status)
		echo  "NOT IMPLEMENTED"
		#rc_status -v
	;;
	*)
		echo "Usage: $0 {start|stop|status|try-restart|restart|reload}"
		exit 1
	;;
esac
rc_exit
