#!/bin/bash
#
# version: 1.2
#
# (un)mounts the ext2 file systems needed by Pollinux and starts/stops the nfs server. 
#
# Original script created by AgentData
#
# Modified by Laszlo H. and Uwe H.
# 
# changes:
#	- moved image files to a fixed directory (UH, v1.2)
#	- added absolute path to commands and centralized configuration of commands (UH, v1.2)
#
# attention: 
#	- The file /etc/exports gets overwritten each time prior to the start of the NFS server!
#	- A backup of the file /etc/exports is created only once when it does not exist.

# test if user has root privileges
if [ $UID != 0 ]; then
	echo you must be root to run this script!
	exit 1
fi

# the common root directory
POLL_LINUX=${HOME}/poll_linux
# change the directory names here if you want 
MOUNT_DIR=${POLL_LINUX}/mount
IMAGES_DIR=${POLL_LINUX}/images

# short cuts to the commands used below
MOUNT=/bin/mount
UMOUNT=/bin/umount
MKDIR=/bin/mkdir
LOSETUP=/sbin/losetup
DD=/bin/dd
MKFS=/sbin/mkfs
CAT=/bin/cat

mount_nfs ()
{
	# create all folders
	echo creating folder structure...
	${MKDIR} -p ${MOUNT_DIR}/{nandfs,nandfs.user,nandfs.config,nandfs.profile}
	${MKDIR} -p ${IMAGES_DIR}
	echo done

	cd ${IMAGES_DIR}

	# if the file does not exist, create it and format it with ext2
	if [ ! -f nandfs.config ]; then
		echo creating nandfs.config file
		LOOP_DEV=`${LOSETUP} -f`
		${DD} if=/dev/zero of=nandfs.config bs=1024 count=5120
		${LOSETUP} ${LOOP_DEV} nandfs.config
		${MKFS} -t ext2 ${LOOP_DEV}
		${LOSETUP} -d ${LOOP_DEV}
	fi

	if [ ! -f nandfs.profile ]; then
        	echo creating nandfs.profile file
		LOOP_DEV=`${LOSETUP} -f`
	        ${DD} if=/dev/zero of=nandfs.profile bs=1024 count=5120
        	${LOSETUP} ${LOOP_DEV} nandfs.profile
	        ${MKFS} -t ext2 ${LOOP_DEV}
        	${LOSETUP} -d ${LOOP_DEV}
	fi

	echo mount rootfs...
	${MOUNT} -o loop -t ext2 ${IMAGES_DIR}/nfsroot.ext2 ${MOUNT_DIR}/nandfs
	echo done

	echo mount userfs...
	${MOUNT} -o loop -t ext2 ${IMAGES_DIR}/nfsuser.ext2 ${MOUNT_DIR}/nandfs.user
	echo done

	echo mount configfs...
	${MOUNT} -o loop -t ext2 ${IMAGES_DIR}/nandfs.config ${MOUNT_DIR}/nandfs.config
	echo done

	echo mount profilefs...
	${MOUNT} -o loop -t ext2 ${IMAGES_DIR}/nandfs.profile ${MOUNT_DIR}/nandfs.profile
	echo done

	# create a backup from the old file
	if [ ! -f /etc/exports.backup ]
	then
		cp -a /etc/exports /etc/exports.backup
		echo created backup of the file /etc/exports
	fi

	echo add exports...
	${CAT} <<EOT > /etc/exports
${MOUNT_DIR}/nandfs		*(rw,insecure,no_root_squash,no_subtree_check)
${MOUNT_DIR}/nandfs.user		*(rw,insecure,no_root_squash,no_subtree_check)
${MOUNT_DIR}/nandfs.config		*(rw,insecure,no_root_squash,no_subtree_check)
${MOUNT_DIR}/nandfs.profile	*(rw,insecure,no_root_squash,no_subtree_check)
EOT
	echo done
}

start_nfs ()
{
	echo start nfs-kernel-server...
	/etc/init.d/portmap start
	/etc/init.d/nfs-kernel-server start
	echo done
}

stop_nfs ()
{
	echo stopping nfs server
	/etc/init.d/nfs-kernel-server stop
	/etc/init.d/portmap stop
	echo done
}

unmount_nfs () 
{
	echo unmounting all POLL_LINUX file systems
	${UMOUNT} ${MOUNT_DIR}/nandfs
	${UMOUNT} ${MOUNT_DIR}/nandfs.user
	${UMOUNT} ${MOUNT_DIR}/nandfs.config
	${UMOUNT} ${MOUNT_DIR}/nandfs.profile
	echo done
}

case $1 in
	"start")
		mount_nfs
		start_nfs
		;;
	"stop")
		stop_nfs
		unmount_nfs
		;;
	"mount")
		mount_nfs
		;;
	"unmount")
		unmount_nfs
		;;
	*)
		echo "usage: `basename ${0}` start|stop|mount|unmount"
		exit 1
		;;
esac

