#!/usr/bin/perl -w

my @footprint = (
    "eao_61-9820.1.fp", # only for indication lamps, possible TODO
    "eao_61-9821.1.fp",
    "eao_61-9823.fp",
);

my %data = ( # from eao_61.pl
    # the difference between theese with mult. type.no. is the contact plating, Ag, Ag+Au or Au
    # so we just do the first one in the schematic

   # Bauteilelayout Schaltbild Typ-Nr...
   5 => [ 20, "61-8101.02" ],
   6 => [ 21, "61-8102.02" ],
   7 => [  3, "61-8006.02" ],
   8 => [ 23, "61-8440.12", "61-8440.22" ],
   9 => [ 24, "61-8450.12", "61-8450.22" ],
  10 => [ 25, "61-8460.12", "61-8460.22" ],
  11 => [ 31, "61-8480.12", "61-8480.22" ],
  12 => [ 24, "61-8550.12", "61-8550.22", "61-8750.37" ],
  13 => [ 34, "61-8520.12", "61-8520.22", "61-8720.37" ],
  14 => [ 30, "61-8574.12", "61-8574.22" ],
  15 => [ 27, "61-8610.17", "61-8610.37" ],
  16 => [ 34, "61-8620.17", "61-8620.37" ],
  17 => [ 30, "61-8670.17", "61-8670.37" ],
  18 => [ 23, "61-8640.17", "61-8640.37" ],
  19 => [ 24, "61-8650.17", "61-8650.37" ],
  20 => [ 30, "61-8770.17", "61-8770.37" ],
  21 => [ 34, "61-8720.17" ],
  22 => [ 24, "61-8750.17" ],
  23 => [ 37, "61-8775.17", "61-8775.37" ],
  24 => [ 38, "61-8745.17", "61-8745.37" ],
  25 => [ 36, "61-8755.17", "61-8755.37" ],
  32 => [ 27, "61-8410.12", "61-8410.22" ],
  36 => [ 34, "61-8420.12", "61-8420.22" ],
  37 => [ 35, "61-8430.12", "61-8430.22" ],
  38 => [ 30, "61-8470.12", "61-8470.22" ],
  39 => [ 32, "61-8490.12", "61-8490.22" ],
  40 => [ 28, "61-8212.12", "61-8212.22" ],
);

my %pin = (
    # fp.no., pin labels from Bauteilelayout, page 53..58
    # diagramms is as show from th bottom,
    # pin numbering is from top rigth -> bottom 1-4, mid top-bot, 5,6, left top-bot 7,8
   5 => [ 1, qw/  -  - 1- 2+ X2+ X1-  -  - / ],
   6 => [ 1, qw/ 3+ 4- 1- 2+ X2+ X1-  -  - / ],
   7 => [ 1, qw/  -  -  -  - X2+ X1-  -  - / ],
   8 => [ 1, qw/  -  -  -  - X2+ X1- 11 12 / ],
   9 => [ 1, qw/ 21 22  -  - X2+ X1- 11 12 / ],
  10 => [ 1, qw/ 21 22 31 32 X2+ X1- 11 12 / ],
  11 => [ 1, qw/ 13 14 21 22 X2+ X1- 31 32 / ],
  12 => [ 1, qw/ 11 12 21 22 X2+ X1-  -  - / ],
  13 => [ 1, qw/ 13 14 23 24 X2+ X1-  -  - / ],
  14 => [ 1, qw/ 13 14 21 22 X2+ X1-  -  - / ],
  15 => [ 2, qw/  -  - X2+ X1- 13 14 / ],
  16 => [ 2, qw/ 23 24 X2+ X1- 13 14 / ],
  17 => [ 2, qw/ 21 22 X2+ X1- 13 14 / ],
  18 => [ 2, qw/  -  - X2+ X1- 11 12 / ],
  19 => [ 2, qw/ 21 22 X2+ X1- 11 12 / ],
  20 => [ 2, qw/ 21 22 X2+ X1- 13 14 / ],
  21 => [ 2, qw/ 23 24 X2+ X1- 13 14 / ],
  22 => [ 2, qw/ 21 22 X2+ X1- 11 12 / ],
  23 => [ 2, qw/ 21 22  -   -  13 14 / ],
  24 => [ 2, qw/  -  -  -   -  11 12 / ],
  25 => [ 2, qw/ 21 22  -   -  11 12 / ],
  32 => [ 1, qw/  -  -  -  - X2+ X1- 13 14 / ],
  36 => [ 1, qw/ 23 24  -  - X2+ X1- 13 14 / ],
  37 => [ 1, qw/ 23 24 33 34 X2+ X1- 13 14 / ],
  38 => [ 1, qw/ 21 22  -  - X2+ X1- 13 14 / ],
  39 => [ 1, qw/ 21 22 33 34 X2+ X1- 13 14 / ],
  40 => [ 1, qw/ 3+ 4- 1- 2+ X2+ X1- 13 14 / ],
);
for my $k (keys %pin) {
    my $v = $pin{$k};
    for (my $ix = 1; $ix < @$v; $ix++) {
	if ($$v[$ix] eq "-") { $$v[$ix] = "NC"; }
    }
}

my $layout;
my $schaltbild;
my $value;
my $footprint;
my @lbl;
my $file;
my $fh;

sub wr1() {
    my $str = "";

    if ($lbl[1] =~ m/^\d\+/) {
	$str .= "# add upper diode
L 850 1750 950 1750 3 0 1 0 -1 -1
L 900 1750 950 1850 3 0 1 0 -1 -1
L 950 1850 850 1850 3 0 1 0 -1 -1
L 850 1850 900 1750 3 0 1 0 -1 -1
L 750 1600 900 1600 3 0 1 0 -1 -1
L 900 2000 750 2000 3 0 1 0 -1 -1
L 900 1600 900 1750 3 0 1 0 -1 -1
L 900 1850 900 2000 3 0 1 0 -1 -1
";
    } elsif ($lbl[1] =~ m/^\d1/) {
	$str .= "# add upper NC switch
L 850 2000 850 1900 3 0 1 0 -1 -1
L 850 1700 850 1600 3 0 1 0 -1 -1
L 850 1900 950 1900 3 0 1 0 -1 -1
L 850 1700 950 1950 3 0 1 0 -1 -1
L 725 1600 850 1600 3 0 1 0 -1 -1
L 725 2000 850 2000 3 0 1 0 -1 -1
";
    } elsif ($lbl[1] =~ m/^\d3/) {
	$str .= "# add upper NO switch
L 750 2000 900 2000 3 0 1 0 -1 -1
L 750 1600 900 1600 3 0 1 0 -1 -1
L 900 2000 900 1900 3 0 1 0 -1 -1
L 900 1600 900 1700 3 0 1 0 -1 -1
L 900 1700 800 1900 3 0 1 0 -1 -1
";
    }

    if ($lbl[4] =~ m/^\d\+/) {
	$str .= "# add lower diode
L 950 1050 850 1050 3 0 1 0 -1 -1
L 900 1050 850 950 3 0 1 0 -1 -1
L 850 950 950 950 3 0 1 0 -1 -1
L 950 950 900 1050 3 0 1 0 -1 -1
L 750 800 900 800 3 0 1 0 -1 -1
L 900 1200 750 1200 3 0 1 0 -1 -1
L 900 800 900 950 3 0 1 0 -1 -1
L 900 1050 900 1200 3 0 1 0 -1 -1
";
    } elsif ($lbl[4] =~ m/^\d2/) {
	$str .= "# add lower NC switch
L 850 1200 850 1100 3 0 1 0 -1 -1
L 850 900 850 800 3 0 1 0 -1 -1
L 850 1100 950 1100 3 0 1 0 -1 -1
L 850 900 950 1150 3 0 1 0 -1 -1
L 725 800 850 800 3 0 1 0 -1 -1
L 725 1200 850 1200 3 0 1 0 -1 -1
";
    } elsif ($lbl[4] =~ m/^\d4/) {
	$str .= "# add lower NO switch
L 750 1200 900 1200 3 0 1 0 -1 -1
L 750 800 900 800 3 0 1 0 -1 -1
L 900 1200 900 1100 3 0 1 0 -1 -1
L 900 800 900 900 3 0 1 0 -1 -1
L 900 900 800 1100 3 0 1 0 -1 -1
";
    }

    if ($lbl[5] eq "X2+") {
	$str .= "# add lamp
L 1200 2000 1200 1500 3 0 1 0 -1 -1
L 1200 1300 1200 800 3 0 1 0 -1 -1
V 1200 1400 100 3 0 1 0 -1 -1 0 -1 -1 -1 -1 -1
L 1270 1330 1130 1470 3 0 1 0 -1 -1
L 1270 1470 1130 1330 3 0 1 0 -1 -1
";
    }

    if ($lbl[7] =~ m/^\d1/) {
	$str .= "# add right NC switch
L 1600 2000 1600 1500 3 0 1 0 -1 -1
L 1600 1300 1600 800 3 0 1 0 -1 -1
L 1600 1500 1700 1500 3 0 1 0 -1 -1
L 1600 1300 1700 1550 3 0 1 0 -1 -1
";
    } elsif ($lbl[7] =~ m/^\d3/) {
	$str .= "# add right NO switch
L 1600 2000 1600 1500 3 0 1 0 -1 -1
L 1600 800 1600 1300 3 0 1 0 -1 -1
L 1600 1300 1500 1500 3 0 1 0 -1 -1
";
    }

    print $fh "v 20210407 2
T 400 5061 5 10 0 0 0 0 1
author=Karl Hammar
T 400 4839 5 10 0 0 0 0 1
copyright=Karl Hammar
T 400 4617 5 10 0 0 0 0 1
dist-license=GPL
T 400 4395 5 10 0 0 0 0 1
use-license=unlimited
T 400 3951 5 10 0 0 0 0 1
device=Switch
T 400 3729 5 10 0 0 0 0 1
footprint=eao_61-9821.1.fp
T 400 3285 5 10 0 0 0 0 1
documentation=http://www.eao.com/global/de/Catalogues/PDF_Datei_mit_Zeichnungen/EAO_Baureihen_Empfehlung/EAO-Baureihen-61-Daten.pdf
T 2200 1617 9 10 1 1 0 4 1
refdes=X?
T 2200 1451 9 6 1 1 0 4 1
description=EAO series 61
T 2200 1262 9 10 1 1 0 4 1
value=$value
T 2200 1096 9 6 1 1 0 4 1
sublabel=layout $layout schaltbild $schaltbild
L 400 2400 400 400 3 40 2 0 -1 -1
L 400 400 2800 400 3 40 2 0 -1 -1
L 2800 400 2800 2400 3 40 2 0 -1 -1
L 2800 2400 400 2400 3 40 2 0 -1 -1
L 400 2400 400 2400 3 40 2 0 -1 -1
P 400 2000 0 2000 1 0 1
{
T 499 2000 9 10 1 1 0 0 1
pinlabel=$lbl[1]
T 325 2085 5 8 1 1 0 7 1
pinnumber=1
T 325 2085 5 8 0 1 0 7 1
pinseq=1
T 275 1940 5 5 0 1 0 7 1
pintype=pas
}
P 400 1600 0 1600 1 0 1
{
T 499 1600 9 10 1 1 0 0 1
pinlabel=$lbl[2]
T 325 1685 5 8 1 1 0 7 1
pinnumber=2
T 325 1685 5 8 0 1 0 7 1
pinseq=2
T 275 1540 5 5 0 1 0 7 1
pintype=pas
}
P 400 1200 0 1200 1 0 1
{
T 499 1200 9 10 1 1 0 0 1
pinlabel=$lbl[3]
T 325 1285 5 8 1 1 0 7 1
pinnumber=3
T 325 1285 5 8 0 1 0 7 1
pinseq=3
T 275 1140 5 5 0 1 0 7 1
pintype=pas
}
P 400 800 0 800 1 0 1
{
T 499 800 9 10 1 1 0 0 1
pinlabel=$lbl[4]
T 325 885 5 8 1 1 0 7 1
pinnumber=4
T 325 885 5 8 0 1 0 7 1
pinseq=4
T 275 740 5 5 0 1 0 7 1
pintype=pas
}
P 1200 2400 1200 2800 1 0 1
{
T 1200 2300 9 10 1 1 0 5 1
pinlabel=$lbl[5]
T 1180 2600 5 8 1 1 0 7 1
pinnumber=5
T 1180 2600 5 8 0 1 0 7 1
pinseq=5
T 1370 2600 5 5 0 1 0 7 1
pintype=pas
}
P 1200 400 1200 0 1 0 1
{
T 1200 499 9 10 1 1 0 3 1
pinlabel=$lbl[6]
T 1180 200 5 8 1 1 0 7 1
pinnumber=6
T 1180 200 5 8 0 1 0 7 1
pinseq=6
T 1370 200 5 5 0 1 0 7 1
pintype=pas
}
P 1600 2400 1600 2800 1 0 1
{
T 1600 2300 9 10 1 1 0 5 1
pinlabel=$lbl[7]
T 1580 2600 5 8 1 1 0 7 1
pinnumber=7
T 1580 2600 5 8 0 1 0 7 1
pinseq=7
T 1770 2600 5 5 0 1 0 7 1
pintype=pas
}
P 1600 400 1600 0 1 0 1
{
T 1600 499 9 10 1 1 0 3 1
pinlabel=$lbl[8]
T 1580 200 5 8 1 1 0 7 1
pinnumber=8
T 1580 200 5 8 0 1 0 7 1
pinseq=8
T 1770 200 5 5 0 1 0 7 1
pintype=pas
}
$str";
}


sub wr2($) {
    my $schaltbild = shift;
    my $str = "";

    if ($lbl[1] =~ m/^\d1/) {
	$str .= "# add left NC switch
L 400 2000 400 1500 3 0 1 0 -1 -1
L 400 1500 500 1500 3 0 1 0 -1 -1
L 400 1300 500 1550 3 0 1 0 -1 -1
L 400 1300 400 800 3 0 1 0 -1 -1
";
	if ($schaltbild == 37) {
	    $str .=
"V 525 1375 50 3 0 1 0 -1 -1 0 -1 -1 -1 -1 -1
L 490 1375 560 1375 3 0 2 0 -1 -1
L 560 1375 525 1368 3 0 2 0 -1 -1
L 560 1375 525 1382 3 0 2 0 -1 -1
"
	}
    } elsif ($lbl[1] =~ m/^\d3/) {
	$str .= "# add left NO switch
L 400 2000 400 1500 3 0 1 0 -1 -1
L 400 800 400 1300 3 0 1 0 -1 -1
L 400 1300 300 1500 3 0 1 0 -1 -1
";
    }

    if ($lbl[3] eq "X2+") {
	$str .=
"L 800 2000 800 1500 3 0 1 0 -1 -1
L 800 1300 800 800 3 0 1 0 -1 -1
V 800 1400 100 3 0 1 0 -1 -1 0 -1 -1 -1 -1 -1
L 870 1330 730 1470 3 0 1 0 -1 -1
L 870 1470 730 1330 3 0 1 0 -1 -1
";
    }

    if ($lbl[5] =~ m/^\d1/) {
	$str .= "# add right NC switch
L 1200 2000 1200 1500 3 0 1 0 -1 -1
L 1200 1500 1300 1500 3 0 1 0 -1 -1
L 1200 1300 1300 1550 3 0 1 0 -1 -1
L 1200 1300 1200 800 3 0 1 0 -1 -1
";
	if ($schaltbild == 36 || $schaltbild == 38) {
	    $str .=
"V 1325 1375 50 3 0 1 0 -1 -1 0 -1 -1 -1 -1 -1
L 1290 1375 1360 1375 3 0 2 0 -1 -1
L 1360 1375 1325 1368 3 0 2 0 -1 -1
L 1360 1375 1325 1382 3 0 2 0 -1 -1
";
	}
    } elsif ($lbl[5] =~ m/^\d3/) {
	$str .= "# add right NO switch
L 1200 2000 1200 1500 3 0 1 0 -1 -1
L 1200 800 1200 1300 3 0 1 0 -1 -1
L 1200 1300 1100 1500 3 0 1 0 -1 -1
";
    }

    print $fh "v 20210407 2
T 0 5061 5 10 0 0 0 0 1
author=Karl Hammar
T 0 4839 5 10 0 0 0 0 1
copyright=Karl Hammar
T 0 4617 5 10 0 0 0 0 1
dist-license=GPL
T 0 4395 5 10 0 0 0 0 1
use-license=unlimited
T 0 3951 5 10 0 0 0 0 1
device=Switch
T 0 3729 5 10 0 0 0 0 1
footprint=eao_61-9823.fp
T 0 3285 5 10 0 0 0 0 1
documentation=http://www.eao.com/global/de/Catalogues/PDF_Datei_mit_Zeichnungen/EAO_Baureihen_Empfehlung/EAO-Baureihen-61-Daten.pdf
T 1800 1617 9 10 1 1 0 4 1
refdes=X?
T 1800 1451 9 6 1 1 0 4 1
description=EAO series 61
T 1800 1262 9 10 1 1 0 4 1
value=$value
T 1800 1096 9 6 1 1 0 4 1
sublabel=layout $layout schaltbild $schaltbild
L 0 2400 0 400 3 40 2 0 -1 -1
L 0 400 2400 400 3 40 2 0 -1 -1
L 2400 400 2400 2400 3 40 2 0 -1 -1
L 2400 2400 0 2400 3 40 2 0 -1 -1
L 0 2400 0 2400 3 40 2 0 -1 -1
P 400 2400 400 2800 1 0 1
{
T 400 2300 9 10 1 1 0 5 1
pinlabel=$lbl[1]
T 380 2600 5 8 1 1 0 7 1
pinnumber=1
T 380 2600 5 8 0 1 0 7 1
pinseq=1
T 570 2600 5 5 0 1 0 7 1
pintype=pas
}
P 400 400 400 0 1 0 1
{
T 400 499 9 10 1 1 0 3 1
pinlabel=$lbl[2]
T 380 200 5 8 1 1 0 7 1
pinnumber=2
T 380 200 5 8 0 1 0 7 1
pinseq=2
T 570 200 5 5 0 1 0 7 1
pintype=pas
}
P 800 2400 800 2800 1 0 1
{
T 800 2300 9 10 1 1 0 5 1
pinlabel=$lbl[3]
T 780 2600 5 8 1 1 0 7 1
pinnumber=3
T 780 2600 5 8 0 1 0 7 1
pinseq=3
T 970 2600 5 5 0 1 0 7 1
pintype=pas
}
P 800 400 800 0 1 0 1
{
T 800 499 9 10 1 1 0 3 1
pinlabel=$lbl[4]
T 780 200 5 8 1 1 0 7 1
pinnumber=4
T 780 200 5 8 0 1 0 7 1
pinseq=4
T 970 200 5 5 0 1 0 7 1
pintype=pas
}
P 1200 2400 1200 2800 1 0 1
{
T 1200 2300 9 10 1 1 0 5 1
pinlabel=$lbl[5]
T 1180 2600 5 8 1 1 0 7 1
pinnumber=5
T 1180 2600 5 8 0 1 0 7 1
pinseq=5
T 1370 2600 5 5 0 1 0 7 1
pintype=pas
}
P 1200 400 1200 0 1 0 1
{
T 1200 499 9 10 1 1 0 3 1
pinlabel=$lbl[6]
T 1180 200 5 8 1 1 0 7 1
pinnumber=6
T 1180 200 5 8 0 1 0 7 1
pinseq=6
T 1370 200 5 5 0 1 0 7 1
pintype=pas
}
$str";
}

for $key (sort { $a <=> $b } keys %data) {
    $layout = $key;
    $schaltbild = ${$data{$key}}[0];
    $value      = ${$data{$key}}[1]; # just use the first typ.nr. as attr. value
    @lbl = @{$pin{$key}};
    my $fp = $lbl[0];
    $footprint = $footprint[$fp];

    $file = "eao_$value.sym";
    open($fh, ">", $file) || die("cannot open <$file>");

    if    ($fp == 1) { wr1(); }
    elsif ($fp == 2) { wr2($schaltbild); }

    close($fh);
}
