#!/usr/bin/perl -w

use strict;
use Opt qw/ $PRG /;
use Pcb;

my @PCB = ("", 0, 0); # board name, board size: x y

#########

sub per_file($$) {
    my $fh = shift;
    my $pfx = shift;

    while(<$fh>) {
	if (m/^PCB\[(.*)\]/) {
	    my $str = $1;
	    my @fld = split(/ +/, $str);
	    my $x = Pcb::dim2nm($fld[1]);
	    my $y = Pcb::dim2nm($fld[2]);
	    if (!defined($x) || !defined($y)) {
		chomp;
		print "Illegal units in <$_>\n";
		exit 1;
	    }
	    @PCB = ($fld[0], $x, $y);
	} elsif (m/^Element/) {
	    my @fld = split('"');
	    $fld[5] = $pfx . $fld[5];
	    $_ = join('"', @fld);
	} else {
	}
	print;
    }
}

sub main() {
    Opt::opt();

    my $pfx = shift @ARGV;

    if (!defined($pfx) || $pfx eq "") { Opt::Usage("Usage:
\t$PRG pfx <pcb_file> ...

\twrite pcb_file to stdout but prepend each elements refdes with pfx
"); }
    for my $file (@ARGV) {
	my $fh;
	next unless (open($fh, $file));
	per_file($fh, $pfx);
	close($fh);
    }
}

main();

