#!/bin/bash
#
# This script checks the disk usage percent of the root filesystem. When the
# disk usage percent exceeds THRESHOLD, then deletes files in the 
# DOWNLOAD_DIR for every user.
#


THRESHOLD=50
DOWNLOAD_DIR='Baixades'

SCRIPT=`basename $0`

function log () {
        logger -t $SCRIPT $1
}

log "There is something to do?"

DISK_USAGE=`df -h | grep -E \/$ | sed "s/  */ /g" | cut -f5 -d' ' | cut -f1 -d'%'`
log "Disk usage: $DISK_USAGE% Threshold: $THRESHOLD%"

if [ $DISK_USAGE -ge $THRESHOLD ]
then
	log "Disk usage exceeds threshold. Deleting files in $DOWNLOAD_DIR for every user."	

	for user in `ls /home`
	do
		download_dir="/home/$user/$DOWNLOAD_DIR"
		if [ -d $download_dir ]
		then
			size=`du -hs $download_dir 2>/dev/null`
			log "removing $size"
			rm -rf $download_dir/*
			log "removed"
		fi
	done

	log "No more users"
	DISK_USAGE=`df -h | grep -E \/$ | sed "s/  */ /g" | cut -f5 -d' ' | cut -f1 -d'%'`
	log "Final disk usage: $DISK_USAGE%"

fi

log "There is nothing to do."

