#!/bin/bash
source /etc/alternc/local.sh
set -x
# Set disk quota to an user
# Won't work over NFS

MID=$1
SIZE=$2
AWK=/usr/bin/awk
MOUNT=/bin/mount
SED=/bin/sed
QUOTA=/usr/sbin/setquota
DF=/bin/df

#checking if quotas are installed
command -v $QUOTA >/dev/null || { echo "Quotas uninstalled"; exit 1; }

if [ $# -ne 2 ] || [[ ! "$MID" =~ ^[0-9]+$ ]] || [[ ! "$SIZE" =~ ^[0-9]+$ ]]; then
  echo "Usage: quota_edit <uid> <size>"
  echo "Edit the quota of the AlternC account having uid <uid> the the available space to <size>"
  exit 1
fi

DATA_PART=`$DF -P "${ALTERNC_HTML}" 2>/dev/null | $AWK 'NR==2 { print $NF }'`

if [[ ! -e $DATA_PART"/aquota.group" ]]; then
  echo "Group quota are not enabled on $DATA_PART filesystem "
  exit 2
fi


sudo $QUOTA -r -g $MID $SIZE 0 0 0 $DATA_PART 2>/dev/null || echo "Group quota are not enabled on $ALTERNC_HTML." >&2