#!/bin/sh


PARTITIONNING=0
UDEV_UTIL_VOL=/lib/udev/vol_id

argument_parsing()
{
	 while [ $# -ge 1 ]
	 do
		 case $1 in
			 "--device")
			 shift;
			 BLOCKDEVICE=$1
			 ;;
			 "--fs")
			 shift;
			 FILESYSTEM=$1
			 ;;
			 "--swap")
			 shift;
			 SPECIFIED_SWAP=$1
			 ;;
			 "--part")
			 shift;
			 SPECIFIED_PART=$1
			 ;;
			 "--partition")
			 PARTITIONNING=1
			 ;;
			 "--flags")
			 shift;
			 MKFS_FLAGS=$@
			 ;;
			 "--help")
			 shift;
			 usage
			 ;;

		 esac
		 shift
	 done
	 
}
detect_swap_partition()
{
	for i in `ls $BLOCKDEVICE[1-9] 2> /dev/null`
	do
		ID_FS_TYPE=0;
		ID_FS_TYPE=$($UDEV_UTIL_VOL $i | grep ID_FS_TYPE | sed -e 's/ID_FS_TYPE=//');
		if [ "X$ID_FS_TYPE" == "Xswap" ]
		then
			FOUND_SWAP=$i
			
		fi
	done
}
usage()
{
	echo "Usage  : $0 --device <device_to_use> | --part_<partition to_use>  --fs <filesystem>]"
	echo " --swap : specify a swap partition to use"
	echo " --device : hard drive you want to use. It will detect existing partition"
	echo " --fs : filesystem to use"
	echo " --flags : flags you want use for mkfs"
	echo " --part : Specify a partition (and not the whole disk) to format, you have to specify a swap partition"
	echo " --partition : Create  partitions on the device (swap and filesytem). All your data in the device will be lost"
	echo " --help : to show this message"
        echo "This utility allow you to format a large hard drive"
        exit	
}

partitionning()
{
	echo -ne "Warning : By executing this script, you agree to loose all data
	on the block device(y/N)\n"
	read AGREE
	if [ "X${AGREE}" == "Xy" ] || [ "X${AGREE}" == "XY" ]
	then
		echo "Ok... if you're sure"
	else
		exit;
	fi
									

	
fdisk $BLOCKDEVICE <<EOCMD
o
n
p
1
	
+512M
n
p
2


w
EOCMD
}

unmount()
{
	if [ "X${BLOCKDEVICE}" != "X" ]
	then
		for i in `mount | grep $BLOCKDEVICE | cut -d ' ' -f 1`
		do
			umount $i
		done
	fi
	
	if [ "X${SPECIFIED_PART}" != "X" ]
	then
		for i in `mount | grep $SPECIFIED_PART | cut -d ' ' -f 1`
		do
			umount $i
		done
	
	fi
	
	if [ "X${SPECIFIED_SWAP}" != "X" ]
	then
		for i in `mount | grep $SPECIFIED_SWAP | cut -d ' ' -f 1`
		do
			umount $i
		done
	fi
}

# Analysing arguments :
if [ $# -eq 0 ] 
then
	usage
fi
argument_parsing $@

if [  "X${SPECIFIED_PART}" != "X" -a "X$BLOCKDEVICE" != "X" ]
then
	echo "You cannot have a blockdevice and a partition specified at the same time" 
	exit
elif [  "X${SPECIFIED_PART}" == "X" -a "X$BLOCKDEVICE" == "X" ]
then
	echo "You need to have exactly a blockdevice or a partition specified "
	exit
fi
if [ -z $FILESYSTEM ]
then
	echo "You need to specify the filesystem you want to use" 
	exit
fi
if [ "X{$SPECIFIED_PART}" != "X"  ]
then
	if [ "X${SPECIFIED_SWAP}" != "X" ]
	then
		DATA=$SPECIFIED_PART;
	else
		echo " If you specify a partition to format you must specify a swap partition to use"
		exit;
	fi
	
fi
if [ "X${SPECIFIED_PART}" != "X" -a $PARTITIONNING -eq 1 ]
then
	echo "You cannot partition a partition" 
	exit
fi
if [  "X$SPECIFIED_SWAP" != "X" -a $PARTITIONNING -eq 1 ]
then
	echo "Warning : since you ask for a partitionning of your device, the specified swap partition will be ignored"
fi
if [ "X$FILESYSTEM" == "Xxfs" -o "X$FILESYSTEM" == "Xvfat" ]
then
	echo "You want to use xfs or fat, you don't need this script"
	exit;
fi


detect_swap_partition;
unmount
if [ "X${BLOCKDEVICE}" != "X" ]
then
	EXISTING_PARTITION=`ls $BLOCKDEVICE[0-9] 2> /dev/null | wc -l `
else
	EXISTING_PARTITION=0
fi

# Is there a specific SWAP partition to use ?
if [ "X$SPECIFIED_SWAP" != "X" -a $PARTITIONNING -eq 0 ]
then
	SWAP=$SPECIFIED_SWAP
	PARTITIONNING=0
	echo "The specified swap partition :\"$SPECIFIED_SWAP\" will be use to format your disk."
elif [ $EXISTING_PARTITION -ne 1 -a  "X$FOUND_SWAP" != "X" -a $PARTITIONNING -eq 0 ]
then 
	PARTITIONNING=0
	SWAP=$FOUND_SWAP
	echo "Found swap on the device: using \"$FOUND_SWAP\" to partition the device."
else
	PARTITIONNING=1
fi


#partitionning if needed
if [ $PARTITIONNING -eq 1 ]
then
	partitionning;
	sleep 2;
	SWAP=${BLOCKDEVICE}1
	DATA=${BLOCKDEVICE}2
	EXISTING_PARTITION=0

fi

unmount
mkswap $SWAP
swapon $SWAP

if [ $EXISTING_PARTITION -eq 1 -o $EXISTING_PARTITION -eq 0 ]
then
	mkfs.$FILESYSTEM $MKFS_FLAGS  $DATA
elif [  "X$SPECIFIED_PART" != "X"  ]
then
	mkfs.$FILESYSTEM $MKFS_FLAGS  $DATA
else
	for j in `ls $BLOCKDEVICE[1-9] | grep -v $SWAP`
	do 
		mkfs.$FILESYSTEM $MKFS_FLAGS $j ;
	done
	
fi

# Desactiving swap
swapoff  $SWAP
