###########################################################################
#
# This script determines the free space on a given partition
# and reports results on stdout in the format that the XNJS expects.
#
# This script expects a single argument, which is a path on the partition
# to be checked.
#
# The format of the output is as follows:
#
#   Listing starts with the line:
#
#   START_DF
#
#   and ends with the line:
#
#   END_DF
#
#   The following values are reported (in bytes):
#
#    - TOTAL: The total space on the partition
#    - FREE: The free space on the partition
#    - USER: The user quota (optional)
#
#     Every line is terminated by \n
#
#
###########################################################################

use strict;

my $path = shift;

print "START_DF\n";

# execute the "df" command, requesting the result in bytes
my @df_data = qx(df -P -B 1 $path 2>&1 );

if ($?) {
    print "ERROR $df_data[0]\n";
}
else {
    my ( $filesystem, $total, $used, $avail, $percentUsed, $mount ) =
      split( /\s+/, $df_data[1] );
    print "TOTAL $total\n";
    print "FREE $avail\n";

    #
    # reporting the user quota is optional
    #
    #print "USER -1\n"
}
print "END_DF\n";

