#!/bin/sh
PART_NUM=2
PART_START=3801088
ROOT_PART=mmcblk0p2

check_commands () {
  for COMMAND in fdisk resize2fs; do
    if ! command -v $COMMAND > /dev/null; then
      FAIL_REASON="Command '$COMMAND' not found"
      return 1
    fi
  done
  return 0
}

if ! check_commands; then
  echo $FAIL_REASON
  exit 1
fi

fdisk /dev/mmcblk0 <<EOF
p
d
$PART_NUM
n
p
$PART_NUM
$PART_START

p
w
EOF

cat <<EOF > resize2fs_once &&
#!/bin/sh
# Resize the root filesystem to fill partition
echo "Starting resize2fs_once" &&
resize2fs /dev/$ROOT_PART &&
rm resize2fs_once &&
echo "The filesystem has been enlarged upon."
EOF

chmod +x resize2fs_once &&
echo "Please reboot system and execute ./resize2fs_once." 

