#!/mod/bin/jimsh

source /mod/webif/lib/setup
require queue.class system.class

proc fixflags {dir} {
	foreach entry [readdir -nocomplain $dir] {
		if {[file isdirectory "$dir/$entry"]} {
			fixflags "$dir/$entry"
			continue
		}
		if {[string match {.auto*r} $entry]} {
			puts "Updating flag $dir/$entry"
			file rename "$dir/$entry" \
			    "$dir/[string range $entry 0 end-1]R"
		}
	}
}

set queries {
	2 { {alter table queue add column last integer default 0} }
}

set latest 3
set cur [queue version]

if {$cur == $latest} {
	puts "Queue database is up-to-date."
	exit
}

if {$cur < 3} {
	puts "Upgrading directory flags..."
	fixflags [system mediaroot]
}

if {$cur < 2} {
	puts "Clearing queue due to version change..."
	queue dbhandle -close
	file delete /mod/etc/queue.db
	queue dbhandle
	set cur $latest
}

set db [queue dbhandle]
loop i $cur $latest {
	puts "Upgrading queue database to version $($i + 1)"
	foreach q $queries($i) {
		catch { $db query $q }
		$db query {
			update config
			set val = %s
			where var = 'version'
		} $($i + 1)
	}
}

