#!/bin/sh

PREREQ=""

prereqs() {
  echo "$PREREQ"
}
case "$1" in
  prereqs)
    prereqs
    exit 0
    ;;
esac

# parse kernel boot command line
ROOT_DEVICE=
for CMD_PARAM in $(cat /proc/cmdline); do
  case ${CMD_PARAM} in
    root=*)
      ROOT_DEVICE=${CMD_PARAM#root=}
      ;;
  esac
done

# get the device name of the data partition (%? removes the last character)
DATA_DEVICE=${ROOT_DEVICE%?}5

features=$(/sbin/tune2fs -l "${DATA_DEVICE}" | grep 'Filesystem features:')
if [ -z "$features" ]; then
  echo "No ext4 device found at ${DATA_DEVICE}"
  exit 0
fi
if echo "$features" | grep -q quota && echo "$features" | grep -q project; then
  echo "quota and project already enabled in ${DATA_DEVICE}"
  exit 0
fi
echo "Enabling ext4 quota and project on ${DATA_DEVICE} "
/sbin/tune2fs -O quota,project "${DATA_DEVICE}" || echo "tune2fs: $?"
