#!/usr/bin/perl

# mkpctcdsl.pl - Compute pct-cdsl.tsv for redistricting

# Reads rg_pctblk#####.tsv.gz and creates distpct-redist#####.tsv

use lib ("$ENV{SMARTVOTER_PL}");
require "countyabb.pl";

use Getopt::Std; getopts('htv'); 

if ($opt_h) { print <<"END"; exit(0); }
Usage: 
    Options:
END

foreach $f (<rg_pctblk*.tsv.gz>) {
    ($fips,$cn)=$f=~/pctblk(\d\d(\d\d\d))/;
    $stcty = lc(countyfips($fips));
    undef(%blk2dist);
    undef(%pct2dist);
    $f2 = "$ENV{TIGER_DB}/ca/tiger10/$cn/redist.blk.gz";
    if (!-s $f2) {
        print "Fetching $f2\n";
        mkdir($dir="$ENV{TIGER_DB}/ca/tiger10/$cn");
        system("rsync -au sv:gis/ca/tiger10/$cn/redist.blk.gz $dir/");
    }
    print "Reading $ENV{TIGER_DB}/ca/tiger10/$cn/redist.blk.gz\n";
    open(IN,"gunzip -c $ENV{TIGER_DB}/ca/tiger10/$cn/redist.blk.gz|");
    $_ = <IN>;
    while(<IN>) {
       ($blk,$dist)=/(\d+)\t(.*\S)/;
       $blk2dist{$blk}=$dist;
    }
    close(IN);
    open(IN,"gunzip -c $f|");
    $_ = <IN>;
    while(<IN>) {
       s/\s+$//;
       ($county,$rgprec,$block,$pct_block,$reg_rgblk,$reg_block)=
           split(/\t/,$_);
       ($blk)=$block=~/06\d\d\d(\d+)/;
       $dist = $blk2dist{$blk};
       (!$blk || !$dist) && die("Can't find block $block");
       if ($dist ne ($odist=$pct2dist{$rgprec}||=$dist)) {
           if ($odist!~/$dist/) {
               $odist .= ";$dist";
           }
           #print "Duplicate: $rgprec $pct2dist{$rgprec}!=$dist\n";
       }
    }
    close(IN);
    undef(%distpct);
    foreach $pct (sort(keys(%pct2dist))) {
        $dist = $pct2dist{$pct};
        if ($dist=~/;/) {
            undef(%cd);
            undef(%sd);
            undef(%ad);
            foreach $_ (split(/;/,$dist)) {
                ($cd,$sd,$ad)=split(/\t/,$dist);
                $cd{$cd}++;
                $sd{$sd}++;
                $ad{$ad}++;
            }
            formdlist("CD",keys(%cd));
            formdlist("SD",keys(%sd));
            formdlist("AD",keys(%ad));
        } else {
            ($cd,$sd,$ad)=split(/\t/,$dist);
            formdlist("CD",$cd);
            formdlist("SD",$sd);
            formdlist("AD",$ad);
        }
    }
    #print "Creating $ENV{SMARTVOTER_HOME}/2012/06/05/$stcty/dat/redistpct.tsv.gz\n";
    open(OUTN,">$ENV{SMARTVOTER_HOME}/2012/06/05/$stcty/dat/redistname.tsv");
    print OUTN "District_Code\tDistrict_Name\n";
    open(OUT,"|gzip -c >$ENV{SMARTVOTER_HOME}/2012/06/05/$stcty/dat/redistpct.tsv.gz");
    print OUT "District_Code\tPrecincts\n";
    foreach $d (sort(keys(%distpct))) {
        ($p,$n)=$d=~/(\w\w)(\d+)/;
        $name = sprintf("%s District %2d",
          $p eq "CD" ? "California Congressional" :
          $p eq "SD" ? "California State Senate" :
                       "California State Assembly",$n);
        $pcts=$distpct{$d};
        $pcts=~s/ $//;
        print OUTN "X$d\t$name\n";
        print OUT "X$d\t$pcts\n";
    }
    #last;
}

sub formdlist {
    local($pref)=shift(@_);
    local(@l)=sort(@_);
    foreach $l (@l) {
        $distpct{sprintf("%s%03d",$pref,$l)} .= $#l>0 ? "?pct " : "$pct ";
    }
}