File: printtool

package info (click to toggle)
printtool 4.5-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 568 kB
  • ctags: 123
  • sloc: tcl: 3,209; sh: 344; makefile: 65
file content (4909 lines) | stat: -rw-r--r-- 146,268 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
#!/usr/bin/wish -f

# Printer config tool
# (C) Copyright 1994 by Red Hat Software
# (C) Copyright 1995 by Michael Callahan
# Version 3.0 additions made by Michael Fulbright (Red Hat Software)
# Smb printing support supplied by Miquel de Icaza
# NCP printing support supplied by Fernando Lozano, BL Inform��tica Ltda.
# uniprint support supplied by Osamu Aoki, Cupertino, USA
# Direct to port support supplied by Josh Buysse, University of Minnesota

if {[catch {set env(CONTROL_PANEL_LIB_DIR)}] != 0} {
    set env(CONTROL_PANEL_LIB_DIR) /usr/local/share/printtool
}
if {[catch {source $env(CONTROL_PANEL_LIB_DIR)/dialog.tcl}] != 0} {
    puts "Couldn't load dialog.tcl"
    puts "Start from control-panel or set environment variable"
    puts "CONTROL_PANEL_LIB_DIR to the control-panel library dir."
    puts "(normally this is /usr/share/printtool)"
    exit 0
}
if {[catch {source $env(CONTROL_PANEL_LIB_DIR)/bindings.tcl}] != 0} {
    puts "Couldn't load bindings.tcl"
    puts "Start from control-panel or set environment variable"
    puts "CONTROL_PANEL_LIB_DIR to the control-panel library dir."
    puts "(normally this is /usr/share/printtool)"
    exit 0
}

set warn_samba 1
set warn_ncpfs 1

catch {source $env(CONTROL_PANEL_LIB_DIR)/printtool.warn}

#########################################################
## @@ Random Data
set version "4.5"
set printer_count 0
set delete_index ""
set trigger 0
set trigger_2 0
set trigger_help 0
set tmp_auto_pseof ""
set tmp_auto_texteof ""
set tmp_auto_sendeof ""
set tmp_auto_crlf_trans ""
set tmp_auto_ascps_trans ""
set tmp_auto_rev_pages ""
set tmp_auto_nup "1"
set gs_installed_drivers ""
set locale ""
set current_printer_type ""
set autodetecting 0

#
# msf - printer_list and resolution_list have been superceded by the
#       printer database entries.
#
# paper_size_list will still be used.
#
set printer_list "PostScript laserjet ljet2p ljet3 ljet4 ljetplus epson epsonc ibmpro jetp3852 deskjet djet500 djet500c bj10e bj200 cdeskjet cdjcolor cdjmono cdj500 cdj550"
set resolution_list "300x300 360x360 400x400 600x600 800x800"
set paper_size_list "letter legal ledger a3 a4 b5 b4"

#
# msf - we will read in a printer database and the following array variables
#       are defined
#  
#
#  printerdb_entry() - array of names of entries in printer database, not
#                      to be confused with the printcap file.
#                      This string is used internally to refer to drivers.
#                      Index from 0 to (# of printerdb entries) - 1
#  printerdb_descr() - array of one-line descriptions of driver.
#                      This string will be used in listbox of drivers
#                      from which the user choses a driver.
#                      Probably indexed by printerdb_entry name for driver.
#  printerdb_descr2entry() - takes a description as index, returns matching
#                            entry name. Use to back-reference from
#                            sorted description list to entry list.
#  printerdb_about() - array of arbitrarily long strings which give more
#                      details information about the driver.
#                      Probably indexed by printerdb_entry name for driver.
#  printerdb_GSdriver() - array of one-line descriptions of driver.
#                      This is the GS driver to use in the filter.
#                      Probably indexed by printerdb_entry name for driver.
#  printerdb_res()   - array of list of lists.
#                      Each list entry is format {XDPI} {XYPI} {Comment}.
#                      Probably indexed by printerdb_entry name for driver.
#  printerdb_color() - array of list of lists.
#                      Each list entry is format {Color depth} {Comment}.
#                      Probably indexed by printerdb_entry name for driver.
#                      For uniprint style command line, each list entry 
#                      is format {upp_file} {upp_comment}.
#                      upp_file is corresponding filename excluding .upp, and
#                      upp_comment is obtained from first line of upp_file
#                      
#
set printerdb_entry(0) ""
set printerdb_descr(0) ""
set printerdb_about(0) ""
set printerdb_GSDriver(0) ""
set printerdb_color(0) ""
set printerdb_res(0) ""
set printerdb_descr2entry(0) ""

#
# auto_printer is the GS driver to use for selected filter
# rest are self explanatory
#
# msf - Added auto_printerdb_entry to store printerdb entryname
#
set auto_printer ""
set auto_printerdb_entry ""
set auto_resolution ""
set auto_paper_size ""
set auto_flag "*auto*"
set auto_pseof ""
set auto_texteof ""
set auto_sendeof ""
set auto_eof ""
set auto_crlf_trans ""
set auto_ascps_trans ""
set auto_rev_pages ""
set auto_color "Default"
set auto_nup "1"
set auto_rtlft_margin "18"
set auto_topbot_margin "18"
set auto_GSextra ""

set auto_direct_printer_ip ""
set auto_direct_port ""

#
# new filter related locations and filenames
#

set filtersrcdir "/usr/local/share/printfilters"
set libexecdir "/usr/local/libexec"
set sbindir "/usr/local/sbin"

#
# The location of PPD files
#

set ppddir "/usr/share/postscript/ppd/"

#set filter_template "$env(CONTROL_PANEL_LIB_DIR)/filter-template"

#########################################################
## @@ User interface

frame .menuf -relief raised -borderwidth 2
menubutton .menuf.fsm -text "PrintTool" -menu .menuf.fsm.menu
menu .menuf.fsm.menu
.menuf.fsm.menu add command -label "Reload" -command menu_reload
.menuf.fsm.menu add separator
.menuf.fsm.menu add command -label "About" -command menu_about
.menuf.fsm.menu add separator
.menuf.fsm.menu add command -label "Quit" -command menu_quit
menubutton .menuf.nfs -text "lpd" -menu .menuf.nfs.menu
menu .menuf.nfs.menu
.menuf.nfs.menu add command -label "Restart lpd" -command menu_restart_lpd
menubutton .menuf.tests -text "Tests" -menu .menuf.tests.menu
menu .menuf.tests.menu
.menuf.tests.menu add command -label "Print ASCII test page" -command print_ascii_testpage
.menuf.tests.menu add command -label "Print Postscript test page" -command print_ps_testpage
.menuf.tests.menu add command -label "Print ASCII directly to port" -command print_ascii_direct_testpage
menubutton .menuf.help -text "Help" -menu .menuf.help.menu
menu .menuf.help.menu
.menuf.help.menu add command -label "General Help" -command menu_genhelp
.menuf.help.menu add command -label "Troubleshooting" -command menu_trouble


pack .menuf.fsm .menuf.nfs .menuf.tests .menuf.help -side left -in .menuf
tk_menuBar .menuf .menuf.fsm .menuf.nfs .menus.tests .menus.help

label .header -font fixed -text "Printer Queues in /etc/printcap"

frame .main -relief flat -borderwidth 2
listbox .list -font fixed -yscrollcommand ".sb set" -setgrid 1 -exportselection 0 -selectmode single
scrollbar .sb -command ".list yview"
pack .list -side left -expand true -fill both -in .main
pack .sb -side left -fill y -in .main

frame .buttons
button .edit -text "Edit" -width 10 -command button_edit
button .add -text "Add" -width 10 -command button_add
button .delete -text "Delete" -width 10 -command button_delete

# MLP
button .autodetect -text "AutoDetect" -width 10 -command button_autodetect
# MLP
pack .edit .add .delete .autodetect -side left -expand true -ipady 1 \
	-padx .5c -in .buttons


pack .menuf -side top -fill x
# The following large padx is so that the header
# lines up properly with the stuff in the listbox
pack .header -side top
pack .main -side top -expand true -fill both -padx 4
pack .buttons -side top -fill x -padx 4 -pady 4

wm title . "Red Hat Linux Print System Manager"
update
scan [wm geometry .] "%d%*c%d" xmin ymin
wm minsize . $xmin $ymin

bind .list <Double-Button-1> "
    .list selection clear 0 end
    .list selection set \[.list nearest %y\]
    button_edit
"

## End of main user interface
###########################################################
## @@ Random Functions

proc {get_locale} {} {
#----------------------------------------------------
# Get the two-character locale code.
#----------------------------------------------------

    global locale

    #------------------------------------------------
    # Open pipe to locale command.
    #------------------------------------------------
    if [catch {open "| locale "} input] {
	# If the open failed, then something is wrong. 
	# Let's assume English locale, and continue.
	set locale "en"
	return
    }

    while {[gets $input line] >= 0} {
	set parts [split $line "="]
	if { [lindex $parts 0] == "LANG" } {
	    # Locale command doesn't give us a standard LANG arguement
	    # so we go with english.  adb 7/13/01
	    if { [lindex $parts 1] == "POSIX" || [lindex $parts 1] == "C" } {
		set locale "en"
		return
	    }
	    set lang [string range [lindex $parts 1] 0 1]
	}
	if { [lindex $parts 0] == "LC_ALL" } {
	    set lc_all [string range [lindex $parts 1] 0 1]
	}
    }

    if { [info exists lang] } {
	set locale $lang
    } elseif { [info exists lc_all] } {
	set locale $lc_all
    } else {
	set locale "en"
    }

}

proc load_ppdxref_file {} {
#----------------------------------------------------
# Load the PPD cross-reference file into an associative
# array.
# $xref(printer-name) = ppd-filename
#----------------------------------------------------
    global filtersrcdir
    global xref

    if {[catch {set fd [open "$filtersrcdir/ppd_xref.rdb" r]}] == 1} {
	# there is no printerdb
	puts "Error cannot find $filtersrcdir/ppd_xref.rdb!"
	return
    }

    # Skip initial comments:
    while {[gets $fd s] != "-1"} {
	# strip spaces at the front
	set cp [string trimleft $s]

	# if not a comment line then break?
	if {[regexp "^\#" $cp] != 1} {
	    break
	}
    }

    set fnames $s

    # read field types
    gets $fd ftypes

    # read first data line
    gets $fd s

    set i 0
    while {[gets $fd s] != "-1"}  {
	
	set pfields [split $s "\t"]

	set xref([lindex $pfields 1]) [lindex $pfields 0]

	incr i
    }
    set xref_count $i

    close $fd
}

proc reload {} {
    global print_count printer_comments printer_entry
    global printer_names printer_type printer_info
    global version

    set print_count 0
    if {[catch {set fd [open "/etc/printcap" r]}] == 1} {
	# there is no /etc/printcap
	puts "There is no /etc/printcap. Creating one now..."
	if {[catch {set fd [open "/etc/printcap" w]}] == 1} {
	    puts "Cannot create /etc/printcap, exiting"
	    exit 1
	}
	puts $fd "#"
	puts $fd "# This printcap is being created with printtool v.$version"
	puts $fd "# Any changes made here manually will be lost if printtool"
	puts $fd "# is run later on."
	puts $fd "# The presence of this header means that no printcap"
	puts $fd "# existed when printtool was run."
        puts $fd "#"
	close $fd
	if {[catch {set fd [open "/etc/printcap" r]}] == 1} {
	    puts "Cannot create /etc/printcap, exiting"
	    exit 1
	}
    }

    # We read the /etc/printcap file quickly into the arrays
    # printer_comments() and printer_entry().  printer_comments($i)
    # contains the comments which precede a given entry.  The special
    # line "##PRINTTOOL## <type> <info>" allows the printtool to
    # store extra information about the printer; <type> is one of the
    # allowed printer types, and <info> is a TCL list, saved in the
    # array printer_info.  

    set i 0
    set printer_comments(0) ""
    set printer_entry(0) ""
    set printer_type(0) ""
    set printer_info(0) ""
    while {[gets $fd s] != "-1"} {
	# Recognize comments and whitespace lines
	set cp [lindex $s 0]p
	if {[regexp "^\#" $cp] == 1 || [regexp {^[ 	]*$} $s] == 1} {
	    # Is it a special printtool line?
	    if {[regexp "^\\#\#PRINTTOOL3\#\#" $cp] == 1} {
		set printer_type($i) [lindex $s 1]
		set printer_info($i) [lrange $s 2 [expr [llength $s]-1]]
	    } elseif {[regexp "^\\#\#PRINTTOOL\#\#" $cp] == 1} {
		set printer_type($i) [lindex $s 1]
		set printer_info($i) [lrange $s 2 [expr [llength $s]-1]]
	    } else {
		set printer_comments($i) "$printer_comments($i)$s\n"
	    }
	    continue
	}
	
	# If we get to here we have a printer definition
	# Read it in--keep reading until we reach a line that doesn't
	# end in the continuation mark, or EOF
	set printer_entry($i) "$s\n"
	while {[regexp "\\\\$" $s] && [gets $fd s] != "-1"} {
	    set printer_entry($i) "$printer_entry($i)$s\n"
	}
	incr print_count
	incr i
	set printer_comments($i) ""
	set printer_entry($i) ""
	set printer_type($i) ""
	set printer_info($i) ""
    }

    close $fd

    # NB: the printer_entry and printer_comments arrays have different
    # array bounds.  printer_entry is defined from 0 to $print_count-1.
    # printer_comments is defined from 0 to $print_count.
    # printer_comments($print_count) consists of comments following
    # the last printer.

    # Now go through to examine each entry
    for {set i 0} {$i < $print_count} {incr i} {
	# Canonicalize entry
	set e $printer_entry($i)
	regsub -all "\\\\\n\[ \t\]*" $e " " e
	regsub -all ":\[ \t\]*:" $e ":" e
	regsub "\[ \t\]*\n\$" $e "" e
	set printer_entry($i) $e

	# First field gives the printer names
	regexp {^([^:]*):} $printer_entry($i) bogus printer_names($i)

	# Fix old mx% bug -- should be mx#, but to avoid duplicate
	# entries we just kill the old mx% specifications
	regsub -all {:(..)%([0-9]*):} $printer_entry($i) {:} printer_entry($i)

	# Fix printer_type
	if {$printer_type($i) == ""} {
	    set printer_type($i) UNKNOWN
	} else {
	    # Make sure this type is known
	    if {[info procs "$printer_type($i)_summaryline"] == ""} {
		set printer_type($i) UNKNOWN
		set printer_info($i) {}
	    }
	}
#
# msf - following check will no longer work, since we are not using
#       the same filter template
#
#       Instead we should have the magic filter script have a special
#       header at its start which we can try and recover this information
#       from.

#
#	if {$printer_type($i) == "UNKNOWN"} {
#	    set res [guess_printer_type $i]
#	    if {[llength $res] > 0} {
#		set printer_type($i) [lindex $res 0]
#		set printer_info($i) [lrange $res 1 [expr [llength $res]-1]]
#	    }
#	}
    }
}

proc guess_printer_type {i} {
    global print_count printer_comments printer_entry
    global printer_names printer_type printer_info

#
# msf - this shouldnt be called currently, needs to be rewritten
#
    puts "ERROR - guess_printer_type called!!"
    exit 1

    if {[printcap_field $i "rm=" ""] != "" && \
	    [printcap_field $i "rp=" ""] != ""} {
	return {REMOTE}
    }

    set spool_dir [printcap_field $i "sd=" ""]
    set i_filter [printcap_field $i "if=" ""]
    if {$i_filter == "$spool_dir/filter"} {
	# possibly a filter generated by this tool
	if {[catch {set fd [open $i_filter "r"]}] == 1} {
	    return {}
	}
	gets $fd s
	gets $fd s
	gets $fd s
	if {[regexp "^DEVICE=\(.*\)\$" $s dummy auto_printer] != 1} {
	    close $fd
	    return {}
	}

	gets $fd s
	if {[regexp "^RESOLUTION=\(.*\)\$" $s dummy auto_resolution] != 1} {
	    close $fd
	    return {}
	}

	gets $fd s
	if {[regexp "^PAPERSIZE=\(.*\)\$" $s dummy auto_paper_size] != 1} {
	    close $fd
	    return {}
	}

	gets $fd s
	if {[regexp "^SENDEOF=\(.*\)\$" $s dummy auto_pseof] != 1} {
	    close $fd
	    return {}
	}

	close $fd
	return [list "LOCAL" $auto_printer $auto_resolution $auto_paper_size \
		    $auto_pseof]
    }
    return {}
}

proc printcap_field {i field default} {
    global printer_entry
    if {[regexp ":$field\(\[^:\]*\):" $printer_entry($i) dummy val] == 1} {
	return $val
    } else {
	return $default
    }
}

proc set_printcap_field {i field val} {
    global printer_entry
    if {[regsub ":$field\[^:\]*:" $printer_entry($i) ":$field$val:" \
	     printer_entry($i)] == 0} {
	# not there already: add to end (recall $printer_entry($i) ends w/":")
	set printer_entry($i) "$printer_entry($i)$field$val:"
    }
}

proc set_printcap_field_if_not_null {i field val} {
    if { $val != "" } {
	set_printcap_field $i "$field" "$val"
    }
}

proc remove_printcap_field {i field} {
    global printer_entry
    regsub ":$field\[^:\]*:" $printer_entry($i) ":" printer_entry($i)
}

proc printcap_switch {i field} {
    global printer_entry
    return [regexp ":$field:" $printer_entry($i)]
}

proc set_printcap_switch {i field val} {
    global printer_entry
    if {$val == 0} {
	regsub ":$field:" $printer_entry($i) ":" printer_entry($i)
    } else {
	if {[regexp ":$field:" $printer_entry($i)] == 0} {
	    set printer_entry($i) "$printer_entry($i)$field:"
	}
    }
}

proc redisplay {} {
    global print_count printer_names printer_type

    .list delete 0 end
    for {set i 0} {$i < $print_count} {incr i} {
	.list insert end [format "%-20s  %s" $printer_names($i) \
			      [$printer_type($i)_summaryline $i] ]
    }
}


#
# Routines to read the print filter information encoded on the
# PRINTOOL(3) comment line in the /etc/printcap
#
# This method of storing this is into is an ugly, temporary hack!
#
#

#
# parse_auto_filter_params { info }
#
# Pass this route the printer_info for the printer of interest.
# The printer_info is everything beyond the printer_type in the comment line
#
# Sets the following (global) auto_filter parameters based on $info:
#
# auto_printer
# auto_resolution
# auto_paper_size
# auto_eof
# auto_printerdb_entry
# auto_color
# auto_crlf_trans
# auto_rev_pages

#
#
#  also checks into several filter config files to get additional auto_filter
#  parameters
#
proc parse_auto_filter_params { index info spool_dir } {
    global auto_printer auto_resolution auto_paper_size auto_flag
    global printerdb_count printerdb_entry printerdb_GSDriver
    global auto_printerdb_entry
    global auto_color auto_crlf_trans auto_rev_pages
    global auto_ascps_trans
    global aout_eof auto_pseof auto_texteof auto_sendeof
    global auto_GSextra
    global auto_nup
    global auto_rtlft_margin auto_topbot_margin

#
# see if the printcap entry has any auto_filter info
#
    if {[llength $info] >= 4} {
	set auto_printer [lindex $info 0]
	set auto_resolution [lindex $info 1]
	set auto_paper_size [lindex $info 2]
	set auto_eof [lindex $info 3]
#
#       see if we have a PRINTTOOL3 info line, or older one 
#       which will need to be converted to a PRINTTOOL3 format
#
	if { [llength $info] > 4} {
	    set auto_printerdb_entry [lindex $info 4]
	    set auto_color [lindex $info 5]
	    set auto_crlf_trans [lindex $info 6]
	    set auto_rev_pages [lindex $info 7]
	} else {
#           we have to find best match in printer database
	    rhs_info_dialog "
It appears this entry in your printcap 
was created by an older version of printtool.
The current printer database will now be 
searched for a compatible printer driver.

A copy of the current '/etc/printcap' file 
will be stored in '/etc/printcap.prior'.
You may want to make a copy of this file
after leaving this dialog box.

It is recommended that you check the driver
selected by the conversion process in order 
to confirm a proper replacement was chosen. 
In particular, the desired print resolution
and color depth may have been inadvertantly 
changed. Simply set these values back to the
 desired settings.

This conversion will not be required again.
"

            if [catch {exec cp /etc/printcap /etc/printcap.prior}] {
		rhs_error_dialog "Unable to make backup of /etc/printcap."
	    }
	    set auto_printerdb_entry ""
	    for {set j 0} {$j < $printerdb_count} {incr j} {
		set pentry $printerdb_entry($j)
#		puts "Looking for $auto_printer, found $printerdb_GSDriver($pentry)"
		if {[string compare $auto_printer $printerdb_GSDriver($pentry)] == 0} {
		    set auto_printerdb_entry $pentry
		    break
		}
	    }
	    if {$auto_printerdb_entry == ""} {
#		puts "Couldnt find driver in printerdb, resetting printer driver"
		rhs_error_dialog "Unable to find compatible driver in the printer database
file. You will need to reselect the driver from the list of available printer types."
		set auto_printerdb_entry $printerdb_entry(0)
	    }
	    set auto_color "Default"
#
#           we want to update printer_info to be new format which includes
#           the printerdb entry and the selected color depth
#
	    set auto_crlf_trans ""
	    set auto_rev_pages ""
	    lappend info $auto_printerdb_entry $auto_color \
		    $auto_crlf_trans $auto_rev_pages
#
#           auto_eof isn't really used anymore
#           guess new eof options based on old eof setting
#
	    if { $auto_eof != "" } {
		set auto_pseof "YES"
		set auto_texteof "YES"
		set auto_sendeof "YES"
	    }
	}
    } else {
	set auto_printer ""
	set auto_resolution ""
	set auto_paper_size ""
	set auto_eof ""
	set auto_pseof "NO"
	set auto_texteof "NO"
	set auto_sendeof "NO"
	set auto_crlf_trans ""
	set auto_rev_pages ""
	set auto_printerdb_entry ""
        set auto_GSDriver ""
	set auto_color ""
    }

#
#    look into filter config files
#
    if {$spool_dir == ""} {
	set auto_ascps_trans "NO"
    } else {

	catch {exec sed -n -e "s/export ASCII_TO_PS=//p" \
		< $spool_dir/general.cfg} auto_ascps_trans
	# MLP:
	set auto_ascps_trans [printcap_field $index "ASCII_TO_PS=" "$auto_ascps_trans"]
	if {$auto_ascps_trans != "YES" && $auto_ascps_trans != "NO"} {
	    set auto_ascps_trans "YES"
	}

	catch {exec sed -n -e "s/PS_SEND_EOF=//p" \
		< $spool_dir/postscript.cfg} auto_pseof
	# MLP:
	set auto_pseof [printcap_field $index "PS_SEND_EOF=" "$auto_pseof"]
	if {$auto_pseof != "YES" && $auto_pseof != "NO"} {
	    set auto_pseof "YES"
	}

	if [catch {exec sed -n -e "s/EXTRA_GS_OPTIONS=//p" \
		< $spool_dir/postscript.cfg} auto_GSextra] {
	    set auto_GSextra ""
	} else {
	    # trim off the quotation marks if they exist
	    set auto_GSextra [string trim $auto_GSextra \"]
	}
	# MLP:
	set auto_GSextra [printcap_field $index "EXTRA_GS_OPTIONS=" "$auto_GSextra"]

	if [catch {exec sed -n -e "s/NUP=//p" \
		< $spool_dir/postscript.cfg} auto_nup] {
	    set auto_nup "1"
	    # MLP:
	    set auto_nup [printcap_field $index "NUP=" "$auto_nup"]
	} else {
	    # MLP:
	    set auto_nup [printcap_field $index "NUP=" "$auto_nup"]
	    if { [regexp {[^0-9]+} $auto_nup junk1 junk2] } {
		puts "Error in NUP value from postscript.cfg"
		set auto_nup "1"
	    }
	    if {$auto_nup != "1" && $auto_nup != "2" && $auto_nup != "4" && \
		    $auto_nup != "8" } {
		set auto_nup "1"
	    }
	}
	
	if [catch {exec sed -n -e "s/RTLFTMAR=//p" \
		< $spool_dir/postscript.cfg} auto_rtlft_margin] {
	    set auto_rtlft_margin "18"
	    # MLP:
	    set auto_rtlft_margin [printcap_field $index "RTLFTMAR=" "$auto_rtlft_margin"]
	} else {
	    set auto_rtlft_margin [string trim $auto_rtlft_margin]
	    # MLP:
	    set auto_rtlft_margin [printcap_field $index "RTLFTMAR=" "$auto_rtlft_margin"]
	    if { [regexp {[^0-9]+} $auto_rtlft_margin junk1 junk2] } {
		puts "Error in RT/LFT margin value from postscript.cfg"
		set auto_rtlft_margin "18"
		puts "Setting to default value of $auto_rtlft_margin pts."
	    } elseif { $auto_rtlft_margin == "" } {
		set auto_rtlft_margin "18"
	    } 
	}

	if [catch {exec sed -n -e "s/TOPBOTMAR=//p" \
		< $spool_dir/postscript.cfg} auto_topbot_margin] {
	    set auto_topbot_margin "18"
	    # MLP:
	    set auto_topbot_margin [printcap_field $index "TOPBOTMAR=" "$auto_topbot_margin"]
	} else {
	    set auto_topbot_margin [string trim $auto_topbot_margin]
	    # MLP:
	    set auto_topbot_margin [printcap_field $index "TOPBOTMAR=" "$auto_topbot_margin"]
	    if { [regexp {[^0-9]+} $auto_topbot_margin junk1 junk2] } {
		puts "Error in TOP/BOT margin value from postscript.cfg"
		set auto_topbot_margin "18"
		puts "Setting to default value of $auto_rtlft_margin pts."
	    } elseif { $auto_topbot_margin == "" } {
		set auto_topbot_margin "18"
	    } 
	}

	catch {exec sed -n -e "s/TEXT_SEND_EOF=//p" \
		< $spool_dir/textonly.cfg} auto_texteof
	# MLP:
	set auto_texteof [printcap_field $index "TEXT_SEND_EOF=" "$auto_texteof"]
	if {$auto_texteof != "YES" && $auto_texteof != "NO"} {
	    set auto_texteof "NO"
	}

	if {$auto_texteof == "YES" || $auto_pseof == "YES"} {
	    set auto_sendeof "YES"
	} else {
	    set auto_sendeof "NO"
	}
    }

    return $info
}




#
# Routines to handle the printer database
#
#
#

#
# Function to read a printer entry from the database
#
# Pass it the file descriptor of the open database
#
# If valid entry found, name of entry will be returned.
#        Null string returned if no more valid entries.
#
#
# Position in database on return will point past the entry read in
# and GetNextField{} should be called to start reading entry in
#
#
proc FindNextEntry { fd } {

    global printerdb_curdbline

#
# search through file until we find a line marking the start of an entry
#

    while {[gets $fd s] != "-1"} {
	incr printerdb_curdbline

	# strip spaces at the front
	set cp [string trimleft $s]

	# is this a comment line?
	if {[regexp "^\#" $cp] == 1} {
	    continue
	}
	# is this the line we're seeking
	if {[regexp "^StartEntry:" $cp] == 1} {
	    return [lindex [split $cp :] 1]
	}
    }

    # we reached EOF and found no more entries
    return {}
}


#
# GetNextField { fd }
#
# Read the next field from file descriptior fd from inside a printer entry.
# ASSUMES that FindNextEntry{} has been called an that
# file is positioned at start of an entry.
# Handles comment lines and continuation markers.
# If end of entry reached, returns 'EndEntry'.
# Otherwise, a list is returned of the format:
# { {FieldName} {param0} {param1} {param2} etc }
# 
#
proc GetNextField { fd } {
    global printerdb_curdbline
    global printerdb_debug

    # First we want to find the line marking the start of a new field
    while {[gets $fd s] != "-1"} {
	incr printerdb_curdbline

	if { $printerdb_debug } {
	    puts "In GetNextField, read line number $curdbline"
	    puts "        ->   $s"
	}

	# strip spaces at the front
	set cp [string trimleft $s]

	# is this a comment line?
	if {[regexp "^\#" $cp] == 1} {
	    continue
	}
	# is something messed up?
	if {[regexp "^StartEntry:" $cp] == 1} {
	    puts "Somehow we've hit the start of a new entry.\n"
	    puts "Printer data base file is corrupt near line $printerdb_curdbline.\n"
	    exit 0
	}
	# if it the end of an entry
	if {[regexp "EndEntry" $cp] == 1} {
	    return [list EndEntry]
	}
	# Ok, is it a field? We can tell if it has a word followed by a colon
	if {[string first : $cp] != -1} {
	    set fieldlist [split $cp :]
	    set fieldname [lindex $fieldlist 0]
	    set fieldparams [string trim [lindex $fieldlist 1]]

	    if { $printerdb_debug } {
		puts  "fieldlist   -> $fieldlist"
		puts  "fieldname   -> $fieldname"
		puts  "fieldparams -> $fieldparams"
	    }

	    # if fieldparams ends with a continuation marker, we need to 
            # keep reading lines.
	    if {[regexp "\\\\$" $fieldparams] == 1} {
		if { $printerdb_debug } {
		    puts "We found trailing continuation marker"
		}

		# remove trailing continuation mark
		set fieldparams [string trimright $fieldparams " \\"]

		while {[gets $fd s] != "-1"} {
		    incr printerdb_curdbline

		    if { $printerdb_debug } {
			puts "Line1: $s"
		    }
		    # strip spaces at both ends
		    set cp [string trim $s]

		    # is this a comment line?
		    if {[regexp "^\#" $cp] == 1} {
			continue
		    }

		    # ok, append to current line
		    append fieldparams " " $cp
		    if { $printerdb_debug } {
			puts "Appended: $fieldparams"
		    }

	            # is this another continuation line?
		    # if so, cleanup end of line and keep going
		    if {[regexp "\\\\$" $fieldparams] == 1} {
			set fieldparams [string trimright $fieldparams " \\"]
			continue
		    } else {
			# we're done
			break
		    }
		}

	    }
	    # finished reading this param, time to move to next
#	    puts "| $fieldparams |"
#	    puts "len of fieldparams is [string length $fieldparams]"
	    return [concat $fieldname  $fieldparams]
	}
    }

    # we reached EOF and found no more entries
    return {}
}

#
# see if ghostscipt is installed and which drivers compiled in
# 
proc find_gs {} {
    global gs_installed_drivers

    set gs_installed_drivers ""

    if [catch {open "|/usr/bin/gs -help |& cat"} input] {
	puts "open result was $input"
    }

    if { [string first "couldn't execute" $input] != -1 } {
	rhs_error_dialog "Ghostscript does not appear to be installed.
This severely restricts your printing options unless either:

 a) You want to setup your printer to print text only, or
 b) Your printer can handle PostScript natively.

If neither of these apply, it would be best to install ghostscript now.
"
set gs_installed_drivers {TEXT POSTSCRIPT}
return
}

    while {[gets $input line] >= 0} {
	append gshelp $line \n
    }
    catch {close $input}

    set avail [string first "Available devices:" $gshelp]
    incr avail [string length "Available devices:\n"]
    set availstr [string range $gshelp $avail end]
    set nextfield [string first ":" $availstr]

    for {set i $nextfield} {[string index $availstr $i] != "\n"} {incr i -1} {
	if { $i <= 1 } {
	    puts "Error - couldnt find available devices in find_gs"
	    puts "        this should not happen!"
	}
    }
    incr i -1
    set availstr [string range $availstr 0 $i]
    regsub -all \t $availstr {} availstr2
    set gs_installed_drivers [split $availstr2]
#
# if pnm2ppa is installed, and the gs driver "pnmraw" is also installed,
# add "ppa" to the list of installed drivers.
    if {[catch {open "|/usr/bin/pnm2ppa -h"} input] == 0}  {
	if {[regexp "pnmraw" $gs_installed_drivers ] == 1} {
	     lappend gs_installed_drivers {ppa} 
	}
    }
#
# following two DO NOT depend upon ghostscript being installed
# please handle this more cleanly in future!
#
    lappend gs_installed_drivers {TEXT} {POSTSCRIPT}
}

#
# see if mpage is installed
# 
proc find_mpage {} {

    if [catch {open "|/usr/bin/mpage -x"} input] {
#	puts "open result was $input"
    }

    if { [string first "couldn't execute" $input] != -1 } {
	rhs_error_dialog "Mpage does not appear to be installed.
This severely restricts your printing options unless
you want to setup your printer to print text only.

It would probably be best to install mpage now.
"
}
}


#
# see if smbclient is installed
# 
proc find_samba {} {
    global warn_samba

    if [catch {open "|/usr/bin/smbclient -x"} input] {
#	puts "open result was $input"
    }
    
    if { [string first "couldn't execute" $input] != -1 } {
	set foo [rhs_dialog .foo "Error" "Samba does not appear to be installed.

You will not be able to print to a SMB/Windows 95/NT printer without it installed.

Please quit and install samba before configuring any SMB/Windows 95/NT printer entries.

If you are not interested in support of SMB/Windows 95/NT remote printers,

please choose 'Ignore', and you will not see this message in the future.
" error 0 "Ok" "Ignore"]
         if { $foo != 0 } {
	     set warn_samba 0
	 }
     }
}

#
# see if nprint is installed
# 
proc find_nprint {} {
     global warn_ncpfs

    if [catch {open "|/usr/bin/nprint -x"} input] {
#	puts "open result was $input"
    }

    if { [string first "couldn't execute" $input] != -1 } {
	set foo [rhs_dialog .foo "Error" "ncpfs does not appear to be installed.

You will not be able to print to a NetWare printer without it installed.

Please quit and install ncpfs before configuring any NetWare printer entries.

If you are not interested in support of NetWare remote printers, 

please choose 'Ignore' and you will not see this message in the future.
" error 0 "Ok" "Ignore"]
	if { $foo != 0 } { 
	    set warn_ncpfs 0
	}
    }
}


#
# load in the printer database
#

proc reload_printerdb {} {
    global printerdb_entry printerdb_descr
    global printerdb_about printerdb_GSDriver
    global printerdb_res   printerdb_color
    global printerdb_count
    global printerdb_descr2entry
    global printerdb_curdbline
    global filtersrcdir

#
#   counter to let us know where printerdb integrity errors occur
#
    set printerdb_curdbline 0
    set printerdb_debug 0
    set printerdb_count 0
    if {[catch {set fd [open "$filtersrcdir/printerdb" r]}] == 1} {
	# there is no printerdb
	puts "Error: cannot find $filtersrcdir/printerdb!"

	rhs_info_dialog	"Could not find printer database
required for printer setup. Make sure
that the gnulpr printfilters package is
properly installed."

        exit
    }
    
    set i 0
    while { 1 == 1}  {
	# get next entry, fill in display widgets
	set nextname [string trim [ FindNextEntry $fd ]]
	# if we find no more, exit
	if { $nextname == {} } {
	    set printerdb_count $i
	    return;
	}
	set printerdb_entry($i) $nextname
#	puts "Found printer entry called $nextname"
#	puts "Setting printerdb_entry($i) to $nextname"
	incr i

	# find all internal fields
	while { 1 == 1} {
	    set fieldlist [GetNextField $fd ]
#	    puts "fieldlist is $fieldlist"
	    if { [string compare $fieldlist "EndEntry" ] == 0 } {
#		puts "End of entry found for printer entry $nextname"
		break
	    } elseif { $fieldlist == {} } {
		break;
	    } 

	    # store into associative array of lists indexed by field name
	    set fieldname [lindex $fieldlist 0]
	    set field_array($fieldname)  [lrange $fieldlist 1 end]
#	    puts "Field $fieldname value is [lrange $fieldlist 1 end]"

	    # grab ones we really want and store now
	    if { [string compare $fieldname "Description" ] == 0 } {
		set printerdb_descr($nextname) [lindex $field_array(Description) 0]
		set printerdb_descr2entry($printerdb_descr($nextname)) $nextname
	    } elseif { [string compare $fieldname "About" ] == 0 } {
		set printerdb_about($nextname) [string trim [lindex $field_array(About) 0]]
	    } elseif { [string compare $fieldname "GSDriver" ] == 0 } {
		set printerdb_GSDriver($nextname) [lindex $field_array(GSDriver) 0]
	    } elseif { [string compare $fieldname "Resolution" ] == 0 } {
		lappend printerdb_res($nextname) $field_array(Resolution)
	    } elseif { [string compare $fieldname "BitsPerPixel" ] == 0 } {
		lappend printerdb_color($nextname) $field_array(BitsPerPixel)
	    }

	}
	# set printdb arrays from entries
    }
}






# Each printer type should export functions
#  TYPE_summaryline {i}  giving a summary for the main printer list
#  TYPE_name {}  giving a readable name for the type
#  TYPE_addpanel {w i} adds the configuration entries to the edit panel
#  TYPE_updateentry {w i} pulls configuration options out of the edit panel
#  TYPE_setup {i}  doing whatever extra type-specific setup is necessary
# printer_type_list is a list of printer types which should be presented
# to the user -- ie everything but UNKNOWN

set printer_type_list {LOCAL REMOTE SMB NCP DIRECT}

proc UNKNOWN_summaryline {i} { return "type unrecognized" }
proc UNKNOWN_name {} { return "unknown printer type--should not appear" }
proc UNKNOWN_addpanel {w i} {
    puts "UNKNOWN_addpanel called--shouldn't happen"
}
proc UNKNOWN_updateentry {w i} { 
    puts "UNKNOWN_updateentry called--shouldn't happen" 
}
proc UNKNOWN_setup {i} { puts "UNKNOWN_setup called--shouldn't happen" }


#
# sets up common stuff for queues which use auto_filter
#
proc FILTERED_updateentry {i filter} {
    global printer_info
    global auto_printer auto_resolution auto_paper_size auto_flag
    global auto_printerdb_entry auto_color auto_crlf_trans
    global suppress_headers
    global auto_eof auto_texteof auto_pseof auto_sendeof

    set is_auto [string trim [lindex [split $filter "-"] 0]]
    if {$filter != ""} {
	if {$is_auto != $auto_flag} {
	    set_printcap_field $i "if=" $filter
	} else {
	    set_printcap_field $i "if=" $auto_flag
	}
    } else {
	remove_printcap_field $i "if="
    }
    set_printcap_switch $i "sh" $suppress_headers
    if {$is_auto == $auto_flag} {
	set printer_info($i) [list $auto_printer $auto_resolution \
				  $auto_paper_size $auto_eof $auto_printerdb_entry $auto_color $auto_crlf_trans]
    } else {
	set printer_info($i) ""
    }
}


proc FILTERED_setup {i} {
    global auto_printer auto_resolution auto_paper_size auto_flag
    global auto_printerdb_entry auto_color auto_crlf_trans
    global auto_rev_pages
    global filtersrcdir
    global libexecdir
    global desired_print_format
    global printer_type printer_names
    global auto_ascps_trans
    global auto_eof auto_pseof auto_texteof auto_sendeof
    global auto_GSextra
    global auto_nup auto_rtlft_margin auto_topbot_margin
    global auto_ppdfile
    global xref
    global printerdb_descr
    global ppddir
    global locale

    # MLP
    # See if we have a matching PPD file

    if {[info exists auto_printerdb_entry] && ($auto_printerdb_entry != "")} {
	set auto_ppdfile ""
	set printer_description $printerdb_descr($auto_printerdb_entry)

	set pos [string last " (Postscript)" $printer_description]
	incr pos -1
	if {$pos >= 0} {
	    set printer_description [string range $printer_description 0 $pos]
	}

	if {[info exists xref($printer_description)]} {
	    set auto_ppdfile $xref($printer_description)

	    # MLP - Determine ppd directory including locale subdirectory
	    get_locale
	    if {[file exists "$ppddir$locale/$auto_ppdfile"]} {
		set ppdfile "$ppddir$locale/$auto_ppdfile"
	    } else {
		set ppdfile "$ppddir$auto_ppdfile"
	    }

	    set_printcap_field $i "ppdfile=" "$ppdfile"
	}

    } else {
	set auto_ppdfile ""
    }

    # End MLP

    set i_filter [printcap_field $i "if=" ""]
    if {$i_filter == $auto_flag} {
	set spool_dir [printcap_field $i "sd=" ""]
	set i_filter "$spool_dir/filter"
	set_printcap_field $i "if=" $i_filter
	catch {exec rm -f $i_filter}
#
#   first copy the master filter into place
#
#	puts "cp'ing $filtersrcdir/master-filter to $i_filter"
#	catch {exec cp "$filtersrcdir/master-filter" $i_filter}
#	catch {exec chmod 755 $i_filter}

#   first put a link to the master filter into place
#
        catch {exec ln -s "$libexecdir/master-filter" $i_filter}

#
#   create the appropriate queue specific fpi config files
#

#
#   see if we need postscript or not
#
	if {$auto_printer != "TEXT"} {
	    set desired_print_format "ps"
        } else {
            set desired_print_format "asc"
            set auto_ascps_trans "NO"
	}

	# MLP: add printcap fields for all the former
	# general.cfg variables:
	set_printcap_field_if_not_null $i "PAPERSIZE=" "$auto_paper_size"
	set_printcap_field_if_not_null $i "DESIRED_TO=" "$desired_print_format"
	set_printcap_field_if_not_null $i "PRINTER_TYPE=" "$printer_type($i)"
	set_printcap_field_if_not_null $i "ASCII_TO_PS=" "$auto_ascps_trans"
	#------------------------------------------------------

	#catch {exec sed -e "s/@@@desiredto@@@/$desired_print_format/g" \
	#	-e "s/@@@papersize@@@/$auto_paper_size/g" \
	#	-e "s/@@@printertype@@@/$printer_type($i)/g" \
	#	-e "s/@@@ascps_trans@@@/$auto_ascps_trans/g" \
	#	< $filtersrcdir/general.cfg.in > $spool_dir/general.cfg}
 	#catch {exec chmod 755 $spool_dir/general.cfg}

#
#   setup postscript/ghostscript filter
#

#
#   fixup color param
#
	if {$auto_color == "Default"} {
	    set tauto_color ""
        } elseif {$auto_printer == "uniprint"} {
	#  if uniprint, COLOR is set to filename of filename.upp
	    set tauto_color "$auto_color"
	} else {
	#  if color setting for traditional drivers
	    set tauto_color "-dBitsPerPixel=$auto_color"
	}

	# MLP: add printcap fields for all the former
	# postscript.cfg variables:
	set_printcap_field_if_not_null $i "GSDEVICE=" "$auto_printer"
	set_printcap_field_if_not_null $i "PAPERSIZE=" "$auto_paper_size"
	set_printcap_field_if_not_null $i "RESOLUTION=" "$auto_resolution"
	set_printcap_field_if_not_null $i "COLOR=" "$tauto_color"
	set_printcap_field_if_not_null $i "REVERSE_ORDER=" "$auto_rev_pages"
	set_printcap_field_if_not_null $i "EXTRA_GS_OPTIONS=" "$auto_GSextra"
	set_printcap_field_if_not_null $i "PS_SEND_EOF=" "$auto_pseof"
	set_printcap_field_if_not_null $i "NUP=" "$auto_nup"
	set_printcap_field_if_not_null $i "RTLFTMAR=" "$auto_rtlft_margin"
	set_printcap_field_if_not_null $i "TOPBOTMAR=" "$auto_topbot_margin"
	#------------------------------------------------------

	#if [catch {exec sed -e "s/@@@gsdevice@@@/$auto_printer/g" \
	#	-e "s/@@@papersize@@@/$auto_paper_size/g" \
	#	-e "s/@@@resolution@@@/$auto_resolution/g" \
	#	-e "s/@@@color@@@/$tauto_color/g" \
	#	-e "s/@@@reversepages@@@/$auto_rev_pages/g" \
	#	-e "s^@@@extragsoptions@@@^\"$auto_GSextra\"^g" \
	#	-e "s/@@@pssendeof@@@/$auto_pseof/g" \
	#	-e "s/@@@nup@@@/$auto_nup/g" \
	#	-e "s/@@@rtlftmar@@@/$auto_rtlft_margin/g" \
	#	-e "s/@@@topbotmar@@@/$auto_topbot_margin/g" \
	#	< $filtersrcdir/postscript.cfg.in \
	#	> $spool_dir/postscript.cfg} blah] {
	#    puts "postscript.cfg error: $blah"
	#}
        #catch {exec chmod 755 $spool_dir/postscript.cfg}
    }
#
#  do we need to setup text-only printing?
#
#  yes - we always write this, just in case they want to use
#        native text printing on their printer, and yet have
#        ghostscript print out other formats
#
        set textonly_opt ""

	# MLP: add printcap fields for all the former
	# textonly.cfg variables:
	set_printcap_field_if_not_null $i "TEXTONLYOPTIONS=" "$textonly_opt"
	set_printcap_field_if_not_null $i "CRLFTRANS=" "$auto_crlf_trans"
	set_printcap_field_if_not_null $i "TEXT_SEND_EOF=" "$auto_texteof"
	#------------------------------------------------------

	#catch {exec sed -e "s/@@@textonlyoptions@@@/$textonly_opt/g" \
	#	-e "s/@@@crlftrans@@@/$auto_crlf_trans/g" \
	#	-e "s/@@@textsendeof@@@/$auto_texteof/g" \
	#	< $filtersrcdir/textonly.cfg.in > $spool_dir/textonly.cfg}
	#catch {exec chmod 755 $spool_dir/textonly.cfg} 

# NickM
	#if {[file executable "/usr/sbin/checkpc"] == 1} {
	    #set queue [string trim [lindex [split $printer_names($i) "|"] 0]]
	    #catch {exec /usr/sbin/checkpc -fP $queue}
#
#
	#}
# end NickM
	if {[info exists ppdfile]} {
	    if {! [file exists "$ppdfile"]} {
	    rhs_info_dialog "NOTE: This printer uses a Postscript Printer\
Description\n(PPD) file called $auto_ppdfile,\navailable from the printer\
vendor. This file should be\ninstalled in the $ppddir directory."
            }
	}    
	
}

proc FILTERED_summaryline {i def} { 
    global printer_info
    global printerdb_descr

    set info $printer_info($i)
    if {[llength $info] > 4} {
	set idx [lindex $info 4]
	if { [info exists printerdb_descr($idx)] } {
	    set printer $printerdb_descr($idx)
	} else {
	    rhs_info_dialog \
"Note: The Printer \"$idx\" (specified in\n\
/etc/printap) wasn't found in the printer database.\n\
Check that you have selected the proper Input Filter."
	    set printer $def
	}
    } elseif {[llength $info] >= 1} {
	set printer [lindex $info 0]
    } else {
	set printer $def
    }
    return "$printer"
}



proc FILTERED_addpanel {w i} {
    global printer_names printer_type printer_info printer_entry
    global printer_comments printer_type_list
    global suppress_headers
    global auto_printer auto_resolution auto_paper_size auto_flag
    global printerdb_count printerdb_entry printerdb_GSDriver
    global auto_printerdb_entry
    global auto_color auto_crlf_trans auto_rev_pages
    global auto_eof auto_pseof auto_texteof

    frame $w.filter
    frame $w.filter.r
    frame $w.suppress

    label $w.l10 -text "Input Filter" -anchor w
    button $w.autofilter -text "Select" -command "LOCAL_select_filter $w.v10"

    entry $w.v10 -font fixed -relief sunken -borderwidth 2
    $w.v10 delete 0 end
    $w.v10 insert 0 [printcap_field $i "if=" ""]
    pack $w.autofilter -side left -anchor w  -in $w.filter.r
    pack $w.v10 -side right -anchor e -in $w.filter.r
    pack $w.l10 -side left -anchor w -in $w.filter
    pack $w.filter.r -side right -anchor e -in $w.filter

    checkbutton $w.v11 -text "Suppress Headers" -anchor w \
	-variable suppress_headers
    set suppress_headers [printcap_switch $i "sh"]

#    pack $w.fl9 -side top -fill x -in $w.f1
#    pack $w.v9 -pady 5 -side top -expand true -fill x -in $w.f2
#    pack $w.v10 -side top -expand true -fill x -in $w.f2

    pack $w.v11 -in $w.suppress
    pack $w.filter -fill x -side top -in $w.f1
    pack $w.suppress  -side top -in $w.f1

    # fill in defaults for select_filter panel
    set spool_dir [printcap_field $i "sd=" ""]
    set info [parse_auto_filter_params $i $printer_info($i) $spool_dir]
    set printer_info($i) $info

    if { $auto_printerdb_entry != "" } {
        $w.v10 delete 0 end
        $w.v10 insert 0 $auto_flag
	$w.v10 insert end " - $auto_printerdb_entry"
    }


}



proc LOCAL_summaryline {i} { 
    global printer_info
    global printerdb_descr
    
    set printer [FILTERED_summaryline $i "local printer"]
    set port [printcap_field $i {lp=} {unspecified device}]
    return "$printer on $port"
}

proc LOCAL_name {} { return "Local Printer" }

proc LOCAL_addpanel {w i} {
    global printer_names printer_type printer_info printer_entry
    global printer_comments printer_type_list
    global suppress_headers
    global auto_printer auto_resolution auto_paper_size auto_flag
    global printerdb_count printerdb_entry printerdb_GSDriver
    global auto_printerdb_entry
    global auto_color auto_crlf_trans auto_rev_pages
    global auto_eof auto_pseof auto_texteof


    frame $w.dev

    label $w.l8 -text "Printer Device" -anchor w
    entry $w.v8 -font fixed -relief sunken -borderwidth 2
    $w.v8 insert 0 [printcap_field $i "lp=" ""]

#
#   if no printer device specified, lets suggest one
#
    set lp0_stat "Not detected"
    set lp1_stat "Not detected"
    set lp2_stat "Not detected"
    set usb_lp0_stat "Not detected"
    set usb_lp1_stat "Not detected"
    set usb_lp2_stat "Not detected"

    if { [$w.v8 get] == "" } {
	if { [catch {open /dev/lp0 w} junkfp] == 0} {
	    set lp0_stat "Detected"
	    if { [$w.v8 get] == ""} {
		$w.v8 insert 0 "/dev/lp0"
	    }
	    close $junkfp
	}
	if { [catch {open /dev/lp1 w} junkfp] == 0} {
	    set lp1_stat "Detected"
	    if { [$w.v8 get] == ""} {
		$w.v8 insert 0 "/dev/lp1"
	    }
	    close $junkfp
	}
	if { [catch {open /dev/lp2 w} junkfp] == 0} {
	    set lp2_stat "Detected"
	    if { [$w.v8 get] == ""} {
		$w.v8 insert 0 "/dev/lp2"
	    }
	    close $junkfp
	}
	if { [catch {open /dev/usb/lp0 w} junkfp] == 0} {
	    set usb_lp0_stat "Detected"
	    if { [$w.v8 get] == ""} {
		$w.v8 insert 0 "/dev/usb/lp0"
	    }
	    close $junkfp
	}
	if { [catch {open /dev/usb/lp1 w} junkfp] == 0} {
	    set usb_lp1_stat "Detected"
	    if { [$w.v8 get] == ""} {
		$w.v8 insert 0 "/dev/usb/lp1"
	    }
	    close $junkfp
	}
	if { [catch {open /dev/usb/lp2 w} junkfp] == 0} {
	    set usb_lp2_stat "Detected"
	    if { [$w.v8 get] == ""} {
		$w.v8 insert 0 "/dev/usb/lp2"
	    }
	    close $junkfp
	}


	# Since this message can be confusing, I'm only
	# presenting it when NO lp devices (lp[0-2]) are
	# present.

	if {(($lp0_stat == "Not detected") && \
	     ($lp1_stat == "Not detected") && \
             ($lp2_stat == "Not detected")) \
	     && \
            (($usb_lp0_stat == "Not detected") && \
             ($usb_lp1_stat == "Not detected") && \
             ($usb_lp2_stat == "Not detected"))} {
	    rhs_info_dialog "Printtool found the following devices:

         /dev/lp0 :      $lp0_stat
         /dev/lp1 :      $lp1_stat
         /dev/lp2 :      $lp2_stat
         /dev/usb/lp0 :  $usb_lp0_stat
         /dev/usb/lp1 :  $usb_lp1_stat
         /dev/usb/lp2 :  $usb_lp2_stat

You may disregard this message 
if you are setting up a serial printer.

This port detection may not always work
on Sparc and Alpha architectures.

If no devices were found, this could
indicate a hardware problem that justifies
further investigation.

If you have a USB printer, but it was not
detected, make sure that the USB printer
module has been inserted and is working."
        }
    }   

    pack $w.l8 -side left -anchor w -in $w.dev
    pack $w.v8 -side right -anchor e -in $w.dev
    pack $w.dev -side top -fill x -in $w.f1

#    pack $w.l8 -pady 3 -side top -fill x -in $w.f1
#    pack $w.v8  -side top -expand true -fill x -in $w.f2

    FILTERED_addpanel $w $i

}

#
# handles picking a new printer type in print filter window.
#
# updates description, resolution, color depth, and paper size listboxes based
# on the printer db entry
#
# you pass it the index of printre type in printerdb_entry()
# and names of the various widgets
#
proc LOCAL_select_printer_type {index printer about res color paper asc2ps window_opened} {

    global printerdb_entry printerdb_descr
    global printerdb_about printerdb_GSDriver
    global printerdb_res   printerdb_color
    global printerdb_descr2entry
    global printerdb_count
    global paper_size_list
    global auto_paper_size
    global auto_color
    global auto_resolution
    global auto_nup
    global auto_ascps_trans
    global current_printer_type auto_sendeof
    global tmp_auto_sendeof
    global autodetecting trigger_2

#   set the printer type selection
    $printer selection clear 0 end
    $printer selection set $index
    $printer see $index

#    set pentry $printerdb_entry([$printer curselection])
    set pentry $printerdb_descr2entry([$printer get $index])

    # Any time the user selects a new Postscript printer
    # filter when setting up a LOCAL printer, we want
    # to default to "SEND EOF".  The $window_opened variable
    # prevents the initial setting of "SEND EOF" from 
    # getting reset when the window is first displayed.
    # The trigger_2 variable indicates whether OK or Cancel
    # has been pressed 
    if { ($current_printer_type == "LOCAL") && (($window_opened == 0) || ($autodetecting == 1) && ($trigger_2 == 0)) } {
	if { $printerdb_GSDriver($pentry) == "POSTSCRIPT" } {
	    set tmp_auto_sendeof "YES"
	} else {
	    set tmp_auto_sendeof "NO"
	} 
    }

#
#   handle nup printing config
#
#   if this is the PostScript device, have to do this.
#   Disable 'Fast printing...' cause it doesn't apply.
#
    if {$pentry == "PostScript"} {
	$asc2ps configure -state disabled
    } else {
	$asc2ps configure -state normal
    }	


#   update the 'about' info
    $about config -state normal
    $about delete 1.0 end
#    $about insert 1.0 $printerdb_about($pentry)

#
#   wish I knew better, but how else to break lines in text widget??
#
    set abstr $printerdb_about($pentry)
    while { 1 == 1 } {
	set nlindex [string first "\\n" $abstr]
	if { $nlindex == -1 } {
	    $about insert end $abstr
	    break
	}
	incr nlindex -1
	$about insert end [string range $abstr 0 $nlindex]
	$about insert end "\n"
	incr nlindex 3
	set abstr [string trimleft [string range $abstr $nlindex end]]
    }

    $about config -state disabled
#
#   rebuild resolution list
#
    $res delete 0 end
    if [llength [array names printerdb_res $pentry]] {
	foreach x $printerdb_res($pentry) {
	    set g [format "%4sx%-4s %-20s"  [lindex $x 0] [lindex $x 1] [lindex $x 2]]
	    $res insert end $g
	}
    } else {
	$res insert end [format "%4s%-4s %-20s"  Default {} {}]
    }
    
#   set the resolution to the first available
#    $res selection clear 0 end
#    $res selection set 0
#
#   set resolution to
#
    if { [string compare $auto_resolution "Default"] == 0 } {
	set rresentry 0
    } elseif { [array names printerdb_res $pentry] == {} } {
	set rresentry 0
    } else {	
	set rresentry 0
	set reslist [split $auto_resolution x]
	set resxdpi [lindex $reslist 0]
	set resydpi [lindex $reslist 1]
	for {set i 0} {$i < [llength $printerdb_res($pentry)]} {incr i} {
	    set curres [lindex $printerdb_res($pentry) $i]
	    if { [lindex $curres 0] == $resxdpi && [lindex $curres 1] == $resydpi } {
		set rresentry $i
		break
	    }
	}
    }
    $res selection clear 0 end
    $res selection set $rresentry
    $res see $rresentry
    
#
#   rebuild color list
#
    $color delete 0 end

#
# ugly - if no color info assume default
#
    if [llength [array names printerdb_color $pentry]] {
    	foreach x $printerdb_color($pentry) {
	    if { $printerdb_GSDriver($pentry) == "uniprint" } {
	        # it's a uniprint upp entry
		# check to make sure the upp file exists
	        set fname [ format "%s.upp" [lindex $x 0] ]

	    	if { ([ glob -nocomplain "/usr/share/ghostscript/*/$fname" ] != "") || ([ glob -nocomplain "/usr/lib/ghostscript/*/$fname" ] != "") } {
		    set g [format "%8s, %-30s"  [lindex $x 0] [lindex $x 1]]
		    $color insert end $g
		}
	    } else {
		    set g [format "%8s, %-30s"  [lindex $x 0] [lindex $x 1]]
		    $color insert end $g
	    }
	}
    } else {
	$color insert end [format "%4s %-30s"  Default {}]
    }


#   set the color depth to the first available
#    $color selection clear 0 end
#    $color selection set 0
#
#       if this is default, then set accordingly
#
    if { [string compare $auto_color "Default"] == 0 } {
	set rcolentry 0
    } elseif { [array names printerdb_color $pentry] == {} } {
	set rcolentry 0
    } else {	
	set rcolentry 0
	for {set i 0} {$i < [llength $printerdb_color($pentry)]} {incr i} {
	    set colentry [lindex $printerdb_color($pentry) $i]
	    if { [lindex $colentry 0] == $auto_color } {
		set rcolentry $i
		break
	    } 
	}
    }
    $color selection clear 0 end
    $color selection set $rcolentry
    $color see $rcolentry

#   set the paper to the first available
#    $paper selection clear 0 end
#    $paper selection set 0
# 
#  if there is no active selection in the paper size listbox
#  make the current paper size the selection
#
    set papselect [$paper curselection]
    if { $papselect != "" } {
	set papentry [$paper get $papselect]
	set papindex [lsearch $paper_size_list $papentry]
	if { $papindex == -1 } {
	    set papindex 0
	}
    } else {
	set papindex [lsearch $paper_size_list $auto_paper_size]
	if { $papindex == -1 } {
	    set papindex 0
	}
    }
    $paper selection clear 0 end
    $paper selection set $papindex
    $paper see $papindex
}

proc enable_widgets {args} {
    puts "enabling widgets"
    foreach w $args {
	$w configure -state normal
    }
}

proc disable_widgets {args} {
    puts "disabling widgets"
    foreach w $args {
	$w configure -state disabled
    }
}


proc LOCAL_select_filter {e} {
    global trigger_2
    global paper_size_list
    global auto_printer auto_resolution auto_paper_size auto_flag

#
# newer printerdb globals, some supercede some above
#
    global printerdb_entry printerdb_descr
    global printerdb_about printerdb_GSDriver
    global printerdb_res   printerdb_color
    global printerdb_count
    global printerdb_descr2entry
    global auto_printerdb_entry
    global auto_color
    global auto_crlf_trans tmp_auto_crlf_trans
    global gs_installed_drivers
    global auto_rev_pages tmp_auto_rev_pages
    global auto_ascps_trans tmp_auto_ascps_trans
    global auto_texteof tmp_auto_texteof
    global auto_pseof tmp_auto_pseof
    global auto_sendeof tmp_auto_sendeof
    global auto_nup tmp_auto_nup
    global auto_rtlft_margin
    global auto_topbot_margin
    global auto_GSextra

#    puts "On entry ..."
#    puts "Selected GSDriver      -> $auto_printer"
#    puts "Selected printer entry -> $auto_printerdb_entry"
#    puts "Selected resolution    -> $auto_resolution"
#    puts "Selected color         -> $auto_color"
#    puts "Selected paper size    -> $auto_paper_size"
#    puts "Selected eof setting   -> $auto_eof"
#    puts "Selected cr/lf trans   -> $auto_crlf_trans"
#    puts "Selected reverse pages -> $auto_rev_pages"


    toplevel .sf
    wm withdraw .sf
#    wm transient .sf .
    wm group .sf .
    wm title .sf "Configure Filter"

#
# create these in an order that makes stacking work out right
#
# .cf's are container frames
#
    frame .sf.cf1
    frame .sf.cf2
    frame .sf.cf3
    frame .sf.cf4

    frame .sf.f1
    frame .sf.f2
    frame .sf.f5
    frame .sf.f3
    frame .sf.f4
    frame .sf.f6
    frame .sf.f7
    frame .sf.f8
    frame .sf.f10

#
# trying something different - define vars with names of Tk widgets here
#  then use names later on. We can allocate names as needed

#
# First we'll handle the listbox containing the printer drivers available
#
    label .sf.f1.l1 -text "Printer Type" -anchor w -width 12
    listbox .sf.f1.list -yscrollcommand [list .sf.f1.sy set] \
                        -width 40 -height 20 -setgrid true -selectmode single \
			-exportselection false

    scrollbar .sf.f1.sy -orient vertical -command [list .sf.f1.list yview]

#
# we want to display a sorted list of descriptions, but lets not
# change the order of the description array
#
    set sorted_descr ""
    for {set i 0} {$i < $printerdb_count} {incr i} {
	lappend sorted_descr $printerdb_descr($printerdb_entry($i))
    }
    set sorted_descr [lsort $sorted_descr]
    set existing_descr ""
    for {set i 0} {$i < $printerdb_count} {incr i} {
	set this_descr [lindex $sorted_descr $i]
	set this_entry $printerdb_descr2entry($this_descr)
	set this_gsdriver $printerdb_GSDriver($this_entry)
	if { [lsearch $gs_installed_drivers $this_gsdriver] != -1 } {
	    .sf.f1.list insert end $this_descr
	    lappend existing_descr $this_descr
	} 

    }
    pack .sf.f1.sy -side right -fill y
    pack .sf.f1.l1 -side top -fill x
    pack .sf.f1.list -side left -fill both -expand true

#
# set binding for selection in printer type window
#
    bind .sf.f1.list <Button-1> "
    LOCAL_select_printer_type \[.sf.f1.list nearest %y\] .sf.f1.list .sf.f2.text .sf.f3.list .sf.f6.list .sf.f4.list .sf.c3 0"
#
# Now add a verbose description of the driver
#
# we disable the text widget so it is read-only
#
    label .sf.f2.l1 -text "Driver Description" -anchor w -width 20
    text .sf.f2.text -relief sunken -setgrid true -wrap word \
                     -width 35 -height 4 -yscrollcommand ".sf.f2.sy set" \
		     -font  -adobe-helvetica-medium-r-normal--12*
    scrollbar .sf.f2.sy -orient vert -command ".sf.f2.text yview"
    .sf.f2.text insert 1.0 "Should be initializing this elsewhere!!"
    .sf.f2.text config -state disabled
    pack .sf.f2.sy -side right -fill y -in .sf.f2
    pack .sf.f2.l1      -side top -fill x -in .sf.f2
    pack .sf.f2.text -side left -fill both -expand true -in .sf.f2

#
# Now add resolution/paper size/color depth selections
#
    label .sf.f3.l1 -text "Resolution" -anchor w -width 12
    listbox .sf.f3.list -yscrollcommand [list .sf.f3.sy set] \
                        -width 20 -height 3 -setgrid true -selectmode single \
			-exportselection false -font fixed

    scrollbar .sf.f3.sy -orient vertical -command [list .sf.f3.list yview]
#    foreach x $resolution_list {
#	.sf.f3.list insert end $x
#    }
    pack .sf.f3.sy -side right -fill y
    pack .sf.f3.l1 -side top -fill x
    pack .sf.f3.list -side left -fill both -expand true

    label .sf.f4.l1 -text "Paper Size" -anchor w -width 12
    listbox .sf.f4.list -yscrollcommand [list .sf.f4.sy set] \
                        -width 7 -height 3 -setgrid true -selectmode single \
			-exportselection false -font fixed
    scrollbar .sf.f4.sy -orient vertical -command [list .sf.f4.list yview]
    foreach x $paper_size_list {
	.sf.f4.list insert end $x
    }
#

#  make the current paper size the selection
#
#    set papindex [lsearch $paper_size_list $auto_paper_size]
#    if { $papindex == -1 } {
#	set papindex 0
#    }
#    .sf.f4.list selection clear 0 end
#    .sf.f4.list selection set $papindex

#fix paper to get initialized color too

    pack .sf.f4.sy -side right -fill y
    pack .sf.f4.l1 -side top -fill x
    pack .sf.f4.list -side left -fill both -expand true


    label .sf.f6.l1 -text "Color Depth / Uniprint Mode" -anchor w -width 12
    listbox .sf.f6.list -yscrollcommand [list .sf.f6.sy set] \
                        -width 60 -height 4 -setgrid true -selectmode single \
			-exportselection false -font fixed

    scrollbar .sf.f6.sy -orient vertical -command [list .sf.f6.list yview]
    set color_list {1 3 8 16 24 43}
    foreach x $color_list {
	.sf.f6.list insert end $x
    }
    pack .sf.f6.sy -side right -fill y
    pack .sf.f6.l1 -side top -fill x
    pack .sf.f6.list -side left -fill both -expand true
#
# put these into the container frame
#
    pack .sf.f3 -side left -expand yes -anchor w -ipadx 10 -in .sf.cf1 
    pack .sf.f4 -side left -expand yes -anchor e -ipadx 10 -in .sf.cf1 
    pack .sf.cf1 .sf.f6 -side top -expand yes -ipadx 10 -in .sf.cf4 

#
# checkbutton to control if EOF is added to output
#
    label .sf.l10 -text "Printing Options" -anchor nw \
	    -font -adobe-helvetica-bold-r-normal--12*
    set tmp_auto_crlf_trans $auto_crlf_trans
    checkbutton .sf.c2 -text "Fix stair-stepping text?" \
	-variable tmp_auto_crlf_trans -offvalue "" -onvalue 1 -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--12*
    set tmp_auto_sendeof $auto_sendeof
    checkbutton .sf.c4 -text "Send EOF after job to eject page?" \
	-variable tmp_auto_sendeof -offvalue "NO" -onvalue "YES" -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--12*

#
# number of input pages per output page
#
    set LabNup ".sf.nupl11"
    set RBNup1 ".sf.nuprb1"
    set RBNup2 ".sf.nuprb2"
    set RBNup4 ".sf.nuprb4"
    set RBNup8 ".sf.nuprb8"
    set FrNup  ".sf.f9"
    
    frame $FrNup
    set tmp_auto_nup $auto_nup
    label $LabNup -text "pages per output page."  -anchor w \
	    -font -adobe-helvetica-medium-r-normal--12*

    radiobutton $RBNup1 -variable tmp_auto_nup -text "1" -value 1 -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--12*
    radiobutton $RBNup2 -variable tmp_auto_nup -text "2" -value 2 -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--12*
    radiobutton $RBNup4 -variable tmp_auto_nup -text "4" -value 4 -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--12*
    radiobutton $RBNup8 -variable tmp_auto_nup -text "8" -value 8 -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--12*
    pack $LabNup $RBNup1 $RBNup2 $RBNup4 $RBNup8 -side right -in $FrNup \
	    -fill none -anchor w

#
# margins used by mpage if asc->ps is selected
#
    set MarLab        ".sf.marlab"
    set MarTopBotLab  ".sf.marentlabtb"
    set MarRtLftLab   ".sf.marentlabrl"
    set MarTopBot ".sf.marenttb"
    set MarRtLft  ".sf.marentrl"
    set MarRtLftEnt ".sf.marrtlftent"
    set MarTopBotEnt ".sf.martopbotent"

    set FrMar     ".sf.marfr"
    set FrMarLab  ".sf.marfrlab"
    set FrMarEnt1 ".sf.marfrent1"
    set FrMarEnt2 ".sf.marfrent2"
    set FrMarEnt  ".sf.marfrent"

    frame $FrMar
    frame $FrMarLab
    frame $FrMarEnt
    frame $FrMarEnt1
    frame $FrMarEnt2

    label $MarLab  -text "Margins (in pts=1/72 of inch)" -anchor nw \
	    -font -adobe-helvetica-bold-r-normal--12*

    label $MarTopBotLab -text "Top/Bottom:" -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--10*

    label $MarRtLftLab -text "Right/Left:" -anchor nw \
	    -font -adobe-helvetica-medium-r-normal--10*
    
    set tmp_auto_rtlft_margin $auto_rtlft_margin
    set tmp_auto_topbot_margin $auto_topbot_margin

    entry $MarRtLftEnt -font fixed -relief sunken -borderwidth 2 -width 6
    $MarRtLftEnt insert 0 $auto_rtlft_margin

    entry $MarTopBotEnt -font fixed -relief sunken -borderwidth 2 -width 6
    $MarTopBotEnt insert 0 $auto_topbot_margin
    
#    pack $MarLab -in $FrMarLab -anchor nw
    pack $MarRtLftLab $MarRtLftEnt  -side left -in $FrMarEnt1 \
	    -anchor w -fill none
    pack $MarTopBotLab $MarTopBotEnt -side left -in $FrMarEnt2 \
	    -anchor w -fill none
    pack $FrMarEnt2 $FrMarEnt1 -in $FrMarEnt -side right -fill none

#    pack $FrMarLab $FrMarEnt -side bottom -in $FrMar
#
# converting ascii to ps? (essentially, use mpage?)
#
    set CBasc2ps ".sf.c3"
    set tmp_auto_ascps_trans $auto_ascps_trans
    checkbutton $CBasc2ps -text "Fast text printing (non-PS printers only)?" \
	-variable tmp_auto_ascps_trans \
	-offvalue "YES" -onvalue "NO" -anchor nw \
	-font -adobe-helvetica-medium-r-normal--12* 

#
# add those promised GS options
#
    set LabGSextra ".sf.gsextlab"
    set EntGSextra ".sf.gsextent"
    set FrGSextra  ".sf.gsextfr"

    frame $FrGSextra

    label $LabGSextra -text "Extra GS options:" -anchor w \
	    -font -adobe-helvetica-bold-r-normal--10*

    entry $EntGSextra -relief sunken -borderwidth 2 -width 35 \
	    -font -adobe-helvetica-bold-r-normal--10*

    $EntGSextra insert 0 $auto_GSextra
    pack $LabGSextra $EntGSextra  -side left -in $FrGSextra \
	    -anchor w -fill none
    
#    set tmp_auto_pseof $auto_pseof
#    checkbutton .sf.c1 -text "Send EOF (\\004) after job?" \
#	-variable tmp_auto_pseof -offvalue "NO" -onvalue "YES" \
#	    -font -adobe-helvetica-medium-r-normal--12*
#
# we will support reversing order in future versions
# requires using 'psorder' program which comes with netatalk pkg
#
#    set tmp_auto_rev_pages $auto_rev_pages
#    checkbutton .sf.c3 -text "non Text-only printers: Reverse page order" \
#	-variable tmp_auto_rev_order -offvalue "" -onvalue 1
#    pack .sf.c3 .sf.c2 .sf.c1 -side top -in .sf.f7
#    pack .sf.c3 .sf.c1 .sf.l11 .sf.c2 .sf.l10 -side bottom -in .sf.f7

    pack  $FrGSextra -in .sf.f7 -side bottom -anchor w
    pack  $FrMarEnt -in .sf.f7 -side bottom -anchor w
    pack  $MarLab -in .sf.f7 -fill x -side bottom -anchor w
    pack  $FrNup  -in .sf.f7 -fill none -anchor w -side bottom
    pack  .sf.c3 -side bottom -fill x -anchor w -in .sf.f7
    pack  .sf.c2 .sf.c4 .sf.l10  -side bottom -fill x -in .sf.f7

#
# add buttons to exit this dialog
#
    button .sf.b1 -text "OK" -width 10 -command "set trigger_2 1" -anchor s
    button .sf.b2 -text "Cancel" -width 10 -command "set trigger_2 0" -anchor s
    button .sf.b3 -text "HELP" -width 10 -anchor s -command { help_dialog "Help for selecting a print filter" "

Help for selecting a print filter
---------------------------------

First choose the type of printer you have:

  Choose 'Text-only' if you only want to print text (ASCII).

  Choose 'PostScript' if you have a PostScript capable printer.

  Otherwise, find a printer in the listing which is close to the 
  model you own. This will configure the print system to use
  ghostscript for that type of printer.

  Many printers are compatible with other brands;
  for example, many printers are compatible with HP printers.
  Try different drivers and then print a PostScript test page.
  Some experimentation may be required. It is advisable to be
  near the printer you are configuring, as if the wrong driver
  is chosen you will want to be nearby to turn off the printer
  if it starts printing garbage.

Second, choose the Paper Size for your printer:

  Note that not all printers support all the listed paper sizes.
  Also, some printers require adjustments before a different size
  paper can be used.

Third, choose the Resolution and Color Depth:

  The resolution controls the quality of the output of the printer.
  Some printers only support one resolution. Others, like 
  Epson dot-matrix printers, support a variety of resolutions.
  Read the description of the printer for extra information on
  resolution and color options.

Final notes:

The \"Printing Options\" -

  \"Send EOF after job to eject page?\" -
      Forces the printer to eject the page when a printer job is done. 
      Some printers will not print until this character is received. 
      If you find you get an extra blank page, try turning this 
      option off.

  \"Fix stair-stepping text?\" -
      Turn this on if you are getting stair-stepped text.

  These options DO NOT apply if your printer is configured 'Text-only'.

  \"Fast text printing (non-PS printers only)?\" -
      Use when setting up a non-PostScript printer.
      If this option is ON, then text (ASCII) files will be sent
      straight to the printer. This will normal result in much
      faster printing than if this option is set OFF. In that
      case, the text input file will be sent to the program 'mpage',
      converted into PostScript, then sent to ghostscript to be printed.

      NOTE - The disadvantage to turning this option on is that you 
      cannot print multiple input pages per output page. That requires
      all input go through the 'mpage' program.

  \"8/4/2/1 pages per output page.\" -
      This option allows you to print multiple input pages per output
      page. If you have selected the \"Fast Text Printing\" option,
      however, this option will not work. By selected the \"Fast Text...\"
      option, you have told the print filter to not use 'mpage', which
      is required to get mulitple input pages per output page.

  \"Margins\" -
      You can adjust the margins of the page if you have selected to
      print 8, 4, or 2 pages per output page. If you have chosen 1
      output page per input page, you cannot adjust the margins at
      this time. This is due to a limitation of 'mpage' in handling
      arbitrary PostScript input.

  \"Extra GS options\" -
      Some ghostscript drivers, like the Epson Stylus driver, can accept
      additional options to control the print quality generated
      by the driver. You can put those options here. For example, on
      the Epson Stylus Color driver, the options:

       -sDithering=fscmyk /usr/share/ghostscript/4.03/stcolor.ps

      can result in better looking color. This requires gs 4.03.
      

What does the print filter do?
------------------------------

  If you have selected a 'Text-only' printer type, the filter simply
sends whatever you print straight to the printer.

  Otherwise, the print system is configured to generate PostScript
output from whatever type of file you print, if possible. An example
of where this conversion occurs is when you print a TeX .dvi file.
If you have dvips installed on your system, the .dvi file you printed
will automatically be converted into the PostScript format, then
sent to the printer. If your printer requires ghostscript to print
PostScript, ghostscript will be run automatically.

  Depending on what software you have installed, the print system
will attempt to convert the files you print into a format your
printer can handle. If the print system is unable to print the file,
it will instead print a diagnostic message to this effect.

  Also, the print system rejects printing files which normally you
wouldn't mean to print, like an ELF executable. These will be rejected
and a diagnostic page will be printed instead.

  If you would like to setup a printer queue which prints whatever is
sent to it without the print system attempting a conversion, setup a
separate queue without a print filter. You may like to call it
\"raw\", and to print to it you would use the command

lpr -Praw <filename>

" }

    pack .sf.b1 .sf.b2 .sf.b3 -side left -expand true -fill both  -ipady 1 \
	    -in .sf.f8 -anchor s

#
# stack printer list box and ok/canel buttons
#
    pack .sf.f1 -side top -expand yes -fill y -in .sf.cf3
    pack .sf.f8 -side top -expand yes -fill none -in .sf.cf3 -anchor s
#
# put printer description to the side and on top of res/paper size/color
#
    pack .sf.f7 -side bottom -fill x -in .sf.cf2
    pack .sf.f2 .sf.cf4 -side top -fill x -in .sf.cf2
    pack .sf.cf3 .sf.cf2 -side left -fill x -in .sf
    

#
#   select entry from auto_printerdb_entry if available
#

if [catch {set curdescr $printerdb_descr($auto_printerdb_entry)}] {
    set curent 0
} else {
    set curent [lsearch $existing_descr $curdescr]
}

    LOCAL_select_printer_type $curent .sf.f1.list .sf.f2.text .sf.f3.list .sf.f6.list .sf.f4.list $CBasc2ps 1

    wm deiconify .sf
    grab set .sf
    wm protocol .sf WM_DELETE_WINDOW { set trigger_2 0 }
    update
    scan [wm geometry .sf] "%d%*c%d" xmin ymin
    wm minsize .sf $xmin $ymin
    wm maxsize .sf $xmin $ymin
    tkwait variable trigger_2
    if {$trigger_2 == 1} {
	set pentry $printerdb_descr2entry([.sf.f1.list get [.sf.f1.list curselection]])
	set auto_printer $printerdb_GSDriver($pentry)
	set auto_printerdb_entry $pentry
	set auto_paper_size [lindex $paper_size_list [.sf.f4.list curselection]]
	set resindex [.sf.f3.list curselection]
	set resentry [lindex $printerdb_res($pentry) $resindex]
	set auto_resolution [format "%sx%s" [lindex $resentry 0] [lindex $resentry 1]]
#
#       get the color selection
#
	set colindex [.sf.f6.list curselection]
#
#       if this is default, then set accordingly
#
	set colentry [string trim [.sf.f6.list get $colindex]]
	set colentry [lindex [split $colentry] 0]
	if { [string compare $colentry "Default"]  } {
	    set colentry [lindex $printerdb_color($pentry) $colindex]
	    set auto_color [lindex $colentry 0]
	} else {
	    set auto_color "Default"
	}

#
# We handle eof more intellgently, depending on what kind of printer
# they have chosen
#

	set auto_sendeof $tmp_auto_sendeof

	# MLP: Don't know why this was originally only set when
	# the box was checked. Seems to prevent an unchecked box
	# value from being saved.
#	if {$tmp_auto_sendeof == "YES"} {
	    if {$auto_printer == "POSTSCRIPT"} {
		set auto_pseof $tmp_auto_sendeof
		set auto_texteof "NO"
	    } else {
		set auto_texteof $tmp_auto_sendeof
		set auto_pseof "NO"
	    }
#	}
	set tmp_auto_pseof $auto_pseof
	set tmp_auto_texteof $auto_texteof

# stairstep control
	set auto_crlf_trans $tmp_auto_crlf_trans
# fast printing - is a PostScript printer then it has to be OFF
	set auto_ascps_trans $tmp_auto_ascps_trans
	if { $auto_printer == "POSTSCRIPT" }  {
	    if {$tmp_auto_ascps_trans == "NO"}  {
		set auto_ascps_trans "NO"
		set tmp_auto_texteof "YES"
		set auto_texteof "YES"
	    } else {
		set auto_ascps_trans "YES"
	    }
	}
# reverse page order
	set auto_rev_pages $tmp_auto_rev_pages
# extra GS options
	set auto_GSextra [$EntGSextra get]
# margins
	set auto_rtlft_margin [$MarRtLftEnt get]
	set auto_topbot_margin [$MarTopBotEnt get]
# number pages per output page
	set auto_nup $tmp_auto_nup

#	puts "Selected printer entry -> $pentry"
#	puts "Selected GSDriver      -> $auto_printer"
#	puts "Selected printer entry -> $auto_printerdb_entry"
#	puts "Selected resolution    -> $auto_resolution"
#	puts "Selected color         -> $auto_color"
#	puts "Selected paper size    -> $auto_paper_size"
#	puts "Selected eof setting   -> $auto_eof"
#	puts "Selected cr/lf trans   -> $auto_crlf_trans"
#	puts "Selected reverse pages -> $auto_rev_pages"
#
#      this is REAL ugly, but we do it this way cause its how it was
#      put stuff into the select filter entry widget
#
	$e delete 0 end
	$e insert 0 $auto_flag
	$e insert end " - $auto_printerdb_entry"
    }
    destroy .sf
    return $trigger_2
}


proc LOCAL_updateentry {w i} {
    global printer_info
    global auto_printer auto_resolution auto_paper_size auto_flag auto_eof
    global auto_printerdb_entry auto_color auto_crlf_trans
    global suppress_headers

    set_printcap_field $i "lp=" [$w.v8 get]
    set filter [$w.v10 get]
    FILTERED_updateentry $i $filter
}

proc LOCAL_setup {i} {
    global auto_printer auto_resolution auto_paper_size auto_flag auto_eof
    global auto_printerdb_entry auto_color auto_crlf_trans
    global auto_rev_pages
    global desired_print_format
#
# nothing specific to a local printer to do, so call generic function
#
    FILTERED_setup $i
}

proc REMOTE_summaryline {i} { 
    set queue [printcap_field $i "rp=" "lp"]
    set host [printcap_field $i "rm=" "(unspecified host)"]
    return "REMOTE lpd queue $queue on $host"
}

proc REMOTE_name {} { return "Remote Unix (lpd) Queue" }

proc REMOTE_addpanel {w i} {

    frame $w.rhost
    frame $w.rqueue

    label $w.l5 -text "Remote Host" -anchor w
    entry $w.v5 -font fixed -relief sunken -borderwidth 2
    $w.v5 insert 0 [printcap_field $i "rm=" ""]

    label $w.l6 -text "Remote Queue" -anchor w
    entry $w.v6 -font fixed -relief sunken -borderwidth 2
    $w.v6 insert 0 [printcap_field $i "rp=" ""]

#    pack $w.l5 -pady 4 -side top -fill x -in $w.f1
#    pack $w.l6 -pady 3 -side top -fill x -in $w.f1
#    pack $w.v5 $w.v6 -side top -expand true -fill x -in $w.f2

    pack $w.l5  -side left -anchor w -fill x -in $w.rhost
    pack $w.v5  -side right -anchor e -fill x -in $w.rhost
    pack $w.l6  -side left  -anchor w -fill x -in $w.rqueue
    pack $w.v6  -side right  -anchor e -fill x -in $w.rqueue

    pack $w.rhost -side top -fill x -in $w.f1
    pack $w.rqueue -side top -fill x -in $w.f1

    FILTERED_addpanel $w $i
}

proc REMOTE_updateentry {w i} {
    set_printcap_field $i "rm=" [$w.v5 get]
    set_printcap_field $i "rp=" [$w.v6 get]
    set_printcap_field $i "lpd_bounce=" "true"
    set filter [$w.v10 get]

    FILTERED_updateentry $i $filter
}

proc REMOTE_setup {i} {
    FILTERED_setup $i
}


#
# Direct to printer port support added by Joshua Buysse
# Adapted from Miquel de Icaza's SMB support
# 
proc DIRECT_config {i} {
    global auto_direct_printer_ip auto_direct_port

    set spool_dir [printcap_field $i "sd=" "none"]
    set config_file "$spool_dir/.config"

    if {[string compare spool_dir none] == 0} {
        return [list "" ""]
    }

    set tmp_ip [printcap_field $i "printer_ip=" ""]
    set tmp_port [printcap_field $i "port=" ""]

    if { ($tmp_ip != "") && ($tmp_port != "") } {
	set direct_printer_ip "$tmp_ip"
	set direct_port "$tmp_port"
        return [list $direct_printer_ip $direct_port]
    } elseif [file exists $config_file] {
        set fd [open $config_file]      
        if {![regexp "printer_ip=\(.*\)" [gets $fd] dummy direct_printer_ip]} {
            set direct_printer_ip ""
        }

        if {![regexp "port=\(.*\)" [gets $fd] dummy direct_port]} {
             set direct_port ""
        }
        return [list $direct_printer_ip $direct_port]
    }
#    return [list "" ""]
    # MLP
    return [list $auto_direct_printer_ip $auto_direct_port]
}

proc DIRECT_summaryline {i} {
    set printer [FILTERED_summaryline $i "printer"]
    set config [DIRECT_config $i]
    set printer_ip  [lindex $config 0]
    set port [lindex $config 1]
    return "DIRECT - $printer at $printer_ip:$port"
}

proc DIRECT_name {} { return "Direct to port printer" }

proc DIRECT_addpanel {w i} {
    set config [DIRECT_config $i]

    frame $w.printer_ip
    frame $w.port

    label $w.l5 -text "Hostname of Printer" -anchor w
    entry $w.v5 -font fixed -relief sunken -borderwidth 2
    $w.v5 insert 0 [lindex $config 0]

    label $w.l6 -text "Port number" -anchor w
    entry $w.v6 -font fixed -relief sunken -borderwidth 2
    $w.v6 insert 0 [lindex $config 1]
 
    pack $w.l5 -side left -fill x -anchor w -in $w.printer_ip
    pack $w.v5 -side right -fill x -anchor e -in $w.printer_ip
    pack $w.l6 -side left -fill x -anchor w -in $w.port
    pack $w.v6 -side right -fill x -anchor e -in $w.port

    pack $w.printer_ip $w.port -side top -fill x -in $w.f1

    FILTERED_addpanel $w $i
}

proc DIRECT_updateentry {w i} {
    global direct_printer_ip direct_port

    set direct_printer_ip    [$w.v5 get]
    set direct_port         [$w.v6 get]
#
#   get the auto_filter setup first
#
#   it will be the input filter, so it sets the printcap entry 'if='
#
    set filter [$w.v10 get]
    FILTERED_updateentry $i $filter
}

proc DIRECT_setup {i} {
    global direct_printer_ip direct_port
    global auto_flag
    global libexecdir

#
#   now do related setup
#
#
    set spool_dir [printcap_field $i "sd=" ""]
    catch {exec mkdir -p $spool_dir}
    catch {exec chown root.lp $spool_dir}
    catch {exec chmod 775 $spool_dir}

    set direct_config "$spool_dir/.config"
    set_printcap_field $i "af=" "$spool_dir/acct"
    set_printcap_field $i "lp=" /dev/null
    set_printcap_field $i "sh" ""

    set_printcap_field $i "printer_ip=" "$direct_printer_ip"
    set_printcap_field $i "port=" "$direct_port"

    #catch {exec rm -f $direct_config}
    #set ffd [open $direct_config w]
    #puts $ffd "printer_ip=$direct_printer_ip"
    #puts $ffd "port=$direct_port"
    #close $ffd

#
#   see if any filter was specified, and if not, default
#   to a filter that will just send data to the printer
#
    set i_filter [printcap_field $i "if=" ""]
    if {$i_filter != $auto_flag} {
        set_printcap_field $i "if=" $libexecdir/directprint
    } else {
#
#       now setup the auto_filter
#
        FILTERED_setup $i
    }
}


#
# SMB support by Miquel de Icaza, adopted to printtool 3.0 by Michael Fulbright
#
# Added support in 3.1 for '-I' option of samba, by "Teddy"
proc SMB_config {i} {
    set spool_dir [printcap_field $i "sd=" "none"]
    set config_file "$spool_dir/.config"

    if {[string compare spool_dir none] == 0} {
 	return [list "" "" "" ""]
    }
    if [file exists $config_file] {
        set fd [open $config_file]	
        if {![regexp "share='\(.*\)'" [gets $fd] dummy sharename]} {
	    set sharename ""
	}
        if {![regexp "hostip=\(.*\)" [gets $fd] dummy hostip]} {
	    set hostip ""
        }
        if {![regexp "user='\(.*\)'" [gets $fd] dummy user]} {
 	     set user ""
        }
        if {![regexp "password='\(.*\)'" [gets $fd] dummy password]} {
 	     set password ""
        }
        if {![regexp "workgroup='\(.*\)'" [gets $fd] dummy workgroup]} {
 	     set workgroup ""
        }

        set translate no
	if {![regexp {\\\\(.*)\\(.*)} $sharename dummy hostname printername] } {
	    set hostname ""
	    set printername ""
	}
        return [list $hostname $printername $user $password $workgroup $hostip]        
    }
    return [list "" "" "" "" "" ""]
}

proc SMB_summaryline {i} {
    set printer [FILTERED_summaryline $i "printer"]
    set config [SMB_config $i]
    set hostname  [lindex $config 0]
    set printername [lindex $config 1]
    return "SMB - $printer on \\\\$hostname\\$printername"
}

proc SMB_name {} { return "SMB/Windows 95/98/NT Printer" }

proc SMB_addpanel {w i} {
    global smb_crlf

    set smb_crlf 0
    set config [SMB_config $i]

    frame $w.smbhost
    frame $w.smbip
    frame $w.smbname
    frame $w.smbuser
    frame $w.smbpasswd
	frame $w.smbworkgroup

    label $w.l5 -text "Hostname of Printer Server" -anchor w
    entry $w.v5 -font fixed -relief sunken -borderwidth 2
    $w.v5 insert 0 [lindex $config 0]

    label $w.l_hostip -text "IP number of Server (optional)" -anchor w
    entry $w.v6_hostip -font fixed -relief sunken -borderwidth 2
    $w.v6_hostip insert 0 [lindex $config 5]
 
    label $w.l6 -text "Printer Name" -anchor w
    entry $w.v6 -font fixed -relief sunken -borderwidth 2
    $w.v6 insert 0 [lindex $config 1]

    label $w.l7 -text "User" -anchor w
    entry $w.v7 -font fixed -relief sunken -borderwidth 2
    $w.v7 insert 0 [lindex $config 2]

    label $w.l8 -text "Password" -anchor w
    entry $w.v8 -show "*" -font fixed -relief sunken -borderwidth 2
    $w.v8 insert 0 [lindex $config 3]
	
	label $w.l9 -text "Workgroup" -anchor w
	entry $w.v9 -font fixed -relief sunken -borderwidth 2
	$w.v9 insert 0 [lindex $config 4]


#
# we'll handle cr/lf translation through the print filter if necessary
#
    set smb_crlf 0

#    pack $w.l5 -pady 4 -side top -fill x -in $w.f1
#    pack $w.l_hostip -pady 4 -side top -fill x -in $w.f1
#    pack $w.l6 -pady 3 -side top -fill x -in $w.f1
#    pack $w.l7 -pady 4 -side top -fill x -in $w.f1
#    pack $w.l8 -pady 4 -side top -fill x -in $w.f1

##    pack $w.v5 $w.v6 -side top -expand true -fill x -in $w.f2
#    pack $w.v5 $w.v6_hostip $w.v6 $w.v7 $w.v8 -side top -expand true -fill x -in $w.f2

    pack $w.l5 -side left -fill x -anchor w -in $w.smbhost
    pack $w.v5 -side right -fill x -anchor e -in $w.smbhost
    pack $w.l_hostip -side left -fill x -anchor w -in $w.smbip
    pack $w.v6_hostip -side right -fill x -anchor e -in $w.smbip
    pack $w.l6 -side left -fill x -anchor w -in $w.smbname
    pack $w.v6 -side right -fill x -anchor e -in $w.smbname
    pack $w.l7 -side left -fill x -anchor w -in $w.smbuser
    pack $w.v7 -side right -fill x -anchor e -in $w.smbuser
    pack $w.l8 -side left -fill x -anchor w -in $w.smbpasswd
    pack $w.v8 -side right -fill x -anchor e -in $w.smbpasswd
	pack $w.l9 -side left -fill x -anchor w -in $w.smbworkgroup
	pack $w.v9 -side right -fill x -anchor e -in $w.smbworkgroup

    pack $w.smbhost $w.smbip $w.smbname $w.smbuser $w.smbpasswd $w.smbworkgroup -side top -fill x -in $w.f1

    FILTERED_addpanel $w $i
}

proc SMB_updateentry {w i} {
global smb_share_name smb_hostip smb_password smb_user smb_workgroup smb_crlf

    set smb_share_name "\\\\[$w.v5 get]\\[$w.v6 get]"
    set smb_hostip    [$w.v6_hostip get]
    set smb_user   [$w.v7 get]
    set smb_password   [$w.v8 get]
	set smb_workgroup [$w.v9 get]
#
#   get the auto_filter setup first
#
#   it will be the input filter, so it sets the printcap entry 'if='
#
    set filter [$w.v10 get]
    FILTERED_updateentry $i $filter

}

proc SMB_setup {i} {
    global smb_share_name smb_hostip smb_password smb_user smb_workgroup smb_crlf
    global auto_flag
    global libexecdir

#
#   now do smbprint related setup
#
#
    set spool_dir [printcap_field $i "sd=" ""]
    catch {exec mkdir -p $spool_dir}
    catch {exec chown root.lp $spool_dir}
    catch {exec chmod 775 $spool_dir}

    set smb_config "$spool_dir/.config"
    set_printcap_field $i "af=" "$spool_dir/acct"
    set_printcap_field $i "lp=" /dev/null
    set_printcap_field $i "sh" ""
    catch {exec rm -f $smb_config}

    set ffd [open $smb_config w]
    if {$smb_crlf} { set translate yes } else { set translate no }
    puts $ffd "share='$smb_share_name'"
    puts $ffd "hostip=$smb_hostip"
    puts $ffd "user='$smb_user'"
    puts $ffd "password='$smb_password'"
	puts $ffd "workgroup='$smb_workgroup'"
    close $ffd
    catch { exec chown root.lp $smb_config }
    catch { exec chmod 0640 $smb_config }

#
#   see if any filter was specified, and if not, default
#   to a filter that will just send data to remote SMB server
#
    set i_filter [printcap_field $i "if=" ""]
    if {$i_filter != $auto_flag} {
	set_printcap_field $i "if=" $libexecdir/smbprint
    } else {
#
#       now setup the auto_filter
#
	FILTERED_setup $i
    }
}


#
# NCP support by Fernando Lozano from BL Inform��tica Ltda. at Rio de
# Janeiro, Brazil, adopted to printtool by Bill Nottingham
#
proc NCP_config {i} {
    set spool_dir [printcap_field $i "sd=" "none"]
    set config_file "$spool_dir/.config"

    if {[string compare spool_dir none] == 0} {
 	return [list "" "" "" ""]
    }
    if [file exists $config_file] {
        set fd [open $config_file]	
        if {![regexp "server=\(.*\)" [gets $fd] dummy server]} {
	    set server ""
	}
        if {![regexp "queue=\(.*\)" [gets $fd] dummy queue]} {
	    set queue ""
        }
        if {![regexp "user=\(.*\)" [gets $fd] dummy user]} {
 	     set user ""
        }
        if {![regexp "password=\(.*\)" [gets $fd] dummy password]} {
 	     set password ""
        }

        set translate no
        return [list $server $queue $user $password]        
    }
    return [list "" "" "" ""]
}

proc NCP_summaryline {i} {
    set printer [FILTERED_summaryline $i "printer"]
    set config [NCP_config $i]
    set servername  [lindex $config 0]
    set queuename [lindex $config 1]
    return "NCP - $printer on server $servername, queue $queuename"
}

proc NCP_name {} { return "NetWare Printer (NCP)" }

proc NCP_addpanel {w i} {
    global ncp_crlf

    set ncp_crlf 0
    set config [NCP_config $i]

    frame $w.ncpserver
    frame $w.ncpqueue
    frame $w.ncpuser
    frame $w.ncppasswd

    label $w.l5 -text "Printer Server Name" -anchor w
    entry $w.v5 -font fixed -relief sunken -borderwidth 2
    $w.v5 insert 0 [lindex $config 0]

    label $w.l6 -text "Print Queue Name" -anchor w
    entry $w.v6 -font fixed -relief sunken -borderwidth 2
    $w.v6 insert 0 [lindex $config 1]

    label $w.l7 -text "User" -anchor w
    entry $w.v7 -font fixed -relief sunken -borderwidth 2
    $w.v7 insert 0 [lindex $config 2]

    label $w.l8 -text "Password" -anchor w
    entry $w.v8 -show "*" -font fixed -relief sunken -borderwidth 2
    $w.v8 insert 0 [lindex $config 3]


#
# we'll handle cr/lf translation through the print filter if necessary
#
    set ncp_crlf 0

#    pack $w.l5 -pady 4 -side top -fill x -in $w.f1
#    pack $w.l_hostip -pady 4 -side top -fill x -in $w.f1
#    pack $w.l6 -pady 3 -side top -fill x -in $w.f1
#    pack $w.l7 -pady 4 -side top -fill x -in $w.f1
#    pack $w.l8 -pady 4 -side top -fill x -in $w.f1

##    pack $w.v5 $w.v6 -side top -expand true -fill x -in $w.f2
#    pack $w.v5 $w.v6_hostip $w.v6 $w.v7 $w.v8 -side top -expand true -fill x -in $w.f2

    pack $w.l5 -side left -fill x -anchor w -in $w.ncpserver
    pack $w.v5 -side right -fill x -anchor e -in $w.ncpserver
    pack $w.l6 -side left -fill x -anchor w -in $w.ncpqueue
    pack $w.v6 -side right -fill x -anchor e -in $w.ncpqueue
    pack $w.l7 -side left -fill x -anchor w -in $w.ncpuser
    pack $w.v7 -side right -fill x -anchor e -in $w.ncpuser
    pack $w.l8 -side left -fill x -anchor w -in $w.ncppasswd
    pack $w.v8 -side right -fill x -anchor e -in $w.ncppasswd

    pack $w.ncpserver $w.ncpqueue $w.ncpuser $w.ncppasswd -side top -fill x -in $w.f1

    FILTERED_addpanel $w $i
}

proc NCP_updateentry {w i} {
global ncp_server ncp_queue ncp_password ncp_user ncp_crlf

    set ncp_server [$w.v5 get]
    set ncp_queue    [$w.v6 get]
    set ncp_user   [$w.v7 get]
    set ncp_password   [$w.v8 get]
#
#   get the auto_filter setup first
#
#   it will be the input filter, so it sets the printcap entry 'if='
#
    set filter [$w.v10 get]
    FILTERED_updateentry $i $filter

}

proc NCP_setup {i} {
    global ncp_server ncp_queue ncp_password ncp_user ncp_crlf
    global auto_flag
    global libexecdir

#
#   now do ncpprint related setup
#
#
    set spool_dir [printcap_field $i "sd=" ""]
    catch {exec mkdir -p $spool_dir}
    catch {exec chown root.lp $spool_dir}
    catch {exec chmod 775 $spool_dir}

    set ncp_config "$spool_dir/.config"
    set_printcap_field $i "af=" "$spool_dir/acct"
    set_printcap_field $i "lp=" /dev/null
    set_printcap_field $i "sh" ""
    catch {exec rm -f $ncp_config}

    set ffd [open $ncp_config w]
    if {$ncp_crlf} { set translate yes } else { set translate no }
    puts $ffd "server=$ncp_server"
    puts $ffd "queue=$ncp_queue"
    puts $ffd "user=$ncp_user"
    puts $ffd "password=$ncp_password"
    close $ffd

#
#   see if any filter was specified, and if not, default
#   to a filter that will just send data to remote NCP server
#
    set i_filter [printcap_field $i "if=" ""]
    if {$i_filter != $auto_flag} {
	set_printcap_field $i "if=" $libexecdir/ncpprint
    } else {
#
#       now setup the auto_filter
#
	FILTERED_setup $i
    }
}

proc write_printcap {} {
    global print_count printer_comments printer_entry
    global printer_info printer_type
    global delete_index

    set fd [open "/etc/printcap" w]

    for {set i 0} {$i < $print_count} {incr i} {
	if {$i == 0 || $i != $delete_index} {
	    puts -nonewline $fd $printer_comments($i)
	}
	if {$i != $delete_index} {
	    if {$printer_type($i) != "UNKNOWN"} {
		puts $fd "\#\#PRINTTOOL3\#\# $printer_type($i) $printer_info($i)"
	    }
	    # De-canonicalize entry
	    set entry $printer_entry($i)
	    regsub -all ":" $printer_entry($i) ":\\\n\t:" entry
	    regsub "\\\\\n\t:\$" $entry "\n" entry
	    puts -nonewline $fd $entry
	}
    }
    puts -nonewline $fd $printer_comments($i)

    close $fd
    set delete_index ""

    # NickM
    # run reload to make lpd snarf the printcap (necessary for lprng)


	#this is a pretty clumsy check to see if we're in lprng-land
	#if {[file executable "/usr/sbin/checkpc"] == 1} {
	    #if {[file executable "/etc/rc.d/init.d/lprng"] == 1} {
		## Imperfect check for user-installed lprng (rh6.x, etc).
		#catch {exec /etc/rc.d/init.d/lprng reload}
	    #} elseif {[file executable "/etc/init.d/lpr"] == 1} {
		## This is a check for debian or any other FHS-compliant system
		## running lprng.
		#catch {exec /etc/init.d/lpr reload}
	    #} else {
		# This is a check for standard lprng install (RH7.x, etc.)
		#catch {exec /etc/rc.d/init.d/lpd reload}
	    #}
	#}

    # -NickM
}

proc sync {} {
    write_printcap
    # If this is an LPRng system, run checkpc to
    # take care of spool directory issues.
    #if {([file executable "/usr/sbin/checkpc"] == 1)} {
	#catch {exec /usr/sbin/checkpc -f}
    #}

    reload
    redisplay
}

proc get_selected_index {} {
    return [.list curselection]
}

proc edit_entry {i} {
    global print_count printer_names printer_entry printer_type printer_type_list
    global trigger
    global current_printer_type

    set current_printer_type $printer_type($i)

    if {$printer_type($i) == "UNKNOWN"} {
	rhs_error_dialog \
"This printer was not created by the 
Print System Manager.  You can remove 
it using this tool but not edit it."
	return 0
    } elseif {$printer_type($i) == "NCP"} {
	rhs_info_dialog \
"               - WARNING -

    The use of a remote NetWare printer 
    may require a username and password for
    its server. These are stored unencrypted
    in a script locally, and must be passed on
    the command line to the nprint program.
            
    In other words, it is possible for 
    another person to learn of the username
    and password. It is therefore recommended
    that the username and password for the
    use of the printer to NOT BE the same as
    that for a user account on the local 
    Linux box. That way, if this information
    is compromised, the only possible damage
    is unauthorized use of the printer.

    If there are file shares from the server,
    it would be best if they required a 
    different password than that required for
    the use of the printer, for the same reason.
"
    } elseif {$printer_type($i) == "SMB"} {
	rhs_info_dialog \
"               - WARNING -

    The use of a remote SMB/Windows 95/NT printer 
    may require a username and password for
    its server. These are stored unencrypted
    in a script locally, and must be passed on
    the command line to the smbclient program.
            
    In other words, it is possible for 
    another person to learn of the username
    and password. It is therefore recommended
    that the username and password for the
    use of the printer to NOT BE the same as
    that for a user account on the local 
    Linux box. That way, if this information
    is compromised, the only possible damage
    is unauthorized use of the printer.

    If there are file shares from the server,
    it would be best if they required a 
    different password than that required for
    the use of the printer, for the same reason.
"
    }

    toplevel .e
    wm withdraw .e
    wm transient .e .
    wm group .e .
    wm title .e "Edit [$printer_type($i)_name] Entry"

    frame .e.f1
#    frame .e.f2
    frame .e.f3

    frame .e.name
    frame .e.spool
    frame .e.filelen

    set traversal_list ""

    label .e.l1 -text "Names (name1|name2|...)"
    entry .e.v1 -font fixed -relief sunken -borderwidth 2
    .e.v1 insert 0 $printer_names($i)
    lappend traversal_list .e.v1

#
# make it so first queue name is copied to tail of spool dir if
# a CR is pressed in the queue name field
#
    bind .e.v1 <Return> {
        set CR_queue_name [lindex [split [.e.v1 get] |] 0]
	set CR_spool_dir [.e.v2 get]
	if {$CR_spool_dir != ""} {
	    set CR_spool_dir [string range $CR_spool_dir 0 \
                                      [string last "/" $CR_spool_dir]]
	    append CR_spool_dir $CR_queue_name
	    .e.v2 delete 0 end
	    .e.v2 insert 0 $CR_spool_dir
	}
    }
#
# ugly, but do it like this for now
#
    bind .e.v1 <Tab> {
        set CR_queue_name [lindex [split [.e.v1 get] |] 0]
	set CR_spool_dir [.e.v2 get]
	if {$CR_spool_dir != ""} {
	    set CR_spool_dir [string range $CR_spool_dir 0 \
                                      [string last "/" $CR_spool_dir]]
	    append CR_spool_dir $CR_queue_name
	    .e.v2 delete 0 end
	    .e.v2 insert 0 $CR_spool_dir
	}
    }



    label .e.l2 -text "Spool Directory"
    entry .e.v2 -font fixed -relief sunken -borderwidth 2
    .e.v2 insert 0 [printcap_field $i "sd=" ""]
    lappend traversal_list .e.v2

    label .e.l3 -text "File Limit in Kb (0 = no limit)" -anchor w
    entry .e.v3 -font fixed -relief sunken -borderwidth 2
    .e.v3 insert 0 [printcap_field $i "mx#" "0"]
    lappend traversal_list .e.v3

#    pack .e.l1 -pady 3 -side top -fill x -in .e.f1
#    pack .e.l2 -pady 4 -side top -fill x -in .e.f1
#    pack .e.l3 -pady 3 -side top -fill x -in .e.f1
#    pack .e.v1 .e.v2 .e.v3 -pady 0 -side top -expand true -fill x -in .e.f2

    pack .e.l1 -side left -anchor w -in .e.name
    pack .e.v1 -side right -fill x -anchor e -in .e.name

    pack .e.l2 -side left -anchor w -in .e.spool
    pack .e.v2 -side right -fill x -anchor e -in .e.spool

    pack .e.l3 -side left -in .e.filelen
    pack .e.v3 -side right -fill x -in .e.filelen

    pack .e.name -fill x -side top -in .e.f1
    pack .e.spool -fill x -side top -in .e.f1
    pack .e.filelen -fill x -side top -in .e.f1


    $printer_type($i)_addpanel .e $i

    frame .e.buttons
    button .e.b1 -text "OK" -width 10 -command "set trigger 1"
    pack .e.b1 -side left -expand true -ipady 1 -in .e.buttons
    button .e.b2 -text "Cancel" -width 10 -command "set trigger 0"
    pack .e.b2 -side left -expand true -ipady 1 -in .e.buttons

    pack .e.buttons -side bottom -expand true -fill x -padx 4 -pady 4 -in .e
    pack .e.f3 -side bottom -expand true -fill x

#    pack .e.f1 -side left -fill both -padx 2 -pady 1 -in .e
#    pack .e.f2 -side left -expand true -fill both -padx 2 -pady 1 -in .e

    pack .e.f1 -side top -fill both -padx 2 -pady 1 -in .e

    center_dialog .e
    grab set .e
    update
    scan [wm geometry .e] "%d%*c%d" xmin ymin
    wm minsize .e $xmin $ymin
    wm maxsize .e 10000 $ymin

    set inp_err 1

    while {$inp_err == 1} {
	set inp_err 0
	tkwait variable trigger

	if {$trigger == 1} {
	    # MLP: Do some sanity checking. Force the user
	    # to enter both the remote host and remote queue, etc.
	    # The printcap entry woon't work otherwise.
	    if {[.e.v1 get] == ""} {
		rhs_error_dialog "The Names field requires an entry."
		set inp_err 1
	    } elseif {[.e.v2 get] == ""} {
		rhs_error_dialog "The Spool Directory field requires an entry."
		set inp_err 1
	    } elseif {[.e.v3 get] == ""} {
		rhs_error_dialog "The File Limit field requires an entry."
		set inp_err 1
	    } elseif {$printer_type($i) == "REMOTE"} {
		set fieldname ""
		if {[.e.v5 get] == ""} {
		    set fieldname "Remote Host"
		} elseif {[.e.v6 get] == ""} {
		    set fieldname "Remote Queue"
		}
		if {$fieldname != ""} {
		    rhs_error_dialog "The $fieldname field requires an entry."
		    set inp_err 1
		}
	    } elseif {$printer_type($i) == "DIRECT"} {
		set fieldname ""
		if {[.e.v5 get] == ""} {
		    set fieldname "Hostname"
		} elseif {[.e.v6 get] == ""} {
		    set fieldname "Port Number"
		}
		if {$fieldname != ""} {
		    rhs_error_dialog "The $fieldname field requires an entry."
		    set inp_err 1
		}
	    } elseif {$printer_type($i) == "LOCAL"} {
		if {[.e.v8 get] == ""} {
		    rhs_error_dialog "The Printer Device field requires an entry."
		    set inp_err 1
		}
	    }

	    if {$inp_err == 0} {
		set printer_names($i) [.e.v1 get]
		regsub {^[^:]*:} $printer_entry($i) "[.e.v1 get]:" printer_entry($i)
		regsub -all {[ ]} [.e.v2 get] "_" tmp  
		
		set_printcap_field $i "sd=" $tmp
		set_printcap_field $i "mx#" [.e.v3 get]

		$printer_type($i)_updateentry .e $i
	    }
	}
    }

    destroy .e
    return $trigger
}

proc edit_entry_aux {i} {
    global printer_type printer_names

    set res [edit_entry $i]

    if {$res == 1} {
	# make spool directory
	set spool_dir [printcap_field $i "sd=" ""]
	catch {exec mkdir -p $spool_dir}
	catch {exec chown root.lp $spool_dir}
	catch {exec chmod 775 $spool_dir}

	# do type-specific setup
	$printer_type($i)_setup $i

	sync

# NickM
	if {[file executable "/usr/sbin/checkpc"] == 1} {
	    set queue [string trim [lindex [split $printer_names($i) "|"] 0]]
	    catch {exec /usr/sbin/checkpc -fP $queue}
	    #catch {exec /usr/sbin/checkpc -f}
	    reload_lpd


	}
# end NickM
    }

    return $res
}

proc printer_type_dialog {} {
    global type_name trigger_3
    global printer_type_list
    
    # MJC
    # display list of printer types

    .list selection clear 0 end

    toplevel .pt
    wm withdraw .pt
    wm transient .pt .
    wm group .pt .
    wm title .pt "Add a Printer Entry"

    label .pt.l1 -text "Printer type:" -anchor w
    pack .pt.l1 -side top
    foreach t $printer_type_list {
	set readabletypeproc ${t}_name
	radiobutton .pt.t_$t -text [$readabletypeproc] -variable type_name \
	    -value $t -anchor w 
	pack .pt.t_$t -side top -fill x -padx 2
    }
    set type_name [lindex $printer_type_list 0]

    frame .pt.buttons
    button .pt.ok -text "OK" -width 10 -command "set trigger_3 1"
    pack .pt.ok -side left -expand true -ipady 1 -pady 3 -in .pt.buttons
    button .pt.cancel -text "Cancel" -width 10 -command "set trigger_3 0"
    pack .pt.cancel -side left -expand true -ipady 1 -pady 3 -in .pt.buttons

    pack .pt.buttons -side top

    center_dialog .pt
    grab set .pt
    update
    scan [wm geometry .pt] "%d%*c%d" xmin ymin
    wm minsize .pt $xmin $ymin
    wm maxsize .pt 10000 $ymin
    set trigger_3 0
    tkwait variable trigger_3

    destroy .pt

    if {$trigger_3 == 1} {
	return $type_name
    }

    return {}
}
   

## End of random functions
##############################################
## @@ User Interface callback functions

proc help_dialog { title msg } {
    global trigger_help


    toplevel .hf
    wm withdraw .hf
    wm group .hf .
    wm title .hf $title

    frame .hf.helpf
    text .hf.helpf.msg -relief sunken -setgrid true -wrap word \
	          -width 60 -height 24 -yscrollcommand ".hf.helpf.msg_sy set" \
                  -font -adobe-helvetica-medium-r-normal--14*
    scrollbar .hf.helpf.msg_sy -orient vert -command ".hf.helpf.msg yview"
    .hf.helpf.msg insert 1.0 $msg
    .hf.helpf.msg config -state disabled
    pack .hf.helpf.msg_sy -side right -fill y -in .hf.helpf
    pack .hf.helpf.msg    -side left -fill both -expand true -in .hf.helpf
    
    frame .hf.okframe
    button .hf.b1 -text "OK" -width 10 -command "set trigger_help 1"
    pack .hf.b1 -side left -expand true -ipady 1 -in .hf.okframe

    pack .hf.helpf   -side top -expand true -fill both -in .hf
    pack .hf.okframe -side bottom -in .hf

#    center_dialog .hf
    wm deiconify .hf
    grab set .hf
    wm protocol .hf WM_DELETE_WINDOW { set trigger_help 0 }
    update
    scan [wm geometry .hf] "%d%*c%d" xmin ymin
    wm minsize .hf $xmin $ymin

    tkwait variable trigger_help
    destroy .hf
}


proc menu_genhelp {} {

    help_dialog "General Help" "
General Help
------------

In order to create a new print queue, select 'ADD',
or, to have Printtool attempt to detect local or 
remote printers, select AUTODETECT.

Then it is necessary to select what type of printer is being added.

There are five types of printers which can be configured with printtool:  LOCAL, REMOTE, SMB, NCP, and DIRECT.


  LOCAL printers are attached to the printer port
  of the local machine. 

  REMOTE printers are attached to another machine 
  which has an lpd daemon running. Usually this 
  will be another Linux machine.

  SMB printers are attached to another machine 
  which is an SMB server. Use this to print to
  printers on a machine running Windows or the
  samba software.
  
  NCP printers are attached to another machine
  which is an NCP server. Use this to print to
  printers on a machine running NetWare or the
  mars_nwe software.

  DIRECT printers are attached directly to the 
  network, and leave a port (typically 9100)
  open. By sending data directly to this port
  you can print. Old HP JetDirect cards along
  with some old printer sharing devices use
  this protocol.


After choosing the printer type, the following information is required:

  Queue name: What the queue will be called. Multiple names
              can be specified with the '|' character
              separating entries.

  Spool Directory: This is the directory on the local machine
              where files are stored before printing occurs.
              Be careful to NOT have more than one printer queue
              use a given spool directory.

  File Limit: Maximum size print job accepted. A size of 0 
              indicates no limit should be imposed.

For LOCAL printers, the following are also required:

  Printer Device: Usually /dev/lp1, the name of the port which
                  the printer is attached to. Serial printers
                  are usually on /dev/ttyS? ports. You will need
                  to manually configure serial parameters.

  Input Filter: Filters convert printed files into a format
                the printer can handle. Choose the filter
                which best matches your printer.

For REMOTE printers, the following are also required:

   Remote Host: Hostname of the remote machine hosting the printer.
 
   Remote Queue: Printer queue name of printer on remote machine.

The remote machine must be configured to allow the local machine to print on the desired queue. Typically hosts.lpd controls this.

For SMB printers, the following are also required:

   Printer Server Name: Name of the machine to which the printer you want
                        to use is attached.

   IP number of Server: Using this parameter will force the client to assume
                        that the server is on the machine with the specified IP
                        number, instead of determining it automatically.

   Printer Name: Name of the printer on which you want to print.

   Printer User: Name of user you must login as to access the printer.
 
   Printer Password: Password (if required) to use the printer.
                     Someone should be able to tell you this if
                     you do not already know it.
  
For NCP printers, the following are also required:

   Printer Server Name: Name of the machine to which the printer you want
                        to use is attached.

   Printer Name: Name of the printer on which you want to print.

   Printer User: Name of user you must login as to access the printer.
 
   Printer Password: Password (if required) to use the printer.
                     Someone should be able to tell you this if
                     you do not already know it.

For DIRECT printers, the following are also required:

   Printer IP address: Hostname of the remote printer.
 
   Port: Port number on the printer (typically 9100).

"
}


proc menu_trouble {} {

    help_dialog "Troubleshooting" "
Troubleshooting
---------------

First try printing a test page from under the \"Tests\":

    Choosing \"Print ASCII directly to port\" sends text straight
    to the printer, without using lpr. This will only work if your
    printer is attached to a printer port on the local machine. It
    will not work if your printer is configured as a REMOTE or SMB
    printer. 

    You should at least see an indicator light on your printer blink
    if your printer port is configured correctly. If this doesn't 
    work, check your cabling and printer device specification. 
    When adding a new printer, printtool tries to detect the port
    the printer is attached to. In case this detection failed, try
    changing the printer device to /dev/lp0, /dev/lp1, and /dev/lp2.
    These are the most common ports on PC hardware.

More complex test pages are available as well, but these use lpr to print.

    If your printer is TEXT-ONLY, choose \"Print ASCII test page\".

    Otherwise try \"Print PostScript test page\".

If ASCII seems to print and PostScript does not, check that you have the correct filter configured for your printer. If your printer model does not appear in the possible choices, try picking a similar model printer. If this fails, see if your printer is compatible with another printer in the available choices. Many printers, for example, are compatible with HP printers. Your printer manual should contain this information.

The \"Suppress Headers\" option sometimes causes trouble if it is off. Try turning it on if you get undesired banner pages, or if your printer appears to accept data and then not print anything.

For text-only printers, if you get stair-stepped text, try turning on the \"LF->CR/LF\" option.

For all printers, if your printer appears to accept data (some printers have a light which blinks when its accepting data), and nothing prints, try enabling the \"Send EOF\" option.

If you get an extra page at the end of every job, try turning off the \"Send EOF\" option.

For JetDirect boxes, be sure to send non-text output to the 'raw' queue on the box.
"
}

proc menu_about {} {
    global version

    rhs_info_dialog "
Red Hat Software Printtool Version $version

This tool allows you to setup printers easily.

The default printer is the first one listed.  To change
this you will have to edit /etc/printcap and move the
appropriate entry to the head of the list.

Once the printer queue and print filter are setup, it
is possible to print a variety of file types to the
queue and the filter will automatically convert it to
the format required by the printer. At the moment all
file types are converted to PostScript, and then printed
either straight to a PostScript printer, or run through
ghostscript if the printer does not handle PostScript
natively.

The printer can also be configured as 'Text-only', in which
case no conversion is attempted. The input text is echoed
straight to the printer. It is possible to do LF->CR/LF
conversion to correct stair-stepping, and to send a form feed
to eject the page at the end of a job.

If you want to print PostScript and do not have a PostScript
capable printer, make sure ghostscript is installed.

(This version was modified by VA Linux Systems
to add printer autodetection.)
"
}

#-------------------------------------------------------
# MLP: Get the first available spooldir of pattern lp[0-9]+,
# where name does not conflict with any existing 
# $printer_names.
#-------------------------------------------------------
proc get_next_unused_qname {} {
    global printer_names

    set num 0
    set done 0
    while {$done == 0} {
	set done 1
	set lpx "lp$num"
	if { [file isdirectory "/var/spool/lpd/$lpx"] == 0 } {
	    foreach i [array names printer_names] {
		if {[regexp $lpx $printer_names($i)] == 1} {
		    set done 0
		    break
		}
	    }
	} else {
	    set done 0
	}
	incr num
    }
    return $lpx
}

proc button_add {} {
    global print_count printer_names printer_entry printer_comments
    global printer_info printer_type

    set type [printer_type_dialog]
    # user may have selected "Cancel"
    if {$type == ""} {
	return
    }

    # First find a candidate lpX
    # (Make first printer added "lp")
    set done 0
    set num 0
    if {$print_count == 0} {
	set lpx "lp"
	set done 1
    }

    # MLP:
    if {$done == 0} {
	set lpx [get_next_unused_qname]
    }

    set i $print_count
    incr print_count

    set printer_names($i) "$lpx"
    set printer_type($i) $type
    set printer_info($i) {}
    set printer_entry($i) "$lpx:sd=/var/spool/lpd/$lpx:mx#0:"
    set printer_comments($print_count) ""
#
#   default to suppress headers on
#
    set_printcap_switch $i "sh" 1
    
    if {[edit_entry_aux $i] != 1} {
	incr print_count -1
    }
}

proc button_delete {} {
    global printer_names spool_dir delete_index

    set i [.list curselection]
    if {$i == ""} {
	rhs_error_dialog "No Printer Selected"
	return
    }

    set res [rhs_continue_dialog "Delete $printer_names($i) from /etc/printcap?"]
    if {$res == 1} {
	return
    }

    set spool_dir [printcap_field $i "sd=" ""]
    if {$spool_dir != ""} {
	rhs_info_dialog "Please remember to remove the spool directory:\n\n$spool_dir"
    }

    set delete_index $i
    # sync writes /etc/printcap first, so this will do
    sync
}

proc button_edit {} {
    set i [get_selected_index]
    if {$i == ""} {
	return
    }

    edit_entry_aux $i
}

proc menu_restart_lpd {} {
    if {[file executable "/etc/rc.d/init.d/lprng"] == 1} {
	# Imperfect check for lprng.
	catch {exec /etc/rc.d/init.d/lprng restart}
    } elseif {[file executable "/etc/init.d/lpr"] == 1} {
	# This is a check for debian running lprng.
	catch {exec /etc/init.d/lpr stop}
	catch {exec /etc/init.d/lpr start}
    } else {
	# This is a check for standard lpr (RH6.x, etc.)
	catch {exec /etc/rc.d/init.d/lpd stop}
	catch {exec /etc/rc.d/init.d/lpd start}
    }
}

proc reload_lpd {} {
    if {[file executable "/etc/rc.d/init.d/lprng"] == 1} {
	# Imperfect check for lprng.
	catch {exec /etc/rc.d/init.d/lprng reload}
    } elseif {[file executable "/etc/init.d/lpr"] == 1} {
	# This is a check for debian running lprng.
	catch {exec /etc/init.d/lpr reload}
    } else {
	# This is a check for standard lpr (RH6.x, etc.)
	catch {exec /etc/rc.d/init.d/lpd reload}
    }
}

proc menu_quit {} {
    # We could sync here but we should always be synced
    exit 0
}

proc menu_reload {} {
    global selected_hint

    .list delete 0 end
    update idletasks

    # We don't need to (and should not) write out printcap
    # since we should always be synced, and the goal
    # is really to pick up any external changes

    reload
    redisplay
}

#
# print the ascii test page
#
proc print_ascii_testpage {} {
    global printer_names
    global filtersrcdir

    set i [get_selected_index]
    if {$i == ""} {
        rhs_error_dialog "Please highlight a printer first"
	return
    }

    set queue [string trim [lindex [split $printer_names($i) "|"] 0]]

    catch {exec lpr -P$queue $filtersrcdir/testpage.asc} result
    if { $result != "" } {
	rhs_error_dialog "Error printing test page to queue $queue

Error reason: $result"
	return
    } else {
	rhs_info_dialog "Test page printed to queue $queue"
    }
}

#
# print the ascii test page directly to printer port
#
proc print_ascii_direct_testpage {} {
    global printer_names
    global printer_type

    set i [get_selected_index]
    if {$i == ""} {
        rhs_error_dialog "Please highlight a printer first"
	return
    }

    if {$printer_type($i) != "LOCAL"} {
	rhs_error_dialog "Can only print directly to a LOCAL printer"
	return
    }

    set port [printcap_field $i {lp=} {unset}]

    if {$port == "unset"} {
	rhs_error_dialog "Please set the printer device for this printer first"
	return
    }

    catch {exec printf "This text should appear on the printer on $port\014" > $port} result
    if { $result != "" } {
	rhs_error_dialog "Error printing test page to queue $port

Error reason: $result"
	return
    } else {
	rhs_info_dialog "Test page printed to port $port"
    }
}

#
# print the PS test page
#
proc print_ps_testpage {} {
    global printer_names printer_type printer_info
    global filtersrcdir

    set i [get_selected_index]
    if {$i == ""} {
        rhs_error_dialog "Please highlight a printer first"
	return
    }

    set queue [string trim [lindex [split $printer_names($i) "|"] 0]]

#
#   make sure that we should be printing PS to this queue
#    
    set sumline [$printer_type($i)_summaryline $i]
    if { $sumline == "" } {
	puts "Something is really wrong in print_ps_testpage"
	return
    }
    set prn_type [string trim [lindex [split $sumline] 0]]
    set paper_type [string trim [lindex [split $printer_info($i)] 2]]

    if { $prn_type == "type unrecognized" || $prn_type == "Text-only" } {
	rhs_error_dialog "You have tried to print a PostScript document
to a printer which you have configured to
have no PostScript support. This will probably
not do what you want. Try printing an ASCII
test page instead..."
        return
    }

    if { $paper_type == "a4" } {
	catch {exec lpr -P$queue $filtersrcdir/testpage-a4.ps} result
    } else {
	catch {exec lpr -P$queue $filtersrcdir/testpage.ps} result
    }

    if { $result != "" } {
	rhs_error_dialog "Error printing test page to queue $queue

Error reason: $result"
	return
    } else {
	rhs_info_dialog "Test page printed to queue $queue"
    }

}

proc reload_printerdb2 {} {
#----------------------------------------------------
#----------------------------------------------------
    global printerdb_entry 
    global printerdb_descr
    global printerdb_about 
    global printerdb_GSDriver
    global printerdb_res   
    global printerdb_color

    global printerdb_count
    global printerdb_descr2entry
    global printerdb_curdbline
    global filtersrcdir

    set tsv_state "TSV_START"
#
#   counter to let us know where printerdb integrity errors occur
#
    set printerdb_curdbline 0
    set printerdb_debug 0
    set printerdb_count 0
    if {[catch {set fd [open "$filtersrcdir/printers.tsv" r]}] == 1} {
	# there is no printerdb
	puts "Error cannot find ./printers.tsv!"
	return
    }

    # Skip initial comments:
    while {[gets $fd s] != "-1"} {
	# strip spaces at the front
	set cp [string trimleft $s]

	# if not a comment line then break?
	if {[regexp "^\#" $cp] != 1} {
	    break
	}
    }

    set i 0
    while { $tsv_state != "TSV_END" && $s != "-1" }  {

	set pfields [split $s "\t"]
	regsub -all " " [lindex $pfields 1] "" nextname

#	puts "#$i $nextname [lindex $pfields 0]|[lindex $pfields 1] | [lindex $pfields 7]x[lindex $pfields 7]"

	set printerdb_entry($i) $nextname
	incr i

	set printerdb_descr($nextname) "[lindex $pfields 0] [lindex $pfields 1]"
	set printerdb_descr2entry($printerdb_descr($nextname)) $nextname

	if {[string toupper [lindex $pfields 5]] == "N/A" && 
	[string toupper [lindex $pfields 6]] == "POSTSCRIPT"} {
	    set printerdb_GSDriver($nextname) "POSTSCRIPT"
	} else {
	    set printerdb_GSDriver($nextname) [lindex $pfields 5]
	}

	set printerdb_about($nextname) "Driver for [lindex $pfields 0] [lindex $pfields 1].\nDriver Type: $printerdb_GSDriver($nextname)"

	if {[lindex $pfields 7] != ""} {
	    lappend printerdb_res($nextname) "[lindex $pfields 7]  [lindex $pfields 8] { }"
	}

	set s [gets $fd]

	# strip spaces at the front
	set cp [string trimleft $s]

	# if not a comment line then break?
	if {[regexp "^\#section:" $cp] == 1 || [string length $s] <= 2} {
	    set tsv_state "TSV_END"
	}
    }

    set printerdb_count $i
}

## End of user interface callback functions
#############################################
## @@ main program

if {[exec id -u] != 0} {
    rhs_error_dialog "You must be root to run the Printer Tool."
    exit 1
}

#catch {source /usr/lib/stelias/printtool-atalksmb}

#
# msf - silly stuff but have to use it for now
#
set printerdb_debug 0
set printerdb_curdbline 0

# We can't call sync right away or /etc/fstab will get clobbered

catch {exec /bin/cp -f /etc/printcap /etc/printcap.bak}
reload
reload_printerdb
load_ppdxref_file
find_gs
find_mpage
if { $warn_samba != 0 } {
 find_samba
}
if { $warn_ncpfs != 0 } {
 find_nprint
}
set fd [ open $env(CONTROL_PANEL_LIB_DIR)/printtool.warn w ]
puts $fd "set warn_samba $warn_samba"
puts $fd "set warn_ncpfs $warn_ncpfs"
close $fd
write_printcap
# If this is an LPRng system, run checkpc to
# take care of spool directory issues.
# 	commented out -NickM
#if {([file executable "/usr/sbin/checkpc"] == 1)} {
    #catch {exec /usr/sbin/checkpc -f}
#}
redisplay

#================================================================
# BEGIN AUTODETECT SECTION
#
# Added by Mark Pruett 3/10/2000
#================================================================
#############################################################################
# Visual Tcl v1.11p1 Project
#

#################################
# GLOBAL VARIABLES
#
global widget; 
global packed;

#################################
# USER DEFINED PROCEDURES
#
proc init {argc argv} {

}

init $argc $argv


proc load_aliases_file {} {
#----------------------------------------------------
# Load the aliases cross-reference file into an 
# associative array.
#
# $alias(printer-alias) = actual-printer-name
#----------------------------------------------------
    global filtersrcdir
    global alias

    if {[catch {set fd [open "$filtersrcdir/aliases.rdb" r]}] == 1} {
	# there is no aliases table
	puts "Error cannot find $filtersrcdir/aliases.rdb!"
	return
    }

    # Skip initial comments:
    while {[gets $fd s] != "-1"} {
	# strip spaces at the front
	set cp [string trimleft $s]

	# if not a comment line then break?
	if {[regexp "^\#" $cp] != 1} {
	    break
	}
    }

    set fnames $s

    # read field types
    gets $fd ftypes

    set i 0
    while {[gets $fd s] != "-1"}  {
	
	set pfields [split $s "\t"]

	set alias([lindex $pfields 0]) [lindex $pfields 1]

	incr i
    }
    set alias_count $i

    close $fd
}

proc is_lprng_spooler { } {
#----------------------------------------------------
#----------------------------------------------------
    set lprng_found 0

    # Check output of "rpm -q LPRng".
    if [catch {open "| rpm -q LPRng"} input] {
	return $lprng_found
    }

    # Look at output to look for string "not installed"
    if {[gets $input line] >= 0} {
	if {[string first "not installed" $line] == -1} {
	    set lprng_found 1
	}
    }
    catch {close $input}

    if {[file executable "/etc/rc.d/init.d/lprng"] == 1} {
	# Imperfect check for lprng.
	set lprng_found 1
    } elseif {[file executable "/etc/init.d/lpr"] == 1} {
	# This is a check for debian running lprng.
	set lprng_found 1
    } elseif {[file executable "/usr/sbin/checkpc"] == 1} {
	set lprng_found 1
    }

    return $lprng_found
}

proc button_autodetect_add {} {
#----------------------------------------------------
# Lifted from button_add (see above).
#----------------------------------------------------
    global print_count printer_names printer_entry printer_comments
    global printer_info printer_type 

    global printerdb_descr2entry
    global printerdb_GSDriver printerdb_res

    global auto_printer auto_resolution auto_paper_size auto_eof 
    global auto_printerdb_entry auto_color auto_crlf_trans

    global auto_ppdfile
    
    global auto_flag
    global detect_type

    global setmeth

    global detport
    global detip
    global detprn

    global auto_direct_printer_ip
    global auto_direct_port

    global xref
    global alias
    global autodetecting trigger_2

    # If detect_type is REMOTE, see if they really 
    # want DIRECT:

    if {$detect_type != "LOCAL"} {
	set detect_type $setmeth
    }

    # What printer was selected?

    set selected [.winFound.fra18.cpd22.01 curselection]
    if {$selected == ""} {
	# Tell user to select something
	rhs_info_dialog "No printer was selected."
	
	return
    }

    if {$detect_type == "LOCAL"} {
	set port $detport($selected)
    }

    set pname [.winFound.fra18.cpd22.01 get $selected]
    # Strip out the parenthetical port number or IP address
    # we prepended to the string:
    set pos [string first ") " $pname]
    incr pos 2
    if {$pos >= 0} {
	set pname [string range $pname $pos end]
    }

    # Some of the printer descriptions may have one of the
    # following appended to it in printerdb_descr2entry:
    # (Color)
    # (Postscript)
    # (Color Postscript)

    set found 0

    set searchname $pname
    set aname "$pname (Color)"
    if {[info exists printerdb_descr2entry($aname)] } {
	set searchname $aname
	set found 1
    }
    set aname "$pname (Postscript)"
    if {[info exists printerdb_descr2entry($aname)] } {
	set searchname $aname
	set found 1
    }
    set aname "$pname (Color Postscript)"
    if {[info exists printerdb_descr2entry($aname)] } {
	set searchname $aname
	set found 1
    }
    # If none of the earlier attempts matched, check alias table.
    if {$found == 0} {
	set test [string trim $pname]

	if {[info exists alias($test)]} {
	    set searchname $alias($test)
	}
    }

    # Once we've done our best to find a matching 
    # printer name, check if we've succeeded; set $entry.

    if {[info exists printerdb_descr2entry($searchname)] } {
	set entry $printerdb_descr2entry($searchname)
    } else {
	# No driver info found. 
	rhs_info_dialog "
No printer driver information\nwas found for Printer:

$searchname

Printtool's database of known printers could 
not find an exact match for this printer's
name. The printer may be in the database 
with different wording. If not, you may
still be able to set the printer up by 
selecting a printer that is compatible
with your printer.

Press the \"Select\" Input Filter button
on the next window to select from the list
of supported printers.

"
	# Get info from internet? Maybe later.
    }

    #------------------------------------------------
    set type $detect_type

    # First find a candidate lpX
    # (Make first printer added "lp")
    set done 0
    set num 0
    if {$print_count == 0} {
	set lpx "lp"
	set done 1
    }

    # MLP:
    if {$done == 0} {
	set lpx [get_next_unused_qname]
    }

    set i $print_count
    incr print_count

    set printer_names($i) "$lpx"
    set printer_type($i) $type
    set printer_info($i) {}
    set printer_entry($i) "$lpx:sd=/var/spool/lpd/$lpx:mx#0:"
    set printer_comments($print_count) ""
#
#   default to suppress headers on
#
    set_printcap_switch $i "sh" 1

    if {$detect_type == "LOCAL"} {
	set_printcap_field $i "lp=" "/dev/lp$port"
    } elseif {$detect_type == "REMOTE"} {
	set_printcap_field $i "rm=" $detip($selected)
	# Generate a queue name 
	set parts [split $detip($selected) "."]
	set remotequeue "q_[join $parts _]"

	# Apparently, the remotequeue name should be
	# simply "lp", so that's what we'll save it as.
	set_printcap_field $i "rp=" "lp"

	if { [is_lprng_spooler] == 1 } {
	    rhs_info_dialog \
		"The detected printer may not work
correctly with your print spooler (lprng).
If you encounter printing problems, 
consider setup as a DIRECT printer."	    
	}

    } elseif {$detect_type == "DIRECT"} {
	set auto_direct_printer_ip $detip($selected)
	set auto_direct_port 9100
    }

    if {[info exists entry]} {
	set_printcap_field $i "if=" "$auto_flag - $entry"

	set auto_printer $printerdb_GSDriver($entry)
	set auto_printerdb_entry $entry
	set auto_paper_size "letter"

	if [llength [array names printerdb_res $entry]] {
	    set x [lindex $printerdb_res($entry) 0]
	    set auto_resolution [format "%sx%s" [lindex $x 0] [lindex $x 1]]
	} else {
	    set auto_resolution "Default"
	}

	set printer_info($i) [list $auto_printer $auto_resolution \
		$auto_paper_size $auto_eof $auto_printerdb_entry \
		$auto_color $auto_crlf_trans]
    } 

    # See if we have a matching PPD file

#    set auto_ppdfile ""
#    if {[info exists xref($pname)]} {
#	set auto_ppdfile $xref($pname)
#    }



    # Show edit dialog and let user decide if they
    # want to add this printer.

    set autodetecting 1
    set trigger_2 0
    if {[edit_entry_aux $i] != 1} {
	incr print_count -1
    }
}

proc {detect_network} {} {
#----------------------------------------------------
# Detect network printers
#----------------------------------------------------
    # The packed list lets us track which dynamic
    # parts of the "detected printers" dialog
    # are currently packed.
    global packed
    global detect_type
    global setmeth

    global detip
    global detprn

    global ip_from
    global ip_to
    global ip_netmask
    global hostspec

    global sbindir

    set detect_type REMOTE

    #------------------------------------------------
    # validate dialog-box text variables:
    #------------------------------------------------

    if { $hostspec == "" } {
	rhs_info_dialog "You must enter a valid host identifier.\nSee Help for more information."
	return;
    }
    
    #------------------------------------------------
    # Run pconf_detect
    #------------------------------------------------

    if [catch {open "|$sbindir/pconf_detect -m NETWORK -i \"$hostspec\" "} input] {

	rhs_info_dialog	"Could not find pconf_detect command-line utility
required for auto-detection. Make sure
that the gnulpr printfilters package is
properly installed."

	return
    }

    # Step through output and create a list of detected
    # network printers.

    set num 0
    while {[gets $input line] >= 0} {
	#set lstPrinters [split $line "\t"]
	#puts "([lindex $lstPrinters 1])"

	# Output should be in the form:
	# printer=10.203.1.2;vendor=HP;model=LaserJet 4

	set parts [split $line ";"]
	set ipval [split [lindex $parts 0] "="]
	set modelval [split [lindex $parts 1] "="]
	set makeval [split [lindex $parts 2] "="]
	set lstPrinters [list [lindex $ipval 1] "[lindex $modelval 1] [lindex $makeval 1]"]

	if {[lindex $lstPrinters 1] != "Unknown device"} {

	    # build parallel arrays with ...?

	    set detip($num) [lindex $lstPrinters 0]
	    set detprn($num) [lindex $lstPrinters 1]

	    incr num
	}
    }
    catch {close $input}

    # FOR TESTING PURPOSES ONLY!!!!!===============================
    # MLP
    #set detip($num) "10.20.30.40"
    #set detprn($num) "HP DeskJet 600"
    #incr num
    #==============================================================

    if {$num == 0} {
	# no printers detected - bail out.
	rhs_info_dialog "No network printers were detected."
	return
    }

    #------------------------------------------------
    # display the detected printers list window:
    #------------------------------------------------

    Window show .winFound
    grab set .winFound
    
    list packed

    catch {
	label .winFound.fra23.lblInfo \
		-justify left \
		-text "Select a printer, then press \"Add Printer\""
    }
    pack .winFound.fra23.lblInfo \
	    -in .winFound.fra23 -anchor center -expand 1 -fill x -side top 
    lappend packed .winFound.fra23.lblInfo

    #------------------------------------------------
    # Set up the window with the right number
    # of setup-method radio buttons:
    #------------------------------------------------

    list packed 
    set i 1
    set methlist {"NONE" "REMOTE" "DIRECT"}
    set setmeth [lindex $methlist 1]

    catch {
	radiobutton .winFound.fra23.rad$i  \
		-justify left -text "Setup as REMOTE printer" \
		-variable setmeth -value [lindex $methlist $i]
    }
    pack .winFound.fra23.rad$i \
	    -in .winFound.fra23 -anchor center -expand 1 -fill x -side top 
    lappend packed .winFound.fra23.rad$i

    incr i
    catch {
	radiobutton .winFound.fra23.rad$i  \
		-justify left -text "Setup as DIRECT printer" \
		-variable setmeth -value [lindex $methlist $i]
    }
    pack .winFound.fra23.rad$i  \
	    -in .winFound.fra23 -anchor center -expand 1 -fill x -side top 
    lappend packed .winFound.fra23.rad$i

    #------------------------------------------------
    # fill the list box with detected printers
    #------------------------------------------------

    # Delete anything in the listbox.

    .winFound.fra18.cpd22.01 delete 0 end

    for {set i 0} {$i < $num} {incr i} {
	.winFound.fra18.cpd22.01 insert end "($detip($i)) $detprn($i)"
	
    }

}

proc {detect_pport} {} {
#----------------------------------------------------
# Detect parallel port printers
#----------------------------------------------------
    # The packed list lets us track which dynamic
    # parts of the "detected printers" dialog
    # are currently packed.
    global packed
    global detect_type

    global detport
    global detprn

    global sbindir

    set detect_type LOCAL

    #------------------------------------------------
    # Let's just sweep through ports 0 through 2
    #------------------------------------------------
    set ports "0,1,2"

    #------------------------------------------------
    # Run pconf_detect
    #------------------------------------------------

    if [catch {open "|$sbindir/pconf_detect -m PPORT -i $ports " } input] {

	rhs_info_dialog	"Could not find pconf_detect command-line utility
required for auto-detection. Make sure
that the gnulpr printfilters package is
properly installed."

	return
	
    }

    set ports_not_configured 0
    set ports_checked 0
    set num 0
    while {[gets $input line] >= 0} {

	# Output should be in the form:
	# port=0;model=HP DeskJet 500

	set pos [string first "port=" $line]
	if {$pos == -1} {
	    # Not a valid line, so skip.
	    continue
	}

	set parts [split $line ";"]
	set portval [split [lindex $parts 0] "="]
	set prnval [split [lindex $parts 1] "="]
	set lstPrinters [list [lindex $portval 1] [lindex $prnval 1]]

	incr ports_checked 1
	if {[lindex $lstPrinters 1] == "Port not configured"} {
	    incr ports_not_configured 1
	}

	if {([lindex $lstPrinters 1] != "Unknown device") &&
            ([lindex $lstPrinters 1] != "Port not configured")} {

	    # build parallel arrays with a port number and 
	    # printer ID.

	    set detport($num) [lindex $lstPrinters 0]
	    set detprn($num) [lindex $lstPrinters 1]

	    incr num
	}
    }
    catch {close $input}

    # FOR TESTING PURPOSES ONLY!!!!!===============================
    # MLP
    #set detport($num) 1
    #set detprn($num) "HP DeskJet 600"
    #incr num
    #==============================================================

    if {$ports_not_configured == $ports_checked} {
	# Couldn't find parport info.
	rhs_info_dialog "
No parallel ports detected. Make sure ports are
properly configured before running printtool.

You may not have parallel port support available
in your Linux kernel, or you may not be loading
the parallel port modules (usually called \"parport\")
needed for communicating with printers through
your computer's parallel ports.

"
	return
    }
	
    if {$num == 0} {
	# no printers detected - bail out.
	rhs_info_dialog "No parallel port printers were detected."
	return
    }

    #------------------------------------------------
    # display the detected printers list window:
    #------------------------------------------------

    Window show .winFound
    grab set .winFound
    
    list packed

    catch {
	label .winFound.fra23.lblInfo  \
		-justify left \
		-text "Select a printer, then press \"Add Printer\""
    }
    pack .winFound.fra23.lblInfo \
	    -in .winFound.fra23 -anchor center -expand 1 -fill x -side top 
    lappend packed .winFound.fra23.lblInfo

    #------------------------------------------------
    # fill the list box with detected printers
    #------------------------------------------------

    .winFound.fra18.cpd22.01 delete 0 end

    for {set i 0} {$i < $num} {incr i} {
	.winFound.fra18.cpd22.01 insert end "(Port $detport($i)) $detprn($i)"
	
    }

}

proc button_autodetect {} {
#----------------------------------------------------
# Display supported autodetect methods
#----------------------------------------------------

#    load_ppdxref_file
    load_aliases_file
    Window show .winDetMeth
    grab set .winDetMeth
}


proc {winDetMeth_hide} {} {
#----------------------------------------------------
# Pop-down the "autodetect methods" window
#----------------------------------------------------

    grab release .winDetMeth
    Window hide .winDetMeth
    
}

proc {winFound_hide} {} {
#----------------------------------------------------
# Pop-down the "Detected Printers" window
#----------------------------------------------------
    global packed
    global autodetecting

    set autodetecting 0

    set i 0
    while {$i < [llength $packed]} {
	pack forget [lindex $packed $i]
	incr i
    }

    grab release .winFound
    Window hide .winFound
}

proc {help_detect_pport} {} {
#----------------------------------------------------
# Popup help for "Detect parallel port printers".
#----------------------------------------------------
    help_dialog "Autodetecting Parallel Ports" "
Detecting Parallel Ports
------------------------

This option will attempt to check for
printers attached to parallel ports
on the local machine. Only printers
that repond to the IEEE 1284 protocol
are detected.
"

}

proc {help_detect_network} {} {
#----------------------------------------------------
# Popup help for "Detect parallel port printers".
#----------------------------------------------------
    help_dialog "Autodetecting Network Printers" "
Detecting Network Printers
--------------------------

This option probes your network seeking 
printers capable of identifying themselves.

The Host Specification can take one of
several forms:

Hostname: 
    myprinter.mydomain.com

IP address: 
    10.203.2.44

IP range: 
    10.203.2.5-23

IP/Netmask: 
    10.203.168.0.0/24 or 
    10.203.168.0.0/255.255.255.0

Note: This option may take several minutes
to execute, depending on the size of the
network you are scanning.

"

}

proc {main} {argc argv} {
#----------------------------------------------------
#----------------------------------------------------
}

proc {Window} {args} {
global vTcl
    set cmd [lindex $args 0]
    set name [lindex $args 1]
    set newname [lindex $args 2]
    set rest [lrange $args 3 end]
    if {$name == "" || $cmd == ""} {return}
    if {$newname == ""} {
        set newname $name
    }
    set exists [winfo exists $newname]
    switch $cmd {
        show {
            if {$exists == "1" && $name != "."} {wm deiconify $name; return}
            if {[info procs vTclWindow(pre)$name] != ""} {
                eval "vTclWindow(pre)$name $newname $rest"
            }
            if {[info procs vTclWindow$name] != ""} {
                eval "vTclWindow$name $newname $rest"
            }
            if {[info procs vTclWindow(post)$name] != ""} {
                eval "vTclWindow(post)$name $newname $rest"
            }
        }
        hide    { if $exists {wm withdraw $newname; return} }
        iconify { if $exists {wm iconify $newname; return} }
        destroy { if $exists {destroy $newname; return} }
    }
}

#################################
# VTCL GENERATED GUI PROCEDURES
#

proc vTclWindow. {base} {
    if {$base == ""} {
        set base .
    }
    ###################
    # CREATING WIDGETS
    ###################
    wm focusmodel $base passive
    wm geometry $base 1x1+0+0
    wm maxsize $base 1009 738
    wm minsize $base 1 1
    wm overrideredirect $base 0
    wm resizable $base 1 1
    wm withdraw $base
    wm title $base "vt.tcl"
    ###################
    # SETTING GEOMETRY
    ###################
}

proc vTclWindow.winDetMeth {base} {
    if {$base == ""} {
        set base .winDetMeth
    }
    if {[winfo exists $base]} {
        wm deiconify $base; return
    }
    ###################
    # CREATING WIDGETS
    ###################
    toplevel $base -class Toplevel
    wm focusmodel $base passive
# MLP: geometry with HP Multicast widgets enabled:
#    wm geometry $base 396x296+326+240
    wm geometry $base 396x226
    wm maxsize $base 1009 738
    wm minsize $base 1 1
    wm overrideredirect $base 0
    wm resizable $base 1 1
    wm deiconify $base
    wm title $base "AutoDetect Printers"
    frame $base.fra23 \
        -borderwidth 2 -height 152 -relief raised -width 125 
    label $base.fra23.lab26 \
        -borderwidth 1 -text {Detection Methods:} 
    frame $base.fra23.fra27 \
        -borderwidth 2 -height 75 -relief groove -width 125 
    label $base.fra23.fra27.lab28 \
        -borderwidth 1 -foreground #0000c0 -text {Parallel Port Printers} 
    button $base.fra23.fra27.but29 \
        -command detect_pport -padx 9 -pady 3 -text Detect... 
    button $base.fra23.fra27.butHelpPPort \
	-command help_detect_pport -padx 9 -pady 3 -text {Help}
    frame $base.fra23.fra30 \
        -borderwidth 2 -height 75 -relief groove -width 125 
    label $base.fra23.fra30.lab31 \
        -borderwidth 1 -foreground #0000c0 -text {Networked Printers} 
#    frame $base.fra23.fra30.fra32 \
#        -borderwidth 2 -height 75 -relief groove -width 125 
#    label $base.fra23.fra30.fra32.lab33 \
#        -borderwidth 1 -text {Foo} 
#    entry $base.fra23.fra30.fra32.ent34 -textvariable ip_from \
#        -width 30
#    label $base.fra23.fra30.fra32.lab35 \
#        -borderwidth 1 -text or 
    frame $base.fra23.fra30.fra37 \
	    -borderwidth 2 -height 75 -relief groove -width 125 
    label $base.fra23.fra30.fra37.lab38 \
	    -borderwidth 1 -text {Host Specification:} 
    entry $base.fra23.fra30.fra37.ent39 -textvariable hostspec \
	    -width 30
    frame $base.fra23.fra30.fra40 \
        -borderwidth 2 -height 75 -width 125 
    button $base.fra23.fra30.butHelpNetwork \
	-command help_detect_network -padx 9 -pady 3 -text {Help}
    button $base.fra23.fra30.fra40.but41 \
        -command detect_network -padx 9 -pady 3 -text Detect... 
    frame $base.fra23.fra42 \
        -borderwidth 2 -height 75 -relief groove -width 125 
    label $base.fra23.fra42.lab43 \
        -borderwidth 1 -foreground #0000c0 -text {HP Multicast} 
    button $base.fra23.fra42.but44 \
        -padx 9 -pady 3 -text Detect... 
    frame $base.fra24 \
        -borderwidth 2 -relief raised -width 125 
    button $base.fra24.but25 \
        -command winDetMeth_hide -padx 9 -pady 3 -text Cancel 
    ###################
    # SETTING GEOMETRY
    ###################
    pack $base.fra23 \
        -in .winDetMeth -anchor s -expand 1 -fill both -side top 
    pack $base.fra23.lab26 \
        -in .winDetMeth.fra23 -anchor sw -expand 0 -fill none -padx 3 \
        -side top 
    pack $base.fra23.fra27 \
        -in .winDetMeth.fra23 -anchor center -expand 0 -fill x -ipadx 3 \
        -padx 3 -pady 3 -side top 
    pack $base.fra23.fra27.lab28 \
        -in .winDetMeth.fra23.fra27 -anchor center -expand 0 -fill none -padx 3 \
        -side left 
    pack $base.fra23.fra27.but29 \
        -in .winDetMeth.fra23.fra27 -anchor center -expand 0 -fill none \
        -side right 
    pack $base.fra23.fra27.butHelpPPort \
        -in .winDetMeth.fra23.fra27 -anchor center -expand 0 -fill none \
        -side right 

    pack $base.fra23.fra30 \
        -in .winDetMeth.fra23 -anchor center -expand 0 -fill x -ipadx 3 \
        -ipady 3 -padx 3 -pady 3 -side top
    pack $base.fra23.fra30.lab31 \
        -in .winDetMeth.fra23.fra30 -anchor sw -expand 0 -fill none -padx 3 \
        -pady 3 -side top 
    pack $base.fra23.fra30.fra37 \
        -in .winDetMeth.fra23.fra30 -anchor center -expand 0 -fill x -ipadx 3 \
        -ipady 3 -padx 3 -pady 3 -side top 
    pack $base.fra23.fra30.fra37.lab38 \
        -in .winDetMeth.fra23.fra30.fra37 -anchor center -expand 0 -fill none \
        -side left 
    pack $base.fra23.fra30.fra37.ent39 \
        -in .winDetMeth.fra23.fra30.fra37 -anchor center -expand 0 -fill none \
        -side left 
    pack $base.fra23.fra30.fra40 \
        -in .winDetMeth.fra23.fra30 -anchor center -expand 0 -fill x -pady 3 \
        -side top 
    pack $base.fra23.fra30.fra40.but41 \
        -in .winDetMeth.fra23.fra30.fra40 -anchor center -expand 0 -fill none \
        -side right 
    pack $base.fra23.fra30.butHelpNetwork \
        -in .winDetMeth.fra23.fra30.fra40 -anchor center -expand 0 -fill none \
        -side right 

# MLP: These are related to HP Multicast, which is not
# yet implemented, but may be in the not-too-distant future.

#    pack $base.fra23.fra42 \
#        -in .winDetMeth.fra23 -anchor center -expand 0 -fill x -ipadx 3 \
#        -ipady 3 -padx 3 -pady 3 -side top 
#    pack $base.fra23.fra42.lab43 \
#        -in .winDetMeth.fra23.fra42 -anchor center -expand 0 -fill none \
#        -side left 
#    pack $base.fra23.fra42.but44 \
#        -in .winDetMeth.fra23.fra42 -anchor center -expand 0 -fill none \
#        -side right 
    pack $base.fra24 \
        -in .winDetMeth -anchor n -expand 0 -fill both -ipadx 5 -ipady 3 \
        -side bottom 
    pack $base.fra24.but25 \
        -in .winDetMeth.fra24 -anchor center -expand 0 -fill y -padx 3 \
        -pady 3 -side right 
}

proc vTclWindow.winFound {base} {
    if {$base == ""} {
        set base .winFound
    }
    if {[winfo exists $base]} {
        wm deiconify $base; return
    }
    ###################
    # CREATING WIDGETS
    ###################
    toplevel $base -class Toplevel
    wm focusmodel $base passive
    wm geometry $base 288x326
    wm maxsize $base 1009 738
    wm minsize $base 1 1
    wm overrideredirect $base 0
    wm resizable $base 1 1
    wm title $base "Detected Printers"
    frame $base.fra18 \
        -borderwidth 2 -height 200 -relief raised -width 125 
    frame $base.fra18.cpd22 \
        -borderwidth 1 -height 70 -width 30 
    listbox $base.fra18.cpd22.01 \
        -font -Adobe-Helvetica-Medium-R-Normal-*-*-120-*-*-*-*-*-* \
        -xscrollcommand {.winFound.fra18.cpd22.02 set} \
        -yscrollcommand {.winFound.fra18.cpd22.03 set} 
    scrollbar $base.fra18.cpd22.02 \
        -borderwidth 1 -command {.winFound.fra18.cpd22.01 xview} \
        -orient horiz -width 10 
    scrollbar $base.fra18.cpd22.03 \
        -borderwidth 1 -command {.winFound.fra18.cpd22.01 yview} -orient vert \
        -width 10 
    frame $base.fra19 \
        -borderwidth 2 -height 41 -relief raised -width 125 
    button $base.fra19.but20 \
        -command button_autodetect_add -padx 9 -pady 3 -text {Add Printer} 
    button $base.fra19.but21 \
        -command winFound_hide -padx 9 -pady 3 -text Cancel 
    frame $base.fra23 \
        -borderwidth 2 -height 126 -relief raised -width 125 
    ###################
    # SETTING GEOMETRY
    ###################
    pack $base.fra18 \
        -in .winFound -anchor center -expand 1 -fill both -side top 
    pack $base.fra18.cpd22 \
        -in .winFound.fra18 -anchor center -expand 1 -fill both -side top 
    grid columnconf $base.fra18.cpd22 0 -weight 1
    grid rowconf $base.fra18.cpd22 0 -weight 1
    grid $base.fra18.cpd22.01 \
        -in .winFound.fra18.cpd22 -column 0 -row 0 -columnspan 1 -rowspan 1 \
        -sticky nesw 
    grid $base.fra18.cpd22.02 \
        -in .winFound.fra18.cpd22 -column 0 -row 1 -columnspan 1 -rowspan 1 \
        -sticky ew 
    grid $base.fra18.cpd22.03 \
        -in .winFound.fra18.cpd22 -column 1 -row 0 -columnspan 1 -rowspan 1 \
        -sticky ns 
    pack $base.fra19 \
        -in .winFound -anchor center -expand 0 -fill x -ipadx 3 -ipady 3 \
        -side bottom 
    pack $base.fra19.but20 \
        -in .winFound.fra19 -anchor center -expand 0 -fill none -padx 3 \
        -side left 
    pack $base.fra19.but21 \
        -in .winFound.fra19 -anchor center -expand 0 -fill none -ipadx 5 \
        -padx 3 -side right 
    pack $base.fra23 \
        -in .winFound -anchor center -expand 0 -fill x -side top 
}