#!/bin/bash
file_system="${1:-/}"
fstype=$(findmnt -n -o FSTYPE $file_system)
echo "Fs is: $file_system"

if [ -s /usr/local/mgr5/etc/ispmgr.conf.d/db.conf ]; then
  mysql -N ispmgr -e "select users.name, userprops.value from users inner join userprops on id=userprops.users where userprops.name='limit_quota' AND value>0;" >quotas_file
else
  sqlite3 -separator " " /usr/local/mgr5/etc/ispmgr.db "select users.name, userprops.value from users inner join userprops on id=userprops.users where userprops.name='limit_quota' AND value>0;" > quotas_file
fi

user_count=$(wc -l < quotas_file)
for ((count = 1; count <= $user_count; count++)); do
  group="$(awk "NR==$count {print \$1}" quotas_file)"
  quota="$(awk "NR==$count {print \$2}" quotas_file)"
  in=$(($quota * 1024 / 8))

  echo "$count Установка квоты для группы $group"
  if [ "$fstype" = "ext4" ]; then
    setquota -g $group "$quota"M "$quota"M $in $in $file_system
  elif [ "$fstype" = "xfs" ]; then
    xfs_quota -x -c "limit -g bsoft=${quota}m bhard=${quota}m isoft=${in} ihard=${in} ${group}"
  fi
done
rm quotas_file -f
