#!/usr/bin/perl -w

use lib '.';
use strict;
use Stat;

my $file = "";
my $ofile = "";
my $line = "";

my %data = ();
my %file = ();
my %opt;

sub opt() {
    while (@ARGV) {
	if ($ARGV[0] =~ m/=/) {
	    my $opt = shift @ARGV;
	    my ($k,$v) = split(/=/, $opt, 2);
	    $opt{$k} = $v;
	} else {
	    last;
	}
    }
}

sub round($) {
    my $val = shift;

    int($val * 10 + 0.5)/10;
}

sub tomm($) {
    my $str = shift;
    my $val = 0;
    #print "$str\n";

    if ($str =~ m/^(\d+(\.\d*)?)(mm|um|nm|mil)?$/) {
	$val = $1;
	my $unit = $3;

	if (defined($unit)) {
	    if ($unit eq "mm") {
	    } elsif ($unit eq "um") {
		$val /= 1000;
	    } elsif ($unit eq "nm") {
		$val /= 1000*1000;
	    } elsif ($unit eq "mil") {
		$val *= 25.4 / 1000;
	    }
	} else {
	    $val *= 25.4 / 1000 / 100;
	}
	#print "$str <$val>\n";
    } else {
	print "Err: <$str>\n";
	$val = -1;
    }
    $val;
}

sub run($$) {
    my $d = shift;
    my $str = shift;
    my @fld = ();
    #print "<$str>\n\n";
    my $flags = "";
    my $annulus = 0;

    my $dr = 0;
    my $th = 0;
    if ($str =~ m/^([^"]*)"([^"]*)"\s*"([^"]*)"(.*)$/) {
	@fld = split(" ", $1);
	if (@fld == 6) {
	} elsif (@fld == 4) {
	} else {
	    print "Err A: $file <$str>\n";
	    return;
	}
	$th = $fld[2];
	$dr = $fld[$#fld];
	push @fld, $2, $3;
	my @end = split(" ", $4);
	if (@end != 1) {
	    print "Err B: $file <$str> <", join(" ", @end), ">\n";
	    return;
	}
	$flags = $end[0];
    } elsif ($str =~ m/^([^"]*)"([^"]*)"(.*)$/) {
	my ($fa, $fb, $fc) = ($1, $2, $3);
	if ($d eq '[') {
	    print "Err C: $file <$str>\n";
	    return;
	}
	@fld = split(" ", $fa);
	if (@fld == 3) {
	    $th = $fld[2];
	    $dr = 0;
	} elsif (@fld == 4) {
	    $th = $fld[2];
	    $dr = $fld[$#fld];
	} else {
	    print "Err D: $file <$str>\n";
	    return;
	}
	push @fld, $fb;
	my @end = split(" ", $fc);
	if (@end != 1) {
	    print "Err E: $file <$str> <$fc>\n";
	    return;
	}
	$flags = $end[0];
    } else {
	print "Err F: $file <$str>\n";
	return;
    }


    if ($d eq '(') {
	$th *= 100; # TODO: guessing this is in mil instead of mil/100
    }
    $th = tomm($th);
    $dr = tomm($dr);
    $annulus = ($th - $dr)/2;
    $th = round($th);
    $dr = round($dr);
    $annulus = int( 20 * $annulus + 0.5 ) / 20;

    if ($dr < 0.21) {
	return;
	#print "$file: <$str>\n";
    }

    if ( $file ne $ofile ) {
	#print "$file\n";
    }
    if ($dr > 500) { print "$file\n"; }

    if ($annulus < 0.1) { return; }
    if ($flags =~ m/(0x0*8($|,)|hole)/) { return; }

    if (!defined($data{$dr})) {
	$data{$dr} = {};
    }
    $data{$dr}{$th}++;

    if (!defined($file{$dr})) {
	$file{$dr} = {};
    }
    $file{$dr}{$file}++;
}

sub nsort() {
    $a <=> $b
}

sub read_data() {
    while (<>) {
	$line = $_;
	chomp $line;
	s/\#.*$//;
	tr/ \t\r\n/ /s;
	s/^ //;
	s/ $//;

	next if ($_ eq "");
	next if (m/^(Attribute|Element)/);
	if (m/^\.\//) {
	    $file = $_;
	    next;
	}

	if (!m/^Pin/) {
	    print "Err: $_\n";
	    next;
	}

	s/ ?([[\]()]) ?/$1/g;
	#s/ \"\"\"/ \"\" \"/;
	#s/0\"\"/0 \"\"/;
	#s/^([^"]*\S)\"/$1 \"/;

	my $str = $_;
	while ($str && $str ne "") {
	    if ($str =~ m/^Pin ?\[([^\]]*)\](.*)$/) {
		run("[", $1);
		$str = $2;
		#print "$1 : $2\n";
		#print $_, "\n"; 
	    } elsif ($str =~ m/^Pin ?\(([^)]*)\)(.*)$/) {
		#print "$file\n<$1><$2>\n";
		run("(", $1);
		$str = $2;
		#$str = "";
	    } else {
		#print "$file: <$str>\n";
		# found ./phil_taylor/footprints/TXFMR__triad-utrad-5447.fp
		$str = "";
	    }
	}

	if ($file ne $ofile) {
	    $ofile = $file;
	    #print "$file\n";
	}
    }
}

sub postproc() {
    #printf "Data from cvs.gedasymbols.org (thickness is the cupper outer dia of the pin annulus)
    #Drill: total count for this drill dia
    #  thickness  count
    #
    #";

    my $fh;
    my %cnt;
    if (open($fh, ">", "file.txt")) {
	for my $dr ( sort nsort keys %file ) {
	    my $v = $file{$dr};
	    my $cnt = 0;
	    for my $fn (sort keys %$v) {
		$cnt += $$v{$fn};
	    }
	    print $fh "$dr: $cnt\n";
	    for my $fn (sort keys %$v) {
		$cnt = $$v{$fn};
		printf $fh " %4d %s\n", $cnt, $fn;
	    }
	}
	close($fh);
    }

    if (open($fh, ">", "data.txt")) {
	for my $dr ( sort nsort keys %data ) {
	    my $v = $data{$dr};
	    my $cnt = 0;
	    for my $th (sort nsort keys %$v) {
		$cnt += $$v{$th};
	    }
	    $cnt{$dr} = $cnt;
	    
	    #print "$dr: $cnt\n";
	    for my $th (sort nsort keys %$v) {
		$cnt = $$v{$th};
		printf $fh "%4.1f %4.1f %5d\n", $dr, $th, $cnt;
	    }
	    #print "\n";
	} 
	close($fh);
    }

    if (open($fh, ">", "cnt.txt")) {
	print $fh "\n";
	for my $dr ( sort nsort keys %cnt ) {
	    my $v = $cnt{$dr};
	    printf $fh "%4.1f %5d\n", $dr, $v;
	}
	close($fh);
    }
}

sub main() {
    opt();
    read_data();
    postproc();
}

main();
