#!/bin/bash 

source /usr/local/lib/hostsharing/adr
source /usr/local/lib/hostsharing/pac
source /root/etc/config
source /usr/local/lib/hostsharing/shquota.bash

# Running as interactive program?
tty | grep 'not a tty' >/dev/null 2>&1
INTERACTIVE=$?

# mysql DB Owner korrigieren
#if ls -d /var/lib/mysql/*_* >/dev/null 2>/dev/null; then
#  for DBPATH in `find /var/lib/mysql/*_* \( -group root -o -group mysql \) -print`; do
#    DB=`echo $DBPATH | cut -d/ -f5`
#    if [ "${DB}" != "mysql" ]; then
#      PAC=${DB:0:5}
#      grep -s -q "^${PAC}:" /etc/group
#      if [ $? -eq 0 ]; then
#        chgrp $PAC $DBPATH
#     fi
#    fi
#  done
#  
#  find /var/lib/mysql/*_* -type d ! -perm 750 -exec chmod 750 \{\} \;
#  find /var/lib/mysql/*_* -type f ! -perm 600 -exec chmod 600 \{\} \;
#fi

QUOTADIR=/var/lib/hostsharing/quota

test ! -d ${QUOTADIR} && mkdir -m 755 ${QUOTADIR}

# Remove old notification flags if older than 23hrs (1380min)
find ${QUOTADIR} -name "notified-*" -type f -mmin +1380 -exec rm -f {} \;

for PAC in `pacs_by_type '*'`; do
	if [ ! -d /home/pacs/${PAC} ]; then
		continue
	fi
	
	pac_get ${PAC} || continue
    addr_get ${PAC_MEMBER}

	quota -g "${PAC}" | secat --user="${PAC}" --umask=022 \
	 --output="/home/pacs/${PAC}/var/quota"

	ShQuotaQuery --group -- "${PAC}" '*(XFS)/dev/sda2' || {
		echo "Quotas are not enabled for ${PAC}"
		continue
	}

	USED="$(ConvBlock2MB "${SHQUOTA_BLOCK_USAGE}")"
	SOFT="$(ConvBlock2MB "${SHQUOTA_BLOCK_SOFTLIMIT}")"
	HARD="$(ConvBlock2MB "${SHQUOTA_BLOCK_HARDLIMIT}")"
	
	let PERC=100*USED/SOFT
	let MAX=100*HARD/SOFT

	if [ "${SHQUOTA_BLOCK_USAGE}" -lt "${SHQUOTA_BLOCK_SOFTLIMIT}" ]; then
		continue
	else
		if [ "${SHQUOTA_BLOCK_GRACEPERIOD}" -gt "${SHQUOTA_QUERY_TIME}" ]; then
			let GRACE=SHQUOTA_BLOCK_GRACEPERIOD-SHQUOTA_QUERY_TIME
			GRACE="$(ConvSecond2Day "${GRACE}")"
		else
			GRACE="0"
		fi
	fi

	if [ $INTERACTIVE -eq 1 ]; then
		echo -n "$PAC uses $PERC% ($USED MB) of their quota ($SOFT MB), $MAX% ($HARD MB) is limit: "
	fi

	CC=
	GRACE_MSG="Die Kulanzfrist laeuft noch maximal $GRACE Tage. 
In dieser Zeit stehen temporaer bis zu $HARD MB ($MAX%) zur Verfuegung."
	if [ "$GRACE" -le 2 ]; then	
		CC=hostmaster@hostsharing.net
	fi

	if [ "$GRACE" = "0" ]; then
		#pac-lock -w -m -r "Speicherplatz ueberschritten" $PAC
		echo "" >/dev/null # nur Kommentar in then...fi ist ein Syntaxfehler
	fi

	if [ $PERC -ge 100 ]; then
		if [  ! -f ${QUOTADIR}/notified-$PAC ]; then

			SIG=`cat /etc/signature`
			mail-encoded <<EOF
To: $EMAIL_EXTERNAL,$PAC@$THIS_HIVE.hostsharing.net
CC: $EMAIL_HOSTMASTER
From: $EMAIL_HOSTMASTER
Reply-To: service@hostsharing.net
Subject: [HS: Alarm] Disk-Quota $PAC zu $PERC% ausgeschoepft
X-HS-Message: Diskquota
X-HS-Package: $PAC
X-HS-Quota-Pct: $PERC

Mit $USED MB ist die Disk-Quota des Paketes $PAC zu $PERC% ausgeschoepft. 
Der derzeit zum Paket gehoerende Speicher betraegt $SOFT MB.
$GRACE_MSG

Sollte binnen dieser Zeit das Paket nicht wieder im gebuchten Rahmen 
liegen, oder gar die o.g. maximale Grenze ueberschritten werden, ist 
ein weiteres Abspeichern von Daten nicht mehr moeglich.

Wir empfehlen dringend, entweder Daten zu loeschen oder Zusatz-Speicher 
nachzubestellen. Bestellungen an die Hostsharing e.G. bitte ggf. per
E-Mail an service@hostsharing.net richten.

Wir bitten um freundliche Beachtung!
	Die Hostsharing-Hostmaster

P.S. Diese Nachricht wurde automatisch erstellt. 
$SIG
EOF

			# immer gesetzt:
			export HS_PAC="$PAC"
			export HS_QUOTA_BLOCK_USED="$USED"
			export HS_QUOTA_BLOCK_PERC="$PERC"
			export HS_QUOTA_BLOCK_SOFT="$SOFT"
			export HS_QUOTA_BLOCK_HARD="$HARD"

			# nur gesetzt, wenn die Quota schon überschritten ist:
			export HS_QUOTA_BLOCK_GRACE_LEFT="$GRACE"

			handle-event pacquotawarning "$PAC"

			aTag="$(date +'%Y-%m-%d %H:%M') $PAC: quota warning mailed ($PERC%, ${GRACE}days)"
			echo "$aTag" >>${QUOTADIR}/notified-$PAC
			echo "$aTag" >>/var/log/hostsharing/quota.log
			if [ $INTERACTIVE -eq 1 ]; then
				echo "mailed"
			fi
		else
			if [ $INTERACTIVE -eq 1 ]; then
				echo "skipped"
			fi

		fi
	else
		if [ $PERC -lt 100 ]; then
			rm -f ${QUOTADIR}/notified-$PAC
		fi
		if [ $INTERACTIVE -eq 1 ]; then
			echo "ok"
		fi
	fi
done

exit 0
