################################################################################
# Filename: ./src/plugins/iscsi/include/iscsi-functions
# 
# The contents of this file are subject to the Qlusters Public License
# Version 1.1 (the License); you may not use this file except in compliance
# with the License.
# You may obtain a copy of the License at
# http://www.openqrm.org/content/view/33/69/.
# 
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
# 
# The Original Code is openQRM.
# 
# The Initial Developer of the Original Code is Qlusters Corporation. Portions
# created by Qlusters Corporation are Copyright (C) 2003-2005. All Rights
# Reserved.
# 
# Contributor(s):
# 
# Description:
# 
# Comments:
################################################################################
function iscsi_kernel_data() {
	KERNEL_PACKAGE=$1
	KERNEL_PACKAGE_DIR_NAME=${KERNEL_PACKAGE%%.tgz}
        KERNEL_PACKAGE_NAME=${KERNEL_PACKAGE_DIR_NAME/\./_}
        KERNEL_PACKAGE_NAME=`echo $KERNEL_PACKAGE_NAME | sed -e "s/-/_/g"`
        KERNEL_PACKAGE_NAME=`basename $KERNEL_PACKAGE_NAME`
	if [ -d $KERNEL_PACKAGE_DIR_NAME/lib/modules ]; then
		QRM_SERVER_KERNEL_VERSION=`ls $KERNEL_PACKAGE_DIR_NAME/lib/modules`
	else
		QRM_SERVER_KERNEL_VERSION=$(tar ztvf $KERNEL_PACKAGE -K $KERNEL_PACKAGE_DIR_NAME/lib/modules --exclude "$KERNEL_PACKAGE_DIR_NAME/lib/modules/lib/modules/*/*" | tail -1 | sed -e 's/.*\/\([^\/]*\)\/$/\1/')
	fi
}

function iscsi_install() {
	for KERNEL_PACKAGE in `ls $QRM_PLUGIN_BASE_DIR/*.tgz`; do
		tar -C $QRM_PLUGIN_BASE_DIR -xzf $KERNEL_PACKAGE
		iscsi_kernel_data $KERNEL_PACKAGE
		$QRM_SERVER_BASE_DIR/qrm/bin/qrm-boot-image create -o -k $QRM_SERVER_KERNEL_VERSION -b $KERNEL_PACKAGE_NAME -y $KERNEL_PACKAGE_NAME -l $KERNEL_PACKAGE_DIR_NAME/ -s ISCSI
	# add the iscsi utils to the initrd
		/bin/mv $QRM_SERVER_BASE_DIR/qrm/tftpboot/boot/initrd-$KERNEL_PACKAGE_NAME.img $QRM_SERVER_BASE_DIR/qrm/tftpboot/boot/initrd-$KERNEL_PACKAGE_NAME.img.gz
		gunzip $QRM_SERVER_BASE_DIR/qrm/tftpboot/boot/initrd-$KERNEL_PACKAGE_NAME.img.gz
		TMPDIR=`mktemp -q -d /tmp/XXXXXX`
		mkdir -p $TMPDIR/iscsi
		mount -o loop $QRM_SERVER_BASE_DIR/qrm/tftpboot/boot/initrd-$KERNEL_PACKAGE_NAME.img $TMPDIR/iscsi
		if [ -f $KERNEL_PACKAGE_DIR_NAME/etc/init.d/iscsi ]; then
			/bin/cp -f $KERNEL_PACKAGE_DIR_NAME/etc/init.d/iscsi $TMPDIR/iscsi/etc/init.d/
		fi
		if [ -d $KERNEL_PACKAGE_DIR_NAME/libs ]; then
			/bin/cp -aR $KERNEL_PACKAGE_DIR_NAME/libs/* $TMPDIR/iscsi/lib/
		fi
		/bin/cp -f $KERNEL_PACKAGE_DIR_NAME/sbin/* $TMPDIR/iscsi/sbin/
		umount $TMPDIR/iscsi	
		/bin/rm -rf $TMPDIR/iscsi
		gzip $QRM_SERVER_BASE_DIR/qrm/tftpboot/boot/initrd-$KERNEL_PACKAGE_NAME.img
		/bin/mv $QRM_SERVER_BASE_DIR/qrm/tftpboot/boot/initrd-$KERNEL_PACKAGE_NAME.img.gz $QRM_SERVER_BASE_DIR/qrm/tftpboot/boot/initrd-$KERNEL_PACKAGE_NAME.img
	done

}

function iscsi_uninstall() {
	: 
}

function iscsi_start() {
	:	
}

function iscsi_stop() {
	:
}

function iscsi_run_once() {
	for KERNEL_PACKAGE in `ls $QRM_PLUGIN_BASE_DIR/*.tgz`; do
		iscsi_kernel_data $KERNEL_PACKAGE
		$QRM_SERVER_BASE_DIR/qrm/bin/qrm-cli kernel add -k $QRM_SERVER_KERNEL_VERSION -n $KERNEL_PACKAGE_NAME -y $KERNEL_PACKAGE_NAME -s ISCSI -m Linux -a i686		
	done
}
