#!/usr/bin/perl -w
# vim: set sw=4 ts=4 si et:
# Copyright: GPL, Author: Guido Socher
# $Revision: $, last changed: $Date: $
#
use strict;
use vars qw($opt_a $opt_o $opt_g $opt_i $opt_h);
use Getopt::Std;
# global vars:
my $textdb="textdb.txt";
my $issuedb="issuedb.txt";
my $issuedbsh="issuedbshort.txt";
# prototypes:
sub help();
#
getopts("h")||die "ERROR: No such option. -h for help\n";
help() if ($opt_h);
my @data;
my %ih;
my $forfastsearch;
open(DB,"<$textdb")||die "ERROR: can not read $textdb\n";
while(<DB>){
    next unless(/^art/);
    $forfastsearch=substr($_,0,100); # accellerate the split
    @data=split(/~/,$forfastsearch);
    next unless($data[5]);
    $ih{$data[4]}->{'longname'}=$data[3];
    push(@{$ih{$data[4]}->{'articles'}},$data[1]);
}
close DB;
open(OUT,">$issuedb")||die "ERROR: can not write $issuedb\n";
print "writing $issuedb ...\n";
print OUT "# generated from $textdb\n";
for my $numissue (sort keys %ih){
    print OUT "\n";
    for my $art (sort @{$ih{$numissue}->{'articles'}}){
        print OUT "$art~$numissue~".$ih{$numissue}->{'longname'}."~\n";
    }
}
close OUT;
#
open(OUT,">$issuedbsh")||die "ERROR: can not write $issuedbsh\n";
print "writing $issuedbsh ...\n";
print OUT "# generated from $textdb\n";
for my $numissue (sort keys %ih){
    print OUT "$numissue~".$ih{$numissue}->{'longname'}."~\n";
}
close OUT;
#
sub help(){
    print "textdb2issue_pl -- sort articles by issue
USAGE: textdb2issue_pl [-h]

OPTIONS: -h this help

This program reads textdb.txt and writes issuedb.txt abd issuedbshort.txt
\n";
exit;
}
__END__ 

