Implemented cleanup system.

There is now a subcommand for purging old state dirs, suitable for calling
from cron.