#!/bin/bash
#################################################################################################
#                                                                                               #
#  Checks if disk usage is above X% and if so, shows a popup. This way you will always be       #
#  alerted before your system halts because of a full disk and won't boot again.                #
#  Needs to be started as root via /etc/rc.local                                                #
#                                                                                               #
#  Depends on: yad, df, sox                                                                     #
#                                                                                               #
#  Made by Koentje  (remon@cobrasoft.nl)                                                        #
#                                                                                 version 1.2   #
#################################################################################################
#
# Loop timer to check disk usage every x seconds.
timer="10"
#
#
# At what % of disk usage, action is taken?
percent="90"
#
# What to do when disk is at % level. Following actions are possible: shutdown/reboot/popup
action="popup"
#
# Only for shutdown or reboot, how many seconds before taking action
seconds="30"
#
#
# Alert sound file
sound="/usr/share/sounds/ubuntu/notifications/Amsterdam.ogg"
#
# Pitch sound
pitch="0"
#
# Speed sound
speed="1"
#
# Volume sound
volume="0.3"
#
#
###########################################################################################################
################################  After this line, editing is at own risk  ################################
###########################################################################################################
echo -e "\n  This script runs in a continues loop to check if a disk is $percent% full.\n"

while :
do
    usage=$(df --output=pcent / | tail -n1)
    usage=${usage:1:-1}

    if [[ $usage -lt $percent ]]; then
      sleep $timer
      continue
    fi


    play_sound () {
       play -q -v $volume $sound pitch $pitch speed $speed &
    }


    isactive=$(wmctrl -lp | grep 'Warning!' | awk '{print $3}')
    if [ "$isactive" != "" ]; then
      kill -n9 $isactive
    fi


    if [ "$action" = "popup" ]; then
        yad --text="\n<span size='xx-large'>   Root disk at $percent% full !</span>" --title="Warning!" --image="drive-harddisk" --center --fixed --on-top --width=550 --height=70 --button="OK" &
        play_sound
    elif [ "$action" = "shutdown" ]; then
        yad --text="\n<span size='xx-large'>   Root disk at $percent% full !</span>\n     System shutdown in $seconds seconds." --title="Warning!" --image="drive-harddisk" --center --fixed --on-top --width=550 --height=70 --button="OK" &
        play_sound
        sleep $seconds; sudo shutdown -P now
    elif [ "$action" = "reboot" ]; then
        yad --text="\n<span size='xx-large'>   Root disk at $percent% full !</span>\n     System reboot in $seconds seconds." --title="Warning!" --image="drive-harddisk" --center --fixed --on-top --width=550 --height=70 --button="OK" &
        play_sound
        sleep $seconds; sudo reboot
    fi

  sleep $timer
done
