#!/usr/bin/perl -w
use strict;
use warnings FATAL=>'all';

use File::Find;

my $HOME = $ENV{'HOME'};
my $PRODUCT = 'chromium';
my $PROFILE = 'Default';
my $SQLITE = 'sqlite3';
my $DUMP_COMMAND = "echo '.dump' | $SQLITE DATABASE | $SQLITE DATABASE-new";

find(\&rebuild, "$HOME/.config/$PRODUCT/$PROFILE");


sub rebuild {


    print "in: " . $File::Find::dir . "\n";
    if ($_ =~ m/(.*)-journal$/) {
	my $database = $1;
	$database =~ s/ /\\ /g;
	my $command = $DUMP_COMMAND;
	$command =~ s/DATABASE/$database/g;
	print "$command\n";
	system ("$command");
	rename $database,"$database.old";
	rename "$database-journal","$database-journal.old";
	rename "$database-new",$database;
    }

}
