#! /usr/local/bin/perl

$top_directory = '/home/toyoda/WWW/newscaster/newscaster';
$audio_directory = "$top_directory/audio";

open(SENTENCE_CONF, "$top_directory/sentence.conf")
    || die "Can't open sentence.conf\n";

$syll_kinds = <SENTENCE_CONF>;
chop $syll_kinds;

while($conf = <SENTENCE_CONF>) {
    chop $conf;
    push(@sentence_conf, $conf);
}

foreach $syll (split(/ /, $syll_kinds)) {
    open(FIND, "find $audio_directory -name $syll-* -print |")
	|| die "Couldn't run find: $!\n";

    local(@tmp_list);
    while($filename = <FIND>) {
	chop $filename;
	push(@tmp_list, $filename);
    }
    $syllables{$syll} = join(" ", @tmp_list, "");
}

srand;

for ($newsnum = 1; $newsnum <= 3; $newsnum++) {
    local(@sentence, @sylls, $conf_idx);
    push(@sentence, 'audioconvert', '-o', "$top_directory/../news$newsnum.au");

    $conf_idx = rand @sentence_conf;
    foreach $syll (split(/ /, $sentence_conf[$conf_idx])) {
	@sylls = split(/ /, $syllables{$syll});
	push(@sentence, splice(@sylls, rand @sylls, 1));
	$syllables{$syll} = join(" ", @sylls, "");
    }
    splice(@sentence_conf, $conf_idx, 1);

#    print join(",", @sentence_conf, "\n");
#    print join(" ", @sentence, "\n");
    system(@sentence);
}

print <<"END";
Content-type: text/html

<HTML>
<HEAD>
<TITLE>Insane News Caster</TITLE>
<H1>NHK(なんか，変で，狂ってる)ニュース放送</H1>
<BODY>

<HR>

<H2>
<P>
<A HREF="http://www.tkl.iis.u-tokyo.ac.jp/~toyoda/newscaster/news1.au">トップニュース</A>
<P>
<A HREF="http://www.tkl.iis.u-tokyo.ac.jp/~toyoda/newscaster/news2.au">次のニュース</A>
<P>
<A HREF="http://www.tkl.iis.u-tokyo.ac.jp/~toyoda/newscaster/news3.au">終りのニュース</A>
</H2>

<HR>

</BODY>
</HTML>
END
