#!/usr/bin/perl

### convert from new style records to old style records
###
### Old Style Record: 
### 393499||104||"104-HR-3414"|3414|"HR"|||||||||||1||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||105|" A bill to amend the Balanced Budget and Emergency Deficit Control Act of 1985 to provide for a sequestration of all budgetary accounts for fiscal year 1997 (except Social Security, Federal retirement, and interest on the debt) equal to 5 percent of the OMB baseline."|||
### The text column is 92, major topic code is 17, and minor is 91.  Column three is the congress number.
###
### New First record: chamber (1)    billno (1)    title (XXXXXX)  congress (80) subtopic (107) major (1)
### New Second recor: chamber (1)    billno (11)  title (XXXXX) congress (80) subtopic (1609) major (16)


use strict;
use warnings;

use Getopt::Long;
use Pod::Usage;

my ( $help, $man );
my $verbose = 0;
my $in_file;
my $out_file;

GetOptions(
           'help|?' => \$help,
           man      => \$man,
           'verbose+' => \$verbose,
           'in-file=s' => \$in_file
  ) or pod2usage(2);
pod2usage(1) if ($help);
pod2usage( -exitstatus => 0, -verbose => 2 ) if ($man);
pod2usage("-in-file required!\n") unless($in_file);

open(my $infh, "<$in_file") or die "Couldn't open file: $!\n";

my $chamber = 0;
my $billno = 1;
#my $title = 2;
my $congress = 3;
#my $subtopic = 4;
#my $major = 5;
my $major = 0;
my $subtopic = 0;
my $title = 1;

my $expected_columns=92;
my $text_column=92;
my $majtop_column=17;
my $subtop_column=91;
my $delimeter = '\|';


while(<$infh>) {
    #print "Old Line: $_";
    chomp($_);
    my @line = split(/\|/);
 
    print "000000||";
    #print $line[$congress];
    print "00";
    print "||\"";
    #print $line[$congress];
    print "00";
    print "-";
    #print "HR" if ($line[$chamber] =~ m/1/);
    #print "SN" if ($line[$chamber] =~ m/2/);
    print "UN";
    print "-";
    #print $line[$billno];
    print "000";
    print "\"|";
    #print $line[$billno];
    print "id";
    print "\|\"";
    #print "HR" if ($line[$chamber] =~ m/1/);
    #print "SN" if ($line[$chamber] =~ m/2/);
    print "UN";
    print "\"|||||||||||";
    print int($line[$major]/100);
    print "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||";
    print $line[$subtopic];
    print "\|\"";
    print $line[$title];
    print "\"|||";
    print "\n";
}

close($infh);

