#!/mod/bin/jimsh

source /mod/webif/lib/setup
require settings.class lock

set s [settings]
set logsize [$s logsize]
set logkeep [$s logkeep]
set logage [$s logage]

defer {catch {release_lock webif_autolog}}
if {![acquire_lock webif_autolog]} {
	acquire_lock webif_autolog 10 1
	puts "Could not acquire exclusive lock, terminating."
	exit
}

puts "Rotating at $logsize, keeping $logkeep, max age $logage"

set logdir "/mod/tmp"
set baselogs {}

puts "\n-- Rotate\n"

set tstamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
foreach log [glob -nocomplain -tail -directory $logdir *.log] {

	puts "\[$log\]"

	# Check for old-style rotated log.
	if {[string match {*_old.log} $log]} {
		set new "[string range $log 0 end-8].$tstamp.log"
		puts "    -> $new"
		file rename "$logdir/$log" "$logdir/$new"
		continue
	}

	# Check for already rotated log.
	if {[regexp -- {\.[0-9]{14}\.log$} $log]} {
		puts "    already rotated."
		if {$logage > 0} {
			set days $(
			    ([clock seconds] - [file mtime $logdir/$log])
			    / 86400)
			if {$days > $logage} {
				file tdelete "$logdir/$log"
				puts "    Deleting old log $log (age=$days)"
			}
		}
		continue
	}

	ladd baselogs $log

	if {[file size "$logdir/$log"] > $logsize} {
		set new "[file rootname $log].$tstamp.log"
		puts "    -> $new"
		file rename "$logdir/$log" "$logdir/$new"
	}
}

puts "\n-- Purge\n"

proc logsort {a b} {
	global logdir
	return [expr {[file mtime "$logdir/$a"] - [file mtime "$logdir/$b"]}]
}

foreach log $baselogs {
	# Count the number of rotated logs

	set old [glob -nocomplain -tail -directory $logdir \
	    "[string range $log 0 end-4].*.log"]
	set num [llength $old]
	
	puts "\[$log\] - $num"

	if {$num <= $logkeep} continue

	lmap i [lrange [lsort -command logsort $old] 0 end-$logkeep] {
		puts "    Deleting old log $i"
		file tdelete "$logdir/$i"
	}
}

