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

	if {[$ts flag "Shrunk"]} {
		::auto::log "  $file - already shrunk." 2
		return
	}

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

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

	if {[catch {
		set perc [exec /mod/bin/stripts -aq [file rootname $file]]
	    } msg]} {
		::auto::log "          Error: $msg" 0
		return
	}
	if {[string match {*%} $perc]} {
		set perc [string range $perc 0 end-1]
	} else {
		set perc 0
	}

	if {$perc == 0} {
		::auto::log "  $file - already shrunk." 2
		$ts set_shrunk
		return
	}

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

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

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

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

::auto::register shrink 400

