#!/usr/bin/perl

# Usage:
#  ./me.pl article/*/*.static_html_files ... > RESULT

use strict;

use Jcode;
use FileHandle;
use Data::Dumper;

my @files;
# Extract Windows' wild cards
while ( my $i = shift @ARGV ) {
	push( @files, glob( $i ) );
}

sub EucSlurp {
	my $fn = shift @_;
	return do {
		local $/;
		my $fh = FileHandle->new( $fn, 'r' )
			or return; # エラー
		local $_ = <$fh>;
		s/\x0D\x0A|\x0D|\x0A/\n/g;
		jcode( $_ )->euc;
	};
}

select STDERR;
$| = 1;

my %entries;
foreach my $file ( @files ) {
	my $cnt = EucSlurp( $file )
		or do {
			warn "No such file: $file";
			next;
		};
	my $oldUrl = ( $cnt =~ m|<!-- XORIGINALFILENAME: (.*?) -->| )[0];
	if ( length( $oldUrl ) < 1 ) {
		warn "No XORIGINALFILENAME for $file";
		next;
	}
	$entries{ $oldUrl } = $file;
}
print "\n";

binmode( STDOUT );
print STDOUT <<"_EOF_";
my %entriesMaps = (
@{[ do {
		my $s;
		foreach my $k ( keys %entries ) {
			$s .= ( $s ne '' ) ? "\n" : '';
			$s .= "\t'$k' => '$entries{$k}',";
		}
		$s;
} ]}
);
_EOF_
