#!/bin/sh
# /usr/local/sbin/dphys-config-quota - automatically set up user quotas
# author franklin, last modification 2006.10.13
# This script is copyright ETH Zuerich Physics Departement,
#   use under either modified/non-advertising BSD or GPL license

# this script is called by dphys-config, once on each install/update
#   no parameters or env vars for this script
# it requires in /etc/dphys-config.list 1 line:
#   local/sbin/dphys-config-quota:/usr/:chmod 755 {}; {}


set -e

# get ready to work
# /usr/local added for FreeBSD, because their ports end up in there
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
export PATH


# eliminate accidental duplicated usrquota options on some of our hosts
#   were result of older scripts that wre not idempotent
while grep -q ',usrquota,usrquota' /etc/fstab ; do

  sed -e '/,usrquota,usrquota/s//,usrquota/' /etc/fstab > /etc/fstab.tmp
  mv /etc/fstab.tmp /etc/fstab

done


# we want quota on / and any filesystems mounted as /export/data* (if any)
#   hope there are no other relevant filesystems
# here no "" around `ls`, else intervening and trailing spaces end up in MPOINT
for MPOINT in / /export/data* ; do

  # is this actually a valid mount point?
  #   one of the /export/data* will be subdir of /, not separate mount
  #     so we must drop that one, so no simple test for directory
  if [ "`awk '$2=="'"${MPOINT}"'" { print $2 }' /etc/fstab`" != "" ] ; then

    # only if quota has not been added yet
    if [ "`awk '$2=="'"${MPOINT}"'" && $4 !~ /usrquota/ { print $2 }' \
        /etc/fstab`" != "" ] ; then

      awk 'BEGIN                        { OFS="\t" }
           $2=="'"${MPOINT}"'" && NF==6 { $4 = $4",usrquota" }
                                        { print }' \
          /etc/fstab > /etc/.fstab
      mv /etc/.fstab /etc/fstab

    fi

    # only if not already mounted with quota
    if [ "`awk '$2=="'"${MPOINT}"'" && $4 !~ /usrquota/ { print $2 }' \
        /etc/mtab`" != "" ] ; then

      # ensure that kernel knows about this mount point using quota
      mount -o remount "${MPOINT}"

      # just in case this interferes with rebuilding
      #   may return non-zero if no quota is running, so catch that
      quotaoff "${MPOINT}" 2> /dev/null || true

      # man page claims following autogenerate, Beat regards these as needed
      rm -f "${MPOINT}/quota.user"
      touch "${MPOINT}/quota.user"
      chmod 600 "${MPOINT}/quota.user"

      # we use -m (no remount as -ro), so risks losing count
      #   but this quota is only for recording, not for enforcement
      #   we only want to see users disk usage without waiting for  du -sk
      # same also -f (force) so that mount is ignored
      # the -c avoids validity check warning message on empty quota.user file
      # quotacheck can take a very long time, like find
      quotacheck -cmf "${MPOINT}"
      quotaon "${MPOINT}"

    fi

  else

    echo "$0: NOTE: no mount point ${MPOINT} in /etc/fstab," \
        "may be a subdir of /, ignoring quota request for it ..." >&2

  fi

done

exit 0
