#!/usr/bin/perl
#   dfgraf [delay [dir [scale]]]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# This program produces an ASCII "graph" of the free space on the  filesystem #
# that  contains  the named directory, with one line produced each n seconds. #
# The lines consist of a hyphen for each <n>K of free space, where <n> is the #
# scaling  factor in the 3rd arg.  It might take a wide window; if your xterm #
# has the "unreadable" 1x2 font, it is ideal for displaying the  graph.   The #
# default  delay  between  lines is 10 seconds (plus whatever Unix adds); the #
# default directory is the current directory. Added goodie:  "tick" marks are #
# added  at  the  right,  with  distinctive  marks on the 1-minute, 5-minute, #
# 10-minute, hour and day lines.                                              #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$| = 1;		# Flush after every output.
$l8 = 'xxxxxxxx';
$l9 = 'xxxxxxxxx';
$lX = $l9 . 'X';
$lY = $lX . $lX;
$lV = 'X' . $l8 . 'X' . $lY . $lY;
$graf  = $lV;
$delay = $ARGV[0] || 10;
$sf    = $ARGV[1] || 100;	# Produces 1-Mbyte "columns".
$dir   = $ARGV[2] || '.';
($pseko,$pmino,$phoro,$ptago) = gmtime(time);
while (1) {
	($nseko,$nmino,$nhoro,$ntago) = gmtime(time);
    $flag = ($ntago != $ptago) ? '   XXXXXXXXXXXXXXXX' # day.
         : ($nhoro != $phoro) ? '   X_X^X_X^X_X'       # hour.
         : ($nmino != $pmino)
            ? ((($nmino % 10) == 0) ? '   x x x x'     # 10-minute
            :  (($nmino %  5) == 0) ? '   x x'         #  5-minute
            : '   x')                                  #  1-minute
         : '';
	@df = split(/\s+/,`df $dir |tail -1`);
	$kb = $df[3];	# Free space in Kbytes.
	$sz = $kb / $sf;
	$graf = $graf . $graf while (length($graf) < $sz);
	$l = substr($graf,0,$sz);
	print "\n", $l, $flag;
	sleep $delay;
	$pseko = $nseko;
	$pmino = $nmino;
	$phoro = $nhoro;
	$ptago = $ntago;
}
