#! /usr/bin/perl

use strict;
use warnings;

if (@ARGV < 1) {
  print STDERR "usage: $0 <angle in deg (cw)>\n";
  exit 2;
}
my $angle = shift(@ARGV);
my $angle_rad = $angle * 3.14159265 / 180.0;
my $angle_cos = cos($angle_rad);
my $angle_sin = sin($angle_rad);

sub round
{
  my ($val) = @_;
  return $val < 0 ? int($val - 0.5) : int($val + 0.5);
}

sub rotate
{
  my ($x, $y) = @_;

  my $xx = $angle_cos * $x - $angle_sin * $y;
  my $yy = $angle_sin * $x + $angle_cos * $y;

  return (round($xx), round($yy));
}

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

  if ($line =~ /^\s+Pad\s*\[([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+"([^"]*)"\s+"([^"]*)"\s+("[^"]*"|0x[0-9A-Fa-f]+)\]\s*$/) {
    my ($x1, $y1, $x2, $y2, $width, $clear, $mask, $name, $no, $flags) =
       ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);
    my ($xx1, $yy1) = rotate($x1, $y1);
    my ($xx2, $yy2) = rotate($x2, $y2);
    $line = "\tPad[$xx1 $yy1 $xx2 $yy2 $width $clear $mask \"$name\" \"$no\" $flags]";
  }

  elsif ($line =~ /^\s+Pin\s*\[([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+"([^"]*)"\s+"([^"]*)"\s+("[^"]*"|0x[0-9A-Fa-f]+)\]\s*$/) {
    my ($x, $y, $diam, $clear, $mask, $drill, $name, $no, $flags) =
       ($1, $2, $3, $4, $5, $6, $7, $8, $9);
    my ($xx, $yy) = rotate($x, $y);
    $line = "\tPin[$xx $yy $diam $clear $mask $drill \"$name\" \"$no\" $flags]";
  }

  elsif ($line =~ /^\s+ElementLine\s*\[([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\]\s*$/) {
    my ($x1, $y1, $x2, $y2, $width) = ($1, $2, $3, $4, $5);
    my ($xx1, $yy1) = rotate($x1, $y1);
    my ($xx2, $yy2) = rotate($x2, $y2);
    $line = "\tElementLine[$xx1 $yy1 $xx2 $yy2 $width]";
  }

  elsif ($line =~ /^\s+ElementArc\s*\[([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\s+([0-9-]+)\]\s*$/) {
    my ($x, $y, $rx, $ry, $start, $delta, $width) = ($1, $2, $3, $4, $5, $6, $7);
    my ($xx, $yy) = rotate($x, $y);
    if ($rx != $ry) {
      print STDERR "WARNING: ElementArc with rx=$rx != ry=$ry\n";
    }
    my $start_rot = round($start - $angle);
    my $rr = round(($rx + $ry) / 2.0);
    $line = "\tElementArc[$xx $yy $rr $rr $start_rot $delta $width]";
  }

  print $line . "\n";
}