#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
#
# To solve the small partition image restored to larger partition problem.
#

. /opt/drbl/sbin/drbl-conf-functions

# Load basic functions of ocs
. /opt/drbl/sbin/ocs-functions

export LC_ALL=C

target_hd="$1"
DEST_PART="$2"

Usage() {
    echo "Usage: `basename $0` harddrive partition"
    echo "Example: `basename $0` hda 1"
}

# check if the user input /dev/hda, /dev/hdb...
check_input_hd() {
    local target_hd="$1"
    case "$target_hd" in
	 [hs]d[a-z])
	   true
	   ;;
	 *)
	  echo "Unknown HD device! Program terminated!"
          Usage
	  exit 1
    esac
}

#
check_col_idx() {
  # DEV is like /dev/hda
  # wanted is like "start", "end", "file" (since it's "file system" in SuSE 10.0, and filesystem in Redhat/FC)
  local DEV=$1
  local wanted=$2
  for i in 1 2 3 4 5 6 7; do
    col="$(parted $DEV p | grep -i flags | awk -F" " "{print \$$i}")"
    if [ -n "$(echo $col | grep -i $wanted)" ]; then
      col_idx=$i
      break
    fi
  done
  echo $col_idx
}

#
[ $# -ne 2 ] && Usage && exit 1

#
check_input_hd $target_hd

#
case "$DEST_PART" in
    [1-9])
             true
             ;;
    [0-9][0-9])
             true
             ;;
        *)
             Usage
             exit 1
esac

target_hd="/dev/$target_hd"
part_info=`mktemp /tmp/part.XXXXXX`

#output example
#Disk geometry for /dev/hda: 0.000-1536.000 megabytes
#Disk label type: msdos
#Minor    Start       End     Type      Filesystem  Flags
#1          0.031   1240.312  primary   reiserfs    
#4       1355.977   1535.625  extended              
#5       1356.007   1535.625  logical               
#Information: Don't forget to update /etc/fstab, if necessary.

partition="${target_hd}${DEST_PART}"

parted $target_hd p > $part_info

# check partition, if no such partition exist, exit, otherwise
# "parted $partition p" will hang
if [ -z "`grep "^$DEST_PART" $part_info`" ]; then
  echo "No such partition ($partition) exists! Program terminated!"
  [ -f "$part_info" ] && rm -f $part_info
  exit 1
fi
if [ -n "`grep "^$partition" /proc/mounts`" ]; then
  echo "Partition $partition is already mounted! You must unmount it first! Program terminated!"
  [ -f "$part_info" ] && rm -f $part_info
  exit 1
fi

start_idx="$(check_col_idx $target_hd start)"
end_idx="$(check_col_idx $target_hd end)"
filesystem_idx="$(check_col_idx $target_hd file)"
part_start=$(grep "^$DEST_PART" $part_info | awk -F" " "{print \$$start_idx}")
part_end=$(grep "^$DEST_PART" $part_info | awk -F" " "{print \$$end_idx}")
part_fs=$(grep "^$DEST_PART" $part_info | awk -F" " "{print \$$filesystem_idx}")

#echo "start_size end_size (megabytes) filesystem: $part_start $part_end $part_fs"

case "$part_fs" in
   reiserfs)
              if ! which resize_reiserfs &> /dev/null; then
                 echo "Unable to find program resize_reiserfs! Skip this partition ${partition}"
		 exit 1
	      fi
              echo "resize_reiserfs ${partition}"
              resize_reiserfs ${partition}
              ;;
   vfat|fat16|fat32)
              if ! which parted &> /dev/null; then
                 echo "Unable to find program parted! Skip this partition ${partition}"
		 exit 1
	      fi
              #  resize MINOR START END
              echo "parted $target_hd resize $DEST_PART $part_start $part_end"
              parted $target_hd resize $DEST_PART $part_start $part_end
              ;;
   ext2|ext3)
              if ! which e2fsck &> /dev/null || ! which resize2fs &> /dev/null ; then
                 echo "Unable to find program e2fsck! Skip this partition ${partition}"
		 exit 1
	      fi
              echo "e2fsck -f ${partition}; resize2fs ${partition}"
              e2fsck -f ${partition}
              resize2fs ${partition}
              ;;
        ntfs)
              if ! which ntfsresize &> /dev/null; then
                 echo "Unable to find program ntfsresize! Skip this partition ${partition}"
		 exit 1
	      fi
              echo "ntfsresize ${partition}"
              ntfsresize ${partition}
              ;;
        *)
	      echo "\"$part_fs\" is unknown or unsupported filesystem...Do not resize that. Program terminated!"
	      exit 1
esac

[ -f "$part_info" ] && rm -f $part_info
