#!/bin/sh  
# \
exec oagtclsh "$0" "$@"

# 
# Tcl script to accumulate disk usage info from 'du' into SDDS file.
# Intended to be run as a cron job on a daily or hourly basis.
#

#
# $Log: not supported by cvs2svn $
# Revision 1.2  1997/01/22 23:55:56  borland
# Added a kludge that helps deal with system malfunctions.
#
# Revision 1.1  1996/08/07 18:39:05  borland
# First version.
#
#

if [llength $argv]!=1 {
    puts stderr "Usage: sddsDiskUsage <dirName>"
    exit
}

cd [lindex $argv 0]

set timeList [split [exec date +%y:%j:%H:%M] :]
set year [lindex $timeList 0]
if $year<96 {
        set year [expr $year+2000]
} else {
        set year [expr $year+1900]
}

scan [lindex $timeList 1] %ld jDay
scan [lindex $timeList 2] %ld hour
scan [lindex $timeList 3] %ld min
set hour [expr $hour+$min/60.0]

set timeNow [exec timeconvert -breakdown=year=$year,julian=$jDay,hour=$hour]

if ![file exists diskUsage.sdds] {
    set fid [open diskUsage.sdds w]
    puts $fid "SDDS1\n&parameter name = Time  type = double &end"
    puts $fid "&column name = SpaceUsed  type = long &end"
    puts $fid "&column name = DirectoryName  type = string &end"
    puts $fid "&data mode = ascii no_row_counts=1 &end"
    flush $fid
    close $fid
}

set tmpfile /tmp/diskUsage.tmp[exec date +%y%j%H%M%S]
set first 1
foreach file [glob -nocomplain *] {
    if [string compare [file type $file] directory]==0 {
        if $first {
            exec echo "$timeNow kludge" >> diskUsage.sdds
        }
        exec du -s $file >> $tmpfile
        set first 0
    }
}
if !$first {
    exec echo "" >> $tmpfile
    exec cat $tmpfile >> diskUsage.sdds
}
exec rm $tmpfile
