#!/bin/ash

. /opt/openslx/config || { echo "Could not source config!"; exit 23; }

#Redirect stdout and stderr to debuging
exec 1<&-
exec 2<&-

exec 1<>/root/quota
exec 2>&1

# check if the /home partition exists
if mount | grep "/home" > /dev/null; then
    echo "/home partition found"
    foundhome="Yes"
fi

modprobe quota_v2 #looad quota module

#Quick and Dirt fix to fstab so it has tabs and flags to quota work
mv /etc/fstab /etc/fstab2
cat /etc/fstab2 | sed 's/\\t/\t/g' | sed 's/home\tauto\t\tnoauto,noexec/home\tauto\t\tdefaults,usrquota,grpquota/g' >> /etc/fstab
rm /etc/fstab2
mount -o remount /home

if ( [ ! -e /home/aquota.user ] || [ ! -e /home/aquota.group ] ); then
  quotaoff /home 2>&1
  quotacheck -ug /home
  echo "Checking Quota"
fi
quotareturn=$(quotaon /home 2>&1) #turn on quota

if ( [ -z ${quotareturn} ]  ); then
  echo "Making Quota"
  for line in $SLX_USERS; do
    IFS=,
    set $line
    unset IFS
    if ( [ $1 = "A" ] ); then
      quotatool -u $2 -bl $4 /home
    fi
  done
else
  echo "Error: cant turn on quota"
  echo ${quotareturn}
fi
