2010-09-07 23:03:19 +03:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# sanitize-profile - Remove items from a KiCad profile that may cause an upset
|
|
|
|
#
|
2012-05-11 06:53:05 +03:00
|
|
|
# Written 2010, 2012 by Werner Almesberger
|
|
|
|
# Copyright 2010, 2012 Werner Almesberger
|
2010-09-07 23:03:19 +03:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
@LIBS = ("/usr/share/kicad/library", "/usr/local/share/kicad/library");
|
|
|
|
|
|
|
|
|
|
|
|
sub rewrite
|
|
|
|
{
|
|
|
|
local ($s) = @_;
|
|
|
|
|
|
|
|
return $s if $section ne "eeschema/libraries";
|
|
|
|
return $s unless /^LibName(\d+)=(.*)\s*$/;
|
|
|
|
my $lib = $2;
|
2010-10-04 09:04:35 +03:00
|
|
|
delete $add{$lib};
|
2010-09-07 23:03:19 +03:00
|
|
|
if ($1 == $in_lib) {
|
|
|
|
$in_lib++;
|
|
|
|
} else {
|
2010-10-04 04:15:43 +03:00
|
|
|
print STDERR "LibName$1 when expecting LibName$in_lib. Renumbering.\n";
|
2010-09-07 23:03:19 +03:00
|
|
|
$in_lib = $1+1;
|
|
|
|
}
|
|
|
|
$out_lib++;
|
|
|
|
my $var = "LibName$out_lib";
|
|
|
|
if ($lib =~ /\//) {
|
|
|
|
return "$var=$lib\n" if -r "$lib.lib";
|
|
|
|
}
|
2012-05-11 06:53:05 +03:00
|
|
|
for (".", @libdir, @LIBS) {
|
2010-09-07 23:03:19 +03:00
|
|
|
return "$var=$lib\n" if -r "$_/$lib.lib";
|
|
|
|
}
|
|
|
|
print STDERR "cannot find $lib\n";
|
|
|
|
$out_lib--;
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-04 09:04:35 +03:00
|
|
|
sub complement
|
|
|
|
{
|
|
|
|
for (sort keys %add) {
|
|
|
|
print STDERR "adding $_\n";
|
|
|
|
$out_lib++;
|
|
|
|
push(@f, "LibName$out_lib=$_\n");
|
|
|
|
delete $add{$_};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-07 23:03:19 +03:00
|
|
|
sub usage
|
|
|
|
{
|
|
|
|
print STDERR "usage: $0 file.pro [outfile]\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-04 09:04:35 +03:00
|
|
|
for (split(",", $ENV{"SCHHIST_ADDLIBS"})) {
|
|
|
|
$add{$_} = 1 if $_ ne "";
|
|
|
|
}
|
|
|
|
|
2010-09-07 23:03:19 +03:00
|
|
|
($file, $out) = @ARGV;
|
|
|
|
&usage if $#ARGV > 1;
|
|
|
|
|
2012-05-24 06:21:36 +03:00
|
|
|
($dir = $file) =~ s|[^/]*$||;
|
2010-09-07 23:03:19 +03:00
|
|
|
$dir = "." if $dir eq "";
|
|
|
|
|
|
|
|
open(FILE, $file) || die "$file: $!";
|
|
|
|
while (<FILE>) {
|
|
|
|
if (/^\[(\S+)\]/) {
|
2010-10-04 09:04:35 +03:00
|
|
|
if ($1 eq "eeschema/libraries") {
|
2010-09-07 23:03:19 +03:00
|
|
|
$in_lib = 1;
|
|
|
|
$out_lib = 0;
|
2010-10-04 09:04:35 +03:00
|
|
|
} else {
|
|
|
|
&complement if $section eq "eeschema/libraries";
|
2010-09-07 23:03:19 +03:00
|
|
|
}
|
2010-10-04 09:04:35 +03:00
|
|
|
$section = $1;
|
2010-09-07 23:03:19 +03:00
|
|
|
}
|
|
|
|
if ($section eq "eeschema") {
|
2012-05-11 06:53:05 +03:00
|
|
|
@libdir = split(/;/, $1) if /^LibDir=(.*)\s*$/;
|
2010-09-07 23:03:19 +03:00
|
|
|
}
|
|
|
|
$s = &rewrite($_);
|
|
|
|
push(@f, $s) if defined $s;
|
|
|
|
}
|
|
|
|
close FILE;
|
2010-10-04 09:04:35 +03:00
|
|
|
∁
|
2010-09-07 23:03:19 +03:00
|
|
|
|
|
|
|
if (!defined $out) {
|
|
|
|
rename($file, "$file.bak");
|
|
|
|
$out= $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
open(FILE, ">$out") || die "$out: $!";
|
|
|
|
print FILE join("", @f) || die "$out: $!";
|
|
|
|
close FILE || die "$out: $!";
|