#!/usr/bin/perl
use lib '../lib';
use Include;
use Lock;
my %GROUPV	= file2hash('data/group.txt');
my @GROUP = grep { $_ if(-f "group/$_.html") } sort {$a <=> $b} keys %GROUPV;

print STDERR join "\n", map{"$_:$GROUPV{$_}"}keys %GROUPV;
print STDERR scalar @GROUP;
print "Content-Type: text/html\n\n";
if(!@GROUP){
	print include('format/index.txt',{
		FIRST	=> include('format/first.txt',{
			ROW		=> 1,
			GROUP	=> 'ただいま登録されている商品はありません。'
		})
	});
	exit;
}
my $FIRST = shift @GROUP;
print include('format/index.txt',{
	FIRST	=> include('format/first.txt',{
		ROW		=> @GROUP+1,
		GROUP	=> map{ "<A href=group/$_.html>$GROUPV{$_}</A>" } $FIRST
	}),
	LIST	=> join("\n", map{ 
		include('format/list.txt',{
			GROUP	=> map{ "<A href=group/$_.html>$GROUPV{$_}</A>" } $_
		})
	} @GROUP)
});

sub file2hash {
	my $file = shift;
	return undef	if($file eq '' || !-f $file);
	my %hash;
	Open(IN,$file);
	while(<IN>){s/\r\n|\r/\n/g;chomp;$hash{$.} = $_;}
	close(IN);
	return %hash;
}
