Speed up unsubtraction from polygons when objects are deleted
[geda-pcb/gde.git] / lib / m4lib_to_newlib.sh
blobe609e33a8eb751e69efc828dee5acf9fe6df3bc0
1 #!/bin/sh
3 # $Id$
6 # This script is used to extract all elements from an "oldlib" (M4)
7 # style library and place them in individual "newlib" style files.
9 outd=/tmp/newlib
10 contents=pcblib.contents
11 AWK=${AWK:-awk}
12 PCB=${PCB:-pcb}
14 usage() {
15 cat << EOF
16 Usage:
17 $0 [-h | --help]
18 $0 [-v | --version]
19 $0 [-c|--contents contents_file] [-o|--output output_directory] [-p|--png] [-d|--dpi]
21 Extracts all footprints from an m4 library and creates a "newlib" style
22 library.
24 The following options are supported:
26 -a | --awk awk : Specifies the awk implementation to use. Defaults to "${AWK}".
28 -c | --contents file : Specifies the contents file to be use as an input.
29 Default is "${contents}".
31 -d | --dpi : Specifies that the png output should use a fixed pixels
32 per inch scaling instead of a fixed maximum size. This
33 option is useful when comparing the before and after footprints
34 when making footprint library changes.
36 -h | --help : Outputs this message and exits.
38 -o | --output dir : Specifies the directory that the newlib library will be
39 written to. This directory must exist and be empty.
40 Default is "${outd}".
42 -P | --pcb pcb : Specifies the pcb binary to use for creating png previews. Defaults to
43 "${PCB}"
45 -p | --png : Generates png previews for all the footprints.
47 -v | --version : Displays the version of this script and exits.
49 EOF
52 version() {
53 $AWK '/# [\$]Id:.*$/ {sub(/,v/,""); \
54 print $3 " Version "$4", "$5}' $0
57 do_png=0
58 png_flag="--xy-max 200"
60 while test $# -gt 0 ; do
61 case $1 in
62 -a|--awk )
63 AWK="$2"
64 shift 2
67 -c|--contents )
68 contents=$2
69 shift 2
72 -d|--dpi )
73 png_flag="--dpi 1000"
74 shift
77 -h|--help )
78 usage
79 exit 0
82 -o|--output )
83 outd=$2
84 shift 2
87 -P|--pcb )
88 PCB="$2"
89 shift 2
92 -p|--png )
93 do_png=1
94 shift
97 -v|--version )
98 version
99 exit 0
102 -* )
103 echo "ERROR: $0: Unknown option $1"
104 usage
105 exit 1
109 break
111 esac
112 done
115 if test -d ${outd} ; then
116 echo "Output directory ${outd} already exists"
117 exit 1
118 else
119 mkdir -p ${outd}
121 outd_full=`cd $outd && pwd`
123 $AWK '
125 BEGIN {
126 first = 1;
127 libind = "";
130 # we have to use this trick because variables like outd are not yet defined
131 # in BEGIN.
132 first == 1 {
133 brokenurl = "broken.html";
134 broken = outd "/" brokenurl;
135 print "" > broken;
137 ind = outd "/index.html";
139 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">" > ind;
140 print "<HTML><HEAD><TITLE>PCB Footprint Library</TITLE></HEAD>" >> ind;
141 print "<BODY bgcolor=\"#FFFFFF\" text=\"#000000\">" >> ind;
142 print "<H1>PCB Footprint Library</H1>" >> ind;
143 print "<UL>" >> ind;
144 print "" >> ind;
146 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">" > broken;
147 print "<HTML><BODY>" >> broken;
148 print "<TABLE BORDER=2>" >> broken;
149 print "<TR><TD><EM>Library</EM></TD>" >> broken;
150 print " <TD><EM>Comment</EM></TD>" >> broken;
151 print " <TD><EM>Footprint Name</EM></TD>" >> broken;
152 print " <TD><EM>Broken Command</EM></TD>" >> broken;
153 print "</TR>" >> broken;
154 print "" >> broken;
156 first = 0;
159 # we are starting a new library
160 /^TYPE=/ {
161 finish_libind();
162 lib=$0;
163 gsub(/TYPE=~/, "", lib);
164 txtdir = lib;
165 urldir = lib;
166 gsub(/ /, "%20", urldir);
168 libind = outd "/" lib "/index.html";
169 #gsub(/ /, "\\ ", libind);
171 dir = outd "/" lib ;
172 gsub(/ /,"\\ ", dir);
173 print "Processing library: " lib " and creating " libind;
174 system("mkdir -p " dir);
176 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">" > libind;
177 print "<HTML><HEAD><TITLE>PCB " lib " Footprint Library</TITLE></HEAD>" >> libind;
178 print "<BODY bgcolor=\"#FFFFFF\" text=\"#000000\">" >> libind;
179 print "<H1>PCB " lib " Footprint Library</H1>" >> libind;
181 print "<TABLE BORDER=2>" >> libind;
182 print "<TR>" >> libind;
183 print " <TD><EM>Comment</EM></TD>" >> libind;
184 print " <TD><EM>Footprint Name</EM></TD>" >> libind;
185 print "</TR>" >> libind;
186 print "" >> libind;
188 print "<LI><A HREF=\"" txtdir "/index.html\">~" lib "</A></LI>" >> ind;
190 next;
194 line=$0;
195 split(line, a, "[:]");
197 # pick out the name of the footprint
198 comp = a[3];
199 txtcomp = comp;
200 urlcomp = comp;
202 # escape the spaces in for URLs and also filenames
203 gsub(/ /, "%20", urlcomp);
204 gsub(/ /, "\\ ", comp);
206 comment = a[4];
208 # extract the footprint
209 # path library template value package
210 templ = a[1];
211 gsub(/ /, "\\ ", templ);
213 pkg = a[2];
214 gsub(/ /, "\\ ", pkg);
217 # skip the QFP builder menu
218 skip = 0;
219 if( templ == "menu_qfp" ) {
220 cmd1 = "Skipping QFP builder menu";
221 rc = 1;
222 } else {
223 cmd1 = "sh " cmd_path "/QueryLibrary.sh . pcblib " templ " " comp " " pkg;
224 cmd = cmd1 " > " dir "/" comp ".fp";
225 rc = system( cmd );
228 if( rc != 0) {
229 printf("<TR><TD>~%s</TD>\n", lib) >> broken;
230 printf(" <TD>%s</TD>\n", comp) >> broken;
231 printf(" <TD>%s</TD>\n", comment) >> broken;
232 printf(" <TD>%s</TD>\n", cmd1) >> broken;
233 printf("</TR>\n") >> broken;
235 # no need to go further with this footprint. It is broken.
236 next;
237 } else {
238 # generate the web index
239 printf(" <TR>\n <TD>%s</TD>\n", comment) >> libind;
240 printf(" <TD><A HREF=\"%s.fp\">%s.fp</A>", txtcomp, txtcomp) >> libind;
241 if( do_png ) {
242 printf("(<A HREF=\"%s.png\">preview</A>)", txtcomp) >> libind;
244 printf("</TD>\n </TR>\n") >> libind;
247 # Now create a layout with that element and print it.
248 if( do_png ) {
249 layout = "temp.pcb" ;
250 laytmpl = "footprint.pcb" ;
251 compfile = dir "/" comp ".fp";
252 pngfile = dir "/" comp ".png";
253 compfile2 = compfile;
254 gsub(/\\/, "", compfile2);
256 printf(" ===> %s\n", compfile);
257 printf("") > layout;
258 pok = 1;
259 while ( (getline < laytmpl) == 1 ) {
260 if( $0 ~ /ELEMENT/ ) {
261 pok = 0;
263 if( pok ) {
264 print >> layout ;
267 close( laytmpl );
269 while( (x = getline < compfile2) == 1 ) {
270 print >> layout;
272 close( compfile2 );
274 pok = 0;
275 while( (getline < laytmpl) == 1 ) {
276 if( pok ) {
277 print >> layout;
279 if( $0 ~ /ELEMENT/ ) {
280 pok = 1;
283 close( laytmpl );
284 close( layout );
286 cmd = PCB " -x png --outfile temp.png ${png_flag} --only-visible " layout " 2>&1 > /dev/null" ;
287 rc = system( cmd );
288 if( rc != 0) {
289 printf("<TR><TD>~%s</TD>\n", lib) >> broken;
290 printf(" <TD>%s</TD>\n", comp) >> broken;
291 printf(" <TD>%s</TD>\n", comment) >> broken;
292 printf(" <TD>%s</TD>\n", cmd) >> broken;
293 printf("</TR>\n") >> broken;
294 } else {
295 system( "mv temp.png " pngfile " ; rm " layout);
300 END {
301 print "" >> ind;
302 print "</UL>" >> ind;
303 print "" >> ind;
304 print "<P>For a list of footprints with either m4 syntax errors" >> ind;
305 print "or PCB syntax errors see <A HREF=\"" brokenurl "\">the broken log file.</A></P>" >> ind;
306 print "</BODY></HTML>" >> ind;
307 close( ind );
309 print "" >> broken;
310 print "</TABLE>" >> broken;
311 print "" >> broken;
312 print "</BODY></HTML>" >> broken;
313 close( broken );
315 finish_libind();
318 function finish_libind() {
319 if(libind != "") {
320 print "" >> libind;
321 print "</TABLE>" >> libind;
322 print "" >> libind;
323 print "</BODY></HTML>" >> libind;
324 close( libind );
328 ' cmd_path=./ do_png=$do_png outd=$outd_full awk=$AWK PCB="${PCB}" $contents