#!/usr/local/bin/ruby

require 'filesystem'

def pct(total, avail)
  (total == 0) ? 0 : (100.0 * avail / total).round
end

FMT = "%-10s %8d %8d  %3d   %8d %8d  %3d"
HDR = "Mount            KB    Avail %free     Files    Avail %free"

def df(mnt)
  puts HDR if mnt.size > 0
  mnt.sort.each do |m|
    s = FileSystem.stat m
    bpct = pct(s.blocks, s.blocks_avail)
    fpct = pct(s.files,  s.files_avail)
    puts format(FMT, m, s.blocks, s.blocks_avail, bpct, 
                        s.files,  s.files_avail,  fpct)
  end
end

def mounted
  mts = []
  FileSystem.mounts.each {|m| mts << m.mount if m.device =~ %r(/dev/)}
  mts
end

df ((ARGV.size == 0) ? mounted : ARGV)

