#!/bin/sh
# 

PREREQ=""
DESCRIPTION="Completing Ubuntu live fstab..."
FSTAB=/root/etc/fstab

. /scripts/casper-functions

prereqs()
{
	echo "$PREREQ"
}

case $1 in
# get pre-requisites
prereqs)
	prereqs
	exit 0
	;;
esac


# To avoid fsck
# Gv4: not needed?
#touch ${rootmnt}/fastboot

# Standard initial part of fstab
# Gv4: not needed?
#cat <<EOF > ${rootmnt}/etc/fstab
## /etc/fstab: static file system information.
##
## <file system> <mount point>   <type>  <options>       <dump>  <pass>
#proc            /proc           proc    defaults        0       0
#sys             /sys            sysfs   defaults        0       0
#EOF

#cat <<EOF > /root/etc/rc2.d/S05disks.sh
cat <<EOF > /root/etc/init.d/disks
#!/bin/bash
#
# disks		Search all the local disks and write them into the /etc/fstab.
#
# Version:	@(#)disks.sh  1.0  15-Oct-2005  juanje@interactors.coop
# 		@(#)disks  2.0  19-Dec-2006  rmartin@emergya.es
#

VERBOSE=yes
TMPTIME=0
[ -f /etc/default/rcS ] && . /etc/default/rcS
. /lib/lsb/init-functions

#
# Search local disks partitions
#
get_partitions ()
{
	echo \$(ls /dev/[sh]d[a-z]?*)
}

#
# Check filesystem
#
get_filesystem ()
{
	#ret=\$(sudo file -s \$1)
	# We better use sfdisk
	disk=\${1:0:8}
	partition=\${1:8}
	ret=\$(sudo sfdisk -c \$disk \$partition)
	case \$ret in
	#	*ext3*)
		*83*)
			echo "\$1 ext3"
			;;
	#	*ext2*)
        #		
        #	echo "\$1 ext3"
	#		;;
	#	*reiserfs*)
	#		echo "\$1 reiserfs"
	#		;;
	#	*XFS*)
	#		echo "\$1 xfs"
	#		;;
	#	*vfat*)
		b)
			echo "\$1 vfat"
			;;
		c)
			echo "\$1 vfat"
			;;
	#	*ntfs*)
		7)
			echo "\$1 ntfs"
			;;
	#	*swap*)
		82)
			echo "\$1 swap"
			;;
		5)
			echo "\$1 extended"
			;;
		*)
			echo "\$1 auto"
			;;
	esac
}

#
# Search for CD/DVDs
#
get_cdroms ()
{
	echo \$(head -3 /proc/sys/dev/cdrom/info | tail -1 | cut -f 3-)
}

#
# Create fstab
#
add_partitions_to_fstab()
{

	partitions=\$(get_partitions)
	win_counter=1
	for part in \$partitions; do
		if \$(grep rw /proc/cmdline) ; then
			defaults="    users,noauto,defaults	0       2"
		else
			defaults="    ro,users,noauto,defaults 	0       2"
		fi
		entry=\$(get_filesystem \$part)
		dev=\$(echo \$entry | cut -d ' ' -f 1 )
		fs=\$(echo \$entry | cut -d ' ' -f 2 )
		if [ "\$fs" == "extended" ] ; then
			continue
		fi 
		if [ "\$fs" == "swap" ] ; then
			entry="\${dev}	none	swap    sw              0       0"
		else
			mntpoint="/media/\${dev#/dev/}"
			if [ "\$fs" == "ntfs" ]; then
				defaults=\$(echo \${defaults} | sed 's|users|gid=100,users,umask=0222,fmask=0333,sync,nls=utf8|g')
				mntpoint="/media/Windows\${win_counter}"
				let win_counter=win_counter+1
			fi
			if [ "\$fs" == "vfat" ]; then
				defaults=\$(echo \${defaults} | sed 's|users|gid=100,users,umask=0002,fmask=0113,sync|g')
				mntpoint="/media/Windows\${win_counter}"
				let win_counter=win_counter+1
			fi
			entry="\${dev}            \${mntpoint}            \${fs}            \${defaults}"
			mkdir -p \$mntpoint
		fi
		echo \$entry >> /etc/fstab
	done
	num=0
# CD-ROMs are now managed by pmount; they should not be in fstab
#	cdroms=\$(get_cdroms)
#	for cd in \$cdroms; do
#		entry="/dev/\${cd}            /media/cdrom\${num}            udf,iso9660 ro,user,noauto  0       0"
#		mkdir -p /media/cdrom\${num} >> /var/log/initramfs.debug 2>&1
#		let num=num+1
#		echo \$entry >> /etc/fstab
#	done
	
}

get_device ()
{
	dev=\$(grep /.dirs/dev /proc/mounts | cut -d " " -f 1)
	echo \$dev
}

log_begin_msg "Searching local disks..."
add_partitions_to_fstab
#rm -f /etc/mtab
#cat /proc/mounts > /etc/mtab
#mount -a  >> /var/log/initramfs.debug 2>&1
#swapon -a >> /var/log/initramfs.debug 2>&1
#device=\$(get_device)
#mount \$device >> /var/log/initramfs.debug 2>&1
log_end_msg 

exit 0
EOF

#/root/bin/chmod +x /root/etc/rc2.d/S05disks.sh 
/root/bin/chmod +x /root/etc/init.d/disks 

# creating symbolic links in /etc/rc?.d/ to /etc/init.d/disks.sh
chroot /root update-rc.d disks start 5 2 3 4 5 .
