#!/usr/bin/perl -w

use strict;

sub Usage($@) {
    my $num = shift;
    print(@_);
    exit($num);
}
my $file = shift;
my $ang  = shift;
$ang = 45 unless ($ang);

Usage(0, "fp_rotate <file> [ang]\n")  unless ($file);
#Usage(1, "no input file specified\n") if (!defined($file));
#Usage(1, "no rotation angle specified\n") if (!defined($ang));

my $pi = 2*atan2(1,0);
my $rad = $pi*$ang/180;
my $ca = cos($rad);
my $sa = sin($rad);
#printf("%7.5f %5.3f %5.3f\n", $pi, $ca, $sa);

sub rot($$) {
    my $x = shift;
    my $y = shift;

    my $X = $x * $ca + $y * $sa;
    my $Y = - $x * $sa + $y * $ca;
    ( $X, $Y );
}

my $new = $file;
$new =~ s/\.fp$//;
$new .= "." . $ang . ".fp";
#print("<$new>\n");

my $fi;
my $fo;
open($fi, $file) || die("cannot open input file");
open($fo, ">", $new ) || die("cannot open output file");

while(<$fi>) {
    my @fld = split;
    if (@fld) {
	#print "$fld[0]\n";
	if (m/(^[ \t]*ElementArc[ \t]*[[(][ \t]*)([-+]?\d*(\.\d*))((mm|mil)?[ \t]+)([-+]?\d*(\.\d*))((mm|mil)?[ \t]+)(\S+[ \t]+\S+[ \t]+)(\d+)([ \t]+.*)/) {
	    my $pre = $1;
	    my $x = $2;
	    my $xd = $3;
	    my $mx = $4;
	    my $y = $6;
	    my $yd = $7;
	    my $my = $8;
	    my $aa = $10;
	    my $a1 = $11;
	    my $rest = $12;

	    my $xlen = length($xd) - 1;
	    my $ylen = length($yd) - 1;
	    #printf("$pre%.${xlen}f$mx%.${ylen}f$my$aa$a1$rest\n", $x, $y);
	    my ($X, $Y) = rot($x, $y);
	    $a1 += $ang;
	    $_ = sprintf("$pre%.${xlen}f$mx%.${ylen}f$my$aa$a1$rest\n", $X, $Y);
	} elsif (m/(^[ \t]*ElementLine[ \t]*[[(][ \t]*)([-+]?\d*(\.\d*))((mm|mil)?[ \t]+)([-+]?\d*(\.\d*))((mm|mil)?[ \t]+)([-+]?\d*(\.\d*))((mm|mil)?[ \t]+)([-+]?\d*(\.\d*))((mm|mil)?[ \t]+)([ \t].*)/) {
	    my $pre = $1;
	    my $x1 = $2;
	    my $x1d = $3;
	    my $m1x = $4;
	    my $y1 = $6;
	    my $y1d = $7;
	    my $m1y = $8;
	    my $x2 = $10;
	    my $x2d = $11;
	    my $m2x = $12;
	    my $y2 = $14;
	    my $y2d = $15;
	    my $m2y = $16;
	    my $rest = $18;

	    my $x1len = length($x1d) - 1;
	    my $y1len = length($y1d) - 1;
	    my $x2len = length($x2d) - 1;
	    my $y2len = length($y2d) - 1;
	    #print;
	    #printf("$pre%.${x1len}f$m1x%.${y1len}f$m1y%.${x2len}f$m2x%.${y2len}f$m2y$rest\n", $x1, $y1, $x2, $y2);
	    my ($X1, $Y1) = rot($x1, $y1);
	    my ($X2, $Y2) = rot($x2, $y2);
	    $_ = sprintf("$pre%.${x1len}f$m1x%.${y1len}f$m1y%.${x2len}f$m2x%.${y2len}f$m2y$rest\n", $X1, $Y1, $X2, $Y2);
	} elsif (m/(^[ \t]*Pin[ \t]*\[[ \t]*)([-+]?\d*(\.\d*))((mm|mil)?[ \t]+)([-+]?\d*(\.\d*))((mm|mil)?[ \t]+)(.*)/) {
	    my $pre = $1;
	    my $x = $2;
	    my $xd = $3;
	    my $mx = $4;
	    my $y = $6;
	    my $yd = $7;
	    my $my = $8;
	    my $rest = $10;

	    my $xlen = length($xd) - 1;
	    my $ylen = length($yd) - 1;
	    #print;
	    #printf("$pre%.${xlen}f$mx%.${ylen}f$my$rest\n", $x, $y);
	    my ($X, $Y) = rot($x, $y);
	    $_ = sprintf("$pre%.${xlen}f$mx%.${ylen}f$my$rest\n", $X, $Y);
	} elsif (m/(^[ \t]*Pad[ \t]*\[[ \t]*)([-+]?\d*(\.\d*))((mm|mil)?[ \t]+)([-+]?\d*(\.\d*))((mm|mil)?[ \t]+)([-+]?\d*(\.\d*))((mm|mil)?[ \t]+)([-+]?\d*(\.\d*))((mm|mil)?[ \t]+)([ \t].*)/) {
	    my $pre = $1;
	    my $x1 = $2;
	    my $x1d = $3;
	    my $m1x = $4;
	    my $y1 = $6;
	    my $y1d = $7;
	    my $m1y = $8;
	    my $x2 = $10;
	    my $x2d = $11;
	    my $m2x = $12;
	    my $y2 = $14;
	    my $y2d = $15;
	    my $m2y = $16;
	    my $rest = $18;

	    my $x1len = length($x1d) - 1;
	    my $y1len = length($y1d) - 1;
	    my $x2len = length($x2d) - 1;
	    my $y2len = length($y2d) - 1;
	    #print;
	    #printf("$pre%.${x1len}f$m1x%.${y1len}f$m1y%.${x2len}f$m2x%.${y2len}f$m2y$rest\n", $x1, $y1, $x2, $y2);
	    my ($X1, $Y1) = rot($x1, $y1);
	    my ($X2, $Y2) = rot($x2, $y2);
	    $_ = sprintf("$pre%.${x1len}f$m1x%.${y1len}f$m1y%.${x2len}f$m2x%.${y2len}f$m2y$rest\n", $X1, $Y1, $X2, $Y2);
	}
    }
    print $fo $_;

}
