#!/usr/bin/perl -w

use strict;
my $str = '';
#my $opt = "\"square,onsolder\"";
my $opt = "\"square\"";

sub mk_mm($) {
  my $val = shift;
  sprintf "%.3fmm", $val;
}

sub mk_element {
  my $str = shift;
  my $file = shift;
  #my $name = shift;

  my $a = shift;
  my $b = shift;
  my $c = shift;
  my $d = shift;

  my $clearance = "0.5mm";
  my $maskgap = 0.1;
  my $linewidth = 0.32;
  if ($c < 1) {
    $maskgap = $c / 5;
    $linewidth = $c / 2.5;
    $clearance = "${c}mm";
  }
  if ($d < ($b+$maskgap)) { $d = $b + 2*$maskgap; }

  if (!open(FH, ">$file")) {
    print "Cannot open/create file $file\n";
    return;
  }
  print FH $str;
  print FH "\n";
  print FH "Element [\"\" \"\" \"??\" \"\"  0 0 -0.7mm -0.8mm 0 100 \"\"]\n";
  print FH "(\n";

  if ($a < $b) {
    my $x = mk_mm($a/2 + $c/2);
    my $y = mk_mm($b/2 - $a/2);
    my $mask = mk_mm($a + 2*$maskgap);

    print FH "  Pad [ -$x -$y   -$x $y   ${a}mm $clearance $mask \"1\" \"1\" $opt ]\n";
    print FH "  Pad [  $x -$y    $x $y   ${a}mm $clearance $mask \"2\" \"2\" $opt ]\n";
    print FH "\n";
  } else {
    my $x1 = mk_mm($c/2 + $a - $b/2);
    my $x2 = mk_mm($c/2 + $b/2);
    my $mask = mk_mm($b + 2*$maskgap);

    print FH "  Pad [ -$x1 0   -$x2 0   ${b}mm $clearance $mask \"1\" \"1\" $opt ]\n";
    print FH "  Pad [  $x2 0    $x1 0   ${b}mm $clearance $mask \"2\" \"2\" $opt ]\n";
    print FH "\n";
 }
  {
    my $x = mk_mm($c/2 + $a + $linewidth/2 + $maskgap);
    my $y = $d/2 + $linewidth/2;

    my $X = mk_mm($c/2 + $linewidth/2 - $maskgap);
    my $Y = $b/2 + $linewidth/2 + $maskgap;
    my $lw = "${linewidth}mm";

    if ($y > $Y) {
      $y = mk_mm($y);
      $Y = mk_mm($Y);
      print FH "  ElementLine [ -$x  $y    $x  $y   ${linewidth}mm ]\n";
      print FH "  ElementLine [  $x  $y    $x -$y   ${linewidth}mm ]\n";
      print FH "  ElementLine [  $x -$y   -$x -$y   ${linewidth}mm ]\n";
      print FH "  ElementLine [ -$x -$y   -$x  $y   ${linewidth}mm ]\n";
      print FH "\n";
      print FH "  ElementLine [ -$x  $Y   -$X  $Y   ${linewidth}mm ]\n";
      print FH "  ElementLine [ -$X  $Y   -$X  $y   ${linewidth}mm ]\n";
      print FH "\n";
      print FH "  ElementLine [ -$x -$Y   -$X -$Y   ${linewidth}mm ]\n";
      print FH "  ElementLine [ -$X -$Y   -$X -$y   ${linewidth}mm ]\n";
    } else {
      $y = mk_mm($y);
      $Y = mk_mm($Y);
      print FH "  ElementLine [ -$x  $y    $x  $y   ${linewidth}mm ]\n";
      print FH "  ElementLine [  $x  $y    $x -$y   ${linewidth}mm ]\n";
      print FH "  ElementLine [  $x -$y   -$x -$y   ${linewidth}mm ]\n";
      print FH "  ElementLine [ -$x -$y   -$x  $y   ${linewidth}mm ]\n";
    }
  }
  print FH ")\n";
  close FH;
}

while (<DATA>) {
  last if (m/^__END__/);
  if (m/^#/) { $str .= $_; }
  elsif (m/^$/) {
    $str = '';
    next;
  } else {
    my @fld = split;
    if (@fld == 5) {
      mk_element($str, @fld);
    } else {
      print "wrong number of fields: $_";
    }
  }
}

# #-lines are written out as is
# filename pad_width pad_height pad_separation component_width
__DATA__

__END__

# http://www.nxp.com/acrobat/datasheets/1PS76SB10_2.pdf
sod323_nxp_a.fp 1.3 0.7 1.0 1.3

# Murata chip inductor, LQH32PN_NO Series (1210 Size)
# http://www.farnell.com/datasheets/1668059.pdf
murata_1210.fp 1.8 2.5 1.1 2.5

# Murata chip inductor, LQH44P Series (1515 Size)
# http://www.farnell.com/datasheets/490895.pdf
#murata_1515.fp 1.7 3.5 0.8 4.0
murata_1515.fp 2.5 3.5 0.8 4.0

# ST SMAJ SMA6J
# http://www.st.com/stonline/products/literature/ds/5544/smaj.pdf
# http://www1.elfa.se/data1/wwwroot/assets/datasheets/uhSTM-SMA6J_data_e.pdf
sma_st.fp 1.4 1.64 2.63 2.6

# ST SMBJ SM6T
# http://www.st.com/stonline/products/literature/ds/5616/smbj.pdf
# http://www.st.com/stonline/products/literature/ds/3082/sm6t.pdf
# http://www1.elfa.se/data1/wwwroot/assets/datasheets/xbSM6T_data_e.pdf
smb_st.fp 1.62 2.18 2.6 3.6

# ST SMCJ
# http://www.st.com/stonline/products/literature/ds/5617/smcj.pdf
smc_st.fp 1.54 3.14 5.11 5.9

# Vishay SMA6J5.0A .. SMA6J28A
# http://www1.elfa.se/data1/wwwroot/assets/datasheets/yvVishay-SMA6J_e.pdf
sma_vishay.fp 1.52 1.68 1.88 2.65

# Vishay SMCJ5.0 .. SMCJ188CA
# http://www1.elfa.se/data1/wwwroot/assets/datasheets/xpVishay_Schutzdiode-1500W_EN.pdf
smc_vishay.fp 1.52 3.2 4.69 5.9

# Vishay D../CRCW.... e3
# http://www1.elfa.se/data1/wwwroot/assets/datasheets/flD-CRCW-e3_data_en.pdf
0201_reflow.fp 0.28 0.43 0.23 0.3
0402_reflow.fp 0.4 0.6 0.5 0.5
0603_reflow.fp 0.5 0.9 1.0 0.85
0805_reflow.fp 0.7 1.3 1.2 1.25
1206_reflow.fp 0.9 1.7 2.0 1.6
1210_reflow.fp 0.9 2.5 2.0 2.5
1218_reflow.fp 1.05 4.9 1.9 4.6
2010_reflow.fp 1.0 2.5 3.9 2.5
2512_reflow.fp 1.0 3.2 5.2 3.15
0603_wave.fp 0.9 0.9 1.0 0.85
0805_wave.fp 0.9 1.3 1.3 1.25
1206_wave.fp 1.1 1.7 2.3 1.6
1210_wave.fp 1.1 2.5 2.2 2.5
1218_wave.fp 1.25 4.8 1.9 4.6
2010_wave.fp 1.2 2.5 3.9 2.5
2512_wave.fp 1.2 3.2 5.2 3.15

# http://catalog.tycoelectronics.com/catalog/common/images/PartImages/chip_res_padsz.jpg
0402_tyco.fp 0.6 0.6 0.5 0.6
0603_tyco.fp 1.0 1.0 0.6 1.0
0805_tyco.fp 1.2 1.3 1.0 1.3
1206_tyco.fp 1.2 1.8 2.2 1.8
1210_tyco.fp 1.2 2.8 2.1 2.8
2010_tyco.fp 1.5 3.0 3.8 3.0
2512_tyco.fp 1.5 3.0 5.0 3.0

# http://www.farnell.com/datasheets/321153.pdf
# http://www.ramitechnology.com/products/spec/crystal/crystal-as-smd.pdf
crystal_hc49_ramitech_as.fp 5.5 2.0 4.0 4.8

# http://www.abracon.com/Resonators/abls-2-3.pdf
crystal_hc49_abracon_abls.fp 5.6 2.1 3.9 4.8

# http://www.cooperbussmann.com/pdf/20b1cc7d-ec0a-4ad3-a489-44761d3b3843.pdf 
up0.4c.fp 1.4 2.9 4.06 4.45

#  http://www1.elfa.se/data1/wwwroot/assets/datasheets/07018583.pdf
q_mini_melf.fp 1.5 1.8 2.0 2.4

# my own findings
0805_hand.fp 1.1 1.5 1.0 1.25

# http://www.farnell.com/datasheets/32258.pdf
sub_sma_a.fp 1.3 1.4 2.0 1.9

# smc
smc_a.fp 2.2 3.6 4.8 5.8

# smb
smb_a.fp 1.9 2.6 2.6 3.7

# sma
sma_a.fp 1.7 2.0 2.7 5.3

# 1608 / 0603 condenser/resistor
m1608_a.fp 1.1 1.2 0.5 0.8

# 2012 / 0805 condenser/resistor
m2012_a.fp 1.3 1.6 0.6 1.2

# 3216 / 1206 condenser/resistor
m3216_a.fp 1.3 2.0 1.8 1.6
