#!/bin/bash

logfile="/etc/opt/nec/elsetup/elsetup.log"

function calc_cyls {
	size=$1
	[ -z $size ] && size=0

	cyl_nr=`echo \($size*1000*1000+\($cylinder_size-1\)\)/$cylinder_size +1 | bc`
	echo $cyl_nr
}

function check_size {

	disk=$1
	check1=$2
	check2=$3
	check3=$4

	sector_size=512

	if echo $disk | grep -e '[0-9]$' > /dev/null 2>&1 ; then
		disk_dev=`echo $disk|sed 's/[0-9]\+//g'`
	else
		disk_dev=$disk
	fi

	block_size=`LANG=C /sbin/sfdisk -l $disk_dev 2>/dev/null | grep "blocks of" | sed 's/^.*blocks[[:space:]]\+of[[:space:]]\+\([0-9]\+\).*$/\1/g'`
	cylinder_nr=`LANG=C /sbin/sfdisk -l $disk_dev 2>/dev/null | grep "^Disk" | sed 's/^.*[[:space:]]\+\([0-9]\+\)[[:space:]]\+cylinders.*$/\1/g'`
	head_nr=`LANG=C /sbin/sfdisk -l $disk_dev 2>/dev/null | grep "^Disk" | sed 's/^.*[[:space:]]\+\([0-9]\+\)[[:space:]]\+heads.*$/\1/g'`
	sector_nr=`LANG=C /sbin/sfdisk -l $disk_dev 2>/dev/null | grep "^Disk" | sed 's/^.*[[:space:]]\+\([0-9]\+\)[[:space:]]\+sectors\/track$/\1/g'`
	cylinder_size=`echo $head_nr*$sector_nr*$sector_size|bc`

	if echo $disk | grep -e '[0-9]$' > /dev/null 2>&1 ; then
		part_blocks=`LANG=C /sbin/sfdisk -s $disk 2>/dev/null`
		cyl_nr=`echo $part_blocks*$block_size/$cylinder_size|bc`
	else
		cyl_nr=`LANG=C /sbin/sfdisk -l $disk 2>/dev/null | grep "^Disk" | sed 's/^.*[[:space:]]\+\([0-9]\+\)[[:space:]]\+cylinders.*$/\1/g'`
	fi

	cyl1=`calc_cyls $check1`
	cyl2=`calc_cyls $check2`
	cyl3=`calc_cyls $check3`

	cyl_sum=`echo $cyl1+$cyl2+$cyl3|bc`
	if [ $cyl_sum -gt $cyl_nr ] ; then
		echo too big partition size were specified.
		exit 1
	fi
}

function make_part {
	line=`/bin/mount | grep -e '.*[[:space:]]\+on[[:space:]]\+/home[[:space:]]\+'`
	if [ -z "$line" ] ; then
		echo "/home is not valid partition"
		exit 1
	fi

	home=`echo $line|cut -d' ' -f1`
	home_dev=`echo $home|sed 's/[0-9]\+//g'`
	home_part=`echo $home|sed 's/[^0-9]\+//g'`
	cluster_dev1=${home_dev}$((home_part+1))
	cluster_dev2=${home_dev}$((home_part+2))

        if [ "$size2" = "0" ]; then
                size2=""
        else
	        check_size $home $size1 $size2 $home_size
                size2="+${size2}M"
        fi


	cd /
	/sbin/quotaoff  -ug /home
	tar zcpf /home.tar.gz --exclude /home/lost+found /home >/dev/null 2>&1
	umount /home

        echo "wbmc_fdisk_cmd: /sbin/fdisk $home_dev ($home_part, $home_size, ${size1}, ${size2})" >> $logfile
        /sbin/fdisk $home_dev >/dev/null 2>&1 <<EOM
	d
	$home_part
	n

	+${home_size}M
	n

	+${size1}M
	n

	${size2}
	w
	q
EOM


	# reboot
	perl -p -i -e 's/^(LABEL=\/home\s+)/\#\1/g' /etc/fstab
	cat <<EOM > $next_script
#!/bin/sh
/sbin/mke2fs -j -L /home $home >/dev/null 2>&1
perl -p -i -e 's/^\#(LABEL=\/home\s+)/\1/g' /etc/fstab
mount /home
tar zxpf /home.tar.gz -C /
rm -f /home.tar.gz
/bin/mount -o remount /home
/sbin/quotacheck -ug /home
#/sbin/quotaon -ugf /home
#/sbin/quotaon -ug /home
dd if=/dev/zero of=$cluster_dev1 count=20000 >/dev/null 2>&1
EOM

#	# not reboot
#        partprobe >> $logfile 2>&1
#        /sbin/mke2fs -j -L /home $home >>$logfile 2>&1
#        mount /home >>$logfile 2>&1
#
#        tar zxpf /home.tar.gz -C /
#        rm -f /home.tar.gz
#        /bin/mount -o remount /home
#
#        echo "wbmc_fdisk_cmd: quotacheck; quotaon" >> $logfile
#        /sbin/quotacheck -ug /home >>$logfile 2>&1
#        #/sbin/quotaon -ugf /home >>$logfile 2>&1
#        #/sbin/quotaon -ug /home >>$logfile 2>&1
#
#        dd if=/dev/zero of=$cluster_dev1 count=20000 >>/tmp/init.log 2>&1
}

function other_device {

	device=$1
	cluster_dev1=${device}1

        if [ "$size2" = "0" ]; then
                size2=""
        else
	        check_size ${device} $size1 $size2 0
                size2="+${size2}M"
        fi

	dd if=/dev/zero of=$device bs=512 count=1 >/dev/null 2>&1
        /sbin/fdisk $device >/dev/null 2>&1 <<EOM
	n
	p
	1

	+${size1}M
	n
	p
	2

	${size2}
	w
	q
EOM

	cat <<EOM > $next_script
#!/bin/sh
dd if=/dev/zero of=$cluster_dev1 count=20000 >/dev/null 2>&1
EOM

#        partprobe
#        dd if=/dev/zero of=$cluster_dev1 count=20000 >/dev/null 2>&1
}

PARTED_CONF=/tmp/parted.conf
CLCONFFILE=/etc/opt/nec/elsetup/initconf.inf
CLDEVVD="DeviceName_Mirror="
HOMEDEVVD="DeviceName_Home="
home_size=10240
size1=10
size2=$1
next_script=/tmp/parted2.sh

if [ $# -ne 1 ] ; then
	echo "$0: command syntax error"
	exit 1
fi

CLVD=`grep -e "^$CLDEVVD" $CLCONFFILE | cut -d "=" -f 2`
HOMEVD=`grep -e "^$HOMEDEVVD" $CLCONFFILE | cut -d "=" -f 2`

#if [ "$CLVD" =  "/dev/sda" ]; then
if [ "$CLVD" =  "$HOMEVD" ]; then
	make_part
else
        other_device $CLVD
fi


#if grep "$CLDEVVD" $CLCONFFILE >/dev/null 2>&1 ; then
#	make_part
#else
#	other_device
#fi

partprobe
chmod +x $next_script
exit 0
