#!/usr/bin/perl
#
# shingetsu-convert.pl - Convert SAKU cache ShinGETsu dat.
# Copyright (C) 2006  mutsu (mutumin03@hotmail.com).
#
# usage:
#   % cd shingetsu-dir
#   % shingetsu-convert.pl /path/to/saku/cache
#

$usage = "usage: cd shingetsu-dir; saku-convert.py /path/to/saku/cache\n";

$cachedir = $ARGV[0];
$dat = "\.dat";

if (!(defined $cachedir)){ print $usage; exit 1; }

mkdir "dat";
opendir DIR, $cachedir;
while($dir = readdir(DIR)){
    if($dir !~ /^thread_/){ next; }
    push @cachedir, $dir;
}
close DIR;

foreach(@cachedir){
    $cachefile = $_; 
    print "converting $cachefile\n";
    opendir DIR, "$cachedir/$_/record";
    @records = ();
    while($record = readdir(DIR)){
        push @records, $record;
    }
    close DIR;
    @sort_records = sort @records;
    @datfiles = ();
    foreach(@sort_records){
        open IN, "$cachedir/$cachefile/record/$_" or die;
	@file = <IN>;
        close IN;
        push @datfiles, @file;
    }
    open OUT, ">dat/$cachefile$dat";
    print OUT @datfiles;
    close OUT;
}
