
proc ::decrypt::ts {ts} {
	set file [$ts get file]

	if {![$ts flag "ODEncrypted"]} {
		::auto::log "  $file - Already decrypted." 2
		return
	}

	if {[queue check $ts decrypt]} {
		::auto::log "  $file - Already queued." 2
		return
	}

	if {[::auto::inuse $ts]} {
		::auto::log "  $file - In use." 2
		return
	}

	# Check that the file is not already decrypted by analysing it.
	set anencd [exec /mod/bin/stripts -qE [file rootname $file]]
	if {$anencd != "1"} {
		::auto::log "  $file - already decrypted but the HMT flag is wrong." 0
		# Fix...
		exec /mod/webif/lib/bin/fixencflags $file
		return
	}

	# Enqueue file
	queue insert $ts decrypt
	::auto::log "  $file - Queued for decryption." 0
}

proc ::decrypt::directory {dir} {
	::auto::direntries $dir ::decrypt::ts
}

proc ::decrypt::rundir {dir} {
	::auto::flagscan $dir decrypt ::decrypt::directory 0
}

proc ::decrypt::run {} {
	::auto::flagscan $::auto::root decrypt ::decrypt::directory
}

::auto::register decrypt 600

