#!/usr/bin/perl -w

# ./gen_mil.pl description=MIL-DTL-26482H serie=pt07 name=mil_amphenol_pt07
# ./gen_mil.pl description=MIL-DTL-26482H name=mil_26482

my $str =
"A B: 8-2
A B C: 12-3 8-98 8-33 8-3
A B C D: 14-4 8-4
A B C D E: 14-5 14-22
A B C D E F: 10-6 10-98
A B C D E F G: 22-96
A B C D E F G H: 16-8 18-8 16-95 12-8 18-88
A B C D E F G H J: 14-9
A B C D E F G H J K: 12-10
A B C D E F G H J K L: 18-11
A B C D E F G H J K L M: 22-12 14-12
A B C D E F G H J K L M N: 18-85
A B C D E F G H J K L M N P: 16-14
A B C D E F G H J K L M N P Q R S T U V W X Y Z a b c d e f g: 24-31
A B C D E F G H J K L M N P R: 14-15 20-90
A B C D E F G H J K L M N P R S: 20-16
A B C D E F G H J K L M N P R S T U: 14-18
A B C D E F G H J K L M N P R S T U V: 14-19 24-19 22-19
A B C D E F G H J K L M N P R S T U V W X: 22-21
A B C D E F G H J K L M N P R S T U V W X Y Z: 16-23 16-99
A B C D E F G H J K L M N P R S T U V W X Y Z a: 20-24
A B C D E F G H J K L M N P R S T U V W X Y Z a b c: 16-26
A B C D E F G H J K L M N P R S T U V W X Y Z a b c d: 24-27 20-27
A B C D E F G H J K L M N P R S T U V W X Y Z a b c d e f g: 18-30
A B C D E F G H J K L M N P R S T U V W X Y Z a b c d e f g h i: 22-95
A B C D E F G H J K L M N P R S T U V W X Y Z a b c d e f g h i j k m n p q r: 20-39
A B C D E F G H J K L M N P R S T U V W X Y Z a b c d e f g h i j k m n p q r s t: 20-41 22-41
A B C D E F G H J K L M N P R S T U V W X Y Z a b c d e f g h i j k m n p q r s t u v w x y z AA BB CC DD EE FF GG HH: 22-55
A B C D E F G H J K L M N P R S T U V W X Y Z a b c d e f g h i j k m n p q r s t u v w x y z AA BB CC DD EE FF GG HH JJ KK LL MM NN PP: 24-61
A B C D E F G H J K L M N P R S T U V W X Y Z a b c d e f g h j: 18-32 22-32
A B C D E F G H J K L M N P R S T U V W X Y Z a b c d e f g h j k l: 22-34
A B C D E: 10-5 # pt07
";

my @pt07 = qw/ 8-3 8-98 10-5 10-6 12-3 12-10 14-18 14-19 16-26 18-11 18-32 20-27 20-41 22-55 24-31 24-61 /;
my %pt07;
for my $ins (@pt07) { $pt07{$ins} = 1; }

sub wnum( $ ) {
    my $insert = shift;

    my @fld = split(/-/, $insert);
    sprintf("%02d-%02d", $fld[0], $fld[1]);
}
sub asort {
    wnum($a) cmp wnum($b);
}

my %option;
for (@ARGV) {
    if (m/^([a-z]+)=(.*)$/) {
	$option{$1} = $2;
    } else {
	$option{$_} = 1;
    }
}

# description=MIL-STD-1669
# description=MIL-DTL-26482H

if (!defined($option{description})) { $option{description} = "MIL-STD-1669"; }
if (!defined($option{serie}      )) { $option{serie}       = "MS3112E"     ; }
if (!defined($option{name}       )) { $option{name}        = "mil_std1669" ; }

my @line = split(/\n/, $str);
for (@line) {
    next unless (m/^([^:]+): ([^#]+)( # (.+))?$/);
    #my ($lbl, $str) = split(/ ?: ?/, $_, 2);
    my $lbl = $1;
    my $str = $2;
    my $cnd = $4;

    $cnd = "" unless (defined($cnd));
    my @sz = sort  { asort } split(/ /, $str);
    for my $sz (@sz) {
	my ($shell, $arrangement) = split(/-/, $sz);
	my $part_num;

	if ($cnd && $cnd ne $option{serie}) { next; }
	if ($option{serie} =~ m/^MS311\d.$/) {
	    $part_num = $option{serie} . $sz . "P";
	} elsif ($option{serie} eq "pt07") {
	    if (!defined($pt07{$sz})) { next; }
	    $part_num = "58-53372" . ($shell-6)/2 . "-" . $arrangement . "P";
	}

	for my $P (("P", "S")) {
	    my $file = sprintf("%s_%s.pins", $option{name}, wnum($sz));
	    next unless open(FH, ">", $file);
	    print FH
"!> .sym * pins conn
! arg w:0.85

# change \"P_\" in footprint and in value to match pin/socket + rotation

device=Connector
footprint=Amphenol_pt07_${sz}P_a.fp
documentation=https://landandmaritimeapps.dla.mil/Downloads/MilSpec/Docs/MIL-STD-1669/std1669.pdf
documentation=http://everyspec.com/  MIL-DTL-26482H_AMENDMENT-2.pdf
documentation=http://amphenol.co.kr/board/product/011-ads-PCB-Cylindrical-Connector.pdf
documentation=http://www.amphenol-aerospace.com/pdf/catalogs/12-070_26482_series_1.pdf

refdes=X?
description=$option{description}
value=$part_num
sublabel=insert ${sz}P

$sz
";
	    my @lbl = split(/ /, $lbl);
	    for my $ltr (@lbl) {
		print FH " $ltr pas  $ltr\n";
	    }
	    close(FH);
	}
    }
}
