File: pp

package info (click to toggle)
sudo 1.8.5p2-1+nmu3+deb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 12,556 kB
  • sloc: ansic: 39,731; sh: 16,999; makefile: 1,974; yacc: 1,471; lex: 1,037; perl: 436
file content (7711 lines) | stat: -rwxr-xr-x 218,726 bytes parent folder | download
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
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
#!/bin/sh
# Copyright 2012 Quest Software, Inc. ALL RIGHTS RESERVED
pp_revision="355"
 # Copyright 2012 Quest Software, Inc.  ALL RIGHTS RESERVED.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
 # are met:
 #
 # 1. Redistributions of source code must retain the above copyright
 #    notice, this list of conditions and the following disclaimer.
 # 2. Redistributions in binary form must reproduce the above copyright
 #    notice, this list of conditions and the following disclaimer in the
 #    documentation and/or other materials provided with the distribution.
 # 3. Neither the name of Quest Software, Inc. nor the names of its
 #    contributors may be used to endorse or promote products derived from
 #    this software without specific prior written permission.
 #
 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 # Please see <http://rc.quest.com/topics/polypkg/> for more information

pp_version="1.0.0.$pp_revision"
pp_copyright="Copyright 2012, Quest Software, Inc. ALL RIGHTS RESERVED."

pp_opt_debug=false
pp_opt_destdir="$DESTDIR"
pp_opt_install_script=
pp_opt_list=false
pp_opt_no_clean=false
pp_opt_no_package=false
pp_opt_only_front=false
pp_opt_platform=
pp_opt_probe=false
pp_opt_strip=false
pp_opt_save_unstripped=false
pp_opt_vas_platforms=false
pp_opt_wrkdir="`pwd`/pp.work.$$"
pp_opt_verbose=false
pp_opt_version=false
pp_opt_input="-"
pp_opt_init_vars=""
pp_opt_eval=

test -n "$PP_NO_CLEAN" && pp_opt_no_clean=true
test -n "$PP_DEBUG" && pp_opt_debug=true
test -n "$PP_VERBOSE" && pp_opt_verbose=true

pp_main_cleanup () {
	pp_debug "main_cleanup"
        pp_remove_later_now
	if $pp_opt_no_clean || test x"$pp_platform" = x"unknown"; then
	    : no cleanup
	else
	    pp_backend_${pp_platform}_cleanup
	    $pp_errors && pp_die "Errors during cleanup"
	    if test -d "$pp_wrkdir"; then
		if $pp_opt_debug; then
		    pp_debug "not removing $pp_wrkdir"
		else
		    pp_verbose rm -rf "$pp_wrkdir"
		fi
	    fi
	fi
}

pp_parseopts () {
        typeset a n _var _val
	while test $# -gt 0; do

	  # convert -[dilpv] to --long-options
	  case "$1" in
	    --?*=?*) n=`echo "$1" | sed -ne 's/^--\([^=]*\)=.*/\1/p'`
	           a=`echo "$1" | sed -ne 's/^--[^=]*=\(.*\)/\1/p'`
		   shift
		   set -- "--$n" "$a" "$@";;
	    --?*) : ;;

	    -d)  shift; set -- "--debug" "$@";;
	    -d*) a=`echo "$1" | sed -ne 's/^-.//'`
		 shift; set -- "--debug" "$@";;

	    -i) shift; set -- "--install-script" "$@";;
	    -i*) a=`echo "$1" | sed -ne 's/^-.//'`
		 shift; set -- "--install-script" "$a" "$@";;

	    -l)  shift; set -- "--list" "$@";;
	    -l*) a=`echo "$1" | sed -ne 's/^-.//'`
		 shift; set -- "--list" "$@";;

	    -p) shift; set -- "--platform" "$@";;
	    -p*) a=`echo "$1" | sed -ne 's/^-.//'`
		 shift; set -- "--platform" "$a" "$@";;

	    -v)  shift; set -- "--verbose" "$@";;
	    -v*) a=`echo "$1" | sed -ne 's/^-.//'`
		 shift; set -- "--verbose" "$@";;

	    -\?)  shift; set -- "--help" "$@";;
	    -\?*) a=`echo "$1" | sed -ne 's/^-.//'`
		 shift; set -- "--help" "$@";;
	  esac

	  case "$1" in
	    --destdir|--eval|--install-script|--platform|--wrkdir)
		test $# -ge 2 || pp_error "missing argument to $1";;
	  esac

	  case "$1" in
	    --) 				       shift;break;;
	    --debug)           pp_opt_debug=true;      shift;;
	    --destdir)         pp_opt_destdir="$2";    shift;shift;;
	    --eval)            pp_opt_eval="$2";       shift;shift;; # undoc
	    --install-script)  pp_opt_install_script="$2"; shift;shift;;
	    --list)            pp_opt_list=true;       shift;;
	    --no-clean)        pp_opt_no_clean=true;   shift;;
	    --no-package)      pp_opt_no_package=true; shift;;
	    --only-front)      pp_opt_only_front=true; shift;;
	    --platform)        pp_opt_platform="$2";   shift;shift;;
	    --probe)           pp_opt_probe=true;      shift;;
	    --strip)           pp_opt_strip=true;      shift;;
	    --save-unstripped) pp_opt_save_unstripped=true; shift;;
	    --wrkdir)          pp_opt_wrkdir="$2";     shift;shift;;
	    --vas-platforms)   pp_opt_vas_platforms=true; shift;;
	    --verbose)         pp_opt_verbose=true;    shift;;
	    --version)         pp_opt_version=true;    shift;;
	    --help)            pp_errors=true;         shift;;
	    -) break;;
	    -*) pp_error "unknown option $1"; shift;;
	    *) break;;
	  esac

	done

	pp_opt_input=-
	if test $# -gt 0; then
	    pp_opt_input="$1"
	    shift
	fi

        #-- extra arguments of the form Foo=bar alter *global* vars
        while test $# -gt 0; do
            case "$1" in
		-*)	pp_error "unexpected option '$1'"
			shift;;
                *=*)    _val="${1#*=}"
                        _var=${1%="$_val"}
                        _val=`echo "$_val"|sed -e 's/[$"\\]/\\&/g'`
                        pp_debug "setting $_var = \"$_val\""
                        pp_opt_init_vars="$pp_opt_init_vars$_var=\"$_val\";"
                        shift;;
                *)      pp_error "unexpected argument $1'"
			shift;;
            esac
        done

	test $# -gt 0 &&
            pp_error "unknown argument $1"

	if $pp_errors; then
	    cat <<. >&2
polypkg $pp_version $pp_copyright
usage: $0 [options] [input.pp] [var=value ...]
    -d --debug                  -- write copious info to stderr
       --destdir=path           -- file root, defaults to \$DESTDIR
    -? --help                   -- display this information
    -i --install-script=path    -- create an install helper script
    -l --list                   -- write package filenames to stdout
       --no-clean               -- don't remove temporary files
       --no-package             -- do everything but create packages
       --only-front             -- only perform front-end actions
    -p --platform=platform      -- defaults to local platform
       --probe                  -- print local system identifier, then exit
       --strip                  -- strip debug symbols from binaries before
                                   packaging (modifies files in destdir)
       --save-unstripped        -- save unstripped binaries to
                                   \$name-\$version-unstripped.tar.gz
       --wrkdir=path            -- defaults to subdirectory of \$TMPDIR or /tmp
    -v --verbose                -- write info to stderr
       --version                -- display version and quit
.
	    exit 1
	fi
}

pp_drive () {
	# initialise the front and back ends
        pp_model_init
	pp_frontend_init
	$pp_opt_only_front || pp_backend_init

	# run the front-end to generate the intermediate files
        # set $pp_input_dir to be the 'include dir' if needed
	pp_debug "calling frontend on $pp_opt_input"
	case "$pp_opt_input" in
	    -)   pp_input_dir=.
		 test -t 1<&0 &&
		    pp_warn "reading directives from standard input"
                 pp_frontend
                 ;;
            */*) pp_input_dir=${pp_opt_input%/*}
	         pp_frontend <"$pp_opt_input"
                 ;;
            *)   pp_input_dir=.
	         pp_frontend <"$pp_opt_input"
                 ;;
	esac

        pp_files_ignore_others
        pp_service_scan_groups

	# some sanity checks after front-end processing
        if test x"$pp_platform" != x"null"; then
	    pp_debug "sanity checks"
	    test -n "$pp_components" || pp_error "No components?"
	    pp_check_var_is_defined  "name"
	    pp_check_var_is_defined  "version"
            pp_files_check_duplicates
            pp_files_check_coverage
	    pp_die_if_errors "Errors during sanity checks"
        fi

	# stop now if we're only running the front
	$pp_opt_only_front && return

	if test x"$pp_opt_strip" = x"true"; then
	    pp_strip_binaries
	fi

	# run the back-end to generate the package
	pp_debug "calling backend"
	pp_backend
	pp_die_if_errors "Errors during backend processing"

	# copy the resulting package files to PP_PKGDESTDIR or .
	for f in `pp_backend_names` -; do
           test x"$f" = x"-" && continue
	   pp_debug "copying: $f to `pwd`"
	   if pp_verbose cp -r $pp_wrkdir/$f ${PP_PKGDESTDIR:-.}; then
               echo "${PP_PKGDESTDIR:+$PP_PKGDESTDIR/}$f"
           else
               pp_error "$f: missing package"
           fi
	done
	pp_die_if_errors "Errors during package copying"
}

pp_install_script () {
        pp_debug "writing install script to $pp_opt_install_script"
        rm -f $pp_opt_install_script
        pp_backend_install_script > $pp_opt_install_script
	pp_die_if_errors "Errors during package install script"
        chmod +x $pp_opt_install_script
}

pp_main () {
	# If PP_DEV_PATH is set, then jump to that script.
	# (Useful when working on polypkg source that isn't installed)
	if test -n "$PP_DEV_PATH" -a x"$PP_DEV_PATH" != x"$0"; then
	    pp_warn "switching from $0 to $PP_DEV_PATH ..."
	    exec "$PP_DEV_PATH" "$@" || exit 1
	fi

	pp_set_expand_converter_or_reexec "$@"
	pp_parseopts "$@"

        if $pp_opt_version; then
            #-- print version and exit
            echo "polypkg $pp_version"
            exit 0
        fi

	pp_set_platform

	trap 'pp_main_cleanup' 0

	pp_wrkdir="$pp_opt_wrkdir"
	pp_debug "pp_wrkdir = $pp_wrkdir"
	rm -rf "$pp_wrkdir"
	mkdir -p "$pp_wrkdir"

	pp_destdir="$pp_opt_destdir"
	pp_debug "pp_destdir = $pp_destdir"

        if $pp_opt_probe; then
	    pp_backend_init
            pp_backend_probe
        elif $pp_opt_vas_platforms; then
	    pp_backend_init
            pp_backend_vas_platforms
	elif test -n "$pp_opt_eval"; then
	    #-- execute a shell command
	    eval "$pp_opt_eval" || exit
	else
	    pp_drive
	    if test -n "$pp_opt_install_script"; then
		pp_install_script
	    fi
        fi

	exit 0
}


pp_errors=false

if test -n "$TERM" -a -t 1 && (tput op) >/dev/null 2>/dev/null; then
   pp_col_redfg=`tput setf 4` 2>/dev/null
   pp_col_bluefg=`tput setf 1` 2>/dev/null
   pp_col_reset=`tput op` 2>/dev/null
else
   pp_col_redfg='['
   pp_col_bluefg='['
   pp_col_reset=']'
fi

pp__warn () {
	if test x"" = x"$pp_lineno"; then
	    echo "$1 $2" >&2
	else
	    echo "$1 line $pp_lineno: $2" >&2
	fi
}

pp_warn () {
	pp__warn "pp: ${pp_col_redfg}warning${pp_col_reset}" "$*"
}

pp_error () {
	pp__warn "pp: ${pp_col_redfg}error${pp_col_reset}" "$*"
	pp_errors=true
}

pp_die () {
	pp_error "$@"
	exit 1
}

pp_die_if_errors () {
	$pp_errors && pp_die "$@"
}

pp_debug () {
	$pp_opt_debug && echo "${pp_col_bluefg}debug${pp_col_reset} $*" >&2
}

pp_verbose () {
	$pp_opt_verbose && echo "pp: ${pp_col_bluefg}info${pp_col_reset} $*" >&2
	"$@";
}

pp_substitute () {
  sed -e 's,%(\([^)]*\)),`\1`,g' \
      -e 's,%{\([^}]*\)},${\1},g' \
      -e 's,$,,' |
  tr '' '\012' |
  sed -e '/^[^]/s/["$`\\]/\\&/g' \
      -e 's/^//' \
      -e '1s/^/echo "/' \
      -e '$s,$,",' \
      -e 's,,"echo ",g' |
  tr -d '\012' |
  tr '' '\012'
  echo
}

pp_incr () {
    eval "$1=\`expr \$$1 + 1\`"
}

pp_decr () {
    eval "$1=\`expr \$$1 - 1\`"
}

pp_check_var_is_defined () {
    if eval test -z "\"\$$1\""; then
	pp_error "\$$1: not set"
	eval "$1=undefined"
    fi
}

pp_contains () {
    case " $1 " in
       *" $2 "*) return 0;;
       *) return 1;;
    esac
}

pp_contains_all () {
    typeset _s _c
    _l="$1"; shift
    for _w
    do
	pp_contains "$_l" "$_w" || return 1
    done
    return 0
}

pp_contains_any () {
    typeset _s _c
    _l="$1"; shift
    for _w
    do
	pp_contains "$_l" "$_w" && return 0
    done
    return 1
}

pp_add_to_list () {
    if eval test -z \"\$$1\"; then
	eval $1='"$2"'
    elif eval pp_contains '"$'$1'"' '"$2"'; then
	: already there
    else
	eval $1='"$'$1' $2"'
    fi
}

pp_unique () {
    typeset result element
    result=
    for element
    do
	pp_add_to_list result $element
    done
    echo $result
}

pp_mode_strip_altaccess () {
    case "$1" in
	??????????[+.])
	    echo `echo "$1" | cut -b -10`;;
	*)
	    echo "$1";;
    esac
}

pp_mode_from_ls () {
   typeset umode gmode omode smode

   set -- `pp_mode_strip_altaccess "$1"`

   case "$1" in
	?--[-X]??????) umode=0;;
	?--[xs]??????) umode=1;;
	?-w[-X]??????) umode=2;;
	?-w[xs]??????) umode=3;;
	?r-[-X]??????) umode=4;;
	?r-[xs]??????) umode=5;;
	?rw[-X]??????) umode=6;;
	?rw[xs]??????) umode=7;;
	*) pp_error "bad user mode $1";;
   esac

   case "$1" in
	????--[-S]???) gmode=0;;
	????--[xs]???) gmode=1;;
	????-w[-S]???) gmode=2;;
	????-w[xs]???) gmode=3;;
	????r-[-X]???) gmode=4;;
	????r-[xs]???) gmode=5;;
	????rw[-X]???) gmode=6;;
	????rw[xs]???) gmode=7;;
	*) pp_error "bad group mode $1";;
   esac

   case "$1" in
	???????--[-T]) omode=0;;
	???????--[xt]) omode=1;;
	???????-w[-T]) omode=2;;
	???????-w[xt]) omode=3;;
	???????r-[-T]) omode=4;;
	???????r-[xt]) omode=5;;
	???????rw[-T]) omode=6;;
	???????rw[xt]) omode=7;;
	*) pp_error "bad other mode $1";;
   esac

   case "$1" in
	???[-x]??[-x]??[-x]) smode=;;
	???[-x]??[-x]??[tT]) smode=1;;
	???[-x]??[Ss]??[-x]) smode=2;;
	???[-x]??[Ss]??[tT]) smode=3;;
	???[Ss]??[-x]??[-x]) smode=4;;
	???[Ss]??[-x]??[tT]) smode=5;;
	???[Ss]??[Ss]??[-x]) smode=6;;
	???[Ss]??[Ss]??[tT]) smode=7;;
	*) pp_error "bad set-id mode $1";;
   esac

   echo "$smode$umode$gmode$omode"
}

pp_find_recurse () {
  pp_debug "find: ${1#$pp_destdir}/"
  for f in "$1"/.* "$1"/*; do
    case "$f" in */.|*/..) continue;; esac  # should never happen!
    if test -d "$f" -o -f "$f" -o -h "$f"; then
        if test -d "$f" -a ! -h "$f"; then
            echo "${f#$pp_destdir}/"
            pp_find_recurse "$f"
        else
            echo "${f#$pp_destdir}"
        fi
    fi
  done
}

pp_prepend () {
    #test -t && pp_warn "pp_prepend: stdin is a tty?"
    if test -f $1; then
        pp_debug "prepending to $1"
        mv $1 $1._prepend
        cat - $1._prepend >$1
        rm -f $1._prepend
    else
        pp_debug "prepend: creating $1"
        cat >$1
    fi
}

pp_note_file_used() {
    echo "$1" >> $pp_wrkdir/all.files
}

pp_create_dir_if_missing () {
    case "$1" in
        */) pp_error "pp_create_dir_if_missing: trailing / forbidden";;
	"") return 0;;
	*)  if test ! -d "$pp_destdir$1"; then
                pp_debug "fabricating directory $1/"
		pp_create_dir_if_missing "${1%/*}"
		mkdir "$pp_destdir$1" &&
                    pp_note_file_used "$1/"
		pp_remove_later "$1" &&
		chmod ${2:-755} "$pp_destdir$1"
	    fi;;
    esac
}

pp_add_file_if_missing () {
    typeset dir
    #-- check that the file isn't already declared in the component
    if test -s $pp_wrkdir/%files.${2:-run}; then
      awk "\$6 == \"$1\" {exit 1}" < $pp_wrkdir/%files.${2:-run} || return 1
    fi

    pp_create_dir_if_missing "${1%/*}"
    pp_debug "fabricating file $1"
    echo "f ${3:-755} - - ${4:--} $1" >> $pp_wrkdir/%files.${2:-run}
    pp_note_file_used "$1"
    pp_remove_later "$1"
    return 0
}

pp_add_transient_file () {
    test -f "$pp_destdir$1" && pp_die "$pp_destdir$1: exists"
    pp_create_dir_if_missing "${1%/*}"
    pp_debug "transient file $1"
    pp_note_file_used "$1"
    pp_remove_later "$1"
}

pp_remove_later () {
   {
	echo "$1"
	test -s $pp_wrkdir/pp_cleanup && cat $pp_wrkdir/pp_cleanup
   } > $pp_wrkdir/pp_cleanup.new
   mv $pp_wrkdir/pp_cleanup.new $pp_wrkdir/pp_cleanup
}

pp_ls_readlink () {
    if test -h "$1"; then
        ls -1ld "$1" | sed -ne 's,.* -> ,,p'
    else
        echo "$1: not a symbolic link" >&2
        return 1
    fi
}

pp_remove_later_now () {
    typeset f
    if test -s $pp_wrkdir/pp_cleanup; then
        pp_debug "pp_remove_later_now"
        while read f; do
            pp_debug "removing $pp_destdir$f"
	    if test -d $pp_destdir$f; then
		rmdir $pp_destdir$f
	    else
		rm $pp_destdir$f
	    fi
        done < $pp_wrkdir/pp_cleanup
        rm $pp_wrkdir/pp_cleanup
    fi
}

pp_readlink() {

pp_debug "&& pp_readlink_fn=$pp_readlink_fn"

    if test -n "$pp_readlink_fn"; then
pp_debug "&& calling $pp_readlink_fn $*"
        "$pp_readlink_fn" "$@"
    else
        readlink "$@"
    fi
}


pp_install_script_common () {
        cat <<-.

            # Automatically generated for
            #    $name $version ($pp_platform)
            # by PolyPackage $pp_version

            usage () {
              case "$1" in
              "list-services")
                echo "usage: \$0 list-services" ;;
              "list-components")
                echo "usage: \$0 list-components" ;;
              "list-files")
                echo "usage: \$0 list-files {cpt...|all}" ;;
              "install")
                echo "usage: \$0 install {cpt...|all}" ;;
              "uninstall")
                echo "usage: \$0 uninstall {cpt...|all}" ;;
              "start")
                echo "usage: \$0 start {svc...}" ;;
              "stop")
                echo "usage: \$0 stop {svc...}" ;;
              "print-platform")
                echo "usage: \$0 print-platform" ;;
              *)
                echo "usage: \$0 [-q] command [args]"
                echo "   list-services"
                echo "   list-components"
                echo "   list-files {cpt...|all}"
                echo "   install {cpt...|all}"
                echo "   uninstall {cpt...|all}"
                echo "   start {svc...}"
                echo "   stop {svc...}"
                echo "   print-platform"
                ;;
              esac >&2
              exit 1
            }

            if test x"\$1" = x"-q"; then
                shift
                verbose () { "\$@"; }
                verbosemsg () { : ; }
            else
                verbose () { echo "+ \$*"; "\$@"; }
                verbosemsg () { echo "\$*"; }
            fi
.
}


pp_functions () {
    typeset func deps allfuncs
    allfuncs=
    while test $# -gt 0; do
	pp_add_to_list allfuncs "$1"
	deps=`pp_backend_function "$1:depends"`
	shift
	set -- `pp_unique "$@" $deps`
    done

    for func in $allfuncs
    do
        pp_debug "generating function code for '$1'"
        echo ""
        echo "$func () {"
	case "$func" in
	    pp_mkgroup|pp_mkuser|pp_havelib) echo <<.;;
		if test \$# -lt 1; then
		    echo "$func: not enough arguments" >&2
		    return 1
		fi
.
	esac
        pp_backend_function "$func" || cat <<.
		echo "$func: not implemented" >&2
		return 1
.
        echo "}"
    done
}

pp_function () {
    pp_functions "$1"
}

pp_makevar () {
    #-- convert all non alpha/digits to underscores
    echo "$*" | tr -c '[a-z][A-Z][0-9]\012' '[_*]'
}

pp_getpwuid () {
    awk -F: '$3 == uid { if (!found) print $1; found=1; } END { if (!found) exit 1; }' uid="$1" \
	< /etc/passwd || pp_error "no local username for uid $1"
}

pp_getgrgid () {
    awk -F: '$3 == gid { if (!found) print $1; found=1; } END { if (!found) exit 1; }' gid="$1" \
	< /etc/group || pp_error "no local group for gid $1"
}

pp_backend_function_getopt () {
    cat <<'..'
pp_getopt () {
     _pp_optstring="$1"; shift; eval `_pp_getopt "$_pp_optstring"`
}
_pp_getopt_meta=s,[\\\\\"\'\`\$\&\;\(\)\{\}\#\%\ \	],\\\\\&,g
_pp_protect () {
    sed "$_pp_getopt_meta" <<. | tr '\012' ' '
$*
.
}
_pp_protect2 () {
    sed "s,^..,,$pp_getopt_meta" <<. | tr '\012' ' '
$*
.
}
_pp_nonl () {
    tr '\012' ' ' <<.
$*
.
}
_pp_getopt () {
    _pp_nonl '_pp_nonl set --; while test $# -gt 0; do case "$1" in "--") shift; break;;'
    sed 's/\([^: 	]:*\)/<@<\1>@>/g;
	 s/<@<\(.\):>@>/"-\1")  _pp_nonl -"\1"; _pp_protect "$2"; shift; shift;; "-\1"*) _pp_nonl -"\1"; _pp_protect2 "$1"; shift;;/g;s/<@<\(.\)>@>/ "-\1")  _pp_nonl -"\1"; shift;; "-\1"*) _pp_nonl -"\1"; _pp_tmp="$1"; shift; set -- -`_pp_protect2 "$_pp_tmp"` "$@";;/g' <<.
$1
.
    _pp_nonl '-*) echo "$1: unknown option">&2; return 1;; *) break;; esac; done; _pp_nonl --; while test $# -gt 0; do _pp_nonl "$1"; shift; done; echo'
    echo
}
..
}

pp_copy_unstripped () {
    typeset filedir realdir
    filedir="`dirname ${1#$pp_destdir}`"
    realdir="$pp_wrkdir/unstripped/$filedir"

    mkdir -p "$realdir"
    # Can't use hardlinks because `strip` modifies the original file in-place
    cp "$1" "$realdir"
}

pp_package_stripped_binaries () {
    (cd "$pp_wrkdir/unstripped" && tar -c .) \
     | gzip > "$name-dbg-$version.tar.gz"
    rm -rf "$pp_wrkdir/unstripped"
}

pp_strip_binaries () {
    if test x"$pp_opt_save_unstripped" = x"true"; then
	rm  -rf "$pp_wrkdir/unstripped"
	mkdir "$pp_wrkdir/unstripped"
    fi

    for f in `find "$pp_destdir" -type f`; do
	if file "$f" | awk '{print $2}' | grep ^ELF >/dev/null 2>&1; then
	    if test x"$pp_opt_save_unstripped" = x"true"; then
		if file "$f" | LC_MESSAGES=C grep 'not stripped' >/dev/null 2>&1; then
		    pp_debug "Saving unstripped binary $f"
		    pp_copy_unstripped "$f"
		else
		    pp_debug "$f is already stripped; not saving a copy"
		fi
	    fi
	    pp_debug "Stripping unnecessary symbols from $f"
	    strip "$f"
	fi
    done

    if test x"$pp_opt_save_unstripped" = x"true"; then
	pp_package_stripped_binaries
    fi
}

pp_if_true=0
pp_if_false=0

pp_frontend_init () {
    name=
    version=
    summary="no summary"
    description="No description"
    copyright="Copyright 2012 Quest Software, Inc. ALL RIGHTS RESERVED."

    #-- if the user supplied extra arguments on the command line
    #   then load them now.
    pp_debug "pp_opt_init_vars=$pp_opt_init_vars"
    test -n "$pp_opt_init_vars" && eval "$pp_opt_init_vars"
}

pp_is_qualifier () {
    typeset ret

    case "$1" in
        "["*"]") ret=true;;
        *)       ret=false;;
    esac
    pp_debug "is_qualifier: $* -> $ret"
    test $ret = true
}

pp_eval_qualifier () {
    typeset ret

    case "$1" in
        "[!$pp_platform]"| \
         "[!"*",$pp_platform]"| \
         "[!$pp_platform,"*"]"| \
         "[!"*",$pp_platform,"*"]") ret=false;;
        "[!"*"]") ret=true;;
        "[$pp_platform]"| \
         "["*",$pp_platform]"| \
         "[$pp_platform,"*"]"| \
         "["*",$pp_platform,"*"]") ret=true;;
        "["*"]") ret=false;;
        *) pp_die "pp_eval_qualifier: bad qualifier '$1'"
    esac
    pp_debug "eval: $* -> $ret"
    test true = $ret
}

pp_frontend_if () {
    typeset ifcmd ifret
    ifcmd="$1";
    shift
    case "$ifcmd" in
	%if) if test 0 = $pp_if_false; then
		case "$*" in
		    true |1) pp_incr pp_if_true;;
		    false|0) pp_incr pp_if_false;;
                    *)
			ifret=true
                        if pp_is_qualifier "$*"; then
                            pp_eval_qualifier "$*" || ifret=false
                        else
			    eval test "$@" || ifret=false
			    pp_debug "evaluating test $* -> $ifret"
			fi
			pp_incr pp_if_$ifret
                        ;;
		esac
	     else
		pp_incr pp_if_false
	     fi;;
	%else)  test $# = 0 || pp_warn "ignoring argument to %else"
		if test $pp_if_false -gt 1; then
		  : no change
		elif test $pp_if_false = 1; then
		  pp_incr pp_if_true
		  pp_decr pp_if_false
		elif test $pp_if_true = 0; then
		  pp_die "unmatched %else"
		else
		  pp_incr pp_if_false
		  pp_decr pp_if_true
		fi;;
	%endif) test $# = 0 || pp_warn "ignoring argument to %endif"
		if test $pp_if_false -gt 0; then
		  pp_decr pp_if_false
		elif test $pp_if_true -gt 0; then
		  pp_decr pp_if_true
		else
		  pp_die "unmatched %endif"
		fi;;
	*) pp_die "frontend_if: unknown cmd $ifcmd";;
    esac
}


pp_frontend () {
  typeset section newsection sed_word sed_ws line cpt svc
  typeset section_enabled newsection_enabled s sed sed_candidate

  section='%_initial'
  newsection='%_initial'
  section_enabled=:
  newsection_enabled=:
  sed_word="[a-zA-Z_][a-zA-Z_0-9]*"
  sed_ws="[ 	]"

  #-- not all seds are created equal
  sed=
  for sed_candidate in ${PP_SED:-sed} /usr/xpg4/bin/sed; do
      if echo 'foo' | $sed_candidate -ne '/^\(x\)*foo/p' | grep foo > /dev/null
      then
        sed="$sed_candidate"
        break
      fi
  done
  test -z "$sed" &&
        pp_die "sed is broken on this system"

  pp_lineno=0

  #-- Note: this sed script should perform similar to pp_eval_qualifier()
  $sed -e "/^#/s/.*//" \
       -e "/^\\[!\\($sed_word,\\)*$pp_platform\\(,$sed_word\\)*\\]/s/.*//" \
       -e "s/^\\[\\($sed_word,\\)*$pp_platform\\(,$sed_word\\)*\\]$sed_ws*//" \
       -e "s/^\\[!\\($sed_word,\\)*$sed_word\\]$sed_ws*//" \
       -e "/^\\[\\($sed_word,\\)*$sed_word\\]/s/.*//" \
       -e "s/^%$sed_ws*/%/" \
       -e "s/^$sed_ws/%\\\\&/" \
     > $pp_wrkdir/frontend.tmp

  #-- add an ignore section at the end to force section completion
  echo '%ignore' >> $pp_wrkdir/frontend.tmp
  echo  >> $pp_wrkdir/frontend.tmp

  exec 0<$pp_wrkdir/frontend.tmp
  : > $pp_wrkdir/tmp
  : > $pp_wrkdir/%fixup
  while read -r line; do
     #-- Convert leading double-% to single-%, or switch sections
     pp_incr pp_lineno

     pp_debug "line $pp_lineno: $line"
     set -f
     set -- $line
     set +f
     #pp_debug "line $pp_lineno: $*"

     case "$line" in %*)
        case "$1" in
	   %if|%else|%endif)
                pp_debug "processing if directive $1"
	   	pp_frontend_if "$@"
		continue;;
	esac
	test 0 -ne $pp_if_false && continue	# ignore lines %if'd out

        case "$1" in
	  %set|%fixup|%ignore)
             pp_debug "processing new section $1"
	     newsection="$1"; shift
             newsection_enabled=:
             if pp_is_qualifier "$1"; then
                pp_eval_qualifier "$1" || newsection_enabled=false
                shift
             fi
	     test $# -eq 0 || pp_warn "ignoring extra arguments: $line"
	     continue;;
	  %pre|%post|%preun|%postup|%postun|%files|%depend|%check|%conflict)
             pp_debug "processing new component section $*"
             s="$1"; shift
             if test $# -eq 0 || pp_is_qualifier "$1"; then
                cpt=run
             else
                cpt="$1"
                shift
             fi
             newsection="$s.$cpt"
             newsection_enabled=:
             if test $# -gt 0 && pp_is_qualifier "$1"; then
                pp_eval_qualifier "$1" || newsection_enabled=false
                shift
             fi
             test $# -eq 0 ||
                pp_warn "ignoring extra arguments: $line"
             case "$cpt" in
                run|dbg|doc|dev)
                    $newsection_enabled && pp_add_component "$cpt";;
                x-*) :;;    # useful for discarding stuff
                *) pp_error "unknown component: $1 $cpt";;
             esac
	     continue;;
          %pp)
            newsection="%ignore"; shift
            if test $# -gt 0; then
                pp_set_api_version "$1"
                shift
            else
                pp_error "%pp: missing version"
            fi
            test $# -gt 0 &&
                pp_error "%pp: too many arguments"
            continue;;
	  %service)
             pp_debug "processing new service section $1 $2"
             s="$1"; shift
             if test $# -eq 0 || pp_is_qualifier "$1"; then
                pp_error "$s: service name required"
                svc=unknown
             else
                svc="$1"; shift
             fi

	     newsection="$s.$svc"
             newsection_enabled=:
	     if test $# -gt 0 && pp_is_qualifier "$1"; then
                pp_eval_qualifier "$1" || newsection_enabled=false
                shift
             fi
             test $# -eq 0 ||
                pp_warn "ignoring extra arguments: $line"
	     $newsection_enabled && pp_add_service "$svc"
	     continue;;
	  %\\*)
             pp_debug "removing leading %\\"
	     line="${line#??}"
             pp_debug "  result is <$line>"
             set -f
             set -- $line
             set +f
             ;;
	  %%*)
             pp_debug "removing leading %"
	     line="${line#%}"
             set -f
             set -- $line
             set +f
	     ;;
	  %*)
	     pp_error "unknown section $1"
	     newsection='%ignore'
             newsection_enabled=:
	     continue;;
	esac;;
     esac

     test 0 != $pp_if_false && continue	# ignore lines %if'd out

     pp_debug "section=$section (enabled=$section_enabled) newsection=$newsection (enabled=$newsection_enabled)"

     #-- finish processing a previous section
     if test x"$newsection" != x""; then
      $section_enabled && case "$section" in
     	%ignore|%_initial)
                pp_debug "leaving ignored section $section"
		: ignore  # guaranteed to be the last section
		;;
	%set)
                pp_debug "leaving $section: sourcing $pp_wrkdir/tmp"
                $pp_opt_debug && cat $pp_wrkdir/tmp >&2
		. $pp_wrkdir/tmp
		: > $pp_wrkdir/tmp
		;;
	%pre.*|%preun.*|%post.*|%postup.*|%postun.*|%depend.*|%check.*|%conflict.*|%service.*|%fixup)
                pp_debug "leaving $section: substituting $pp_wrkdir/tmp"
                # cat $pp_wrkdir/tmp >&2    # debugging
                $pp_opt_debug && pp_substitute < $pp_wrkdir/tmp >&2
		pp_substitute < $pp_wrkdir/tmp > $pp_wrkdir/tmp.sh
                . $pp_wrkdir/tmp.sh >> $pp_wrkdir/$section ||
                    pp_error "shell error in $section"
		rm -f $pp_wrkdir/tmp.sh
		: > $pp_wrkdir/tmp
		;;
      esac
      section="$newsection"
      section_enabled="$newsection_enabled"
      newsection=
     fi

     #-- ignore section content that is disabled
     $section_enabled || continue

     #-- process some lines in-place
     case "$section" in
	%_initial)
		case "$line" in "") continue;; esac # ignore non-section blanks
		pp_die "Ignoring text before % section introducer";;
	%set|%pre.*|%preun.*|%post.*|%postup.*|%postun.*|%check.*|%service.*|%fixup)
                pp_debug "appending line to \$pp_wrkdir/tmp"
		echo "$line" >> $pp_wrkdir/tmp
		;;
	%files.*)
		test $# -eq 0 && continue;
		pp_files_expand "$@" >> $pp_wrkdir/$section
		;;
	%depend.*)
		pp_debug "Adding explicit dependency $@ to $cpt"
		echo "$@" >> $pp_wrkdir/%depend.$cpt
		;;
	%conflict.*)
		pp_debug "Adding explicit conflict $@ to $cpt"
		echo "$@" >> $pp_wrkdir/%conflict.$cpt
		;;
     esac
  done
  exec <&-

  if test $pp_if_true != 0 -o $pp_if_false != 0; then
	pp_die "missing %endif at end of file"
  fi

  pp_lineno=

  pp_debug " name        = $name"
  pp_debug " version     = $version"
  pp_debug " summary     = $summary"
  pp_debug " description = $description"
  pp_debug " copyright   = $copyright"
  pp_debug ""
  pp_debug "\$pp_components: $pp_components"
  pp_debug "\$pp_services:   $pp_services"
}

pp_set_api_version() {
    case "$1" in
        1.0)    : ;;
        *)      pp_error "This version of polypackage is too old";;
    esac
}

pp_platform=

pp_set_platform () {
    if test -n "$pp_opt_platform"; then
	pp_contains "$pp_platforms" "$pp_opt_platform" ||
		pp_die "$pp_opt_platform: unknown platform"
	pp_platform="$pp_opt_platform"
    else
	uname_s=`uname -s 2>/dev/null`
	pp_platform=
	for p in $pp_platforms; do
	    pp_debug "probing for platform $p"
	    if eval pp_backend_${p}_detect "$uname_s"; then
		pp_platform="$p"
		break;
	    fi
	done
	test -z "$pp_platform" &&
		pp_die "cannot detect platform (supported: $pp_platforms)"
    fi
    pp_debug "pp_platform = $pp_platform"
}

pp_expand_path=

pp_expand_test_usr_bin () {
	awk '$1 == "/usr" || $2 == "/usr" {usr++}
	     $1 == "/bin" || $2 == "/bin" {bin++}
	     END { if (usr == 1 && bin == 1) exit(0); else exit(1); }'
}

pp_set_expand_converter_or_reexec () {
    test -d /usr -a -d /bin ||
	pp_die "missing /usr or /bin"
    echo /usr /bin | pp_expand_test_usr_bin || pp_die "pp_expand_test_usr_bin?"
    if (eval "echo /{usr,bin}" | pp_expand_test_usr_bin) 2>/dev/null; then
	pp_expand_path=pp_expand_path_brace
    elif (eval "echo /@(usr|bin)" | pp_expand_test_usr_bin) 2>/dev/null; then
	pp_expand_path=pp_expand_path_at
    else
	test x"$pp_expand_rexec" != x"true" ||
	    pp_die "problem finding shell that can do brace expansion"
	for shell in ksh ksh93 bash; do
	    if ($shell -c 'echo /{usr,bin}' |
			pp_expand_test_usr_bin) 2>/dev/null ||
	       ($shell -c 'echo /@(usr|bin)' |
			pp_expand_test_usr_bin) 2>/dev/null
	    then
                pp_debug "switching to shell $shell"
		pp_expand_rexec=true exec $shell "$0" "$@"
	    fi
	done
	pp_die "cannot find a shell that does brace expansion"
    fi
}

pp_expand_path_brace () {
	typeset f
	eval "for f in $1; do echo \"\$f\"; done|sort -u"
}

pp_expand_path_at () {
	typeset f
	eval "for f in `
	    echo "$1" | sed -e 's/{/@(/g' -e 's/}/)/g' -e 's/,/|/g'
		`; do echo \"\$f\"; done|sort -u"
}

pp_shlib_suffix='.so*'

pp_model_init () {
    #@ $pp_components: whitespace-delimited list of components seen in %files
    pp_components=
    #@ $pp_services: whitespace-delimited list of %service seen
    pp_services=

    rm -f $pp_wrkdir/%files.* \
          $pp_wrkdir/%post.* \
          $pp_wrkdir/%pre.* \
          $pp_wrkdir/%preun.* \
          $pp_wrkdir/%postup.* \
          $pp_wrkdir/%postun.* \
          $pp_wrkdir/%service.* \
          $pp_wrkdir/%set \
          $pp_wrkdir/%fixup
}


pp_have_component () {
	pp_contains "$pp_components" "$1"
}

pp_have_all_components () {
	pp_contains_all "$pp_components" "$@"
}

pp_add_component () {
	pp_add_to_list 'pp_components' "$1"
}

pp_add_service () {
	pp_add_to_list 'pp_services' "$1"
}

pp_service_init_vars () {
	cmd=
	pidfile=
	stop_signal=15		# SIGTERM
	user=root
	group=
	enable=yes		# make it so the service starts on boot
	optional=no		# Whether installing this service is optional
	pp_backend_init_svc_vars
}

pp_service_check_vars () {
	test -n "$cmd" ||
		pp_error "%service $1: cmd not defined"
	case "$enable" in
	    yes|no) : ;;
	    *) pp_error "%service $1: \$enable must be set to yes or no";;
	esac
}

pp_load_service_vars () {
	pp_service_init_vars
	. "$pp_wrkdir/%service.$1"
	pp_service_check_vars "$1"
}

pp_files_expand () {
    typeset _p _mode _group _owner _flags _path _optional _has_target _tree
    typeset _path _file _tgt _m _o _g _f _type _lm _ll _lo _lg _ls _lx
    typeset _ignore _a

    test $# -eq 0 && return

    pp_debug "pp_files_expand: path is: $1"

    case "$1" in "#"*) return;; esac
    _p="$1"; shift

    pp_debug "pp_files_expand: other arguments: $*"

    #-- the mode must be an octal number of at least three digits
    _mode="="
    _a=`eval echo \"$1\"`
    case "$_a" in
	*:*) :;;
	-|=|[01234567][01234567][01234567]*) _mode="$_a"; shift;;
    esac

    #-- the owner:group field may have optional parts
    _a=`eval echo \"$1\"`
    case "$_a" in
	*:*) _group=${_a#*:}; _owner=${_a%:*}; shift;;
	=|-) _group=$_a;      _owner=$_a; shift;;
	*)   _group=;         _owner=;;
    esac

    #-- process the flags argument
    _flags=
    _optional=false
    _has_target=false
    _ignore=false
    if test $# -gt 0; then
        _a=`eval echo \"$1\"`
	case ",$_a," in *,volatile,*) _flags="${_flags}v";; esac
	case ",$_a," in *,optional,*) _optional=true;; esac
	case ",$_a," in *,symlink,*) _has_target=true;; esac
	case ",$_a," in *,ignore-others,*) _flags="${_flags}i";; esac
	case ",$_a," in *,ignore,*) _ignore=true;; esac
	shift
    fi

    #-- process the target argument
    if $_has_target; then
	test $# -ne 0 || pp_error "$_p: missing target"
	_a=`eval echo \"$1\"`
	_target="$_a"
	shift
    fi

    pp_debug "pp_files_expand: $_mode|$_owner:$_group|$_flags|$_target|$*"

    test $# -eq 0 || pp_error "$_p: too many arguments"

    #-- process speciall suffixes
    tree=
    case "$_p" in
        *"/**")  _p="${_p%"/**"}"; tree="**";;
        *".%so") _p="${_p%".%so"}$pp_shlib_suffix";;
    esac

    #-- expand the path using the shell glob
    pp_debug "expanding .$_p ... with $pp_expand_path"
    (cd ${pp_destdir} && $pp_expand_path ".$_p") > $pp_wrkdir/tmp.files.exp

    #-- expand path/** by rewriting the glob output file
    case "$tree" in
        "") : ;;
        "**")
            pp_debug "expanding /** tree ..."
            while read _path; do
                _path="${_path#.}"
                pp_find_recurse "$pp_destdir${_path%/}"
            done < $pp_wrkdir/tmp.files.exp |
                 sort -u > $pp_wrkdir/tmp.files.exp2
            mv $pp_wrkdir/tmp.files.exp2 $pp_wrkdir/tmp.files.exp
            ;;
    esac

    while read _path; do
	_path="${_path#.}"
	_file="${pp_destdir}${_path}"
	_tgt=
	_m="$_mode"
	_o="${_owner:--}"
	_g="${_group:--}"
	_f="$_flags"

        case "$_path" in
            /*) :;;
            *)  pp_warn "$_path: inserting leading /"
                _path="/$_path";;  # ensure leading /
        esac

        #-- sanity checks
        case "$_path" in
            */../*|*/..) pp_error "$_path: invalid .. in path";;
            */./*|*/.)   pp_warn  "$_path: invalid component . in path";;
            *//*)        pp_warn  "$_path: redundant / in path";;
        esac

	#-- set the type based on the real file's type
        if $_ignore; then
           _type=f _m=_ _o=_ _g=_
	elif test -h "$_file"; then
	   case "$_path" in
		*/) pp_warn "$_path (symlink $_file): removing trailing /"
		    _path="${_path%/}"
		    ;;
	   esac
	   _type=s
	   if test x"$_target" != x"=" -a -n "$_target"; then
	       _tgt="$_target"
pp_debug "symlink target is $_tgt"
	   else
	       _tgt=`pp_readlink "$_file"`;
               test -z "$_tgt" && pp_error "can't readlink $_file"
               case "$_tgt" in
                    ${pp_destdir}/*)
                       pp_warn "stripped \$destdir from symlink ($_path)"
                       _tgt="${_tgt#$pp_destdir}";;
               esac
	   fi
	   _m=777
	elif test -d "$_file"; then
	   #-- display a warning if the user forgot the trailing /
	   case "$_path" in
		*/) :;;
		*) pp_warn "$_path (matching $_file): adding trailing /"
		   _path="$_path/";;
	   esac
	   _type=d
	   $_has_target && pp_error "$_file: not a symlink"
	elif test -f "$_file"; then
	   case "$_path" in
		*/) pp_warn "$_path (matching $_file): removing trailing /"
		    _path="${_path%/}"
		    ;;
	   esac
	   _type=f
	   $_has_target && pp_error "$_file: not a symlink"
	else
	   $_optional && continue
	   pp_error "$_file: missing"
	   _type=f
	fi

	#-- convert '=' shortcuts into mode/owner/group from ls
	case ":$_m:$_o:$_g:" in *:=:*)
	    if LS_OPTIONS=--color=never /bin/ls -ld "$_file" \
		    > $pp_wrkdir/ls.tmp
	    then
                read _lm _ll _lo _lg _ls _lx < $pp_wrkdir/ls.tmp
                test x"$_m" = x"=" && _m=`pp_mode_from_ls "$_lm"`
                test x"$_o" = x"=" && _o="$_lo"
                test x"$_g" = x"=" && _g="$_lg"
            else
                pp_error "cannot read $_file"
                test x"$_m" = x"=" && _m=-
                test x"$_o" = x"=" && _o=-
                test x"$_g" = x"=" && _g=-
            fi
	    ;;
	esac

	test -n "$_f" || _f=-

	#-- sanity checks
	test -n "$_type" || pp_die "_type empty"
	test -n "$_path" || pp_die "_path empty"
	test -n "$_m" || pp_die "_m empty"
	test -n "$_o" || pp_die "_o empty"
	test -n "$_g" || pp_die "_g empty"

	#-- setuid/gid files must be given an explicit owner/group (or =)
	case "$_o:$_g:$_m" in
	    -:*:[4657][1357]??|-:*:[4657]?[1357]?|-:*:[4657]??[1357])
		pp_error "$_path: setuid file ($_m) missing explicit owner";;
	    *:-:[2367][1357]??|*:-:[2367]?[1357]?|*:-:[2367]??[1357])
		pp_error "$_path: setgid file ($_m) missing explicit group";;
	esac

	# convert numeric uids into usernames; only works for /etc/passwd
	case "$_o" in [0-9]*) _o=`pp_getpwuid $_o`;; esac
	case "$_g" in [0-9]*) _g=`pp_getgrgid $_g`;; esac

	pp_debug "$_type $_m $_o $_g $_f $_path" $_tgt
	$_ignore || echo "$_type $_m $_o $_g $_f $_path" $_tgt
        pp_note_file_used "$_path"
        case "$_f" in *i*) echo "$_path" >> $pp_wrkdir/ign.files;; esac
    done < $pp_wrkdir/tmp.files.exp
}

pp_files_check_duplicates () {
    typeset _path
    if test -s $pp_wrkdir/all.files; then
        sort < $pp_wrkdir/all.files | uniq -d > $pp_wrkdir/duplicate.files
	if test -f $pp_wrkdir/ign.awk; then
	    # Remove ignored files
	    mv $pp_wrkdir/duplicate.files $pp_wrkdir/duplicate.files.ign
	    sed -e 's/^/_ _ _ _ _ /' < $pp_wrkdir/duplicate.files.ign |
		awk -f $pp_wrkdir/ign.awk |
		sed -e 's/^_ _ _ _ _ //' > $pp_wrkdir/duplicate.files
	fi
        while read _path; do
            pp_warn "$_path: file declared more than once"
        done <$pp_wrkdir/duplicate.files
    fi
}

pp_files_check_coverage () {
    pp_find_recurse "$pp_destdir" | sort > $pp_wrkdir/coverage.avail
    if test -s $pp_wrkdir/all.files; then
        sort -u < $pp_wrkdir/all.files
    else
        :
    fi > $pp_wrkdir/coverage.used
    join -v1 $pp_wrkdir/coverage.avail $pp_wrkdir/coverage.used \
        > $pp_wrkdir/coverage.not-packaged
    if test -s $pp_wrkdir/coverage.not-packaged; then
        pp_warn "The following files/directories were found but not packaged:"
        sed -e 's,^,    ,' <  $pp_wrkdir/coverage.not-packaged >&2
    fi
    join -v2 $pp_wrkdir/coverage.avail $pp_wrkdir/coverage.used \
        > $pp_wrkdir/coverage.not-avail
    if test -s $pp_wrkdir/coverage.not-avail; then
        pp_warn "The following files/directories were named but not found:"
        sed -e 's,^,    ,' <  $pp_wrkdir/coverage.not-avail >&2
    fi
}

pp_files_ignore_others () {
    typeset p f

    test -s $pp_wrkdir/ign.files || return

    #-- for each file in ign.files, we remove it from all the
    #   other %files.* lists, except where it has an i flag.
    #   rather than scan each list multiple times, we build
    #   an awk script

    pp_debug "stripping ignore files"

    while read p; do
        echo '$6 == "'"$p"'" && $5 !~ /i/ { next }'
    done < $pp_wrkdir/ign.files > $pp_wrkdir/ign.awk
    echo '{ print }' >> $pp_wrkdir/ign.awk

    $pp_opt_debug && cat $pp_wrkdir/ign.awk

    for f in $pp_wrkdir/%files.*; do
	mv $f $f.ign
        awk -f $pp_wrkdir/ign.awk < $f.ign > $f || pp_error "awk"
    done
}

pp_service_scan_groups () {
    typeset svc

    #-- scan for "group" commands, and build a list of groups
    pp_service_groups=
    if test -n "$pp_services"; then
        for svc in $pp_services; do
	    group=
	    . $pp_wrkdir/%service.$svc
	    if test -n "$group"; then
		pp_contains "$pp_services" "$group" && pp_error \
		    "%service $svc: group name $group in use by a service"
		pp_add_to_list 'pp_service_groups' "$group"
		echo "$svc" >> $pp_wrkdir/%svcgrp.$group
	    fi
        done
    fi
}

pp_service_get_svc_group () {
    (tr '\012' ' ' < $pp_wrkdir/%svcgrp.$1 ; echo) | sed -e 's/ $//'
}

for _sufx in _init '' _names _cleanup _install_script \
    _init_svc_vars _function _probe _vas_platforms
do
 eval "pp_backend$_sufx () { pp_debug pp_backend$_sufx; pp_backend_\${pp_platform}$_sufx \"\$@\"; }"
done


pp_platforms="$pp_platforms aix"

pp_backend_aix_detect () {
	test x"$1" = x"AIX"
}

pp_backend_aix_init () {
        pp_aix_detect_arch
        pp_aix_detect_os

	pp_aix_bosboot=		# components that need bosboot
	pp_aix_lang=en_US
	pp_aix_copyright=
        pp_aix_start_services_after_install=false
        pp_aix_init_services_after_install=true

        pp_aix_sudo=sudo	# AIX package tools must run as root

        case "$pp_aix_os" in
            *) pp_readlink_fn=pp_ls_readlink;;  # XXX
        esac

	pp_aix_abis_seen=
}

pp_aix_detect_arch () {
	pp_aix_arch_p=`uname -p 2>/dev/null`
	case "$pp_aix_arch_p"  in
	   "")      pp_debug "can't get processor type from uname -p"
                    pp_aix_arch_p=powerpc
                    pp_aix_arch=R;;  # guess (lsattr -l proc0 ??)
	   powerpc) pp_aix_arch=R;;
	   *)       pp_aix_arch_p=intel
                    pp_aix_arch=I;;  # XXX? verify
	esac

	case "`/usr/sbin/lsattr -El proc0 -a type -F value`" in
	    PowerPC_POWER*) pp_aix_arch_std=ppc64;;
	    PowerPC*) pp_aix_arch_std=ppc;;
	    *) pp_aix_arch_std=unknown;;
	esac
}

pp_aix_detect_os () {
        typeset r v

        r=`uname -r`
        v=`uname -v`
        pp_aix_os=aix$v$r
}

pp_aix_version_fix () {
    typeset v
    v=`echo $1 | sed 's/[-+]/./' | tr -c -d '[0-9].\012' | awk -F"." '{ printf "%d.%d.%d.%.4s\n", $1, $2, $3, $4 }' | sed 's/[.]*$//g'`
    if test x"$v" != x"$1"; then
        pp_warn "stripped version '$1' to '$v'"
    fi
    case $v in
        ""|*..*|.*|*.) pp_error "malformed '$1'"
                 echo "0.0.0.0";;
        *.*.*.*.*)
                 # 5 components are only valid for fileset updates, not base
                 # filesets (full packages). We trim 5+ components down to 4.
                 pp_warn "version '$1' has too many dots for AIX, truncating"
                 echo "$v" | cut -d. -f1-4;;
        *.*.*.*) echo "$v";;
        *.*.*) echo "$v.0";;
        *.*) echo "$v.0.0";;
        *) echo "$v.0.0.0";;
    esac
}

pp_aix_select () {
	case "$1" in
	    -user) op="";;
	    -root) op="!";;
	    *) pp_die "pp_aix_select: bad argument";;
	esac
	#pp_debug awk '$5 '$op' /^\/(usr|opt)(\/|$)/ { print; }'
	#awk '$5 '$op' /^\/(usr|opt)(\/|$)/ { print; }'
	awk $op'($6 ~ /^\/usr\// || $6 ~ /^\/opt\//) { print; }'
}

pp_aix_copy_root () {
    typeset t m o g f p st target
    while read t m o g f p st; do
        case "$t" in
           d) pp_create_dir_if_missing "$1${p%/}";;
           f) pp_add_transient_file "$1$p"
	      pp_verbose ln "$pp_destdir$p" "$pp_destdir$1$p" ||
		pp_error "can't link $p into $1";;
           *) pp_warn "pp_aix_copy_root: filetype $t not handled";;
        esac
    done
}


pp_aix_size () {
    typeset prefix t m o g f p st

    prefix="$1"
    while read t m o g f p st; do
      case "$t" in f) du -a "$pp_destdir$p";; esac
    done | sed -e 's!/[^/]*$!!' | sort +1 |
    awk '{ if ($2 != d)
           { if (sz) print d,sz;
             d=$2; sz=0 }
           sz += $1; }
         END { if (sz) print d,sz }' |
    sed -n -e "s!^$pp_destdir!$prefix!p"
}

pp_aix_list () {
    awk '{ print "." pfx $6; }' pfx="$1"
}

pp_aix_make_liblpp () {
    typeset out dn fl f

    out="$1"; shift
    dn=`dirname "$2"`
    fl=
    for f
    do
	case "$f" in "$dn/"*) fl="$fl `basename $f`" ;;
		     *) pp_die "liblpp name $f not in $dn/";; esac
    done
    (cd "$dn" && pp_verbose  ar -c -g -r "$out" $fl) || pp_error "ar error"
}

pp_aix_make_script () {
    rm -f "$1"
    echo "#!/bin/sh" > "$1"
    cat >> "$1"
    echo "exit 0" >> "$1"
    chmod +x "$1"
}

pp_aix_inventory () {
    typeset fileset t m o g f p st type

    fileset="$1"
    while read t m o g f p st; do
      case "$p" in *:*) pp_error "path $p contains colon";; esac
      echo "$p:"
      case "$t" in
	f)   type=FILE;      defm=644 ;;
	s)   type=SYMLINK;   defm=777 ;;
	d)   type=DIRECTORY; defm=755 ;;
      esac
      echo " type = $type"
      echo " class = inventory,apply,$fileset"
      if test x"$m" = x"-"; then m="$defm"; fi
      if test x"$o" = x"-"; then o="root"; fi
      if test x"$g" = x"-"; then g="system"; fi
      echo " owner = $o"
      echo " group = $g"

      case "$m" in ????)
	m=`echo $m|sed -e 's/^1/TCB,/' \
		       -e 's/^[23]/TCB,SGID,/' \
		       -e 's/^[45]/TCB,SUID,/' \
		       -e 's/^[67]/TCB,SUID,SGID,/'`;;  # vtx bit ignored
      esac
      echo " mode = $m"
      case "$t" in
	f) if test ! -f "$pp_destdir$p"; then
		pp_error "$p: missing file"
	   fi
	   case "$flags" in
	    *v*)
	      echo " size = VOLATILE"
	      echo " checksum = VOLATILE"
	      ;;
	    *)
	      if test -r "$pp_destdir$p"; then
	        echo " size = $size"
                pp_verbose  sum -r < "$pp_destdir$p" |
	      	  sed -e 's/.*/ checksum = "&"/'
	      fi
	      ;;
	   esac;;
	s)
	   echo " target = $st"
	   ;;
      esac

      #-- Record ABI types seen
      case "$t" in
        f) if test -r "$pp_destdir$p"; then
	    case "`file "$pp_destdir$p"`" in
		*"executable (RISC System/6000)"*) abi=ppc;;
		*"64-bit XCOFF executable"*)       abi=ppc64;;
		*) abi=;;
	    esac
	    if test -n "$abi"; then
		pp_add_to_list pp_aix_abis_seen $abi
	    fi
	   fi;;
      esac

    done
}

pp_aix_depend ()
{
    if test -s "$1"; then
       pp_warn "aix dependencies not implemented"
    fi
}

pp_aix_add_service () {
	typeset svc cmd_cmd cmd_arg f
	svc="$1"

	pp_load_service_vars $svc

	set -- $cmd
	cmd_cmd="$1"; shift
	cmd_arg="${pp_aix_mkssys_cmd_args:-$*}";

	case "$stop_signal" in
		HUP) stop_signal=1;;
		INT) stop_signal=2;;
		QUIT) stop_signal=3;;
		KILL) stop_signal=9;;
		TERM) stop_signal=15;;
		USR1) stop_signal=30;;
		USR2) stop_signal=31;;
		"")
		  pp_error "%service $svc: stop_signal not set";;
		[a-zA-Z]*)
		  pp_error "%service $svc: bad stop_signal ($stop_signal)";;
	esac

	test -z "$pidfile" || pp_error "aix requires empty pidfile (non daemon)"

	pp_add_component run
	if test "$user" = "root"; then
	    uid=0
	else
            uid="\"\`/usr/bin/id -u $user\`\""
	fi


        #-- add command text to create/remove the service
	cat <<-. >> $pp_wrkdir/%post.$svc
svc=$svc
uid=0
cmd_cmd="$cmd_cmd"
cmd_arg="$cmd_arg"
stop_signal=$stop_signal
force_signal=9
srcgroup="$pp_aix_mkssys_group"
instances_allowed=${pp_aix_mkssys_instances_allowed:--Q}

lssrc -s \$svc > /dev/null 2>&1
if [ \$? -eq 0 ]; then
  lssrc -s \$svc | grep "active" > /dev/null 2>&1
  if [ \$? -eq 0 ]; then
    stopsrc -s \$svc > /dev/null 2>&1
  fi
  rmsys -s \$svc > /dev/null 2>&1
fi

mkssys -s \$svc -u \$uid -p "\$cmd_cmd" \${cmd_arg:+-a "\$cmd_arg"} -S -n \$stop_signal -f 9 ${pp_aix_mkssys_args} \${srcgroup:+-G \$srcgroup} \$instances_allowed
.

        #-- add code to start the service on reboot
        ${pp_aix_init_services_after_install} &&
          cat <<-. >> $pp_wrkdir/%post.$svc
id=\`echo "\$svc" | cut -c1-14\`
mkitab "\$id:2:once:/usr/bin/startsrc -s \$svc" > /dev/null 2>&1
.

	${pp_aix_start_services_after_install} &&
          cat <<-. >> $pp_wrkdir/%post.$svc
startsrc -s \$svc
.

if [ -f "$pp_wrkdir/%post.run" ];then
    cat $pp_wrkdir/%post.run >> $pp_wrkdir/%post.$svc
fi
mv $pp_wrkdir/%post.$svc $pp_wrkdir/%post.run


        ${pp_aix_init_services_after_install} &&
           pp_prepend $pp_wrkdir/%preun.$svc <<-.
rmitab `echo "$svc" | cut -c1-14` > /dev/null 2>&1
.
	pp_prepend $pp_wrkdir/%preun.$svc <<-.
stopsrc -s $svc >/dev/null 2>&1
rmssys -s $svc
.

if [ -f "$pp_wrkdir/%preun.run" ];then
    cat $pp_wrkdir/%preun.run >> $pp_wrkdir/%preun.$svc
fi
mv $pp_wrkdir/%preun.$svc $pp_wrkdir/%preun.run
}

pp_backend_aix () {
        typeset briefex instuser instroot svc cmp outbff
        typeset user_wrkdir root_wrkdir
        typeset user_files root_files

	test -n "$pp_destdir" ||
	   pp_error "AIX backend requires the '--destdir' option"

	instuser="/usr/lpp/$name"
	instroot="$instuser/inst_root"
	pp_aix_bff_name=${pp_aix_bff_name:-$name}

	# Here is the component mapping:
	#  run -> $pp_aix_bff_name.rte ('Run time environment')
	#  doc -> $pp_aix_bff_name.doc (non-standard)
	#  dev -> $pp_aix_bff_name.adt ('Application developer toolkit')
	#  dbg -> $pp_aix_bff_name.diag ('Diagnostics')

	test `echo "$summary" | wc -c ` -gt 40 && pp_error "\$summary too long"

	user_wrkdir=$pp_wrkdir/u
	root_wrkdir=$pp_wrkdir/r
	pp_verbose  rm -rf $user_wrkdir $root_wrkdir
	pp_verbose  mkdir -p $user_wrkdir $root_wrkdir

	for svc in $pp_services .; do
	    test . = "$svc" && continue
            pp_aix_add_service $svc
	done

        {
	  echo "4 $pp_aix_arch I $name {"

	  for cmp in $pp_components; do
	    case "$cmp" in
		run) ex=rte  briefex="runtime";;
		doc) ex=doc  briefex="documentation";;
		dev) ex=adt  briefex="developer toolkit";;
		dbg) ex=diag briefex="diagnostics";;
	    esac

	    user_files=$pp_wrkdir/%files.$cmp.u
	    root_files=$pp_wrkdir/%files.$cmp.r

	    pp_aix_select -user < $pp_wrkdir/%files.$cmp > $user_files
	    pp_aix_select -root < $pp_wrkdir/%files.$cmp > $root_files

            # Default to USR only unless there are root files,
            # or a post/pre/check script associated
	    content=U
            if test -s $root_files \
                    -o -s $pp_wrkdir/%pre.$cmp \
                    -o -s $pp_wrkdir/%post.$cmp \
                    -o -s $pp_wrkdir/%preun.$cmp \
                    -o -s $pp_wrkdir/%postun.$cmp \
                    -o -s $pp_wrkdir/%check.$cmp
            then
                content=B
            fi

            if $pp_opt_debug; then
                echo "$cmp USER %files:"
                cat $user_files
                echo "$cmp ROOT %files:"
                cat $root_files
            fi >&2

	    bosboot=N; pp_contains_any "$pp_aix_bosboot" $cmp && bosboot=b

            echo $pp_aix_bff_name.$ex \
             `[ $pp_aix_version ] && pp_aix_version_fix $pp_aix_version || pp_aix_version_fix "$version"` \
	         1 $bosboot $content \
	         $pp_aix_lang "$summary $briefex"
	    echo "["

	    pp_aix_depend $pp_wrkdir/%depend.$cmp

	    echo "%"

	    # generate per-directory size information
	    pp_aix_size < $user_files
	    pp_aix_size $instroot < $root_files

	    pp_aix_list            < $user_files  > $user_wrkdir/$pp_aix_bff_name.$ex.al
	    pp_aix_list $instroot  < $root_files >> $user_wrkdir/$pp_aix_bff_name.$ex.al
	    pp_aix_list            < $root_files  > $root_wrkdir/$pp_aix_bff_name.$ex.al

            if $pp_opt_debug; then
                echo "$cmp USER $pp_aix_bff_name.$ex.al:"
                cat $user_wrkdir/$pp_aix_bff_name.$ex.al
                echo "$cmp ROOT $pp_aix_bff_name.$ex.al:"
                cat $root_wrkdir/$pp_aix_bff_name.$ex.al
            fi >&2

	    pp_aix_inventory $pp_aix_bff_name.$ex < $user_files \
                                       > $user_wrkdir/$pp_aix_bff_name.$ex.inventory
	    pp_aix_inventory $pp_aix_bff_name.$ex < $root_files \
                                       > $root_wrkdir/$pp_aix_bff_name.$ex.inventory

            if $pp_opt_debug; then
                pp_debug "$cmp USER $pp_aix_bff_name.$ex.inventory:"
                cat $user_wrkdir/$pp_aix_bff_name.$ex.inventory
                pp_debug "$cmp ROOT $pp_aix_bff_name.$ex.inventory:"
                cat $root_wrkdir/$pp_aix_bff_name.$ex.inventory
            fi >&2

	    if test x"" != x"${pp_aix_copyright:-$copyright}"; then
	        echo "${pp_aix_copyright:-$copyright}" > $user_wrkdir/$pp_aix_bff_name.$ex.copyright
	        echo "${pp_aix_copyright:-$copyright}" > $root_wrkdir/$pp_aix_bff_name.$ex.copyright
	    fi

	    #-- assume that post/pre uninstall scripts only make
	    #   sense when installed in a root context

	    if test -r $pp_wrkdir/%pre.$cmp; then
			pp_aix_make_script $user_wrkdir/$pp_aix_bff_name.$ex.pre_i \
                < $pp_wrkdir/%pre.$cmp
	    fi

	    if test -r $pp_wrkdir/%post.$cmp; then
		pp_aix_make_script $root_wrkdir/$pp_aix_bff_name.$ex.post_i \
			< $pp_wrkdir/%post.$cmp
	    fi

	    if test -r $pp_wrkdir/%preun.$cmp; then
		pp_aix_make_script $root_wrkdir/$pp_aix_bff_name.$ex.unpost_i \
			< $pp_wrkdir/%preun.$cmp
	    fi

	    if test -r $pp_wrkdir/%postun.$cmp; then
		pp_aix_make_script $root_wrkdir/$pp_aix_bff_name.$ex.unpre_i \
			< $pp_wrkdir/%postun.$cmp
	    fi

	    # remove empty files
	    for f in $user_wrkdir/$pp_aix_bff_name.$ex.* $root_wrkdir/$pp_aix_bff_name.$ex.*; do
	      if test ! -s "$f"; then
                pp_debug "removing empty $f"
                rm -f "$f"
              fi
	    done

	    # copy/link the root files so we can do an easy backup later
	    pp_aix_copy_root $instroot < $root_files

	    echo "%"
	    echo "]"
	  done
	  echo "}"
	} > $pp_wrkdir/lpp_name

        if $pp_opt_debug; then
            echo "/lpp_name :"
            cat $pp_wrkdir/lpp_name
        fi >&2

        #-- copy the /lpp_name file to the destdir
        pp_add_transient_file /lpp_name
        cp $pp_wrkdir/lpp_name $pp_destdir/lpp_name

        #-- copy the liblpp.a files under destdir for packaging
	(cd $user_wrkdir && pp_verbose  ar -c -g -r liblpp.a $name.*) ||
		pp_error "ar error"
	if test -s $user_wrkdir/liblpp.a; then
           pp_add_transient_file $instuser/liblpp.a
	   pp_verbose cp $user_wrkdir/liblpp.a $pp_destdir$instuser/liblpp.a ||
		pp_error "cannot create user liblpp.a"
	fi
	(cd $root_wrkdir && pp_verbose  ar -c -g -r liblpp.a $name.*) ||
		pp_error "ar error"
	if test -s $root_wrkdir/liblpp.a; then
           pp_add_transient_file $instroot/liblpp.a
	   pp_verbose cp $root_wrkdir/liblpp.a $pp_destdir$instroot/liblpp.a ||
		pp_error "cannot create root liblpp.a"
	fi

        { echo ./lpp_name
	  test -s $user_wrkdir/liblpp.a && echo .$instuser/liblpp.a
	  test -s $root_wrkdir/liblpp.a && echo .$instroot/liblpp.a
	  cat $user_wrkdir/$name.*.al   # includes the relocated root files!
	} > $pp_wrkdir/bff.list

	if test -n "$pp_aix_abis_seen" -a x"$pp_aix_arch_std" = x"auto"; then
	    case "$pp_aix_abis_seen" in
		"ppc ppc64"|"ppc64 ppc")
		    pp_aix_arch_std=ppc64
		    ;;
		ppc|ppc64)
		    pp_aix_arch_std=$pp_aix_abis_seen
		    ;;
		*" "*)
		    pp_warn "multiple architectures detected: $pp_aix_abis_seen"
		    pp_aix_arch_std=unknown
		    ;;
		"")
		    pp_warn "no binary executables detected; using noarch"
		    pp_aix_arch_std=noarch
		    ;;
		*)
		    pp_warn "unknown architecture detected $pp_aix_abis_seen"
		    pp_aix_arch_std=$pp_aix_abis_seen
		    ;;
	    esac
	fi

	. $pp_wrkdir/%fixup

        outbff=`pp_backend_aix_names`
        pp_debug "creating: $pp_wrkdir/$outbff"
	(cd $pp_destdir && pp_verbose  /usr/sbin/backup -i -q -p -f -) \
          < $pp_wrkdir/bff.list \
	  > $pp_wrkdir/$outbff || pp_error "backup failed"
        $pp_aix_sudo /usr/sbin/installp -l -d $pp_wrkdir/$outbff
}

pp_backend_aix_cleanup () {
    :
}

pp_backend_aix_names () {
    echo "$name.`[ $pp_aix_version ] && pp_aix_version_fix $pp_aix_version || pp_aix_version_fix "$version"`.bff"
}

pp_backend_aix_install_script () {
	typeset pkgname platform
        #
        # The script should take a first argument being the
        # operation; further arguments refer to components or services
        #
        # list-components           -- lists components in the pkg
        # install component...      -- installs the components
        # uninstall component...    -- uninstalles the components
        # list-services             -- lists the services in the pkg
        # start service...          -- starts the name service
        # stop service...           -- stops the named services
        # print-platform            -- prints the platform group
        #
        pkgname="`pp_backend_aix_names`"
	platform="`pp_backend_aix_probe`"   # XXX should be derived from files

        fsets=
        for cmp in $pp_components; do
	    case "$cmp" in
		run) ex=rte;;
		doc) ex=doc;;
		dev) ex=adt;;
		dbg) ex=diag;;
	    esac
            fsets="$fsets $name.$ex"
        done

        echo '#!/bin/sh'
        pp_install_script_common

        cat <<-.

            cpt_to_fileset () {
                test x"\$*" = x"all" &&
                    set -- $pp_components
                for cpt
                do
                    case "\$cpt" in
                        run) echo "$name.rte";;
                        doc) echo "$name.doc";;
                        dev) echo "$name.adt";;
                        dbg) echo "$name.diag";;
                        *) usage;;
                    esac
                done
            }

	    test \$# -eq 0 && usage
            op="\$1"; shift

            case "\$op" in
                list-components)
                    test \$# -eq 0 || usage \$op
                    echo "$pp_components"
                    ;;
                list-services)
                    test \$# -eq 0 || usage \$op
                    echo "$pp_services"
                    ;;
                list-files)
                    test \$# -ge 1 || usage \$op
                    echo \${PP_PKGDESTDIR:-.}/$pkgname
                    ;;
                install)
                    test \$# -ge 1 || usage \$op
                    verbose /usr/sbin/installp -acX -V0 -F \
                        -d \${PP_PKGDESTDIR:-.}/$pkgname \
                        \`cpt_to_fileset "\$@"\`
                    ;;
                uninstall)
                    test \$# -ge 1 || usage \$op
                    verbose /usr/sbin/installp -u -e/dev/null \
			-V0 \`cpt_to_fileset "\$@"\`
                    ;;
                start|stop)
                    test \$# -ge 1 || usage \$op
                    ec=0
                    for svc
                    do
                        verbose \${op}src -s \$svc || ec=1
                    done
                    exit \$ec
                    ;;
                print-platform)
                    echo "$platform"
		    ;;
                *)
                    usage;;
            esac
.
}

pp_backend_aix_init_svc_vars () {
    :
}

pp_backend_aix_probe () {
	echo "${pp_aix_os}-${pp_aix_arch_std}"
}

pp_backend_aix_vas_platforms () {
    case "${pp_aix_arch_std}" in
	ppc*)	:;;
	*)	pp_die "unknown architecture ${pp_aix_arch_std}";;
    esac
    case "${pp_aix_os}" in
	aix43)	echo "aix-43";;
	aix51)	echo "aix-51 aix-43";;
	aix52)	echo "aix-51 aix-43";;
	aix53)	echo "aix-53 aix-51 aix-43";;
	aix61)	echo "aix-53 aix-51 aix-43";;
	*)	pp_die "unknown system ${pp_aix_os}";;
    esac
}
pp_backend_aix_function () {
    case "$1" in
    pp_mkgroup) cat <<'.';;
            /usr/sbin/lsgroup "$1" >/dev/null &&
		return 0
	    echo "Creating group $1"
            /usr/bin/mkgroup -A "$1"
.
    pp_mkuser:depends) echo pp_mkgroup;;
    pp_mkuser) cat <<'.';;
            /usr/sbin/lsuser "$1" >/dev/null &&
	        return 0
	    pp_mkgroup "${2:-$1}" || return 1
	    echo "Creating user $1"
	    /usr/bin/mkuser \
	        login=false \
	        rlogin=false \
		account_locked=true \
		home="${3:-/nohome.$1}" \
		pgrp="${2:-$1}" \
		"$1"
.
    pp_havelib) cat <<'.';;
            case "$2" in
                "")    pp_tmp_name="lib$1.so";;
                *.*.*) pp_tmp_name="lib$1.so.$2";;
                *.*)   pp_tmp_name="lib$1.so.$2.0";;
                *)     pp_tmp_name="lib$1.so.$2";;
            esac
            for pp_tmp_dir in `echo "/usr/lib:/lib${3:+:$3}" | tr : ' '`; do
                test -r "$pp_tmp_dir/$pp_tmp_name" -a \
		    -r "$pp_tmp_dir/lib$1.so" && return 0
            done
            return 1
.
    *) false;;
    esac
}

pp_platforms="$pp_platforms sd"

pp_backend_sd_detect () {
    test x"$1" = x"HP-UX"
}

pp_backend_sd_init () {
    pp_sd_sudo=
    pp_sd_startlevels=2
    pp_sd_stoplevels=auto
    pp_sd_config_file=
    pp_sd_vendor=
    pp_sd_vendor_tag=Quest
    pp_sd_default_start=1           # config_file default start value

    pp_readlink_fn=pp_ls_readlink   # HPUX has no readlink
    pp_shlib_suffix='.sl'           # .so on most other platforms

    pp_sd_detect_os
}

pp_sd_detect_os () {
    typeset revision

    revision=`uname -r`
    pp_sd_os="${revision#?.}"
    test -z "$pp_sd_os" &&
        pp_warn "cannot detect OS version"
    pp_sd_os_std="hpux`echo $pp_sd_os | tr -d .`"

    case "`uname -m`" in
	9000/[678]??) pp_sd_arch_std=hppa;;
	ia64) pp_sd_arch_std=ia64;;
	*) pp_sd_arch_std=unknown;;
    esac
}

pp_sd_write_files () {
    typeset t m o g f p st line dm
    while read t m o g f p st; do
        line="                file"
        case "$f" in *v*) line="$line -v";; esac    # FIXME for uninstall
	case ${pp_sd_os} in
	    10.*)
		case $t in
		    f) dm=644;;
		    d) p=${p%/}; dm=755;;
		esac
		;;
	    *)
		case $t in
		    f) dm=644;;
		    d) line="$line -t d"; p=${p%/}; dm=755;;
		    s) line="$line -t s";;
		esac
		;;
	esac

        test x"$o" = x"-" && o=root
        test x"$g" = x"-" && g=sys
        test x"$m" = x"-" && m=$dm

        case $t in
            s)
		# swpackage will make unqualified links relative to the
		# current working (source) directory, not the destination;
		# we need to qualify them to prevent this.
		case "$st" in
		    /*) echo "$line $st $p";;
		    *) echo "$line `dirname $p`/$st $p";;
		esac
		;;
            *)
		echo "$line -o $o -g $g -m $m $pp_destdir$p $p"
		;;
        esac

    done
}

pp_sd_service_group_script () {
    typeset grp svcs scriptpath out
    grp="$1"
    svcs="$2"
    scriptpath="/sbin/init.d/$grp"
    out="$pp_destdir$scriptpath"

    pp_add_file_if_missing $scriptpath run 755 || return 0

    cat <<-. > $out
	#!/sbin/sh
	# generated by pp $pp_version
	svcs="$svcs"
.

    cat <<-'.' >> $out
	#-- starts services in order.. stops them all if any break
	pp_start () {
	    undo=
	    for svc in \$svcs; do
	        /sbin/init.d/\$svc start
	        case \$? in
	          0|4)
	            undo="\$svc \$undo"
	            ;;
	          *)
	            if test -n "\$undo"; then
	                for svc in \$undo; do
	                    /sbin/init.d/\$svc stop
	                done
	                return 1
	            fi
	            ;;
	        esac
	    done
	    return 0
	}

	#-- stops services in reverse
	pp_stop () {
	    reverse=
	    for svc in \$svcs; do
	        reverse="\$svc \$reverse"
	    done
	    rc=0
	    for svc in \$reverse; do
	        /sbin/init.d/\$svc stop || rc=\$?
	    done
	    return \$rc
        }

	case \$1 in
	    start_msg) echo "Starting \$svcs";;
	    stop_msg)  echo "Stopping \$svcs";;
	    start)     pp_start;;
	    stop)      pp_stop;;
	    *)	       echo "usage: \$0 {start|stop|start_msg|stop_msg}"
	               exit 1;;
	esac
.
}

pp_sd_service_script () {
    typeset svc config_file config_value scriptpath out

    svc="$1"
    scriptpath="/sbin/init.d/$svc"

    config_file=${pp_sd_config_file:-/etc/rc.config.d/$svc}
    sd_config_var=`echo run-$svc | tr '[a-z]-' '[A-Z]_'`
    sd_config_value=${pp_sd_default_start:-0}
    pp_load_service_vars "$svc"

    test -n "$user" -a x"$user" != x"root" &&
        cmd="SHELL=/usr/bin/sh /usr/bin/su $user -c \"exec `echo $cmd | sed -e 's,[$\\\`],\\&,g'`\""
    if test -z "$pidfile"; then
        pidfile="/var/run/$svc.pid"
        cmd="$cmd & echo \$! > \$pidfile"
    fi

    pp_debug "config file is $config_file"

    pp_add_file_if_missing $scriptpath run 755
    pp_add_file_if_missing $config_file run 644 v

    cat <<-. >> $pp_destdir$config_file

	# Controls whether the $svc service is started
	$sd_config_var=$sd_config_value
.

    if test ! -f $pp_destdir$scriptpath; then
    cat <<-. > $pp_destdir$scriptpath
	#!/sbin/sh
	# generated by pp $pp_version

	svc="$svc"
	pidfile="$pidfile"
	config_file="$config_file"

	pp_start () {
	    $cmd
	}

	pp_disabled () {
	    test \${$sd_config_var:-0} -eq 0
	}

	pp_stop () {
	    if test ! -s "\$pidfile"; then
	        echo "Unable to stop \$svc (no pid file)"
	        return 1
	    else
	        read pid < "\$pidfile"
	        if kill -0 "\$pid" 2>/dev/null; then
	            if kill -${stop_signal:-TERM} "\$pid"; then
	                rm -f "\$pidfile"
	                return 0
	            else
	                echo "Unable to stop \$svc"
	                return 1
	            fi
	        else
	            rm -f "\$pidfile"
	            return 0
	        fi
	    fi
	}

	pp_running () {
	    if test ! -s "\$pidfile"; then
	        return 1
	    else
	        read pid < "\$pidfile"
	        kill -0 "\$pid" 2>/dev/null
	    fi
	}

	case \$1 in
	    start_msg) echo "Starting the \$svc service";;
	    stop_msg)  echo "Stopping the \$svc service";;
	    start)
	            if test -f "\$config_file"; then
	                . \$config_file
	            fi
	            if pp_disabled; then
	                exit 2
	            elif pp_running; then
	                echo "\$svc already running";
	                exit 0
	            elif pp_start; then
	                echo "\$svc started";
	                # rc(1M) says we should exit 4, but nobody expects it!
	                exit 0
	            else
	                exit 1
	            fi;;
	    stop)   if pp_stop; then
	                echo "\$svc stopped";
	                exit 0
	            else
	                exit 1
	            fi;;
	    *) echo "usage: \$0 {start|stop|start_msg|stop_msg}"
	       exit 1;;
	esac
.
    fi
}

pp_sd_make_service () {
        typeset level startpriority stoppriority startlevels stoplevels
        typeset svc svcvar symtype

        svc="$1"
	svcvar=`pp_makevar $svc`

	case ${pp_sd_os} in
	    10.*) symtype="file";;
	    *) symtype="file -t s";;
	esac

        # TODO: Figure out why this check is here
        #-- don't do anything if the script exists
        #if test -s "$pp_destdir/sbin/init.d/$svc"; then
        #    pp_error "$pp_destdir/sbin/init.d/$svc exists"
        #    return
        #fi

        # symlink the script, depending on the priorities chosen
        eval startpriority='${pp_sd_startpriority_'$svcvar'}'
        eval stoppriority='${pp_sd_stoppriority_'$svcvar'}'
        test -z "$startpriority" && startpriority="${pp_sd_startpriority:-50}"
        test -z "$stoppriority" && stoppriority="${pp_sd_stoppriority:-50}"

        eval startlevels='${pp_sd_startlevels_'$svcvar'}'
        test -z "$startlevels" && startlevels="$pp_sd_startlevels"

        eval stoplevels='${pp_sd_stoplevels_'$svcvar'}'
        test -z "$stoplevels" && stoplevels="$pp_sd_stoplevels"

        # create the script and config file
        pp_sd_service_script $svc

        # fix the priority up
        case "$startpriority" in
            ???) :;;
            ??) startpriority=0$startpriority;;
            ?) startpriority=00$startpriority;;
        esac
        case "$stoppriority" in
            ???) :;;
            ??) stoppriority=0$stoppriority;;
            ?) stoppriority=00$stoppriority;;
        esac

        if test x"$stoplevels" = x"auto"; then
            stoplevels=
            test -z "$startlevels" || for level in $startlevels; do
                stoplevels="$stoplevels `expr $level - 1`"
            done
        fi

        # create the symlinks
        test -z "$startlevels" || for level in $startlevels; do
            echo "                ${symtype}" \
                    "/sbin/init.d/$svc" \
                    "/sbin/rc$level.d/S$startpriority$svc"
        done
        test -z "$stoplevels" || for level in $stoplevels; do
            echo "                ${symtype}" \
                    "/sbin/init.d/$svc" \
                    "/sbin/rc$level.d/K$stoppriority$svc"
        done
}

pp_sd_control () {
    typeset ctrl script
    typeset cpt

    ctrl="$1"; shift
    cpt="$1"; shift
    script="$pp_wrkdir/control.$ctrl.$cpt"
    cat <<. >$script
.
    cat "$@" >> $script
    echo "exit 0" >> $script
    /usr/bin/chmod +x $script
    echo "                $ctrl $script"
}

pp_sd_depend () {
    typeset _name _vers
    while read _name _vers; do
	case "$_name" in ""| "#"*) continue ;; esac
	echo "                prerequisites $_name ${_vers:+r>= $_vers}"
    done
}

pp_sd_conflict () {
    typeset _name _vers
    while read _name _vers; do
	case "$_name" in ""| "#"*) continue ;; esac
	echo "                exrequisites $_name ${_vers:+r>= $_vers}"
    done
}

pp_backend_sd () {
    typeset psf cpt svc outfile release swp_flags

    psf=$pp_wrkdir/psf
    release="?.${pp_sd_os%.[0-9][0-9]}.*"

    echo "depot" > $psf
    echo "layout_version 1.0" >>$psf

    #-- vendor
    cat <<. >>$psf
        vendor
            tag             $pp_sd_vendor_tag
            title           "${pp_sd_vendor:-$vendor}"
        end

        product
            tag             $name
            revision        $version
            vendor_tag      $pp_sd_vendor_tag
            is_patch        false
            title           "$summary"
            copyright       "$copyright"
            machine_type    *
            os_name         HP-UX
            os_release      $release
            os_version      ?
            directory       /
            is_locatable    false
.
    test -n "$description" \
        && echo $description > $pp_wrkdir/description \
        && cat <<. >> $psf
            description     < $pp_wrkdir/description
.

    # make convenience service groups
    if test -n "$pp_service_groups"; then
	for grp in $pp_service_groups; do
	    pp_sd_service_group_script \
		$grp "`pp_service_get_svc_group $grp`"
	done
    fi

    for cpt in $pp_components; do
        cat <<. >>$psf
            fileset
                tag             ${pp_sd_fileset_tag:-$cpt}
                title           "${summary:-cpt}"
                revision        $version
.
        test -s $pp_wrkdir/%depend.$cpt &&
              pp_sd_depend < $pp_wrkdir/%depend.$cpt >> $psf
        test -s $pp_wrkdir/%conflict.$cpt &&
              pp_sd_conflict < $pp_wrkdir/%conflict.$cpt >> $psf

	#-- make sure services are shut down during uninstall
        if test $cpt = run -a -n "$pp_services"; then
            for svc in $pp_services; do
                pp_prepend $pp_wrkdir/%preun.$cpt <<-.
			/sbin/init.d/$svc stop
.
            done
        fi

        #-- we put the post/preun code into configure/unconfigure
        # and not postinstall/preremove, because configure/unconfigure
        # scripts are run on the hosts where the package is installed,
        # not loaded (a subtle difference).
        test -s $pp_wrkdir/%pre.$cpt &&
            pp_sd_control checkinstall $cpt $pp_wrkdir/%pre.$cpt >> $psf
        test -s $pp_wrkdir/%post.$cpt &&
            pp_sd_control configure $cpt $pp_wrkdir/%post.$cpt >> $psf
        test -s $pp_wrkdir/%preun.$cpt &&
            pp_sd_control unconfigure $cpt $pp_wrkdir/%preun.$cpt >> $psf
        test -s $pp_wrkdir/%postun.$cpt &&
            pp_sd_control postremove $cpt $pp_wrkdir/%postun.$cpt >> $psf
        test -s $pp_wrkdir/%check.$cpt &&
            pp_sd_control checkinstall $cpt $pp_wrkdir/%check.$cpt >> $psf

        if test $cpt = run -a -n "$pp_services"; then
            for svc in $pp_services; do
                #-- service names are 10 chars max on hpux
                case "$svc" in ???????????*)
                    pp_warn "service name '$svc' is too long for hpux";;
                esac
                pp_sd_make_service $svc >> $psf
            done
            #pp_sd_make_service_config
        fi

        pp_sd_write_files < $pp_wrkdir/%files.$cpt >> $psf

        #-- end fileset clause
        cat <<. >>$psf
            end
.

    done

    #-- end product clause
    cat <<. >>$psf
        end
.

    $pp_opt_debug && cat $psf >&2

    test -s $pp_wrkdir/%fixup && . $pp_wrkdir/%fixup

    outfile=`pp_backend_sd_names`
    case ${pp_sd_os} in
	10.*)
	    swp_flags="-x target_type=tape"
	    ;;
	*)
	    swp_flags="-x media_type=tape"
	    ;;
    esac
    if pp_verbose ${pp_sd_sudo} /usr/sbin/swpackage -s $psf $swp_flags \
        @ $pp_wrkdir/$outfile
    then
        pp_verbose ${pp_sd_sudo} /usr/sbin/swlist -l file -s $pp_wrkdir/$outfile
    else
        pp_error "swpackage failed"
    fi
}

pp_backend_sd_cleanup () {
    :
}

pp_backend_sd_names () {
    echo "$name-$version.$pp_sd_arch_std.depot"
}

pp_backend_sd_install_script () {
    typeset pkgname platform

    pkgname=`pp_backend_sd_names`
    platform="`pp_backend_sd_probe`"

    echo "#!/bin/sh"
    pp_install_script_common
    cat <<.

        cpt_to_tags () {
            test x"\$*" = x"all" && set -- $pp_components
            for cpt
            do
                echo "$name.\$cpt"
            done
        }

        test \$# -eq 0 && usage
        op="\$1"; shift

        case "\$op" in
            list-components)
                test \$# -eq 0 || usage \$op
                echo "$pp_components"
                ;;
            list-services)
                test \$# -eq 0 || usage \$op
                echo "$pp_services"
                ;;
            list-files)
                test \$# -ge 1 || usage \$op
                echo \${PP_PKGDESTDIR:-.}/$pkgname
                ;;
            install)
                test \$# -ge 1 || usage \$op
                verbose /usr/sbin/swinstall -x verbose=0 \
                    -s \${PP_PKGDESTDIR:-\`pwd\`}/$pkgname \
                    \`cpt_to_tags "\$@"\`
                ;;
            uninstall)
                test \$# -ge 1 || usage \$op
                verbose /usr/sbin/swremove -x verbose=0 \
                    \`cpt_to_tags "\$@"\`
                ;;
            start|stop)
                test \$# -ge 1 || usage \$op
                ec=0
                for svc
                do
                    verbose /sbin/init.d/\$svc \$op
                    [ \$? -eq 4 -o \$? -eq 0 ] || ec=1
                done
                exit \$ec
                ;;
            print-platform)
		echo "$platform"
		;;
            *)
                usage
                ;;
        esac
.
}

pp_backend_sd_probe () {
    echo "${pp_sd_os_std}-${pp_sd_arch_std}"
}

pp_backend_sd_vas_platforms () {
    case "`pp_backend_sd_probe`" in
	hpux*-hppa) echo hpux-pa;;
	hpux*-ia64) echo hpux-ia64 hpux-pa;;
	*)	    pp_die "unknown system `pp_backend_sd_probe`";;
    esac
}

pp_backend_sd_init_svc_vars () {
    :
}
pp_backend_sd_function () {
    case "$1" in
        pp_mkgroup) cat <<'.';;
	    /usr/sbin/groupmod "$1" 2>/dev/null ||
		/usr/sbin/groupadd "$1"
.
        pp_mkuser:depends) echo pp_mkgroup;;
        pp_mkuser) cat <<'.';;
	    pp_mkgroup "${2:-$1}" || return 1
	    /usr/sbin/useradd \
		-g "${2:-$1}" \
		-d "${3:-/nonexistent}" \
		-s "${4:-/bin/false}" \
		"$1"
.
        pp_havelib) cat <<'.';;
            for pp_tmp_dir in `echo /usr/lib${3:+:$3} | tr : ' '`; do
                test -r "$pp_tmp_dir/lib$1${2:+.$2}.sl" && return 0
            done
            return 1
.
        *) false;;
    esac
}

pp_platforms="$pp_platforms solaris"

pp_backend_solaris_detect () {
	test x"$1" = x"SunOS"
}

pp_backend_solaris_init () {
	pp_solaris_category=
	pp_solaris_istates="s S 1 2 3"	# run-states when install is ok
	pp_solaris_rstates="s S 1 2 3"	# run-states when remove is ok
	pp_solaris_maxinst=
	pp_solaris_vendor=
	pp_solaris_pstamp=
	pp_solaris_copyright=
	pp_solaris_name=
	pp_solaris_desc=
	pp_solaris_package_arch=auto

        pp_solaris_detect_os
        pp_solaris_detect_arch

        pp_solaris_init_svc

        #-- readlink not reliably available on Solaris
	pp_readlink_fn=pp_ls_readlink
}

pp_solaris_detect_os () {
        typeset osrel

        osrel=`/usr/bin/uname -r`
        case "$osrel" in
	    5.[0-6])	pp_solaris_os="sol2${osrel#5.}";;
	    5.*)        pp_solaris_os="sol${osrel#5.}";;
        esac
        test -z "$pp_solaris_os" &&
             pp_warn "can't determine OS suffix from uname -r"

}

pp_solaris_detect_arch () {
	pp_solaris_arch=`/usr/bin/optisa amd64 sparcv9 i386 sparc`
	[ -z "$pp_solaris_arch" ] &&
	    pp_error "can't determine processor architecture"
	case "$pp_solaris_arch" in
	    amd64)   pp_solaris_arch_std=x86_64;;
	    i386)    pp_solaris_arch_std=i386;;
	    sparcv9) pp_solaris_arch_std=sparc64;;
	    sparc)   pp_solaris_arch_std=sparc;;
	    *)       pp_solaris_arch_std=unknown;;
	esac
}

pp_solaris_is_request_script_necessary () {
    typeset has_optional_services

    has_optional_services=no
    for _svc in $pp_services; do
    	pp_load_service_vars $_svc
	if test "$optional" = "yes"; then
	    has_optional_services=yes
	fi
    done

    # If the package has no optional services and only one component, don't
    # create a request script at all.
    if test "$has_optional_services" = "no" &&
       test `echo $pp_components | wc -w` -eq 1; then
	return 1 # no
    fi

    return 0 # yes
}

pp_solaris_request () {
    typeset _cmp _svc

    #-- The common part of the request script contains the ask() function
    #   and resets the CLASSES list to empty
    cat <<'.'
	trap 'exit 3' 15
	ask () {
	   ans=`ckyorn -d "$1" \
                -p "Do you want to $2"` \
            || exit $?
	   case "$ans" in y*|Y*) return 0;; *) return 1;; esac
	}
	CLASSES=
.
    #-- each of our components adds itself to the CLASSES list
    for _cmp in $pp_components; do
      case "$_cmp" in
            run) :;;
            doc) echo 'ask y "install the documentation files" &&';;
            dev) echo 'ask y "install the development files" &&';;
            dbg) echo 'ask n "install the diagnostic files" &&';;
      esac
      echo '    CLASSES="$CLASSES '$_cmp'"'
    done

    #-- the request script writes the CLASSES var to its output
    cat <<'.'
	echo "CLASSES=$CLASSES" > $1
.

    if test -n "$pp_services"; then
        echo 'SERVICES='
        for _svc in $pp_services; do
	    pp_load_service_vars $_svc
	    if test "$enable" = "yes"; then
		_default_prompt=y
	    else
		_default_prompt=n
	    fi
	    if test "$optional" = "yes"; then
		echo 'ask '$_default_prompt' "install '$_svc' service" &&'
	    fi
            echo '    SERVICES="$SERVICES '$_svc'"'
        done
        echo 'echo "SERVICES=$SERVICES" >> $1'
    fi

}

pp_solaris_procedure () {
    cat <<.

        #-- $2 for $1 component of $name
        case " \$CLASSES " in *" $1 "*)
.
    cat
    cat <<.
        ;; esac
.
}

pp_solaris_depend () {
    typeset _name _vers
    while read _name _vers; do
	if test -n "$_name"; then
	    echo "P $_name $_name"
	    test -n "$_vers" && echo " $_vers"
	fi
    done
}

pp_solaris_conflict () {
    typeset _name _vers
    while read _name _vers; do
	if test -n "$_name"; then
	    echo "I $_name $_name"
	    test -n "$_vers" && echo " $_vers"
	fi
    done
}

pp_solaris_space() {
    echo "$2:$3:$1" >> $pp_wrkdir/space.cumulative
}

pp_solaris_sum_space () {
    if test -s $pp_wrkdir/space.cumulative; then
        sort -t: +2 < $pp_wrkdir/space.cumulative |
        awk -F: 'NR==1{n=$3}{if($3==n){b+=$1;i+=$2}else{print n" "b" "i;b=$1;i=$2;n=$3}}END{print n" "b" "i}' > $pp_wrkdir/space
    fi
}

pp_solaris_proto () {
	typeset t m o g f p st
	typeset abi

	while read t m o g f p st; do
	  # Use Solaris default mode, owner and group if all unspecified
	  if test x"$m$o$g" = x"---"; then
	    m="?"; o="?"; g="?"
	  fi
	  test x"$o" = x"-" && o="root"
	  case "$t" in
	    f) test x"$g" = x"-" && g="bin"
	       test x"$m" = x"-" && m=444
	       case "$f" in
		*v*) echo "v $1 $p=$pp_destdir$p $m $o $g";;
		*)   echo "f $1 $p=$pp_destdir$p $m $o $g";;
	       esac
	       if test -r "$pp_destdir$p"; then
		  #-- Use file to record ABI types seen
		  case "`file "$pp_destdir$p"`" in
		    *"ELF 32"*80386*) abi=i386;;
		    *"ELF 64"*AMD*) abi=x86_64;;
		    *"ELF 32"*SPARC*) abi=sparc;;
		    *"ELF 64"*SPARC*) abi=sparc64;;
		    *) abi=;;
		  esac
		  if test -n "$abi"; then
		    pp_add_to_list pp_solaris_abis_seen $abi
		  fi
	       fi
               ;;
	    d) test x"$g" = x"-" && g="sys"
	       test x"$m" = x"-" && m=555
	       echo "d $1 $p $m $o $g"
               ;;
	    s) test x"$g" = x"-" && g="bin"
	       test x"$m" = x"-" && m=777
               if test x"$m" != x"777" -a x"$m" != x"?"; then
                  pp_warn "$p: invalid mode $m for symlink, should be 777 or -"
	       fi
	       echo "s $1 $p=$st $m $o $g"
               ;;
	  esac
	done
}

pp_backend_solaris () {
        typeset _cmp _svc _grp

	prototype=$pp_wrkdir/prototype
	: > $prototype

	pkginfo=$pp_wrkdir/pkginfo
	: > $pkginfo
	echo "i pkginfo=$pkginfo" >> $prototype

        case "${pp_solaris_name:-$name}" in
            [0-9]*)
                pp_error "Package name '${pp_solaris_name:-$name}'" \
                        "cannot start with a number"
                ;;
            ???????????????*)
                pp_warn "Package name '${pp_solaris_name:-$name}'" \
                        "too long for Solaris 2.6 or 2.7 (max 9 characters)"
                ;;
            ??????????*)
                pp_warn "Package name '${pp_solaris_name:-$name}'" \
                        "too long for 2.7 Solaris (max 9 characters)"
                ;;
        esac

        #-- generate the package info file
	echo "VERSION=$version" >> $pkginfo
	echo "PKG=${pp_solaris_name:-$name}" >> $pkginfo
	echo "CLASSES=$pp_components" >> $pkginfo
	echo "BASEDIR=/" >> $pkginfo
	echo "NAME=$name $version" >> $pkginfo
	echo "CATEGORY=${pp_solaris_category:-application}" >> $pkginfo

	desc="${pp_solaris_desc:-$description}"
	test -n "$desc" &&
	  echo "DESC=$desc" >> $pkginfo

	test -n "$pp_solaris_rstates" &&
	  echo "RSTATES=$pp_solaris_rstates" >> $pkginfo
	test -n "$pp_solaris_istates" &&
	  echo "ISTATES=$pp_solaris_istates" >> $pkginfo
	test -n "$pp_solaris_maxinst" &&
	  echo "MAXINST=$pp_solaris_maxinst" >> $pkginfo
	test -n "${pp_solaris_vendor:-$vendor}" &&
	  echo "VENDOR=${pp_solaris_vendor:-$vendor}" >> $pkginfo
	test -n "$pp_solaris_pstamp" &&
	  echo "PSTAMP=$pp_solaris_pstamp" >> $pkginfo

	if test -n "${pp_solaris_copyright:-$copyright}"; then
	    echo "${pp_solaris_copyright:-$copyright}" > $pp_wrkdir/copyright
	    echo "i copyright=$pp_wrkdir/copyright" >> $prototype
	fi

        #-- scripts to run before and after install
        : > $pp_wrkdir/postinstall
        : > $pp_wrkdir/preremove
        : > $pp_wrkdir/postremove
	for _cmp in $pp_components; do
        #-- add the preinstall scripts in definition order
        if test -s $pp_wrkdir/%pre.$_cmp; then
            pp_solaris_procedure $_cmp preinst < $pp_wrkdir/%pre.$_cmp \
                >> $pp_wrkdir/preinstall
        fi
        #-- add the postinstall scripts in definition order
        if test -s $pp_wrkdir/%post.$_cmp; then
            pp_solaris_procedure $_cmp postinst < $pp_wrkdir/%post.$_cmp \
                >> $pp_wrkdir/postinstall
        fi
        #-- add the preremove rules in reverse definition order
        if test -s $pp_wrkdir/%preun.$_cmp; then
            pp_solaris_procedure $_cmp preremove < $pp_wrkdir/%preun.$_cmp |
                    pp_prepend $pp_wrkdir/preremove
        fi
        #-- add the postremove scripts in definition order
        if test -s $pp_wrkdir/%postun.$_cmp; then
            pp_solaris_procedure $_cmp postremove < $pp_wrkdir/%postun.$_cmp \
                >> $pp_wrkdir/postremove
        fi
        #-- Add the check script in definition order
        if test -s $pp_wrkdir/%check.$_cmp; then
            pp_solaris_procedure $_cmp checkinstall \
                        < $pp_wrkdir/%check.$_cmp \
			>> $pp_wrkdir/checkinstall
        fi
        #-- All dependencies and conflicts are merged together for Solaris pkgs
        test -s $pp_wrkdir/%depend.$_cmp &&
              pp_solaris_depend < $pp_wrkdir/%depend.$_cmp >> $pp_wrkdir/depend
        test -s $pp_wrkdir/%conflict.$_cmp &&
              pp_solaris_conflict < $pp_wrkdir/%conflict.$_cmp >> $pp_wrkdir/depend
	done


	if pp_solaris_is_request_script_necessary; then
	    pp_solaris_request > $pp_wrkdir/request
	fi

        test -n "$pp_services" &&
            for _svc in $pp_services; do
                pp_load_service_vars $_svc
                pp_solaris_smf $_svc
                pp_solaris_make_service $_svc
                pp_solaris_install_service $_svc | pp_prepend $pp_wrkdir/postinstall
                pp_solaris_remove_service $_svc | pp_prepend $pp_wrkdir/preremove
                pp_solaris_remove_service $_svc | pp_prepend $pp_wrkdir/postremove
                unset pp_svc_xml_file
            done

        test -n "$pp_service_groups" &&
	    for _grp in $pp_service_groups; do
		pp_solaris_make_service_group \
		    $_grp "`pp_service_get_svc_group $_grp`"
	    done

        #-- if installf was used; we need to indicate a termination
        grep installf $pp_wrkdir/postinstall >/dev/null &&
            echo 'installf -f $PKGINST' >> $pp_wrkdir/postinstall

        pp_solaris_sum_space

        # NB: pkginfo and copyright are added earlier
        for f in compver depend space checkinstall \
                 preinstall request postinstall \
                 preremove postremove; do
            if test -s $pp_wrkdir/$f; then
		case $f in
		    *install|*remove|request)
			# turn scripts into a proper shell scripts
			mv $pp_wrkdir/$f $pp_wrkdir/$f.tmp
			{ echo "#!/bin/sh";
			  echo "# $f script for ${pp_solaris_name:-$name}-$version"
			  cat $pp_wrkdir/$f.tmp
			  echo "exit 0"; } > $pp_wrkdir/$f
			chmod +x $pp_wrkdir/$f
			rm -f $pp_wrkdir/$f.tmp
			;;
		esac
                if $pp_opt_debug; then
                    pp_debug "contents of $f:"
                    cat $pp_wrkdir/$f >&2
                fi
                echo "i $f=$pp_wrkdir/$f" >> $prototype
            fi
        done

        #-- create the prototype file which lists the files to install
        # do this as late as possible because files could be added
	pp_solaris_abis_seen=
	for _cmp in $pp_components; do
	  pp_solaris_proto $_cmp < $pp_wrkdir/%files.$_cmp
	done >> $prototype

	if test x"$pp_solaris_package_arch" = x"auto"; then
	    if pp_contains "$pp_solaris_abis_seen" sparc64; then
		pp_solaris_package_arch_std="sparc64"
		echo "ARCH=sparcv9" >> $pkginfo
	    elif pp_contains "$pp_solaris_abis_seen" sparc; then
		pp_solaris_package_arch_std="sparc"
		echo "ARCH=sparc" >> $pkginfo
	    elif pp_contains "$pp_solaris_abis_seen" x86_64; then
		pp_solaris_package_arch_std="x86_64"
		echo "ARCH=amd64" >> $pkginfo
	    elif pp_contains "$pp_solaris_abis_seen" i386; then
		pp_solaris_package_arch_std="i386"
		echo "ARCH=i386" >> $pkginfo
	    else
		pp_warn "No ELF files found: not supplying an ARCH type"
		pp_solaris_package_arch_std="noarch"
	    fi
	else
	    pp_solaris_package_arch_std="$pp_solaris_package_arch"
	    echo "ARCH=$pp_solaris_package_arch" >> $pkginfo
	fi

	mkdir $pp_wrkdir/pkg

	. $pp_wrkdir/%fixup

if $pp_opt_debug; then
  echo "$pkginfo::"; cat $pkginfo
  echo "$prototype::"; cat $prototype
fi >&2

	pkgmk -d $pp_wrkdir/pkg -f $prototype \
		|| { error "pkgmk failed"; return; }
        pkgtrans -s $pp_wrkdir/pkg \
		$pp_wrkdir/`pp_backend_solaris_names` \
                ${pp_solaris_name:-$name} \
		|| { error "pkgtrans failed"; return; }
}

pp_backend_solaris_cleanup () {
	:
}

pp_backend_solaris_names () {
	echo ${pp_solaris_name:-$name}-$version-${pp_solaris_package_arch_std:-$pp_solaris_arch}.pkg
}

pp_backend_solaris_install_script () {
        typeset pkgname platform

	platform="${pp_solaris_os:-solaris}-${pp_solaris_package_arch_std:-$pp_solaris_arch}"

        echo "#! /sbin/sh"
        pp_install_script_common
        pkgname=`pp_backend_solaris_names`

        cat <<.
            tmpnocheck=/tmp/nocheck\$\$
            tmpresponse=/tmp/response\$\$
            trap 'rm -f \$tmpnocheck \$tmpresponse' 0

            make_tmpfiles () {
                cat <<-.. > \$tmpresponse
                        CLASSES=\$*
                        SERVICES=$pp_services
..
                cat <<-.. > \$tmpnocheck
			mail=
			instance=overwrite
			partial=nocheck
			runlevel=nocheck
			idepend=nocheck
			rdepend=nocheck
			space=nocheck
			setuid=nocheck
			conflict=nocheck
			action=nocheck
			basedir=default
..
            }

            test \$# -eq 0 && usage
            op="\$1"; shift

            case "\$op" in
                list-components)
                    test \$# -eq 0 || usage \$op
                    echo "$pp_components"
                    ;;
                list-services)
                    test \$# -eq 0 || usage \$op
                    echo "$pp_services"
                    ;;
                list-files)
                    test \$# -ge 1 || usage \$op
                    echo \${PP_PKGDESTDIR:-.}/$pkgname
                    ;;
                install)
                    test \$# -ge 1 || usage \$op
                    make_tmpfiles "\$@"
                    verbose /usr/sbin/pkgadd -n -d \${PP_PKGDESTDIR:-.}/$pkgname \
                        -r \$tmpresponse \
                        -a \$tmpnocheck \
                        ${pp_solaris_name:-$name}
                    ;;
                uninstall)
                    test \$# -ge 1 || usage \$op
                    make_tmpfiles "\$@"
                    verbose /usr/sbin/pkgrm -n \
                        -a \$tmpnocheck \
                        ${pp_solaris_name:-$name}
                    ;;
                start|stop)
                    test \$# -ge 1 || usage \$op
                    ec=0
                    for svc
                    do
                        verbose /etc/init.d/\$svc \$op || ec=1
                    done
                    exit \$ec
                    ;;
                print-platform)
		    echo "$platform"
		    ;;
                *)
                    usage
                    ;;
            esac
.
}

pp_solaris_dynlib_depend () {
	xargs ldd 2>/dev/null |
	sed -e '/^[^ 	]*:$/d' -e 's,.*=>[	 ]*,,' -e 's,^[ 	]*,,' |
	sort -u |
	grep -v '^/usr/platform/' | (
	  set -- ""; shift
	  while read p; do
	    set -- "$@" -p "$p"
	    if [ $# -gt 32 ]; then
		echo "$# is $#" >&2
		pkgchk -l "$@"
		set -- ""; shift
	    fi
	  done
	  [ $# -gt 0 ] && pkgchk -l "$@"
	)|
	awk '/^Current status:/{p=0} p==1 {print $1} /^Referenced by/ {p=1}' |
	sort -u |
	xargs -l32 pkginfo -x |
	awk 'NR % 2 == 1 { name=$1; } NR%2 == 0 { print name, $2 }'
}

pp_solaris_add_dynlib_depends () {
    typeset tmp
    tmp=$pp_wrkdir/tmp.dynlib

    for _cmp in $pp_components; do
	awk '{print destdir $6}' destdir="$pp_destdir" \
		< $pp_wrkdir/%files.$_cmp |
	pp_solaris_dynlib_depend > $tmp
	if test -s $tmp; then
	    cat $tmp >> $pp_wrkdir/%depend.$_cmp
	fi
	rm -f $tmp
    done
}

pp_backend_solaris_probe () {
    echo "${pp_solaris_os}-${pp_solaris_arch_std}"
}

pp_backend_solaris_vas_platforms () {
    case `pp_backend_solaris_probe` in
	sol10-sparc* | sol9-sparc* | sol8-sparc*)
			echo solaris8-sparc solaris7-sparc solaris26-sparc;;
	sol7-sparc*)	echo                solaris7-sparc solaris26-sparc;;
	sol26-sparc*)	echo                               solaris26-sparc;;
	sol8-*86)	echo solaris8-x86;;
	sol10-*86 | sol10-x86_64)
			echo solaris10-x64 solaris8-x86;;
	*)		pp_die "unknown system `pp_backend_solaris_probe`";;
    esac
}
pp_backend_solaris_function() {
    case "$1" in
        pp_mkgroup) cat<<'.';;
	    /usr/sbin/groupmod "$1" 2>/dev/null && return 0
            /usr/sbin/groupadd "$1"
.
        pp_mkuser:depends) echo pp_mkgroup;;
        pp_mkuser) cat<<'.';;
	    id "$1" >/dev/null 2>/dev/null && return 0
	    pp_mkgroup "${2:-$1}" || return 1
	    /usr/sbin/useradd \
		-g "${2:-$1}" \
		-d "${3:-/nonexistent}" \
		-s "${4:-/bin/false}" \
		"$1"
.
    *) false;;
    esac
}

pp_backend_solaris_init_svc_vars () {
    _smf_category=${pp_solaris_smf_category:-application}
    _smf_method_envvar_name=${smf_method_envvar_name:-"PP_SMF_SERVICE"}
    pp_solaris_service_shell=/sbin/sh
}

pp_solaris_init_svc () {
    smf_version=1
    smf_type=service
    solaris_user=
    solaris_stop_signal=
    solaris_sysv_init_start=S70     # invocation order for start scripts
    solaris_sysv_init_kill=K30      # invocation order for kill scripts
    solaris_sysv_init_start_states="2" # states to install start link
    solaris_sysv_init_kill_states="S 0 1"  # states to install kill link

    #
    # To have the service be installed to start automatically,
    #   %service foo
    #   solaris_sysv_init_start_states="S 0 1 2"
    #
}

pp_solaris_smf () {
    typeset f _pp_solaris_service_script svc _pp_solaris_manpage

    pp_solaris_name=${pp_solaris_name:-$name}
    pp_solaris_manpath=${pp_solaris_manpath:-"/usr/share/man"}
    pp_solaris_mansect=${pp_solaris_mansect:-1}
    smf_start_timeout=${smf_start_timeout:-60}
    smf_stop_timeout=${smf_stop_timeout:-60}
    smf_restart_timeout=${smf_restart_timeout:-60}

    svc=${pp_solaris_smf_service_name:-$1}
    _pp_solaris_service_script=${pp_solaris_service_script:-"/etc/init.d/${pp_solaris_service_script_name:-$svc}"}
    _pp_solaris_manpage=${pp_solaris_manpage:-$svc}

    if [ -z $pp_svc_xml_file ]; then
        pp_svc_xml_file="/var/svc/manifest/$_smf_category/$svc.xml"
        echo "## Generating the smf service manifest file for $pp_svc_xml_file"
    else
        echo "## SMF service manifest file already defined at $pp_svc_xml_file"
        if [ -z $pp_solaris_smf_service_name ] || [ -z $pp_solaris_smf_category ] || [ -z $pp_solaris_service_script ] || [ -z $smf_method_envvar_name ]; then
          pp_error "All required variables are not set.\n"\
                   "When using a custom manifest file all of the following variables must be set:\n"\
                   "pp_solaris_smf_service_name, pp_solaris_smf_category, pp_solaris_service_script and smf_method_envvar_name.\n\n"\
                   "Example:\n"\
                   " \$pp_solaris_smf_category=application\n"\
                   " \$pp_solaris_smf_service_name=pp\n\n"\
                   "  <service name='application/pp' type='service' version='1'>\n\n"\
                   "Example:\n"\
                   " \$pp_solaris_service_script=/etc/init.d/pp\n\n"\
                   "  <exec_method type='method' name='start' exec='/etc/init.d/pp' />\n\n"\
                   "Example:\n"\
                   " \$smf_method_envvar_name=PP_SMF_SERVICE\n\n"\
                   "  <method_environment>\n"\
                   "    <envvar name='PP_SMF_SERVICE' value='1'/>\n"\
                   "  </method_environment>\n"

          return 1
        fi
        return 0
    fi

    f=$pp_svc_xml_file
    pp_add_file_if_missing $f ||
        return 0
    pp_solaris_add_parent_dirs "$f"

    _pp_solaris_smf_dependencies="
          <dependency name='pp_local_filesystems'
                grouping='require_all'
                restart_on='none'
                type='service'>
                <service_fmri value='svc:/system/filesystem/local'/>
          </dependency>

          <dependency name='pp_single-user'
                grouping='require_all'
                restart_on='none'
                type='service'>
                <service_fmri value='svc:/milestone/single-user' />
          </dependency>
"
    _pp_solaris_smf_dependencies=${pp_solaris_smf_dependencies:-$_pp_solaris_smf_dependencies}

    cat <<-. >$pp_destdir$f
<?xml version="1.0"?>
<!DOCTYPE service_bundle SYSTEM '/usr/share/lib/xml/dtd/service_bundle.dtd.1'>
<!--
	$copyright
        Generated by PolyPackage $pp_version
-->

    <service_bundle type='manifest' name='${pp_solaris_name}:${svc}' >
          <service name='$_smf_category/$svc'
                type='$smf_type'
                version='$smf_version'>

          <create_default_instance enabled='false'/>

          <single_instance />

          $_pp_solaris_smf_dependencies

          $pp_solaris_smf_additional_dependencies

          <method_context>
                <method_credential user='${solaris_user:-$user}' />
                <method_environment>
                    <envvar name='$_smf_method_envvar_name' value='1'/>
                </method_environment>
          </method_context>

          <exec_method type='method' name='start'
                exec='$_pp_solaris_service_script start'
                timeout_seconds='$smf_start_timeout' />

          <exec_method type='method' name='stop'
                exec='$_pp_solaris_service_script stop'
                timeout_seconds='$smf_stop_timeout' />

          <exec_method type='method' name='restart'
                exec='$_pp_solaris_service_script restart'
                timeout_seconds='$smf_restart_timeout' />

          <template>
              <common_name>
                  <loctext xml:lang='C'>$description</loctext>
              </common_name>
              <documentation>
                  <manpage title='$pp_solaris_manpage' section='$pp_solaris_mansect' manpath='$pp_solaris_manpath'/>
              </documentation>
          </template>
        </service>
    </service_bundle>
.
}

pp_solaris_make_service_group () {
    typeset group out file svcs svc

    group="$1"
    svcs="$2"
    file="/etc/init.d/$group"
    out="$pp_destdir$file"

    #-- return if the script is supplied already
    pp_add_file_if_missing "$file" run 755 || return 0
    pp_solaris_add_parent_dirs "$file"

    echo "#! /sbin/sh" > $out
    echo "# polypkg service group script for these services:" >> $out
    echo "svcs=\"$svcs\"" >> $out

    cat <<'.' >>$out

	#-- starts services in order.. stops them all if any break
	pp_start () {
	    undo=
	    for svc in $svcs; do
		if /etc/init.d/$svc start; then
		    undo="$svc $undo"
		else
		    if test -n "$undo"; then
		        for svc in $undo; do
			   /etc/init.d/$svc stop
			done
			return 1
		    fi
		fi
	    done
	    return 0
	}

	#-- stops services in reverse
	pp_stop () {
	    reverse=
	    for svc in $svcs; do
		reverse="$svc $reverse"
	    done
	    rc=0
	    for svc in $reverse; do
		/etc/init.d/$svc stop || rc=$?
	    done
	    return $rc
	}

	#-- returns true only if all services return true status
	pp_status () {
	    rc=0
	    for svc in $svcs; do
		/etc/init.d/$svc status || rc=$?
	    done
	    return $rc
	}

        case "$1" in
            start)   pp_start;;
            stop)    pp_stop;;
            status)  pp_status;;
            restart) pp_stop && pp_start;;
            *)       echo "usage: $0 {start|stop|restart|status}" >&2; exit 1;;
        esac
.
}

pp_solaris_make_service () {
    typeset file out _cmd svc

    svc="${pp_solaris_smf_service_name:-$1}"
    file=${pp_solaris_service_script:-"/etc/init.d/${pp_solaris_service_script_name:-$svc}"}
    out="$pp_destdir$file"

    #-- return if we don't need to create the init script
    pp_add_file_if_missing "$file" run 755 ||
        return 0
    pp_solaris_add_parent_dirs "$file"

    echo "#! /sbin/sh" >$out
    echo "#-- This service init file generated by polypkg" >>$out

    #-- Start SMF integration.
    if [ -n "$pp_svc_xml_file" ] ; then
        cat <<_EOF >>$out
if [ -x /usr/sbin/svcadm ] && [ "x\$1" != "xstatus" ] && [ "t\$$_smf_method_envvar_name" = "t" ] ; then
    case "\$1" in
        start)
            echo "starting $svc"
            /usr/sbin/svcadm clear svc:/$_smf_category/$svc:default >/dev/null 2>&1
            /usr/sbin/svcadm enable -s $_smf_category/$svc
            RESULT=\$?
            if [ "\$RESULT" -ne 0 ] ; then
                echo "Error \$RESULT starting $svc" >&2
            fi
            ;;
        stop)
            echo "stopping $svc"
            /usr/sbin/svcadm disable -ts $_smf_category/$svc
	    RESULT=0
            ;;
        restart)
            echo "restarting $svc"
            /usr/sbin/svcadm disable -ts $_smf_category/$svc
            /usr/sbin/svcadm clear svc:/$_smf_category/$svc:default >/dev/null 2>&1
            /usr/sbin/svcadm enable -s $_smf_category/$svc
            RESULT=\$?
            if [ "\$RESULT" -ne 0 ] ; then
                echo "Error \$RESULT starting $svc" >&2
            fi
            ;;
        *)
            echo "Usage: $file {start|stop|restart|status}" >&2
            RESULT=1
    esac
    exit $RESULT
fi
_EOF
    fi

    #-- construct a start command that builds a pid file as needed
    #   and forks the daemon
    _cmd="$cmd";
    if test -z "$pidfile"; then
	# The service does not define a pidfile, so we have to make
	# our own up. On Solaris systems where there is no /var/run
	# we must use /tmp to guarantee the pid files are removed after
	# a system crash.
	cat <<. >>$out
	    pp_piddir="/var/run"
	    test -d "\$pp_piddir/." || pp_piddir="/tmp"
	    pidfile="\$pp_piddir/$svc.pid"
.
        _cmd="$cmd & echo \$! > \$pidfile"
    else
	# The service is able to write its own PID file
	cat <<. >>$out
	    pidfile="$pidfile"
.
    fi

    if test "${user:-root}" != "root"; then
        _cmd="su $user -c exec $_cmd";
    fi

    cat <<. >>$out
	stop_signal="${stop_signal:-TERM}"
	svc="${svc}"

        # generated command to run $svc as a daemon process
        pp_exec () { $_cmd; }
.

    #-- write the invariant section of the init script
    cat <<'.' >>$out

        # returns true if $svc is running
        pp_running () {
            test -s "$pidfile" || return 1
            read pid junk < "$pidfile" 2>/dev/null
            test ${pid:-0} -gt 1 &&
            kill -0 "$pid" 2>/dev/null
        }

        # prints a message describing $svc's running state
        pp_status () {
            if pp_running; then
                echo "service $svc is running (pid $pid)"
                return 0
            elif test -f "$pidfile"; then
                echo "service $svc is not running, but pid file exists"
                return 2
            else
                echo "service $svc is not running"
                return 1
            fi
        }

        # starts $svc
        pp_start () {
            if pp_running; then
                echo "service $svc already running" >&2
                return 0
            fi
            echo "starting $svc... \c"
            if pp_exec; then
                echo "done."
            else
                echo "ERROR."
                exit 1
            fi
        }

        # stops $svc
        pp_stop () {
            if pp_running; then
                echo "stopping $svc... \c"
                if kill -$stop_signal $pid; then
                    rm -f "$pidfile"
                    echo "done."
                else
                    echo "ERROR."
                    return 1
                fi
            else
                echo "service $svc already stopped" >&2
                return 0
            fi
        }

        umask 022
        case "$1" in
            start)   pp_start;;
            stop)    pp_stop;;
            status)  pp_status;;
            restart) pp_stop && pp_start;;
            *)       echo "usage: $0 {start|stop|restart|status}" >&2; exit 1;;
        esac
.
}

pp_solaris_remove_service () {
    typeset file svc

    svc="${pp_solaris_smf_service_name:-$1}"
    file=${pp_solaris_service_script:-"/etc/init.d/${pp_solaris_service_script_name:-$svc}"}

    echo '
'$file' stop >/dev/null 2>/dev/null
if [ "x${PKG_INSTALL_ROOT}" = 'x' ]; then
    if [ -x /usr/sbin/svcadm ] ; then
        # Likely un-needed, but just in case.
        /usr/sbin/svcadm disable -s '$svc' 2>/dev/null
        /usr/sbin/svccfg delete '$svc' 2>/dev/null
    fi
fi
    '
}

pp_solaris_install_service () {
    typeset s k l file svc

    svc="${pp_solaris_smf_service_name:-$1}"
    file=${pp_solaris_service_script:-"/etc/init.d/${pp_solaris_service_script_name:-$svc}"}

    s="${solaris_sysv_init_start}$svc"
    k="${solaris_sysv_init_kill}$svc"

    echo '
if [ "x${PKG_INSTALL_ROOT}" != "x" ]; then
  if [ -x ${PKG_INSTALL_ROOT}/usr/sbin/svcadm ]; then
    echo "/usr/sbin/svccfg import '$pp_svc_xml_file' 2>/dev/null" >> ${PKG_INSTALL_ROOT}/var/svc/profile/upgrade
  else'
    test -n "${solaris_sysv_init_start_states}" &&
        for state in ${solaris_sysv_init_start_states}; do
            l="/etc/rc$state.d/$s"
            echo "echo '$l'"
            echo "installf -c run \$PKGINST \$PKG_INSTALL_ROOT$l=$file s"
            pp_solaris_space /etc/rc$state.d 0 1
        done
    test -n "${solaris_sysv_init_kill_states}" &&
        for state in ${solaris_sysv_init_kill_states}; do
            l="/etc/rc$state.d/$k"
            echo "echo '$l'"
            echo "installf -c run \$PKGINST \$PKG_INSTALL_ROOT$l=$file s"
            pp_solaris_space /etc/rc$state.d 0 1
        done
    echo '
  fi
else
    if [ -x /usr/sbin/svcadm ]; then
        echo "Registering '$svc' with SMF"
        /usr/sbin/svcadm disable -s '$svc' 2>/dev/null
        /usr/sbin/svccfg delete '$svc' 2>/dev/null
        /usr/sbin/svccfg import '$pp_svc_xml_file'
    else'
    test -n "${solaris_sysv_init_start_states}" &&
        for state in ${solaris_sysv_init_start_states}; do
            l="/etc/rc$state.d/$s"
            echo "echo '$l'"
            echo "installf -c run \$PKGINST \$PKG_INSTALL_ROOT$l=$file s"
            pp_solaris_space /etc/rc$state.d 0 1
        done
    test -n "${solaris_sysv_init_kill_states}" &&
        for state in ${solaris_sysv_init_kill_states}; do
            l="/etc/rc$state.d/$k"
            echo "echo '$l'"
            echo "installf -c run \$PKGINST \$PKG_INSTALL_ROOT$l=$file s"
            pp_solaris_space /etc/rc$state.d 0 1
        done
    echo '
    fi
fi'
}

pp_solaris_add_parent_dirs () {
    typeset dir

    dir=${1%/*}
    while test -n "$dir"; do
	if awk "\$6 == \"$dir/\" {exit 1}" < $pp_wrkdir/%files.run; then
	    echo "d - - - - $dir/" >> $pp_wrkdir/%files.run
	fi
	dir=${dir%/*}
    done
}

pp_platforms="$pp_platforms deb"

pp_backend_deb_detect () {
    test -f /etc/debian_version
}

pp_deb_cmp_full_name () {
    local prefix
    prefix="${pp_deb_name:-$name}"
    case "$1" in
        run) echo "${prefix}" ;;
        dbg) echo "${prefix}-${pp_deb_dbg_pkgname}";;
        dev) echo "${prefix}-${pp_deb_dev_pkgname}";;
        doc) echo "${prefix}-${pp_deb_doc_pkgname}";;
        *)   pp_error "unknown component '$1'";
    esac
}

pp_backend_deb_init () {
    pp_deb_dpkg_version="2.0"
    pp_deb_name=
    pp_deb_version=
    pp_deb_release=
    pp_deb_arch=
    pp_deb_arch_std=
    pp_deb_maintainer=support@quest.com
    pp_deb_copyright=
    pp_deb_distro=
    pp_deb_control_description=
    pp_deb_summary=
    pp_deb_description=
    pp_deb_dbg_pkgname="dbg"
    pp_deb_dev_pkgname="dev"
    pp_deb_doc_pkgname="doc"
    pp_deb_section=contrib # Free software that depends on non-free software

    # Detect the host architecture
    pp_deb_detect_arch

    # Make sure any programs we require are installed
    pp_deb_check_required_programs

    # Set generated/interrogated platforms variables
    pp_deb_munge_description
}

pp_deb_check_required_programs () {
    local p needed notfound ok
    needed= notfound=
    for prog in dpkg dpkg-deb install md5sum fakeroot
    do
        if which $prog 2>/dev/null >/dev/null; then
	    pp_debug "$prog: found"
	else
	    pp_debug "$prog: not found"
	    case "$prog" in
		dpkg|dpkg-deb)	p=dpkg;;
		install|md5sum) p=coreutils;;
		fakeroot)	p=fakeroot;;
		*)		pp_die "unexpected dpkg tool $prog";;
	    esac
	    notfound="$notfound $prog"
	    pp_contains "$needed" "$p" || needed="$needed $p"
	fi
    done
    if [ -n "$notfound" ]; then
	pp_error "cannot find these programs: $notfound"
	pp_error "please install these packages: $needed"
    fi
}

pp_deb_munge_description () {
    # Insert a leading space on each line, replace blank lines with a
    #space followed by a full-stop.
    pp_deb_control_description=`echo ${pp_deb_description:-$description} | \
        sed "s,^\(.*\)$, \1, " \
        | sed "s,^[ \t]*$, .,g"`

}

pp_deb_detect_arch () {
   pp_deb_arch=`dpkg --print-architecture`
   pp_deb_arch_std=`uname -m`
}

pp_deb_sanitize_version() {
    echo "$@" | tr -d -c '[:alnum:].+-:~'
}

pp_deb_version_final() {
    if test -n "$pp_deb_version"; then
        # Don't sanitize; assume the user is sane (hah!)
        echo "$pp_deb_version"
    else
        pp_deb_sanitize_version "$version"
    fi
}

pp_deb_conflict () {
    local _name _vers _conflicts

    _conflicts="Conflicts:"
    while read _name _vers; do
	case "$_name" in ""| "#"*) continue ;; esac
	_conflicts="$_conflicts $_name"
	test -n "$_vers" && _conflicts="$_conflicts $_name (>= $vers)"
	_conflicts="${_conflicts},"
    done
    echo "${_conflicts%,}"
}

pp_deb_make_control() {
    package_name=`pp_deb_cmp_full_name "$1"`
    cat <<-.
	Package: ${package_name}
	Version: `pp_deb_version_final`-${pp_deb_release:-1}
	Section: ${pp_deb_section:-contrib}
	Priority: optional
	Architecture: ${pp_deb_arch}
	Maintainer: ${pp_deb_maintainer:-$maintainer}
	Description: ${pp_deb_summary:-$summary}
	${pp_deb_control_description}
.
    if test -s $pp_wrkdir/%depend."$1"; then
	sed -ne '/^[ 	]*$/!s/^[ 	]*/Depends: /p' \
	    < $pp_wrkdir/%depend."$1"
    fi
    if test -s $pp_wrkdir/%conflict."$1"; then
	pp_deb_conflict < $pp_wrkdir/%conflict."$1"
    fi
}

pp_deb_make_md5sums() {
    local cmp="$1"; shift
    local pkg_dir

    pkg_dir=$pp_wrkdir/`pp_deb_cmp_full_name $cmp`
    (cd $pkg_dir && md5sum "$@") > $pkg_dir/DEBIAN/md5sums ||
	pp_error "cannot make md5sums"
}

pp_deb_make_package_maintainer_script() {
    local output="$1"
    local source="$2"
    local desc="$3"

    # See if we need to create this script at all
    if [ -s "$source" ]
    then

        # Create header
        cat <<-. >$output || pp_error "Cannot create $output"
	#!/bin/sh
	# $desc
	# Generated by PolyPackage $pp_version

.

        cat $source >> "$output" || pp_error "Cannot append to $output"

        # Set perms
        chmod 755 "$output" || pp_error "Cannot chmod $output"
    fi
}

pp_deb_handle_services() {
    local svc

    #-- add service start/stop code
    if test -n "$pp_services"; then
        #-- record the uninstall commands in reverse order
        for svc in $pp_services; do
            pp_load_service_vars $svc

            # Create init script (unless one exists)
            pp_deb_service_make_init_script $svc

            #-- append %post code to install the svc
	    test x"yes" = x"$enable" &&
            cat<<-. >> $pp_wrkdir/%post.run
		# Install the service links
		/usr/sbin/update-rc.d $svc defaults
.

            #-- prepend %preun code to stop svc
            cat<<-. | pp_prepend $pp_wrkdir/%preun.run
		# Stop the $svc service
		if test -x /usr/sbin/invoke-rc.d; then
		    /usr/sbin/invoke-rc.d $svc stop
		else
		    /etc/init.d/$svc stop
		fi
		# Remove the service links
		/usr/sbin/update-rc.d -f $svc remove
.
        done
        #pp_deb_service_remove_common | pp_prepend $pp_wrkdir/%preun.run
    fi

}
pp_deb_fakeroot () {
    if test -s $pp_wrkdir/fakeroot.save; then
	fakeroot -i $pp_wrkdir/fakeroot.save -s $pp_wrkdir/fakeroot.save "$@"
    else
	fakeroot -s $pp_wrkdir/fakeroot.save "$@"
    fi
}

pp_deb_make_DEBIAN() {
    local cmp="${1:-run}"
    local data cmp_full_name
    local old_umask

    old_umask=`umask`
    umask 0022
    cmp_full_name=`pp_deb_cmp_full_name $cmp`
    data=$pp_wrkdir/$cmp_full_name

    # Create DEBIAN dir $data/DEBIAN
    mkdir -p $data/DEBIAN

    # Create control file
    pp_deb_make_control $cmp > $data/DEBIAN/control

    # Copy in conffiles
    if test -f $pp_wrkdir/%conffiles.$cmp; then
	cp $pp_wrkdir/%conffiles.$cmp $data/DEBIAN/conffiles
    fi

    # Create postinst
    pp_deb_make_package_maintainer_script "$data/DEBIAN/postinst" \
        "$pp_wrkdir/%post.$cmp" "Post install script for $cmp_full_name"\
        || exit $?

    # Create prerm
    pp_deb_make_package_maintainer_script "$data/DEBIAN/prerm" \
        "$pp_wrkdir/%preun.$cmp" "Pre-uninstall script for $cmp_full_name"\
        || exit $?

    # Create postrm
    pp_deb_make_package_maintainer_script "$data/DEBIAN/postrm" \
        "$pp_wrkdir/%postun.$cmp" "Post-uninstall script for $cmp_full_name"\
        || exit $?

    umask $old_umask
}

pp_deb_make_data() {
    local _l t m o g f p st data
    local data share_doc owner group
    cmp=$1
    data=$pp_wrkdir/`pp_deb_cmp_full_name $cmp`
    cat $pp_wrkdir/%files.${cmp} | while read t m o g f p st; do
	if test x"$m" = x"-"; then
	    case "$t" in
		d) m=755;;
		f) m=644;;
	    esac
	fi
	test x"$o" = x"-" && o=root
	test x"$g" = x"-" && g=root
        case "$t" in
        f) # Files
           pp_deb_fakeroot install -D -o $o -g $g -m ${m} $pp_destdir/$p $data/$p;
           if [ x"$f" = x"v" ]
           then
               # File marked as "volatile". Assume this means it's a conffile
	       # TODO: check this as admins like modified conffiles to be left
	       #       behind
               echo "$p" >> $pp_wrkdir/%conffiles.$cmp
           fi;;

        d) # Directories
           pp_deb_fakeroot install -m ${m} -o $o -g $g -d $data/$p;;

        s) # Symlinks
           # Remove leading / from vars
           rel_p=`echo $p | sed s,^/,,`
           rel_st=`echo $st | sed s,^/,,`
           # TODO: we are always doing absolute links here. We should follow
	   # the debian policy of relative links when in the same top-level
	   # directory
           (cd $data; ln -sf $st $rel_p);;
	    *) pp_error "Unsupported data file type: $t";;
	esac
    done

    # If no copyright file is present add one. This is a debian requirement.
    share_doc="/usr/share/doc/`pp_deb_cmp_full_name $cmp`"
    if [ ! -f "$data/$share_doc/copyright" ]
    then
        echo "${pp_deb_copyright:-$copyright}" > "$pp_wrkdir/copyright"
        install -D -m 644 "$pp_wrkdir/copyright" "$data/$share_doc/copyright"
    fi

}

pp_deb_makedeb () {
    local cmp
    local package_build_dir

    cmp="$1"

    package_build_dir=$pp_wrkdir/`pp_deb_cmp_full_name $cmp`

    # Create package dir
    mkdir -p $package_build_dir

    # Copy in data
    pp_deb_make_data $cmp ||
	pp_die "Could not make DEBIAN data files for $cmp"

    # Make control files
    # must be done after copying data so conffiles are found
    pp_deb_make_DEBIAN $cmp ||
	pp_die "Could not make DEBIAN control files for $cmp"

    # Create md5sums
    pp_deb_make_md5sums $cmp `(cd $package_build_dir;
	find . -type f -a -not -name DEBIAN | sed "s,^\./,,")` ||
	    pp_die "Could not make DEBIAN md5sums for $cmp"
}

pp_backend_deb () {
    local debname

    # Handle services
    pp_deb_handle_services $cmp

    for cmp in $pp_components
    do
        debname=`pp_deb_name $cmp`
        pp_deb_makedeb $cmp
    done

    . $pp_wrkdir/%fixup

    for cmp in $pp_components
    do
        debname=`pp_deb_name $cmp`
	# Create debian package
	pp_debug "Building `pp_deb_cmp_full_name $cmp` -> $output"
	pp_deb_fakeroot dpkg-deb \
	    --build $pp_wrkdir/`pp_deb_cmp_full_name $cmp` \
	    $pp_wrkdir/$debname ||
		pp_error "failed to create $cmp package"
    done
}

pp_backend_deb_cleanup () {
    # rm -rf $pp_wrkdir
    :
}

pp_deb_name () {
    local cmp="${1:-run}"
    echo `pp_deb_cmp_full_name $cmp`"_"`pp_deb_version_final`"-${pp_deb_release:-1}_${pp_deb_arch}.deb"
}
pp_backend_deb_names () {
    for cmp in $pp_components
    do
        pp_deb_name $cmp
    done
}

pp_backend_deb_install_script () {
    local cmp _cmp_full_name

    echo "#!/bin/sh"
    pp_install_script_common

    cat <<.

        cmp_to_pkgname () {
            test x"\$*" = x"all" &&
                set -- $pp_components
            for cmp
            do
                case \$cmp in
.
    for cmp in $pp_components; do
         echo "$cmp) echo '`pp_deb_cmp_full_name $cmp`';;"
    done
    cat <<.
                *) usage;;
                esac
            done
        }


        cmp_to_pathname () {
            test x"\$*" = x"all" &&
                set -- $pp_components
            for cmp
            do
                case \$cmp in
.
    for cmp in $pp_components; do
         echo "$cmp) echo \${PP_PKGDESTDIR:-.}/'`pp_deb_name $cmp`';;"
    done
    cat <<.
                *) usage;;
		esac
            done
        }

        test \$# -eq 0 && usage
        op="\$1"; shift
        case "\$op" in
            list-components)
                test \$# -eq 0 || usage \$op
                echo $pp_components
                ;;
            list-services)
                test \$# -eq 0 || usage \$op
                echo $pp_services
                ;;
            list-files)
                test \$# -ge 1 || usage \$op
                cmp_to_pathname "\$@"
                ;;
            install)
                test \$# -ge 1 || usage \$op
                dpkg --install \`cmp_to_pathname "\$@"\`
                ;;
            uninstall)
                test \$# -ge 1 || usage \$op
                dpkg --remove \`cmp_to_pkgname "\$@"\`; :
                ;;
            start|stop)
                test \$# -ge 1 || usage \$op
                ec=0
                for svc
                do
                    /etc/init.d/\$svc \$op || ec=1
                done
                exit \$ec
                ;;
            print-platform)
                test \$# -eq 0 || usage \$op
		echo "linux-${pp_deb_arch}"
		;;
            *)
                usage
                ;;
        esac
.
}

pp_backend_deb_probe() {
    local arch distro release

    pp_deb_detect_arch

    # /etc/debian_version exists on Debian & Ubuntu, so it's no use
    # to us. Use lsb_release instead.

    case `(lsb_release -is || echo no-lsb) 2>/dev/null` in
        Debian)
            distro=deb
	    ;;
        Ubuntu)
            distro=ubu
	    ;;
	no-lsb)
	    echo unknown-$pp_deb_arch_std
	    return 0
	    ;;
        *)
            distro=unknown
	    ;;
    esac

    release=`lsb_release -rs`

    # If release is not numeric, use the codename
    case $release in
        *[!.0-9r]*)
	    release=`lsb_release -cs`
	    case $release in
		buzz)
		    release="11"
		    ;;
		rex)
		    release="12"
		    ;;
		bo)
		    release="13"
		    ;;
		hamm)
		    release="20"
		    ;;
		slink)
		    release="21"
		    ;;
		potato)
		    release="22"
		    ;;
		woody)
		    release="30"
		    ;;
		sarge)
		    release="31"
		    ;;
		etch)
		    release="40"
		    ;;
		lenny)
		    release="50"
		    ;;
		squeeze)
		    release="60"
		    ;;
	    esac
	    ;;
	*)
	    # Remove trailing revision number and any dots
            release=`echo $release | cut -dr -f1 | tr -d .`
	    ;;
    esac

    echo $distro$release-$pp_deb_arch_std
}

pp_backend_deb_vas_platforms () {
    case "$pp_deb_arch_std" in
	x86_64)	echo "linux-x86_64.deb";; # DO NOT add linux-x86.deb here!!
	*86)	echo "linux-x86.deb";;
	*)	pp_die "unknown architecture ${pp_deb_arch_std}";;
    esac
}
pp_backend_deb_init_svc_vars () {

    reload_signal=
    start_runlevels=${pp_deb_default_start_runlevels-"2 3 4 5"} # == lsb default-start
    stop_runlevels=${pp_deb_default_stop_runlevels-"0 1 6"}     # == lsb default-stop
    svc_description="${pp_deb_default_svc_description}" # == lsb short descr
    svc_process=

    lsb_required_start='$local_fs $network'
    lsb_should_start=
    lsb_required_stop=
    lsb_description=

    start_priority=50
    stop_priority=50            #-- stop_priority = 100 - start_priority
}

pp_deb_service_make_init_script () {
    local svc=$1
    local script=/etc/init.d/$svc
    local out=$pp_destdir$script
    local _process _cmd

    pp_add_file_if_missing $script run 755 || return 0

    #-- start out as an empty shell script
    cat <<-'.' >$out
	#!/bin/sh
.

    #-- determine the process name from $cmd unless $svc_process is given
    set -- $cmd
    #_process=${svc_process:-"$1"} --? WTF

    #-- construct a start command that builds a pid file if needed
    #-- the command name in /proc/[pid]/stat is limited to 15 characters 
    _cmd="$cmd";
    _cmd_path=`echo $cmd | cut -d" " -f1`
    _cmd_name=`basename $_cmd_path | cut -c1-15`
    _cmd_args=`echo $cmd | cut -d" " -f2-`
    test x"$_cmd_path" != x"$_cmd_args" || _cmd_args=

    #-- generate the LSB init info
    cat <<-. >>$out
	### BEGIN INIT INFO
	# Provides: ${svc}
	# Required-Start: ${lsb_required_start}
	# Should-Start: ${lsb_should_start}
	# Required-Stop: ${lsb_required_stop}
	# Default-Start: ${start_runlevels}
	# Default-Stop: ${stop_runlevels}
	# Short-Description: ${svc_description:-no description}
	### END INIT INFO
	# Generated by PolyPackage ${pp_version}
	# ${copyright}

.

    if test x"${svc_description}" = x"${pp_deb_default_svc_description}"; then
        svc_description=
    fi

    #-- write service-specific definitions
    cat <<. >>$out
NAME="${_cmd_name}"
DESC="${svc_description:-$svc service}"
USER="${user}"
GROUP="${group}"
PIDFILE="${pidfile}"
STOP_SIGNAL="${stop_signal}"
RELOAD_SIGNAL="${reload_signal}"
CMD="${_cmd}"
DAEMON="${_cmd_path}"
DAEMON_ARGS="${_cmd_args}"
SCRIPTNAME=${script}
.

    #-- write the generic part of the init script
    cat <<'.' >>$out

[ -x "$DAEMON" ] || exit 0

[ -r /etc/default/$NAME ] && . /etc/default/$NAME

[ -f /etc/default/rcS ] && . /etc/default/rcS

. /lib/lsb/init-functions

do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
    if [ -n "$PIDFILE" ]
    then
        pidfile_opt="--pidfile $PIDFILE"
    else
        pidfile_opt="--make-pidfile --background --pidfile /var/run/$NAME.pid"
    fi
    if [ -n "$USER" ]
    then
        user_opt="--user $USER"
    fi
    if [ -n "$GROUP" ]
    then
        group_opt="--group $GROUP"
    fi
    if [ "$VERBOSE" = no ]
    then
        quiet_opt="--quiet"
    else
        quiet_opt="--verbose"
    fi

	start-stop-daemon --start $quiet_opt $pidfile_opt $user_opt --exec $DAEMON --test > /dev/null \
	    || return 1

    # Note: there seems to be no way to tell whether the daemon will fork itself or not, so pass
    # --background for now
    start-stop-daemon --start $quiet_opt $pidfile_opt $user_opt --exec $DAEMON -- \
    	$DAEMON_ARGS \
    	|| return 2
}

do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred
    if [ -n "$PIDFILE" ]
    then
        pidfile_opt="--pidfile $PIDFILE"
    else
        pidfile_opt="--pidfile /var/run/$NAME.pid"
    fi
    if [ -n "$USER" ]
    then
        user_opt="--user $USER"
    fi
    if [ -n $STOP_SIGNAL ]
    then
        signal_opt="--signal $STOP_SIGNAL"
    fi
    if [ "$VERBOSE" = "no" ]
    then
        quiet_opt="--quiet"
    else
        quiet_opt="--verbose"
    fi
	start-stop-daemon --stop $quiet_opt $signal_opt --retry=TERM/30/KILL/5 $pidfile_opt --name $NAME
	RETVAL="$?"
	[ "$RETVAL" = 2 ] && return 2
	# Wait for children to finish too if this is a daemon that forks
	# and if the daemon is only ever run from this initscript.
	# If the above conditions are not satisfied then add some other code
	# that waits for the process to drop all resources that could be
	# needed by services started subsequently.  A last resort is to
	# sleep for some time.
	start-stop-daemon --stop $quiet_opt --oknodo --retry=0/30/KILL/5 --exec $DAEMON
	[ "$?" = 2 ] && return 2
	# Many daemons don't delete their pidfiles when they exit.
	test -z $PIDFILE || rm -f $PIDFILE
	return "$RETVAL"
}

do_reload() {
	#
	# If the daemon can reload its configuration without
	# restarting (for example, when it is sent a SIGHUP),
	# then implement that here.
	#
    if [ -n "$PIDFILE" ]
    then
        pidfile_opt="--pidfile $PIDFILE"
    else
        pidfile_opt="--pidfile /var/run/$NAME.pid"
    fi
    if [ -n "$RELOAD_SIGNAL" ]
    then
	    start-stop-daemon --stop --signal $RELOAD_SIGNAL $quiet_opt $pidfile_opt --name $NAME
    fi
	return 0
}

case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  reload|force-reload)
    if [ -n "$RELOAD_SIGNAL" ]
    then
	    log_daemon_msg "Reloading $DESC" "$NAME"
	    do_reload
	    log_end_msg $?
    else
        # Do a restart instead
        "$0" restart
    fi
	;;
  restart)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 3
	;;
esac

:
.
    chmod 755 $out
}
pp_backend_deb_function() {
    case "$1" in
        pp_mkgroup) cat<<'.';;
	    /usr/sbin/groupmod "$1" 2>/dev/null && return 0
            /usr/sbin/groupadd "$1"
.
        pp_mkuser:depends) echo pp_mkgroup;;
        pp_mkuser) cat<<'.';;
	    pp_tmp_system=
	    id -u "$1" >/dev/null 2>/dev/null && return 0
	    # deb 3.1's useradd changed API in 4.0. Gah!
	    /usr/sbin/useradd --help 2>&1 | /bin/grep -q .--system &&
		pp_tmp_system=--system
	    pp_mkgroup "${2:-$1}" || return 1
	    /usr/sbin/useradd \
		-g "${2:-$1}" \
		-d "${3:-/nonexistent}" \
		-s "${4:-/bin/false}" \
		$pp_tmp_system \
		"$1"
.
        pp_havelib) cat<<'.';;
            for pp_tmp_dir in `echo "/usr/lib:/lib${3:+:$3}" | tr : ' '`; do
                test -r "$pp_tmp_dir/lib$1.so{$2:+.$2}" && return 0
            done
            return 1
.
    *) false;;
    esac
}

pp_platforms="$pp_platforms kit"

pp_backend_kit_detect () {
    test x"$1" = x"OSF1"
}

pp_backend_kit_init () {
        pp_kit_name=
        pp_kit_package=
        pp_kit_desc=
        pp_kit_version=
        pp_kit_subset=
        pp_readlink_fn=pp_ls_readlink
        pp_kit_startlevels="2 3"
        pp_kit_stoplevels="0 2 3"
}

pp_backend_kit () {
    typeset mi_file k_file svc outfile
    typeset desc

    pp_backend_kit_names > /dev/null

    if test -z "$pp_kit_desc"; then
        pp_kit_desc="$description"
    fi

    mi_file="$pp_wrkdir/$pp_kit_subset.mi"
    k_file="$pp_wrkdir/$pp_kit_subset.k"
    scp_file="$pp_wrkdir/$pp_kit_subset.scp"

    desc="${pp_kit_desc:-$description}"

    cat <<-. >> $k_file
	NAME='$name'
	CODE=$pp_kit_name
	VERS=$pp_kit_version
	MI=$mi_file
	COMPRESS=0
	%%
	$pp_kit_subset	.	0	'$desc'
.

    if test -n "$pp_services"; then
        for svc in $pp_services; do
            pp_kit_make_service $svc
            pp_prepend $pp_wrkdir/%preun.run <<-.
		/sbin/init.d/$svc stop
.
         done
    fi

    pp_backend_kit_make_mi "$mi_file"
    pp_backend_kit_make_scp
    #rm -rf  $pp_wrkdir/kit_dest
    mkdir -p $pp_wrkdir/kit_dest
    pp_backend_kit_kits $k_file $pp_opt_destdir $pp_wrkdir/kit_dest
    tar cvf $pp_wrkdir/$pp_kit_subset.tar -C $pp_wrkdir/kit_dest .
    gzip -c $pp_wrkdir/$pp_kit_subset.tar > $pp_wrkdir/$pp_kit_subset.tar.gz
    #rm -rf $pp_wrkdir/$pp_kit_subset.tar $pp_wrkdir/scps
}

pp_backend_kit_make_mi () {
    # XXX this information should go into the .inv files
    typeset t m o g f p st line dm
    while read t m o g f p st; do
        case $t in
            f|d)
                echo "0	.$p	$pp_kit_subset"
                echo "        chmod $m $p" >> $pp_wrkdir/%post.run
                if [ x"$o" = x"-" ] ; then
                    echo "        chown root $p" >> $pp_wrkdir/%post.run
                else
                    echo "        chown $o $p" >> $pp_wrkdir/%post.run
                fi
                if [ x"$g" = x"-" ] ; then
                    echo "        chgrp 0 $p" >> $pp_wrkdir/%post.run
                else
                    echo "        chgrp $g $p" >> $pp_wrkdir/%post.run
                fi
                ;;
            s)
                echo "        ln -s $st $p" >> $pp_wrkdir/%post.run
                echo "        rm -f $p" >> $pp_wrkdir/%preun.run
                ;;
         esac
    done < $pp_wrkdir/%files.run | sort -k3  |uniq > $1
}


pp_backend_kit_make_scp () {
    scpdir="$pp_wrkdir/scps"
    mkdir "$scpdir" && touch "$scpdir"/$pp_kit_subset.scp
    cat <<EOF >"$scpdir"/$pp_kit_subset.scp

    . /usr/share/lib/shell/libscp

    case "\$ACT" in
    PRE_L)
    STL_ScpInit



    ;;
    POST_L)
        STL_ScpInit
        STL_LinkCreate
EOF

    cat $pp_wrkdir/%post.run >>"$scpdir"/$pp_kit_subset.scp
    cat >>"$scpdir"/$pp_kit_subset.scp <<EOF
    ;;
    PRE_D)
        STL_ScpInit
        STL_LinkRemove
EOF
    cat $pp_wrkdir/%preun.run >>"$scpdir"/$pp_kit_subset.scp
    cat >>"$scpdir"/$pp_kit_subset.scp <<EOF
        ;;
    POST_D)

        ;;
    C)
        STL_ScpInit

        case "\$1" in
        INSTALL)
        echo "Installation of the \$_DESC (\$_SUB) subset is complete."
        ;;
    DELETE)
        ;;
    esac

        ;;
    V)

        ;;
    esac

    exit 0
EOF
    chmod 744 "$scpdir"/$pp_kit_subset.scp
}


pp_backend_kit_cleanup () {
    :
}

pp_backend_kit_names () {
    if test -z "$pp_kit_name"; then
        pp_warn "pp_kit_name not specified, using XXX"
        pp_kit_name=XXX
    fi
    case "$pp_kit_name" in
        ???) : ok;;
        *) pp_error "\$pp_kit_name $pp_kit_name must be three characters";;
    esac
    if test -z "$pp_kit_package"; then
        pp_warn "pp_kit_package not specified, using YYYY"
        pp_kit_package=YYYY
    fi
    if test -z "$pp_kit_version"; then
        pp_kit_version=`echo $version|tr -d '.a-zA-Z'`
    fi
    case "$pp_kit_version" in
        [0-9]) pp_kit_version="${pp_kit_version}00";;
        [0-9][0-9]) pp_kit_version="${pp_kit_version}0";;
        [0-9][0-9][0-9]) : ok;;
        *) pp_error "\$pp_kit_version $pp_kit_version must be three digits, ";;
    esac
    if test -z "$pp_kit_subset"; then
        pp_kit_subset="$pp_kit_name$pp_kit_package$pp_kit_version"
    fi
    echo "$pp_kit_subset.tar.gz"
}

pp_backend_kit_install_script () {
    typeset pkgname platform

    pkgname=`pp_backend_kit_names`
    platform="`pp_backend_kit_probe`"

    echo "#!/bin/sh"
    pp_install_script_common
    cat <<.

        cpt_to_tags () {
            test x"\$*" = x"all" && set -- $pp_components
            for cpt
            do
                echo "$name.\$cpt"
            done
        }

        test \$# -eq 0 && usage
        op="\$1"; shift

        case "\$op" in
            list-components)
                test \$# -eq 0 || usage \$op
                echo "$pp_components"
                ;;
            list-services)
                test \$# -eq 0 || usage \$op
                echo "$pp_services"
                ;;
            list-files)
                test \$# -ge 1 || usage \$op
                echo \${PP_PKGDESTDIR:-.}/$pkgname
                ;;
            install)
                test \$# -ge 1 || usage \$op
                verbose echo \${PP_PKGDESTDIR:-\`pwd\`}/$pkgname \`cpt_to_tags "\$@"\`
                #verbose swinstall -x verbose=0 -s \${PP_PKGDESTDIR:-\`pwd\`}/$pkgname \`cpt_to_tags "\$@"\`
                ;;
            uninstall)
                test \$# -ge 1 || usage \$op
                verbose echo \`cpt_to_tags "\$@"\`
                #verbose swremove -x verbose=0 \`cpt_to_tags "\$@"\`
                ;;
            start|stop)
                test \$# -ge 1 || usage \$op
                ec=0
                for svc
                do
                    verbose /sbin/init.d/\$svc \$op
                    [ \$? -eq 4 -o \$? -eq 0 ] || ec=1
                done
                exit \$ec
                ;;
            print-platform)
		echo "$platform"
		;;
            *)
                usage
                ;;
        esac
.
}

pp_backend_kit_function () {
    case "$1" in
        pp_mkgroup) cat <<'.';;
            grep "^$1:" /etc/group >/dev/null ||
                /usr/sbin/groupadd $1
.
        pp_mkuser) cat <<'.';;
            eval user=\$$#
            grep "^$user:" /etc/passwd >/dev/null ||
                /usr/sbin/useradd -s /usr/bin/false "$@"
.
        pp_havelib) cat <<'.';;
            for dir in `echo /usr/lib${3+:$3} | tr : ' '`; do
                test -r "$dir/lib$1.${2-sl}" && return 0
            done
            return 1
.
        *) pp_error "unknown function request: $1";;
    esac
}

pp_backend_kit_init_svc_vars () {
    :
}

pp_backend_kit_probe () {
    echo tru64-`uname -r | sed 's/V\([0-9]*\)\.\([0-9]*\)/\1\2/'`
}

pp_kit_service_group_script () {
    typeset grp svcs scriptpath out
    grp="$1"
    svcs="$2"
    scriptpath="/sbin/init.d/$grp"
    out="$pp_destdir$scriptpath"

    pp_add_file_if_missing $scriptpath run 755 || return 0

    cat <<-. > $out
	#!/sbin/sh
	# generated by pp $pp_version
	svcs="$svcs"
.

cat <<-'.' >> $out
	#-- starts services in order.. stops them all if any break
	pp_start () {
	    undo=
	    for svc in $svcs; do
	        /sbin/init.d/$svc start
	        case $? in
	            0|4)
	                undo="$svc $undo"
	                ;;
	            *)
	                if test -n "$undo"; then
	                for svc in $undo; do
	                    /sbin/init.d/$svc stop
	                done
	                return 1
	                fi
	                ;;
	        esac
	    done
	    return 0
	}

	#-- stops services in reverse
	pp_stop () {
	    reverse=
	        for svc in $svcs; do
	            reverse="$svc $reverse"
	        done
	        rc=0
	        for svc in $reverse; do
	            /sbin/init.d/$svc stop || rc=$?
	        done
	        return $rc
	}

	case "$1" in
	    start_msg) echo "Starting $svcs";;
	stop_msg)  echo "Stopping $svcs";;
	start)     pp_start;;
	stop)      pp_stop;;
	*)         echo "usage: $0 {start|stop|start_msg|stop_msg}"
	    exit 1;;
	esac
.
}

pp_kit_service_script () {
    typeset svc scriptpath out

    svc="$1"
    scriptpath="/sbin/init.d/$svc"

    pp_load_service_vars "$svc"

    test -n "$user" -a x"$user" != x"root" &&
    cmd="SHELL=/usr/bin/sh /usr/bin/su $user -c \"exec `echo $cmd | sed -e 's,[$\\\`],\\&,g'`\""
    if test -z "$pidfile"; then
        pidfile="/var/run/$svc.pid"
        cmd="$cmd & echo \$! > \$pidfile"
    fi

    pp_add_file_if_missing $scriptpath run 755

    cat <<-. > $pp_destdir$scriptpath
	svc="$svc"
	pidfile="$pidfile"

	pp_start () {
	    $cmd
	}
.
    cat <<-'.' >>$pp_destdir$scriptpath
	    pp_stop () {
	        if test ! -s "$pidfile"; then
	            echo "Unable to stop $svc (no pid file)"
	                return 1
	        else
	            read pid < "$pidfile"
	            if kill -0 "$pid" 2>/dev/null; then
	                if kill -${stop_signal:-TERM} "$pid"; then
	                    rm -f "$pidfile"
	                    return 0
	                else
	                    echo "Unable to stop $svc"
	                    return 1
	                fi
	            else
	                rm -f "$pidfile"
	                return 0
	            fi
	        fi
	    }

	    pp_running () {
	        if test ! -s "$pidfile"; then
	            return 1
	        else
	            read pid < "$pidfile"
	            kill -0 "$pid" 2>/dev/null
	        fi
	    }
	    case "$1" in
	        start_msg) echo "Starting the $svc service";;
	        stop_msg)  echo "Stopping the $svc service";;
	        start)
	            if pp_running; then
	                echo "$svc already running";
	                exit 0
	            elif pp_start; then
	                echo "$svc started";
	# rc(1M) says we should exit 4, but nobody expects it!
	                exit 0
	            else
	                exit 1
	            fi
	            ;;
	        stop)
	            if pp_stop; then
	                echo "$svc stopped";
	                exit 0
	            else
	                exit 1
	            fi
	            ;;
	        *) echo "usage: $0 {start|stop|start_msg|stop_msg}"
	           exit 1
	           ;;
	    esac
.
}

pp_kit_make_service () {
    typeset level priority startlevels stoplevels
    typeset svc svcvar

    svc="$1"
    svcvar=`pp_makevar $svc`

    #-- don't do anything if the script exists
    if test -s "$pp_destdir/sbin/init.d/$svc"; then
        pp_error "$pp_destdir/sbin/init.d/$svc exists"
        return
    fi

    # symlink the script, depending on the priorities chosen
    eval priority='${pp_kit_priority_'$svcvar'}'
    test -z "$priority" && priority="${pp_kit_priority:-50}"

    eval startlevels='${pp_kit_startlevels_'$svcvar'}'
    test -z "$startlevels" && startlevels="$pp_kit_startlevels"

    eval stoplevels='${pp_kit_stoplevels_'$svcvar'}'
    test -z "$stoplevels" && stoplevels="$pp_kit_stoplevels"

    # create the script and config file
    pp_kit_service_script $svc

    # fix the priority up
    case "$priority" in
        ???) :;;
        ??) priority=0$priority;;
        ?) priority=00$priority;;
    esac

    if test x"$stoplevels" = x"auto"; then
        stoplevels=
        test -z "$startlevels" || for level in $startlevels; do
            stoplevels="$stoplevels `expr $level - 1`"
        done
    fi

    # create the symlinks
    test -z "$startlevels" || for level in $startlevels; do
        echo "        ln -s /sbin/init.d/$svc /sbin/rc$level.d/S$priority$svc" >>$pp_wrkdir/%post.run
        echo "        rm /sbin/rc$level.d/S$priority$svc" >>$pp_wrkdir/%preun.run
    done
    test -z "$stoplevels" || for level in $stoplevels; do
        echo "        ln -s /sbin/init.d/$svc /sbin/rc$level.d/K$priority$svc" >>$pp_wrkdir/%post.run
        echo "        rm -f /sbin/rc$level.d/K$priority$svc" >>$pp_wrkdir/%preun.run
    done
}




pp_backend_kit_sizes () {
    awk '
    BEGIN { root = usr = var = 0; }
    {
        if (substr($9, 1, 1) != "l")
            if (substr($10, 1, 6) == "./var/")
                var += $2;
            else if (substr($10, 1, 10) == "./usr/var/")
                var += $2
            else if (substr($10, 1, 6) == "./usr/")
                usr += $2
            else
                root += $2
    }
    END { printf "%d\t%d\t%d", root, usr, var }
    ' "$@"
}

pp_kit_kits_global () {
    line=`sed -n '/^%%/q;/^'$2'=/{s/^'$2'=//p;q;}' <"$1"`
    test -z "$line" && return 1
    eval "echo $line"
    :
}

pp_backend_kit_kits () {
    typeset KITFILE FROMDIR TODIR
    typeset SCPDIR

    SCPDIR="$pp_wrkdir/scps"

    PATH="/usr/lbin:/usr/bin:/etc:/usr/ucb:$PATH"; export PATH # XXX
    #umask 2 # XXX

    test $# -ge 3 || pp_die "pp_backend_kit_kits: too few arguments"
    KITFILE="$1"; shift
    FROMDIR="$1"; shift
    TODIR="$1"; shift

    test -f "$KITFILE" || pp_die "$KITFILE not found"
    test -d "$FROMDIR" || pp_die "$FROMDIR not found"
    test -d "$TODIR"   || pp_die "$TODIR not found"

    INSTCTRL="$TODIR/instctrl"
    mkdir -p "$INSTCTRL" || pp_die "cannot create instctrl directory"
    chmod 775 "$INSTCTRL"

    grep "%%" $KITFILE > /dev/null || pp_die "no %% in $KITFILE"

    typeset NAME CODE VERS MI ROOT COMPRESS
    typeset S_LIST ALLSUBS

    NAME=`pp_kit_kits_global "$KITFILE" NAME` || pp_die "no NAME in $KITFILE"
    CODE=`pp_kit_kits_global "$KITFILE" CODE` || pp_die "no CODE in $KITFILE"
    VERS=`pp_kit_kits_global "$KITFILE" VERS` || pp_die "no VERS in $KITFILE"
    MI=`pp_kit_kits_global "$KITFILE" MI` || pp_die "no MI in $KITFILE"
    ROOT=`pp_kit_kits_global "$KITFILE" ROOT`
    COMPRESS=`pp_kit_kits_global "$KITFILE" COMPRESS`

    test -f "$MI" || pp_die "Inventory file $MI not found"

    case "$ROOT" in
    *ROOT)
        test -f "$TODIR/$ROOT" ||
            pp_die "Root image $ROOT not found in $TODIR" ;;
    esac

    ALLSUBS=`awk 'insub==1 {print $1} /^%%/ {insub=1}' <"$KITFILE"`
    test $# -eq 0 && set -- $ALLSUBS

    pp_debug "Creating $# $NAME subsets."
    pp_debug "ALLSUBS=<$ALLSUBS>"

    if test x"$COMPRESS" = x"1"; then
        COMPRESS=:
    else
        COMPRESS=false
    fi

    #rm -f *.ctrl Volume*

    for SUB
    do
        test -z "$SUB" && pp_die "SUB is empty"

        typeset INV CTRL ROOTSIZE USRSIZE VARSIZE TSSUB
	#rm -f Volume*
	case $SUB in
            .*) :;;
	    *)  pp_verbose rm -f "$TODIR/$SUB"* "$INSTCTRL/$SUB"*;;
        esac

        TSSUB="$pp_wrkdir/ts.$SUB"

	pp_debug "kits: Subset $SUB"

	INV="$SUB.inv"
	CTRL="$SUB.ctrl"
	pp_debug "kits: Generating media creation information..."

        # Invcutter takes as input
        #   SUB dir/path
        # and generates stl_inv(4) files, like this
        #   f 0 00000 0 0 100644 2/11/09 010 f dir/path none SUB
	grep "	$SUB\$" "$MI" |
            pp_verbose /usr/lbin/invcutter \
                -v "$VERS" -f "$FROMDIR" > "$INSTCTRL/$INV" ||
            pp_die "failed to create $INSTCTRL/$INV"
        chmod 664 "$INSTCTRL/$INV"

        pp_backend_kit_sizes "$INSTCTRL/$INV" > "$pp_wrkdir/kit.sizes"
        read ROOTSIZE USRSIZE VARSIZE < "$pp_wrkdir/kit.sizes"

        # Prefix each line with $FROMDIR. This will be stripped
        awk '$1 != "d" {print from $10}' from="$FROMDIR/" \
            > "$TSSUB" < "$INSTCTRL/$INV" ||
            pp_die "failed"

        NVOLS=0

	pp_debug "kits: Creating $SUB control file..."

        sed '1,/^%%/d;/^'"$SUB"'/{p;q;}' < "$KITFILE" > "$pp_wrkdir/kit.line"
        read _SUB _IGNOR DEPS FLAGS DESC < "$pp_wrkdir/kit.line"
        if test -z "$_SUB"; then
            pp_warn "No such subset $SUB in $KITFILE"
            continue
	fi
        DEPS=`echo $DEPS | tr '|' ' '`
        case $FLAGS in
            FLGEXP*) pp_verbose FLAGS='"${'"$FLAGS"'}"' ;;
        esac
        case $DESC in
            *%*) DESC=`echo $DESC|awk -F% '{printf "%-36s%%%s\n", $1, $2}'`;;
        esac

	cat > "$INSTCTRL/$CTRL" <<-.
		NAME='$NAME $SUB'
		DESC=$DESC
		ROOTSIZE=$ROOTSIZE
		USRSIZE=$USRSIZE
		VARSIZE=$VARSIZE
		NVOLS=1:$NVOLS
		MTLOC=1:$TLOC
		DEPS="$DEPS"
		FLAGS=$FLAGS
.
        chmod 664 "$INSTCTRL/$CTRL"

	pp_debug "kits: Making tar image"

	pp_verbose tar cfPR "$TODIR/$SUB" "$FROMDIR/" "$TSSUB" ||
             pp_error "problem creating kit file"

        if $COMPRESS; then
            pp_debug "kits: Compressing"
            (cd "$TODIR" && compress -f -v "$SUB") ||
                pp_die "problem compressing $TODIR/$SUB"
            SPC=`expr $SUB : '\(...\).*'`    # first three characters
            SVC=`expr $SUB : '.*\(...\)'`    # last three characters
            : > "$INSTCTRL/$SPC$SVC.comp"
            chmod 664 "$INSTCTRL/$SPC$SVC.comp"
            pp_debug "kits: Padding compressed file to 10kB" # wtf?
            rm -f "$TODIR/$SUB"
            pp_verbose \
            dd if="$TODIR/$SUB.Z" of="$TODIR/$SUB" bs=10k conv=sync ||
                pp_die "problem moving compressed file"
            rm -f "$TODIR/$SUB.Z"
        fi
        chmod 664 "$TODIR/$SUB"

	if test -f "$SCPDIR/$SUB.scp"; then
		cp "$SCPDIR/$SUB.scp" "$INSTCTRL/$SUB.scp"
                chmod 755 "$INSTCTRL/$SUB.scp"
	else
		pp_debug "kits: null subset control program for $SUB"
		: > "$INSTCTRL/$SUB.scp"
		chmod 744 "$INSTCTRL/$SUB.scp"
	fi

        pp_debug "kits: Finished creating media image for $SUB"
    done

    pp_debug "kits: Creating $CODE.image"

    case "$ROOT" in
    *ROOT)	ALLSUBS="$ROOT $ALLSUBS"
                ;;
    esac

    (cd "$TODIR" && sum $ALLSUBS) > "$INSTCTRL/$CODE.image"
    chmod 664 "$INSTTRL/$CODE.image"
    pp_debug "kits: Creating INSTCTRL"
    (cd "$INSTCTRL" && tar cpvf - *) > "$TODIR/INSTCTRL"
    chmod 664 "$TODIR/INSTCTRL"
    cp "$INSTCTRL/$CODE.image" "$TODIR/$CODE.image"
    chmod 664 "$TODIR/$CODE.image"

    pp_debug "kits: Media image production complete"
}

pp_platforms="$pp_platforms rpm"

pp_backend_rpm_detect () {
    test x"$1" = x"Linux" -a ! -f /etc/debian_version
}

pp_backend_rpm_init () {

    pp_rpm_version=
    pp_rpm_summary=
    pp_rpm_description=
    pp_rpm_group="Applications/Internet"
    pp_rpm_license="Unspecified"
    pp_rpm_vendor=
    pp_rpm_url=
    pp_rpm_packager=
    pp_rpm_provides=
    pp_rpm_requires=
    pp_rpm_release=
    pp_rpm_epoch=
    pp_rpm_dev_group="Development/Libraries"
    pp_rpm_dbg_group="Development/Tools"
    pp_rpm_doc_group="Documentation"
    pp_rpm_dev_description=
    pp_rpm_dbg_description=
    pp_rpm_doc_description=
    pp_rpm_dev_requires=
    pp_rpm_dbg_requires=
    pp_rpm_doc_requires=
    pp_rpm_dev_provides=
    pp_rpm_dbg_provides=
    pp_rpm_doc_provides=

    pp_rpm_dbg_pkgname=debug
    pp_rpm_dev_pkgname=devel
    pp_rpm_doc_pkgname=doc

    pp_rpm_defattr_uid=root
    pp_rpm_defattr_gid=root

    pp_rpm_detect_arch
    pp_rpm_detect_distro
    pp_rpm_rpmbuild=`pp_rpm_detect_rpmbuild`

    # SLES8 doesn't always come with readlink
    test -x /usr/bin/readlink -o -x /bin/readlink ||
        pp_readlink_fn=pp_ls_readlink
}

pp_rpm_detect_arch () {
    pp_rpm_arch=auto

    #-- Find the default native architecture that RPM is configured to use
    cat <<-. >$pp_wrkdir/dummy.spec
	Name: dummy
	Version: 1
	Release: 1
	Summary: dummy
	Group: ${pp_rpm_group}
	License: ${pp_rpm_license}
	%description
	dummy
.
    $pp_opt_debug && cat $pp_wrkdir/dummy.spec
    pp_rpm_arch_local=`rpm -q --qf '%{arch}\n' --specfile $pp_wrkdir/dummy.spec`
    rm $pp_wrkdir/dummy.spec

    #-- Ask the kernel what machine architecture is in use
    local arch
    for arch in "`uname -m`" "`uname -p`"; do
	case "$arch" in
	    i?86)
		pp_rpm_arch_std=i386
		break
		;;
	    x86_64|ppc|ppc64|ia64|s390|s390x)
		pp_rpm_arch_std="$arch"
		break
		;;
	    powerpc)
		# Probably AIX
		case "`/usr/sbin/lsattr -El proc0 -a type -F value`" in
		    PowerPC_POWER*)	pp_rpm_arch_std=ppc64;;
		    *)			pp_rpm_arch_std=ppc;;
		esac
		break
		;;
	    *)	pp_rpm_arch_std=unknown
		;;
	esac
    done

    #-- Later on, when files are processed, we use 'file' to determine
    #   what platform ABIs are used. This is used when pp_rpm_arch == auto
    pp_rpm_arch_seen=
}

pp_rpm_detect_distro () {
    pp_rpm_distro=
    if test -f /etc/whitebox-release; then
       pp_rpm_distro=`awk '
          /^White Box Enterprise Linux release/ { print "wbel" $6; exit; }
       ' /etc/whitebox-release`
    elif test -f /etc/mandrakelinux-release; then
       pp_rpm_distro=`awk '
          /^Mandrakelinux release/ { print "mand" $3; exit; }
       ' /etc/mandrake-release`
    elif test -f /etc/mandrake-release; then
       pp_rpm_distro=`awk '
          /^Linux Mandrake release/ { print "mand" $4; exit; }
          /^Mandrake Linux release/ { print "mand" $4; exit; }
       ' /etc/mandrake-release`
    elif test -f /etc/fedora-release; then
       pp_rpm_distro=`awk '
          /^Fedora Core release/ { print "fc" $4; exit; }
          /^Fedora release/ { print "f" $3; exit; }
       ' /etc/fedora-release`
    elif test -f /etc/redhat-release; then
       pp_rpm_distro=`awk '
          /^Red Hat Enterprise Linux/ { print "rhel" $7; exit; }
          /^CentOS release/           { print "centos" $3; exit; }
          /^CentOS Linux release/     { print "centos" $4; exit; }
          /^Red Hat Linux release/    { print "rh" $5; exit; }
       ' /etc/redhat-release`
    elif test -f /etc/SuSE-release; then
       pp_rpm_distro=`awk '
          /^SuSE Linux [0-9]/ { print "suse" $3; exit; }
          /^SUSE LINUX [0-9]/ { print "suse" $3; exit; }
          /^openSUSE [0-9]/   { print "suse" $2; exit; }
          /^S[uU]SE Linux Enterprise Server [0-9]/ { print "sles" $5; exit; }
          /^S[uU]SE LINUX Enterprise Server [0-9]/ { print "sles" $5; exit; }
          /^SuSE SLES-[0-9]/  { print "sles" substr($2,6); exit; }
       ' /etc/SuSE-release`
    elif test -f /etc/pld-release; then
       pp_rpm_distro=`awk '
          /^[^ ]* PLD Linux/ { print "pld" $1; exit; }
       ' /etc/pld-release`
    elif test X"`uname -s 2>/dev/null`" = X"AIX"; then
	local r v
	r=`uname -r`
	v=`uname -v`
	pp_rpm_distro="aix$v$r"
    fi
    pp_rpm_distro=`echo $pp_rpm_distro | tr -d .`
    test -z "$pp_rpm_distro" &&
       pp_warn "unknown distro"
}

pp_rpm_detect_rpmbuild () {
    local cmd
    for cmd in rpmbuild rpm; do
        if `which $cmd > /dev/null 2>&1`; then
            echo $cmd
            return 0
        fi
    done

    pp_error "Could not find rpmbuild"
    # Default to `rpmbuild` in case it magically appears
    echo rpmbuild
    return 1
}

pp_rpm_label () {
    local label arg
    label="$1"; shift
    for arg
    do
        test -z "$arg" || echo "$label: $arg"
    done
}

pp_rpm_writefiles () {
    local _l t m o g f p st fo farch
    while read t m o g f p st; do
        _l="$p"
	test $t = d && _l="%dir ${_l%/}/"
        if test x"$m" = x"-"; then
            case "$t" in
                d) m=755;;
                f) m=644;;
            esac
        fi
        test x"$o" = x"-" && o="${pp_rpm_defattr_uid:-root}"
        test x"$g" = x"-" && g="${pp_rpm_defattr_gid:-root}"
	_l="%attr($m,$o,$g) $_l"

	if test "$t" = "f" -a x"$pp_rpm_arch" = x"auto"; then
	    fo=`file "${pp_destdir}$p" 2>/dev/null`
	    #NB: The following should match executables and shared objects,
	    #relocatable objects. It will not match .a files however.
	    case "$fo" in
		*": ELF 32-bit LSB "*", Intel 80386"*)
		    farch=i386;;
		*": ELF 64-bit LSB "*", AMD x86-64"*|\
		*": ELF 64-bit LSB "*", x86-64"*)
		    farch=x86_64;;
		*": ELF 32-bit MSB "*", PowerPC"*)
		    farch=ppc;;
		*": ELF 64-bit MSB "*", 64-bit PowerPC"*)
		    farch=ppc64;;
		*": ELF 64-bit LSB "*", IA-64"*)
		    farch=ia64;;
		*": ELF 32-bit MSB "*", IBM S/390"*)
		    farch=s390;;
		*": ELF 64-bit MSB "*", IBM S/390"*)
		    farch=s390x;;
		*"executable (RISC System/6000)"*)
		    farch=ppc;;
		*"64-bit XCOFF executable"*)
		    farch=ppc64;;
		*" ELF "*)
		    farch=ELF;;
		*)
		    farch=noarch;;
	    esac
	    # If file(1) doesn't provide enough info, try readelf(1)
	    if test "$farch" = "ELF"; then
		fo=`readelf -h "${pp_destdir}$p" | awk '{if ($1 == "Class:") {class=$2} else if ($1 == "Machine:") {machine=$0; sub(/^ *Machine: */, "", machine)}} END {print class " " machine}' 2>/dev/null`
		case "$fo" in
		    "ELF32 Intel 80386")
			farch=i386;;
		    "ELF64 "*[xX]"86-64")
			farch=x86_64;;
		    "ELF32 PowerPC")
			farch=ppc;;
		    "ELF64 PowerPC"*)
			farch=ppc64;;
		    "ELF64 IA-64")
			farch=ia64;;
		    "ELF32 IBM S/390")
			farch=s390;;
		    "ELF64 IBM S/390")
			farch=s390x;;
		    *)
			farch=noarch;;
		esac
	    fi
	    pp_debug "file: $fo -> $farch"
	    test x"$farch" = x"noarch" || pp_add_to_list pp_rpm_arch_seen $farch
	fi

	case $f in *v*) _l="%config(noreplace) $_l";; esac
	echo "$_l"
    done
    echo
}

pp_rpm_subname () {
    case "$1" in
        run) : ;;
        dbg) echo "${2}${pp_rpm_dbg_pkgname}";;
        dev) echo "${2}${pp_rpm_dev_pkgname}";;
        doc) echo "${2}${pp_rpm_doc_pkgname}";;
        *)   pp_error "unknown component '$1'";
    esac
}

pp_rpm_depend () {
    local _name _vers
    while read _name _vers; do
        case "$_name" in ""| "#"*) continue ;; esac
        echo "Requires: $_name ${_vers:+>= $_vers}"
    done
}

pp_rpm_conflict () {
    local _name _vers
    while read _name _vers; do
        case "$_name" in ""| "#"*) continue ;; esac
        echo "Conflicts: $_name ${_vers:+>= $_vers}"
    done
}

pp_rpm_override_requires () {
    local orig_find_requires

    if test -z "$pp_rpm_depend_filter_cmd"; then
	return 0
    fi

    orig_find_requires=`rpm --eval '%{__find_requires}'`
    cat << EOF > "$pp_wrkdir/filtered-find-requires"
$orig_find_requires \$@ | $pp_rpm_depend_filter_cmd
EOF
    chmod +x "$pp_wrkdir/filtered-find-requires"
    echo "%define __find_requires $pp_wrkdir/filtered-find-requires"
    # Might be necessary for old versions of RPM? Not for 4.4.2.
    #echo "%define _use_internal_dependency_generator 0"
}

pp_backend_rpm () {
    local cmp specfile _summary _group _desc _pkg _subname svc

	specfile=$pp_wrkdir/$name.spec
        : > $specfile

        #-- force existence of a 'run' component
        pp_add_component run
        : >> $pp_wrkdir/%files.run

	if test -z "$pp_rpm_arch"; then
            pp_error "Unknown RPM architecture"
            return 1
        fi

	#-- Write the header components of the RPM spec file
	cat <<-. >>$specfile
		Name: ${pp_rpm_name:-$name}
		Version: ${pp_rpm_version:-$version}
		Release: ${pp_rpm_release:-1}
		Summary: ${pp_rpm_summary:-$summary}
		Group:   ${pp_rpm_group}
		License: ${pp_rpm_license}
.
	if test -n "$pp_rpm_url"; then
	    pp_rpm_label "URL"  "$pp_rpm_url"               >>$specfile
	fi
	pp_rpm_label "Vendor"   "${pp_rpm_vendor:-$vendor}" >>$specfile
	pp_rpm_label "Packager" "$pp_rpm_packager"          >>$specfile
	pp_rpm_label "Provides" "$pp_rpm_provides"          >>$specfile
	pp_rpm_label "Requires" "$pp_rpm_requires"          >>$specfile

	test -n "$pp_rpm_serial" && pp_warn "pp_rpm_serial deprecated"
	if test -n "$pp_rpm_epoch"; then
	    #-- Epoch was introduced in RPM 2.5.6
	    case `$pp_rpm_rpmbuild --version 2>/dev/null` in
		1.*|2.[0-5].*|2.5.[0-5])
		    pp_rpm_label "Serial" $pp_rpm_epoch >>$specfile;;
		*)
		    pp_rpm_label "Epoch" $pp_rpm_epoch >>$specfile;;
	    esac
	fi

        if test -n "$pp_rpm_requires"; then
            pp_rpm_label "Requires" "$pp_rpm_requires" >>$specfile
        elif test -s $pp_wrkdir/%depend.run; then
            pp_rpm_depend < $pp_wrkdir/%depend.run >> $specfile
        fi
        if test -s $pp_wrkdir/%conflict.run; then
            pp_rpm_conflict < $pp_wrkdir/%conflict.run >> $specfile
        fi

	pp_rpm_override_requires >> $specfile

	cat <<-. >>$specfile

		%description
		${pp_rpm_description:-$description}
.

	for cmp in $pp_components; do
		case $cmp in
		   run) continue;;
		   dev) _summary="development tools for $pp_rpm_summary"
		   	_group="$pp_rpm_dev_group"
			_desc="${pp_rpm_dev_description:-Development libraries for $name. $pp_rpm_description.}"
		   	;;
		   doc) _summary="documentation for $pp_rpm_summary"
		   	_group="$pp_rpm_doc_group"
			_desc="${pp_rpm_doc_description:-Documentation for $name. $pp_rpm_description.}"
		   	;;
		   dbg) _summary="diagnostic tools for $pp_rpm_summary"
		   	_group="$pp_rpm_dbg_group"
			_desc="${pp_rpm_dbg_description:-Diagnostic tools for $name.}"
		   	;;
		esac

                _subname=`pp_rpm_subname $cmp`
		cat <<-.

			%package $_subname
			Summary: $name $_summary
			Group: $_group
.
                eval '_pkg="$pp_rpm_'$cmp'_requires"'
                if test -n "$_pkg"; then
                    eval pp_rpm_label Requires ${pp_rpm_name:-$name} $_pkg
                elif test -s $pp_wrkdir/%depend.$cmp; then
                    pp_rpm_depend < $pp_wrkdir/%depend.$cmp >> $specfile
                fi
                if test -s $pp_wrkdir/%conflict.$cmp; then
                    pp_rpm_conflict < $pp_wrkdir/%conflict.$cmp >> $specfile
                fi

                eval '_pkg="$pp_rpm_'$cmp'_provides"'
		eval pp_rpm_label Provides $_pkg

		cat <<-.

			%description $_subname
			$_desc
.
	done >>$specfile

        #-- NB: we don't put any %prep, %build or %install RPM sections
	#   into the spec file.

        #-- add service start/stop code
        if test -n "$pp_services"; then
            pp_rpm_service_install_common >> $pp_wrkdir/%post.run

            #-- record the uninstall commands in reverse order
            for svc in $pp_services; do
                pp_load_service_vars $svc

                pp_rpm_service_make_init_script $svc

                #-- append %post code to install the svc
                pp_rpm_service_install $svc >> $pp_wrkdir/%post.run

                #-- prepend %preun code to uninstall svc
                # (use files in case vars are modified)
                pp_rpm_service_remove $svc | pp_prepend $pp_wrkdir/%preun.run
            done
            pp_rpm_service_remove_common | pp_prepend $pp_wrkdir/%preun.run
        fi

	# make convenience service groups
        if test -n "$pp_service_groups"; then
	    for grp in $pp_service_groups; do
		pp_rpm_service_group_make_init_script \
		    $grp "`pp_service_get_svc_group $grp`"
	    done
	fi

	#-- Write the RPM %file sections
        #   (do this after services, since services adds to %files.run)
	for cmp in $pp_components; do
            _subname=`pp_rpm_subname $cmp`

            if test -s $pp_wrkdir/%check.$cmp; then
                echo ""
                echo "%pre $_subname"
                cat $pp_wrkdir/%check.$cmp
                echo :   # causes script to exit true by default
            fi

            if test -s $pp_wrkdir/%files.$cmp; then
                echo ""
                echo "%files $_subname"
                pp_rpm_writefiles < $pp_wrkdir/%files.$cmp
            fi

            if test -n "$pp_rpm_ghost"; then
                for ghost in $pp_rpm_ghost; do
                    echo "%ghost $ghost"
                done
            fi

            if test -s $pp_wrkdir/%pre.$cmp; then
                echo ""
                echo "%pre $_subname"
                cat $pp_wrkdir/%pre.$cmp
                echo :   # causes script to exit true
            fi

            if test -s $pp_wrkdir/%post.$cmp; then
                echo ""
                echo "%post $_subname"
                cat $pp_wrkdir/%post.$cmp
                echo :   # causes script to exit true
            fi

            if test -s $pp_wrkdir/%preun.$cmp; then
                echo ""
                echo "%preun $_subname"
                cat $pp_wrkdir/%preun.$cmp
                echo :   # causes script to exit true
            fi

            if test -s $pp_wrkdir/%postun.$cmp; then
                echo ""
                echo "%postun $_subname"
                cat $pp_wrkdir/%postun.$cmp
                echo :   # causes script to exit true
            fi
	done >>$specfile

        #-- create a suitable work area for rpmbuild
	cat <<-. >$pp_wrkdir/.rpmmacros
		%_topdir $pp_wrkdir
		# XXX Note escaped %% for use in headerSprintf
		%_rpmfilename   %%{ARCH}/%%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm
	.
	mkdir $pp_wrkdir/RPMS
	mkdir $pp_wrkdir/BUILD

	if test x"$pp_rpm_arch" = x"auto"; then
	    #-- Reduce the arch_seen list to exactly one item
	    case "$pp_rpm_arch_seen" in
		"i386 x86_64"|"x86_64 i386")
		    pp_rpm_arch_seen=x86_64;;
		*"s390 s390x"* | *"s390x s390"* )
		    pp_rpm_arch_seen=s390x;;
		*" "*)
		    pp_error "detected multiple targets: $pp_rpm_arch_seen"
		    pp_rpm_arch_seen=unknown;;	    # not detected
		"")
		    pp_warn "detected no binaries: using target noarch"
		    pp_rpm_arch_seen=noarch;;
		*)
		    pp_debug "detected architecture $pp_rpm_arch_seen"
	    esac
	    pp_rpm_arch="$pp_rpm_arch_seen"
	fi

        . $pp_wrkdir/%fixup

$pp_opt_debug && cat $specfile

        pp_debug "creating: `pp_backend_rpm_names`"

pp_debug "pp_rpm_arch_seen = <${pp_rpm_arch_seen}>"
pp_debug "pp_rpm_arch = <${pp_rpm_arch}>"

	HOME=$pp_wrkdir \
	pp_verbose \
        $pp_rpm_rpmbuild -bb \
		--buildroot="$pp_destdir/" \
                --target="${pp_rpm_arch}" \
                --define='_unpackaged_files_terminate_build 0' \
                --define='_use_internal_dependency_generator 0' \
                `$pp_opt_debug && echo --verbose || echo --quiet` \
                $pp_rpm_rpmbuild_extra_flags \
		$specfile ||
            pp_error "Problem creating RPM packages"

	for f in `pp_backend_rpm_names`; do
	    # The package might be in an arch-specific subdir
	    pkgfile=not-found
	    for dir in $pp_wrkdir/RPMS/${pp_rpm_arch} $pp_wrkdir/RPMS; do
		if test -f $dir/$f; then
		    pkgfile=$dir/$f
		fi
	    done
	    if test x"$pkgfile" = x"not-found"; then
                pp_error "Problem predicting RPM filename: $f"
	    else
		ln $pkgfile $pp_wrkdir/$f
	    fi
	done
}

pp_rpm_output_name () {
    echo "${pp_rpm_name:-$name}`pp_rpm_subname "$1" -`-${pp_rpm_version:-$version}-${pp_rpm_release:-1}.${pp_rpm_arch}.rpm"
}

pp_backend_rpm_names () {
    local cmp _subname
    for cmp in $pp_components; do
	pp_rpm_output_name $cmp
    done
}

pp_backend_rpm_cleanup () {
    :
}

pp_rpm_print_requires () {
    local _subname _name

    echo "CPU:$pp_rpm_arch"
    ## XXX should be lines of the form (from file/ldd/objdump)
    #    EXEC:/bin/sh
    #    RTLD:libc.so.4:open
    rpm -q --requires -p $pp_wrkdir/`pp_rpm_output_name $1` |sed -e '/^rpmlib(/d;s/ //g;s/^/RPM:/' | sort -u
}

pp_backend_rpm_install_script () {
    local cmp _subname

    echo "#!/bin/sh"
    pp_install_script_common

    cat <<.

        cmp_to_pkgname () {
	    local oi name
	    if test x"\$1" = x"--only-installed"; then
		#-- only print if installation detected
		oi=false
		shift
	    else
		oi=true
	    fi
            test x"\$*" = x"all" &&
                set -- $pp_components
            for cmp
            do
                case \$cmp in
.
    for cmp in $pp_components; do
        _subname=`pp_rpm_subname $cmp -`
         echo "$cmp) name=${pp_rpm_name:-$name}${_subname};;"
    done
    cat <<.
                *) usage;;
                esac
		if \$oi || rpm -q "\$name" >/dev/null 2>/dev/null; then
		    echo "\$name"
		fi
            done
        }


        cmp_to_pathname () {
            test x"\$*" = x"all" &&
                set -- $pp_components
            for cmp
            do
                case \$cmp in
.
    for cmp in $pp_components; do
        echo "$cmp) echo \${PP_PKGDESTDIR:-.}/`pp_rpm_output_name $cmp` ;;"
    done
    cat <<.
                *) usage;;
                esac
            done
        }

	print_requires () {
            test x"\$*" = x"all" &&
                set -- $pp_components
            for cmp
            do
                case \$cmp in
.
    for cmp in $pp_components; do
        echo "$cmp) cat <<'._end'"
	pp_rpm_print_requires $cmp
        echo "._end"; echo ';;'
    done
    cat <<.
                *) usage;;
                esac
            done
        }

        test \$# -eq 0 && usage
        op="\$1"; shift
        case "\$op" in
            list-components)
                test \$# -eq 0 || usage \$op
                echo $pp_components
                ;;
            list-services)
                test \$# -eq 0 || usage \$op
                echo $pp_services
                ;;
            list-files)
                test \$# -ge 1 || usage \$op
                cmp_to_pathname "\$@"
                ;;
            install)
                test \$# -ge 1 || usage \$op
                verbose rpm -U --replacepkgs --oldpackage \
                    \`cmp_to_pathname "\$@"\`
                ;;
            uninstall)
                test \$# -ge 1 || usage \$op
                pkgs=\`cmp_to_pkgname --only-installed "\$@"\`
                if test -z "\$pkgs"; then
                    verbosemsg "nothing to uninstall"
                else
                    verbose rpm -e \$pkgs
                fi
                ;;
            start|stop)
                test \$# -ge 1 || usage \$op
                ec=0
                for svc
                do
                    verbose /etc/init.d/\$svc \$op || ec=1
                done
                exit \$ec
                ;;
            print-platform)
                test \$# -eq 0 || usage \$op
		echo "linux-${pp_rpm_arch}"
		;;
            print-requires)
                test \$# -ge 1 || usage \$op
                print_requires "\$@"
		;;
            *)
                usage
                ;;
        esac
.

}

pp_backend_rpm_probe () {
        echo "${pp_rpm_distro}-${pp_rpm_arch_std}"
}

pp_backend_rpm_vas_platforms () {
    case "$pp_rpm_arch_std" in
	x86_64)	echo "linux-x86_64.rpm linux-x86.rpm";;
	*86)	echo "linux-x86.rpm";;
	s390)	echo "linux-s390";;
	s390x)	echo "linux-s390x";;
	ppc*)	echo "linux-glibc23-ppc64 linux-glibc22-ppc64";;
	ia64)	echo "linux-ia64";;
	*)	pp_die "unknown architecture $pp_rpm_arch_std";;
    esac
}

pp_rpm_service_install_common () {
    cat <<-'.'

        _pp_install_service () {
            local svc level
            svc="$1"
            if [ -x /usr/lib/lsb/install_initd -a ! -r /etc/redhat-release ]
            then
                # LSB-style install
                /usr/lib/lsb/install_initd /etc/init.d/$svc &> /dev/null
            elif [ -x /sbin/chkconfig ]; then
                # Red Hat/chkconfig-style install
                /sbin/chkconfig --add $svc &> /dev/null
                /sbin/chkconfig $svc off &> /dev/null
            else
		: # manual links under /etc/init.d
            fi
        }

        _pp_enable_service () {
            local svc level
            svc="$1"
            if [ -x /usr/lib/lsb/install_initd -a ! -r /etc/redhat-release ]
            then
                # LSB-style install
		: # not sure how to enable
            elif [ -x /sbin/chkconfig ]; then
                # Red Hat/chkconfig-style install
                /sbin/chkconfig $svc on &> /dev/null
            else
                # manual install
                set -- `sed -n -e 's/^# Default-Start://p' /etc/init.d/$svc`
                start_priority=`sed -n -e 's/^# X-Quest-Start-Priority:[[:space:]]*//p' /etc/init.d/$svc`
                stop_priority=`sed -n -e 's/^# X-Quest-Stop-Priority:[[:space:]]*//p' /etc/init.d/$svc`

                # Provide default start & stop priorities of 20 & 80 in
                # accordance with Debian update-rc.d defaults
                if [ -z "$start_priority" ]; then
                    start_priority=20
                fi
                if [ -z "$stop_priority" ]; then
                    stop_priority=80
                fi
                    
                if [ -d "/etc/rc.d" ];then
                    rcdir=/etc/rc.d
                else
                    rcdir=/etc
                fi

                for level
                do ln -sf /etc/init.d/$svc $rcdir/rc$level.d/S$start_priority$svc; done
                set -- `sed -n -e 's/^# Default-Stop://p' /etc/init.d/$svc`
                for level
                do ln -sf /etc/init.d/$svc $rcdir/rc$level.d/K$stop_priority$svc; done
            fi
        }
.
}

pp_rpm_service_remove_common () {
    cat <<-'.'

        _pp_remove_service () {
            local svc
            svc="$1"
            /etc/init.d/$svc stop >/dev/null 2>&1
            if [ -x /usr/lib/lsb/remove_initd -a ! -r /etc/redhat-release ]
            then
                /usr/lib/lsb/remove_initd /etc/init.d/$svc &> /dev/null
            elif [ -x /sbin/chkconfig ]; then
                /sbin/chkconfig --del $svc &> /dev/null
            else
                if [ -d "/etc/rc.d" ];then
                    rcdir=/etc/rc.d
                else
                    rcdir=/etc
                fi

                rm -f $rcdir/rc?.d/[SK]??$svc
            fi
        }
.
}


pp_rpm_service_install () {
    pp_rpm_service_make_init_script $1 >/dev/null ||
        pp_error "could not create init script for service $1"
    echo "_pp_install_service $1"
    test $enable = yes && echo "_pp_enable_service $1"
}

pp_rpm_service_remove () {
    cat <<-.
        if [ "\$1" = "remove" -o "\$1" = "0" ]; then
            # only remove the service if not upgrade
            _pp_remove_service $1
        fi
.
}


pp_backend_rpm_init_svc_vars () {

    reload_signal=
    start_runlevels=${pp_rpm_default_start_runlevels-"2 3 4 5"} # == lsb default-start
    stop_runlevels=${pp_rpm_default_stop_runlevels-"0 1 6"} # == lsb default-stop
    svc_description="${pp_rpm_default_svc_description}" # == lsb short descr
    svc_process=

    lsb_required_start='$local_fs $network'
    lsb_should_start=
    lsb_required_stop=
    lsb_description=

    start_priority=50
    stop_priority=50            #-- stop_priority = 100 - start_priority
}

pp_rpm_service_group_make_init_script () {
    local grp=$1
    local svcs="$2"
    local script=/etc/init.d/$grp
    local out=$pp_destdir$script

    pp_add_file_if_missing $script run 755 || return 0

    cat <<-. >>$out
	#!/bin/sh
	svcs="$svcs"
.

    cat <<-'.' >>$out

        #-- prints usage message
        pp_usage () {
            echo "usage: $0 {start|stop|status|restart|reload|condrestart|try-restart|force-reload}" >&2
            return 2
        }

        #-- starts services in order.. stops them all if any break
        pp_start () {
            undo=
            for svc in $svcs; do
                if /etc/init.d/$svc start; then
                    undo="$svc $undo"
                else
                    if test -n "$undo"; then
                        for svc in $undo; do
                           /etc/init.d/$svc stop
                        done
                        return 1
                    fi
                fi
            done
            return 0
        }

        #-- stops services in reverse
        pp_stop () {
            reverse=
            for svc in $svcs; do
                reverse="$svc $reverse"
            done
            rc=0
            for svc in $reverse; do
                /etc/init.d/$svc stop || rc=$?
            done
            return $rc
        }

        #-- returns true only if all services return true status
        pp_status () {
            rc=0
            for svc in $svcs; do
                /etc/init.d/$svc status || rc=$?
            done
            return $rc
        }

        pp_reload () {
            rc=0
            for svc in $svcs; do
                /etc/init.d/$svc reload || rc=$?
            done
            return $rc
        }

        case "$1" in
            start)          pp_start;;
            stop)           pp_stop;;
            restart)        pp_stop; pp_start;;
            status)         pp_status;;
            try-restart|condrestart)
                            if pp_status >/dev/null; then
                                    pp_restart
                            fi;;
            reload)         pp_reload;;
            force-reload)   if pp_status >/dev/null; then
                                    pp_reload
                            else
                                    pp_restart
                            fi;;
            *)              pp_usage;;
        esac
.
    chmod 755 $out
}

pp_rpm_service_make_init_script () {
    local svc=$1
    local script=/etc/init.d/$svc
    local out=$pp_destdir$script
    local _process _cmd _rpmlevels

    pp_add_file_if_missing $script run 755 || return 0

    #-- start out as an empty shell script
    cat <<-'.' >$out
	#!/bin/sh
.

    #-- determine the process name from $cmd unless $svc_process is given
    set -- $cmd
    _process=${svc_process:-"$1"}

    #-- construct a start command that builds a pid file if needed
    _cmd="$cmd";
    if test -z "$pidfile"; then
        pidfile=/var/run/$svc.pid
        _cmd="$cmd & echo \$! > \$pidfile"
    fi
    if test "$user" != "root"; then
        _cmd="su $user -c exec $_cmd";
    fi

    #-- generate the Red Hat chkconfig headers
    _rpmlevels=`echo $start_runlevels | tr -d ' '`
    cat <<-. >>$out
	# chkconfig: ${_rpmlevels:--} ${start_priority:-50} ${stop_priority:-50}
	# description: ${svc_description:-no description}
	# processname: ${_process}
	# pidfile: ${pidfile}
.

    #-- generate the LSB init info
    cat <<-. >>$out
	### BEGIN INIT INFO
	# Provides: ${svc}
	# Required-Start: ${lsb_required_start}
	# Should-Start: ${lsb_should_start}
	# Required-Stop: ${lsb_required_stop}
	# Default-Start: ${start_runlevels}
	# Default-Stop: ${stop_runlevels}
	# Short-Description: ${svc_description}
	### END INIT INFO
	# Generated by PolyPackage ${pp_version}
	# ${copyright}

.

    if test x"${svc_description}" = x"${pp_rpm_default_svc_description}"; then
        svc_description=
    fi

    #-- write service-specific definitions
    cat <<. >>$out
	#-- definitions specific to service ${svc}
	svc_name="${svc_description:-$svc service}"
	user="${user}"
	pidfile="${pidfile}"
	stop_signal="${stop_signal}"
	reload_signal="${reload_signal}"
	pp_exec_cmd () { $_cmd; }
.

    #-- write the generic part of the init script
    cat <<'.' >>$out

        #-- use system message logging, if available
        if [ -f /lib/lsb/init-functions -a ! -r /etc/redhat-release ]; then
            . /lib/lsb/init-functions
            pp_success_msg () { log_success_msg "$@"; }
            pp_failure_msg () { log_failure_msg "$@"; }
            pp_warning_msg () { log_warning_msg "$@"; }
        elif [ -f /etc/init.d/functions ]; then
            . /etc/init.d/functions
            pp_success_msg () { echo -n "$*"; success "$@"; echo; }
            pp_failure_msg () { echo -n "$*"; failure "$@"; echo; }
            pp_warning_msg () { echo -n "$*"; warning "$@"; echo; }
        else
            pp_success_msg () { echo ${1:+"$*:"} OK; }
            pp_failure_msg () { echo ${1:+"$*:"} FAIL; }
            pp_warning_msg () { echo ${1:+"$*:"} WARNING; }
        fi

        #-- prints a status message
        pp_msg () { echo -n "$*: "; }

        #-- prints usage message
        pp_usage () {
            echo "usage: $0 {start|stop|status|restart|reload|condrestart|try-restart|force-reload}" >&2
            return 2
        }

        #-- reloads the service, if possible
        #   returns 0=success 1=failure 3=unimplemented
        pp_reload () {
            test -n "$reload_signal" || return 3 # unimplemented
            pp_msg "Reloading ${svc_name}"
            if pp_signal -${reload_signal}; then
                pp_success_msg
                return 0
            else
                pp_failure_msg "not running"
                return 1
            fi
        }

        #-- delivers signal $1 to the pidfile
        #   returns 0=success 1=failure
        pp_signal () {
            if test -r "$pidfile"; then
                read pid < $pidfile
                kill "$@" "$pid" 2>/dev/null
            else
                return 1
            fi
        }

        #-- prints information about the service status
        #   returns 0=running 1=crashed 3=stopped
        pp_status () {
            pp_msg "Checking for ${svc_name}"
            if pp_signal -0; then
                pp_success_msg "running"
                return 0
            elif test -r "$pidfile"; then
                pp_failure_msg "not running (crashed)"
                return 1
            else
                pp_failure_msg "not running"
                return 3
            fi
        }

        #-- starts the service
        #   returns 0=success 1=failure
        pp_start () {
            pp_msg "Starting ${svc_name}"
            if pp_status >/dev/null; then
                pp_warning_msg "already started"
                return 0
            elif pp_exec_cmd; then
                pp_success_msg
                return 0
            else
                pp_failure_msg "cannot start"
                return 1
            fi
        }

        #-- stops the service
        #   returns 0=success (always)
        pp_stop () {
            pp_msg "Stopping ${svc_name}"
            if pp_signal -${stop_signal}; then
                pp_success_msg
            else
                pp_success_msg "already stopped"
            fi
            rm -f "$pidfile"
            return 0
        }

        #-- stops and starts the service
        pp_restart () {
            pp_stop
            pp_start
        }

        case "$1" in
            start)          pp_start;;
            stop)           pp_stop;;
            restart)        pp_restart;;
            status)         pp_status;;
            try-restart|condrestart)
                            if pp_status >/dev/null; then
                                    pp_restart
                            fi;;
            reload)         pp_reload;;
            force-reload)   if pp_status >/dev/null; then
                                    pp_reload
                            else
                                    pp_restart
                            fi;;
            *)              pp_usage;;
        esac

.
    chmod 755 $out
}
pp_backend_rpm_function () {
    case "$1" in
        pp_mkgroup) cat<<'.';;
            /usr/sbin/groupadd -f -r "$1"
.
        pp_mkuser:depends) echo pp_mkgroup;;
        pp_mkuser) cat<<'.';;
            pp_mkgroup "${2:-$1}" || return 1
            /usr/sbin/useradd \
		-g "${2:-$1}" \
		-M -d "${3:-/nonexistent}" \
		-s "${4:-/bin/false}" \
		-r "$1"
.
        pp_havelib) cat<<'.';;
            for pp_tmp_dir in `echo "/usr/lib:/lib${3:+:$3}" | tr : ' '`; do
                test -r "$pp_tmp_dir/lib$1.so{$2:+.$2}" && return 0
            done
            return 1
.
	*) false;;
    esac
}

: NOTES <<.

 # creating a dmg file for publishing on the web
    hdiutil create -srcfolder /path/foo foo.dmg
    hdiutil internet-enable -yes /path/foo.dmg
 # Layout for packages
    <name>-<cpy>/component/<file>
    <name>-<cpt>/extras/postinstall
    <name>-<cpt>/extras/postupgrade
 # /Developer/usr/bin/packagemaker (man packagemaker)

    Make a bunch of packages, and then build a 'distribution'
    which is only understood by macos>10.4

 # Message files in the resource path used are
    Welcome.{rtf,html,rtfd,txt} - limited text shown in Intro
    ReadMe.{rtf,html,rtfd,txt} - scrollable/printable, after Intro
    License.{rtf,html,rtfd,txt} - ditto, user must click 'Accept'
    background.{jpg,tif,gif,pict,eps,pdf} 620x418 background image

 # These scripts looked for in the resource path
    InstallationCheck $pkgpath $defaultloc $targetvol
	0:ok 32:warn 32+x:warn[1] 64:stop 96+x:stop[2]
    VolumeCheck $volpath
	0:ok 32:failure 32+x:failure[3]
    preflight   $pkgpath $targetloc $targetvol    [priv]
    preinstall  $pkgpath $targetloc $targetvol    [priv]
    preupgrade  $pkgpath $targetloc $targetvol    [priv]
    postinstall $pkgpath $targetloc $targetvol    [priv]
    postupgrade $pkgpath $targetloc $targetvol    [priv]
    postflight  $pkgpath $targetloc $targetvol    [priv]
	0:ok else fail (for all scripts)

    A detailed reason is deduced by finding an index x (16..31)
    in the file InstallationCheck.strings or VolumeCheck.strings.

    Scripts marked [priv] are executed with root privileges.
    None of the [priv] scripts are used by metapackages.

 # Default permissions
    Permissions of existing directories should match those
    of a clean install of the OS; typically root:admin 0775
    New directories or files should be 0775 or 0664 with the
    appropriate user:group.
    Exceptions:
	/etc	root:admin 0755
	/var    root:admin 0755

    <http://developer.apple.com/documentation/DeveloperTools/Conceptual/SoftwareDistribution4/Concepts/sd_pkg_flags.html>
    Info.plist = {
     CFBundleGetInfoString: "1.2.3, Quest Software, Inc.",
     CFBundleIdentifier: "com.quest.rc.openssh",
     CFBundleShortVersionString: "1.2.3",
     IFMajorVersion: 1,
     IFMinorVersion: 2,
     IFPkgFlagAllowBackRev: false,
     IFPkgFlagAuthorizationAction: "AdminAuthorization",
     IFPkgFlagDefaultLocation: "/",
     IFPkgFlagFollowLinks: true,
     IFPkgFlagInstallFat: false,
     IFPkgFlagInstalledSize: <integer>,	    # this is added by packagemaker
     IFPkgFlagIsRequired: false,
     IFPkgFlagOverwritePermissions: false,
     IFPkgFlagRelocatable: false,
     IFPkgFlagRestartAction: "NoRestart",
     IFPkgFlagRootVolumeOnly: false,
     IFPkgFlagUpdateInstalledLanguages: false,
     IFPkgFormatVersion= 0.10000000149011612,
     IFRequirementDicts: [ {
       Level = "requires",
       SpecArgument = "/opt/quest/lib/libvas.4.2.0.dylib",
       SpecType = "file",
       TestObject = true,
       TestOperator = "eq", } ]
    }

    Description.plist = {
     IFPkgDescriptionDescription = "this is the description text",
     IFPkgDescriptionTitle = "quest-openssh"
    }

 # Startup scripts
    'launchd' is a kind of combined inetd and rc/init.d system.
    <http://developer.apple.com/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/DesigningDaemons.html>
    Create a /Library/LaunchDaemons/$daemonname.plist file
    Examples found in /System/Library/LaunchDaemons/
    See manual page launchd.plist(5) for details:

    { Label: "com.quest.rc.foo",                        # required
      Program: "/sbin/program",
      ProgramArguments: [ "/sbin/program", "arg1", "arg2" ], # required
      RunAtLoad: true,
      WatchPaths: [ "/etc/crontab" ],
      QueueDirectories: [ "/var/cron/tabs" ],
      inetdCompatibility: { Wait: false },                   # inetd-only
      OnDemand: false,                                       # recommended
      SessionCreate: true,
      UserName: "nobody",
      InitGroups: true,
      Sockets: {                                             # inetd only
	Listeners: {
	   SockServiceName: "ssh",
	   Bonjour: ["ssh", "sftp-ssh"], } },
      Disabled: false,
      StandardErrorPath: "/dev/null",
    }


    How to add a new user
	dscl . -create /Users/$user
	dscl . -create /Users/$user UserShell /bin/bash
	dscl . -create /Users/$user RealName "$user"
	dscl . -create /Users/$user UniqueID $uid
	dscl . -create /Users/$user PrimaryGroupID $gid
	dscl . -create /Users/$user NFSHomeDirectory /Users/$user
	dscl . -passwd /Users/$user "$passwd"
	mkdir /Users/$user
	chown $uid.$gid /Users/$user

.


pp_platforms="$pp_platforms macos"

pp_backend_macos_detect () {
    [ x"$1" = x"Darwin" ]
}

pp_backend_macos_init () {
    pp_macos_default_bundle_id_prefix="com.quest.rc."
    pp_macos_bundle_id=
    pp_macos_bundle_vendor=
    pp_macos_bundle_version=
    pp_macos_bundle_info_string=
    pp_macos_pkg_type=bundle
    pp_macos_pkg_license=
    pp_macos_pkg_readme=
    pp_macos_pkg_welcome=
    pp_macos_sudo=sudo
    # OS X puts the library version *before* the .dylib extension
    pp_shlib_suffix='*.dylib'
}

pp_macos_plist () {
    typeset in
    in=""
    while test $# -gt 0; do
     case "$1" in

      start-plist) cat <<-.; in="  "; shift ;;
	<?xml version="1.0" encoding="UTF-8"?>
	<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
	<plist version="1.0">
.
      end-plist) echo "</plist>"; in=; shift;;

      '[')   echo "$in<array>"; in="$in  "; shift;;
      ']')   echo "$in</array>"; in="${in#  }"; shift;;
      '{')   echo "<dict>"; in="$in      "; shift;;
      '}')   echo "</dict>"; in="${in#      }"; shift;;
      key)         shift; echo "$in<key>$1</key>"; shift;;
      string)      shift;
		   echo "$1" | sed -e 's/&/&amp;/g;s/</\&lt;/g;s/>/\&gt;/g;' \
				   -e 's/^/'"$in"'<string>/;s/$/<\/string>/';
		   shift;;
      true)        echo "$in<true/>"; shift;;
      false)       echo "$in<false/>"; shift;;
      real)        shift; echo "$in<real>$1</real>"; shift;;
      integer)     shift; echo "$in<integer>$1</integer>"; shift;;
      date)        shift; echo "$in<date>$1</date>"; shift;; # ISO 8601 format
      data)        shift; echo "$in<data>$1</data>"; shift;; # base64 encoded
      *)	   pp_error "pp_macos_plist: bad argument '$1'"; shift;;
     esac
    done
}

pp_macos_rewrite_cpio () {
    typeset script
    script=$pp_wrkdir/cpio-rewrite.pl
    cat <<-'.' >$script
	#!/usr/bin/perl
	#
	# Filter a cpio file, applying the user/group/mode specified in %files
	#
	# A CPIO header block has octal fields at the following offset/lengths:
	#   0  6 magic
	#   6  6 dev
	#  12  6 ino
	#  18  6 mode
	#  24  6 uid
	#  30  6 gid
	#  36  6 nlink
	#  42  6 rdev
	#  48 11 mtime
	#  59  6 namesize (including NUL terminator)
	#  65 11 filesize
	#  76    --
	#
	use strict;
	use warnings;
	no strict 'subs';

	# set %uid, %gid, %mode based on %files
	my (%uid, %gid, %mode, %users, %groups);
	my %type_map = ( d => 0040000, f => 0100000, s => 0120000 );
	while (<DATA>) {
	    my ($type,$mode,$uid,$gid,$flags,$name) =
	        m/^(.) (\S+) (\S+) (\S+) (\S+) (\S+)/;
	    $mode = $type eq "f" ? "0644" : "0755" if $mode eq "-";
	    $uid = 0 if $uid eq "-";
	    $gid = 0 if $gid eq "-";
	    if ($uid ne "=" and $uid =~ m/\D/) {
	        unless (exists $users{$uid}) {
	            my @pw = getpwnam($uid) or die "bad username '$uid'";
	            $users{$uid} = $pw[2];
	        }
	        $uid = $users{$uid};
	    }
	    if ($gid ne "=" and $gid =~ m/\D/) {
	        unless (exists $groups{$gid}) {
	            my @gr = getgrnam($gid) or die "bad group'$gid'";
	            $groups{$gid} = $gr[2];
	        }
	        $gid = $groups{$gid};
	    }
	    $name =~ s:/$:: if $type eq "d";
	    $name = ".".$name."\0";
	    $uid{$name} = sprintf("%06o",int($uid)) unless $uid eq "=";
	    $gid{$name} = sprintf("%06o",int($gid)) unless $gid eq "=";
	    $mode{$name} = sprintf("%06o",oct($mode)|$type_map{$type}) unless $mode eq "=";
	}
	undef %users;
	undef %groups;
	# parse the cpio file
	my $hdrlen = 76;
	while (read(STDIN, my $header, $hdrlen)) {
	    my ($name, $namesize, $filesize);
	    my $filepad = 0;
	    if ($header =~ m/^07070[12]/) {
	        # SVR4 ASCII format, convert to ODC
	        if ($hdrlen == 76) {
	            # Read in rest of header and update header len for SVR4
	            read(STDIN, $header, 110 - 76, 76);
	            $hdrlen = 110;
	        }
	        my $ino = hex(substr($header, 6, 8)) & 0x3ffff;
	        my $mode = hex(substr($header, 14, 8)) & 0x3ffff;
	        my $uid = hex(substr($header, 22, 8)) & 0x3ffff;
	        my $gid = hex(substr($header, 30, 8)) & 0x3ffff;
	        my $nlink = hex(substr($header, 38, 8)) & 0x3ffff;
	        my $mtime = hex(substr($header, 46, 8)) & 0xffffffff;
	        $filesize = hex(substr($header, 54, 8)) & 0xffffffff;
	        my $dev_maj = hex(substr($header, 62, 8));
	        my $dev_min = hex(substr($header, 70, 8));
	        my $dev = &makedev($dev_maj, $dev_min) & 0x3ffff;
	        my $rdev_maj = hex(substr($header, 78, 8));
	        my $rdev_min = hex(substr($header, 86, 8));
	        my $rdev = &makedev($rdev_maj, $rdev_min) & 0x3ffff;
	        $namesize = hex(substr($header, 94, 8)) & 0x3ffff;
	        read(STDIN, $name, $namesize);
	        # Header + name is padded to a multiple of 4 bytes
	        my $namepad = (($hdrlen + $namesize + 3) & 0xfffffffc) - ($hdrlen + $namesize);
	        read(STDIN, my $padding, $namepad) if ($namepad);
	        # File data is padded to be a multiple of 4 bytes
	        $filepad = (($filesize + 3) & 0xfffffffc) - $filesize;

	        my $new_header = sprintf("070707%06o%06o%06o%06o%06o%06o%06o%011o%06o%011o", $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime, $namesize, $filesize);
	        $header = $new_header;
	    } elsif ($header =~ m/^070707/) {
	        # POSIX Portable ASCII Format
	        $namesize = oct(substr($header, 59, 6));
	        $filesize = oct(substr($header, 65, 11));
	        read(STDIN, $name, $namesize);
	    } else {
	        die "bad magic";
	    }
	    # update uid, gid and mode (already in octal)
	    substr($header, 24, 6) = $uid{$name} if exists $uid{$name};
	    substr($header, 30, 6) = $gid{$name} if exists $gid{$name};
	    substr($header, 18, 6) = $mode{$name} if exists $mode{$name};
	    print($header, $name);
	    # check for trailer at EOF
	    last if $filesize == 0 && $name =~ /^TRAILER!!!\0/;
	    # copy-through the file data
	    while ($filesize > 0) {
	        my $seg = 8192;
	        $seg = $filesize if $filesize < $seg;
	        read(STDIN, my $data, $seg);
	        print $data;
	        $filesize -= $seg;
	    }
	    # If file data is padded, skip it
	    read(STDIN, my $padding, $filepad) if ($filepad);
	}
	# pass through any padding at the end (blocksize-dependent)
	for (;;) {
	    my $numread = read(STDIN, my $data, 8192);
	    last unless $numread;
	    print $data;
	}
	exit(0);

	sub makedev {
	    (((($_[0] & 0xff)) << 24) | ($_[1] & 0xffffff));
	}
	__DATA__
.
    # Append to the script the %files data
    cat "$@" </dev/null >> $script
    /usr/bin/perl $script || pp_error "pp_macos_rewrite_cpio error";
}

pp_macos_files_bom () {
    typeset _l t m o g f p st owner
    while read t m o g f p st; do
	# make sure that $m is padded up to 4 digits long
	case "$m" in
	    ?) m="000$m";;
	    ??) m="00$m";;
	    ???) m="0$m";;
	    ?????*) pp_error "pp_macos_writebom: mode '$m' too long";;
	esac

	# convert owner,group into owner/group in octal
	case $o in -)	o=0;; esac
	case $g in -)	g=0;; esac
	owner=`pp_d2o $o`/`pp_d2o $g`

	case $t in
	    f)
		test x"$m" = x"000-" && m=0644
		echo ".$p	10$m	$owner	`
		    /usr/bin/cksum < "${pp_destdir}$p" |
		    awk '{print $2 "	" $1}'`"
		;;
	    d)
		test x"$m" = x"000-" && m=0755
		echo ".${p%/}	4$m	$owner"
		;;
	    s)
		test x"$m" = x"000-" && m=0755
		rl=`/usr/bin/readlink "${pp_destdir}$p"`
		#test x"$rl" = x"$st" ||
		#    pp_error "symlink mismatch $rl != $st"
		echo ".$p	12$m	$owner	`
		    /usr/bin/readlink -n "${pp_destdir}$p" |
		    /usr/bin/cksum |
		    awk '{print $2 "	" $1}'`	$st"
		;;
	esac
    done
}

pp_macos_bom_fix_parents () {
    perl -pe '
	sub dirname { my $d=shift; $d=~s,/[^/]*$,,; $d; }
	sub chk { my $d=shift;
		  &chk(&dirname($d)) if $d =~ m,/,;
		  unless ($seen{$d}++) {
		      print "$d\t40755\t0/0\n";
		  }
		}
	m/^(\S+)\s+(\d+)/;
	if (oct($2) & 040000) {
	    $seen{$1}++; # directory
	}
	&chk(&dirname($1));'
}

pp_macos_files_size () {
    typeset _l t m o g f p st owner
    while read t m o g f p st; do
	case $t in
	    f)	wc -c < "${pp_destdir}$p";;
	    s)	echo 4095;;
	    d)	;; # always seems to be zero
	esac
    done | awk '{n+=1+int($1/4096)} END {print n*4}'
}

pp_o2d () {
    awk 'BEGIN { x=0; '`echo "$1" |
	sed -e 's/./x=x*8+&;/g'`'print x;}' </dev/null
}
pp_d2o () {
    case "$1" in
	[0-7]) echo $1;;
	*) awk 'BEGIN { printf("%o\n", 0+('"$1"'));}' < /dev/null;;
    esac
}

pp_macos_mkbom () {
    #/usr/bin/mkbom -i $1 $2
    typeset path mode ugid size cksum linkpath
    typeset bomstage

    # Use mkbom if it understands -i (avoids a copy)
    if /usr/bin/mkbom -i /dev/null "$2" 2>/dev/null; then
	rm -f "$2"
	/usr/bin/mkbom -i "$1" "$2"
	return
    fi

    # On 10.4 we have this nonsense.
    pp_warn "mkbom workaround: copying source files to staging area"

    bomstage=$pp_wrkdir/bom_stage
    $pp_macos_sudo /bin/mkdir "$bomstage"
    while IFS='	' read path mode ugid size cksumi linkpath; do
	if test -h "$pp_destdir/$path"; then
	    $pp_macos_sudo /bin/ln -s "$linkpath" "$bomstage/$path"
	else
	    if test -d "$pp_destdir/$path"; then
		$pp_macos_sudo /bin/mkdir -p "$bomstage/$path"
	    else
		$pp_macos_sudo /bin/cp "$pp_destdir/$path" "$bomstage/$path"
	    fi
	    $pp_macos_sudo /bin/chmod $mode "$bomstage/$path"
	    $pp_macos_sudo /usr/sbin/chown `echo $ugid| tr / :` "$bomstage/$path"
	fi
    done <"$1"
    (cd $bomstage && $pp_macos_sudo mkbom . $pp_wrkdir/bom_stage.bom) ||
	pp_error "mkbom failed"
    $pp_macos_sudo mv $pp_wrkdir/bom_stage.bom "$2"
}

pp_backend_macos () {
    : ${pp_macos_bundle_id:=$pp_macos_default_bundle_id_prefix$name}
    case "$pp_macos_pkg_type" in
	bundle) pp_backend_macos_bundle;;
	flat) pp_backend_macos_flat;;
	*) pp_error "unsupported package type $pp_macos_pkg_type";;
    esac
}

pp_backend_macos_bundle () {
    typeset pkgdir Contents Resources lprojdir svc
    typeset Info_plist Description_plist
    typeset bundle_vendor bundle_version size cmp filelists

    mac_version=`sw_vers -productVersion`
    bundle_vendor=${pp_macos_bundle_vendor:-$vendor}

    if test -z "$pp_macos_bundle_version"; then
        bundle_version=`echo "$version.0.0.0" | sed -n -e 's/[^0-9.]//g' \
            -e 's/^\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*/\1/p'`
    else
        bundle_version="$pp_macos_bundle_version"
    fi
    source_version=`echo $version | sed 's/.*\.//'`

    # build the package layout
    pkgdir=$pp_wrkdir/$name.pkg
    Contents=$pkgdir/Contents
    Resources=$Contents/Resources
    lprojdir=$Resources/en.lproj
    mkdir $pkgdir $Contents $Resources $lprojdir ||
	pp_error "Can't make package temporary directories"

    echo "major: 1" > $Resources/package_version
    echo "minor: 0" >> $Resources/package_version
    echo "pmkrpkg1" > $Contents/PkgInfo
    case $mac_version in
        "10.6"*)
            xattr -w "com.apple.TextEncoding" "macintosh;0" "$Resources/package_version"
            xattr -w "com.apple.TextEncoding" "macintosh;0" "$Contents/PkgInfo"
            ;;
    esac

    # Copy welcome file/dir for display at package install time.
    if test -n "$pp_macos_pkg_welcome"; then
	typeset sfx
	sfx=`echo "$pp_macos_pkg_welcome"|sed 's/^.*\.\([^\.]*\)$/\1/'`
	case "$sfx" in
	    rtf|html|rtfd|txt) ;;
	    *) sfx=txt;;
	esac
	cp -R ${pp_macos_pkg_welcome} $Resources/Welcome.$sfx
    fi

    # Copy readme file/dir for display at package install time.
    if test -n "$pp_macos_pkg_readme"; then
	typeset sfx
	sfx=`echo "$pp_macos_pkg_readme"|sed 's/^.*\.\([^\.]*\)$/\1/'`
	case "$sfx" in
	    rtf|html|rtfd|txt) ;;
	    *) sfx=txt;;
	esac
	cp -R ${pp_macos_pkg_readme} $Resources/ReadMe.$sfx
    fi

    # Copy license file/dir for display at package install time.
    if test -n "$pp_macos_pkg_license"; then
	typeset sfx
	sfx=`echo "$pp_macos_pkg_license"|sed 's/^.*\.\([^\.]*\)$/\1/'`
	case "$sfx" in
	    rtf|html|rtfd|txt) ;;
	    *) sfx=txt;;
	esac
	cp -R ${pp_macos_pkg_license} $Resources/License.$sfx
    fi

    # Add services (may modify %files)
    for svc in $pp_services .; do
	test . = "$svc" && continue
	pp_macos_add_service $svc
    done

    # Find file lists (%files.* includes ignore files)
    for cmp in $pp_components; do
	test -f $pp_wrkdir/%files.$cmp && filelists="$filelists${filelists:+ }$pp_wrkdir/%files.$cmp"
    done

    # compute the installed size
    size=`cat $filelists | pp_macos_files_size`

    #-- Create Info.plist
    Info_plist=$Contents/Info.plist
    pp_macos_plist \
	start-plist \{ \
	key CFBundleGetInfoString string \
	    "${pp_macos_bundle_info_string:-$version $bundle_vendor}" \
	key CFBundleIdentifier string \
	    "${pp_macos_bundle_id}" \
    key CFBundleName string "$name" \
	key CFBundleShortVersionString string "$bundle_version" \
	key IFMajorVersion integer 1 \
	key IFMinorVersion integer 0 \
	key IFPkgFlagAllowBackRev false \
	key IFPkgFlagAuthorizationAction string "RootAuthorization" \
	key IFPkgFlagDefaultLocation string "/" \
	key IFPkgFlagFollowLinks true \
	key IFPkgFlagInstallFat true \
	key IFPkgFlagInstalledSize integer $size \
	key IFPkgFlagIsRequired false \
	key IFPkgFlagOverwritePermissions true \
	key IFPkgFlagRelocatable false \
	key IFPkgFlagRestartAction string "NoRestart" \
	key IFPkgFlagRootVolumeOnly true \
	key IFPkgFlagUpdateInstalledLanguages false \
	key IFPkgFlagUseUserMask false \
	key IFPkgFormatVersion real 0.10000000149011612 \
	key SourceVersion string $source_version \
	\} end-plist> $Info_plist

    # write en.lproj/Description.plist
    Description_plist=$lprojdir/Description.plist
    pp_macos_plist \
 	start-plist \{ \
        key IFPkgDescriptionDeleteWarning string "" \
	    key IFPkgDescriptionDescription string "$pp_macos_bundle_info_string" \
	    key IFPkgDescriptionTitle       string "$name" \
	    key IFPkgDescriptionVersion string "$version" \
 	\} end-plist > $Description_plist

    # write Resources/files
    awk '{print $6}' $filelists > $Resources/files

    # write package size file
    printf \
"NumFiles 0
InstalledSize $size
CompressedSize 0
" > $Resources/$name.sizes

    # write Resources/preinstall
    for cmp in $pp_components; do
	if test -s $pp_wrkdir/%pre.$cmp; then
	    if test ! -s $Resources/preinstall; then
		echo "#!/bin/sh" > $Resources/preinstall
		chmod +x $Resources/preinstall
	    fi
	    cat $pp_wrkdir/%pre.$cmp >> $Resources/preinstall
	    echo : >> $Resources/preinstall
	fi
    done

    # write Resources/postinstall
    for cmp in $pp_components; do
	if test -s $pp_wrkdir/%post.$cmp; then
	    if test ! -s $Resources/postinstall; then
		echo "#!/bin/sh" > $Resources/postinstall
		chmod +x $Resources/postinstall
	    fi
	    cat $pp_wrkdir/%post.$cmp >> $Resources/postinstall
	    echo : >> $Resources/postinstall
	fi
    done

    # write Resources/postupgrade
    for cmp in $pp_components; do
	if test -s $pp_wrkdir/%postup.$cmp; then
	    if test ! -s $Resources/postupgrade; then
		echo "#!/bin/sh" > $Resources/postupgrade
		chmod +x $Resources/postupgrade
	    fi
	    cat $pp_wrkdir/%postup.$cmp >> $Resources/postupgrade
	    echo : >> $Resources/postupgrade
	fi
    done

    # write Resources/preremove
    for cmp in $pp_components; do
	if test -s $pp_wrkdir/%preun.$cmp; then
	    if test ! -s $Resources/preremove; then
		echo "#!/bin/sh" > $Resources/preremove
		chmod +x $Resources/preremove
	    fi
	    cat $pp_wrkdir/%preun.$cmp >> $Resources/preremove
	    echo : >> $Resources/preremove
	fi
    done

    # write Resources/postremove
    for cmp in $pp_components; do
	if test -s $pp_wrkdir/%postun.$cmp; then
	    if test ! -s $Resources/postremove; then
		echo "#!/bin/sh" > $Resources/postremove
		chmod +x $Resources/postremove
	    fi
	    cat $pp_wrkdir/%postun.$cmp >> $Resources/postremove
	    echo : >> $Resources/postremove
	fi
    done

    # write uninstall info
    echo "version=$version" > $Resources/uninstall
    if [ -n "$pp_macos_requires" ];then
        echo "requires=$pp_macos_requires" >> $Resources/uninstall
    fi

    . $pp_wrkdir/%fixup

    # Create the bill-of-materials (Archive.bom)
    cat $filelists | pp_macos_files_bom | sort |
	pp_macos_bom_fix_parents > $pp_wrkdir/tmp.bomls

    pp_macos_mkbom $pp_wrkdir/tmp.bomls $Contents/Archive.bom

    # Create the cpio archive (Archive.pax.gz)
    (
    cd $pp_destdir &&
    awk '{ print "." $6 }' $filelists | sed 's:/$::' | sort | /usr/bin/cpio -o | pp_macos_rewrite_cpio $filelists | gzip -9f -c > $Contents/Archive.pax.gz
    )

    test -d $pp_wrkdir/bom_stage && $pp_macos_sudo rm -rf $pp_wrkdir/bom_stage

    rm -f ${name}-${version}.dmg
    hdiutil create -fs HFS+ -srcfolder $pkgdir -volname $name ${name}-${version}.dmg
}

pp_backend_macos_flat () {
    typeset pkgdir bundledir Resources lprojdir svc
    typeset Info_plist Description_plist
    typeset bundle_vendor bundle_version size numfiles cmp filelists

    mac_version=`sw_vers -productVersion`
    bundle_vendor=${pp_macos_bundle_vendor:-$vendor}

    if test -z "$pp_macos_bundle_version"; then
        bundle_version=`echo "$version.0.0.0" | sed -n -e 's/[^0-9.]//g' \
            -e 's/^\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*/\1/p'`
    else
        bundle_version="$pp_macos_bundle_version"
    fi
    source_version=`echo $version | sed 's/.*\.//'`

    # build the flat package layout
    pkgdir=$pp_wrkdir/pkg
    bundledir=$pp_wrkdir/pkg/$name.pkg
    Resources=$pkgdir/Resources
    lprojdir=$Resources/en.lproj
    mkdir $pkgdir $bundledir $Resources $lprojdir ||
	pp_error "Can't make package temporary directories"

    # Add services (may modify %files)
    for svc in $pp_services .; do
	test . = "$svc" && continue
	pp_macos_add_service $svc
    done

    # Find file lists (%files.* includes ignore files)
    for cmp in $pp_components; do
	test -f $pp_wrkdir/%files.$cmp && filelists="$filelists${filelists:+ }$pp_wrkdir/%files.$cmp"
    done

    # compute the installed size and number of files/dirs
    size=`cat $filelists | pp_macos_files_size`
    numfiles=`cat $filelists | wc -l`
    numfiles="${numfiles##* }"

    # Write Distribution file
    cat <<-. >$pkgdir/Distribution
	<?xml version="1.0" encoding="UTF-8"?>
	<installer-script minSpecVersion="1.000000" authoringTool="com.quest.rc.PolyPkg" authoringToolVersion="$pp_version" authoringToolBuild="$pp_revision">
	    <title>$name $version</title>
	    <options customize="never" allow-external-scripts="no"/>
	    <domains enable_localSystem="true"/>
.
    if test -n "$pp_macos_pkg_welcome"; then
	cp -R "${pp_macos_pkg_welcome}" $Resources
	echo "    <welcome file=\"${pp_macos_pkg_welcome##*/}\"/>" >>$pkgdir/Distribution
    fi
    if test -n "$pp_macos_pkg_readme"; then
	cp -R "${pp_macos_pkg_readme}" $Resources
	echo "    <readme file=\"${pp_macos_pkg_readme##*/}\"/>" >>$pkgdir/Distribution
    fi
    if test -n "$pp_macos_pkg_license"; then
	cp -R "${pp_macos_pkg_license}" $Resources
	echo "    <license file=\"${pp_macos_pkg_license##*/}\"/>" >>$pkgdir/Distribution
    fi
    cat <<-. >>$pkgdir/Distribution
	    <choices-outline>
	        <line choice="choice0"/>
	    </choices-outline>
	    <choice id="choice0" title="$name $version">
	        <pkg-ref id="${pp_macos_bundle_id}"/>
	    </choice>
	    <pkg-ref id="${pp_macos_bundle_id}" installKBytes="$size" version="$version" auth="Root">#$name.pkg</pkg-ref>
	</installer-script>
.

    # write scripts archive
    # XXX - missing preupgrade, preflight, postflight
    mkdir $pp_wrkdir/scripts
    for cmp in $pp_components; do
	if test -s $pp_wrkdir/%pre.$cmp; then
	    if test ! -s $pp_wrkdir/scripts/preinstall; then
		echo "#!/bin/sh" > $pp_wrkdir/scripts/preinstall
		chmod +x $pp_wrkdir/scripts/preinstall
	    fi
	    cat $pp_wrkdir/%pre.$cmp >> $pp_wrkdir/scripts/preinstall
	    echo : >> $pp_wrkdir/scripts/preinstall
	fi
	if test -s $pp_wrkdir/%post.$cmp; then
	    if test ! -s $pp_wrkdir/scripts/postinstall; then
		echo "#!/bin/sh" > $pp_wrkdir/scripts/postinstall
		chmod +x $pp_wrkdir/scripts/postinstall
	    fi
	    cat $pp_wrkdir/%post.$cmp >> $pp_wrkdir/scripts/postinstall
	    echo : >> $pp_wrkdir/scripts/postinstall
	fi
	if test -s $pp_wrkdir/%postup.$cmp; then
	    if test ! -s $pp_wrkdir/scripts/postupgrade; then
		echo "#!/bin/sh" > $pp_wrkdir/scripts/postupgrade
		chmod +x $pp_wrkdir/scripts/postupgrade
	    fi
	    cat $pp_wrkdir/%postup.$cmp >> $pp_wrkdir/scripts/postupgrade
	    echo : >> $pp_wrkdir/scripts/postupgrade
	fi
	# XXX - not supported
	if test -s $pp_wrkdir/%preun.$cmp; then
	    if test ! -s $pp_wrkdir/scripts/preremove; then
		echo "#!/bin/sh" > $pp_wrkdir/scripts/preremove
		chmod +x $pp_wrkdir/scripts/preremove
	    fi
	    cat $pp_wrkdir/%preun.$cmp >> $pp_wrkdir/scripts/preremove
	    echo : >> $pp_wrkdir/scripts/preremove
	fi
	# XXX - not supported
	if test -s $pp_wrkdir/%postun.$cmp; then
	    if test ! -s $pp_wrkdir/scripts/postremove; then
		echo "#!/bin/sh" > $pp_wrkdir/scripts/postremove
		chmod +x $pp_wrkdir/scripts/postremove
	    fi
	    cat $pp_wrkdir/%postun.$cmp >> $pp_wrkdir/scripts/postremove
	    echo : >> $pp_wrkdir/scripts/postremove
	fi
    done
    if test "`echo $pp_wrkdir/scripts/*`" != "$pp_wrkdir/scripts/*"; then
	# write scripts archive, scripts are mode 0755 uid/gid 0/0
	# resetting the owner and mode is not strictly required
	(
	cd $pp_wrkdir/scripts || pp_error "Can't cd to $pp_wrkdir/scripts"
	rm -f $pp_wrkdir/tmp.files.scripts
	for s in *; do
	    echo "f 0755 0 0 - ./$s" >>$pp_wrkdir/tmp.files.scripts
	done
	find . -type f | /usr/bin/cpio -o | pp_macos_rewrite_cpio $pp_wrkdir/tmp.files.scripts | gzip -9f -c > $bundledir/Scripts
	)
    fi

    # Write PackageInfo file
    cat <<-. >$bundledir/PackageInfo
	<?xml version="1.0" encoding="UTF-8"?>
	<pkg-info format-version="2" identifier="${pp_macos_bundle_id}" version="$version" install-location="/" relocatable="false" overwrite-permissions="true" followSymLinks="true" auth="root">
	    <payload installKBytes="$size" numberOfFiles="$numfiles"/>
.
    if test -s $bundledir/Scripts; then
	echo "    <scripts>" >>$bundledir/PackageInfo
	for s in preflight postflight preinstall postinstall preupgrade postupgrade; do
	    if test -s "$pp_wrkdir/scripts/$s"; then
		echo "	<$s file=\"$s\"/>" >>$bundledir/PackageInfo
	    fi
	done
	echo "    </scripts>" >>$bundledir/PackageInfo
    fi
    cat <<-. >>$bundledir/PackageInfo
	</pkg-info>
.

    . $pp_wrkdir/%fixup

    # Create the bill-of-materials (Bom)
    cat $filelists | pp_macos_files_bom | sort |
	pp_macos_bom_fix_parents > $pp_wrkdir/tmp.bomls
    pp_macos_mkbom $pp_wrkdir/tmp.bomls $bundledir/Bom

    # Create the cpio payload
    (
    cd $pp_destdir || pp_error "Can't cd to $pp_destdir"
    awk '{ print "." $6 }' $filelists | sed 's:/$::' | sort | /usr/bin/cpio -o | pp_macos_rewrite_cpio $filelists | gzip -9f -c > $bundledir/Payload
    )

    test -d $pp_wrkdir/bom_stage && $pp_macos_sudo rm -rf $pp_wrkdir/bom_stage

    # Create the flat package with xar (like pkgutil --flatten does)
    # Note that --distribution is only supported by Mac OS X 10.6 and above
    xar_flags="--compression=bzip2 --no-compress Scripts --no-compress Payload"
    case $mac_version in
        "10.5"*) ;;
	*)	 xar_flags="$xar_flags --distribution";;
    esac
    (cd $pkgdir && /usr/bin/xar $xar_flags -cf "../$name-$version.pkg" *)
}

pp_backend_macos_cleanup () {
    :
}

pp_backend_macos_names () {
    case "$pp_macos_pkg_type" in
	bundle) echo ${name}.pkg;;
	flat) echo ${name}-${version}.pkg;;
	*) pp_error "unsupported package type $pp_macos_pkg_type";;
    esac
}

pp_backend_macos_install_script () {
    echo '#!/bin/sh'
    typeset pkgname platform

    pkgname="`pp_backend_macos_names`"
    platform="`pp_backend_macos_probe`"
    pp_install_script_common

    cat <<.
	test \$# -eq 0 && usage
	op="\$1"; shift

	case "\$op" in
	list-components)
	    test \$# -eq 0 || usage \$op
	    echo "$pp_components"
	    ;;
	list-services)
	    test \$# -eq 0 || usage \$op
	    echo "$pp_services"
	    ;;
	list-files)
	    test \$# -ge 1 || usage \$op
	    echo \${PP_PKGDESTDIR:-.}/"$pkgname"
	    ;;
	install)
	    test \$# -ge 1 || usage \$op
	    vol=/Volumes/pp\$\$
	    pkg=\$vol/${name}-${version}.pkg
	    hdiutil attach -readonly -mountpoint \$vol \
		\${PP_PKGDESTDIR:-.}/"$pkgname"
	    trap "hdiutil detach \$vol" 0
	    installer -pkginfo -pkg \$pkg
	    installer -verbose -pkg \$pkg -target /
	    ;;
	uninstall)
	    test \$# -ge 1 || usage \$op
	    # XXX
	    echo "Uninstall not implemented" >&2
	    exit 1;;
	start|stop)
	    test \$# -ge 1 || usage \$op
	    ec=0
	    for svc
	    do
		# XXX
		echo "\${op} not implemented" >&2
		ec=1
	    done
	    exit \$ec
	    ;;
	print-platform)
	    echo "$platform"
	    ;;
	*)
	    usage;;
	esac
.
}

pp_backend_macos_init_svc_vars () {
    pp_macos_start_services_after_install=false
    pp_macos_service_name=
    pp_macos_default_service_id_prefix="com.quest.rc."
    pp_macos_service_id=
    pp_macos_service_user=
    pp_macos_service_group=
    pp_macos_service_initgroups=
    pp_macos_service_umask=
    pp_macos_service_cwd=
    pp_macos_service_nice=
    pp_macos_svc_plist_file=
}

pp_macos_launchd_plist () {
    typeset svc svc_id

    svc="$1"
    svc_id="$2"

    set -- $cmd

    if [ -n "$pp_macos_svc_plist_file" ]; then
        echo "## Launchd plist file already defined at $pp_macos_svc_plist_file"
        return
    fi

    echo "## Generating the launchd plist file for $svc"
    pp_macos_svc_plist_file="$pp_wrkdir/$svc.plist"
    cat <<-. > $pp_macos_svc_plist_file
	<?xml version="1.0" encoding="UTF-8"?>
	<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
	http://www.apple.com/DTDs/PropertyList-1.0.dtd >
	<plist version="1.0">
	<dict>
	    <key>Label</key>
	    <string>$svc_id</string>
	    <key>ProgramArguments</key>
	    <array>
.
    while test $# != 0; do
	printf "        <string>$1</string>\n" >> $pp_macos_svc_plist_file
	shift
    done
    cat <<-. >> $pp_macos_svc_plist_file
	    </array>
	    <key>KeepAlive</key>
	    <true/>
.
    if test -n "$pp_macos_service_user"; then
	printf "    <key>UserName</key>\n" >> $pp_macos_svc_plist_file
	printf "    <string>$pp_macos_service_user</string>\n" >> $pp_macos_svc_plist_file
    fi
    if test -n "$pp_macos_service_group"; then
	printf "    <key>GroupName</key>\n" >> $pp_macos_svc_plist_file
	printf "    <string>$pp_macos_service_group</string>\n" >> $pp_macos_svc_plist_file
    fi
    if test -n "$pp_macos_service_initgroups"; then
	printf "    <key>InitGroups</key>\n" >> $pp_macos_svc_plist_file
	printf "    <string>$pp_macos_service_initgroups</string>\n" >> $pp_macos_svc_plist_file
    fi
    if test -n "$pp_macos_service_umask"; then
	printf "    <key>Umask</key>\n" >> $pp_macos_svc_plist_file
	printf "    <string>$pp_macos_service_umask</string>\n" >> $pp_macos_svc_plist_file
    fi
    if test -n "$pp_macos_service_cwd"; then
	printf "    <key>WorkingDirectory</key>\n" >> $pp_macos_svc_plist_file
	printf "    <string>$pp_macos_service_cwd</string>\n" >> $pp_macos_svc_plist_file
    fi
    if test -n "$pp_macos_service_nice"; then
	printf "    <key>Nice</key>\n" >> $pp_macos_svc_plist_file
	printf "    <string>$pp_macos_service_nice</string>\n" >> $pp_macos_svc_plist_file
    fi
    cat <<-. >> $pp_macos_svc_plist_file
	</dict>
	</plist>
.
}

pp_macos_add_service () {
    typeset svc svc_id plist_file plist_dir

    pp_load_service_vars "$1"
    svc=${pp_macos_service_name:-$1}
    svc_id=${pp_macos_service_id:-$pp_macos_default_service_id_prefix$svc}

    #-- create a plist file for svc
    pp_macos_launchd_plist "$svc" "$svc_id"

    #-- copy the plist file into place and add to %files
    plist_dir="/Library/LaunchDaemons"
    plist_file="$plist_dir/$svc_id.plist"
    mkdir -p "$pp_destdir/$plist_dir"
    cp "$pp_macos_svc_plist_file" "$pp_destdir/$plist_file"
    pp_add_file_if_missing "$plist_file"

    #-- add code to start the service on install
    ${pp_macos_start_services_after_install} && <<-. >> $pp_wrkdir/%post.$svc
	# start service '$svc' automatically after install
	launchctl load "$plist_file"
.
}

pp_backend_macos_probe () {
    typeset name vers arch
    case `sw_vers -productName` in
         "Mac OS X") name="macos";;
	 *)          name="unknown";;
    esac
    vers=`sw_vers -productVersion | sed -e 's/^\([^.]*\)\.\([^.]*\).*/\1\2/'`
    arch=`arch`
    echo "$name$vers-$arch"
}

pp_backend_macos_vas_platforms () {
    echo "osx"    # XXX non-really sure what they do.. it should be "macos"
}
pp_backend_macos_function () {
    case "$1" in
	_pp_macos_search_unused) cat<<'.';;
	    # Find an unused value in the given path
	    # args: path attribute minid [maxid]
		pp_tmp_val=$3
		while :; do
		    test $pp_tmp_val -ge ${4:-999999} && return 1
		    /usr/bin/dscl . -search "$1" "$2" $pp_tmp_val |
			grep . > /dev/null || break
		    pp_tmp_val=`expr $pp_tmp_val + 1`
		done
		echo $pp_tmp_val
.
        pp_mkgroup:depends) echo _pp_macos_search_unused;;
        pp_mkgroup) cat<<'.';;
	    set -e
	    /usr/bin/dscl . -read /Groups/"$1" >/dev/null 2>&1 && return
	    pp_tmp_gid=`_pp_macos_search_unused /Groups PrimaryGroupID 100`
	    /usr/bin/dscl . -create /Groups/"$1"
	    /usr/bin/dscl . -create /Groups/"$1" PrimaryGroupID $pp_tmp_gid
	    /usr/bin/dscl . -create /Groups/"$1" RealName "Group $1"
	    /usr/bin/dscl . -create /Groups/"$1" GroupMembership ""
	    /usr/bin/dscl . -create /Groups/"$1" Password '*'
.
        pp_mkuser:depends) echo pp_mkgroup _pp_macos_search_unused;;
        pp_mkuser) cat<<'.';;
	    set -e
	    /usr/bin/dscl . -read /Users/"$1" >/dev/null 2>&1 && return
	    pp_tmp_uid=`_pp_macos_search_unused /Users UniqueID 100`
	    pp_mkgroup "${2:-$1}"
	    pp_tmp_gid=`/usr/bin/dscl . -read /Groups/"${2:-$1}" \
		PrimaryGroupID | awk '{print $2}'`
	    /usr/bin/dscl . -create /Users/"$1"
	    /usr/bin/dscl . -create /Users/"$1" PrimaryGroupID $pp_tmp_gid
	    /usr/bin/dscl . -create /Users/"$1" NFSHomeDirectory \
				    "${3:-/var/empty}"
	    /usr/bin/dscl . -create /Users/"$1" UserShell \
				    "${4:-/usr/bin/false}"
	    /usr/bin/dscl . -create /Users/"$1" RealName "$1"
	    /usr/bin/dscl . -create /Users/"$1" UniqueID $pp_tmp_uid
	    /usr/bin/dscl . -create /Users/"$1" Password '*'
.
        pp_havelib) cat<<'.';;
	    # (use otool -L to find dependent libraries)
            for pp_tmp_dir in `echo "${3:+$3:}/usr/local/lib:/lib:/usr/lib" |
		    tr : ' '`; do
                test -r "$pp_tmp_dir/lib$1{$2:+.$2}.dylib" && return 0
            done
            return 1
.
	*) false;;
    esac
}

pp_platforms="$pp_platforms inst"

pp_backend_inst_detect () {
    case "$1" in
	IRIX*)	return 0;;
	*)	return 1;;
    esac
}

pp_backend_inst_init () {
    pp_readlink_fn=pp_ls_readlink
}

pp_backend_inst_create_idb()
{
    typeset t m o g f p st

    while read t m o g f p st; do
        if test x"$o" = x"-"; then
            o="root"
        fi
        if test x"$g" = x"-"; then
            g="sys"
        fi
        case "$t" in
            f)  test x"$m" = x"-" && m=444
                echo "f 0$m $o $g $p $p $name.sw.base"
                ;;
            d)  test x"$m" = x"-" && m=555
                echo "d 0$m $o $g $p $p $name.sw.base"
                ;;
            s)  test x"$m" = x"-" && m=777
                test x"$m" = x"777" ||
                    pp_warn "$p: invalid mode $m for symlink, should be 777 or -"
                echo "l 0$m $o $g $p $p $name.sw.base symval($st)"
                ;;
        esac
    done
}

pp_backend_inst_create_spec()
{
    echo "product $name"
    echo "    id \"${summary}. Version: ${version}\""
    echo "    image sw"
    echo "        id \"Software\""
    echo "        version $version"
    echo "        order 9999"
    echo "        subsys base"
    echo "            id \"Base Software\""
    echo "            replaces self"
    echo "            exp $name.sw.base"
    echo "        endsubsys"
    echo "    endimage"
    echo "endproduct"
}

pp_backend_inst () {
    curdir=`pwd`

    cd "$pp_opt_wrkdir"

    # initialize
    pp_inst_tardist=tardist
    pp_inst_spec=${name}.spec
    pp_inst_idb=${name}.idb
 
    rm -rf $pp_inst_tardist $pp_inst_spec $pp_inst_idb
    mkdir -p $pp_inst_tardist

    # Create idb file
    (for _cmp in $pp_components; do
        cat  %files.$_cmp | sort +4u -6 | pp_backend_inst_create_idb
    done) >> $pp_inst_idb

    pp_backend_inst_create_spec >> $pp_inst_spec

    # Generate tardist
    gendist -verbose -all -root / -source $pp_opt_destdir -idb $pp_inst_idb -spec $pp_inst_spec -dist $pp_inst_tardist $name
    tar -cvf `pp_backend_inst_names` $pp_inst_tardist

    cd "$curdir"
}

pp_backend_inst_cleanup () {
    :
}

pp_backend_inst_names () {
    echo ${name}-${version}.tardist
}

pp_backend_inst_install_script () {
    :
}

pp_backend_inst_function () {
    echo false
}

pp_backend_inst_init_svc_vars () {
    :
}

pp_backend_inst_probe () {
    cpu=`hinv|sed -n '/^CPU/{s/000 /k /;s/^CPU: //;s/ Process.*//;s/^MIPS //;p;q;}'|tr A-Z a-z`
    echo irix`uname -r`-$cpu
}

pp_backend_inst_vas_platforms () {
    echo "irix-65"
}

pp_platforms="$pp_platforms null"

pp_backend_null_detect () {
    ! :
}

pp_backend_null_init () {
    :
}


pp_backend_null () {
    :
}

pp_backend_null_cleanup () {
    :
}

pp_backend_null_names () {
    :
}

pp_backend_null_install_script () {
    :
}

pp_backend_null_function () {
    echo false
}

pp_backend_null_init_svc_vars () {
    :
}

pp_backend_null_probe () {
    echo unknown-unknown
}

pp_backend_null_vas_platforms () {
:
}


quest_require_vas () {
    typeset v d

    if test $# -ne 1; then
        return
    fi
    set -- `echo "$1" | tr . ' '` 0 0 0

    for d
    do
        echo $d | grep '^[0-9][0-9]*$' > /dev/null ||
            pp_error "quest_require_vas: Bad version component $d"
    done

    test $# -lt 4 &&
            pp_error "quest_require_vas: missing version number"

    case "$1.$2.$3.$4" in
        *.0.0.0) v=$1;;
        *.*.0.0) v=$1.$2;;
        *.*.*.0) v=$1.$2.$3;;
        *)       v=$1.$2.$3.$4;;
    esac

    cat <<.
        if test -x /opt/quest/bin/vastool &&
           /opt/quest/bin/vastool -v |
            awk 'NR == 1 {print \$4}' |
            awk -F. '{ if (\$1<$1 || \$1==$1 && ( \
                           \$2<$2 || \$2==$2 && ( \
                           \$3<$3 || \$2==$3 && ( \
                           \$4<$4 )))) exit(1); }'
        then
            exit 0
        else
            echo "Requires VAS $v or later"
            exit 1
        fi
.
}
pp_main ${1+"$@"}