summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/cjk-gs-integrate/cjk-gs-integrate.pl
blob: 2dbfdd49b013923f2cd5932ca0d36cc16274609e (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
#!/usr/bin/env perl
#
# cjk-gs-integrate - setup Ghostscript for CID/TTF CJK fonts
#
# Copyright 2015-2020 by Norbert Preining
# Copyright 2016-2020 by Japanese TeX Development Community
#
# This work is based on research and work by (in alphabetical order)
#   Yusuke Kuroki
#   Yusuke Terada
#   Bruno Voisin
#   Munehiro Yamamoto
#   Hironobu Yamashita
# and the Japanese TeX Q&A wiki page
#
# This file is licensed under GPL version 3 or any later version.
# For copyright statements see end of file.
#
# For development see
#  https://github.com/texjporg/cjk-gs-support
#
# LIMITATIONS:
# - Running the script (with default mode = actual setup/removing operations)
#   always overwrites "cidfmap.local" and "cidfmap.aliases" without asking,
#   whose file names might be common enough. If you choose to run the script,
#   leave these files untouched. (Do NOT edit these files by yourself!)
#   (This note also applies to MacTeX pre-shipped configuration files.)
#
# TODO:
# - interoperability with kanji-config-updmap
#
# Note that symlink names should be consistent with ptex-fontmaps!

$^W = 1;
use Getopt::Long qw(:config no_autoabbrev ignore_case_always);
use File::Basename;
use File::Path qw(make_path);
use Cwd 'abs_path';
use strict;

(my $prg = basename($0)) =~ s/\.pl$//;
my $version = '20200115.0';

if (win32()) {
  # conversion between internal (utf-8) and console (cp932):
  # multibyte characters should be encoded in cp932 at least during
  #   * kpathsea file search
  #   * abs_path existence test
  #   * input/output on console
  #   * batch file output
  # routines. make sure all of these should be restricted to win32 mode!
  # TODO: what to do with $opt_fontdef, @opt_aliases and $opt_filelist,
  #       with regard to encodings?
  use utf8;
  use Encode;
  # some perl functions (symlink, -l test) does not work
  print_warning("Sorry, we have only partial support for Windows!\n");
}

# The followings are installed by ptex-fontmaps (texjporg):
#   * 2004-H
#   * 2004-V
# The followings are created by Adobe but not considered official
# (see https://forums.adobe.com/thread/537415)
#   * GB-RKSJ-H
#   * GBT-RKSJ-H
#   * KSC-RKSJ-H
# All others are provided in the latest Adobe CMap Resources:
#   https://github.com/adobe-type-tools/cmap-resources
my %encode_list = (
  Japan => [ qw/
    2004-H
    2004-V
    78-EUC-H
    78-EUC-V
    78-H
    78-RKSJ-H
    78-RKSJ-V
    78-V
    78ms-RKSJ-H
    78ms-RKSJ-V
    83pv-RKSJ-H
    90ms-RKSJ-H
    90ms-RKSJ-V
    90msp-RKSJ-H
    90msp-RKSJ-V
    90pv-RKSJ-H
    90pv-RKSJ-V
    Add-H
    Add-RKSJ-H
    Add-RKSJ-V
    Add-V
    Adobe-Japan1-0
    Adobe-Japan1-1
    Adobe-Japan1-2
    Adobe-Japan1-3
    Adobe-Japan1-4
    Adobe-Japan1-5
    Adobe-Japan1-6
    Adobe-Japan1-7
    EUC-H
    EUC-V
    Ext-H
    Ext-RKSJ-H
    Ext-RKSJ-V
    Ext-V
    H
    Hankaku
    Hiragana
    Identity-H
    Identity-V
    Katakana
    NWP-H
    NWP-V
    RKSJ-H
    RKSJ-V
    Roman
    UniJIS-UCS2-H
    UniJIS-UCS2-HW-H
    UniJIS-UCS2-HW-V
    UniJIS-UCS2-V
    UniJIS-UTF16-H
    UniJIS-UTF16-V
    UniJIS-UTF32-H
    UniJIS-UTF32-V
    UniJIS-UTF8-H
    UniJIS-UTF8-V
    UniJIS2004-UTF16-H
    UniJIS2004-UTF16-V
    UniJIS2004-UTF32-H
    UniJIS2004-UTF32-V
    UniJIS2004-UTF8-H
    UniJIS2004-UTF8-V
    UniJISPro-UCS2-HW-V
    UniJISPro-UCS2-V
    UniJISPro-UTF8-V
    UniJISX0213-UTF32-H
    UniJISX0213-UTF32-V
    UniJISX02132004-UTF32-H
    UniJISX02132004-UTF32-V
    V
    WP-Symbol
    / ],
  GB => [ qw/
    Adobe-GB1-0
    Adobe-GB1-1
    Adobe-GB1-2
    Adobe-GB1-3
    Adobe-GB1-4
    Adobe-GB1-5
    GB-EUC-H
    GB-EUC-V
    GB-H
    GB-RKSJ-H
    GB-V
    GBK-EUC-H
    GBK-EUC-V
    GBK2K-H
    GBK2K-V
    GBKp-EUC-H
    GBKp-EUC-V
    GBT-EUC-H
    GBT-EUC-V
    GBT-H
    GBT-RKSJ-H
    GBT-V
    GBTpc-EUC-H
    GBTpc-EUC-V
    GBpc-EUC-H
    GBpc-EUC-V
    Identity-H
    Identity-V
    UniGB-UCS2-H
    UniGB-UCS2-V
    UniGB-UTF16-H
    UniGB-UTF16-V
    UniGB-UTF32-H
    UniGB-UTF32-V
    UniGB-UTF8-H
    UniGB-UTF8-V
    / ],
  CNS => [ qw/
    Adobe-CNS1-0
    Adobe-CNS1-1
    Adobe-CNS1-2
    Adobe-CNS1-3
    Adobe-CNS1-4
    Adobe-CNS1-5
    Adobe-CNS1-6
    Adobe-CNS1-7
    B5-H
    B5-V
    B5pc-H
    B5pc-V
    CNS-EUC-H
    CNS-EUC-V
    CNS1-H
    CNS1-V
    CNS2-H
    CNS2-V
    ETHK-B5-H
    ETHK-B5-V
    ETen-B5-H
    ETen-B5-V
    ETenms-B5-H
    ETenms-B5-V
    HKdla-B5-H
    HKdla-B5-V
    HKdlb-B5-H
    HKdlb-B5-V
    HKgccs-B5-H
    HKgccs-B5-V
    HKm314-B5-H
    HKm314-B5-V
    HKm471-B5-H
    HKm471-B5-V
    HKscs-B5-H
    HKscs-B5-V
    Identity-H
    Identity-V
    UniCNS-UCS2-H
    UniCNS-UCS2-V
    UniCNS-UTF16-H
    UniCNS-UTF16-V
    UniCNS-UTF32-H
    UniCNS-UTF32-V
    UniCNS-UTF8-H
    UniCNS-UTF8-V
    / ],
  Korea => [ qw/
    Adobe-Korea1-0
    Adobe-Korea1-1
    Adobe-Korea1-2
    Identity-H
    Identity-V
    KSC-EUC-H
    KSC-EUC-V
    KSC-H
    KSC-Johab-H
    KSC-Johab-V
    KSC-RKSJ-H
    KSC-V
    KSCms-UHC-H
    KSCms-UHC-HW-H
    KSCms-UHC-HW-V
    KSCms-UHC-V
    KSCpc-EUC-H
    KSCpc-EUC-V
    UniKS-UCS2-H
    UniKS-UCS2-V
    UniKS-UTF16-H
    UniKS-UTF16-V
    UniKS-UTF32-H
    UniKS-UTF32-V
    UniKS-UTF8-H
    UniKS-UTF8-V
    / ] );

#
# location where links to fonts in texmf are created, relative to TEXMF
my $otf_pathpart = "fonts/opentype/cjk-gs-integrate";
my $ttf_pathpart = "fonts/truetype/cjk-gs-integrate";

# location where cidfmap, cidfmap.local and cidfmap.aliases are placed
# when found gs is tlgs (win32), then files will be placed in lib/ instead of Resource/Init/
my $cidfmap_pathpart = "Init/cidfmap";
my $cidfmap_local_pathpart = "Init/cidfmap.local";
my $cidfmap_aliases_pathpart = "Init/cidfmap.aliases";

# support for ps2otfps by Akira Kakuto
my $akotfps_pathpart = "dvips/ps2otfps";
my $akotfps_datafilename = "psnames-for-otf";
my $akotfps_datacontent = '';

# if windows, we might create batch file for links
my $winbatch = "makefontlinks.bat";
my $winbatch_content = '';

# dump output for data file (for easy editing for users)
my $dump_datafile = "$prg-data.dat";

my $opt_output;
my $opt_fontdef;
my @opt_fontdef_add;
my @opt_aliases;
my $opt_filelist;
my $opt_texmflink;
my $opt_akotfps;
my $opt_force = 0;
my $opt_remove = 0;
my $opt_cleanup = 0;
my $opt_hardlink = 0;
my $opt_winbatch;
my $opt_dump_data;
my $opt_only_aliases = 0;
my $opt_listaliases = 0;
my $opt_listallaliases = 0;
my $opt_listfonts = 0;
my $opt_info = 0;
my $opt_machine = 0;
my $opt_strictpsname = 0;
my $dry_run = 0;
my $opt_quiet = 0;
my $opt_debug = 0;
my $opt_help = 0;
my $opt_markdown = 0;

if (! GetOptions(
        "o|output=s"       => \$opt_output,
        "f|fontdef=s"      => \$opt_fontdef,
        "fontdef-add=s"    => \@opt_fontdef_add,
        "a|alias=s"        => \@opt_aliases,
        "filelist=s"       => \$opt_filelist,
        "link-texmf:s"     => \$opt_texmflink,
        "otfps:s"          => \$opt_akotfps,
        "force"            => \$opt_force,
        "remove"           => \$opt_remove,
        "cleanup"          => \$opt_cleanup,
        "hardlink"         => \$opt_hardlink,
        "winbatch:s"       => \$opt_winbatch,
        "dump-data:s"      => \$opt_dump_data,
        "only-aliases"     => \$opt_only_aliases,
        "list-aliases"     => \$opt_listaliases,
        "list-all-aliases" => \$opt_listallaliases,
        "list-fonts"       => \$opt_listfonts,
        "info"             => \$opt_info,
        "machine-readable" => \$opt_machine,
        "strict-psname"    => \$opt_strictpsname, # hidden option for debugging
        "n|dry-run"        => \$dry_run,
        "q|quiet"          => \$opt_quiet,
        "d|debug+"         => \$opt_debug,
        "h|help"           => \$opt_help,
        "markdown"         => \$opt_markdown,
        "v|version"        => sub { print &version(); exit(0); }, ) ) {
  die "Try \"$0 --help\" for more information.\n";
}

sub win32 { return ($^O=~/^MSWin(32|64)$/i); }
sub macosx { return ($^O=~/^darwin$/i); }
my $nul = (win32() ? 'nul' : '/dev/null') ;
my $sep = (win32() ? ';' : ':');
my %fontdb;
my %aliases;
my %user_aliases;

if ($opt_help || $opt_markdown) {
  Usage();
  exit(0);
}

# check for the existence of kpsewhich, otherwise we cannot do anything
if (system("kpsewhich --version >$nul 2>&1 <$nul" ) != 0) {
  print_error("We need `kpsewhich' being installed! Exiting.\n");
  exit(1);
}

if ($opt_debug >= 2) {
  require Data::Dumper;
  $Data::Dumper::Indent = 1;
}

my $zrlistttc = kpse_miscfont("zrlistttc.lua");
my $zrlistttc_available;
chomp(my $zrlistttc_help = `texlua $zrlistttc 2>$nul`);
if ($?) {
  if ($opt_strictpsname) {
    print_error("The script 'zrlistttc.lua' not found, cannot proceed!\n");
    exit(1);
  }
  # show info only for debugging
  print_debug("The script 'zrlistttc.lua' not found.\n");
  print_debug("Sorry, we can't be safe enough to distinguish\n");
  print_debug("uppercase / lowercase file names.\n");
  $zrlistttc_available = 0;
} else {
  $zrlistttc_available = 1;
}

if (macosx()) {
  # due to frequent incompatible changes in font file names by Apple,
  # our built-in database doesn't support OS X 10.11 El Capitan or
  # later versions
  my $macos_ver = `sw_vers -productVersion`;
  my $macos_ver_major = $macos_ver;
  $macos_ver_major =~ s/^(\d+)\.(\d+).*/$1/;
  my $macos_ver_minor = $macos_ver;
  $macos_ver_minor =~ s/^(\d+)\.(\d+).*/$2/;
  if ($macos_ver_major==10 && $macos_ver_minor>=8) {
    if (!$opt_cleanup && !$opt_fontdef && !@opt_fontdef_add) { # if built-in only
      print_warning("Our built-in database does not support recent\n");
      print_warning("versions of Mac OS (10.8 Mountain Lion or later)!\n");
      print_warning("If you want to use Hiragino fonts bundled with\n");
      print_warning("your OS, obtain external database file and\n");
      print_warning("specify it with --fontdef-add option!\n");
      print_warning("I'll continue with my built-in database ...\n");
    }
  }
}

if (defined($opt_texmflink)) {
  my $foo;
  if ($opt_texmflink eq '') {
    # option was passed but didn't receive a value
    #  -> use TEXMFLOCAL
    chomp($foo = `kpsewhich -var-value=TEXMFLOCAL`);
  } else {
    # option was passed with an argument
    #  -> use it
    $foo = $opt_texmflink;
  }
  $opt_texmflink = $foo;
}

if (defined($opt_akotfps)) {
  my $foo;
  if ($opt_akotfps eq '') {
    if (defined($opt_texmflink)) {
      $foo = $opt_texmflink;
    } else {
      chomp($foo = `kpsewhich -var-value=TEXMFLOCAL`);
    }
  } else {
    $foo = $opt_akotfps;
  }
  $opt_akotfps = $foo;
}

if (defined($opt_winbatch)) {
  if ($opt_winbatch ne '') {
    $winbatch = $opt_winbatch;
  }
  if (win32()) {
    $opt_winbatch = 1;
    unlink $winbatch if (-f $winbatch);
  } else {
    print_warning("ignoring --winbatch option due to non-Windows\n");
    $opt_winbatch = 0;
  }
} else {
  $opt_winbatch = 0;
}
if ($opt_hardlink) {
  if (win32()) {
    $opt_hardlink = 1;
  } else {
    print_warning("ignoring --hardlink option due to non-Windows\n");
    $opt_hardlink = 0;
  }
}

if (defined($opt_dump_data)) {
  if ($opt_dump_data ne '') {
    $dump_datafile = $opt_dump_data;
  }
  $opt_dump_data = 1;
  unlink $dump_datafile if (-f $dump_datafile);
} else {
  $opt_dump_data = 0;
}

if ($opt_cleanup) {
  $opt_remove = 1;
}

if ($opt_info) {
  $opt_listfonts = 1;
  $opt_listaliases = 1;
}

# check exclusive options; unsafe due to make_all_available()
if ($opt_listallaliases && $opt_listaliases) {
  print_error("Both --list-all-aliases and --list-aliases!? I'm confused!\n");
  exit(1);
}
if ($opt_listallaliases && $opt_listfonts) {
  print_error("Options --list-all-aliases and --list-fonts cannot be used at the same time!\n");
  exit(1);
}
if ($opt_cleanup && $opt_listfonts) {
  print_error("Options --cleanup and --list-fonts cannot be used at the same time!\n");
  exit(1);
}
if ($opt_cleanup && $opt_listaliases) {
  print_error("Options --cleanup and --list-aliases cannot be used at the same time!\n");
  exit(1);
}

main(@ARGV);

#
# only sub definitions from here on
#
sub main {
  # first, read font database to obtain %fontdb
  print_info("reading font database ...\n");
  read_font_database();
  if ($opt_dump_data) {
    # with --dump-data, dump only effective database and exit
    dump_font_database();
    if (-f $dump_datafile) {
      print_info("*** Data dumped to $dump_datafile ***\n");
      exit(0);
    } else {
      print_error("Failed to dump data to $dump_datafile!\n");
      exit(1);
    }
  }
  # second, determine non-otf link name
  # this is actually required only by info_found_fonts() and do_nonotf_fonts()
  # operations, but it does no harm for other cases too
  determine_nonotf_link_name(); # see comments there

  # set 'available' flags and 'type' by kpsewhich search
  # if $opt_cleanup or $opt_listallaliases is given, treat all files
  # in the database as if they were actually available as OTF
  if (!$opt_cleanup && !$opt_listallaliases) {
    print_info("checking for files ...\n");
    check_for_files();
  } else {
    make_all_available();
  }
  # obtain %aliases and %user_aliases
  compute_aliases();

  # informative operations
  if ($opt_listfonts) {
    info_found_fonts();
  }
  if ($opt_listaliases || $opt_listallaliases) {
    info_list_aliases();
  }
  exit(0) if ($opt_listfonts || $opt_listaliases || $opt_listallaliases);
  # if $opt_machine is still alive after the above exit(0), it's useless
  if ($opt_machine) {
    print_error("Option --machine-readable should be used with at least one of the followings:\n");
    print_error("  --list-aliases, --list-all-aliases, --list-fonts, --info\n");
    print_error("terminating.\n");
    exit(1);
  }

  # do actual setup/removing operations
  if (!$opt_output) {
    print_info("searching for Ghostscript resource\n");
    my $gsres = find_gs_resource();
    if (!$gsres) {
      print_error("Cannot find Ghostscript, terminating!\n");
      exit(1);
    } else {
      $opt_output = $gsres;
    }
  }
  if (! -d $opt_output) {
    $dry_run || mkdir($opt_output) ||
      die("Cannot create directory $opt_output: $!");
  }
  if ($opt_cleanup) {
    print_info("going to clean up $opt_output\n");
  } else {
    print_info("output is going to $opt_output\n");
  }
  if (!$opt_only_aliases) {
    if ($opt_cleanup) {
      # all font types are handled at the same time
      print_info("cleaning up all links, snippets and cidfmap.local ...\n");
      do_all_fonts();
    } else {
      # OTF and TTF/TTC/OTC must be handled separately, depending on the found files
      print_info(($opt_remove ? "removing" : "generating") . " links and snippets for CID fonts ...\n");
      do_otf_fonts();
      print_info(($opt_remove ? "removing" : "generating") . " links, snippets and cidfmap.local for non-CID fonts ...\n");
      do_nonotf_fonts();
    }
    write_winbatch() if $opt_winbatch;
  }
  print_info(($opt_remove ? "removing" : "generating") . " snippets and cidfmap.aliases for font aliases ...\n");
  do_aliases();
  write_akotfps_datafile() if $opt_akotfps;
  if ($opt_texmflink && !$dry_run) {
    print_info("running mktexlsr ...\n");
    system("mktexlsr");
  }
  print_info("finished\n");
  if ($opt_winbatch) {
    if (-f $winbatch) {
      print_info("*** Batch file $winbatch created ***\n");
      print_info("*** to complete, run it as administrator privilege.***\n");
    } else {
      print_error("Failed to create $winbatch!\n");
      exit(1);
    }
  }
}

sub do_all_fonts {
  # try to clean up all possible links/snippets/cidfmap which could have been
  # generated in the previous runs
  # previous versions of cjk-gs-integrate included following bugs:
  #   * the database sometimes identified GB/CNS classes wrongly
  #   * symlink names were sometimes invalid (some of which contained
  #     white-spaces, due to the absence of proper database entry) or
  #     inconsistent with ptex-fontmaps (Name <-> PSName or redundant Filename)
  #   * confused symlinks between TTF/TTC/OTC (including ttf <-> ttc links)
  # also, current version generates OTC links into $otf_pathpart instead of
  # $ttf_pathpart, which was not true in the older versions
  # we'd like to clean up all such files
  my $fontdest = "$opt_output/Font";
  my $ciddest  = "$opt_output/CIDFont";
  my $cidfsubst = "$opt_output/CIDFSubst";
  for my $k (sort keys %fontdb) {
    #
    # remove snippets: note that $class = $fontdb{$k}{'class'} is not enough
    # due to previous bugs
    for my $class (%encode_list) {
      for my $enc (@{$encode_list{$class}}) {
        unlink "$fontdest/$k-$enc" if (-f "$fontdest/$k-$enc");
      }
    }
    #
    # remove links; borrow link_font operation here for convenience
    # since we don't need target in cleanup mode, initialize with stub "none"
    #
    # for OTF/TTF fonts, first remove both $k[.otf,ttf] and $fontdb{$k}{'origname'}[.otf]
    # the links $k.otf and $fontdb{$k}{'origname'} are coming from previous bugs,
    # and the links $k.ttf are (sometimes) coming from inconsistency
    if ($fontdb{$k}{'origname'}) { # this test skips alias-only fonts (e.g. Ryumin-Light)
      link_font("none", $ciddest, $k);
      link_font("none", $cidfsubst, "$k.ttf");
      link_font("none", "$opt_texmflink/$otf_pathpart", "$k.otf")
        if $opt_texmflink;
      link_font("none", "$opt_texmflink/$ttf_pathpart", "$k.ttf")
        if $opt_texmflink;
      link_font("none", $ciddest, $fontdb{$k}{'origname'});
      link_font("none", "$opt_texmflink/$otf_pathpart", "$fontdb{$k}{'origname'}.otf")
        if $opt_texmflink;
    }
    # for OTF/TTF/TTC/OTC fonts, loop through all file candidates
    for my $f (keys %{$fontdb{$k}{'files'}}) {
      # check for subfont extension
      my $foo = $f;
      $foo =~ s/^(.*)\(\d*\)$/$1/;
      link_font("none", $cidfsubst, "$foo");
      link_font("none", "$opt_texmflink/$otf_pathpart", "$foo") if $opt_texmflink;
      link_font("none", "$opt_texmflink/$ttf_pathpart", "$foo") if $opt_texmflink;
    }
  }
  update_master_cidfmap('cidfmap.local');
  # we are in cleanup mode, also remove cidfmap.local itself
  if (-f "$opt_output/$cidfmap_local_pathpart") {
    unlink "$opt_output/$cidfmap_local_pathpart";
  }
}

sub do_otf_fonts {
  my $fontdest = "$opt_output/Font";
  my $ciddest  = "$opt_output/CIDFont";
  make_dir($fontdest, "cannot create CID snippets there!");
  make_dir($ciddest,  "cannot link CID fonts there!");
  make_dir("$opt_texmflink/$otf_pathpart",
           "cannot link fonts to it!")
    if $opt_texmflink;
  for my $k (sort keys %fontdb) {
    if ($fontdb{$k}{'available'} && $fontdb{$k}{'type'} eq 'OTF') {
      generate_font_snippet($fontdest,
        $k, $fontdb{$k}{'class'}, $fontdb{$k}{'target'});
      link_font($fontdb{$k}{'target'}, $ciddest, $k);
      link_font($fontdb{$k}{'target'}, "$opt_texmflink/$otf_pathpart", "$fontdb{$k}{'origname'}.otf")
        if $opt_texmflink;
    }
  }
}

sub do_nonotf_fonts {
  my $fontdest = "$opt_output/Font";
  my $cidfsubst = "$opt_output/CIDFSubst";
  my $outp = '';
  make_dir($fontdest, "cannot create CID snippets there!");
  make_dir($cidfsubst,  "cannot link TTF fonts there!");
  make_dir("$opt_texmflink/$ttf_pathpart",
           "cannot link fonts to it!")
    if $opt_texmflink;
  for my $k (sort keys %fontdb) {
    if ($fontdb{$k}{'available'} && $fontdb{$k}{'type'} eq 'TTF') {
      $outp .= generate_cidfmap_entry($k, $fontdb{$k}{'class'}, $fontdb{$k}{'ttfname'}, -1);
      link_font($fontdb{$k}{'target'}, $cidfsubst, $fontdb{$k}{'ttfname'});
      link_font($fontdb{$k}{'target'}, "$opt_texmflink/$ttf_pathpart", $fontdb{$k}{'ttfname'})
        if $opt_texmflink;
    } elsif ($fontdb{$k}{'available'} && $fontdb{$k}{'type'} eq 'TTC') {
      $outp .= generate_cidfmap_entry($k, $fontdb{$k}{'class'}, $fontdb{$k}{'ttcname'}, $fontdb{$k}{'subfont'});
      link_font($fontdb{$k}{'target'}, $cidfsubst, $fontdb{$k}{'ttcname'});
      link_font($fontdb{$k}{'target'}, "$opt_texmflink/$ttf_pathpart", $fontdb{$k}{'ttcname'})
        if $opt_texmflink;
    } elsif ($fontdb{$k}{'available'} && $fontdb{$k}{'type'} eq 'OTC') {
      # currently Ghostscript does not have OTC support; we don't know what to do
      print_debug("gs does not support OTC, not creating gs resource for $k\n");
      link_font($fontdb{$k}{'target'}, "$opt_texmflink/$otf_pathpart", $fontdb{$k}{'otcname'})
        if $opt_texmflink;
    }
  }
  return if $dry_run;
  if ($outp) {
    if (! -d "$opt_output/Init") {
      mkdir("$opt_output/Init") ||
        die("Cannot create directory $opt_output/Init: $!");
    }
    open(FOO, ">$opt_output/$cidfmap_local_pathpart") ||
      die("Cannot open $opt_output/$cidfmap_local_pathpart: $!");
    print FOO $outp;
    close(FOO);
  }
  update_master_cidfmap('cidfmap.local');
}

sub do_aliases {
  my $fontdest = "$opt_output/Font";
  my $ciddest  = "$opt_output/CIDFont"; # required for Heisei* check only
  my $cidfsubst = "$opt_output/CIDFSubst";
  my $outp = '';
  #
  # alias handling
  # we use two levels of aliases
  #  * one is for the default generic names (these are not actual fonts)
  #      Ryumin-Light, GothicBBB-Medium, ... etc.
  #  * the second level of aliases is for Morisawa OTF font names
  #      RyuminPro-Light, GothicBBBPro-Medium, ... etc.
  # the order of fonts selected is
  # defined in the Provides(Priority): Name in the font definiton
  #
  $outp .= "\n\n% Aliases\n";
  #
  my (@jal, @kal, @tal, @sal);
  #
  for my $al (sort keys %aliases) {
    my $target;
    my $class;
    if ($user_aliases{$al}) {
      $target = $user_aliases{$al};
      # determine class
      if ($fontdb{$target}{'available'}) {
        $class = $fontdb{$target}{'class'};
      } else {
        # must be an aliases, we checked this when initializing %user_aliases
        # reset the $al value
        # and since $class is still undefined we will use the next code below
        $al = $target;
      }
    }
    if (!$class) {
      if (!%{$aliases{$al}}) {
        print_warning("Alias candidate for $al is empty, skipping!\n");
        next;
      }
      # search lowest number
      my @ks = keys(%{$aliases{$al}});
      my $first = (sort { $a <=> $b} @ks)[0];
      $target = $aliases{$al}{$first};
      $class  = $fontdb{$target}{'class'};
    }
    # we also need to create font snippets in Font (or add configuration)
    # for the aliases!
    generate_font_snippet($fontdest, $al, $class, $target);
    if ($class eq 'Japan') {
      push @jal, "/$al /$target ;";
    } elsif ($class eq 'Korea') {
      push @kal, "/$al /$target ;";
    } elsif ($class eq 'GB') {
      push @sal, "/$al /$target ;";
    } elsif ($class eq 'CNS') {
      push @tal, "/$al /$target ;";
    } else {
      print STDERR "unknown class $class for $al\n";
    }
  }
  # special case for native CID fonts in ancient days
  # if not readable, add aliases for substitution
  push @jal, "/HeiseiMin-W3 /Ryumin-Light ;" if (! -r "$ciddest/HeiseiMin-W3");
  push @jal, "/HeiseiKakuGo-W5 /GothicBBB-Medium ;" if (! -r "$ciddest/HeiseiKakuGo-W5");
  #
  $outp .= "\n% Japanese fonts\n" . join("\n", @jal) . "\n" if @jal;
  $outp .= "\n% Korean fonts\n" . join("\n", @kal) . "\n" if @kal;
  $outp .= "\n% Traditional Chinese fonts\n" . join("\n", @tal) . "\n" if @tal;
  $outp .= "\n% Simplified Chinese fonts\n" . join("\n", @sal) . "\n" if @sal;
  #
  return if $dry_run;
  if ($outp && !$opt_remove) {
    if (! -d "$opt_output/Init") {
      mkdir("$opt_output/Init") ||
        die("Cannot create directory $opt_output/Init: $!");
    }
    open(FOO, ">$opt_output/$cidfmap_aliases_pathpart") ||
      die("Cannot open $opt_output/$cidfmap_aliases_pathpart: $!");
    print FOO $outp;
    close(FOO);
  }
  update_master_cidfmap('cidfmap.aliases');
  # if we are in cleanup mode, also remove cidfmap.aliases itself
  if (-f "$opt_output/$cidfmap_aliases_pathpart") {
    unlink "$opt_output/$cidfmap_aliases_pathpart" if $opt_cleanup;
  }
}

sub update_master_cidfmap {
  # what we have to do is:
  #   in add mode:
  #     * add an entry for the given argument
  #     * for tlgs.win32 pre-shipped cidfmap, prepend '%' to override
  #       the default of "(cidfmap.TeXLive) .runlibfile",
  #   in remove mode:
  #     * remove an entry for the given argument
  #     * for tlgs.win32 pre-shipped cidfmap, remove '%' to restore the default
  my $add = shift;
  my $cidfmap_master = "$opt_output/$cidfmap_pathpart";
  print_info(sprintf("%s $add %s cidfmap file ...\n",
    ($opt_remove ? "removing" : "adding"), ($opt_remove ? "from" : "to")));
  if (-r $cidfmap_master) {
    open(FOO, "<", $cidfmap_master) ||
      die("Cannot open $cidfmap_master for reading: $!");
    my $found = 0;
    my $found_tl = 0;
    my $newmaster = "";
    # in add mode: just search for the entry and set $found
    # in remove mode: collect all lines that do not match
    # also, we handle "cidfmap.TeXLive" now
    while(<FOO>) {
      if (m/^\s*\(\Q$add\E\)\s\s*\.runlibfile\s*$/) {
        $found = 1;
      } elsif (m/^\s*\(cidfmap\.TeXLive\)\s\s*\.runlibfile\s*$/) {
        # if found, it has to be disabled in add mode in a way in which it can
        # be detected in the (future) remove mode
        next if $found_tl; # skip it as duplicate (though unlikely to happen)
        $found_tl = 1;
        $newmaster .= "\%" if (!$opt_remove); # in add mode, disable it
        $newmaster .= $_; # pass it as-is
      } elsif (m/^\s*\%\%*\s*\(cidfmap\.TeXLive\)\s\s*\.runlibfile\s*$/) {
        # if found, it should be the one disabled by myself in the previous run;
        # restore it in remove mode
        next if $found_tl; # skip it as duplicate (though unlikely to happen)
        $found_tl = 1;
        $_ =~ s/\%//g if $opt_remove; # in remove mode, enable it
        $newmaster .= $_; # pass it
      } else {
        $newmaster .= $_;
      }
    }
    close(FOO);
    # if the original master cidfmap has a new line at end of file,
    # then $newmaster should end with "\n".
    # otherwise we add a new line, since there is a possibility of %EOF comment
    # without trailing new line (e.g. TL before r44039)
    $newmaster =~ s/\n$//;
    $newmaster =~ s/$/\n/;
    if ($opt_remove) {
      if ($found || $found_tl) {
        return if $dry_run;
        open(FOO, ">", $cidfmap_master) ||
          die("Cannot clean up $cidfmap_master: $!");
        print FOO $newmaster;
        close FOO;
      }
    } else {
      if ($found && !$found_tl) {
        print_info("$add already loaded in $cidfmap_master, no changes\n");
      } else {
        return if $dry_run;
        open(FOO, ">", $cidfmap_master) ||
          die("Cannot open $cidfmap_master for appending: $!");
        print FOO $newmaster;
        print FOO "($add) .runlibfile\n";
        close(FOO);
      }
    }
  } else {
    return if $dry_run;
    return if $opt_remove;
    open(FOO, ">", $cidfmap_master) ||
      die("Cannot open $cidfmap_master for writing: $!");
    print FOO "($add) .runlibfile\n";
    close(FOO);
  }
}

sub generate_cidfmap_entry {
  my ($n, $c, $f, $sf) = @_;
  return "" if $opt_remove;
  # $f is already the link target name 'ttfname' (or 'ttcname' or 'otcname')
  # as determined by minimal priority number
  # extract subfont
  my $s = "/$n << /FileType /TrueType 
  /Path pssystemparams /GenericResourceDir get 
  (CIDFSubst/$f) concatstrings\n";
  if ($sf >= 0) { # in this script, $sf < 0 represents TTF
    $s .= "  /SubfontID $sf\n";
  }
  $s .= "  /CSI [($c";
  if ($c eq "Japan") {
    $s .= "1) 6]";
  } elsif ($c eq "GB") {
    $s .= "1) 5]";
  } elsif ($c eq "CNS") {
    $s .= "1) 5]";
  } elsif ($c eq "Korea") {
    $s .= "1) 2]";
  } else {
    print_warning("unknown class $c for $n, skipping.\n");
    return '';
  }
  $s .= " >> ;\n";
  return $s;
}

sub generate_font_snippet {
  my ($fd, $n, $c, $f) = @_;
  return if $dry_run;
  if ($opt_akotfps) {
    add_akotfps_data($n);
    return;
  }
  for my $enc (@{$encode_list{$c}}) {
    if ($opt_remove) {
      unlink "$fd/$n-$enc" if (-f "$fd/$n-$enc");
      next;
    }
    open(FOO, ">$fd/$n-$enc") ||
      die("cannot open $fd/$n-$enc for writing: $!");
    print FOO "%!PS-Adobe-3.0 Resource-Font
%%DocumentNeededResources: $enc (CMap)
%%IncludeResource: $enc (CMap)
%%BeginResource: Font ($n-$enc)
($n-$enc)
($enc) /CMap findresource
[($n) /CIDFont findresource]
composefont
pop
%%EndResource
%%EOF
";
    close(FOO);
  }
}

sub add_akotfps_data {
  my ($fn) = @_;
  return if $dry_run;
  if (!$opt_remove) {
    $akotfps_datacontent .= "$fn\n";
  }
}

#
# link_font operation
# $opt_force is *not* treated first to warn only
# at really critical cases
# case 1:
#   exists, is link, link targets agree
#     $opt_force is ignored
#     remove or remove+add according to $opt_remove
# case 2:
#   exists, is link, dangling symlink
#     $opt_force is ignored
#     remove or remove+add according to $opt_remove
# case 3:
#   exists, is link, link target different
#     if $opt_force
#       warn, remove or remove+add according to $opt_remove
#     else
#       error message
# case 4:
#   exists, not a link
#     if $opt_force
#       warn, remove or remove+add according to $opt_remove
#     else
#       error message
# case 5:
#   not exists
#     $opt_force is ignored
#     do nothing or add according to $opt_remove
#
sub link_font {
  my ($f, $cd, $n) = @_;
  return if $dry_run;
  if (!$n) {
    $n = basename($f);
  }
  my $target = "$cd/$n";
  my $do_unlink = 0;
  if (-l $target) {
    if ($opt_cleanup) {
      $do_unlink = 1;
    } else {
      my $linkt = readlink($target);
      if ($linkt) {
        if ($linkt eq $f) {
          # case 1: exists, link, targets agree
          $do_unlink = 1;
        } elsif (-r $linkt) {
          # case 3: exists, link, targets different
          if ($opt_force) {
            print_info("Removing link $target due to --force!\n");
            $do_unlink = 1;
          } else {
            print_error("Link $target already existing, but target different from $f, exiting!\n");
            exit(1);
          }
        } else {
          # case 2: dangling symlink or link-to-link
          print_warning("Removing dangling symlink $target to $linkt\n");
          $do_unlink = 1;
        }
      } else {
        print_error("This should not happen, we have a link but cannot read the target?\n");
        exit(1);
      }
    }
  } elsif (-r $target) {
    # case 4: exists, but not link (NTFS hardlink on win32 falls into this)
    if (-s $target) {
      if ($opt_force) {
        print_info("Removing $target due to --force!\n");
        $do_unlink = 1;
      } else {
        print_error("$target already existing, exiting!\n");
        exit(1);
      }
    } else {
      # NTFS symlink on win32 has file size 0, we're safe to remove it
      $do_unlink = 1;
    }
  } # case 5: otherwise it is not existing!

  # if we are still here and $do_unlink is set, remove it
  maybe_unlink($target) if $do_unlink;
  # recreate link if we are not in the remove case
  if (!$opt_remove) {
    maybe_symlink($f, $target) || die("Cannot link font $f to $target: $!");
  }
}

sub make_dir {
  my ($d, $w) = @_;
  if (-r $d) {
    if (! -d $d) {
      print_error("$d is not a directory, $w\n");
      exit(1);
    }
  } else {
    $dry_run || make_path($d);
  }
}

# perl symlink function does not work on windows, so leave it to
# cmd.exe mklink function (or write to batch file).
# if target already exists, do not try to override it. otherwise
# "mklink error: Cannot create a file when that file already exists"
# is thrown many times
sub maybe_symlink {
  my ($realname, $targetname) = @_;
  if (win32()) {
    # hardlink vs. symlink -- HY 2017/04/26
    #   * readablitiy: hardlink is easier, but it seems that current gs can read
    #                  symlink properly, so it doesn't matter
    #   * permission:  hardlink creation does not require administrator privilege,
    #                  but is likely to fail for c:/windows/fonts/* system files
    #                  due to "Access denied"
    #                  symlink creation requires administrator privilege, but
    #                  it can link to all files in c:/windows/fonts/
    #   * versatility: symlink can point to a file on a different/remote volume
    # for these reasons, we try to create symlink by default.
    # if --hardlink option is given, we create hardlink instead.
    # also, if --winbatch option is given, we prepare batch file for link generation,
    # instead of creating links right away.
    $realname =~ s!/!\\!g;
    $targetname =~ s!/!\\!g;
    if ($opt_winbatch) {
      # re-encoding of $winbatch_content is done by write_winbatch()
      $winbatch_content .= "if not exist \"$targetname\" mklink ";
      $winbatch_content .= "/h " if $opt_hardlink;
      $winbatch_content .= "\"$targetname\" \"$realname\"\n";
    } else {
      # should be encoded in cp932 for win32 console
      $realname = encode_utftocp($realname);
      $targetname = encode_utftocp($targetname);
      my $cmdl = "cmd.exe /c if not exist \"$targetname\" mklink ";
      $cmdl .= "/h " if $opt_hardlink;
      $cmdl .= "\"$targetname\" \"$realname\"";
      my @ret = `$cmdl`;
      # sometimes hard link creation may fail due to "Access denied"
      # (especially when $realname is located in c:/windows/fonts).
      # TODO: what should we do to ensure resources, which might be
      #       different from $realname? -- HY (2017/03/21)
      # -- one possibility:
      # if (@ret) {
      #   @ret ="done";
      # } else {
      #   print_info("Hard link creation for $realname failed. I will copy this file instead.\n");
      #   $cmdl = "cmd.exe /c if not exist \"$targetname\" copy \"$realname\" \"$targetname\"";
      #   @ret = `$cmdl`;
      # }
      # -- however, both tlgs (TeX Live) and standalone gswin32/64 (built
      #    by Akira Kakuto) can search in c:/windows/fonts by default.
      #    Thus, copying such files is waste of memory
    }
  } else {
    symlink ($realname, $targetname);
  }
}

# unlink function actually works also on windows, however,
# leave it to batch file for consistency. otherwise
# option $opt_force may not work as expected
sub maybe_unlink {
  my ($targetname) = @_;
  if (win32()) {
    $targetname =~ s!/!\\!g;
    if ($opt_winbatch) {
      # re-encoding of $winbatch_content is done by write_winbatch()
      $winbatch_content .= "if exist \"$targetname\" del \"$targetname\"\n";
    } else {
      # should be encoded in cp932 for win32 console
      $targetname = encode_utftocp($targetname);
      my $cmdl = "cmd.exe /c if exist \"$targetname\" del \"$targetname\"";
      my @ret = `$cmdl`;
    }
  } else {
    unlink ($targetname);
  }
}

# write batch file (windows only)
sub write_winbatch {
  return if $dry_run;
  open(FOO, ">$winbatch") ||
    die("cannot open $winbatch for writing: $!");
  # $winbatch_content may contain multibyte characters, and they
  # should be encoded in cp932 in batch file
  $winbatch_content = encode_utftocp($winbatch_content);
  print FOO "\@echo off\n",
            "$winbatch_content",
            "\@echo symlink ", ($opt_remove ? "removed\n" : "generated\n"),
            "\@pause 1\n";
  close(FOO);
}

# write to psnames-for-otfps
sub write_akotfps_datafile {
  return if $dry_run;
  make_dir("$opt_akotfps/$akotfps_pathpart",
         "cannot create $akotfps_datafilename in it!");
  open(FOO, ">$opt_akotfps/$akotfps_pathpart/$akotfps_datafilename") ||
    die("cannot open $opt_akotfps/$akotfps_pathpart/$akotfps_datafilename for writing: $!");
  print FOO "% psnames-for-otf
%
% PostSctipt names for OpenType fonts
%
% This file is used by a program ps2otfps
% in order to add needed information to a ps file
% created by the dvips
%
$akotfps_datacontent";
  close(FOO);
}

#
# dump found files
sub info_found_fonts {
  print "List of found fonts:\n\n";
  for my $k (sort keys %fontdb) {
    my @foundfiles;
    if ($fontdb{$k}{'available'}) {
      print "Font:  $k\n";
      print "Type:  $fontdb{$k}{'type'}\n";
      print "Class: $fontdb{$k}{'class'}\n";
      my $fn = $fontdb{$k}{'target'};
      # cp932 for win32 console
      if (win32()) {
        $fn = encode_utftocp($fn);
      }
      if ($fontdb{$k}{'type'} eq 'TTC' || $fontdb{$k}{'type'} eq 'OTC') {
        $fn .= "($fontdb{$k}{'subfont'})";
      }
      print "File:  $fn\n";
      if ($fontdb{$k}{'type'} eq 'TTF') {
        print "Link:  $fontdb{$k}{'ttfname'}\n";
      } elsif ($fontdb{$k}{'type'} eq 'TTC') {
        print "Link:  $fontdb{$k}{'ttcname'}\n";
      } elsif ($fontdb{$k}{'type'} eq 'OTC') {
        print "Link:  $fontdb{$k}{'otcname'}\n";
      }
      my @ks = sort { $fontdb{$k}{'files'}{$a}{'priority'}
                      <=>
                      $fontdb{$k}{'files'}{$b}{'priority'} }
                    keys %{$fontdb{$k}{'files'}};
      # remove the top element which is the winner and shown above
      shift @ks;
      if (@ks) {
        print "Other candidates in decreasing order:\n";
        for my $f (@ks) {
          print "       ", $fontdb{$k}{'files'}{$f}{'target'}, "\n";
        }
      }
      print "\n";
    }
  }
}

#
# dump aliases
sub info_list_aliases {
  print "List of ", ($opt_listallaliases ? "all" : "available"), " aliases and their options (in decreasing priority):\n" unless $opt_machine;
  my (@jal, @kal, @tal, @sal);
  for my $al (sort keys %aliases) {
    my $cl;
    my @ks = sort { $a <=> $b} keys(%{$aliases{$al}});
    my $foo = '';
    $foo = "$al:\n" unless $opt_machine;
    for my $p (@ks) {
      my $t = $aliases{$al}{$p};
      my $fn = ($opt_listallaliases ? "-" : $fontdb{$t}{'target'} );
      # cp932 for win32 console
      if (win32()) {
        $fn = encode_utftocp($fn);
      }
      # should always be the same ;-)
      $cl = $fontdb{$t}{'class'};
      if (!$opt_listallaliases && ($fontdb{$t}{'type'} eq 'TTC' || $fontdb{$t}{'type'} eq 'OTC')) {
        $fn .= "($fontdb{$t}{'subfont'})";
      }
      if ($opt_machine) {
        $foo .= "$al:$p:$aliases{$al}{$p}:$fn\n";
      } else {
        $foo .= "\t($p) $aliases{$al}{$p} ($fn)\n";
      }
    }
    if ($cl eq 'Japan') {
      push @jal, $foo;
    } elsif ($cl eq 'Korea') {
      push @kal, $foo;
    } elsif ($cl eq 'GB') {
      push @sal, $foo;
    } elsif ($cl eq 'CNS') {
      push @tal, $foo;
    } else {
      print STDERR "unknown class $cl for $al\n";
    }
  }
  if ($opt_machine) {
    print @jal if @jal;
    print @kal if @kal;
    print @sal if @sal;
    print @tal if @tal;
  } else {
    print "Aliases for Japanese fonts:\n", @jal, "\n" if @jal;
    print "Aliases for Korean fonts:\n", @kal, "\n" if @kal;
    print "Aliases for Simplified Chinese fonts:\n", @sal, "\n" if @sal;
    print "Aliases for Traditional Chinese fonts:\n", @tal, "\n" if @tal;
  }
}

#
# make all fonts available for listing all aliases
sub make_all_available {
  for my $k (keys %fontdb) {
    $fontdb{$k}{'available'} = 1;
    $fontdb{$k}{'type'} = 'OTF';
    delete $fontdb{$k}{'files'} if (!$opt_cleanup);
  }
}

#
# checks all file names listed in %fontdb
# and sets
sub check_for_files {
  my @foundfiles;
  if ($opt_filelist) {
    open(FOO, "<", $opt_filelist) || die("Cannot open $opt_filelist: $!");
    @foundfiles = <FOO>;
    close(FOO) || warn "Cannot close $opt_filelist: $!";
  } else {
    # first collect all files:
    my @fn;
    for my $k (keys %fontdb) {
      for my $f (keys %{$fontdb{$k}{'files'}}) {
        # check for subfont extension
        if ($f =~ m/^(.*)\(\d*\)$/) {
          push @fn, $1;
        } else {
          push @fn, $f;
        }
      }
    }
    #
    # collect extra directories for search
    my @extradirs;
    if (win32()) {
      push @extradirs, "c:/windows/fonts//";
    } else {
      # other dirs to check, for normal unix?
      for my $d (qw!/Library/Fonts /System/Library/Fonts
                    /System/Library/Assets /System/Library/AssetsV2
                    /Network/Library/Fonts /usr/share/fonts!) {
        push @extradirs, "$d//" if (-d $d); # recursive search
      }
      # the path contains white space, so hack required
      for my $d (qw!/Library/Application__Support/Apple/Fonts!) {
        my $sd = $d;
        $sd =~ s/__/ /;
        push @extradirs, "$sd//" if (-d "$sd"); # recursive search
      }
      # office for mac 2016
      for my $d (qw!/Applications/Microsoft__Word.app
                    /Applications/Microsoft__Excel.app
                    /Applications/Microsoft__PowerPoint.app!) {
        my $sd = $d;
        $sd =~ s/__/ /;
        push @extradirs, "$sd/Contents/Resources/Fonts/" if (-d "$sd/Contents/Resources/Fonts");
        push @extradirs, "$sd/Contents/Resources/DFonts/" if (-d "$sd/Contents/Resources/DFonts");
      }
      my $home = $ENV{'HOME'};
      push @extradirs, "$home/Library/Fonts//" if (-d "$home/Library/Fonts");
    }
    #
    if (@extradirs) {
      # TODO: we want that files in OSFONTDIR are found first, before
      # links that we have created in TEXMFLOCAL
      # Thus, instead of setting OSFONTDIR which is at the *END* of
      # the kpsewhich variables OPENTYPEFONTS and TTFONTS, we'd like to
      # put all these fonts at the front of them
      #
      # There are problems with case-insensitive file systems like HFS
      # on MacOS, as we might catch different names (batang/Batang)
      # and identify them wrongly.
      # https://github.com/texjporg/cjk-gs-support/issues/9
      # For now until we have dealt with that, do not set the
      # two variables (HY 2016/09/27) and think about a different approach.
      push @extradirs, $ENV{'OSFONTDIR'} if $ENV{'OSFONTDIR'};
      if (@extradirs) {
        # comment out -- HY (2016/09/27)
        # my $newotf = join($sep, @extradirs) . $sep;
        # my $newttf = $newotf;
        # $newotf .= $ENV{'OPENTYPEFONTS'} if $ENV{'OPENTYPEFONTS'};
        # $newttf .= $ENV{'TTFONTS'} if $ENV{'TTFONTS'};
        # $ENV{'OPENTYPEFONTS'} = $newotf;
        # $ENV{'TTFONTS'} = $newttf;
        # new code for uppercase/lowercase workaround -- HY (2016/09/27)
        my $extrafontdir = join($sep, @extradirs) . $sep;
        $ENV{'OSFONTDIR'} = $extrafontdir;
      }
    }
    # prepare for kpsewhich call, we need to do quoting
    # we need as much candidate files as possible, so -all is needed
    my $cmdl = 'kpsewhich -all ';
    for my $f (@fn) {
      $cmdl .= " \"$f\" ";
    }
    # shoot up kpsewhich
    # this call (derived from the database) contains multibyte characters,
    # and they should be encoded in cp932 for win32 console
    if (win32()) {
      $cmdl = encode_utftocp($cmdl);
    }
    print_ddebug("checking for $cmdl\n");
    @foundfiles = `$cmdl`;
  }
  # at this point, on windows, @foundfiles is encoded in cp932
  # which is suitable for the next few lines
  chomp(@foundfiles);
  print_ddebug("Found files @foundfiles\n");
  # map basenames to filenames
  my %bntofn;
  for my $f (@foundfiles) {
    $f =~ s/[\r\n]+\z//; # perl's chomp() on git-bash cannot strip CR of CRLF ??
    my $realf = abs_path($f);
    if (!$realf) {
      print_warning("dead link or strange file found: $f - ignored!\n");
      next;
    }
    if (win32()) {
      # abs_path cannot read NTFS symlink/hardlink, and it serves as
      # identity transformation.
      # this might lead to dangling links for multiple runs of cjk-gs-integrate,
      # when $opt_texmflink (in the previous run) is contained in the (current)
      # kpsewhich search path.
      # to avoid this, ignore targets which contain $otf_pathpart or $ttf_pathpart
      my $temp_realfdir = "$realf";
      $temp_realfdir =~ s!^(.*)/(.*)$!$1!;
      next if ($temp_realfdir =~ $otf_pathpart || $temp_realfdir =~ $ttf_pathpart);
    }
    # decode now on windows! (cp932 -> internal utf-8)
    if (win32()) {
      $f = encode_cptoutf($f);
      $realf = encode_cptoutf($realf);
    }
    my $bn = basename($f);
    # kpsewhich -all might return multiple files with the same basename;
    # collect all of them
    $bntofn{$bn}{$realf} = 1;
  }

  # show the %fontdb before file check
  if ($opt_debug >= 2) {
    print_ddebug("dumping font database before file check:\n");
    print_ddebug(Data::Dumper::Dumper(\%fontdb));
  }
  if ($opt_debug >= 3) {
    print_dddebug("dumping basename to filename list:\n");
    print_dddebug(Data::Dumper::Dumper(\%bntofn));
  }

  # update the %fontdb with the found files
  for my $k (keys %fontdb) {
    $fontdb{$k}{'available'} = 0;
    for my $f (keys %{$fontdb{$k}{'files'}}) {
      # check for subfont extension
      my $realfile = $f;
      $realfile =~ s/^(.*)\(\d*\)$/$1/;
      my $index = 0;
      if ($fontdb{$k}{'files'}{$f}{'type'} eq 'TTC' || $fontdb{$k}{'files'}{$f}{'type'} eq 'OTC') {
        if ($f =~ m/^(.*)\((\d*)\)$/) {
          $index = $2;
        }
      }
      # double check for casefolding or incompatible OTC/TTC index
      #   [1] casefolding issue
      #     we might catch different names (batang/Batang) and identify them wrongly on
      #        * case-insensitive file systems (like HFS on MacOS)
      #        * kpathsea 6.3.0 or later, with casefolding fallback search (TL2018)
      #     check the actual psname using zrlistttc.lua, only when we "know"
      #     both uppercase/lowercase font files are possible and they are different
      #   [2] incompatible index
      #     the index in msgothic.ttc changed at some time between Win7 and Win10.
      my $actualpsname;
      my $bname;
      for my $b (sort keys %{$bntofn{$realfile}}) {
        $fontdb{$k}{'doublecheck'} = "debug" if $opt_strictpsname;
        if ($fontdb{$k}{'doublecheck'} && $zrlistttc_available) {
          print_debug("We need to test whether\n");
          print_debug("  $b\n");
          print_debug("is the correct one. Invoking zrlistttc ...\n");
          chomp($actualpsname = `texlua $zrlistttc -i $index "$b"`);
          if ($?) {
            # something is wrong with the font file, or zrlistttc does not support it;
            # still there is a chance that Ghostscript supports, so don't discard it
            print_debug("... command exited with $?!\n");
            print_debug("OK, I'll take this, but it may not work properly.\n");
            print_warning("zrlistttc check failed for $b\n") if $opt_strictpsname;
            $bname = $b;
            last;
          }
          $actualpsname =~ s/[\r\n]+\z//; # perl's chomp() on git-bash cannot strip CR of CRLF ??
          if ($actualpsname ne $k) {
            print_debug("... PSName returned by zrlistttc ($actualpsname) is\n");
            print_debug("different from our database ($k), discarding!\n");
            print_warning("zrlistttc check failed for $b\n") if $opt_strictpsname;
          } else {
            print_debug("... test passed.\n");
            $bname = $b;
            last;
          }
        } else {
          $bname = $b;
          last;
        }
      }
      if ($bname) {
        # we found a representative, make it available
        $fontdb{$k}{'files'}{$f}{'target'} = $bname;
        $fontdb{$k}{'available'} = 1;
      } else {
        # delete the entry for convenience
        delete $fontdb{$k}{'files'}{$f};
      }
    }
  }
  # second round to determine the winner in case of more targets
  for my $k (keys %fontdb) {
    if ($fontdb{$k}{'available'}) {
      my $mp = 1000000; my $mf;
      for my $f (keys %{$fontdb{$k}{'files'}}) {
        if ($fontdb{$k}{'files'}{$f}{'priority'} < $mp) {
          $mp = $fontdb{$k}{'files'}{$f}{'priority'};
          $mf = $f;
        }
      }
      # extract subfont if necessary
      my $sf = 0;
      if ($mf =~ m/^(.*)\((\d*)\)$/) { $sf = $2; }
      $fontdb{$k}{'target'} = $fontdb{$k}{'files'}{$mf}{'target'};
      $fontdb{$k}{'type'} = $fontdb{$k}{'files'}{$mf}{'type'};
      $fontdb{$k}{'subfont'} = $sf if ($fontdb{$k}{'type'} eq 'TTC' || $fontdb{$k}{'type'} eq 'OTC');
    }
    # not needed anymore
    # delete $fontdb{$k}{'files'};
  }
  if ($opt_debug >= 2) {
    print_ddebug("dumping font database:\n");
    print_ddebug(Data::Dumper::Dumper(\%fontdb));
  }
}

sub compute_aliases {
  # go through fontdb to check for provides
  # accumulate all provided fonts in @provides
  for my $k (keys %fontdb) {
    if ($fontdb{$k}{'available'}) {
      for my $p (keys %{$fontdb{$k}{'provides'}}) {
        # do not check alias if the real font is available in OTF/TTF/TTC format
        if ($fontdb{$p}{'available'}) {
          next if ($fontdb{$p}{'type'} ne 'OTC');
        }
        # use the priority as key
        # if priorities are double, this will pick one at chance
        if ($aliases{$p}{$fontdb{$k}{'provides'}{$p}}) {
          print_warning("duplicate provide levels:\n");
          print_warning("  current $p $fontdb{$k}{'provides'}{$p} $aliases{$p}{$fontdb{$k}{'provides'}{$p}}\n");
          print_warning("  ignored $p $fontdb{$k}{'provides'}{$p} $k\n");
        } else {
          # if OTC font is caught, then skip it as Ghostscript doesn't support it (2016/12/12)
          if ($fontdb{$k}{'type'} eq 'OTC') {
            print_debug("Currently Ghostscript does not support OTC font,\n");
            print_debug("not adding $fontdb{$k}{'otcname'} to alias candidates\n");
          } else {
            $aliases{$p}{$fontdb{$k}{'provides'}{$p}} = $k;
          }
        }
      }
    }
  }
  # check for user supplied aliases
  for my $a (@opt_aliases) {
    if ($a =~ m/^(.*)=(.*)$/) {
      my $ll = $1;
      my $rr = $2;
      # check for consistency of user provided aliases:
      # - ll must not be available
      # - rr needs to be available as font or alias
      # check whether $rr is available, either as real font or as alias
      if ($fontdb{$ll}{'available'}) {
        print_error("left side of alias spec is provided by a real font: $a\n");
        print_error("stopping here\n");
        exit(1);
      }
      if (!($fontdb{$rr}{'available'} || $aliases{$rr})) {
        print_error("right side of alias spec is not available as real font or alias: $a\n");
        print_error("stopping here\n");
        exit(1);
      }
      $user_aliases{$ll} = $rr;
    }
  }
  if ($opt_debug >= 2) {
    print_ddebug("dumping aliases:\n");
    print_ddebug(Data::Dumper::Dumper(\%aliases));
  }
}

# While the OTF link target is determined by the filename itself
# for TTF we can have ttc with several fonts.
# The following routine determines the link target by selecting
# the file name of the ttf candidates with the lowest priority
# as the link target name for TTF
sub determine_nonotf_link_name {
  for my $k (keys %fontdb) {
    my $ttfname = "";
    my $ttcname = "";
    my $otcname = "";
    my $mpttf = 10000000;
    my $mpttc = 10000000;
    my $mpotc = 10000000;
    for my $f (keys %{$fontdb{$k}{'files'}}) {
      if ($fontdb{$k}{'files'}{$f}{'type'} eq 'TTF') {
        my $p = $fontdb{$k}{'files'}{$f}{'priority'};
        if ($p < $mpttf) {
          $ttfname = $f;
          $ttfname =~ s/^(.*)\(\d*\)$/$1/;
          $mpttf = $p;
        }
      } elsif ($fontdb{$k}{'files'}{$f}{'type'} eq 'TTC') {
        my $p = $fontdb{$k}{'files'}{$f}{'priority'};
        if ($p < $mpttc) {
          $ttcname = $f;
          $ttcname =~ s/^(.*)\(\d*\)$/$1/;
          $mpttc = $p;
        }
      } elsif ($fontdb{$k}{'files'}{$f}{'type'} eq 'OTC') {
        my $p = $fontdb{$k}{'files'}{$f}{'priority'};
        if ($p < $mpotc) {
          $otcname = $f;
          $otcname =~ s/^(.*)\(\d*\)$/$1/;
          $mpotc = $p;
        }
      }
    }
    if ($ttfname) {
      $fontdb{$k}{'ttfname'} = $ttfname;
    }
    if ($ttcname) {
      $fontdb{$k}{'ttcname'} = $ttcname;
    }
    if ($otcname) {
      $fontdb{$k}{'otcname'} = $otcname;
    }
  }
}

sub read_font_database {
  my @dbl;
  # if --fontdef=foo is given, disregard built-in database and
  # use "foo" as a substitute; otherwise, use built-in database
  if ($opt_fontdef) {
    my $foo = kpse_miscfont($opt_fontdef);
    open(FDB, "<$foo") ||
      die("Cannot find $opt_fontdef: $!");
    @dbl = <FDB>;
    close(FDB);
    print_debug("New database file: $opt_fontdef...\n");
  } else {
    @dbl = <DATA>;
  }
  read_each_font_database(@dbl);
  # if --fontdef-add=bar is given, use "bar" as an addition
  # to the current database; if the same Name entry appears,
  # overwrite existing one (that is, the addition wins)
  for (@opt_fontdef_add) {
    my $foo = kpse_miscfont($_);
    open(FDB, "<$foo") ||
      die("Cannot find $_: $!");
    @dbl = <FDB>;
    close(FDB);
    print_debug("Additional database file: $_...\n");
    read_each_font_database(@dbl);
  }
}

sub read_each_font_database {
  my (@curdbl) = @_;
  my $fontname = "";
  my $fontclass = "";
  my %fontprovides = ();
  my $fontdoublecheck = "";
  my %fontfiles;
  my $psname = "";
  my $lineno = 0;
  chomp(@curdbl);
  push @curdbl, ""; # add a "final empty line" to easy parsing
  for my $l (@curdbl) {
    $lineno++;
    next if ($l =~ m/^\s*#/); # skip comment line
    $l =~ s/\s*#.*$//; # skip comment after '#'
    if ($l =~ m/^\s*$/) { # empty line is a separator between entries
      if ($fontname || $fontclass || keys(%fontfiles)) {
        if ($fontname && $fontclass && keys(%fontfiles)) {
          my $realfontname = ($psname ? $psname : $fontname);
          if ($fontdb{$realfontname}{'origname'}) {
            # needed for --fontdef-add, which allows overwriting with external database given by user
            print_debug("$fontdb{$realfontname}{'origname'} is already registered in database,\n");
            print_debug("overwriting with the new one ...\n");
          }
          $fontdb{$realfontname}{'origname'} = $fontname;
          $fontdb{$realfontname}{'class'} = $fontclass;
          $fontdb{$realfontname}{'doublecheck'} = $fontdoublecheck;
          $fontdb{$realfontname}{'files'} = { %fontfiles };
          $fontdb{$realfontname}{'provides'} = { %fontprovides };
          if ($opt_debug >= 3) {
            print_dddebug("Dumping fontfiles for $realfontname: " . Data::Dumper::Dumper(\%fontfiles));
          }
          # reset to start
          $fontname = $fontclass = $psname = "";
          $fontdoublecheck = "";
          %fontfiles = ();
          %fontprovides = ();
        } else {
          print_warning("incomplete entry above line $lineno for $fontname/$fontclass, skipping!\n");
          # reset to start
          $fontname = $fontclass = $psname = "";
          $fontdoublecheck = "";
          %fontfiles = ();
          %fontprovides = ();
        }
      } else {
        # no term is set, so nothing to warn about
      }
      next;
    }
    if ($l =~ m/^!INCLUDE\s*(.*)$/) { # for remove-only database
      next if (!$opt_cleanup);
      my @dbl;
      my $foo = kpse_miscfont($1);
      if (!open(FDB, "<$foo")) {
        print_warning("Cannot find $1, skipping!\n");
        next;
      }
      @dbl = <FDB>;
      close(FDB);
      print_debug("Reading database file $1...\n");
      read_each_font_database(@dbl);
      next;
    }
    if ($l =~ m/^INCLUDE\s*(.*)$/) {
      my @dbl;
      my $foo = kpse_miscfont($1);
      if (!open(FDB, "<$foo")) {
        print_warning("Cannot find $1, skipping!\n");
        next;
      }
      @dbl = <FDB>;
      close(FDB);
      print_debug("Reading database file $1...\n");
      read_each_font_database(@dbl);
      next;
    }
    if ($l =~ m/^Name:\s*(.*)$/) { $fontname = $1; next; }
    if ($l =~ m/^PSName:\s*(.*)$/) { $psname = $1; next; }
    if ($l =~ m/^Class:\s*(.*)$/) { $fontclass = $1 ; next ; }
    if ($l =~ m/^Provides\((\d+)\):\s*(.*)$/) { $fontprovides{$2} = $1; next; }
    if ($l =~ m/^Doublecheck:\s*(.*)$/) { $fontdoublecheck = $1 ; next ; }
    if ($l =~ m/^Casefold:\s*(.*)$/) { $fontdoublecheck = $1 ; next ; } # no longer used
    # new code: distinguish 4 types (otf, otc, ttf, ttc)
    if ($l =~ m/^OTFname(\((\d+)\))?:\s*(.*)$/) {
      my $fn = $3;
      $fontfiles{$fn}{'priority'} = ($2 ? $2 : 10);
      # cp932 for win32 console
      my $encoded_fn;
      if (win32()) {
        $encoded_fn = encode_utftocp($fn);
      }
      print_dddebug("filename: ", ($encoded_fn ? "$encoded_fn" : "$fn"), "\n");
      print_dddebug("type: otf\n");
      $fontfiles{$fn}{'type'} = 'OTF';
      next;
    }
    if ($l =~ m/^OTCname(\((\d+)\))?:\s*(.*)$/) {
      my $fn = $3;
      $fontfiles{$fn}{'priority'} = ($2 ? $2 : 10);
      # cp932 for win32 console
      my $encoded_fn;
      if (win32()) {
        $encoded_fn = encode_utftocp($fn);
      }
      print_dddebug("filename: ", ($encoded_fn ? "$encoded_fn" : "$fn"), "\n");
      print_dddebug("type: otc\n");
      $fontfiles{$fn}{'type'} = 'OTC';
      next;
    }
    if ($l =~ m/^TTFname(\((\d+)\))?:\s*(.*)$/) {
      my $fn = $3;
      $fontfiles{$fn}{'priority'} = ($2 ? $2 : 10);
      # cp932 for win32 console
      my $encoded_fn;
      if (win32()) {
        $encoded_fn = encode_utftocp($fn);
      }
      print_dddebug("filename: ", ($encoded_fn ? "$encoded_fn" : "$fn"), "\n");
      print_dddebug("type: ttf\n");
      $fontfiles{$fn}{'type'} = 'TTF';
      next;
    }
    if ($l =~ m/^TTCname(\((\d+)\))?:\s*(.*)$/) {
      my $fn = $3;
      $fontfiles{$fn}{'priority'} = ($2 ? $2 : 10);
      # cp932 for win32 console
      my $encoded_fn;
      if (win32()) {
        $encoded_fn = encode_utftocp($fn);
      }
      print_dddebug("filename: ", ($encoded_fn ? "$encoded_fn" : "$fn"), "\n");
      print_dddebug("type: ttc\n");
      $fontfiles{$fn}{'type'} = 'TTC';
      next;
    }
    # only for backward compatibility; guess type from the file extension
    if ($l =~ m/^Filename(\((\d+)\))?:\s*(.*)$/) {
      my $fn = $3;
      $fontfiles{$fn}{'priority'} = ($2 ? $2 : 10);
      # cp932 for win32 console
      my $encoded_fn;
      if (win32()) {
        $encoded_fn = encode_utftocp($fn);
      }
      print_dddebug("filename: ", ($encoded_fn ? "$encoded_fn" : "$fn"), "\n");
      if ($fn =~ m/\.otf$/i) {
        print_dddebug("type: otf\n");
        $fontfiles{$fn}{'type'} = 'OTF';
      } elsif ($fn =~ m/\.otc(\(\d+\))?$/i) {
        print_dddebug("type: otc\n");
        $fontfiles{$fn}{'type'} = 'OTC';
      } elsif ($fn =~ m/\.ttf$/i) {
        print_dddebug("type: ttf\n");
        $fontfiles{$fn}{'type'} = 'TTF';
      } elsif ($fn =~ m/\.ttc(\(\d+\))?$/i) {
        print_dddebug("type: ttc\n");
        $fontfiles{$fn}{'type'} = 'TTC';
      } else {
        print_warning("cannot determine font type of $fn at line $lineno, skipping!\n");
        delete $fontfiles{$fn};
      }
      next;
    }
    # only for removing
    if ($l =~ m/^RMVname(\((\d+)\))?:\s*(.*)$/) {
      my $fn = $3;
      $fontfiles{$fn}{'priority'} = ($2 ? $2 : 10);
      # cp932 for win32 console
      my $encoded_fn;
      if (win32()) {
        $encoded_fn = encode_utftocp($fn);
      }
      print_dddebug("filename: ", ($encoded_fn ? "$encoded_fn" : "$fn"), "\n");
      print_dddebug("type: remove\n");
      $fontfiles{$fn}{'type'} = 'RMV';
      next;
    }
    # we are still here??
    print_error("Cannot parse this file at line $lineno, exiting.
                 Strange line: >>>$l<<<\n");
    exit(1);
  }
}

sub dump_font_database {
  open(FOO, ">$dump_datafile") ||
    die("cannot open $dump_datafile for writing: $!");
  for my $k (sort keys %fontdb) {
    print FOO "Name: $fontdb{$k}{'origname'}\n";
    print FOO "PSName: $k\n" if ($fontdb{$k}{'origname'} ne $k);
    print FOO "Class: $fontdb{$k}{'class'}\n";
    for my $p (sort keys %{$fontdb{$k}{'provides'}}) {
      print FOO "Provides($fontdb{$k}{'provides'}{$p}): $p\n";
    }
    print FOO "Doublecheck: $fontdb{$k}{'doublecheck'}\n" if ($fontdb{$k}{'doublecheck'});
    for my $f (sort { $fontdb{$k}{'files'}{$a}{'priority'}
                      <=>
                      $fontdb{$k}{'files'}{$b}{'priority'} }
                    keys %{$fontdb{$k}{'files'}}) {
      print FOO "$fontdb{$k}{'files'}{$f}{'type'}name($fontdb{$k}{'files'}{$f}{'priority'}): $f\n";
    }
    print FOO "\n"; # empty line is a separator between entries
  }
  close(FOO);
}

sub find_gs_resource {
  my $foundres = '';
  if (win32()) {
    # determine tlgs or native gs
    chomp(my $foo = `kpsewhich -var-value=SELFAUTOPARENT`);
    if ( -d "$foo/tlpkg/tlgs" ) {
      # should be texlive with tlgs
      print_debug("Assuming tlgs win32 ...\n");
      $foundres = "$foo/tlpkg/tlgs/Resource";
      # for TL2016, tlgs binary has built-in Resource,
      # so we cannot set up CJK fonts correctly.
      # the following test forces to exit in such case
      if ( ! -d $foundres ) {
        print_error("No Resource directory available for tlgs,\n");
        print_error("we cannot support such gs, sorry.\n");
        $foundres = '';
      }
      # change output location
      $cidfmap_pathpart = "../lib/cidfmap";
      $cidfmap_local_pathpart = "../lib/cidfmap.local";
      $cidfmap_aliases_pathpart = "../lib/cidfmap.aliases";
    } else {
      # we assume gswin32c is in the path
      # TODO: what should we do for gswin64c?
      chomp($foundres = `where gswin32c 2>$nul`); # assume 'where' is available
      if ($?) {
        print_error("Cannot run where gswin32c ...\n");
      } else {
        # trial 1: assume the relative path
        # when C:\path\to\bin\gswin32c.exe is found, then there should be
        # C:\path\to\Resource (note that 'where' returns backslash-ed path)
        print_debug("Finding gs resource by assuming relative path ...\n");
        $foundres = encode_cptoutf($foundres); # 99.99% unnecessary
        $foundres =~ s!\\!/!g;
        $foundres =~ s!/bin/gswin32c\.exe$!/Resource!;
        if ( ! -d $foundres ) {
          $foundres = '';
        }
        if (!$foundres) {
          print_debug("Found gs but no resource, try another routine ...\n");
        }
      }
      if (!$foundres) {
        chomp(my $gsver = `gswin32c --version 2>$nul`);
        if ($?) {
          print_error("Cannot run gswin32c --version ...\n");
        } else {
          # trial 2: assume the fixed path, c:/gs/gs$gsver/Resource
          print_debug("Finding gs resource by assuming fixed path ...\n");
          $foundres = "c:/gs/gs$gsver/Resource";
          if ( ! -d $foundres ) {
            $foundres = '';
          }
          if (!$foundres) {
            print_error("Found gs but no resource???\n");
          }
        }
      }
    }
  } else {
    # we assume that gs is in the path
    chomp(my $gsver = `gs --version 2>$nul`);
    if ($?) {
      print_error("Cannot run gs --version ...\n");
    } else {
      # trial 1: assume the relative path
      # when /path/to/bin/gs is found, then there should be
      # /path/to/share/ghostscript/$(gs --version)/Resource
      print_debug("Finding gs resource by assuming relative path ...\n");
      chomp($foundres = `which gs`);
      $foundres =~ s!/bin/gs$!/share/ghostscript/$gsver/Resource!;
      if ( ! -d $foundres ) {
        $foundres = '';
      }
      if (!$foundres) {
        print_debug("Found gs but no resource, try another routine ...\n");
      }
    }
    if (!$foundres) {
      chomp(my @ret = `gs --help 2>$nul`);
      if ($?) {
        print_error("Cannot run gs --help ...\n");
      } else {
        # trial 2: parse gs help message
        print_debug("Finding gs resource by parsing help message ...\n");
        $foundres = '';
        # try to find resource line
        for (@ret) {
          if (m!Resource/Font!) {
            $foundres = $_;
            # extract the first substring of non-space chars
            # up to Resource/Font and drop the /Font part
            $foundres =~ s!^.*\s(\S*Resource)/Font.*$!$1!;
            last;
          }
        }
        if (!$foundres) {
          print_error("Found gs but no resource???\n");
        }
      }
    }
  }
  return $foundres;
}

sub kpse_miscfont {
  my ($file) = @_;
  my $foo = '';
  # first, prioritize GitHub repository diretory structure
  $foo = "database/$file" if (-f "database/$file");
  if ($foo eq "") {
    chomp($foo = `kpsewhich -format=miscfont $file`);
  }
  return $foo;
}

sub encode_utftocp {
  my ($foo) = @_;
  $foo = Encode::decode('utf-8', $foo);
  $foo = Encode::encode('cp932', $foo);
  return $foo;
}

sub encode_cptoutf {
  my ($foo) = @_;
  $foo = Encode::decode('cp932', $foo);
  $foo = Encode::encode('utf-8', $foo);
  return $foo;
}

sub version {
  my $ret = sprintf "%s version %s\n", $prg, $version;
  return $ret;
}

sub Usage {
  my $headline = "Configuring Ghostscript for CJK CID/TTF fonts";
  my $usage = "[perl] $prg\[.pl\] [OPTIONS]";
  my $options = "
-o, --output DIR      specifies the base output dir, if not provided,
                      the Resource directory of an installed Ghostscript
                      is searched and used.
-f, --fontdef FILE    specify alternate set of font definitions, if not
                      given, the built-in set is used
--fontdef-add FILE    specify additional set of font definitions, to
                      overwrite subset of built-in definitions;
                      can be given multiple times
-a, --alias LL=RR     defines an alias, or overrides a given alias;
                      illegal if LL is provided by a real font, or
                      RR is neither available as real font or alias;
                      can be given multiple times
--filelist FILE       read list of available font files from FILE
                      instead of searching with kpathsea
--link-texmf [DIR]    link fonts into
                         DIR/$otf_pathpart
                      and
                         DIR/$ttf_pathpart
                      where DIR defaults to TEXMFLOCAL
--otfps [DIR]         generate configuration file (psnames-for-otf) into
                         DIR/$akotfps_pathpart
                      which is used by ps2otfps (developed by Akira Kakuto),
                      instead of generating snippets
--force               do not bail out if linked fonts already exist
--remove              try to remove instead of create
--cleanup             try to clean up all possible links/snippets and
                      cidfmap.local/cidfmap.aliases, which could have been
                      generated in the previous runs
-n, --dry-run         do not actually output anything
-q, --quiet           be less verbose
-d, --debug           output debug information, can be given multiple times
-v, --version         outputs only the version information
-h, --help            this help
";

  my $winonlyoptions = "
--hardlink            create hardlinks instead of symlinks
--winbatch [FILE]     prepare a batch file for link generation, instead of
                      generating links right away
                      the batch file name defaults to $winbatch
";

  my $commandoptions = "
--dump-data [FILE]    dump the set of font definitions which is currently
                      effective, where FILE (the dump output) defaults to
                      $dump_datafile; you can easily modify it,
                      and tell me with -f (or --fontdef) option
--only-aliases        regenerate only cidfmap.aliases file, instead of all
--list-aliases        lists the available aliases and their options, with the
                      selected option on top
--list-all-aliases    list all possible aliases without searching for
                      actually present files
--list-fonts          lists the fonts found on the system
--info                combines the information of --list-aliases and
                      --list-fonts
--machine-readable    output of --list-aliases is machine readable
";

  my $shortdesc = "
This script searches a list of directories for CJK fonts, and makes
them available to an installed Ghostscript. In the simplest case with
sufficient privileges, a run without arguments should effect in a
complete setup of Ghostscript.
Search is done using the kpathsea library, in particular `kpsewhich`
program. To run this script, you will need some TeX distribution in
your system.
";

my $operation = "
For each found TrueType (TTF) font it creates a cidfmap entry in

    <Resource>/Init/cidfmap.local
      -- if you are using tlgs win32, tlpkg/tlgs/lib/cidfmap.local instead

and links the font to

    <Resource>/CIDFSubst/

For each CID font it creates a snippet in

    <Resource>/Font/

and links the font to

    <Resource>/CIDFont/

The `<Resource>` dir is either given by `-o`/`--output`, or otherwise searched
from an installed Ghostscript (binary name is assumed to be 'gs' on unix,
'gswin32c' on win32).

Aliases are added to

    <Resource>/Init/cidfmap.aliases
      -- if you are using tlgs win32, tlpkg/tlgs/lib/cidfmap.aliases instead

Finally, it tries to add runlib calls to

    <Resource>/Init/cidfmap
      -- if you are using tlgs win32, tlpkg/tlgs/lib/cidfmap

to load the cidfmap.local and cidfmap.aliases.
";

  my $dirsearch = '
Search is done using the kpathsea library, in particular using kpsewhich
program. By default the following directories are searched:
  - all TEXMF trees
  - `/Library/Fonts`, `/Library/Fonts/Microsoft`, `/System/Library/Fonts`,
    `/System/Library/Assets`, `/Network/Library/Fonts`,
    `~/Library/Fonts` and `/usr/share/fonts` (all if available)
  - `/Applications/Microsoft Word.app/Contents/Resources/{Fonts,DFonts}`,
    `/Applications/Microsoft Excel.app/Contents/Resources/{Fonts,DFonts}`,
    `/Applications/Microsoft PowerPoint.app/Contents/Resources/{Fonts,DFonts}`
     (all if available, meant for Office for Mac 2016)
  - `c:/windows/fonts` (on Windows)
  - the directories in `OSFONTDIR` environment variable

In case you want to add some directories to the search path, adapt the
`OSFONTDIR` environment variable accordingly: Example:

`````
    OSFONTDIR="/usr/local/share/fonts/truetype//:/usr/local/share/fonts/opentype//" $prg
`````

will result in fonts found in the above two given directories to be
searched in addition.
';

  my $outputfile = "
If no output option is given, the program searches for a Ghostscript
interpreter 'gs' and determines its Resource directory. This might
fail, in which case one need to pass the output directory manually.

Since the program adds files and link to this directory, sufficient
permissions are necessary.
";

  my $aliases = "
Aliases are managed via the Provides values in the font database.
At the moment entries for the basic font names for CJK fonts
are added:

Japanese:

    Ryumin-Light GothicBBB-Medium FutoMinA101-Bold FutoGoB101-Bold
    MidashiMin-MA31 MidashiGo-MB31 Jun101-Light

Korean:

    HYSMyeongJo-Medium HYGoThic-Medium HYRGoThic-Medium

Simplified Chinese:

    STSong-Light STSong-Regular STHeiti-Regular STHeiti-Light
    STKaiti-Regular STFangsong-Light STFangsong-Regular

Traditional Chinese:

    MSung-Light MSung-Medium MHei-Medium MKai-Medium

In addition, we also include provide entries for the OTF Morisawa names:

    RyuminPro-Light GothicBBBPro-Medium
    FutoMinA101Pro-Bold FutoGoB101Pro-Bold
    MidashiMinPro-MA31 MidashiGoPro-MB31 Jun101Pro-Light

The order is determined by the `Provides` setting in the font database.
That is, the first font found in this order will be used to provide the
alias if necessary.

For the Japanese fonts:
    Morisawa Pr6N, Morisawa, Hiragino ProN, Hiragino,
    Kozuka Pr6N, Kozuka ProVI, Kozuka Pro, Kozuka Std,
    Yu OS X, Yu Win, MS,
    Moga-Mobo-ex, Moga-Mobo, IPAex, IPA, Ume

For the Korean fonts:
    (Hanyang,) Adobe, Solaris, MS, Unfonts, Baekmuk

For the Simplified Chinese:
    Adobe, Fandol, Hiragino, Founder, MS, CJKUnifonts, Arphic, CJKUnifonts-ttf

For the Traditional Chinese:
    Adobe, MS, CJKUnifonts, Arphic, CJKUnifonts-ttf

#### Overriding aliases ####

Using the command line option `--alias LL=RR` one can add arbitrary aliases,
or override ones selected by the program. For this to work the following
requirements of `LL` and `RR` must be fulfilled:
  * `LL` is not provided by a real font
  * `RR` is available either as real font, or as alias (indirect alias)
";

  my $authors = "
The script and its documentation was written by Norbert Preining, based
on research and work by Yusuke Kuroki, Yusuke Terada, Bruno Voisin,
Hironobu Yamashita, Munehiro Yamamoto and the TeX Q&A wiki page.

Maintained by Japanese TeX Development Community. For development, see
  https://github.com/texjporg/cjk-gs-support

The script is licensed under GNU General Public License Version 3 or later.
The contained font data is not copyrightable.
";


  if ($opt_markdown) {
    print "$headline\n";
    print ("=" x length($headline));
    print "\n$shortdesc\nUsage\n-----\n\n`````\n$usage\n`````\n\n";
    print "#### Options ####\n\n`````";
    print_for_out($options, "  ");
    print "`````\n\n#### Windows only options ####\n\n`````";
    print_for_out($winonlyoptions, "  ");
    print "`````\n\n#### Command like options ####\n\n`````";
    print_for_out($commandoptions, "  ");
    print "`````\n\nOperation\n---------\n$operation\n";
    print "How and which directories are searched\n";
    print "--------------------------------------\n$dirsearch\n";
    print "Output files\n";
    print "------------\n$outputfile\n";
    print "Aliases\n";
    print "-------\n$aliases\n";
    print "Authors, Contributors, and Copyright\n";
    print "------------------------------------\n$authors\n";
  } else {
    print "\nUsage: $usage\n\n$headline\n$shortdesc";
    print "\nOptions:\n";
    print_for_out($options, "  ");
    if (win32()) {
      print "\nWindows only options:\n";
      print_for_out($winonlyoptions, "  ");
    }
    print "\nCommand like options:\n";
    print_for_out($commandoptions, "  ");
    print "\nOperation:\n";
    print_for_out($operation, "  ");
    print "\nHow and which directories are searched:\n";
    print_for_out($dirsearch, "  ");
    print "\nOutput files:\n";
    print_for_out($outputfile, "  ");
    print "\nAliases:\n";
    print_for_out($aliases, "  ");
    print "\nAuthors, Contributors, and Copyright:\n";
    print_for_out($authors, "  ");
    print "\n";
  }
  exit(0);
}

sub print_for_out {
  my ($what, $indent) = @_;
  for (split /\n/, $what) {
    next if m/`````/;
    s/\s*####\s*//g;
    if ($_ eq '') {
      print "\n";
    } else {
      print "$indent$_\n";
    }
  }
}

# info/warning can be suppressed
# verbose/error cannot be suppressed
sub print_info {
  print STDOUT "$prg: ", @_ if (!$opt_quiet);
}
sub print_verbose {
  print STDOUT "$prg: ", @_;
}
sub print_warning {
  print STDERR "$prg [WARNING]: ", @_ if (!$opt_quiet)
}
sub print_error {
  print STDERR "$prg [ERROR]: ", @_;
}
sub print_debug {
  print STDERR "$prg [DEBUG]: ", @_ if ($opt_debug >= 1);
}
sub print_ddebug {
  print STDERR "$prg [DEBUG]: ", @_ if ($opt_debug >= 2);
}
sub print_dddebug {
  print STDERR "$prg [DEBUG]: ", @_ if ($opt_debug >= 3);
}


__DATA__
#
# CJK FONT DEFINITIONS
#

# Noto
INCLUDE cjkgs-notoserif.dat
INCLUDE cjkgs-notosans.dat

# SourceHan
INCLUDE cjkgs-sourcehanserif.dat
INCLUDE cjkgs-sourcehansans.dat

#
# JAPANESE FONTS
#

# Morisawa -- Provides J10(Pr6N), J15(Pr6), J18(Pr5), J20(Pro)
INCLUDE cjkgs-morisawa.dat
#INCLUDE cjkgs-morisawa-extra.dat

# Hiragino -- Provides J30(ProN), J40(Pro)
INCLUDE cjkgs-hiragino.dat

# Kozuka -- Provides J50(Pr6N), J55(ProVI), J60(Pro), J65(Std)
INCLUDE cjkgs-kozuka.dat
INCLUDE cjkgs-ryokana.dat

# Yu-fonts MacOS version -- Provides J80
INCLUDE cjkgs-yu-osx.dat

# Yu-fonts Windows/MSOffice version -- Provides J90
INCLUDE cjkgs-yu-win.dat

# MS -- Provides J95
INCLUDE cjkgs-microsoft.dat

# BIZ UD
INCLUDE cjkgs-bizud.dat

# TypeBank
INCLUDE cjkgs-typebank.dat

# Fontworks
INCLUDE cjkgs-fontworks.dat

# Toppan
INCLUDE cjkgs-toppan.dat

# Heisei
INCLUDE cjkgs-heisei.dat

# Moga-Mobo from Y.Oz Vox (free) -- Provides J100(Ex), J110(none)
INCLUDE cjkgs-mogamobo.dat

# IPA (free) -- Provides J120(Ex), J130(none)
INCLUDE cjkgs-ipa.dat

# Ume-font (free) -- Provides J140
INCLUDE cjkgs-ume.dat

# Sazanami (free)
INCLUDE cjkgs-sazanami.dat

# Harano Aji Fonts (free)
INCLUDE cjkgs-haranoaji.dat

# Osaka (Apple)

Name: Osaka
Class: Japan
TTFname: Osaka.ttf

Name: Osaka-Mono
Class: Japan
TTFname: OsakaMono.ttf

#
# CHINESE FONTS
#

# Adobe -- Provides S30, T30
INCLUDE cjkgs-adobe.dat

# Hiragino -- Provides S50
# (already included in JAPANESE section)

# Beijing Founder Electronics -- Provides S55
INCLUDE cjkgs-founder.dat

# Changzhou SinoType -- Provides S??
INCLUDE cjkgs-sinotype.dat

# DynaComware -- Provides T??
INCLUDE cjkgs-dynacomware.dat

# Monotype
INCLUDE cjkgs-monotype.dat

# Apple
INCLUDE cjkgs-apple.dat

# Shanghai Ikarus Ltd./URW Software & Type GmbH

Name: SIL-Hei-Med-Jian
Class: GB
TTFname: Hei.ttf

Name: SIL-Kai-Reg-Jian
Class: GB
TTFname: Kai.ttf

# Fandol (free) -- Provides S40
INCLUDE cjkgs-fandol.dat

# Arphic (free) -- Provides S80, T80
INCLUDE cjkgs-arphic.dat

# CJK-Unifonts new ttc edition (free) -- Provides T70, S70
# CJK-Unifonts old ttf edition (free) -- Provides T90, S90
INCLUDE cjkgs-cjkuni.dat

# WenQuanYi (free)
INCLUDE cjkgs-wenquanyi.dat

# cwTeX (free)

Name: cwTeXMing
Class: CNS
TTFname: cwming.ttf

Name: cwTeXHeiBold
Class: CNS
TTFname: cwheib.ttf

Name: cwTeXKai
Class: CNS
TTFname: cwkai.ttf

Name: cwTeXYen
Class: CNS
TTFname: cwyen.ttf

Name: cwTeXFangSong
Class: CNS
TTFname: cwfs.ttf

#
# KOREAN FONTS
#

# Adobe -- Provides K30/80
# (already included in CHINESE section)

# Solaris -- Provides K40
INCLUDE cjkgs-solaris.dat

# Baekmuk (free)
# This is a special case, because "batang.ttf" in baekmuk and
# "Batang.ttf" in Microsoft Mac Office font share the same filename;
# symlink name should be "Baekmuk-Batang.ttf"
# similar for "Gulim.ttf" -- HY (2016/09/29)

Name: Baekmuk-Batang
Class: Korea
Provides(70): HYSMyeongJo-Medium
Doublecheck: true
TTFname(20): batang.ttf
TTFname(10): Baekmuk-Batang.ttf

Name: Baekmuk-Dotum
Class: Korea
Provides(70): HYGoThic-Medium
TTFname(20): dotum.ttf
TTFname(10): Baekmuk-Dotum.ttf

Name: Baekmuk-Gulim
Class: Korea
Provides(70): HYRGoThic-Medium
Doublecheck: true
TTFname(20): gulim.ttf
TTFname(10): Baekmuk-Gulim.ttf

Name: Baekmuk-Headline
Class: Korea
TTFname(20): hline.ttf
TTFname(10): Baekmuk-Headline.ttf

# Unfonts (free) -- Provides K60
INCLUDE cjkgs-unfonts.dat

# Nanum (free)
INCLUDE cjkgs-nanum.dat

# Design font by Ho-Seok Ee, aka. "ALee's font" (free)

Name: Bandal
Class: Korea
TTFname: Bandal.ttf

Name: Bangwool
Class: Korea
TTFname: Bangwool.ttf

Name: Eunjin
Class: Korea
TTFname: Eunjin.ttf

Name: EunjinNakseo
Class: Korea
TTFname: EunjinNakseo.ttf

Name: Guseul
Class: Korea
TTFname: Guseul.ttf

# Woowa Brothers (free)

Name: BMHANNA
Class: Korea
TTFname: BM-HANNA.ttf

# Hancom HCR (free)
INCLUDE cjkgs-hancom.dat


#
# Microsoft Windows, Windows/Mac Office fonts
#

# korea

Name: Batang
Class: Korea
Doublecheck: true
TTFname(50): Batang.ttf
TTCname(20): batang.ttc(0)

Name: BatangChe
Class: Korea
Provides(50): HYSMyeongJo-Medium
TTCname(20): batang.ttc(1)

Name: Dotum
Class: Korea
TTCname(20): gulim.ttc(2)

Name: DotumChe
Class: Korea
Provides(50): HYGoThic-Medium
TTCname(20): gulim.ttc(3)

Name: Gulim
Class: Korea
Doublecheck: true
TTFname(50): Gulim.ttf
TTCname(20): gulim.ttc(0)

Name: GulimChe
Class: Korea
Provides(50): HYRGoThic-Medium
Provides(90): HYGoThic-Medium
TTCname(20): gulim.ttc(1)

Name: Gungsuh
Class: Korea
TTCname(20): batang.ttc(2)

Name: GungsuhChe
Class: Korea
TTCname(20): batang.ttc(3)

Name: MalgunGothicRegular
Class: Korea
TTFname: malgun.ttf

Name: MalgunGothicBold
Class: Korea
TTFname: malgunbd.ttf

Name: MalgunGothic-Semilight
Class: Korea
TTFname: malgunsl.ttf

# simplified chinese

Name: SimHei
Class: GB
Provides(60): STHeiti-Regular
Provides(60): STHeiti-Light
TTFname(50): SimHei.ttf
TTFname(20): simhei.ttf

Name: SimSun
Class: GB
Provides(60): STSong-Light
Provides(60): STSong-Regular
TTFname(50): SimSun.ttf
TTFname(21): simsun.ttf
TTCname(20): simsun.ttc(0)

Name: NSimSun
Class: GB
TTCname(20): simsun.ttc(1)

Name: KaiTi
Class: GB
Provides(60): STKaiti-Regular
TTFname(40): Kaiti.ttf
TTFname(20): simkai.ttf

Name: FangSong
Class: GB
Provides(60): STFangsong-Light
Provides(60): STFangsong-Regular
TTFname(40): Fangsong.ttf
TTFname(20): simfang.ttf

Name: LiSu
Class: GB
TTCname(20): SIMLI.TTF
TTCname(19): simli.ttf

Name: YouYuan
Class: GB
TTCname(20): SIMYOU.TTF
TTCname(19): simyou.ttf

Name: MicrosoftYaHei
Class: GB
TTFname(20): msyh.ttf
TTCname(30): msyh.ttc(0)

Name: MicrosoftYaHei-Bold
Class: GB
TTFname(20): msyhbd.ttf
TTCname(30): msyhbd.ttc(0)

Name: MicrosoftYaHeiLight
Class: GB
TTFname(20): msyhl.ttf
TTCname(30): msyhl.ttc(0)

Name: DengXian-Regular
Class: GB
TTFname: Deng.ttf

Name: DengXian-Bold
Class: GB
TTFname: Dengb.ttf

Name: DengXian-Light
Class: GB
TTFname: Dengl.ttf

# traditional chinese

Name: MingLiU
Class: CNS
Provides(60): MSung-Medium
Provides(60): MSung-Light
TTFname(50): MingLiU.ttf
TTCname(20): mingliu.ttc(0)

Name: PMingLiU
Class: CNS
TTFname(50): PMingLiU.ttf
TTCname(20): mingliu.ttc(1)

Name: DFKaiShu-SB-Estd-BF
Class: CNS
Provides(60): MKai-Medium
TTFname(50): BiauKai.ttf
TTFname(20): kaiu.ttf

Name: MicrosoftJhengHeiRegular
Class: CNS
Provides(60): MHei-Medium
TTFname(40): MSJH.ttf
TTFname(20): msjh.ttf
TTCname(30): msjh.ttc(0)

Name: MicrosoftJhengHeiBold
Class: CNS
TTFname(40): MSJHBD.ttf
TTFname(20): msjhbd.ttf
TTCname(30): msjhbd.ttc(0)

Name: MicrosoftJhengHeiLight
Class: CNS
TTCname(30): msjhl.ttc(0)

Name: MicrosoftMHei
Class: CNS
Provides(65): MHei-Medium
TTFname(10): MSMHei.ttf

Name: MicrosoftMHei-Bold
Class: CNS
TTFname(10): MSMHei-Bold.ttf

# Remove-only database (should begin with !INCLUDE)
# that is, entries which contain at least one 'RMVname' line
# note that this line should come at the _end_ of all INCLUDE files
!INCLUDE cjkgs-removeonly.dat
!INCLUDE cjkgs-macos-removeonly.dat


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