#! /usr/bin/env perl

#================================================================
# mkftart
# Generate an article whose body text is made with `fortune'.
#================================================================


# set initialization variables
$language = 'en';
@categories = ( 'rave', 'agenda', 'suggestion', 'question', 'answer', 'confession', 'joke' );
$author = 'fortune';
$dictfile = '/usr/share/dict/words';
$fortune = 'fortune -l';


# get the category
$category = $categories[rand(int(@categories))];


# get the date
$mytime = time() - rand(3600*24*800);
($sec , $min, $hour, $mday, $mon, $year, $wday, $isdst) = gmtime($mytime);
$mdate = sprintf('%04d-%02d-%02d %02d:%02d:%02d',
  $year + 1900, $mon + 1, $mday, $hour, $min, $sec);
$mytime -= rand(3600*24*200);
($sec , $min, $hour, $mday, $mon, $year, $wday, $isdst) = gmtime($mytime);
$cdate = sprintf('%04d-%02d-%02d %02d:%02d:%02d',
  $year + 1900, $mon + 1, $mday, $hour, $min, $sec);


# get the random words
open(IN, "$dictfile") or die "$dictfile could not be opened";
$tnum = 0;
while($line = <IN>){
    chomp($line);
    $titles[$tnum++] = $line;
}
close(IN);
$title = $titles[rand($tnum)];
while(int(rand(2)) > 0){
    $title = sprintf("%s %s", $title, $titles[rand($tnum)]);
}
$title =~ s/&/\&amp;/g;
$title =~ s/</\&lt;/g;
$title =~ s/>/\&gt;/g;


# output the result
open(IN, "$fortune|") or die "$fortune failed";
print(
<<__EOF
<?xml version="1.0" encoding="UTF-8"?>
<article>
<head>
<flags></flags>
<language>$language</language>
<category>$category</category>
<subject>$title</subject>
<author>$author</author>
<cdate>$cdate</cdate>
<mdate>$mdate</mdate>
</head>
<body>
__EOF
);
printf("<asis>");
while($line = <IN>){
    $line =~ s/&/\&amp;/g;
    $line =~ s/</\&lt;/g;
    $line =~ s/>/\&gt;/g;
    printf("%s", $line);
}
printf("</asis>\n");
print(
<<__EOF
</body>
<tail>
</tail>
</article>
__EOF
);
close(IN);


# exit normally
exit(0);



# END OF FILE
