#!/bin/sh
#
# (c) 2007 Swisslinux.org fbianco(a)swisslinux(d)org
# GPL V.3 or later
#
#
# Autoparted :
#
# The goal of this script is to partition the disk automatically.
#
# It should be able to detect an already partitioned disk and do a backup of the user data.
# Partman does not currently support such a complex recipe for recovery or OEM CD
# (Not implemented yet)
#
#
getSize_sda() {
	#
	# Return the disk size in Megabytes
	#
	SIZE_SDA=`sed -n 's/.* \([0-9]*\) sda$/\1/p' < /proc/partitions`

	SIZE_SDA=$(expr 0000000"$SIZE_SDA" : '0*\(..*\)......$') # convert to megabytes
	
	echo $ram
}

getSize_ram() {
	#
	# Return the RAM size in Megabytes
	#
	ram=$(grep ^Mem: /proc/meminfo | { read x y z; echo $y; }) # in bytes
	if [ -z "$ram" ]; then
		ram=$(grep ^MemTotal: /proc/meminfo | { read x y z; echo $y; })000
	fi
	ram=$(expr 0000000"$ram" : '0*\(..*\)......$') # convert to megabytes
	
	echo $ram
}

create_partition() {
	#
	# Create the partition in fuction of the disk and ram sizes
	#
	# FIXME do the partitionning in % of the available space and depending on the ram (getSize_ram, getSize_sda())
	logger AUTOPARTED: Create partitions

	dd if=/dev/zero of=/dev/sda count=1 bs=512 # write zero

	parted -s -- /dev/sda mklabel msdos				# Create a disk label
	parted -s -- /dev/sda mkpartfs primary ext2 0 50		# /boot
	parted -s -- /dev/sda mkpart extended 50 -1s			# --> extended
	parted -s -- /dev/sda mkpartfs logical linux-swap 50 1050	# swap
	parted -s -- /dev/sda mkpartfs logical ext2 1050 6000		# /
	parted -s -- /dev/sda mkpartfs logical ext2 6000 -1s		# /home

	# Allow the kernel to refresh the partition table
	sleep 5

	# Journalized the partitions, i.e change from ext2 to ext3
	mkfs.ext3 /dev/sda1
	tune2fs -j /dev/sda6
	tune2fs -j /dev/sda7
}

make_swap() {
	#
	# Setup and activate swap
	#
	logger AUTOPARTED: Make swap

	mkswap /dev/sda5
	swapon /dev/sda5
}

make_directories() {
	#
	# Create the directories structure for the debian-installer
	#
	logger AUTOPARTED: Make directories

	mkdir -p /target

}

mount_disks() {
	#
	# Mount the disk in the dir structure for the debian-installer
	#
	logger AUTOPARTED: Mount disks

	mount /dev/sda6 /target -t ext3

	mkdir -p /target/boot
	mkdir -p /target/home
	mount /dev/sda1 /target/boot -t ext3
	mount /dev/sda7 /target/home -t ext3
}

write_fstab() {
	#
	# Create fstab config file
	#
	# TODO use UUID instead of /dev/sda*
	logger AUTOPARTED: Write fstab

	mkdir -p /target/etc
	echo "# /etc/fstab: static file system information." > /target/etc/fstab
	echo "#" >> /target/etc/fstab
	echo "#                   " >> /target/etc/fstab
	echo "/dev/sda1     /boot   ext3  defaults  1  2" >> /target/etc/fstab
	echo "/dev/sda6     /       ext3  defaults  1  1" >> /target/etc/fstab
	echo "/dev/sda5     none    swap  sw        0  0" >> /target/etc/fstab
	echo "/dev/sda7     /home   ext3  defaults  0  0" >> /target/etc/fstab
	echo "proc          /proc   proc  defaults  0  0" >> /target/etc/fstab
}

case "$1" in
	execute)
		#
		# Do all the needed stuff, partition, mount, write fstab
		#
		logger AUTOPARTED: Starting

		modprobe dm_mod # ? no idea, if this is needed at all.
				
		# FIXME : Create the text and info file
		# 	  to have a "nice" screen output during the debian installer process
		#db_progress START 0 5 TextFileName
		#db_progress INFO InfoFileName
		
		# TODO : Write a function which is able to detect the available partitions
		#	 and if they were created by autoparted, then copy all the data in the /home
		#	 in a backup dir of the /home and do not format this partition.

		create_partition
		#db_progress STEP 1
		
		make_swap
		#db_progress STEP 1
		
		make_directories
		#db_progress STEP 1

		mount_disks
		#db_progress STEP 1
		
		write_fstab
		#db_progress STOP
	;;

	*)
		echo $0: This script is destructive and should only be run as part of the debian-installer process
	;;
esac