summaryrefslogtreecommitdiff
path: root/Master/texmf/scripts/texlive/tlmgr.pl
blob: 0b1f9620d9160f2e1b727d9ea7c9a82073c6d511 (plain)
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
#!/usr/bin/env perl
# $Id$
#
# Copyright 2008, 2009 Norbert Preining
# This file is licensed under the GNU General Public License version 2
# or any later version.
#
# TODO:
# - in GUI mode updating bin-texlive/texlive.infra DOES work without
#   the warning, but it does NOT force to restart the GUI. THAT IS BAD!!!
#
# - when tlmgr2.pl is shipped globally as tlmgr.pl we can switch the
#   installer from using either texconfig paper or texlua ... to
#   tlmgr paper letter which will work on all platforms transparently.
#
# - tlmgr should have a "progress" bar for the update --all and install
#   etc options, echoing number of total packages etc etc.
#

my $svnrev = '$Revision$';
my $datrev = '$Date$';
my $tlmgrrevision;
if ($svnrev =~ m/: ([0-9]+) /) {
  $tlmgrrevision = $1;
} else {
  $tlmgrrevision = "unknown";
}
$datrev =~ s/^.*Date: //;
$datrev =~ s/ \(.*$//;
$tlmgrrevision .= " ($datrev)";

our $Master;

BEGIN {
  $^W = 1;
  # make subprograms (including kpsewhich) have the right path:
  $mydir = $0;
  $mydir =~ s,/[^/]*$,,;
  if (!$^O=~/^MSWin(32|64)$/i) {
    $ENV{"PATH"} = "$mydir:$ENV{PATH}";
    $kpsewhichname = "kpsewhich.exe";
  } else {
    $kpsewhichname = "kpsewhich";
  }

  #
  if (! -r "$mydir/$kpsewhichname") {
    # no kpsewhich in the same directory, so we are in a bootstrapping
    # mode, use for Master $mydir/../..
    $Master = "$mydir/../..";
  } else {
    chomp($Master = `kpsewhich -var-value=SELFAUTOPARENT`);
  }

  #
  # make Perl find our packages first:
  unshift (@INC, "$Master/tlpkg");
  unshift (@INC, "$Master/texmf/scripts/texlive");
}

use Cwd qw/abs_path/;
use Pod::Usage;
use Getopt::Long qw(:config no_autoabbrev permute);
use strict;

use TeXLive::TLConfig;
use TeXLive::TLMedia;
use TeXLive::TLPDB;
use TeXLive::TLPOBJ;
use TeXLive::TLPackageConfigurations;
use TeXLive::TLUtils;
use TeXLive::TLWinGoo;
TeXLive::TLUtils->import(qw(member info give_ctan_mirror win32 dirname
                            mkdirhier merge_into copy log debug));

use TeXLive::TLPaper;

binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");

our $tlmediasrc;   # media from which we install/update
our $tlmediatlpdb;
our $location;     # location from which the new packages come
our $localtlmedia; # local installation which we are munging
our $localtlpdb;   # local installation which we are munging
my %options;       # TL options from local tlpdb

# flags for machine-readable form
our $FLAG_REMOVE = "d";
our $FLAG_FORCIBLE_REMOVED = "f";
our $FLAG_UPDATE = "u";
our $FLAG_REVERSED_UPDATE = "r";
our $FLAG_AUTOINSTALL = "a";

# option variables
$::gui_mode = 0;
$::machinereadable = 0;

my %globaloptions = (
  "gui" => 1,
  "gui-lang" => "=s",
  "location" => "=s",
  "machine-readable" => 1,
  "package-logfiles" => "=s",
  "pause" => 1,
  "version" => 1,
  "help|h|?" => 1);

my %actionoptions = (
  "remove"   =>  { "no-depends" => 1, 
                   "no-depends-at-all" => 1,
                   "force" => 1,
                   "dry-run|n" => 1 },
  "show"     =>  { "list" => 1 },
  "search"   =>  { "global" => 1, 
                   "file" => 1 },
  "restore"  =>  { "backupdir" => "=s", 
                   "dry-run|n" => 1, 
                   "force" => 1 },
  "backup"   =>  { "backupdir" => "=s", 
                   "clean" => ":-99", 
                   "all" => 1, 
                   "dry-run|n" => 1 },
  "update"   =>  { "no-depends" => 1, 
                   "no-depends-at-all" => 1, 
                   "all" => 1, 
                   "list" => 1,
                   "no-remove" => 1, 
                   "force" => 1, 
                   "backupdir" => "=s", 
                   "backup" => 1, 
                   "dry-run|n" => 1 },
  "paper"    =>  { "list" => 1 },
  "install"  =>  { "no-depends" => 1, 
                   "no-depends-at-all" => 1, 
                   "reinstall" => 1,
                   "force" => 1, 
                   "dry-run|n" => 1 },
  "arch"     =>  { "dry-run|n" => 1 },
  "generate" =>  { "localcfg" => "=s", 
                   "dest" => "=s" },
  "uninstall"=>  { "force" => 1 },
  "check"    =>  { "use-svn" => 1 },
  "recreate-tlpdb" => { "arch" => "=s" }
  );

my %optarg;
for my $k (keys %globaloptions) { 
  if ($globaloptions{$k} eq "1") {
    $optarg{$k} = 1;
  } else {
    $optarg{"$k" . $globaloptions{$k}} = 1;
  }
}
for my $v (values %actionoptions) {
  for my $k (keys %$v) { 
    if ($v->{$k} eq "1") {
      $optarg{$k} = 1;
    } else {
      $optarg{"$k" . $v->{$k}} = 1;
    }
  }
}
TeXLive::TLUtils::process_logging_options();

our %opts;

GetOptions(\%opts, keys(%optarg)) or pod2usage(2);

$::machinereadable = $opts{"machine-readable"}  
  if (defined($opts{"machine-readable"}));

my $action = shift;
if (!defined($action)) {
  $action = $opts{"gui"} ? "gui" : "";
}

ddebug("action = $action\n");
for my $k (keys %opts) {
  ddebug("$k => $opts{$k}\n");
}
ddebug("arguments: @ARGV\n") if @ARGV;

sub give_version {
  if (!defined($::version_string)) {
    $::version_string = "";
    $::version_string .= "tlmgr revision $tlmgrrevision\n";
    $::version_string .= "tlmgr using installation: $Master\n";
    if (open (REL_TL, "$Master/release-texlive.txt")) {
      # print first and last lines, which have the TL version info.
      my @rel_tl = <REL_TL>;
      $::version_string .= $rel_tl[0];
      $::version_string .= $rel_tl[$#rel_tl];
      close (REL_TL);
    }
  }
  return $::version_string;
}

if ($opts{"version"} || (defined $action && $action eq "version")) {
  info(give_version());
  finish(0);
}

if (defined($action) && ($action =~ m/^help/i)) {
  $opts{"help"} = 1;
}

if (defined($action) && ($action =~ m/^print-arch$/i)) {
  $::installerdir = $Master;
  print TeXLive::TLUtils::platform(), "\n";
  exit 0;
}

if ((!defined($action) || !$action) && !$opts{"help"}) {
  die "$0: missing action; try --help if you need it.\n";
}

if ($opts{"help"}) {
  # perldoc does ASCII emphasis on the output, so it's nice to use it.
  # But not all Unix platforms have it, and on Windows our Config.pm
  # can apparently interfere, so always skip it there.
  my @noperldoc = ();
  if (win32() || ! TeXLive::TLUtils::which("perldoc")) {
    @noperldoc = ("-noperldoc", "1");
  }
  pod2usage("-exitstatus" => 0, "-verbose" => 2, @noperldoc);
}

# unify arguments so that the $action contains paper in all cases
# and push the first arg back to @ARGV for action_paper processing
if ($action =~ /^(paper|xdvi|pdftex|dvips|dvipdfmx?|context)$/) {
  unshift(@ARGV, $action);
  $action = "paper";
}

# --machine-readable is only supported by update.
# 
if ($::machinereadable && $action ne "update") {
  tlwarn("tlmgr: --machine-readable output not supported for $action\n");
}

# check on supported arguments
#
my %suppargs;
%suppargs = %{$actionoptions{$action}}
  if defined($actionoptions{$action});
my @notvalidargs;
for my $k (keys %opts) {
  my $kk = $k;
  if ($k eq "n" || $k eq "dry-run") {
    $kk = "dry-run|n";
  }
  if (!defined($suppargs{$kk}) && !defined($globaloptions{$kk})) {
    push @notvalidargs, $k;
  }
}
if (@notvalidargs) {
  my $msg = 
    "The following arguments are not supported for the action $action:\n";
  for my $c (@notvalidargs) {
    $msg .= " $c";
  }
  $msg .= "\n";
  # here we should call pod2usage actually with the argument
  # -verbose => 99
  # -sections => "ACTIONS/$action.*"
  # to show the correct invocation of the action
  my @noperldoc = ();
  if (win32() || ! TeXLive::TLUtils::which("perldoc")) {
    @noperldoc = ("-noperldoc", "1");
  }
  pod2usage(-msg => $msg, -exitstatus => 1, -verbose => 1, @noperldoc);
}

# besides doing normal logging if -logfile is specified, we try to log
# package related actions (install, remove, update) to 
# the package-log file TEXMFSYSVAR/web2c/tlmgr.log
my $packagelogged = 0;  # how many msgs we logged
chomp (my $texmfsysvar = `kpsewhich -var-value=TEXMFSYSVAR`);
my $packagelogfile = $opts{"package-logfile"};
$packagelogfile ||= "$texmfsysvar/web2c/tlmgr.log";
#
# Try to open the packagelog file, but do NOT die when that does not work
if (!open(PACKAGELOG, ">>$packagelogfile")) {
  debug("Cannot open package log file $packagelogfile for appending\n");
  debug("Will not log package installation/removal/update for that run\n");
  $packagelogfile = "";
}

my $loadmediasrcerror = "Cannot load TeX Live database from ";

execute_action($action, @ARGV);
# end of main program.



sub execute_action {
  my ($action, @argv) = @_;
  my %ret;

  # we have to set @ARGV to the @argv since many of the action_* subs
  # use GetOption
  @ARGV = @argv;

  # actions which shouldn't have any lasting effects, such as search or
  # list, end by calling finish(0), which skips postinstall actions.
  if ($action =~ m/^_include_tlpobj$/) {
    # this is an internal function that should not be used outside
    init_local_db();
    for my $f (@ARGV) {
      my $tlpobj = TeXLive::TLPOBJ->new;
      $tlpobj->from_file($f);
      # we now have to check whether that is a .doc or .src package, so shipping
      # src or doc files from a different package.
      # We should have that package already installed ...
      my $pkg = $tlpobj->name;
      if ($pkg =~ m/^(.*)\.(source|doc)$/) {
        # got a .src or .doc package
        my $type = $2;
        my $mothership = $1;
        my $mothertlp = $localtlpdb->get_package($mothership);
        if (!defined($mothertlp)) {
          tlwarn("We are trying to add ${type} files to a not existing package $mothership!\n");
          tlwarn("Trying to continue!\n");
          # the best we can do is rename that package to $mothername and add it!
          $tlpobj->name($mothership);
          # add the src/docfiles tlpobj under the mothership name
          $localtlpdb->add_tlpobj($tlpobj);
        } else {
          if ($type eq "source") {
            $mothertlp->srcfiles($tlpobj->srcfiles);
            $mothertlp->srcsize($tlpobj->srcsize);
          } else {
            # must be "doc"
            $mothertlp->docfiles($tlpobj->docfiles);
            $mothertlp->docsize($tlpobj->docsize);
          }
          # that make sure that the original entry is overwritten
          $localtlpdb->add_tlpobj($mothertlp);
        }
      } else {
        # completely normal package, just add it
        $localtlpdb->add_tlpobj($tlpobj);
      }
      $localtlpdb->save;
    }
    finish(0);
  } elsif ($action =~ m/^get-mirror$/i) {
    my $loc = give_ctan_mirror();
    print "$loc\n";
    finish(0);
  } elsif ($action =~ m/^generate$/i) {
    merge_into(\%ret, action_generate());
  } elsif ($action =~ m/^gui$/i) {
    action_gui();
  } elsif ($action =~ m/^arch$/i) {
    merge_into(\%ret, action_arch());
  } elsif ($action =~ m/^option$/i) {
    merge_into(\%ret, action_option());
    finish(0);
  } elsif ($action =~ m/^list$/i) {
    merge_into(\%ret, action_list());
    finish(0);
  } elsif ($action =~ m/^check$/i) {
    merge_into(\%ret, action_check());
  } elsif ($action =~ m/^install$/i) {
    merge_into(\%ret, action_install());
  } elsif ($action =~ m/^update$/i) {
    merge_into(\%ret, action_update());
  } elsif ($action =~ m/^backup$/i) {
    merge_into(\%ret, action_backup());
  } elsif ($action =~ m/^restore$/i) {
    merge_into(\%ret, action_restore());
  } elsif ($action =~ m/^symlinks$/i) {
    merge_into(\%ret, action_symlinks());
    finish(0);
  } elsif ($action =~ m/^search$/i) {
    merge_into(\%ret, action_search());
    finish(0);
  } elsif ($action =~ m/^show$/i) {
    merge_into(\%ret, action_show());
    finish(0);
  } elsif ($action =~ m/^remove$/i) {
    merge_into(\%ret, action_remove());
  } elsif ($action =~ /^paper$/) {
    merge_into(\%ret, action_paper());
  } elsif ($action =~ m/^uninstall$/i) {
    merge_into(\%ret, action_uninstall());
    finish(0);
  } elsif ($action =~ m/^recreate-tlpdb$/i) {
    merge_into(\%ret, action_recreate_tlpdb());
    finish(0);
  } else {
    die "$0: unknown action: $action; try --help if you need it.\n";
  }

  # close the special log file
  if ($packagelogfile && !$::gui_mode) {
    info("tlmgr: package log updated at $packagelogfile\n") if $packagelogged;
    close(PACKAGELOG);
  }

  # run external programs.
  my $error_count = &handle_ret_hash(%ret);

  # done, just in case there are 256 errors.
  finish($error_count ? 1 : 0);
}



# run external programs (mktexlsr, updmap-sys, etc.) as specified by the
# keys in the RET hash.  We return the number of unsuccessful runs, zero
# if all ok.
# 
# If the "map" key is specified, the value may be a reference to a list
# of map command strings to pass to updmap, e.g., "enable Map=ascii.map".
# 
sub handle_ret_hash {
  my (%ret) = @_;
  my $errors = 0;
  
  if (exists $ret{'mktexlsr'}) {
    info("running mktexlsr\n");
    $errors += system("mktexlsr");
  }

  if (exists $ret{'map'}) {
    # We run updmap-sys instead of calling our create_updmap() function
    # (as we do for formats and languages), because many users run
    # updmap themselves (not using our local cfg feature), since that's
    # been the documented way to add fonts for many years.  If we called
    # create_updmap, such user maps would be lost.
    foreach my $m (@{$ret{'map'}}) {
      my $cmd = "updmap-sys --nomkmap --nohash --$m";
      info("running $cmd\n");
      $errors += system($cmd);
    }
    info("running updmap-sys once more\n");
    $errors += system("updmap-sys"); # runs mktexlsr.
  }

  chomp(my $TEXMFSYSVAR = `kpsewhich -var-value=TEXMFSYSVAR`);
  chomp(my $TEXMFLOCAL = `kpsewhich -var-value=TEXMFLOCAL`);

  # format means create missing formats (because a new one was added).
  # format-regenerate is used when the paper size changes.  In that
  # case, if option_create_formats is set, we simply want to generate
  # all formats; if not, then we only want to refresh existing ones.
  # 
  my $opt_fmt = $localtlpdb->option_create_formats;
  if (exists $ret{'format'}) {
    info("regenerating fmtutil.cnf in $TEXMFSYSVAR\n");
    TeXLive::TLUtils::create_fmtutil($localtlpdb,
                                     "$TEXMFSYSVAR/web2c/fmtutil.cnf",
                                     "$TEXMFLOCAL/web2c/fmtutil-local.cnf");
    if ($opt_fmt && !$ret{'format-regenerate'}) {
      info("running fmtutil-sys --missing\n");
      $errors += system("fmtutil-sys", "--missing");
    }
  }
  #
  if (exists $ret{'format-regenerate'}) {
    my $a = $opt_fmt ? "--all" : "--refresh";
    info("running fmtutil-sys $a to regenerate formats\n");
    $errors += system("fmtutil-sys", $a);
  }

  if (exists $ret{'language'}) {
    for my $ext ("dat", "def") {
      my $lang = "language.$ext";
      info("regenerating $lang\n");
      
      my $arg1 = "$TEXMFSYSVAR/tex/generic/config/language.$ext";
      my $arg2 = "$TEXMFLOCAL/tex/generic/config/language-local.dat";
      if ($ext eq "dat") {
        TeXLive::TLUtils::create_language_dat($localtlpdb, $arg1, $arg2);
      } else {
        TeXLive::TLUtils::create_language_def($localtlpdb, $arg1, $arg2);
      }

      if (! TeXLive::TLUtils::win32()) {
        # Use full path for external command, except on Windows.
        $lang = "$TEXMFSYSVAR/tex/generic/config/$lang";
      }
      if ($localtlpdb->option_create_formats) {
        info("running fmtutil-sys --byhyphen $lang\n");
        $errors += system("fmtutil-sys", "--byhyphen", $lang);
      }
    }
  }

  return $errors / 256; # we were accumulating wait statuses
}


#  REMOVE
#
# tlmgr remove foo bar baz
#   will remove the packages foo bar baz itself
#   and will remove all .ARCH dependencies, too
#   and if some of them are collections it will also remove the
#   depending packages which are NOT Collections|Schemes.
#   if some of them are referenced somewhere they will not be removed
#   unless --force given
#
# tlmgr remove --no-depends foo bar baz
#   will remove the packages foo bar baz itself without any dependencies
#   but it will still remove all .ARCH dependency
#   if some of them are referenced somewhere they will not be removed
#   unless --force given
#
# tlmgr remove --no-depends-at-all foo bar baz 
#   willabsolutely only install foo bar baz not even taking .ARCH into
#   account
#
sub action_remove {
  if ($opts{"gui"}) {
    action_gui("remove");
  }
  # we do the following:
  # - (not implemented) order collections such that those depending on
  #   other collections are first removed, and then those which only
  #   depend on packages. Otherwise
  #     remove collection-latex collection-latexrecommended
  #   will not succeed
  # - first loop over all cmd line args and consider only the collections
  # - for each to be removed collection:
  #   . check that no other collections/scheme asks for that collection
  #   . remove the collection
  #   . remove all dependencies
  # - for each normal package not already removed (via the above)
  #   . check that no collection/scheme still depends on this package
  #   . remove the package
  #
  my %ret;
  $opts{"no-depends"} = 1 if $opts{"no-depends-at-all"};
  my %already_removed;
  my @more_removal;
  init_local_db();
  info("remove: dry run, no changes will be made\n") if $opts{"dry-run"};
  my @packs = @ARGV;
  #
  # we have to be carefull not to remove too many packages. The idea is
  # as follows:
  # - let A be the set of all packages to be removed from the cmd line
  # - let A* be the set of A with all dependencies expanded
  # - let B be the set of all packages
  # - let C = B \ A*, ie the set of all packages without those packages
  #   in the set of A*
  # - let C* be the set of C with all dependencies expanded
  # - let D = A* \ C*, ie the set of all packages to be removed (A*)
  #   without all the package that are still needed (C*)
  # - remove all package in D
  # - for any package in A (not in A*, in A, ie on the cmd line) that is
  #   also in C* (so a package that was asked for to be removed on the
  #   cmd line, but it isn't because someone else asks for it), warn the
  #   user that it is still needed
  #
  # remove all .ARCH dependencies, too, unless $opts{"no-depends-at-all"}
  @packs = $localtlpdb->expand_dependencies("-only-arch", $localtlpdb, @packs) unless $opts{"no-depends-at-all"};
  # remove deps unless $opts{"no-depends"}
  @packs = $localtlpdb->expand_dependencies("-no-collections", $localtlpdb, @packs) unless $opts{"no-depends"};
  my %allpacks;
  for my $p ($localtlpdb->list_packages) { $allpacks{$p} = 1; }
  for my $p (@packs) { delete($allpacks{$p}); }
  my @neededpacks = $localtlpdb->expand_dependencies($localtlpdb, keys %allpacks);
  my %packs;
  my %origpacks;
  my @origpacks = $localtlpdb->expand_dependencies("-only-arch", $localtlpdb, @ARGV) unless $opts{"no-depends-at-all"};
  for my $p (@origpacks) { $origpacks{$p} = 1; }
  for my $p (@packs) { $packs{$p} = 1; }
  for my $p (@neededpacks) { 
    if (defined($origpacks{$p})) {
      # that package was asked for to be removed on the cmd line
      my @needed = $localtlpdb->needed_by($p);
      if ($opts{"force"}) {
        info("tlmgr: $p is needed by " . join(" ", @needed) . "\n");
        info("tlmgr: still removing it due to --force\n");
      } else {
        delete($packs{$p});
        tlwarn("tlmgr: not removing $p, needed by " .
          join(" ", @needed) . "\n");
      }
    } else {
      delete($packs{$p});
    }
  }
  @packs = keys %packs;
  foreach my $pkg (sort @packs) {
    my $tlp = $localtlpdb->get_package($pkg);
    next if defined($already_removed{$pkg});
    if (!defined($tlp)) {
      info("$pkg: package not present, cannot remove\n");
    } else {
      # in the first round we only remove collections, nothing else
      # but removing collections will remove all dependencies, too
      # save the information of which packages have already been removed
      # into %already_removed.
      if ($tlp->category eq "Collection") {
        my %foo;
        info ("remove $pkg\n");
        if ($opts{"dry-run"}) {
          # we need to set $foo to something positive otherwise
          # the rest will not be run in dry_run mode
          $foo{'mktexlsr'} = 1;
        } else {
          merge_into(\%foo, 
                     $localtlmedia->remove_package($pkg));
          logpackage("remove: $pkg");
        }
        if (keys %foo) {
          # removal was successful, so the return is at least 0x0001 mktexlsr
          # remove dependencies, too
          merge_into(\%ret, \%foo);
          $already_removed{$pkg} = 1;
        }
      } else {
        # save all the other packages into the @more_removal list to
        # be removed at the second state. Note that if a package has
        # already been removed due to a removal of a collection
        # it will be marked as such in %already_removed and not tried again
        push @more_removal, $pkg;
      }
    }
  }
  foreach my $pkg (sort @more_removal) {
    if (!defined($already_removed{$pkg})) {
      info ("remove $pkg\n");
      if (!$opts{"dry-run"}) {
        my %foo;
        merge_into(\%foo, 
                   $localtlmedia->remove_package($pkg));
        if (keys %foo) {
          # removal was successful
          logpackage("remove: $pkg");
          merge_into(\%ret, \%foo);
          $already_removed{$pkg} = 1;
        }
      }
    }
  }
  if ($opts{"dry-run"}) {
    # stop here, don't do any postinstall actions
    return;
  } else {
    $localtlpdb->save;
    my @foo = sort keys %already_removed;
    if (@foo) {
      info("tlmgr: actually removed these packages: @foo\n");
    } else {
      info("tlmgr: no packages removed.\n");
    }
    return(\%ret);
  }
}


#  PAPER
# ARGV can look like:
#   paper a4 
#   paper letter
#   [xdvi|...|context] paper [help|papersize|--list]
#
sub action_paper {
  my %ret = ();
  action_gui("config") if $opts{"gui"};

  init_local_db();
  chomp(my $texmfsysconfig = `kpsewhich -var-value=TEXMFSYSCONFIG`);
  $ENV{"TEXMFCONFIG"} = $texmfsysconfig;

  my $action = shift @ARGV;
  if ($action =~ m/^paper$/i) {  # generic paper
    my $newpaper = shift @ARGV;
    if ($opts{"list"}) {  # tlmgr paper --list => complain.
      tlwarn("tlmgr: ignoring paper setting to $newpaper with --list\n")
        if $newpaper;  # complain if they tried to set, too.
      tlwarn("tlmgr: please specify a program before paper --list, ",
             "as in: tlmgr pdftex paper --list\n");

    } elsif (!defined($newpaper)) {  # tlmgr paper => show all current sizes.
      TeXLive::TLPaper::paper_all($texmfsysconfig,undef);
      
    } elsif ($newpaper !~ /^(a4|letter)$/) {  # tlmgr paper junk => complain.
      $newpaper = "the empty string" if !defined($newpaper);
      tlwarn("tlmgr: expected `a4' or `letter' after paper, not $newpaper\n");

    } else { # tlmgr paper {a4|letter} => do it.
      merge_into(\%ret,TeXLive::TLPaper::paper_all($texmfsysconfig,$newpaper));
    }

  } else {  # program-specific paper
    my $prog = $action;     # first argument is the program to change
    my $arg = shift @ARGV;  # get "paper" argument
    if (!defined($arg) || $arg ne "paper") {
      $arg = "the empty string." if ! $arg;
      tlwarn("tlmgr: expected `paper' after $prog, not $arg\n");
      return;
    }
    # the do_paper progs check for the argument --list, so if given
    # restore it to the cmd line.
    unshift(@ARGV, "--list") if $opts{"list"};
    merge_into(\%ret, TeXLive::TLPaper::do_paper($prog,$texmfsysconfig,@ARGV));
  }
  return \%ret;
}


#  SHOW
#
sub action_show {
  if ($opts{"gui"}) {
    action_gui("config");
  }
  init_local_db();
  foreach my $pkg (@ARGV) {
    my $tlpdb = $localtlpdb;
    my $tlp = $localtlpdb->get_package($pkg);
    my $installed = 0;
    if (!$tlp) {
      if (!$tlmediatlpdb) {
        init_tlmedia();
      }
      $tlp = $tlmediatlpdb->get_package($pkg);
      $tlpdb = $tlmediatlpdb;
    } else {
      $installed = 1;
    }
    if ($tlp) {
      my @colls;
      if ($tlp->category ne "Collection" && $tlp->category ne "Scheme") {
        @colls = $localtlpdb->needed_by($pkg);
        if (!@colls) {
          # not referenced in the local tlpdb, so try the remote here, too
          if (!$tlmediatlpdb) {
            init_tlmedia();
          }
          @colls = $tlmediatlpdb->needed_by($pkg);
        }
      }
      print "Package:    ", $tlp->name, "\n";
      print "Category:   ", $tlp->category, "\n";
      print "ShortDesc:  ", $tlp->shortdesc, "\n" if ($tlp->shortdesc);
      print "LongDesc:   ", $tlp->longdesc, "\n" if ($tlp->longdesc);
      print "Installed:  ", ($installed ? "Yes" : "No"), "\n";
      print "Revision:   ", $tlp->revision, "\n" if ($installed);
      print "Collection: ", @colls, "\n" if (@colls);
      if ($opts{"list"}) {
        print "Included files, by type:\n";
        # if the package has a .ARCH dependency we also list the files for
        # those packages
        my @todo = $tlpdb->expand_dependencies("-only-arch", $tlpdb, ($pkg));
        for my $d (sort @todo) {
          my $foo = $tlpdb->get_package($d);
          if (!$foo) {
            warn "That should not happen, no such package here!";
            next;
          }
          if ($d ne $pkg) {
            print "depending package $d:\n";
          }
          if ($foo->runfiles) {
            print "run files:\n";
            for my $f (sort $foo->runfiles) { print "  $f\n"; }
          }
          if ($foo->srcfiles) {
            print "source files:\n";
            for my $f (sort $foo->srcfiles) { print "  $f\n"; }
          }
          if ($foo->docfiles) {
            print "doc files:\n";
            for my $f (sort $foo->docfiles) { 
              print "  $f"; 
              my $dfd = $foo->docfiledata;
              if (defined($dfd->{$f})) {
                for my $k (keys %{$dfd->{$f}}) {
                  print " $k=\"", $dfd->{$f}->{$k}, '"';
                }
              }
              print "\n";
            }
          }
          # in case we have them
          if ($foo->allbinfiles) {
            print "bin files (all architectures):\n";
            for my $f (sort $foo->allbinfiles) { print " $f\n"; }
          }
        }
      }
      print "\n";
    } else {
      printf STDERR "tlmgr: cannot find $pkg\n";
    }
  }
  return;
}

#  SYMLINKS
#
sub action_symlinks {
  my %ret;
  my $what = shift @ARGV;
  if (!defined($what) || ($what !~ m/^(add|remove)$/i)) {
    tlwarn("action symlinks needs one argument, either add or remove\n");
    return;
  }
  init_local_db();
  if ($what =~ m/^add$/i) {
    $localtlpdb->add_symlinks();
  } elsif ($what =~ m/^remove$/i) {
    # remove symlinks
    $localtlpdb->remove_symlinks();
  } else {
    # that should not happen
    tlwarn("that should not happen, action_symlinks what=$what\n");
    exit 1;
  }
  return;
}

#  SEARCH
#
sub action_search {
  my %ret;
  my $r = shift @ARGV;
  my $ret = "";
  my $tlpdb;
  init_local_db();
  if ($opts{"global"}) {
    init_tlmedia();
    $tlpdb = $tlmediasrc->tlpdb;
  } else {
    $tlpdb = $localtlpdb;
  }
  foreach my $pkg ($tlpdb->list_packages) {
    if ($opts{"file"}) {
      my @ret = grep(m;$r;, $tlpdb->get_package($pkg)->all_files);
      if (@ret) {
        print "$pkg:\n";
        foreach (@ret) {
          print "\t$_\n";
        }
      }
    } else {
      next if ($pkg =~ m/\./);
      my $t = $tlpdb->get_package($pkg)->shortdesc;
      $t |= "";
      my $lt = $tlpdb->get_package($pkg)->longdesc;
      $lt |= "";
      if (($pkg =~ m/$r/) || ($t =~ m/$r/) || ($lt =~ m/$r/)) {
        $ret .= " $pkg - $t\n";
      }
    }
  }
  print "$ret";
  return;
}


#  RESTORE
# 
sub action_restore {
  # tlmgr restore --backupdir dir 
  #   lists all packages with all revisions
  # tlmgr restore --backupdir dir pkg
  #   lists all revisions of pkg
  # tlmgr restore --backupdir dir pkg rev
  #   restores pkg to revision rev
  my %ret;
  # check the backup dir argument
  if ($opts{"backupdir"}) {
    my $ob = abs_path($opts{"backupdir"});
    $ob && ($opts{"backupdir"} = $ob);
    if (! -d $opts{"backupdir"}) {
      tlwarn ("backupdir argument $opts{'backupdir'} is not a directory.\n");
      tlwarn ("Don't know from where to restore backups, terminating.\n");
      exit 1;
    }
  } else {
    # no argument, check for presence in TLPDB
    init_local_db(1);
    $opts{"backupdir"} = $localtlpdb->option("backupdir");
    if (!$opts{"backupdir"}) {
      tlwarn ("Don't know from where to restore backups, terminating.\n");
      exit 1;
    }
    # we are stil here, there is something set in tlpdb
    my $ob = abs_path($opts{"backupdir"});
    $ob && ($opts{"backupdir"} = $ob);
    if (! -d $opts{"backupdir"}) {
      tlwarn ("backupdir as set in tlpdb $opts{'backupdir'} is not a directory.\n");
      tlwarn ("Don't know from where to restore backups, terminating.\n");
      exit 1;
    }
  }

  info("restore: dry run, no changes will be made\n") if $opts{"dry"};

  # initialize the hash(packages) of hash(revisions)
  my %backups;
  opendir (DIR, $opts{"backupdir"}) || die "opendir($opts{'backupdir'}) failed: $!";
  my @dirents = readdir (DIR);
  closedir (DIR) || warn "closedir($opts{'backupdir'}) failed: $!";
  for my $dirent (@dirents) {
    next if (-d $dirent);
    next if ($dirent !~ m/^(.*)\.r([0-9]+)\.tar\.xz$/);
    $backups{$1}->{$2} = 1;
  }
  my ($pkg, $rev) = @ARGV;
  if (!defined($pkg)) {
    if (keys %backups) {
      print "Available backups:\n";
      foreach my $p (sort keys %backups) {
        print "$p: ";
        my @rs = sort (keys %{$backups{$p}});
        print "@rs\n";
      }
    } else {
      print "No backups available in $opts{'backupdir'}\n";
    }
    finish(0);
  }
  if (!defined($rev)) {
    print "Available backups for $pkg: ";
    my @rs = sort (keys %{$backups{$pkg}});
    print "@rs\n";
    finish(0);
  }
  # we did arrive here, so we try to restore ...
  if (defined($backups{$pkg}->{$rev})) {
    if (!$opts{"force"}) {
      print "Do you really want to restore $pkg to revision $rev (y/N): ";
      my $yesno = <STDIN>;
      if ($yesno !~ m/^y(es)?$/i) {
        print "Ok, cancelling the restore!\n";
        finish(0);
      }
    }
    print "Restoring $pkg, $rev from $opts{'backupdir'}/${pkg}.r${rev}.tar.xz\n";
    if (!$opts{"dry"}) {
      init_local_db(1);
      # first remove the package, then reinstall it
      # this way we get rid of useless files
      $opts{"backupdir"} = abs_path($opts{"backupdir"});
      merge_into(\%ret, $localtlmedia->remove_package($pkg));
      TeXLive::TLMedia->_install_package("$opts{'backupdir'}/${pkg}.r${rev}.tar.xz" , [] ,$localtlpdb);
      logpackage("restore: $pkg ($rev)");
      # now we have to read the .tlpobj file and add it to the DB
      my $tlpobj = TeXLive::TLPOBJ->new;
      $tlpobj->from_file($localtlpdb->root . "/tlpkg/tlpobj/$pkg.tlpobj");
      $localtlpdb->add_tlpobj($tlpobj);
      merge_into(\%ret, $localtlpdb->get_package($pkg)->make_return_hash_from_executes("enable"));
      $localtlpdb->save;
    }
  } else {
    print "revision $rev for $pkg is not present in $opts{'backupdir'}\n";
  }
  return \%ret;
}

sub action_backup {
  init_local_db(1);
  # --clean argument
  # can be either -1 ... don't clean
  #               0  ... remove all backups
  #               N  ... keep only N backups
  # that parallels the value of autoclean in the configuration
  # we have to be careful, because if simply --clean is given, we should
  # check for the value saved in the tlpdb, and if that is not present
  # do nothing. 
  # We have set clean to clean:-99 which makes -99 the default value
  # if only --clean is given without any argument
  # !defined($opts{"clean"})  -> no --clean given
  # $opts{"clean"} = -99      -> --clean without argument given, check tlpdb
  # $opts{"clean"} = -1, 0, N -> --clean=N given, check argument
  #
  my $clean_mode = 0;
  $clean_mode = 1 if defined($opts{"clean"});
  if ($clean_mode) {
    if ($opts{"clean"} == -99) {
      # we need to check the tlpdb
      $opts{"clean"} = $localtlpdb->option("autobackup");
      if (!$opts{"clean"}) {
        tlwarn ("--clean given without an argument, but no default clean\n");
        tlwarn ("mode specified in the tlpdb, terminating.\n");
        exit 1;
      }
    }
    # now $opts{"clean"} is something, but maybe not a number, check for
    # validity
    if ($opts{"clean"} =~ m/^(-1|[0-9]+)$/) {
      # get rid of leading zeros etc etc
      $opts{"clean"} = $opts{"clean"} + 0;
    } else {
      tlwarn ("clean mode as specified on the command line or as given by default\n");
      tlwarn ("must be an integer larger or equal than -1, terminating.\n");
      exit 1;
    }
  }
  # check the backup dir argument
  if ($opts{"backupdir"}) {
    my $ob = abs_path($opts{"backupdir"});
    $ob && ($opts{"backupdir"} = $ob);
    if (! -d $opts{"backupdir"}) {
      tlwarn ("backupdir argument $opts{'backupdir'} is not a directory.\n");
      if ($clean_mode) {
        tlwarn ("Cannot clean a non existing directory, terminating.\n");
      } else {
        tlwarn ("Don't know where to save backups, terminating.\n");
      }
      exit 1;
    }
  } else {
    # no argument, check for presence in TLPDB
    $opts{"backupdir"} = $localtlpdb->option("backupdir");
    if (!$opts{"backupdir"}) {
      if ($clean_mode) {
        tlwarn ("Cannot clean an unknown directory, terminating.\n");
      } else {
        tlwarn ("Don't know where to save backups, terminating.\n");
      }
      exit 1;
    }
    # we are stil here, there is something set in tlpdb
    my $ob = abs_path($opts{"backupdir"});
    $ob && ($opts{"backupdir"} = $ob);
    if (! -d $opts{"backupdir"}) {
      tlwarn ("backupdir as set in tlpdb $opts{'backupdir'} is not a directory.\n");
      if ($clean_mode) {
        tlwarn ("Cannot clean a non existing directory, terminating.\n");
      } else {
        tlwarn ("Don't know where to save backups, terminating.\n");
      }
      exit 1;
    }
  }

  my %ret;
  my @todo;
  if ($opts{"all"}) {
    @todo = $localtlpdb->list_packages;
  } else {
    @todo = @ARGV;
    @todo = $localtlpdb->expand_dependencies("-only-arch", $localtlpdb, @todo);
  }
  if (!@todo) {
    printf "tlmgr backup takes either a list of packages or --all\n";
  }
  foreach my $pkg (@todo) {
    if ($clean_mode) {
      clear_old_backups ($pkg, $opts{"backupdir"}, $opts{"clean"}, $opts{"dry-run"});
    } else {
      my $tlp = $localtlpdb->get_package($pkg);
      info("saving current status of $pkg to $opts{'backupdir'}/${pkg}.r" . 
        $tlp->revision . "tar.xz\n");
      if (!$opts{"dry-run"}) {
        $tlp->make_container("xz", $localtlpdb->root, 
                             $opts{"backupdir"}, "${pkg}.r" . $tlp->revision);
      }
    }
  }
  return(\%ret);
}

#
sub write_w32_updater {
  my @w32_updated = @_;
  my $media = $tlmediasrc->media;
  my $mediatlpdb = $tlmediasrc->tlpdb;
  # we have to download/copy also the src/doc files if necessary!
  my $container_src_split = $mediatlpdb->config_src_container;
  my $container_doc_split = $mediatlpdb->config_doc_container;
  # get options about src/doc splitting from $totlpdb
  # TT: should we use local options to decide about install of doc & src?
  my $opt_src = $localtlpdb->option_install_srcfiles;
  my $opt_doc = $localtlpdb->option_install_docfiles;
  my $root = $localtlpdb->root;
  my $temp = "$root/temp";
  my $repo = $mediatlpdb->root . "/$Archive";
  TeXLive::TLUtils::mkdirhier($temp);
  tlwarn("Backup option not implemented for infrastructure update.\n") if ($opts{"backup"});
  if ($media eq 'DVD') {
    tlwarn("Creating updater from DVD currently not implemented!\n");
    tlwarn("But it should not be necessary!\n");
    return 1; # abort
  }
  my ($mediatlp, $localtlp, $opt_real_doc, $sysret, $size, $md5, $fullname,
      $oldrev, $newrev, $pkg_part, @pkg_parts, @updater_args);
  my $totalnr = $#w32_updated + 1;
  my $currnr = 0;
  foreach my $pkg (@w32_updated) {
    $mediatlp = $mediatlpdb->get_package($pkg);
    $localtlp = $localtlpdb->get_package($pkg);
    $oldrev = $localtlp->revision;
    $newrev = $mediatlp->revision;
    # we do install documenation files for category Documentation even if option_install_docfiles is false
    $opt_real_doc = ($mediatlp->category =~ m/documentation/i) ? 1 : $opt_doc;
    @pkg_parts = ($pkg);
    push(@pkg_parts, "$pkg.source") if ($container_src_split && $opt_src && $mediatlp->srcfiles);
    push(@pkg_parts, "$pkg.doc") if ($container_doc_split && $opt_real_doc && $mediatlp->docfiles);
    $currnr++;
    if ($opts{"dry-run"}) {
      info("[$currnr/$totalnr] update: $pkg ($oldrev -> $newrev)\n");
      next;
    }
    # create backup; make_container expects file name in a format: some-name.r[0-9]+
    ($size, $md5, $fullname) = $localtlp->make_container("tar", $root, $temp, "__BACKUP_$pkg.r$oldrev");
    if ($size <= 0) {
      tlwarn("Creation of backup container of $pkg failed.\n");
      return 1; # backup failed? abort
    }
    foreach $pkg_part (@pkg_parts) {
      if ($media eq 'CD') {
        copy("$repo/$pkg_part.tar.xz", "$temp");
      } else { # net
        TeXLive::TLUtils::download_file("$repo/$pkg_part.tar.xz", "$temp/$pkg_part.tar.xz");
      }
      # now we should have the file present
      if (!-r "$temp/$pkg_part.tar.xz") {
        tlwarn("Couldn't get $pkg_part.tar.xz, that is bad\n");
        return 1; # abort
      } 
      # unpack xz archive
      $sysret = system("$::progs{'xzdec'} < \"$temp/$pkg_part.tar.xz\" > \"$temp/$pkg_part.tar\"");
      if ($sysret) {
        tlwarn("Couldn't unpack $pkg_part.tar.xz\n");
        return 1; # unpack failed? abort
      }
      unlink("$temp/$pkg_part.tar.xz"); # we don't need that archive anymore
      push @updater_args, $pkg_part, $oldrev, $newrev;
    }
  }
  return 0 if ($opts{"dry-run"}); # nothing else to do
  # write the updater script file
  open UPDATER, ">$root/temp/run-updater-w32" or die "Cannot create updater: $!";
  print UPDATER '
@echo off
setlocal enableextensions
(set errorlevel=)
(set guimode=' . $::gui_mode . ')
>nul copy /y "%~dp0..\texmf\scripts\texlive\updater-w32.bat" "%~dp0updater-w32.bat"
rem Execute with cmd.exe to avoid problems if 4NT is used
rem Keep files write open during execution to signal running state
cmd.exe /c call "%~dp0updater-w32.bat" ' . join(' ',@updater_args) . ' 8^>^>"%~f0" 9^>^>"%~dp0updater-w32.bat"
rem Self-delete without error message about missing file
(if not errorlevel 1 start /b cmd.exe /c del "%~f0" "%~dp0updater-w32.bat") & exit /b %errorlevel%
';
  close UPDATER;
  return 0;
}


#  UPDATE
#
# tlmgr update foo
#   if foo is of type Package|Documentation it will update only foo
#     and the respective .ARCH dependencies
#   if foo is of type Collection|Scheme it will update itself AND
#     will check all depending packs of type NOT(COllection|Scheme)
#     for necessary updates
#
# tlmgr update --no-depends foo
#   as above, but will not check for depends of Collections/Schemes
#   but it will still update .ARCH deps
#
# tlmgr update --no-depends-at-all foo
#   will absolutely only update foo not even taking .ARCH into account
# 
# TLMedia->install_package INSTALLS ONLY ONE PACKAGE, no deps whatsoever
# anymore. That has all to be done by hand.
# 
sub machine_line {
  my ($flag1) = @_;
  my $ret = 0;
  if ($flag1 eq "-ret") {
    $ret = 1;
    shift;
  }
  my ($pkg, $flag, $lrev, $rrev, @args) = @_;
  $lrev ||= "-";
  $rrev ||= "-";
  $flag ||= "?";
  my $str = "$pkg\t$flag\t$lrev\t$rrev\t" . join("\t", @args) . "\n";
  return($str) if $ret;
  print $str;
}

sub action_update {
  if ($opts{"gui"}) {
    action_gui("update");
  }
  init_local_db(1);
  $opts{"no-depends"} = 1 if $opts{"no-depends-at-all"};
  my %ret;
  
  init_tlmedia();
  info("update: dry run, no changes will be made\n") if $opts{"dry-run"};

  # if the update is not for one of the critical packages then do
  # check for updates to tlmgr and die unless either --force or --list
  # is given
  my $other_updates_asked_for = 0;
  if ($opts{"all"}) {
    $other_updates_asked_for = 1;
  } else {
    for my $p (@ARGV) {
      my $matched = 0;
      for my $cp (@CriticalPackagesList) {
        # we match for initial package name, that shold be fine
        if ($p =~ m/^$cp/) {
          $matched = 1;
          last;
        }
      }
      if (!$matched) {
        $other_updates_asked_for = 1;
        last;
      }
    }
  } 
  if ($other_updates_asked_for) {
    if (check_for_critical_updates($localtlpdb, $tlmediatlpdb)) {
      if ($opts{"force"}) {
        tlwarn("$0: Continuing due to --force.\n");
      } elsif ($opts{"list"}) {
        # do not warn here
      } else {
        if ($::gui_mode) {
          # return here and don't do any updates
          return;
        } else {
          die "$0: Exiting, please read above warning.\n";
        }
      }
    }
  }

  # do backup dir checking now so that we don't run into troubles
  # later, and exit if that doesn't work
  if ($opts{"backupdir"}) {
    $opts{"backupdir"} = abs_path($opts{"backupdir"});
    if (! -d $opts{"backupdir"}) {
      tlwarn("Argument for --backupdir must be an existing directory. Terminating.\n");
      exit 1;
    }
  }

  my $autobackup = 0;
  # check for the tlpdb option autobackup, and if present and true (!= 0)
  # assume we are doing backups
  if (!$opts{"backup"}) {
    $autobackup = $localtlpdb->option("autobackup");
    if ($autobackup) {
      # check the format, we currently allow only natural numbers, and -1
      if ($autobackup eq "-1") {
        debug ("Automatic backups activated, keeping \\infty backups.\n");
        $opts{"backup"} = 1;
      } elsif ($autobackup eq "0") {
        debug ("Automatic backups disabled.\n");
      } elsif ($autobackup =~ m/^[0-9]+$/) {
        debug ("Automatic backups activated, keeping $autobackup backups.\n");
        $opts{"backup"} = 1;
      } else {
        tlwarn ("Option autobackup can only be an integer >= -1.\n");
        tlwarn ("Disabling auto backups.\n");
        $localtlpdb->option("autobackup", 0);
        $autobackup = 0;
      }
    }
  }

  # cmd line --backup, we check for --backupdir, and if that is not given
  # we try to get the default from the tlpdb. If that doesn't work, exit.
  if ($opts{"backup"}) {
    my $diebackupdir = 0;
    if (!$opts{"backupdir"}) {
      $opts{"backupdir"} = $localtlpdb->option("backupdir");
      if ($opts{"backupdir"}) {
        # check again:
        $opts{"backupdir"} = abs_path($opts{"backupdir"});
        $diebackupdir = 1 if (! -d $opts{"backupdir"});
      } else {
        # not set in the tlpdb, and not set on cmd line, but asked for
        # --backup 
        $diebackupdir = 1;
      }
    } 
    # no else branch necessary, we already checked that --backupdir if 
    # given is ok, see above
    if ($diebackupdir) {
      tlwarn("You have asked for backups, but the backup directory as specified\n");
      tlwarn("in the local TLPDB or the cmd line does not exists, exiting.\n");
      exit 1;
    }
  }

  # finally, if we have --backupdir, but no --backup, just enable it
  $opts{"backup"} = 1 if $opts{"backupdir"};
      
  debug("Doing backups to $opts{'backupdir'}\n") if $opts{"backup"};

  # these two variables are used throughout this function
  my $root = $localtlpdb->root;
  my $temp = "$root/temp";

  # remove old _BACKUP packages that have piled up in temp
  # they can be recognized by their name starting with __BACKUP_
  for my $f (<$temp/__BACKUP_*>) {
    unlink($f) unless $opts{"dry-run"};
  }

  my @todo;
  my %removals;
  my %forcermpkgs;
  my %newpkgs;
  # check for new/removed/forcibly removed packages.
  # we start from the list of installed collections in the local tlpdb
  # which are also present in the remote database
  # and expand this list once with expand_dependencies in the local tlpdb
  # and once in the tlmedia tlpdb. Then we compare the lists
  # let A = set of local expansions
  #     B = set of remote expansions
  # then we should(?) have
  #     B \ A  set of new packages
  #     A \ B  set of packages removed on the server
  #     A \cup B set of packages which should be checked for forcible removal
  #
  my @colls = ();
  for my $p ($localtlpdb->collections) {
    push @colls, $p
      if defined($tlmediatlpdb->get_package($p));
  }
  my @localexpansion = 
    $localtlpdb->expand_dependencies($localtlpdb, @colls);
  my @remoteexpansion = 
    $tlmediatlpdb->expand_dependencies($localtlpdb, @colls);
  for my $p (@remoteexpansion) {
    $newpkgs{$p} = 1;
  }
  for my $p (@localexpansion) {
    delete($newpkgs{$p});
    $removals{$p} = 1;
  }
  for my $p (@remoteexpansion) {
    delete($removals{$p});
  }
  for my $p (@localexpansion) {
    # intersection, don't check A\B and B\A
    next if $newpkgs{$p};
    next if $removals{$p};
    my $tlp = $localtlpdb->get_package($p);
    if (!defined($tlp)) {
      $forcermpkgs{$p} = 1;
    }
  }
  debug ("tlmgr: new pkgs: " . join("\n\t",keys %newpkgs) . "\n");
  debug ("tlmgr: deleted : " . join("\n\t",keys %removals) . "\n");
  debug ("tlmgr: forced  : " . join("\n\t",keys %forcermpkgs) . "\n");

  if ($opts{"all"} || $opts{"list"}) {
    @todo = $localtlpdb->list_packages;
  } else {
    @todo = @ARGV;
  }
  # don't do anything if we have been invoced in a strange way
  if (!@todo) {
    tlwarn("tlmgr update: please specify a list of packages or --all.\n");
  }

  # update all .ARCH dependencies, too, unless $opts{"no-depends-at-all"}:
  @todo = $tlmediatlpdb->expand_dependencies("-only-arch", $localtlpdb, @todo)
    unless $opts{"no-depends-at-all"}; 
  #
  # update general deps unless $opts{"no-depends"}:
  @todo = $tlmediatlpdb->expand_dependencies("-no-collections",$localtlpdb,@todo)
    unless $opts{"no-depends"}; 

  #
  # we first collect the list of packages to be actually updated or installed
  my @updated;
  my @w32_updated; # win32 special packages that need the updater batch script
  my @new;
  my @addlines;

  foreach my $pkg (sort @todo) {
    next if ($pkg =~ m/^00texlive/);
    my $tlp = $localtlpdb->get_package($pkg);
    if (!defined($tlp)) {
      # if the user has forcibly removed (say) bin-makeindex, then the
      # loop above has no way to add bin-makeindex.ARCH into the
      # %forcermpkgs hash, but the .ARCH will still be in the dependency
      # expansion.  So try both with and without the .ARCH extension.
      (my $pkg_noarch = $pkg) =~ s/\.[^.]*$//;
      my $forcerm_coll = $forcermpkgs{$pkg} || $forcermpkgs{$pkg_noarch};
      
      # similarly for new packages.  If latexmk is new, latexmk.ARCH
      # will be in the dependency expansion, and we want it.
      my $newpkg_coll = $newpkgs{$pkg} || $newpkgs{$pkg_noarch};
      if ($forcerm_coll) {
        if ($::machinereadable) {
          # TODO should we add a revision number
          push @addlines, 
            machine_line("-ret", $pkg, $FLAG_FORCIBLE_REMOVED, "-", "-", "-");
        } else {
          info("skipping forcibly removed package $pkg\n");
        }
        next;
      } elsif ($newpkg_coll) {
        # do nothing here, it will be reported below.
      } elsif (defined($removals{$pkg})) {
        # skipping this package, it has been removed due to server removal
        # and has already been removed
        next;
      } else {
        tlwarn("\ntlmgr: $pkg mentioned, neither new nor forcibly removed\n");
        next;
      }
      # install new packages
      my $mediatlp = $tlmediatlpdb->get_package($pkg);
      if (!defined($mediatlp)) {
        tlwarn("\nShould not happen: $pkg not found in $location\n");
        next;
      }
      my $mediarev = $mediatlp->revision;
      push @new, $pkg;
      next;
    }
    my $rev = $tlp->revision;
    my $mediatlp = $tlmediatlpdb->get_package($pkg);
    if (!defined($mediatlp)) {
      debug("$pkg cannot be found in $location\n");
      next;
    }
    my $mediarev = $mediatlp->revision;
    if ($rev < $mediarev) {
      if (win32() && !$opts{"list"} && ($pkg =~ m/$WinSpecialUpdatePackagesRegexp/)) {
        # w32 specials are treated as other packages for --list
        push @w32_updated, $pkg;
      } else {
        push @updated, $pkg;
      }
    } elsif ($rev > $mediarev) {
      if ($::machinereadable) {
        push @addlines,
          machine_line("-ret", $pkg, $FLAG_REVERSED_UPDATE, $rev, $mediarev, "-");
      } else {
        info("$pkg: local revision ($rev) is newer than revision in $location"
            . " ($mediarev), not updating.\n");
      }
    }
  }
  for my $i (sort @new) {
    debug("$i new package\n");
  }
  for my $i (sort @updated) {
    debug("$i upd package\n");
  }

  # number calculation
  # without w32 special packages, those are dealt with in the updater batch script
  my $totalnr = $#new + $#updated + 2; 
  my $nrupdated = 0;
  my $currnr = 1;
  # sizes_of_packages returns the sizes of *all* packages if nothing
  # is passed over, so if @new and @updated both are empty we will 
  # get something wrong back, namely the total size of all packages
  my %sizes;
  #  for size calculation we also want w32 special packages
  if (@new || @updated || @w32_updated) {
    %sizes = %{$tlmediatlpdb->sizes_of_packages( 
      $localtlpdb->option_install_srcfiles,
      $localtlpdb->option_install_docfiles,
      @new, @updated, @w32_updated)};
  } else {
    $sizes{'__TOTAL__'} = 0;
  }

  print "total-bytes\t$sizes{'__TOTAL__'}\n" if $::machinereadable;
  print "end-of-header\n" if $::machinereadable;

  # print deferred machine-readable lines after the header
  for (@addlines) { print; }

  #
  # we have to remove all the stuff before we install other packages
  # to support moving of files from one package to another.
  # remove the packages that have disappeared:
  for my $p (keys %removals) {
    if ($opts{"no-remove"}) {
      info("not removing $p due to -no-remove (removed on server)\n");
    } else {
      if ($::machinereadable) {
        # TODO version numbers
        machine_line($p, $FLAG_REMOVE, "-", "-", "-");
      } else {
        info("remove $p (removed on server)\n");
      }
    }
    if (!($opts{"dry-run"} or $opts{"list"} or $opts{"no-remove"})) {
      merge_into(\%ret, $localtlmedia->remove_package($p));
      logpackage("remove: $p");
    }
  }


  # install all the new packages first
  for my $pkg (sort @new) {
    # install new packages
    my $mediatlp = $tlmediatlpdb->get_package($pkg);
    if (!defined($mediatlp)) {
      tlwarn("\nShould not happen: $pkg not found in $location\n");
      next;
    }
    my $mediarev = $mediatlp->revision;
    if ($::machinereadable) {
      machine_line($pkg, $FLAG_AUTOINSTALL, "-", $mediatlp->revision, $sizes{$pkg});
    } else {
      info("[$currnr/$totalnr] auto-install: $pkg\n");
    }
    $currnr++;
    next if ($opts{"dry-run"} || $opts{"list"});
    my $foo = $tlmediasrc->install_package($pkg, $localtlpdb);
    if (ref $foo) {
      # installation succeeded because we got a reference
      merge_into (\%ret, $foo);
      logpackage("auto-install new: $pkg ($mediarev)");
      $nrupdated++;
    } else {
      tlwarn("$0: couldn't install new package $pkg\n");
    }
  }

  #
  foreach my $pkg (sort @updated) {
    next if ($pkg =~ m/^00texlive/);
    my $tlp = $localtlpdb->get_package($pkg);
    # we checked already that this package is present!
    my $unwind_package;
    my $remove_unwind_container = 0;
    my $rev = $tlp->revision;
    my $mediatlp = $tlmediatlpdb->get_package($pkg);
    if (!defined($mediatlp)) {
      debug("$pkg cannot be found in $location\n");
      next;
    }
    my $mediarev = $mediatlp->revision;
    $nrupdated++;
    if ($opts{"list"}) {
      if ($::machinereadable) {
        machine_line($pkg, $FLAG_UPDATE, $rev, $mediarev, $sizes{$pkg});
      } else {
        info("$pkg: local: $rev, source: $mediarev\n");
      }
      next;
    }
    
    if ($opts{"backup"} && !$opts{"dry-run"}) {
      $tlp->make_container("xz", $root, 
                           $opts{"backupdir"}, "${pkg}.r" . $tlp->revision);
      $unwind_package = 
        "$opts{'backupdir'}/${pkg}.r" . $tlp->revision . ".tar.xz";

      if ($autobackup) {
        # in case we do auto backups we remove older backups
        clear_old_backups($pkg, $opts{"backupdir"}, $autobackup);
      }
    }

    if ($::machinereadable) {
      machine_line($pkg, $FLAG_UPDATE, $rev, $mediarev, $sizes{$pkg});
    } else {
      info("[$currnr/$totalnr] update: $pkg ($rev -> $mediarev)");
    }
    $currnr++;
    if ($opts{"dry-run"}) {
      info("\n") unless $::machinereadable;
      next;
    } else {
      info(" ... ") unless $::machinereadable;  # more to come
    }

    if (!$unwind_package) {
      # no backup was made, so let us create a temporary .tar file
      # of the package
      my $tlp = $localtlpdb->get_package($pkg);
      my ($s, $m, $fullname) = $tlp->make_container("tar", $root, $temp, 
                                    "__BACKUP_${pkg}.r" . $tlp->revision);
      if ($s <= 0) {
        tlwarn("\n$0: Creation of backup container of $pkg failed.\n");
        tlwarn("Continuing to update other packages, please retry...\n");
        # we should try to update other packages at least
        next;
      }
      $remove_unwind_container = 1;
      $unwind_package = "$fullname";
    }
    # first remove the package, then reinstall it
    # this way we get rid of useless files
    # force the deinstallation since we will reinstall it
    #
    # the remove_package should also remove empty dirs in case
    # a dir is changed into a file
    merge_into(\%ret, $localtlmedia->remove_package($pkg));
    my $foo = $tlmediasrc->install_package($pkg, $localtlpdb);
    if (ref $foo) {
      # installation succeeded because we got a reference
      merge_into (\%ret, $foo);
      logpackage("update: $pkg ($rev -> $mediarev)");
      unlink($unwind_package) if $remove_unwind_container;
    } else {
      # install_package returned a scalar, so error.
      # now in fact we should do some cleanup, removing files and
      # dirs from the new package before re-installing the old one.
      # TODO
      logpackage("failed update: $pkg ($rev -> $mediarev)");
      tlwarn("\n\nInstallation of new version of $pkg did fail, trying to unwind.\n");
      if (win32()) {
        # w32 is notorious for not releasing a file immediately
        # we experienced permission denied errors 
        my $newname = $unwind_package;
        $newname =~ s/__BACKUP/___BACKUP/;
        copy ("-f", $unwind_package, $newname);
        # try to remove the file if has been created by us
        unlink($unwind_package) if $remove_unwind_container;
        # and make sure that the temporary file is removed in any case
        $remove_unwind_container = 1;
        $unwind_package = $newname;
      }
      my $instret = TeXLive::TLMedia->_install_package("$unwind_package",
                                                       [], $localtlpdb);
      if ($instret) {
        # now we have to include the tlpobj
        my $tlpobj = TeXLive::TLPOBJ->new;
        $tlpobj->from_file($root . "/tlpkg/tlpobj/$pkg.tlpobj");
        $localtlpdb->add_tlpobj($tlpobj);
        $localtlpdb->save;
        logpackage("restore: $pkg ($rev)");
        tlwarn("Restoring old package state succeeded.\n");
      } else {
        logpackage("failed restore: $pkg ($rev)");
        tlwarn("Restoring of old package did NOT succeed.\n");
        tlwarn("Most likely repair: run tlmgr install $pkg and hope.\n");
      }
      unlink($unwind_package) if $remove_unwind_container;
    }
    info("done\n") unless $::machinereadable;
  }
  
  if (win32() && !$opts{"list"} && @w32_updated) {
    info("Preparing TeX Live infrastructure update...\n");
    my $ret = write_w32_updater(@w32_updated);
    tlwarn ("Aborting infrastructure update.\n") if ($ret);
  }
  
  if (!(@new || @updated || @w32_updated) && ($tlmediasrc->media ne "NET") && $opts{"all"}) {
    # for all but net updates we warn if nothing is updated
    tlwarn("\nNothing to update.\n");
    tlwarn("\nYour installation is set up to look on the disk for updates.\n");
    tlwarn("To install from the Internet for this one time only, run\n");
    tlwarn("  tlmgr -location $TeXLiveURL\n");
    tlwarn("\nTo change the default for all future updates, run\n");
    tlwarn("  tlmgr option location $TeXLiveURL\n\n");
  }
  return(\%ret);
}


#  INSTALL
#
# tlmgr install foo bar baz
#   will create the closure under dependencies of {foo,bar,baz}, i.e. all
#   dependencies recursively down to the last package, and install all
#   the packages that are necessary
#
# tlmgr install --no-depends foo bar baz
#   will *only* install these three packages (if they are not already installed
#   but it will STILL INSTALL foo.ARCH if they are necessary.
#
# tlmgr install --no-depends-at-all foo bar baz
#   will absolutely only install these three packages, and will not even
#   take .ARCH deps into account
#
# tlmgr install --reinstall ...
#   behaves exactely like without --reinstall BUT the following two 
#   differences:
#   . dependencies are not expanded from collection to collection, so
#     if you reinstall a collection then all its dependencies of type
#     Package will be reinstalled, too, but not the dependencies on
#     other collection, because that would force the full reinstallation
#     of the whole installation
#   . it does not care for whether a package seems to be installed or
#     not (that is the --reinstall)
#
# TLMedia->install_package does ONLY INSTALL ONE PACKAGE, no deps whatsoever
# anymore!  That has all to be done by hand.
# 
sub action_install {
  if ($opts{"gui"}) {
    action_gui("install");
  }
  init_local_db(1);
  # initialize the TLMedia from $location
  $opts{"no-depends"} = 1 if $opts{"no-depends-at-all"};
  my %ret;
  init_tlmedia();

  # check for updates to tlmgr itself, and die unless --force is given
  if (check_for_critical_updates( $localtlpdb, $tlmediatlpdb)) {
    if ($opts{"force"}) {
      tlwarn("Continuing due to --force\n");
    } else {
      if ($::gui_mode) {
        # return here and don't do any updates
        return;
      } else {
        die "tlmgr: Not continuing, please see warning above!\n";
      }
    }
  }

  $opts{"no-depends"} = 1 if $opts{"no-depends-at-all"};
  info("install: dry run, no changes will be made\n") if $opts{"dry-run"};

  my @packs = @ARGV;
  # first expand the .ARCH dependencies unless $opts{"no-depends-at-all"}
  @packs = $tlmediatlpdb->expand_dependencies("-only-arch", $localtlpdb, @ARGV) unless $opts{"no-depends-at-all"};
  # now expand all others unless $opts{"no-depends"}
  # if $opts{"reinstall"} do not collection->collection dependencies
  if ($opts{"reinstall"}) {
    @packs = $tlmediatlpdb->expand_dependencies("-no-collections", $localtlpdb, @packs) unless $opts{"no-depends"};
  } else {
    @packs = $tlmediatlpdb->expand_dependencies($localtlpdb, @packs) unless $opts{"no-depends"};
  }
  foreach my $pkg (sort @packs) {
    my $re = "";
    if (defined($localtlpdb->get_package($pkg))) {
      if ($opts{"reinstall"}) {
        $re = "re";
      } else {
        debug("already installed: $pkg\n");
        next;
      }
    }
    info("${re}install: $pkg\n");
    if (!$opts{"dry-run"}) {
      merge_into(\%ret, $tlmediasrc->install_package($pkg, $localtlpdb));
      logpackage("${re}install: $pkg");
    }
  }
  if ($opts{"dry-run"}) {
    # stop here, don't do any postinstall actions
    return(0);
  }
  return(\%ret);
}

sub action_list {
  init_local_db();
  # make sure that the @ARGV hash is not changed in case we switch to 
  # show mode
  my ($what) = @ARGV;
  if ($what) {
    # if the argument to list is either 'collection' or 'scheme'
    # we list them, otherwise we go direct into tlmgr show $pkg mode
    if ($what !~ m/^(collection|scheme)/i) {
      tlwarn("(switching to show mode)\n");
      action_show();
      return;
    }
  } else {
    $what = "";
  }
  init_tlmedia();
  my @whattolist;
  if ($what =~ m/^collection/i) {
    @whattolist = $tlmediasrc->tlpdb->collections;
  } elsif ($what =~ m/^scheme/i) {
    @whattolist = $tlmediasrc->tlpdb->schemes;
  } else {
    @whattolist = $tlmediasrc->tlpdb->list_packages;
  }
  foreach (@whattolist) {
    if (defined($localtlpdb->get_package($_))) {
      print "i ";
    } else {
      print "  ";
    }
    my $foo = $tlmediasrc->tlpdb->get_package($_)->shortdesc;
    print "$_: ", defined($foo) ? $foo : "(shortdesc missing)" , "\n";
  }
  return;
}


#  OPTION
# 
sub action_option {
  if ($opts{"gui"}) {
    action_gui("config");
  }
  my $what = shift @ARGV;
  $what = "show" unless defined($what);
  init_local_db();
  if ($what =~ m/^show$/i) {
    print "Default installation location (location):          ",
          $localtlpdb->option_location, "\n";
    print "Create formats on installation (formats):          ", 
          ($localtlpdb->option_create_formats ? "yes" : "no"), "\n";
    print "Install documentation files (docfiles):            ",
          ($localtlpdb->option_install_docfiles ? "yes": "no"), "\n";
    print "Install source files (srcfiles):                   ",
          ($localtlpdb->option_install_srcfiles ? "yes": "no"), "\n";
    print "Destination for symlinks for binaries (sys_bin):   ",
          $localtlpdb->option_sys_bin, "\n"
      if $localtlpdb->option_sys_bin;
    print "Destination for symlinks for man pages (sys_man):  ",
          $localtlpdb->option_sys_man, "\n"
      if $localtlpdb->option_sys_man;
    print "Destination for symlinks for info docs (sys_info): ",
          $localtlpdb->option_sys_info, "\n"
      if $localtlpdb->option_sys_info;
    print "Directory for backups (backupdir):                 ",
          $localtlpdb->option("backupdir"), "\n"
      if $localtlpdb->option("backupdir");
    print "Number of backups to keep (autobackup):            ",
          $localtlpdb->option("autobackup"), "\n"
      if $localtlpdb->option("autobackup");
  } elsif ($what =~ m/^location$/i) {
    # changes the default location
    my $loc = shift @ARGV;
    if ($loc) {
      # support "ctan" on the cmd line, and don't abs_path it!
      if ($loc =~ m/^ctan$/i) {
        $loc = "$TeXLive::TLConfig::TeXLiveURL";
      }
      if ($loc !~ m!^(http|ftp)://!i) {
        # seems to be a local path, try to normalize it
        my $testloc = abs_path($loc);
        # however, if we were given a url, that will get "normalized" to the
        # empty string, it not being a path.  Restore the original value if so.
        $loc = $testloc if $testloc;
      }
      info("tlmgr: setting default installation location to $loc\n");
      $localtlpdb->option_location($loc);
      $localtlpdb->save;
    } else {
      info("Default installation location: ",
           $localtlpdb->option_location, "\n");
    }
  } elsif ($what =~ m/^docfiles$/i) {
    # changes the default docfiles
    my $loc = shift @ARGV;
    if (defined($loc)) {
      info("tlmgr: defaulting to", ($loc ? "" : " not"),
           " installing documentation files.\n");
      $localtlpdb->option_install_docfiles($loc);
      $localtlpdb->save;
    } else {
      info("Install documentation files: ",
           $localtlpdb->option_install_docfiles, "\n");
    }
  } elsif ($what =~ m/^srcfiles$/i) {
    # changes the default srcfiles
    my $loc = shift @ARGV;
    if (defined($loc)) {
      info("tlmgr: defaulting to", ($loc ? "" : " not"),
           " installing source files.\n");
      $localtlpdb->option_install_srcfiles($loc);
      $localtlpdb->save;
    } else {
      info("Install source files: ",
           $localtlpdb->option_install_srcfiles, "\n");
    }
  } elsif ($what =~ m/^formats$/i) {
    # changes the default formats
    my $loc = shift @ARGV;
    if (defined($loc)) {
      info("tlmgr: defaulting to", ($loc ? "" : " not"),
           " generating format files on installation.\n");
      $localtlpdb->option_create_formats($loc);
      $localtlpdb->save;
    } else {
      info("Create formats on installation: ",
            $localtlpdb->option_create_formats, "\n");
    }
  } elsif ($what =~ m/^sys_man$/i) {
    # changes the default sys_man
    my $loc = shift @ARGV;
    if (defined($loc)) {
      info("tlmgr: setting destination for symlinks to man pages to $loc\n");
      $localtlpdb->option_sys_man ($loc);
      $localtlpdb->save;
    } else {
      info("Destination for symlinks to man pages: ",
           $localtlpdb->option_sys_man, "\n");
    }
  } elsif ($what =~ m/^sys_info$/i) {
    # changes the default sys_info
    my $loc = shift @ARGV;
    if (defined($loc)) {
      info("tlmgr: setting destination for symlinks to info pages to $loc\n");
      $localtlpdb->option_sys_info ($loc);
      $localtlpdb->save;
    } else {
      info("Default destination for symlinks to info pages: ",
           $localtlpdb->option_sys_info, "\n");
    }
  } elsif ($what =~ m/^sys_bin$/i) {
    # changes the default sys_bin
    my $loc = shift @ARGV;
    if (defined($loc)) {
      info("tlmgr: setting destination for symlinks to binaries to $loc\n");
      $localtlpdb->option_sys_bin ($loc);
      $localtlpdb->save;
    } else {
      info("Default destination for symlinks to binaries: ",
           $localtlpdb->option_sys_bin, "\n");
    }
  } elsif ($what =~ m/^backupdir$/i) {
    my $val = shift @ARGV;
    if (defined($val)) {
      info("tlmgr: setting option $what to $val.\n");
      if (! -d $val) {
        info("tlmgr: the directory $val does not exists, it has to be created\n");
        info("tlmgr: before backups can be done automatically.\n");
      }
      $localtlpdb->option($what,$val);
      $localtlpdb->save;
    } else {
      info("Option $what = ", $localtlpdb->option($what), "\n");
    }
  } elsif ($what =~ m/^autobackup$/i) {
    my $val = shift @ARGV;
    if (defined($val)) {
      if ($val =~ m/-?[0-9]+/) {
        if ($val < -1) {
          tlwarn "Value $val for autobackup not supported, must be >= -1\n";
          return;
        }
      } elsif ($val =~ m/^infty$/i) {
        $val = -1;
      } else {
        tlwarn "Value $val for autobackup not supported, must be >= -1\n";
        return;
      }
      info("tlmgr: setting option $what to $val");
      $localtlpdb->option($what,$val);
      $localtlpdb->save;
    } else {
      $val = $localtlpdb->option($what);
      info("Option $what = $val");
    }
    if ($val == -1) {
      info(" (make and keep infinitely many backups)\n");
    } elsif ($val == 0) {
      info(" (don't keep any backups)\n");
    } else {
      info(" (make and keep $val backups)\n");
    }
  } elsif (member($what, @AllowedConfigOptions)) {
    # for all further options not handled above, we check whether they
    # appear in the list of allowed options; if they do, we set/read
    # the values, otherwise we warn.
    my $val = shift @ARGV;
    if (defined($val)) {
      info("tlmgr: setting option $what to $val.\n");
      $localtlpdb->option($what,$val);
      $localtlpdb->save;
    } else {
      info("Option $what = ", $localtlpdb->option($what), "\n");
    }
  } else {
    tlwarn "Option $what is currently not supported.\n";
  }
  return;
}


#  ARCH
#
sub action_arch {
  if ($^O=~/^MSWin(32|64)$/i) {
    warn("action `arch' not supported on Windows\n");
  }
  if ($opts{"gui"}) {
    action_gui("arch");
  }
  my %ret;
  my $what = shift @ARGV;
  init_local_db(1);
  info("arch: dry run, no changes will be made\n") if $opts{"dry-run"};
  $what || ($what = "list");
  if ($what =~ m/^list$/i) {
    # list the available architectures
    # initialize the TLMedia from $location
    init_tlmedia();
    my $mediatlpdb = $tlmediasrc->tlpdb;
    my @already_installed_arch = $localtlpdb->available_architectures;
    print "Available architectures:\n";
    foreach my $a ($mediatlpdb->available_architectures) {
      if (member($a,@already_installed_arch)) {
        print "(i) $a\n";
      } else {
        print "    $a\n";
      }
    }
    print "Already installed architectures are marked with (i)\n";
    print "You can add new architectures with tlmgr arch add arch1 arch2\n";
    finish(0);
  } elsif ($what =~ m/^add$/i) {
    init_tlmedia();
    my $mediatlpdb = $tlmediasrc->tlpdb;
    my @already_installed_arch = $localtlpdb->available_architectures;
    my @available_arch = $mediatlpdb->available_architectures;
    my @todoarchs;
    foreach my $a (@ARGV) {
      if (TeXLive::TLUtils::member($a, @already_installed_arch)) {
        print "Arch $a is already installed\n";
        next;
      }
      if (!TeXLive::TLUtils::member($a, @available_arch)) {
        print "Arch $a not available, use 'tlmgr available_archs'!\n";
        next;
      }
      push @todoarchs, $a;
    }
    foreach my $pkg ($localtlpdb->list_packages) {
      next if ($pkg =~ m/^00texlive/);
      my $tlp = $localtlpdb->get_package($pkg);
      foreach my $dep ($tlp->depends) {
        if ($dep =~ m/^(.*)\.ARCH$/) {
          # we have to install something
          foreach my $a (@todoarchs) {
            if ($opts{"dry-run"}) {
              info("Installing $pkg.$a\n");
            } else {
              info("install: $pkg.$a\n");
              merge_into(\%ret, $tlmediasrc->install_package("$pkg.$a", $localtlpdb));
            }
          }
        }
      }
    }
    if (TeXLive::TLUtils::member('win32', @todoarchs)) {
      # install the necessary w32 stuff
      info("install: bin-tlperl.win32\n");
      merge_into (\%ret, $tlmediasrc->install_package("bin-tlperl.win32", $localtlpdb));
      info("install: bin-tlgs.win32\n");
      merge_into (\%ret, $tlmediasrc->install_package("bin-tlgs.win32", $localtlpdb));
      info("install: bin-tlpsv.win32\n");
      merge_into (\%ret, $tlmediasrc->install_package("bin-tlpsv.win32", $localtlpdb));
    }
    # update the option_archs list of installed archs
    my @larchs = $localtlpdb->option_available_architectures;
    push @larchs, @todoarchs;
    $localtlpdb->option_available_architectures(@larchs);
    $localtlpdb->save;
  } else {
    die "Unknown option for arch: $what";
  }
  return(\%ret);
}


#  GENERATE
# 
sub action_generate {
  my $dest = defined($opts{"dest"}) ? $opts{"dest"} : "";
  my $localconf = defined($opts{"localcfg"}) ? $opts{"localcfg"} : "";
  my $what = shift @ARGV;
  init_local_db();

  # we create fmtutil.cnf, language.dat, language.def in TEXMFSYSVAR
  # and updmap.cfg in TEXMFSYSCONFIG. The reason is that calls to
  # updmap-sys (as is done by the tlmgr update call when packages with
  # maps are installed) will create the updmap.cfg file in TEXMFSYSCONFIG
  # from the version in TEXMFSYSVAR. But after that the TEXMFSYSCONFIG
  # takes precedence and the mechanism explained in updmap.cfg header
  # does not work.
  #
  chomp (my $TEXMFSYSVAR = `kpsewhich -var-value=TEXMFSYSVAR`);
  chomp (my $TEXMFSYSCONFIG = `kpsewhich -var-value=TEXMFSYSCONFIG`);
  chomp (my $TEXMFLOCAL = `kpsewhich -var-value=TEXMFLOCAL`);
  
  if ($what =~ m/^language(\.dat|\.def)?$/i) {
    if ($what =~ m/^language(\.dat)?$/i) {
      $dest ||= "$TEXMFSYSVAR/tex/generic/config/language.dat";
      $localconf ||= "$TEXMFLOCAL/tex/generic/config/language-local.dat";
      debug ("$0: writing language.dat data to $dest\n");
      TeXLive::TLUtils::create_language_dat($localtlpdb, $dest, $localconf);
      if ($what =~ m/^language$/i) {
        # the next if will match but we have to undefine the $dest
        # variable so that it is reset to the right value
        undef $dest;
      }
    } 
    if ($what =~ m/^language(\.def)?$/i) {
      $dest ||= "$TEXMFSYSVAR/tex/generic/config/language.def";
      $localconf ||= "$TEXMFLOCAL/tex/generic/config/language-local.def";
      debug("$0: writing language.def data to $dest\n");
      TeXLive::TLUtils::create_language_def($localtlpdb, $dest, $localconf);
    } 

  } elsif ($what =~ m/^fmtutil$/i) {
    $dest ||= "$TEXMFSYSVAR/web2c/fmtutil.cnf";
    $localconf ||= "$TEXMFLOCAL/web2c/fmtutil-local.cnf";
    debug("$0: writing new fmtutil.cnf to $dest\n");
    TeXLive::TLUtils::create_fmtutil($localtlpdb, $dest, $localconf);

  } elsif ($what =~ m/^updmap$/i) {
    $dest ||= "$TEXMFSYSCONFIG/web2c/updmap.cfg";
    $localconf ||= "$TEXMFLOCAL/web2c/updmap-local.cfg";
    debug("$0: writing new updmap.cfg to $dest\n");
    TeXLive::TLUtils::create_updmap($localtlpdb, $dest, $localconf);

  } else {
    die "$0: Unknown option for generate: $what; try --help if you need it.\n";
  }

  return;
}


#  GUI
# 
sub action_gui {
  my ($guiscreen) = @_;
  # yes, two times to make perl warnings shut up ...
  $::guiscreen = $guiscreen;
  $::guiscreen = $guiscreen;
  unshift (@INC, "$Master/texmf/scripts/texlive/tlmgrgui");
  eval { require Tk; };
  if ($@) {
    # that didn't work out, give some usefull error message and stop
    my $tkmissing = 0;
    if ($@ =~ /^Can\'t locate Tk\.pm/) {
      $tkmissing = 1;
    }
    if ($tkmissing) {
      if ($^O=~/^MSWin(32|64)$/i) {
        # that should not happen, we are shipping Tk!!
        require Win32;
        my $msg = "Cannot load Tk, that should not happen as we ship it!\nHow did you start tlmgrgui??\n(Error message: $@)\n";
        Win32::MsgBox($msg, 1|Win32::MB_ICONSTOP(), "Warning");
      } else {
        printf STDERR "
Cannot load Tk, thus the GUI cannot be started!
The Perl/Tk module is not shipped with the TeX Live installation.
You have to install it to get tlmgr GUI running. See
http://tug.org/texlive/distro.html for more details.

";
      }
    } else {
      printf STDERR "Problem loading Tk: $@\n";
    }
    exit 1;
  }

  # now check that we can actually create a top level window, 
  # on darwin the X server might not be started, or on unix we are working
  # on a console, or whatever.
  eval { my $foo = Tk::MainWindow->new; $foo->destroy; };
  if ($@) {
    printf STDERR "perl/Tk unusable, cannot create main windows.
That could be a consequence of not having X Windows installed or started!
Error message from creating MainWindow:
  $@
";
    exit 1;
  }

  # be sure that sub actions do *not* finish
  $::gui_mode = 1;
  # also unset the $opts{"gui"} to make recursive calls to action_* not starting
  # another GUI instance (or better trying to ...)
  $opts{"gui"} = 0;

  require("tlmgrgui.pl");
  # should not be reached
  exit(1);
}


#  UNINSTALL
# 
sub action_uninstall {
  if (win32()) {
    printf STDERR "Please use \"Add/Remove Programs\" from the Control Panel to removing TeX Live!\n";
    finish(1);
  }
  if ($opts{"gui"}) {
    action_gui("uninstall");
  }
  my $force = defined($opts{"force"}) ? $opts{"force"} : 0;
  if (!$force) {
    print("If you answer yes here the whole TeX Live installation will be removed!\n");
    print "Remove TeX Live (y/N): ";
    my $yesno = <STDIN>;
    if ($yesno !~ m/^y(es)?$/i) {
      print "Ok, cancelling the removal!\n";
      finish(0);
    }
  }
  print ("Ok, removing the whole installation:\n");
  init_local_db();
  $localtlpdb->remove_symlinks;
  # now do remove the rest
  system("rm", "-rf", "$Master/texmf-dist");
  system("rm", "-rf", "$Master/texmf-doc");
  system("rm", "-rf", "$Master/texmf-var");
  system("rm", "-rf", "$Master/texmf");
  system("rm", "-rf", "$Master/tlpkg");
  system("rm", "-rf", "$Master/bin");
  system("rm", "-rf", "$Master/readme-html.dir");
  system("rm", "-rf", "$Master/readme-txt.dir");
  for my $f (qw/doc.html index.html LICENSE.CTAN LICENSE.TL README
                README.usergroups release-texlive.txt texmf.cnf/) {
    system("rm", "-f", "$Master/$f");
  }
  if (-d "$Master/temp") {
    system("rmdir", "--ignore-fail-on-non-empty", "$Master/temp");
  }
  unlink("$Master/install-tl.log");
  # should we do that????
  system("rm", "-rf", "$Master/texmf-config");
  system("rmdir", "--ignore-fail-on-non-empty", "$Master");
}

#  RECREATE TLPDB
#
sub action_recreate_tlpdb {
  my $tlpdb = TeXLive::TLPDB->new;
  $tlpdb->root($Master);
  my $inst = TeXLive::TLPOBJ->new;
  $inst->name("00texlive.installation");
  $inst->category("TLCore");
  my @deps;
  push @deps, "location:$TeXLive::TLConfig::TeXLiveURL";
  push @deps, "opt_create_formats:0";
  push @deps, "opt_create_symlinks:0";
  push @deps, "opt_sys_bin:/usr/local/bin";
  push @deps, "opt_sys_info:/usr/local/info";
  push @deps, "opt_sys_man:/usr/local/man";
  push @deps, "opt_install_docfiles:0";
  push @deps, "opt_install_srcfiles:0";
  # find list of available archs
  my @archs;
  opendir (DIR, "$Master/bin") || die "opendir($Master/bin) failed: $!";
  my @dirents = readdir (DIR);
  closedir (DIR) || warn "closedir($Master/bin) failed: $!";
  for my $dirent (@dirents) {
    next if $dirent eq ".";
    next if $dirent eq "..";
    next unless -d "$Master/bin/$dirent";
    if (-r "$Master/bin/$dirent/kpsewhich" || -r "$Master/bin/$dirent/kpsewhich.exe") {
      push @archs, $dirent;
      debug("Skipping directory $Master/bin/$dirent, no kpsewhich there\n");
    }
  }
  push @deps, "available_architectures:" . join(" ",@archs);
  # we have to find out the default arch
  # if there is only one dir in $Master/bin then we are settled,
  # otherwise we expect the user to pass a correct arch string
  if ($^O =~ /^MSWin(32|64)$/i) {
    push @deps, "platform:win32";
  } else {
    if ($#archs == 0) {
      # only one arch available, fine, use it as default
      push @deps, "platform:$archs[0]";
    } else {
      if (defined($opts{"arch"})) {
        if (member($opts{"arch"}, @archs)) {
          push @deps, "platform:" . $opts{"arch"};
        } else {
          tlwarn("The architecture you passed in with --arch is not present in $Master/bin\n");
          tlwarn("Please specify one from the available ones: @archs\n");
          exit(1);
        }
      } else {
        tlwarn("More than one architecture available: @archs\n");
        tlwarn("Please pass one as the default you are running on with --arch=...\n");
        exit(1);
      }
    }
  }
  $inst->depends(@deps);
  # now we have all the stuff for 00texlive-installation.config done
  $tlpdb->add_tlpobj($inst);
  # add the other stuff in $Master/tlpkg/tlpobj/*.tlpobj
  # we can ignore *.{source,doc}.tlpobj because they are already
  # included in the *.tlpobj parent one at install time
  # (TODO: we should actually REMOVE the *.{source,doc}.tlpobj files
  #        at package install time)
  opendir (DIR, "$Master/tlpkg/tlpobj") or die "opendir($Master/tlpkg/tlpobj) failed: $!";
  my @tlps = readdir(DIR);
  closedir (DIR) || warn "closedir($Master/tlpkg/tlpobj) failed: $!";
  for my $t (@tlps) {
    next if -d $t; # also does . and ..
    next if ($t !~ m/\.tlpobj$/i);
    # ignore .source and .doc tlpobjs
    next if ($t =~ m/\.(source|doc)\.tlpobj$/i);
    my $tlp = TeXLive::TLPOBJ->new;
    $tlp->from_file("$Master/tlpkg/tlpobj/$t");
    $tlpdb->add_tlpobj($tlp);
  }
  # writeout the re-created tlpdb to stdout
  $tlpdb->writeout;
  return;
}

#  CHECK
#
sub action_check {
  my $svn = defined($opts{"use-svn"}) ? $opts{"use-svn"} : 0;
  my $what = shift @ARGV;
  $what || ($what = "all");
  init_local_db();
  my $ret = 0;
  if ($what =~ m/^all/i) {
    $ret ||= check_files($svn);
    $ret ||= check_collections($svn);
  } elsif ($what =~ m/^files/i) {
    $ret ||= check_files($svn);
  } elsif ($what =~ m/^collections/i) {
    $ret ||= check_collections($svn);
  } else {
    print "No idea how to check that: $what\n";
  }
  finish($ret);
}

# check file coverage, roughly equivalent to tlpkg/bin/check-file-coverage
#
sub check_files {
  my $svn = shift;
  my $ret = 0;
  my %filetopacks;
  my $Master = $localtlpdb->root;
  debug("Collecting all files of all packages\n");
  for my $p ($localtlpdb->list_packages()) {
    # ignore files in the installer
    next if ($p eq "00texlive.installer");
    my $tlp = $localtlpdb->get_package($p);
    for my $f ($tlp->all_files) {
      push @{$filetopacks{$f}}, $p;
    }
  }
  my @multiple = ();
  my @missing = ();
  debug("Checking for occurrences and existence of all files\n");
  for (keys %filetopacks) {
    push @missing, $_ if (! -r "$Master/$_");
    my @foo = @{$filetopacks{$_}};
    if ($#foo < 0) {
      warn "that shouldn't happen: $_\n";
    } elsif ($#foo > 0) {
      push @multiple, $_;
    }
  }
  if ($#multiple >= 0) {
    $ret = 1;
    print "Multiple included files (relative to $Master):\n";
    for (sort @multiple) {
      my @foo = @{$filetopacks{$_}};
      print "  $_ (@foo)\n";
    }
    print "\n";
  }
  if ($#missing >= 0) {
    $ret = 1;
    print "Files mentioned in tlpdb but missing (relative to $Master):\n";
    for my $m (@missing) {
      print "\t$m\n";
    }
    print "\n";
  }

  # if we are on Win32 or MacOS we return, they currently do not allow
  # find -wholename (missing find on w32, or bsd find on darwin).
  # we need the -use-svn version only for the check-file-coverage 
  # replacement anyway, so it will be used on tug, which is neither w32 nor
  # darwin.
  my $arch = $localtlpdb->option_platform;
  return $ret if ($arch eq "win32" || $arch eq "universal-darwin");

  # do check that all files in the trees are covered
  #
  my @IgnorePatterns = qw!
    .mkisofsrc$ autorun.inf$
    support/ source/ setuptl/
    texmf-dist/ls-R$ texmf-doc/ls-R$ texmf/ls-R$
    tlpkg/tlpsrc tlpkg/bin tlpkg/lib/ tlpkg/libexec tlpkg/tests/ tlpkg/etc
    tlpkg/texlive.tlpdb
    tlpkg/tlpobj
    texmf-var/
    texmf-config/
    texmf.cnf
    install-tl.log
    tlpkg/texlive.profile
    tlpkg/installer
    install-tl$
    install-tl.bat$
    install-tl.bat.manifest$
  !;
  my $tltree = TeXLive::TLTREE->new ("svnroot" => $Master);
  if ($svn) {
    debug("Initializine TLTREE from svn\n");
    $tltree->init_from_svn;
  } else {
    debug("Initializine TLTREE from find\n");
    $tltree->init_from_files;
  }
  my %tltreefiles = %{$tltree->{'_allfiles'}};
  my @tlpdbfiles = keys %filetopacks;
  my @nohit;
  for my $f (keys %tltreefiles) {
    # if it is mentioned in the tlpdb or is ignored it is considered
    # as covered, thus, otherwise we push it onto the nothit list
    if (!defined($filetopacks{$f})) {
      my $ignored = 0;
      for my $p (@IgnorePatterns) {
        if ($f =~ m/^$p/) {
          $ignored = 1;
          last;
        }
      }
      if (!$ignored) {
        push @nohit, $f;
      }
    }
  }
  if (@nohit) {
    $ret = 1;
    print "Files present but not covered (relative to $Master):\n";
    for my $f (sort @nohit) {
      print "  $f\n";
    }
    print "\n";
  }
  return($ret);
}

# check collections
#
sub check_collections {
  my @missing = ();
  for my $p ($localtlpdb->collections()) {
    my $col = $localtlpdb->get_package($p);
    for my $d ($col->depends) {
      push @missing, "$d ($p)" if (!defined($localtlpdb->get_package($d)));
    }
  }
  return(0) if (!@missing);
  print "Packages listed in collections but not present:\n";
  for my $m (@missing) {
    print "\t$m\n";
  }
  print "\n";
}



# Subroutines galore.
# 
# set global $location variable.
# if we cannot read tlpdb, die if arg SHOULD_I_DIE is true.
#
# if an argument is given and is true init_local_db will die if 
# setting up of programs failed.
# 
sub init_local_db {
  my ($should_i_die) = @_;
  # if the localtlpdb is already defined do simply return here already
  # to make sure that the settings in the local tlpdb do not overwrite
  # stuff changed via the GUI
  return if defined $localtlmedia;
  return if defined $localtlpdb;
  $localtlmedia = TeXLive::TLMedia->new ( $Master );
  die("cannot setup TLMedia in $Master") unless (defined($localtlmedia));
  $localtlpdb = $localtlmedia->tlpdb;
  die("cannot find tlpdb in $Master") unless (defined($localtlpdb));
  # setup the programs, for w32 we need the shipped wget/xz etc, so we
  # pass the location of these files to setup_programs.
  if (!setup_programs("$Master/tlpkg/installer", 
                      $localtlpdb->option_platform)) {
    tlwarn("Couldn't set up the necessary programs.\nInstallation of packages is not supported.\nPlease report to texlive\@tug.org.\n");
    if (defined($should_i_die) && $should_i_die) {
      finish(1);
    } else {
      tlwarn("Continuing anyway ...\n");
    }
  }
  # let cmd line options override the settings in localtlpdb
  my $loc = $localtlpdb->option_location;
  if (defined($loc)) {
    $location = $loc;
  }
  if (defined($opts{"location"})) {
    $location = $opts{"location"};
  }
  if (!defined($location)) {
    die("No installation source found, nor in the texlive.tlpdb nor on the cmd line.\nPlease specify one!");
  }
  if ($location =~ m/^ctan$/i) {
    $location = "$TeXLive::TLConfig::TeXLiveURL";
  }
  # we normalize the path only if it is
  # - neither a URL starting with http or ftp
  # - if we are on windows it does not start with Drive:
  if (! ( $location =~ m!^(http|ftp)://!i  ||
          (win32() && $location =~ m!^.:!) ) ) {
    # seems to be a local path, try to normalize it
    my $testloc = abs_path($location);
    # however, if we were given a url, that will get "normalized" to the
    # empty string, it not being a path.  Restore the original value if so.
    $location = $testloc if $testloc;
  }
}


# initialize the global $tlmediasrc object, or die.
# uses the global $location.
#
sub init_tlmedia {
  if (defined($tlmediatlpdb) && ($tlmediatlpdb->root eq $location)) {
    # nothing to be done
  } else {
    if ($location =~ m/^($TeXLiveServerURL|ctan$)/) {
      $location = give_ctan_mirror();
    }
    # that "location-url" line should not be changed since GUI programs
    # depend on it.
    print "location-url\t$location\n" if $::machinereadable;
    info("tlmgr: installation location $location\n");

    # $tlmediasrc is a global variable
    $tlmediasrc = TeXLive::TLMedia->new($location);
    die($loadmediasrcerror . $location) unless defined($tlmediasrc);
    $tlmediatlpdb = $tlmediasrc->tlpdb;
  }
}

# finish handles the -pause option (wait for input from stdin),
# and then exits unless  the global $::gui_mode is set, in which case we
# merely return.
# 
sub finish {
  my ($ret) = @_;

  if ($ret > 0) {
    print "tlmgr: exiting unsuccessfully (status $ret).\n";
  }
  
  if ($opts{"pause"}) {
    print "Press Enter to exit the program.\n";
    <STDIN>;
  }

  if ($::gui_mode) {
    return($ret);
  } else {
    exit($ret);
  }
}

# if the packagelog variable is set then write to PACKAGELOG filehandle
#
sub logpackage {
  if ($packagelogfile) {
    $packagelogged++;
    my $tim = localtime();
    print PACKAGELOG "[$tim] @_\n";
  }
}

# clear the backup dir for $pkg and keep only $autobackup packages
# mind that with $autobackup == 0 all packages are cleared
sub clear_old_backups
{
  my ($pkg, $backupdir, $autobackup, $dry) = @_;

  my $dryrun = 0;
  $dryrun = 1 if ($dry);
  # keep arbitrary many backups
  return if ($autobackup == -1);
 
  opendir (DIR, $backupdir) || die "opendir($backupdir) failed: $!";
  my @dirents = readdir (DIR);
  closedir (DIR) || warn "closedir($backupdir) failed: $!";
  my @backups;
  for my $dirent (@dirents) {
    next if (-d $dirent);
    next if ($dirent !~ m/^$pkg\.r([0-9]+)\.tar\.xz$/);
    push @backups, $1;
  }
  my $i = 1;
  for my $e (reverse sort {$a <=> $b} @backups) {
    if ($i > $autobackup) {
      info ("Removing backup $backupdir/$pkg.r$e.tar.xz\n");
      unlink("$backupdir/$pkg.r$e.tar.xz") unless $dryrun;
    }
    $i++;
  }
}

# check for updates to tlcritical packages
#
sub check_for_critical_updates
{
  my ($localtlpdb, $mediatlpdb) = @_;

  my $criticalupdate = 0;
  my @critical = $localtlpdb->expand_dependencies("-no-collections",
    $localtlpdb, @CriticalPackagesList);
  for my $pkg (sort @critical) {
    my $tlp = $localtlpdb->get_package($pkg);
    if (!defined($tlp)) {
      # that should not happen, we expanded in the localtlpdb so why 
      # should it not be present, any anyway, those are so fundamental
      # so they have to be there
      tlwarn("Fundamental package $pkg not present, that is sooo bad.\n");
      die "Serious error, $pkg not found";
    }
    my $localrev = $tlp->revision;
    my $mtlp = $mediatlpdb->get_package($pkg);
    if (!defined($mtlp)) {
      debug("Very surprising, $pkg is not present in the remote tlpdb.\n");
      next;
    }
    my $remoterev = $mtlp->revision;
    if ($remoterev > $localrev) {
      $criticalupdate = 1;
      last;
    }
  }
  if ($criticalupdate) {
    tlwarn("Updates for tlmgr itself are present.\n");
    tlwarn("=" x 79, "\n");
    tlwarn("Please update the packages bin-texlive and texlive.infra first, ");
    tlwarn("via either\n");
    tlwarn("  tlmgr update bin-texlive texlive.infra\n");
    tlwarn("or by getting the latest updater for Unix-ish systems:\n");
    tlwarn("  $TeXLiveURL/update-tlmgr-latest.sh\n");
    tlwarn("and/or Windows systems:\n");
    tlwarn("  $TeXLiveURL/update-tlmgr-latest.exe\n");
    tlwarn("Then continue with other updates.\n");
    tlwarn("=" x 79, "\n");
  }
  return($criticalupdate);
}


# return all the directories from which all content will be removed
#
# idea:
# - create a hashes by_dir listing all files that should be removed
#   by directory, i.e., key = dir, value is list of files
# - for each of the dirs (keys of by_dir and ordered deepest first)
#   check that all actually contained files are removed
#   and all the contained dirs are in the removal list. If this is the
#   case put that directory into the removal list
# - return this removal list
#
sub removed_dirs
{
  my (@files) = @_;
  my %removed_dirs;
  my %by_dir;

  # construct hash of all directories mentioned, values are lists of the
  # files/dirs in that directory.
  for my $f (@files) {
    # what should we do with not existing entries????
    next if (! -r "$f");
    my $abs_f = abs_path ($f);
    if (!$abs_f) {
      warn ("oops, no abs_path($f) from " . `pwd`);
      next;
    }
    (my $d = $abs_f) =~ s,/[^/]*$,,;
    my @a = exists $by_dir{$d} ? @{$by_dir{$d}} : ();
    push (@a, $abs_f);
    $by_dir{$d} = \@a;
  }

  # for each of our directories, see if we are removing everything in
  # the directory.  if so, return the directory; else return the
  # individual files.
  for my $d (reverse sort keys %by_dir) {
    opendir (DIR, $d) || die "opendir($d) failed: $!";
    my @dirents = readdir (DIR);
    closedir (DIR) || warn "closedir($d) failed: $!";

    # initialize test hash with all the files we saw in this dir.
    # (These idioms are due to "Finding Elements in One Array and Not
    # Another" in the Perl Cookbook.)
    my %seen;
    my @rmfiles = @{$by_dir{$d}};
    @seen{@rmfiles} = ();

    # see if everything is the same.
    my $cleandir = 1;
    for my $dirent (@dirents) {
      next if $dirent =~ /^\.(\.|svn)?$/;  # ignore . .. .svn
      my $item = "$d/$dirent";  # prepend directory for comparison
      if (
           ((-d $item) && (defined($removed_dirs{$item})))
           ||
           (exists $seen{$item})
         ) {
        # do nothing
      } else {
        $cleandir = 0;
        last;
      }
    }
    if ($cleandir) {
      $removed_dirs{$d} = 1;
    }
  }
  return keys %removed_dirs;
}

__END__

=head1 NAME

tlmgr - the TeX Live Manager

=head1 SYNOPSIS

tlmgr [I<option>]... I<action> [I<option>]... [I<operand>]...

=head1 DESCRIPTION

B<tlmgr> manages an existing TeX Live installation, both packages and
configuration options.  It performs many of the same actions as
B<texconfig>(1), and more besides.  (C<texconfig> continues to be
included and supported, but C<tlmgr> is now preferred.)

The most up-to-date version of this documentation is available at
L<http://tug.org/texlive/tlmgr.html>, along with procedures for updating
tlmgr itself, disaster recovery when it's broken, and test versions.

=head1 OPTIONS

The following options to C<tlmgr> have to be given I<before> you specify
the main action to perform.  In all cases, C<-->I<option> and
C<->I<option> are equivalent, and an C<=> is optional between an option
name and its value.

=over 4

=item B<--location> I<location>

Specifies the location from which packages should be installed or
updated, overriding the location found in the installation's TeX Live
Package Database (TLPDB).

=item B<--gui> [I<action>]

You can give this option together with an action to be brought directly
into the respective screen of the GUI.  For example, running

  tlmgr --gui update

starts you directly at the update screen.

=item B<--gui-lang> I<llcode>

Normally the GUI tries to deduce your language from the environment (on
Windows via the registry, on Unix via C<LC_MESSAGES>). If that fails you
can select a different language by giving this option a two-letter
language code.

=item B<--machine-readable>

In lieu of the normal output intended for human consumption, write to
standard output in a fixed format more suitable for machine parsing.
See the L</"MACHINE-READABLE OUTPUT"> section below for details.

=item B<--package-logfile> I<file>

B<tlmgr> logs all package actions (install, remove, update, failed
updates, failed restores) to a separate log file, by default
C<TEXMFSYSVAR/web2c/tlmgr.log>. This option allows you to select a
different file for that.  This is separate from normal logging; for
that, see the option C<-v> below, and TeXLive::TLUtils.

=item B<--pause>

This option make B<tlmgr> wait for user input before exiting.  Useful on
Windows to avoid command windows disappearing.

=back

The standard options for TeX Live programs are also accepted:
C<--help/-h/-?>, C<--version>, C<-q> (no informational messages), C<-v>
(debugging messages, can be repeated).  For the details about these, see
the TeXLive::TLUtils documentation.

The C<--version> option shows version information about the TeX Live
release as well as the C<tlmgr> script itself.

=head1 ACTIONS

=head2 help

Gives this help information (same as C<--help>).

=head2 version

Gives version information (same as C<--version>).

=head2 gui

Start the graphical user interface.


=head2 install [I<option>]... I<pkg>...

Install each I<pkg> given on the command line. By default this installs
all packages on which the given I<pkg>s are dependent, also.  Options:

=over 4

=item B<--reinstall>

Reinstall a package (including dependencies for collections) even if it
seems to be already installed (i.e, is present in the TLPDB).  This is
useful to recover from accidental removal of files in the hierarchy.

When re-installing, only dependencies on normal packages are followed
(not those of category Scheme or Collection).

=item B<--no-depends>

Do not install dependencies.  (By default, installing a package ensures
that all dependencies of this package are fulfilled.)

=item B<--no-depends-at-all>

When you install a package which ships binary files the respective binary
package will also be installed. (So for package bin-foobar also the package
bin-foobar.i386-linux will be installed on an i386-linux system.)
This switch supresses this behaviour, and also implies C<--no-depends>.
Don't use it unless you are sure of what you are doing.

=item B<--dry-run>

Nothing is actually installed; instead, the actions to be performed are
written to the terminal.

=item B<--force>

If updates to the tlmgr itself (and the underlying infrastructure) are 
present tlmgr will bail out and not perform the installation unless this option
is given.

=back


=head2 update [I<option>]... [I<pkg>]...

Updates the packages given as arguments to the latest version available
at the installation source.  Either C<--all> or at least one I<pkg> name
must be specified.  Options:

=over 4

=item B<--all>

Update all installed packages.  For packages contained in an installed
collection, also install new packages and remove deleted packages in
those collections.  Packages not contained in an installed collection
are not considered for addition or removal.

More precisely, if both the server and the local installation have a
collection C, and C on the server contains a package A that is not
present locally, A will be added.  Conversely, if the local collection C
contains a package B, but B is no longer in the server's C, then B will
be deleted.

=item B<--list>

Concisely list the packages which would be updated, newly installed, or
removed, without actually changing anything.

=item B<--dry-run>

Nothing is actually installed; instead, the actions to be performed are
written to the terminal.  This is a more detailed report than C<--list>.

=item B<--force>

If updates to tlmgr itself (that is, the infrastructure packages) are
present, tlmgr will not perform the update unless this option is given.
(C<--list> is still performed regardless of this option.)

=item B<--no-remove>

Under normal circumstances tlmgr tries to remove packages which have
disappeared on the server when called with C<--all>, as described
above. This prevents any such removals.

=item B<--backup> and B<--backupdir> I<directory>

These two options control the creation of backups of packages before an
update is started; that is, the backup is of the package as it's
installed.  If neither of the two are given, no backup package will be
saved. If B<--backupdir> is given and specifies a writable directory
then a backup will be made in that location. If only B<--backup> is
given, then a backup will be made to the directory previously set via
the C<option> action (see below). If both are given then a backup will
be made to the specified I<directory>.

You can set options via the C<option> action to automatically create
backups for all packages, and/or keep only a certain number of
backups. Please see the C<option> action for details. 

C<tlmgr> always makes a temporary backup when updating packages, in case
of download or other failure during an update.  The purpose of this
B<--backup> option is to allow you to save a persistent backup in case
the actual I<content> of the update causes problems, e.g., introduces an
incompatibility.

The C<restore> action explains how to restore from a backup.

=item B<--no-depends>

If you call for updating a package normally all depending packages
will also be checked for updates and updated if necessary. This switch
supresses this behaviour.

=item B<--no-depends-at-all>

By default, when you update a package which ships binary files the
respective binary package will also be updated.  That is, for an update
of a package C<foo>, the package C<foobar.i386-linux> will also be
updated if the i386-linux platform is installed.  This option suppresses
this behaviour, and implies C<--no-depends>.  Don't use it unless you
are sure of what you are doing.

=back

If the package on the server is older than the package already installed
(e.g., if the selected mirror is out of date), C<tlmgr> does not
downgrade.  Also, packages for uninstalled platforms are not installed.


=head2 backup [--clean[=I<N>]] [--backupdir I<dir>] [--all] [I<pkg>]...

If the B<--clean> option is not specified, this action makes a backup of
the given packages, or all packages given C<--all>. These backups are
saved to the value of B<--backupdir> option if that is an existing and
writable directory; if B<--backupdir> is not given, the C<backupdir>
option setting in the TLPDB is used, if present.  If both are missing,
no backups are made.

If the B<--clean> option is specified, backups are cleaned (pruned)
instead of made.  If the value for the B<--clean> switch is not given,
the value of the C<autobackup> option is used. If both are missing,
an error is issued.  For the details of backup cleaning, see the
C<option> action below.

Options:

=over 4

=item B<--backupdir> I<directory>

The I<directory> argument is required and must specify an existing
directory where backups are to be placed.

=item B<--all>

Make a backup of all packages in the TeX Live installation.  This will
take quite a lot of space and time.

=item B<--clean>[=I<N>]

Instead of making backups, prune the backup directory of old backups.

=item B<--dry-run>

Nothing is actually backed up or removed; instead, the actions to be 
performed are written to the terminal. 

=back


=head2 restore --backupdir I<dir> [I<pkg> [I<rev>]

Restore a package from a previously-made backup.

If neither I<pkg> nor I<rev> are given, list the available backup
revisions for all packages.

With I<pkg> given but no I<rev>, list all available backup revisions of
I<pkg>.

With both I<pkg> and I<rev>, tries to restore the package from the
specified backup.

Options:

=over 4

=item B<--dry-run>

Nothing is actually restored; instead, the actions to be performed are
written to the terminal.

=item B<--backupdir> I<directory>

Specify the directory where the backups are to be found.

=back


=head2 remove [I<option>]... I<pkg>...

Remove each I<pkg> specified.  Removing a collection removes all package
dependencies, but not collection dependencies, in that collection
(unless C<--no-depends> is specified).  However, when removing a
package, dependencies are never removed.  Options:

=over 4

=item B<--no-depends>

Do not remove dependent packages.

=item B<--no-depends-at-all>

See above under B<action> (and beware).

=item B<--force>

By default, removal of a package or collection that is a dependency of
another collection or scheme is not allowed.  With this option, the
package will be removed unconditionally.  Use with care.

=item B<--dry-run>

Nothing is actually removed; instead, the actions to be performed are
written to the terminal.

=back


=head2 option

=over 4

=item B<option [show]>

=item B<option I<key> [I<value>]>

=back

The first form shows the global TeX Live settings currently saved in the
TLPDB.

In the second form, if I<value> is not given, the setting for I<key> is
displayed.  If I<value> is present, I<key> is set to I<value>.

Possible values for I<key> are:
 C<location> (default installation location),
 C<formats> (create formats at installation time),
 C<docfiles> (install documentation files),
 C<srcfiles> (install source files),
 C<backupdir> (default directory for backups),
 C<autobackup> (number of backups to keep).
 C<sys_bin> (location where binaries are linked to by action symlinks)
 C<sys_man> (location where man pages are linked to by action symlinks)
 C<sys_info> (location where info pages are linked to by action symlinks)

Perhaps the most common use of C<option> is if you originally installed from
DVD, and want to permanently change the installation to get further
updates from the Internet.  To do this, you can run

 tlmgr option location http://mirror.ctan.org/systems/texlive/tlnet/YYYY

where YYYY is the TeX Live release year.

The two options C<autobackup> and C<backupdir> determine defaults for
the C<update>, C<backup> and C<restore> actions.  These three actions
need a directory to write to or read from the backups.  If
C<--backupdir> is not specified on the command line, the C<backupdir>
option value is used (if set).

The C<autobackup> option (de)activates the automatic generation of
backups. Its value is an integer.  If C<autobackup> is C<-1>, backups
will not be removed by default.  If C<autobackup> is 0 or positive, it
specifies the number of backups to be kept.  Thus, backups are disabled
with 0 as the values.

B<Warning:> If you want to set the optin C<autobackup> on the command line
it might be interpreted as an option. In this case you have to use

  tlmgr option -- autobackup -1

The C<--> makes tlmgr stop parsing for options at that place.

Alternatively, you can ue C<infty> instead of C<-1>, so the above is
equivalent to

  tlmgr option autobackup infty


In the C<--clean> mode of the C<backup> action this option also
specifies the number to be kept.

=head2 symlinks [add|remove]

Adds or removes symlinks for binaries, man pages, and info pages in the
directories specified by the respective options (see above).


=head2 paper

=over 4

=item B<paper [a4|letter]>

=item B<S<xdvi|pdftex|dvips|dvipdfmx|dvipdfm|context paper [I<papersize>|--list]>>

=back

With no arguments (C<tlmgr paper>), shows the default paper size setting
for all known programs.

With one argument (e.g., C<tlmgr paper a4>), sets the default for all
known programs to that paper size.

With a program given as the first argument and no paper size specified
(e.g., C<tlmgr dvips paper>), shows the default paper size for that
program.

With a program given as the first argument and a paper size as the last
argument (e.g., C<tlmgr dvips paper a4>), set the default for that
program to that paper size.

With a program given as the first argument and C<--list> given as the
last argument (e.g., C<tlmgr dvips paper --list>), shows all valid paper
sizes for that program.  The first size shown is the default.

Incidentally, this syntax of having a specific program name before the
C<paper> keyword may seem strange.  It is inherited from the
longstanding C<texconfig> script, which supports other configuration
settings for some programs, notably C<dvips>.  C<tlmgr> does not support
those extra settings at present.

=head2 arch list|add I<arch>...

C<arch list> lists the TeX Live names of all the architectures
(C<i386-linux>, ...) available at the default install location.

C<arch add> I<arch> adds the executables for each given architecture
I<arch> to the installation.  Options:

=over 4

=item B<--dry-run>

Nothing is actually installed; instead, the actions to be performed are
written to the terminal.

=back


=head2 search [I<option>]... I<what>

By default searches the names, short and long descriptions of all
locally installed packages for the given argument (interpreted as
regexp).  Options:

=over 4

=item B<--file>

List all filenames containing I<what>.

=item B<--global>

Search the TeX Live Database of the installation medium, instead of the
local installation.

=back


=head2 show [--list] I<pkg>...

Shows information about I<pkg>: the name, category, installation status,
short and long description.  Searches in the remote installation source
for the package if it is not locally installed.

If the option B<--list> is given the list of contained files is also
shown, including those for architecture specific dependencies.


=head2 list [collections|schemes|I<pkg>...]

With no argument, lists all packages available at the default install
location, prefixing those already installed with C<i>.

With the single word C<collections> or C<schemes> as the argument, lists
the request type.

With anything else as arguments, operates as the C<show> action.


=head2 check [I<option>]... [files|collections|all]

Executes one (or all) check(s) on the consistency of the installation.

=over 4

=item B<files>

Checks that all files listed in the TeX Live Database (C<texlive.tlpdb>)
are actually present, and lists those missing.

=item B<collections>

Lists those packages which occur as dependencies in an installed collections,
but are themselves not installed.  Options:

=back

=over 4

=item B<--use-svn>

Use svn status instead of listing the files, for checking the
development repository.

=back


=head2 uninstall

Uninstalls the entire TeX Live installation.  Options:

=over 4

=item B<--force>

Do not ask for confirmation, remove immediately.

=back


=head2 generate [I<option>]... I<what>

=over 4

=item B<generate language>

=item B<generate language.dat>

=item B<generate language.def>

=item B<generate fmtutil>

=item B<generate updmap>

=back

The C<generate> action overwrites any manual changes made in the
respective files: it recreates them from scratch.

For C<fmtutil> and the language files, this is normal, and both the TeX
Live installer and C<tlmgr> routinely call I<generate> for them.

For C<updmap>, however, neither the installer nor C<tlmgr> use
C<generate>, because the result would be to disable all maps which have
been manually installed via S<C<updmap-sys --enable>>, e.g., for
proprietary or local fonts.  Only the changes in the C<--localcfg> file
mentioned below are incorporated by I<generate>.  Furthermore, C<tlmgr>
updates and maintains the final C<updmap.cfg> in C<TEXMFSYSCONFIG> (the
others use C<TEXMFSYSVAR>) because that is the location that
C<updmap-sys> (via C<tcfmgr>) uses.

Notwithstanding the above, if you only use the fonts and font packages
within TeX Live, and maintain your local fonts (if any) with the local
config file, there is nothing wrong with using C<generate updmap>.
Indeed, we use it ourselves to generate the C<updmap.cfg> file that is
maintained in the live source repository.

In more detail: C<generate> remakes any of the four config files
C<language.dat>, C<language.def>, C<fmtutil.cnf>, and C<updmap.cfg> from
the information present in the local TLPDB. If the files
C<language-local.dat>, C<language-local.def>, C<fmtutil-local.cnf>, or
C<updmap-local.cfg> are present under C<TEXMFLOCAL> in the respective
directories, their contents will be simply merged into the final files,
with no error checking of any kind.

The form C<generate language> recreates both the C<language.dat> and the
C<language.def> files, while the forms with extension only recreates
the given file.

Options:

=over 4

=item B<--dest> I<output file>

specifies the output file (defaults to the respective location in
C<TEXMFSYSVAR> for C<language*> and C<fmtutil>, and C<TEXMFSYSCONFIG>
for C<updmap>).  If B<--dest> is given to C<generate language>, its
value will be used for the C<language.dat> output, and C<.def> will be
appended to the value for the name of the C<language.def> output file.
(This is just to avoid overwriting; if you want a specific name for each
output file, we recommend invoking C<tlmgr> twice.)

=item B<--localcfg> I<local conf file>

specifies the (optional) local additions (defaults to the respective
location in C<TEXMFLOCAL>).

=back

The respective locations are as follows:

  tex/generic/config/language.dat (and language-local.dat);
  tex/generic/config/language.def (and language-local.def);
  web2c/fmtutil.cnf (and fmtutil-local.cnf);
  web2c/updmap.cfg (and updmap-local.cfg).

=head2 print-arch

Prints out the currently detected architecture.


=head1 MACHINE-READABLE OUTPUT

Given the C<--machine-readable> option, C<tlmgr> writes to stdout in the
fixed line-oriented format described here, and the usual informational
messages for human consumption are written to stderr (normally they are
written to stdout).  The idea is that a program can get all the
information it needs by reading stdout.

Currently this option only applies to the L<update|/"update
[I<option>]... [I<pkg>]..."> action.  The output format is as follows:

  fieldname "\t" value
  ...
  "end-of-header"
  pkgname status localrev serverrev size
  ...  

Currently the header section contains two fields.  The field name and
value are separated by a tab.  Line endings may be LF or CRLF depending
on the current platform.

=over 4

=item C<location-url> I<location>

The I<location> may be a url (including C<file:///foo/bar/...>), or a
directory name (C</foo/bar>).  It is the server repository from which
the new package information was drawn.

=item C<total-bytes> I<count>

The I<count> is simply a decimal number, the sum of the sizes of all the
packages that need updating or installing (which are listed subseqently).

=back

Then comes a line with only the literal string C<end-of-header>.

Each following line until EOF reports on one package.  The fields on
each line are separated by a tab.  Here are the fields.

=over 4

=item I<pkgname>

The TeX Live package identifier, with a possible architecture suffix for
executables.   For instance, C<pdftex> and C<pdftex.i386-linux> are
given as two separate packages, one on each line.

=item I<status>

The status of the package update.  One character, as follows:

=over 8

=item C<d>

The package was removed on the server.

=item C<f>

The package was removed in the local installation, even though a
collection depended on it.  (E.g., the user ran C<tlmgr remove
--force>.)

=item C<u>

Normal update is needed.

=item C<r>

Reversed non-update: the locally-installed version is newer than the
version on the server.

=item C<a>

Automatically-determined need for installation, the package is new on
the server and is (most probably) part of an installed collection.

=back

=item I<localrev>

The revision number of the installed package, or C<-> if it is not
present locally.

=item I<serverrev>

The revision number of the package on the server, or C<-> if it is not
present on the server.

=item I<size>

The size in bytes of the package on the server.  The sum of all the
package sizes is given in the C<total-bytes> header field mentioned above.

=back

If you are developing a program that uses this output, and find that
changes would be helpful, do not hesitate to write the mailing list.


=head1 AUTHORS AND COPYRIGHT

This script and its documentation were written for the TeX Live
distribution (L<http://tug.org/texlive>) and both are licensed under the
GNU General Public License Version 2 or later.

=cut

# to remake HTML version: pod2html --cachedir=/tmp tlmgr.pl >/foo/tlmgr.html.

### Local Variables:
### perl-indent-level: 2
### tab-width: 2
### indent-tabs-mode: nil
### End:
# vim:set tabstop=2 expandtab: #