#! /usr/bin/perl

use strict;

my @flags_str2int = (
  { str => 'hole', int => 0x00000008 },
  { str => 'clearpoly', int => 0x00000010 },
  { str => 'clearline', int => 0x00000020 },
  { str => 'auto', int => 0x00000080 },
  { str => 'onsolder', int => 0x00000080 },
  { str => 'square', int => 0x00000100 },
  { str => 'rubberend', int => 0x00000200 },
  { str => 'edge2', int => 0x00004000 },
  { str => 'thermal(1)', int => 0x00030000 },
);

sub flag_str2int {
  my $strs_str = shift;
  my $int = 0;
  my @strs = split( /[ ,]/, $strs_str );
  foreach my $str (@strs) {
    foreach my $i_str2int (@flags_str2int) {
      my $i_str = $i_str2int->{str};
      my $i_int = $i_str2int->{int};
      if( $str eq $i_str ) {
        $int |= $i_int;
      }
    }
  }
  return $int;
}

my $section = '';
my $line;
while( $line = <> ) {
  chomp $line;
  chomp $line;

  # global section
  if( $section eq '' ) {

    # add indicator to pcb release comment
    if( $line =~ /^# release: (.*)$/ ) {
      print( "# release: $1 - pcb_old2new.pl\n" );

    # reformat Grid
    } elsif( $line =~ /^Grid\[([0-9.]+) ([0-9]+ [0-9]+ [0-9]+)\]$/ ) {
      printf( "Grid[%.8f %s]\n", $1, $2 );

    # remove PolyAera
    } elsif( $line =~ /^PolyArea\[[0-9.]*\]$/ ) {

    # shorten 6 value DRC to 4 values
    } elsif( $line =~ /^DRC\[([0-9]+ [0-9]+ [0-9]+ [0-9]+) [0-9]+ [0-9]+\]$/ ) {
      print( "DRC[$1]\n" );

    # convert Symbol to old format
    } elsif( $line =~ /^Symbol\(('.') ([0-9]+)\)$/ ) {
      printf( "Symbol[%s %u]\n", $1, $2 * 100 );
      $section = 'Symbol';

    # convert Element to old format
    } elsif( $line =~ /^Element\["([^"]*)" ("[^"]*" "[^"]*" "[^"]*" [-0-9 ]*) "([^"]*)"\]$/ ) {
      printf( "Element[0x%08X %s 0x%08X]\n", flag_str2int( $1 ), $2, flag_str2int( $3 ) );

    # convert Pad to old format
    } elsif( $line =~ /^\tPad\[([-0-9 ]* "[^"]*" "[^"]*") "([^"]*)"\]$/ ) {
      printf( "\tPad[%s 0x%08X]\n", $1, flag_str2int( $2 ) );

    # convert Pin to old format
    } elsif( $line =~ /^\tPin\[([-0-9 ]* "[^"]*" "[^"]*") "([^"]*)"\]$/ ) {
      printf( "\tPin[%s 0x%08X]\n", $1, flag_str2int( $2 ) | 0x03000001 );

    # convert Via to old format
    } elsif( $line =~ /^Via\[([-0-9 ]* "[^"]*") "([^"]*)"\]$/ ) {
      printf( "Via[%s 0x%08X]\n", $1, flag_str2int( $2 ) | 0x03000002 );

    # convert Line to old format
    } elsif( $line =~ /^\tLine\[([-0-9 ]+) "([^"]*)"\]$/ ) {
      printf( "\tLine[%s 0x%08X]\n", $1, flag_str2int( $2 ) );

    # convert Polygon to old format
    } elsif( $line =~ /^\tPolygon\("([^"]*)"\)$/ ) {
      printf( "\tPolygon(0x%08X)\n", flag_str2int( $1 ) );

    # convert Text to old format
    } elsif( $line =~ /^\tText\[([-0-9 ]* "[^"]*") "([^"]*)"\]$/ ) {
      printf( "\tText[%s 0x%08X]\n", $1, flag_str2int( $2 ) );

    # keep unknown lines
    } else {
      print( "$line\n" );
    }

  # Symbol section
  } elsif( $section eq 'Symbol' ) {

    # end of Symbol
    if( $line eq ')' ) {
      print( "$line\n" );
      $section = '';

    # convert SymbolLine to old format
    } elsif( $line =~ /^\tSymbolLine\(([-0-9]+) ([-0-9]+) ([-0-9]+) ([-0-9]+) ([-0-9]+)\)$/ ) {
      printf( "\tSymbolLine[%u %u %u %u %u]\n", $1 * 100, $2 * 100, $3 * 100, $4 * 100, $5 * 100 );

    # keep unknown lines
    } else {
      print( "$line\n" );
    }

  # keep lines in unknown sections
  } else {
    print( "$line\n" );
  }

} # while( $line = <> )