#!/usr/bin/perl -w

use strict;
use Opt;
use Pcb;

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;
	    }
	} elsif (m/^Element/) {
	    my @fld = split('"');
	    $fld[5] = $pfx . $fld[5];
	    $_ = join('"', @fld);
	} else {
	}
	print;
    }
}

sub main() {
    my $prg = Opt::prg();
    my $pfx = shift @ARGV;

    if (!defined($pfx) || $pfx eq "") { Opt::usage(0, "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();

