#/bin/sh
# Automated partitioning of new unformated hard disk drives for kanotix

typeset -i CYLSIZE ALLSIZE ALLCYL RESTSIZE
typeset -i SWAPSIZE SWAPCYL ROOTSIZE ROOTCYL HOMESIZE HOMECYL

# Set the root partition to 8GB and swap to 2GB, the rest is for the home partition
SWAPSIZE=2048*1024*1024
ROOTSIZE=8*1024*1024*1024

RAM=$(sed -n 's/^MemTotal: *\([0-9][0-9]*\) [a-zA-Z ]*$/\1/p' /proc/meminfo)
SWAPMIN=$(($RAM*3/2))

# SWAP=$(LANG=C fdisk -l | sed s/+// | awk '/Linux swap/{print $4" "$1}' | sort -nr | head -1)

CYLSIZE=`sfdisk -l 2>/dev/null | sed -n '/=/p'| sed 's/ Bytes.*$//;s/^.* //g' | head -1`
ALLCYL=`sfdisk -l 2>/dev/null | sed -n '/:/p'| sed 's/^.*: //;s/ .*$//g' | head -1`
DEVICE=`sfdisk -l 2>/dev/null | sed -n '/:/p'| sed 's/:.*$//;s/^.* //' | head -1`

# Test if partition exist and exit in such case without repartitionig
PART=`fdisk -l $DEVICE 2>/dev/null | grep "^${DEVICE}[1-9]" | sed 's/ .*$//g'`
if [ "$PART" != "" ]; then
  echo "At least one partition exists on the destination drive!"
  echo "If necessary please change the partitions manually."
  exit 1
fi

ALLSIZE=($ALLCYL*$CYLSIZE)
RESTSIZE=($ALLSIZE-$ROOTSIZE-$SWAPSIZE)
HOMESIZE=($RESTSIZE)

SWAPCYL=($SWAPSIZE/$CYLSIZE)
ROOTCYL=($ROOTSIZE/$CYLSIZE)
HOMECYL=($ALLCYL-$BOOTCYL-$SWAPCYL-$ROOTCYL)

sfdisk $DEVICE << EOF
0,$ROOTCYL,83
,$HOMECYL,83
,,82
EOF

mkswap `echo $DEVICE`2

exit 0
