summarylogtreecommitdiffstats
path: root/arx-install-data
blob: 04e4dfce1e222ccfd724b699fc9ed1bf202e7d7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
#!/bin/sh

# Copyright 2013 Arx Libertatis Team (see the AUTHORS file)
#
# This file is part of Arx Libertatis.
#
# Arx Libertatis is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Arx Libertatis is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Arx Libertatis.  If not, see <http://www.gnu.org/licenses/>.

##########################################################################################
# Install script for Arx Fatalis data files to be used with Arx Libertatis
# Usage: just run the damned script, maybe check --help

# This scripts targets Linux and FreeBSD, but may also work on other UNIX-like systems.

# Is this a multi-thousand-line bas^H^H^HPOSIX shell script?
#  Sure looks like it.
# Am I mad?
#  Most likely.

# If you want to edit the required files and checksums, scroll to the end.


##########################################################################################
# Colors

disable_color() {
	red='' ; green='' ; yellow='' ; blue='' ; pink='' ; cyan='' ; white=''
	dim_red='' ; dim_green='' ; dim_yellow='' ; dim_blue='' ; dim_pink=''
	dim_cyan='' ; dim_white='' ; reset=''
}
disable_color
if [ -t 1 ] && [ "$(tput colors 2> /dev/null)" != -1 ] ; then
	
	       red="$(printf '\033[1;31m')"
	     green="$(printf '\033[1;32m')"
	    yellow="$(printf '\033[1;33m')"
	      blue="$(printf '\033[1;34m')"
	      pink="$(printf '\033[1;35m')"
	      cyan="$(printf '\033[1;36m')"
	     white="$(printf '\033[1;37m')"
	
	   dim_red="$(printf '\033[0;31m')"
	 dim_green="$(printf '\033[0;32m')"
	dim_yellow="$(printf '\033[0;33m')"
	  dim_blue="$(printf '\033[0;34m')"
	  dim_pink="$(printf '\033[0;35m')"
	  dim_cyan="$(printf '\033[0;36m')"
	 dim_white="$(printf '\033[0;37m')"
	
	     reset="$(printf '\033[m')"
fi


##########################################################################################
# Constants

# Name and download locations for the 1.21 patch
patch_ver='1.21'
patch_name="ArxFatalis_${patch_ver}_MULTILANG.exe"
patch_name_localized="ArxFatalis_${patch_ver}_%s.exe"
patch_url_path="arxfatalis/patches/${patch_ver}/${patch_name}"
patch_url_master="http://cdn.bethsoft.com/${patch_url_path}"
patch_urls="http://arx.vg/${patch_name} ${patch_url_master}"
patch_urls="$patch_urls http://download.zenimax.com/${patch_url_path}"
patch_urls="$patch_urls http://web.archive.org/web/${patch_url_master}"

# Name and download locations for the Japanese 1.02j patch
patch_jp_ver='1.02j'
patch_jp_name="arx_jpn_patch_${patch_jp_ver}.exe"
patch_jp_url_master="http://www.capcom.co.jp/pc/arx/patch/${patch_jp_name}"
patch_jp_urls="http://arx.vg/${patch_jp_name}" # master URL is no longer available
patch_jp_urls="$patch_jp_urls http://web.archive.org/web/${patch_jp_url_master}"

# Name and store page for the GOG.com download
gog_names='setup_arx_fatalis.exe setup_arx_fatalis_2.0.0.7.exe'
gog_url='http://www.gog.com/gamecard/arx_fatalis'

# Store page for the Steam download
steam_appid='1700'
steam_url="http://store.steampowered.com/app/$steam_appid/"
steam_install_dir="Arx Fatalis"

# Name and wiki page for the demo download
demo_names="arx_demo_english.zip arxdemoenglish.zip arx_demo_english.exe"
demo_names="$demo_names arx_fatalis_demo_fr.zip arx_demo_german.exe arx_jpn_demo.exe"
demo_url='http://arx.vg/Getting_the_game_data#Demo'

bug_tracker_url='http://bugs.arx-libertatis.org/'

cabextract_url='http://www.cabextract.org.uk/'
innoextract_url='http://constexpr.org/innoextract/'


##########################################################################################
# Standard directories

user_pwd="$PWD"
user_pwd="${user_pwd%/}"
platform="$(uname)"
command="$(basename "$0")"
scommand="$(printf '%s' "$command" | tr - _)"
if [ "$platform" = 'Darwin' ] ; then
	# Mac OS X
	data_dirs='/Applications'
	data_home="$HOME/Library/Application Support"
	config_home="$HOME/Library/Application Support"
	data_dir_suffixes='ArxLibertatis'
	user_dir_suffixes='ArxLibertatis'
	config_dir_suffixes='ArxLibertatis'
	downloads_dir="$HOME/Downloads"
else
	# Linux, FreeBSD, ...
	data_dirs="${XDG_DATA_DIRS:-"/usr/local/share/:/usr/share/"}:/opt"
	data_home="${XDG_DATA_HOME:-"$HOME/.local/share"}"
	config_home="${XDG_CONFIG_HOME:-"$HOME/.config"}"
	data_dir_suffixes='games/arx:arx'
	user_dir_suffixes='arx'
	config_dir_suffixes='arx'
	[ -f "${config_home}/user-dirs.dirs" ] && . "${config_home}/user-dirs.dirs"
	downloads_dir="${XDG_DOWNLOAD_DIR:-"$HOME/Downloads"}"
fi
downloads_dir="${downloads_dir%/}"
tempdir="${TMPDIR:-"/tmp"}"
tempdir="${tempdir%/}"
[ -d "$tempdir" ] || tempdir="$PWD"
eval "data_path=\"\$${scommand}_PATH\""
[ -z "$data_path" ] && data_path="$arx_PATH"


##########################################################################################
# Helper functions

exec 4>&2  # fd to the original stderr (we redirect output to a log file in some cases)
logfile='' # log file receiving sdout and stderr

true=0  # Return value / exit status that evaluates to true
false=1 # Return value / exit status that evaluates to false

# 1 if the script is being run as root, false otherwise
if [ "$(id -u)" = 0 ] ; then is_root=1 ; else is_root=0 ; fi

# Print one line of text, without escape codes or other shell-specific shenanigans.
# Seriously, shells, you can't even agree on a consistent implementation of echo?
# Usage: print <text>
print() {
	printf '%s\n' "$1"
}

puts() {
	printf '%s' "$1"
}

disabled_commands=' ' # List of commands that should not be used, even if they exist

# Make `have` return false for a comand
# Usage: disable_command <command>
disable_command() {
	disabled_commands="$disabled_commands$1 "
}

# Check if a command is available.
# Usage: have <command>
# Return: $true if the command is available, $false otherwise
have() {
	case "$disabled_commands" in *" $1 "*) return $false ; esac
	command -v "$1" > /dev/null 2>&1
}

# Make a path absolute no matter if it is relative or not
# Usage: abspath <path>
# Too bad we can't just use readlink -m
abspath() {
	case "$1" in
		/*) print "$1" ;;
		 *) print "$PWD/$1" ;;
	esac
}

# Get the canonical representation of an existing path
# Usage: canonicalize <path>
# Too bad we can't just use readlink -f
if have realpath ; then
	canonicalize() { realpath "$1" ; }
elif have grealpath ; then
	canonicalize() { grealpath "$1" ; }
elif have greadlink ; then
	canonicalize() { greadlink -f "$1" ; }
else
	canonicalize() {
		_canonicalize_old_pwd="$PWD"
		_canonicalize_file="$1"
		while true ; do
			cd "$(dirname "$_canonicalize_file")"
			_canonicalize_file="$(basename "$_canonicalize_file")"
			[ -L "$_canonicalize_file" ] || break;
			_canonicalize_file="$(readlink "$_canonicalize_file")"
		done
		echo "$(pwd -P)/$_canonicalize_file"
		cd "$_canonicalize_old_pwd"
	}
fi

cleanup_functions='' # List of functions to be run on exit

# Add a function to ron on exit.
# Functions are un in the order they are added.
# Usage: on_exit <code>
# Cleanup functions will receive one argument: the exit message if any or an empty string.
on_exit() {
	[ -z "$cleanup_functions" ] || cleanup_functions=" $cleanup_functions"
	cleanup_functions="$1$cleanup_functions"
}

# Run exit runctions.
cleanup() {
	_cleanup_functions="$cleanup_functions" ; cleanup_functions=''
	[ -z "$_cleanup_functions" ] && return
	eval "for _cleanup_func in $_cleanup_functions ; do \"\$_cleanup_func\" \"\$@\" ; done"
}

# Register our cleanup handler.
trap "cleanup" EXIT
# Some shells don't have their own (non-libc) SIGINT handler, but the EXIT trap
# won't trigger if there is none!
trap 'print >&4 ; quit 1' INT

# Run cleanup functions with a possible message and then exit.
# Usage: quit <status> [<message>]
quit() {
	cleanup "$2"
	exit $1
}

# Exit with a non-zero status and optionally print a message.
# Usage: die [<message>...]
die() {
	_die_message=''
	if [ $# -gt 0 ] ; then
		_die_message="$1" ; shift
		for _die_arg ; do _die_message="$_die_message $_die_arg" ; done
		_die_message="$_die_message

If you think this is a bug in the install script
please report the complete output at
  $bug_tracker_url"
		if [ ! -z "$logfile" ] && [ -f "$logfile" ] ; then
			_die_message="$_die_message

Also attach the contents of
  $logfile"
			logfile='' # so that we don't remove it on exit
			printf "${red}%s${reset}\\n" "$_die_message" >&4 # also print to priginal stdout
			printf '\n%s\n' 'Preserving log file.' >&4
		fi
	
		printf "${red}%s${reset}\\n" "$_die_message"
	fi
	quit 1 "$_die_message"
}

# Escape a string from stdin for use in a whitespace-seperated list.
# Usage: print <string> | escape_pipe
escape_pipe() {
	sed "s:[^a-zA-Z0-9/_.$1]:\\\\&:g"
}

# Escape a string for use in a whitespace-seperated list.
# Usage: escape <string>
escape() {
	print "$1" | escape_pipe "$2"
}

# Convert a colon-seperated list into an escaped whitespace-seperated list.
# Usage: to_list <colon-list>
to_list() {
	escape "$1" | sed 's/\\:/ /g'
}

# Line-based output into a list
# Usage: ls | lines_to_list
lines_to_list() {
	escape_pipe | tr '\n' ' '
}

# Check if a whitespace separated list contains a string.
# Usage: list_contains <list-var> <needle>
list_contains() {
	eval "_list_contents=\"\$$1\""
	[ -z "$_list_contents" ] && return $false
	eval "for _list_contains_entry in $_list_contents ; do" \
		" [ \"\$_list_contains_entry\" = \"\$2\" ] && return \$true ; done"
	return $false
}

# Append a string to a whitespace separated list.
# Usage: list_append <list-var> <string> [comment]
# Whitespace seperated lists can be loaded into the argument list using:
#  eval "set -- $var"
list_append() {
	_list_entry="$(escape "$2")"
	eval "_list_contents=\"\$$1\""
	if [ -z "$_list_contents" ]
		then eval "$1=\"\$_list_entry\""
		else eval "$1=\"\$_list_contents \$_list_entry\""
	fi
	eval "[ -z \"\$$1__list_count\" ] && $1__list_count=0"
	eval "_list_count=\$$1__list_count"
	eval "$1__list_comment_$_list_count=\"\$3\""
	eval "$1__list_count=\$(($1__list_count + 1))"
}

# Append one list to another, preserving comments.
# Usage: list_merge <list-var> <append-list-var>
list_merge() {
	eval "_list_append=\"\$$2\""
	[ -z "$_list_append" ] && return
	eval "
		_list_merge_i=0
		for _list_merge_entry in $_list_append ; do
			list_append $1 \"\$_list_merge_entry\" \"\$(list_comment $2 \$_list_merge_i)\"
			_list_merge_i=\$((_list_merge_i + 1))
		done
	"
}

# Get a comment associated with alist entry
# Usage: list_comment <list-var> <index>
list_comment() {
	eval "print \"\$$1__list_comment_$2\""
}

# Set a comment associated with alist entry
# Usage: list_comment <list-var> <index> <comment>
set_list_comment() {
	eval "$1__list_comment_$2=\"\$3\""
}

# Append a string to a whitespace separated list if it isn't already in the list.
# Usage: set_append <list-var> <string> [comment]
set_append() {
	if ! list_contains "$1" "$2" ; then
		list_append "$1" "$2" "$3"
	fi
}

# Check if a directory contains a file while ignoring case differences.
# Usage: icontains <dir> <filename>
icontains() {
	[ ! -z "$(find "$1" -mindepth 1 -maxdepth 1 -iname "$2")" ]
}

# Check if a directory or file is writable or can be created.
# Usage: is_writable <path>
is_writable() {
	[ -w "$1" ] && return $true
	[ ! -e "$1" ] && is_writable "$(dirname "$1")"
}

# Create a directory and die with a message on error.
# Usage: create_dir <path> <type>
create_dir() {
	mkdir -p "$1" || die "Could not create $2 directory: $1"
}

probe_file_dirs=''
set_append probe_file_dirs "$user_pwd"
set_append probe_file_dirs "$downloads_dir"
set_append probe_file_dirs "$HOME"
set_append probe_file_dirs "$tempdir"

# Find a file in standard directories.
# Usage: probe_file <command> <filename> [comment]
# Will call `command <file>` for each file found.
probe_file() {
	eval "for _probe_file_d in $probe_file_dirs ; do [ -f \"\$_probe_file_d/\$2\" ] && \$1 \"\$_probe_file_d/\$2\" \"\$3\" && return \$true ; done"
}

# Find files in standard directories.
# Usage: probe_file <command> <list> [comment]
# Will call `command <file>` for each file found.
probe_files() {
	[ -z "$2" ] && return $false
	eval "for _probe_files_file in $2 ; do probe_file \"\$1\" \"\$_probe_files_file\" \"\$3\" && return \$true ; done"
	return $false
}


##########################################################################################
# Parse command-line arguments

extract_zip_reqs=''
list_append extract_zip_reqs 'bsdtar' 'libarchive'
list_append extract_zip_reqs 'unzip'
list_append extract_zip_reqs '7za'
list_append extract_zip_reqs '7z' 'p7zip'
extract_ms_cab_reqs=''
list_append extract_ms_cab_reqs 'bsdtar' 'with libarchive 3.1+'
list_append extract_ms_cab_reqs 'cabextract' "$cabextract_url"
list_append extract_ms_cab_reqs '7za'
list_append extract_ms_cab_reqs '7z' 'p7zip'
extract_installshield_reqs=''
list_append extract_installshield_reqs 'unshield'
extract_rar_reqs=''
list_append extract_rar_reqs 'unrar'
extract_ace_reqs=''
list_append extract_ace_reqs 'unace'
extract_innosetup_reqs=''
list_append extract_innosetup_reqs 'innoextract' "$innoextract_url"
mount_cdrom_reqs=''
list_append mount_cdrom_reqs 'fuseiso'
extract_iso_reqs=''
list_append extract_iso_reqs 'isoinfo'
list_append extract_iso_reqs 'bsdtar' 'libarchive'
list_append extract_iso_reqs '7z' 'p7zip'
extract_cdrom_reqs=''
list_merge extract_cdrom_reqs mount_cdrom_reqs
list_merge extract_cdrom_reqs extract_iso_reqs
download_reqs=''
list_append download_reqs 'wget'
list_append download_reqs 'curl'
list_append download_reqs 'fetch' 'FreeBSD'

printf '%s %s\n' "${white}Welome to the ${green}Arx Fatalis${white} ${patch_ver} data" \
     "install script for UNIX-like systems!${reset}"

patchfile=''      # Main patch file
patchfile_jp=''   # Japanese patch file
sourcefile=''     # Source file or directory
datadir=''        # Output data directory
batch=0           # Never wait for user input
gui=0             # Display a graphical user interface (command-line interface otherwise)
install=1         # Install new non-patch files
selected_stuff=0  # Has the user made a selection
installed_stuff=0 # Have we already installed anything?
patch=1           # Install patch files if needed
probe_patch=1     # Look for patch files in standard locations and download if needed
redirect_log=1    # Redirect standard output/error output to a log file in GUI mode

# Enable compatiblity with old install-* scripts.
# Usage: enable_compat_mode <help-flag> <sourcefile> <patchfile> <datadir>
enable_compat_mode() {
	print \
		"${yellow}Enabling compatibility mode for ${pink}$command${yellow}.${reset}

${dim_yellow}The individual ${dim_pink}install-*${dim_yellow} scripts have been merged.
Rename this script to something else (like ${dim_pink}arx-install-data${dim_yellow}) to unlock its full power!${reset}
" >&2
	batch=1
	probe_patch=0
	if [ -z "$1" ] || [ "$1" = '--help' ] || [ "$1" = '-h' ] ; then
		printf '%s\n\n%s\n' "$5" \
			"${yellow}More options are available in the non-compatiblity mode.${reset}"
		exit $false
	fi
	if [ -z "$2" ] ; then install=0           ; else sourcefile="$2" ; fi
	if [ -z "$3" ] ; then patch=0             ; else patchfile="$3"  ; fi
	if [ -z "$4" ] ; then datadir="$user_pwd" ; else datadir="$4"    ; fi
}

case "$command" in

install-cd)
[ "$1" = "--no-progress" ] && shift # ignore - not supported
enable_compat_mode "$1" "$1" "$2" "$3" "\
Usage: $command path/to/mount/point/ path/to/ArxFatalis_1.21_MULTILANG.exe [output_dir]
or     $command path/to/cd.iso path/to/ArxFatalis_1.21_MULTILANG.exe [output_dir]" ;;

install-copy)
enable_compat_mode "$1" "$1" '' "$2" "\
Usage: $command path/to/ArxFatalis/ [output_dir]" ;;

install-demo)
enable_compat_mode "$1" "$1" '' "$2" "\
Usage: $command path/to/arx_demo_english.zip [output_dir]" ;;

install-gog)
[ "$1" = "--no-progress" ] && shift # ignore - not supported
enable_compat_mode "$1" "$1" '' "$2" "\
Usage: $command path/to/setup_arx_fatalis.exe [output_dir]" ;;

install-verify)
enable_compat_mode "$1" '' '' "$1" "\
Usage: $command [directory]" ;;

*) # non-compatibility mode

# Print elements in a list, joined by ' or '
# Usage: print_help_or <list-var> [color]
print_help_or() {
	_print_help_or_var=$1
	eval "_print_help_or_list=\"\$$1\""
	_print_help_or_color="$2"
	[ -z "$1" ] && return
	eval "
		_print_help_or_i=0
		for _print_help_or_entry in $_print_help_or_list ; do
			[ \$_print_help_or_i = 0 ] || puts ' or '
			printf '%s%s' \"\$_print_help_or_color\" \"\$_print_help_or_entry\"
			[ -z \"\$_print_help_or_color\" ] || puts \"\$reset\"
			_print_help_or_comment=\"\$(list_comment \$_print_help_or_var \$_print_help_or_i)\"
			[ -z \"\$_print_help_or_comment\" ] || printf ' (%s)' \"\$_print_help_or_comment\"
			_print_help_or_i=\$((_print_help_or_i + 1))
		done
	"
}

# Print elements in a list, one per line.
# Usage: print_help_list <prefix-format> <list-var>
# prefi-format will receive one argument: the list index starting at 1
print_help_list() {
	_print_help_list_prefix="$1"
	_print_help_list_var=$2
	eval "_print_help_list_list=\"\$$2\""
	[ -z "$1" ] && return
	eval "
		_print_help_list_i=0
		for _print_help_list_entry in $_print_help_list_list ; do
			case \"\$_print_help_list_prefix\" in
				*%*) printf \"\$_print_help_list_prefix\" \$((_print_help_list_i + 1)) ;;
				*)   puts \"\$_print_help_list_prefix\"
			esac
			printf \"%s\${reset}\" \"\$_print_help_list_entry\"
			_print_help_list_comment=\"\$(list_comment \$_print_help_list_var \$_print_help_list_i)\"
			[ -z \"\$_print_help_list_comment\" ] || printf ' (%s)' \"\$_print_help_list_comment\"
			printf '\n'
			_print_help_list_i=\$((_print_help_list_i + 1))
		done
	"
}

# Print help output.
# Usage: print_help [<error-message>]
print_help() {
	[ -z "${1-}" ] || ( printf '%s\n\n' "${red}$1${reset}" )
	print "
${white}Start the script without any arguments to select paths interactively:
       \$ $command${reset}

Usage: $command [--source] source [--patch patchfile] [[--data-dir] datadir]
       $command [--patch patchfile] [--data-dir datadir]
       $command --verify [[--data-dir] datadir]

 ${green}-s, --source PATH${reset}   Path to the source file or directory
 ${cyan}-d, --data-dir DIR${reset}  Where to install the data
 ${blue}-p, --patch FILE${reset}    Path to the ${patch_ver} patch file
 --patch-jp FILE     Path to the ${patch_jp_ver} Japanese patch file
 -v, --verify        Only verify the files in the data-dir, don't install new ones,
                     except for patch files.
 -n, --no-patch      Don't use a patch file unless explicitly specified.
 -h, --help          Print this message and maybe more
 -b, --batch         Never ask the user questions
 -g, --gui           Show a GUI asking the user what to do
                     Requires ${dim_pink}KDialog${reset}, ${dim_pink}Zenity${reset}, or ${dim_pink}Xdialog${reset}.
                     If none of them are available the script is re-launched
                     in a terminal emulator.
 -c, --cli           Interactively ask the user to select files/directories (no GUI)
 --no-redirect-log   Don't redirect output to a log file when in GUI mode
 --disable-COMMAND   Don't use the given tool, even if it exists.
                     Valid values are 7z, 7za, aterm, bsdtar, cabextract, curl,
                     dcop, fetch, fuseiso, fusermount, gnome-terminal, greadlink,
                     grealpath, gtkterm, gxmessage, innoextract, isoinfo, kdialog,
                     konsole, md5, md5sum, mount, qdbus, realpath, rxvt, umount,
                     unace, unrar, unshield, unzip, urxvt, wget, Xdialog,
                     xmessage, xterm, x-terminal-emulator, zenity.

--gui is enabled by default if there are no arguments *and* stdin, stdout or stderr is not a terminal
"
	[ ! -z "${1-}" ] && exit $false
	help_innosetup="$(print_help_or extract_innosetup_reqs "$dim_pink")"
	help_cdrom="$(print_help_or extract_cdrom_reqs "$dim_pink") or root access"
	help_cab="$(print_help_or extract_ms_cab_reqs "$dim_pink")"
	help_zip="$(print_help_or extract_zip_reqs "$dim_pink")"
	help_unshield="$(print_help_or extract_installshield_reqs "$dim_pink")"
	help_rar="$(print_help_or extract_rar_reqs "$dim_pink")"
	help_ace="$(print_help_or extract_ace_reqs "$dim_pink")"
	help_download="$(print_help_or download_reqs "$dim_pink")"
	help_optpatch="may use the 1.21 patch file and require ${help_innosetup} if not already patched"
	help_probe_file_dirs="
   a) the current working directory  (\$PWD):              $user_pwd
   b) the user's downloads directory (\$XDG_DOWNLOAD_DIR): $downloads_dir
   c) the user's home directory      (\$HOME):             $HOME
   d) the temp directory             (\$TMPDIR):           $tempdir"
  help_probe_file_dirs_patch="${help_probe_file_dirs}
   e) the directory containing the source file"
  help_probed_files="$gog_names $demo_names"
	print "
The ${pink}dependencies${reset} required by the ${command} script depend on the source files.
However, you always need either ${dim_pink}md5sum${reset} or ${dim_pink}md5${reset}.


The ${green}source${reset} can be one of many things:

 * ${white}Mounted Arx Fatalis ${green}cdrom${reset}
   requires:
    - ${help_cab}
    - ${help_innosetup}
   needs the 1.21 patch file

 * ${white}Arx Fatalis cdrom ${green}ISO${white} image / device file${reset}
   requires:
    - ${help_cdrom}
    - ${help_cab}
    - ${help_innosetup}
   needs the 1.21 patch file

 * ${white}Arx Fatalis installer from ${green}GOG.com${white}${reset} ($(print_help_or gog_names))
   requires:
    - ${help_innosetup}
   never uses the 1.21 patch file
   get it from ${dim_green}${gog_url}${reset}

 * ${green}Installed${white} copy of Arx Fatalis${reset} (for example from ${green}Steam${reset})
   ${help_optpatch}
   get it from ${dim_green}${steam_url}${reset}

 * ${white}Arx Fatalis ${green}demo${reset} (one of the following)
$(print_help_list "    - " demo_names)
   requires:
    - ${help_zip} [english/french .zip]
    - ${help_cab}
    - ${help_rar} [english .exe]
    - ${help_ace} [german .exe]
    - ${help_unshield} [japanese .exe]
   never uses the 1.21 patch file
   get it from ${dim_green}${demo_url}${reset}

 * ${white}Extracted Arx Fatalis demo installer${reset}
   requires:
    - ${help_cab}
   never uses the 1.21 patch file

 * ${white}Installed copy of the Arx Fatalis demo${reset}
   never uses the 1.21 patch file

If no source is specified, these files will be probed:
1. The following files in${help_probe_file_dirs}
$(print_help_list "   1.%d ${green}" help_probed_files)
2. The user's ${green}Steam${reset} library, if available
3. If \$WINEPREFIX is set, any installation in there
4. Any installation in the default WINEPREFIX (${green}~/.wine${reset})
5. Any mounted ${green}cdrom${reset} or ISO file


If no ${blue}patch${reset} file is specified, but is needed and
the --no-patch option wasn't specified:
1. Try to find the following files in${help_probe_file_dirs_patch}
   1.1. ${blue}${patch_name}${reset}
   1.2. $(printf "$patch_name_localized" '<LANG>')
        Where <LANG> is one of EN, ES, FR, GE, IT, RU,
        depending on the language of the data files.
2. Downloaded from:
$(print_help_list " - ${dim_blue}" patch_urls)
Downloading the patch file requires ${help_download}.
Extracting the ${patch_ver} patch file requires ${help_innosetup}.

For the Japanese version, if no ${blue}patch-jp${reset} file is specified,
but is needed and the --no-patch option wasn't specified specified:
1. Try to find ${blue}${patch_jp_name}${reset} in${help_probe_file_dirs_patch}
2. Downloaded from:
$(print_help_list " - ${dim_blue}" patch_jp_urls)
Downloading the patch file requires ${help_download}.
Extracting the Japanese patch file requires:
    - ${help_unshield}
    - ${help_cab}.


If no ${cyan}data-dir${reset} to install into is specified,
one is automatically selected similarly to how Arx Libertatis would:
If --verify and --no-patch are give, use the first existing directory
of the following, otherwise, use the first existing writable directory
or, if none exists, the first directory that can be created:
1. Any path in \$${scommand}_PATH or \$arx_PATH (for use in wrapper scripts)
2. \"\${XDG_DATA_DIRS:-\"/usr/local/share/:/usr/share/\"}:/opt\" / \"$data_dir_suffixes\":"
	i=1
	eval "set -- $(to_list "$data_dirs")"
	for prefix in "$@" ; do
		eval "set -- $(to_list "$data_dir_suffixes")"
		for suffix ; do
			printf "   2.%d. ${dim_cyan}%s${reset}\\n" $i "$prefix/$suffix"
			i=$((i + 1))
		done
	done
print "3. \"\${XDG_DATA_HOME:-\"\$HOME/.local/share\"}\" / \"$user_dir_suffixes\""
	i=1
	eval "set -- $(to_list "$user_dir_suffixes")"
	for suffix ; do
		printf "   3.%d. ${dim_cyan}%s${reset}\\n" $i "$data_home/$suffix"
			i=$((i + 1))
	done
	print
	exit $true
}

user_is_sane=1
if [ ! -t 0 ] || [ ! -t 1 ] || [ ! -t 2 ] ; then
	[ $# = 0 ] && gui=1
fi
while [ $# -gt 0 ] ; do
	case "$1" in
		--source=*)               sourcefile="${1#--source=}"   ; install=1 ;;
		-s|--source)      shift ; sourcefile="$1"               ; install=1 ;;
		--data-dir=*)             datadir="${1#--data-dir=}"                ;;
		-d|--data-dir)    shift ; datadir="$1"                              ;;
		--patch=*)                patchfile="${1#--patch=}"       ; patch=1 ;;
		-p|--patch)       shift ; patchfile="$1"                  ; patch=1 ;;
		--patch-jp=*)             patchfile_jp="${1#--patch-jp=}" ; patch=1 ;;
		--patch-jp)       shift ; patchfile_jp="$1"               ; patch=1 ;;
		-v|--verify)                                      install=0 patch=0 ;;
		-n|--no-patch)    [ -z "$patchfile" ] && [ -z "$patchfile_jp" ] && patch=0
		                  probe_patch=0   ;;
		-b|--batch)       batch=1         ;;
		-g|--gui)                   gui=1 ;;
		-c|--cui|--cli)   batch=0 ; gui=0 ;;
		--no-redirect-log) redirect_log=0 ;;
		--i-am-insane)    user_is_sane=0  ;;
		--disable-*)              disable_command "${1#--disable-}" ;;
		--disable)        shift ; disable_command "${1#--disable-}" ;;
		-h|--help)        print_help                     ;;
		-*)               print_help "Uknown option: $1" ;;
		*)
			if [ -z "${sourcefile-}" ] && [ $install = 1 ] ; then sourcefile="$1"
			elif [ -z "${datadir-}"    ] ; then datadir="$1"
			else print_help "Too many options: $1" ; fi
	esac
	[ -z "${1-}" ] && print_help "Expected more options"
	shift;
done

print "See \`${dim_pink}$command --help${reset}\` for available options."

esac

# Make user-provided paths absolute
[ ! -z "$sourcefile" ] && sourcefile="$(abspath "$sourcefile")"
[ ! -z "$datadir" ]    && datadir="$(abspath "$datadir")"
[ ! -z "$patchfile" ]  && patchfile="$(abspath "$patchfile")"

# Sanity check
[ $install = 1 ] && [ $batch = 1 ] && [ -z "$sourcefile" ] && [ $user_is_sane = 1 ] \
	&& die "You have used --batch without providing a source file!
This would just pick the first source file found, which is a bad idea™.
If you really want this, add the --i-am-insane option."


##########################################################################################
# User interface abstraction

_dialog_title="Arx Fatalis ${patch_ver} data installer"

# Handle magic environment variable to tell the script that it has been launched
# in its own terminal and should not try to create a GUI.
if [ $batch = 0 ] && [ "$_arx_install_data_force_cli" = 1 ] ; then
	trap '_arx_install_data_force_cli=0 ; quit 1' INT
	printf "\n${yellow}%s${reset}\n\n" \
		'Note: Install KDialog, Zenity or Xdialog for a better GUI'
	wait_exit() {
		[ "$_arx_install_data_force_cli" = 1 ] && print 'Press enter to exit...' && read f
		exit $false
	}
	on_exit wait_exit
	gui=0
	for var in batch install patch probe_patch sourcefile datadir \
		patchfile patchfile_jp disabled_commands; do
		eval "$var=\"\$_arx_install_data_force_$var\""
	done
fi

# Select the dialog backend to use
if [ $gui = 1 ] ; then
	
	# Detect if we are running in a KDE session
	is_kde=0
	case "$DESKTOP_SESSION" in *kde*|*KDE*) is_kde=1 ; esac
	[ -z "$KDE_FULL_SESSION" ]           || is_kde=1
	[ -z "$KDE_SESSION_UID" ]            || is_kde=1
	[ -z "$KDE_SESSION_VERSION" ]        || is_kde=1
	
	# Select the GUI backend, prefer kdialog for KDE sessions, zenity otherwise
	if [ $is_kde = 1 ] ; then preferred=kdialog ; else preferred=zenity ; fi
	for backend in $preferred zenity kdialog Xdialog ; do
		have $backend && gui=$backend && break
	done
	
	if [ $gui = 1 ] ; then
		
		# No dialog backend available
		# Try opening a graphical terminal and launching the script in there.
		print 'No GUI dialog backend is available - trying to launch a terminal emulator'
		term_cmd="$(abspath "$(command -v "$0" 2> /dev/null)")"
		# Not all terminals accept command arguments in the same way.
		# Instead of hacking terminal-specific code, use a magic environment
		# variable to tell the sub-process how to behave.
		_arx_install_data_force_cli=1
		export _arx_install_data_force_cli
		for var in batch install patch probe_patch sourcefile datadir \
			patchfile patchfile_jp disabled_commands ; do
			eval "_arx_install_data_force_$var=\"\$$var\""
			eval "export _arx_install_data_force_$var"
		done
		if [ $is_kde = 1 ] ; then preferred=konsole ; else preferred=x-terminal-emulator ; fi
		for backend in x-terminal-emulator $preferred \
			aterm urxvt rxvt konsole xterm gnome-terminal
		do
			if have $backend ; then
				$backend -e "$term_cmd" || continue
				exit $true
			fi
		done
		
		# Hm, that didn't work either - bail
		message="No GUI dialog backend is available"
		message="$message - install KDialog, Zenity or Xdialog, or use the --cli option."
		# Final attempt to let the user know what happened
		for backend in gxmessage xmessage ; do
			if have $backend ; then
				$backend -center -buttons OK "$_dialog_title

$message"
				break
			fi
		done
		die "$message"
		
	fi
	
	# We don't need colors for the UI, but they may cause problems - get rid of them
	disable_color
	
	if [ $redirect_log = 1 ] ; then
		# Redirect all further output into a log file
		logfile="$(abspath "$(mktemp "$tempdir/arx-install-data.log.XXXXX")")"
		clean_logfile() {
			[ -z "$logfile" ] || rm -f "$logfile"
		}
		on_exit clean_logfile
		print "Enabling GUI mode, standard output/error saved to $logfile"
		print "Use the --cli option for an interactive command-line interface."
		exec > "$logfile" 2>&1
	else
		print "Enabling GUI mode..."
		print "Use the --cli option for an interactive command-line interface."
	fi
	
elif [ ! "$_arx_install_data_force_cli" = 1 ] ; then
	
	print "Enabling CLI mode, use the --gui option for a graphical interface."
	
fi

#----------------------------------------------------------------------------------------#
# Functions for controlling an asynchronous process via stdin

pipe_file=''
pipe_pid=0

# Run a command in the background and open a pipe to pass commmands to it
# Usage: pipe_create <command> [<args>...]
pipe_create() {
	
	pipe_destroy
	
	# Pipe commands via a FIFO or, if that fails, via a regular file
	pipe_file="$(mktemp -u "$tempdir/arx-install-data.pipe.XXXXX")"
	if mkfifo -m 600 "$pipe_file" 2> /dev/null ; then
		# Fast communication via a FIFO
		"$@" < "$pipe_file" &
		pipe_pid="$!"
	else
		# Fallback via regular file, may use polling
		pipe_file="$(mktemp "$tempdir/arx-install-data.pipe.XXXXX")"
		[ -z "$pipe_file" ] && return $false
		tail -f "$pipe_file" | "$@" &
		pipe_pid="$!"
	fi
	
	# Open fd 3 for writing into the pipe
	exec 3> "$pipe_file"
	
	return $true
}

# Kill the program created via pipe_create and cleanup files
# Usage: pipe_destroy
pipe_destroy() {
	
	# Close fd pointing to the pipe
	exec 3<&-
	
	# Remove the FIFO or temp file
	[ -z "$pipe_file" ] || rm -f "$pipe_file" > /dev/null 2>&1
	pipe_file=''
	
	# Terminate the remote process
	[ "$pipe_pid" = 0 ] || kill "$pipe_pid" > /dev/null 2>&1
	pipe_pid=0
	
	return $true
}

# Check if the remote process is running
# Usage: pipe_exists || print 'oh noes'
pipe_exists() {
	[ -z "$pipe_file" ] && return $false
	[ "$pipe_pid" = 0 ] && return $false
	kill -s 0 "$pipe_pid" > /dev/null 2>&1
}

# Send a message to the remote process
# Usage: pipe_write <command>
pipe_write() {
	[ -z "$pipe_file" ] || print "$1" >&3
}

#----------------------------------------------------------------------------------------#
# Code for the different GUI/CLI implementations
# Each implementation exposes dialog_* primitives that are used by generic functions.

case $gui in

#----------------------------------------------------------------------------------------#
zenity)

# Helper functions

# Run Zenity
# Usage: zenity_run <title-prefix> <dialog-type> [<args>...]
zenity_run() {
	_zenity_run_t="$1" ; shift
	[ -z "$_zenity_run_t" ] || _zenity_run_t="$_zenity_run_t - "
	zenity --title "$_zenity_run_t$_dialog_title" "$@"
}

# Dialog abstraction

# Create the main progress window.
# Usage: dialog_create
dialog_create() {
	pipe_create zenity --title "$_dialog_title" --width 450 --progress
}

# Destroy the main progress window.
# Usage: dialog_destroy
dialog_destroy() {
	pipe_destroy
}

# Show an error dialog.
# Usage: dialog_error <message>
dialog_error() {
	zenity_run 'Error' --error --no-wrap --text="$1"
}

# Show a message box.
# Usage: dialog_message <message>
dialog_message() {
	zenity_run 'Status' --info --no-wrap --text="$1"
}

# Ask a yes/no question.
# Usage: dialog_ask <question>
dialog_ask() {
	zenity_run 'Confirm' --question --no-wrap --ok-label=Yes --cancel-label=No --text="$1"
}

# Has the user quested to cancel the operation?
# Usage: dialog_cancelled && print "cancelled"
dialog_cancelled() {
	! pipe_exists
}

# Set the status text.
# Usage: dialog_set_text <text>
dialog_set_text() {
	pipe_write "#$1"
}

# Set if the progress bar should continously animate instead of showing the value.
# Usage: dialog_set_pulsate <enable>
dialog_set_pulsate() {
	if [ $1 = 1 ]
		then pipe_write "pulsate:true"
		else pipe_write "pulsate:false"
	fi
}

# Set the current progress value.
# Usage: dialog_set_value <percentage>
dialog_set_value() {
	pipe_write "$1"
}

# Select an entry in a list.
# Usage dialog_select_entry <var> <label> <tag1> <item1> [ <tag2> < item2> ... ]
dialog_select_entry() {
	_zenity_select_entry_v="$1" ; shift
	_zenity_select_entry_t="$1" ; shift
	_zenity_select_entry_r="$(
		zenity_run 'Select path' --width 550 --height 300 \
			--list --text="$_zenity_select_entry_t" \
			--column '#'  --column 'Path' --hide-column=1 "$@" --hide-header
	)"
	[ -z "$_zenity_select_entry_r" ] && return $false
	eval "$_zenity_select_entry_v=\"\$_zenity_select_entry_r\""
	return $true
}

# dialog_select_path does not support the --any flag
dialog_select_path_any=0

# Let the user select a path.
# Usage: dialog_select_path (--file|--dir|--any) <result-var> <label>
# Any is only supported if $dialog_select_path_any is 1.
dialog_select_path() {
	case "$1" in
		--any) die 'not implemented' ;;
		--file) _zenity_select_path_f='--file-selection' ;;
		--dir)  _zenity_select_path_f='--file-selection --directory' ;;
	esac
	_zenity_select_path="$(
		eval "zenity_run \"\$3\" $_zenity_select_path_f" 2> /dev/null
	)"
	[ -z "$_zenity_select_path" ] && return $false
	eval "$2=\"\$_zenity_select_path\""
	return $true
}

dialog_retry() {
	zenity_run 'Error' --question --no-wrap --text="$1" \
		--ok-label='Retry' --cancel-label='Ignore'
	case $? in
		0) dialog_retry_choice='retry' ;;
		1) dialog_retry_choice='ignore' ;;
		*) dialog_retry_choice='abort' ;;
	esac
}

;;

#----------------------------------------------------------------------------------------#
kdialog)

# Helper functions

kdialog_handle='' # dbus/dcop handle for the main progress window

# Send a message to the main KDialog instance non-_q variants hide all output
kdialog_qdbus_q() { have qdbus && eval "qdbus $kdialog_handle \"\$@\"" 2> /dev/null ; }
kdialog_qdbus()   { kdialog_qdbus_q "$@" > /dev/null                                ; }
kdialog_dcop_q()  { have dcop  && eval "dcop  $kdialog_handle \"\$@\"" 2> /dev/null ; }
kdialog_dcop()    { kdialog_dcop_q  "$@" > /dev/null                                ; }
kdialog_cmd_q()   { kdialog_qdbus_q "$@" || kdialog_dcop_q "$@"                     ; }
kdialog_cmd()     { kdialog_cmd_q "$@" > /dev/null                                  ; }

# Run KDialog
# Usage: kdialog_run <title-prefix> <dialog-type> [<args>...]
kdialog_run() {
	_kdialog_run_t="$1" ; shift
	[ -z "$_kdialog_run_t" ] || _kdialog_run_t="$_kdialog_run_t - "
	kdialog --icon arx-libertatis --title "$_kdialog_run_t$_dialog_title" "$@"
}

# Dialog abstraction

# Create the main progress window.
# Usage: dialog_create
dialog_create() {
	dialog_destroy
	_kdialog_force_width='WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
	kdialog_handle="$(kdialog_run '' --progressbar "$_kdialog_force_width" 0)"
	[ -z "$kdialog_handle" ] && return $false
	kdialog_cmd showCancelButton true
	return $true
}

# Destroy the main progress window.
# Usage: dialog_destroy
dialog_destroy() {
	[ -z "$kdialog_handle" ] && return $true
	kdialog_cmd close
	kdialog_handle=''
}

# Show an error dialog.
# Usage: dialog_error <message>
dialog_error() {
	kdialog_run 'Error' --error "$1" > /dev/null 2> /dev/null
}

# Show a message box.
# Usage: dialog_message <message>
dialog_message() {
	kdialog_run 'Status' --msgbox "$1" > /dev/null 2> /dev/null
}

# Ask a yes/no question.
# Usage: dialog_ask <question>
dialog_ask() {
	kdialog_run 'Confirm' --warningyesno "$1" > /dev/null 2> /dev/null
}

# Has the user quested to cancel the operation?
# Usage: dialog_cancelled && print "cancelled"
dialog_cancelled() {
	[ "$(kdialog_cmd_q wasCancelled || print true)" = true ]
}

# Set the status text.
# Usage: dialog_set_text <text>
dialog_set_text() {
	kdialog_qdbus setLabelText "$1" || kdialog_dcop setLabel "$1"
}

# Set if the progress bar should continously animate instead of showing the value.
# Usage: dialog_set_pulsate <enable>
dialog_set_pulsate() {
	_kdialog_max=100
	[ $1 = 1 ] && _kdialog_max=0
	kdialog_qdbus Set "" maximum $_kdialog_max || kdialog_dcop setMaximum $_kdialog_max
}

# Set the current progress value.
# Usage: dialog_set_value <percentage>
dialog_set_value() {
	kdialog_qdbus Set "" value "$1" || kdialog_dcop setProgress "$1"
	[ $1 = 100 ] && kdialog_cmd showCancelButton true
}

# Select an entry in a list.
# Usage dialog_select_entry <var> <label> <tag1> <item1> [ <tag2> < item2> ... ]
dialog_select_entry() {
	_kdialog_select_entry_v="$1" ; shift
	_kdialog_select_entry_t="$1" ; shift
	_kdialog_select_entry_w="                                   "
	_kdialog_select_entry_w="$_kdialog_select_entry_w$_kdialog_select_entry_w"
	_kdialog_select_entry_r="$(
		kdialog_run 'Select path' \
			--menu "$_kdialog_select_entry_t$_kdialog_select_entry_w" "$@" 2> /dev/null
	)"
	[ -z "$_kdialog_select_entry_r" ] && return $false
	eval "$_kdialog_select_entry_v=\"\$_kdialog_select_entry_r\""
	return $true
}

# dialog_select_path does not support the --any flag
dialog_select_path_any=0

# Let the user select a path.
# Usage: dialog_select_path (--file|--dir|--any) <result-var> <label>
# Any is only supported if $dialog_select_path_any is 1.
dialog_select_path() {
	case "$1" in
		--any) die 'not implemented' ;;
		--file) _kdialog_select_path_f=--getopenfilename ;;
		--dir)  _kdialog_select_path_f=--getexistingdirectory ;;
	esac
	_kdialog_select_path="$(
		kdialog_run "$3" $_kdialog_select_path_f "$HOME" 2> /dev/null
	)"
	[ -z "$_kdialog_select_path" ] && return $false
	eval "$2=\"\$_kdialog_select_path\""
	return $true
}

dialog_retry() {
	kdialog_run 'Error' \
		--yes-label 'Retry' --no-label 'Ignore' --cancel-label 'Abort' \
		--warningyesnocancel "$1" 2>&1
	case $? in
		0) dialog_retry_choice='retry' ;;
		1) dialog_retry_choice='ignore' ;;
		*) dialog_retry_choice='abort' ;;
	esac
}

;;

#----------------------------------------------------------------------------------------#
Xdialog)

# Helper functions

# Run Xdialog
# Usage: Xdialog_run <title-prefix> <dialog-type> [<args>...]
Xdialog_run() {
	_Xdialog_run_t="$1" ; shift
	[ -z "$_Xdialog_run_t" ] || _Xdialog_run_t="$_Xdialog_run_t - "
	Xdialog --left --title "$_Xdialog_run_t$_dialog_title" "$@"
}

# Dialog abstraction

# Create the main progress window.
# Usage: dialog_create
dialog_create() {
	_Xdialog_width='WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW'
	pipe_create Xdialog --left --title "$_dialog_title" --gauge "$_Xdialog_width" 0 0
}

# Destroy the main progress window.
# Usage: dialog_destroy
dialog_destroy() {
	pipe_destroy
}

# Show an error dialog.
# Usage: dialog_error <message>
dialog_error() {
	dialog_message "$1" # no dedicated error box for Xdialog
}

# Show a message box.
# Usage: dialog_message <message>
dialog_message() {
	Xdialog_run 'Status' --msgbox "$1" 0 0
}

# Ask a yes/no question.
# Usage: dialog_ask <question>
dialog_ask() {
	Xdialog_run 'Confirm' --yesno "$1" 0 0
}

# Has the user quested to cancel the operation?
# Usage: dialog_cancelled && print "cancelled"
dialog_cancelled() {
	! pipe_exists
}

# Set the status text.
# Usage: dialog_set_text <text>
dialog_set_text() {
	pipe_write 'XXX'
	pipe_write "$1"
	pipe_write 'XXX'
}

# Set if the progress bar should continously animate instead of showing the value.
# Usage: dialog_set_pulsate <enable>
dialog_set_pulsate() {
	true # Pulsate is not supported by Xdialog
}

# Set the current progress value.
# Usage: dialog_set_value <percentage>
dialog_set_value() {
	pipe_write "$1"
}

# Select an entry in a list.
# Usage dialog_select_entry <var> <label> <tag1> <item1> [ <tag2> < item2> ... ]
dialog_select_entry() {
	_Xdialog_select_entry_v="$1" ; shift
	_Xdialog_select_entry_t="$1" ; shift
	_Xdialog_select_entry_r="$(
		Xdialog_run 'Select path' \
			--menubox "$_Xdialog_select_entry_t" 20 80 10 "$@" 2>&1
	)"
	[ -z "$_Xdialog_select_entry_r" ] && return $false
	eval "$_Xdialog_select_entry_v=\"\$_Xdialog_select_entry_r\""
	return $true
}

# dialog_select_path does not support the --any flag
dialog_select_path_any=0

# Let the user select a path.
# Usage: dialog_select_path (--file|--dir|--any) <result-var> <label>
# Any is only supported if $dialog_select_path_any is 1.
dialog_select_path() {
	case "$1" in
		--any) die 'not implemented' ;;
		--file) _Xdialog_select_path_f=--fselect ;;
		--dir)  _Xdialog_select_path_f=--dselect ;;
	esac
	_Xdialog_select_path="$(
		Xdialog_run "$3" $_Xdialog_select_path_f "$HOME" 0 0 2>&1
	)"
	[ -z "$_Xdialog_select_path" ] && return $false
	eval "$2=\"\$_Xdialog_select_path\""
	return $true
}

dialog_retry() {
	Xdialog_run 'Error' --ok-label='Retry' --cancel-label='Ignore' --yesno "$1" 0 0
	case $? in
		0) dialog_retry_choice='retry' ;;
		1) dialog_retry_choice='ignore' ;;
		*) dialog_retry_choice='abort' ;;
	esac
}

;;

#----------------------------------------------------------------------------------------#
0) # command-line

# Dialog abstraction

# Create the main progress window.
# Usage: dialog_create
dialog_create() {
	true
}

# Destroy the main progress window.
# Usage: dialog_destroy
dialog_destroy() {
	true
}

# Show an error dialog.
# Usage: dialog_error <message>
dialog_error() {
	true # error messages are always printed to stdout
}

# Show a message box.
# Usage: dialog_message <message>
dialog_message() {
	true
}

# Ask a yes/no question.
# Usage: dialog_ask <question>
dialog_ask() {
	die 'unimplemented'
}

# Has the user quested to cancel the operation?
# Usage: dialog_cancelled && print "cancelled"
dialog_cancelled() {
	false # never cancelled SIGINT is not trapped
}

# Set the status text.
# Usage: dialog_set_text <text>
dialog_set_text() {
	true
}

# Set if the progress bar should continously animate instead of showing the value.
# Usage: dialog_set_pulsate <enable>
dialog_set_pulsate() {
	true
}

# Set the current progress value.
# Usage: dialog_set_value <percentage>
dialog_set_value() {
	true
}

# Select an entry in a list.
# Usage dialog_select_entry <var> <label> <tag1> <item1> [ <tag2> < item2> ... ]
dialog_select_entry() {
	_cli_select_entry_var="$1" ; shift
	
	# Print a list for the user to select from
	print "$1:" ; shift
	_cli_select_entry_min=$1
	_cli_select_entry_max=$1
	_cli_select_entry_f=' [default]'
	while [ $# -gt 0 ] ; do
		_cli_select_entry_i=$1 ; shift
		_cli_select_entry_t="$1" ; shift
		if [ $_cli_select_entry_i -lt $_cli_select_entry_min ] ; then
			_cli_select_entry_min=$_cli_select_entry_i
		fi
		if [ $_cli_select_entry_i -gt $_cli_select_entry_max ] ; then
			_cli_select_entry_max=$_cli_select_entry_i
		fi
		printf ' %d) %s%s\n' $_cli_select_entry_i \
			"$_cli_select_entry_t" "$_cli_select_entry_f"
		_cli_select_entry_f=''
	done
	
	# Read a number (or empty string for the first entry)
	while true ; do
		
		puts '> #'
		read -r _cli_select_entry_r
		
		[ -z "$_cli_select_entry_r" ] && _cli_select_entry_r=1
		
		case "$_cli_select_entry_r" in
			'quit') ;; 'q') ;; 'exit') ;; 'abort') ;;
			*)
			if [ ! "$_cli_select_entry_r" -lt $_cli_select_entry_min ] 2> /dev/null \
			   && [ ! "$_cli_select_entry_r" -gt $_cli_select_entry_max ] 2> /dev/null
			then
				eval "$_cli_select_entry_var=\"\$_cli_select_entry_r\""
				return $true
			else
				printf "Please enter a number between %d and %d.\n" \
					$_cli_select_entry_min $_cli_select_entry_max
				continue
			fi
		esac
		die
		
	done
	
	return $true
}

# dialog_select_path supports the --any flag
dialog_select_path_any=1

# Let the user select a path.
# Usage: dialog_select_path (--file|--dir|--any) <result-var> <label>
# Any is only supported if $dialog_select_path_any is 1.
dialog_select_path() {
	_cli_select_path_var="$2"
	print "$3:" ; shift
	puts '> '
	read -r _cli_select_path_r
	[ -z "$_cli_select_path_r" ] && return $false
	eval "$_cli_select_path_var=\"\$_cli_select_path_r\""
	return $true
}

dialog_retry() {
	printf '\n%s\n' "${red}Error:${reset} $1"
	while true ; do
		print "Abort / [Retry] / Ignore"
		puts '> '
		read -r _cli_dialog_retry_r
		case "$_cli_dialog_retry_r" in
			a|A|abort|Abort|ABORT)    dialog_retry_choice='abort'  ; return ;;
			''|r|R|retry|Retry|RETRY) dialog_retry_choice='retry'  ; return ;;
			i|I|ignore|Ignore|IGNORE) dialog_retry_choice='ignore' ; return ;;
		esac
	done
}

esac


##########################################################################################
# Common user interface implementation

# Ask the user if the setup should really be cancelled.
handle_cancel() {
	_handle_cancel_message="Are you sure you want to exit the Arx Fatalis data installer?"
	if [ $installed_stuff = 1 ] ; then
		_handle_cancel_message="$_handle_cancel_message

Already installed files will not be removed!"
	fi
	if [ $installed_stuff = 1 ] || [ $selected_stuff = 1 ] ; then
		dialog_ask "$_handle_cancel_message" || return $true
	fi
	print 'Aborted by user' && die
}

# Update the status.
# Usage: status (<percent>|--temp) [<message>]
_status_text="Initializing..."
_status_cur=''
_status_pulsate=default
status() {
	
	if [ "$1" = '--temp' ] ; then
		_status_value=0
		_status_temp=1
	else
		_status_value=$1
		_status_temp=0
	fi
	_status_new="${2:-$_status_text}"
	
	# Handle the cancel and close buttons
	if dialog_cancelled ; then
		handle_cancel
		dialog_create || die "Could not re-create progress window."
		_status_cur=''
		_status_pulsate=default
	fi
	
	# Update the progress text if one was provided
	if [ ! "$_status_cur" = "$_status_new" ] ; then
		_status_cur="$_status_new"
		print "$_status_new"
		dialog_set_text "$_status_new"
	fi
	[ $_status_temp = 0 ] && _status_text="$_status_new"
	
	# Set the maximum progress value
	if [ $_status_value = 0 ] ; then _new_status_pulsate=1 ; else _new_status_pulsate=0 ; fi
	if [ ! "$_status_pulsate" = $_new_status_pulsate ] ; then
		_status_pulsate="$_new_status_pulsate"
		dialog_set_pulsate $_status_pulsate
	fi
	
	# Update the progress value
	[ $_status_pulsate = 0 ] && dialog_set_value $_status_value
	
}

# Print an error message and show an error dialog if we have a GUI/
# Usage: error <message>
error() {
	print "${dim_red}$1${reset}"
	dialog_error "$1"
}

# Let the user select an item from a list or enter a custom one.
# In batch mode, select the first one if --first is given, die otherwise.
# Usage: user_select_entry (--existing|--writable) (--any|--file|--dir) \
#                          <list> <result-var> <desc> <desc-color> <list-color> <verb>
user_select_entry() {
	
	_user_select_entry_access="$1"
	_user_select_entry_t="$2"
	_user_select_entry_lname="$3"
	eval "_user_select_entry_list=\"\$$_user_select_entry_lname\""
	_user_select_entry_var="$4"
	_user_select_entry_desc="$5"
	_user_select_entry_color1="$6"
	_user_select_entry_color2="$7"
	_user_select_entry_verb="$8"
	
	# Select the first element if in batch mode
	eval "_user_select_entry_current=\"\$$_user_select_entry_var\""
	if [ $batch = 1 ] || [ ! -z "$_user_select_entry_current" ] ; then
		_user_select_entry_=''
		eval "set -- $_user_select_entry_list"
		for _user_select_entry ; do
			_user_select_entry_="$_user_select_entry"
			break
		done
		[ -z "$_user_select_entry_" ] && die "Missing $_user_select_entry_desc!"
		eval "$_user_select_entry_var=\"\$_user_select_entry_\""
		return $true
	fi
	
	if [ $gui = 0 ]
		then print
		else status --temp "Select a ${_user_select_entry_desc}"
	fi
	
	_user_select_entry_i=1
	_user_select_entry_nolist=0
	if [ -z "$_user_select_entry_list" ] \
	&& [ ! $_user_select_entry_t = --any ] \
	&& [ ! $dialog_select_path_any = 1 ] ; then
		
		# No entries detected - direclty promt the user
		_user_select_entry_num=1
		_user_select_entry_nolist=1
		
	else
		
		_user_select_entry_tlist=''
		
		# Format the list entries in a user friendly way
		eval "set -- $_user_select_entry_list"
		for _user_select_entry ; do
			
			# Use 'command (file)' if comment available or 'file' otherwise
			_user_select_entry_comment="$(
				list_comment "$_user_select_entry_lname" "$((_user_select_entry_i - 1))"
			)"
			if [ -z "$_user_select_entry_comment" ] ; then
				_user_select_entry_label="$_user_select_entry_color2$_user_select_entry$reset"
			else
				_user_select_entry_label="$_user_select_entry_color2$_user_select_entry_comment$reset"
				_user_select_entry_label="$_user_select_entry_label: $_user_select_entry"
			fi
			
			# Add the tag and label to the arguments
			list_append _user_select_entry_tlist $_user_select_entry_i
			list_append _user_select_entry_tlist "$_user_select_entry_label"
			
			# Remeber the file for the tag
			eval "_user_select_entry_$_user_select_entry_i=\"\$_user_select_entry\""
			
			_user_select_entry_i=$((_user_select_entry_i + 1))
		done
		
		# Add entries for custom files/diectories
		if [ $_user_select_entry_t = --any ] && [ $dialog_select_path_any = 1 ] ; then
			list_append _user_select_entry_tlist $_user_select_entry_i
			list_append _user_select_entry_tlist "Select file or directory to $_user_select_entry_verb.."
		else
			_user_select_entry_ii=$_user_select_entry_i
			if [ $_user_select_entry_t = --any ] || [ $_user_select_entry_t = --file ] ; then
				list_append _user_select_entry_tlist $_user_select_entry_ii
				list_append _user_select_entry_tlist "Select file to $_user_select_entry_verb..."
				_user_select_entry_ii=$((_user_select_entry_ii + 1))
			fi
			if [ $_user_select_entry_t = --any ] || [ $_user_select_entry_t = --dir ] ; then
				list_append _user_select_entry_tlist $_user_select_entry_ii
				list_append _user_select_entry_tlist "Select directory to $_user_select_entry_verb..."
				_user_select_entry_ii=$((_user_select_entry_ii + 1))
			fi
		fi
		
	fi
	
	# Loop until we have selected a value
	while true ; do
		
		if [ $_user_select_entry_nolist = 0 ] ; then
			
			# Ask the user to select an entry
			eval "set -- $_user_select_entry_tlist"
			while true ; do
				if dialog_select_entry _user_select_entry_num \
					"${_user_select_entry_color1}Select a ${_user_select_entry_desc}${reset}" "$@"
				then
					break
				else
					handle_cancel
				fi
			done
			
		fi
		
		if [ $_user_select_entry_num -lt $_user_select_entry_i ] ; then
			
			# User selected an entry -- return that
			eval "$_user_select_entry_var=\"\$_user_select_entry_$_user_select_entry_num\""
			selected_stuff=1
			return $true
			
		fi
		
		# Loop until we have selected a path of the correct type
		while true ; do
			
			# Adjust type based on user selection
			if [ $_user_select_entry_t = --any ] && [ ! $dialog_select_path_any = 1 ] ; then
				if [ $_user_select_entry_num = $_user_select_entry_i ]
					then _user_select_entry_type=--file
					else _user_select_entry_type=--dir
				fi
			else
				_user_select_entry_type=$_user_select_entry_t
			fi
			
			_user_select_entry_c="Select a custom source " 
			
			if ! dialog_select_path "$_user_select_entry_type" _user_select_entry_path \
				"${_user_select_entry_color1}Choose a custom ${_user_select_entry_desc}${reset}" \
				"$@" ; then
				
				# User cancelled the dialog - return to the main selection, unless there is none
				if [ $_user_select_entry_nolist = 1 ] ; then
					handle_cancel
					continue # let the user try again
				fi
				break # return to list selection
				
			fi
			
			_user_select_entry_path="$(abspath "$_user_select_entry_path")"
			
			# The user selected a path, now check if it meets our criteria.
			case "$_user_select_entry_access" in
				--existing)
				if [ ! -e "$_user_select_entry_path" ]  ; then
					error "$_user_select_entry_path does not exist!"
					continue # let the user try again
				fi ;;
				--writable)
				if ! is_writable "$_user_select_entry_path" ; then
					error "$_user_select_entry_path is not writable!"
					continue # let the user try again
				fi ;;
			esac
			case "$_user_select_entry_t" in
				--any) ;; # anything goes
				--file)
				if [ -e "$_user_select_entry_path" ] && [ -d "$_user_select_entry_path" ] ; then
					error "$_user_select_entry_path is is a directory, but we need a file!"
					continue
				fi ;;
				--dir)
				if [ -e "$_user_select_entry_path" ] && [ ! -d "$_user_select_entry_path" ] ; then
					error "$_user_select_entry_path is is a file, but we need a directory!"
					continue
				fi ;;
			esac
			
			# Everything went better than expected - save the result
			eval "$_user_select_entry_var=\"\$_user_select_entry_path\""
			selected_stuff=1
			return $true
			
		done
		
	done
	
}

# Show error message and close windows on exit
ui_cleanup() {
	[ -z "$1" ] || dialog_error "$1"
	dialog_destroy
}
on_exit ui_cleanup

# Initilize the UI
dialog_create || die "
Could not create main window.

You could try the --cli option.
"
[ $gui = 0 ] || status --temp "$_status_text"


##########################################################################################
# Autodetect source file/dir

sourcefiles='' # List of source file/directory candidates

# Add a source file or directory to the list of candidates.
# Usage: found_source_file <file> [comment]
# Return: $true if more source files should be probed, $false otherwise.
found_source_file() {
	set_append sourcefiles "$(canonicalize "$1")" "$2"
	if [ $batch = 1 ] ; then return $true ; else return $false ; fi
}

# Get a subsection in a .vdf file
# Usage: cat config | steam_get_section <section>
steam_get_section() {
	# Abuse indentation to find the section end
	grep -Pzoi "\\n(\\s*)\"$1\"\\n\\s+\\{(\\n.*)*?\\n\\1}"
}

# Get a subsection in a .vdf file - and everything after it
# Usage: cat config | steam_get_section_tail <section>
steam_get_section_tail() {
	# Abuse indentation to find the section end
	grep -Pzoi "\\n\\s*\"$1\"\\n\\s+\\{(\\n.*)*"
}

# Remove backtick escapes
# Usage: echo <escaped_string> | unescape
steam_unescape() {
	sed 's/\\\(.\)/\1/g'
}

# Get an entry in a .vdf file, or the first one if there are multiple
# Usage: cat config | steam_get_entry <key>
steam_get_entry() {
	grep -Pi "^\\s*\"$1\"\s*\".*\"\\s*$" \
	| head -1 \
	| sed 's/^[^"]*"[^"]*"[^"]*"\(.*\)"[^"]*$/\1/' \
	| steam_unescape
}

# Get the app information section for an appid
# Usage: steam_get_appinfo <config_file> <appid>
steam_get_appinfo() {
	_steam_config="$1"
	_steam_appid="$2"
	
	_steam_appinfo="$(
		  steam_get_section 'Software' < "$_steam_config" \
		| steam_get_section 'Valve' \
		| steam_get_section 'Steam' \
		| steam_get_section 'apps' \
		| steam_get_section "$_steam_appid"
	)"
	
	# Fallback if the tree structure changed
	[ -z "$_steam_appinfo" ] && _steam_appinfo="$(
		steam_get_section "$_steam_appid" < "$_steam_config"
	)"
	
	# Fallback if the indentation changed
	[ -z "$_steam_appinfo" ] && _steam_appinfo="$(
		steam_get_section_tail "$_steam_appid" < "$_steam_config"
	)"
	
	print "$_steam_appinfo"
}

# Get a config entry for an appid
# Usage: steam_get_app_config <config_file> <appid>
steam_get_installdir() {
	steam_get_appinfo "$1" "$2" | steam_get_entry 'installdir'
}

# Find possible source directories from a Steam library
# Usage: probe_steam_library <steamlibrary>
# Return: $true if more source files should be probed, $false otherwise.
probe_steam_library() {
	_steam_library="$1"
	for _steam_appdir in "$_steam_library/SteamApps" "$_steam_library/steamapps" ; do
		_steam_installdir="$_steam_appdir/common/$steam_install_dir"
		if [ -d "$_steam_installdir" ] ; then
			found_source_file "$_steam_installdir" "Steam" && return $true
		fi
	done
	return $false
}

# Find possible source directories from a Steam installation
# Usage: probe_steam_root <steamroot>
# Return: $true if more source files should be probed, $false otherwise.
probe_steam_root() {
	_steam_root="$1"
	
	_steam_config="$_steam_root/config/config.vdf"
	
	if [ -f "$_steam_config" ] ; then
		
		# Strategy 1: Read install directory from Steam config file
		_steam_installdir="$(steam_get_installdir "$_steam_config" "$steam_appid")"
		if [ -d "$_steam_installdir" ] ; then
			found_source_file "$_steam_installdir" "Steam" && return $true
		fi
		
		# Strategy 2: Look for known install directory in steam libraries
		_steam_libraries="$(
			sed -n '/BaseInstallFolder/s/^[^\"]*\"[^\"]*\"[^\"]*\"\([^\"]*\)\".*/\1/p' \
			< "$_steam_config" \
			| sed 's:[^a-zA-Z0-9/_.\n]:\\&:g'
		)"
		_steam_library_call='probe_steam_library "$_steam_library" && return $true'
		eval "for _steam_library in $_steam_libraries ; do $_steam_library_call ; done"
		
	fi
	
	# Strategy 3: Look for known install directory in the default library
	probe_steam_library "$_steam_root" && return $true
	
	return $false
}

# Find possible source directories from Steam
# Usage: probe_steam
# Return: $true if more source files should be probed, $false otherwise.
probe_steam() {
	_steam_roots=''
	for _steam_root in "$HOME/.steam/root" "$HOME/.steam/steam" "$data_home/Steam" ; do
		[ -d "$_steam_root" ] || continue
		set_append _steam_roots "$(canonicalize "$_steam_root")"
	done
	_steam_root_call='probe_steam_root "$_steam_root" && return $true'
	[ -z "$_steam_roots" ] && return $false
	eval "for _steam_root in $_steam_roots ; do $_steam_root_call ; done"
	return $false
}

# Find a match for a case insensitive path containing arx.exe
# Usage: probe_wine_path <baseprefix> <ipath>
probe_wine_path() {
	[ -d "$1" ] || return $false
	if [ -z "$2" ] ; then
		if icontains "$1" 'arx.exe' ; then
			found_source_file "$1" "Wine"
		fi
		return $false
	fi
	_wine_path_dir="${2%%\\*}"
	if [ "$_wine_path_dir" = "$2" ]
		then _wine_path=''
		else _wine_path="${2#*\\}"
	fi
	_wine_paths="$(
		find "$1/" -mindepth 1 -maxdepth 1 -iname "$(escape "$_wine_path_dir")" -print \
			| lines_to_list
	)"
	[ -z "$_wine_paths" ] && return $false
	_wine_path_call='probe_wine_path "$_wine_path_prefix" "$_wine_path" && return $true'
	eval "for _wine_path_prefix in $_wine_paths ; do $_wine_path_call ; done"
	return $false
}

# Find possible source directories from a registry key.
# Usage: probe_wine_registry <key> <variable>
# Return: $true if more source files should be probed, $false otherwise.
probe_wine_registry() {
	
	# This is intentionally implemented without calling wine as we don't want to
	# modify the source.
	
	# Get candidate paths from the registry file
	_wine_pattern='Software\\\\(Wow6432Node\\\\)?'
	_wine_pattern="$_wine_pattern$(print "$1" | sed 's/\\/\\\\/g' | escape_pipe '()|')"
	_wine_paths="$(
		# The --after is just an arbitrary limit
		# We verify the paths by checking for arx.exe, but for effiency we still want
		# to avoid false positives.
		cat "$_wine_prefix"/*.reg \
			| grep -iPA 20 "^\\[$_wine_pattern\\]" 2> /dev/null \
			| grep -iP "^\"?$2\"?=" 2> /dev/null \
			| sed 's/^[^=]*="\([^"]*\)".*$/\1/;s/\\\(.\)/\1/g' \
			| lines_to_list
	)"
	
	# For each candidate, find a case-insensitive match and check if it contains arx.exe
	eval "set -- $_wine_paths"
	for _wine_path ; do
		probe_wine_path "$_wine_prefix/dosdevices" "$_wine_path" && return $true
	done
	
	return $false
}

# Find possible source directories from uninstall registry entries.
# Usage: probe_wine_uninstall_info <id>
# Return: $true if more source files should be probed, $false otherwise.
probe_wine_uninstall_info() {
	probe_wine_registry "Microsoft\\Windows\\CurrentVersion\\Uninstall\\$1" \
	                    'InstallLocation'
}

# Find possible source directories in a WINEPREFIX.
# Usage: probe_wineprefix <wineprefix>
# Return: $true if more source files should be probed, $false otherwise.
probe_wineprefix() {
	
	[ -d "$1" ] || return $false
	[ -e "$1/system.reg" ] || [ -e "$1/user.reg" ] || return $false
	_wine_prefix="$1"
	
	# Normal install
	probe_wine_registry 'Arkane Studios\Installed Apps\Arx Fatalis' 'Folder' && return $true
	
	# GOG version
	probe_wine_registry 'GOG.com\GOGARXFATALIS' 'PATH' && return $true
	
	# Probe uninstall entries - combined for effiency
	_wine_uninstall='Microsoft\Windows\CurrentVersion\Uninstall\'
	# Steam
	_wine_steam="Steam App $steam_appid"
	# Original game
	_wine_orig='{96443F45-13E2-11D6-AC87-00D0B7A9E540}'
	# 1.21 patch
	_wine_patch='{171251E0-4EED-4EA1-A46D-3213A226F2B3}_is1'
	probe_wine_registry "$_wine_uninstall($_wine_steam|$_wine_orig|$_wine_patch)" \
		'InstallLocation' && return $true
	
}

# Check a mounte point if it's an Arx Fatalis cd
# Usage: probe_cd <mountpoint> <fstype>
# Return: $true if more source files should be probed, $false otherwise
probe_cd() {
	node="$1"
	mountpoint="$2"
	fstype="$3"
	
	case "$fstype" in
		cd9660) ;; iso9660) ;; udf) ;; *fuseiso) ;;
		*) return $true
	esac
	
	if [ -d "$mountpoint/bin" ] && icontains "$mountpoint/bin" 'arx.ttf' 2> /dev/null ; then
		found_source_file "$mountpoint" "CDROM at $node" && return $true
	fi
	
}

# Find mounted Arx Fatalis CDs
# Usage: probe_cdrom
# Return: $true if more source files should be probed, $false otherwise
probe_cdrom() {
	
	_probe_cdrom_mountpoints="$(
		[ -f /proc/mounts ] && lines_to_list < /proc/mounts # Linux - use /proc/mounts
		mount -p 2> /dev/null | lines_to_list               # Hope that mount has a -p option
	)"
	
	eval "set -- $_probe_cdrom_mountpoints"
	for _probe_cdrom_line ; do
		eval "probe_cd $(escape "$_probe_cdrom_line" ' ' | sed 's/\\\\\040/\\ /g')"  # space-separated
		eval "probe_cd $(print "$_probe_cdrom_line" | tr '\t' '\n' | lines_to_list)" # tab-separated
	done
	
}

# Find possible source files/directories
# Usage: probe_source_files
# If sourcefile is already set, uses that.
probe_source_files() {
	
	if [ ! -z "$sourcefile" ] ; then
		
		# Trust the user
		found_source_file "$sourcefile" && return $true
		
		# But also allow to refile the dir in non-batch mode if it is a wineprefix
		[ -d "$sourcefile" ] && probe_wineprefix "$sourcefile"
		
		[ "$sourcefiles" = "$sourcefile" ] || sourcefile=''
		
		return $true
	fi
	
	status 5 "Searching for source files..."
	
	# Find by filename
	probe_files found_source_file "$gog_names" 'GOG.com setup'
	probe_files found_source_file "$demo_names" 'Demo'
	
	probe_steam && return $true
	
	# Find an Arx Fatalis installation in $WINEPREFIX
	[ ! -z "${WINEPREFIX-}" ] && probe_wineprefix "$WINEPREFIX" && return $true
	
	# Find an Arx Fatalis installation in ~/.wine
	[ ! "${WINEPREFIX-}" = "$HOME/.wine" ] && probe_wineprefix "$HOME/.wine" && return $true
	
	# Find an Arx Fatalis cdrom
	probe_cdrom && return $true
	
	# Just in case it will ever be installable under non-Windows systems
	steam_source_path="$HOME/.steam/root/SteamApps/common/Arx Fatalis"
	[ -d "$steam_source_path" ] && found_source_file "$steam_source_path" 'Steam'
	
}


##########################################################################################
# Autodetect destination dir

datadirs='' # List of destination data directory candidate

# Add a destination data directory to the list of candidates.
# Usage: found_data_dir <dir> [comment]
# Return: $true if more data directories should be probed, $false otherwise.
found_data_dir() {
	set_append datadirs "$(abspath "$1")" "$2"
	if [ $batch = 1 ] ; then return $true ; else return $false ; fi
}

# Add a destination data directory to the list of candidates if it is writable.
# In verify mode, also existing read-only directories are added.
# Usage: probe_data_dir <must-exists> <dir> [comment]
# Return: $true if more data directories should be probed, $false otherwise.
probe_data_dir() {
	[ "$1" = 1 ] && [ ! -e "$2" ] && return $false
	[ $install = 0 ] && [ ! -e "$2" ] && return $false
	if [ $install = 1 ] || [ $patch = 1 ] ; then is_writable "$2" || return $false ; fi
	found_data_dir "$2" "$3"
}

# Find possible destination data directories
# Usage: probe_data_dirs
# If datadir is already set, uses that.
probe_data_dirs() {
	
	if [ ! -z "$datadir" ] ; then
		found_data_dir "$datadir"
		return $true
	fi
	
	status 10 "Searching for destination directories..."
	
	for _probe_data_dirs_existing in 1 0 ; do
		
		# Try paths supplied by wrapper scripts
		if [ ! -z "$data_path" ] ; then
			eval "set -- $(to_list "$data_path")"
			for _probe_data_dirs_path ; do
				if [ -f "$_probe_data_dirs_path/data" ] ; then
					eval "set -- $(lines_to_list < "$_probe_data_dirs_path/data")"
					for _probe_data_dirs_line ; do
						case "$_probe_data_dirs_line" in
							/*) ;; *) _probe_data_dirs_line="$(
							 canonicalize "$_probe_data_dirs_path/$_probe_data_dirs_line"
							)"
						esac
						probe_data_dir $_probe_data_dirs_existing "$_probe_data_dirs_line" \
							"portable" && return $true
					done
				elif [ -d "$_probe_data_dirs_path/data" ] ; then
					probe_data_dir $_probe_data_dirs_existing "$_probe_data_dirs_path/data" \
						"portable" && return $true
				else
					probe_data_dir $_probe_data_dirs_existing "$_probe_data_dirs_path" "portable" \
						&& return $true
				fi
			done
		fi
		
		# Try system paths
		eval "set -- $(to_list "$data_dirs")"
		for _probe_data_dirs_prefix in "$@" ; do
			eval "set -- $(to_list "$data_dir_suffixes")"
			for _probe_data_dirs_suffix ; do
				_probe_data_dirs_force=$_probe_data_dirs_existing
				[ -e "$_probe_data_dirs_prefix/$_probe_data_dirs_suffix" ] && \
					_probe_data_dirs_force=0
				probe_data_dir $_probe_data_dirs_force \
					"$_probe_data_dirs_prefix/$_probe_data_dirs_suffix/data" "system" \
					&& return $true
				probe_data_dir $_probe_data_dirs_existing \
					"$_probe_data_dirs_prefix/$_probe_data_dirs_suffix" "system" \
					&& return $true
			done
		done
		
		# Try user paths
		if [ $is_root = 0 ] ; then
			eval "set -- $(to_list "$user_dir_suffixes")"
			for _probe_data_dirs_suffix ; do
				_probe_data_dirs_force=$_probe_data_dirs_existing
				[ -e "$data_home/$_probe_data_dirs_suffix" ] && \
					_probe_data_dirs_force=0
				probe_data_dir $_probe_data_dirs_force \
					"$data_home/$_probe_data_dirs_suffix/data" "user" \
					&& return $true
				[ $install = 0 ] && probe_data_dir $_probe_data_dirs_existing \
					"$data_home/$_probe_data_dirs_suffix" "user" \
					&& return $true
			done
		fi
		
	done
	
}


##########################################################################################
# Extract helpers and other utility abstractions

# Calculate the MD5 checksum of a file.
# Usage: checksum <result-var> <file>
checksum() {
	
	if have md5sum ; then
		_checksum_result="$(md5sum -b "$2" | sed 's/ .*//g')"
		eval "$1=\"\$_checksum_result\""
		return $true
	fi
	
	if have md5 ; then
		_checksum_result="$(md5 -q "$2")"
		eval "$1=\"\$_checksum_result\""
		return $true
	fi
	
	die "You need either md5sum or md5."
}

have_run() {
	eval "_have_run_p=\"\$${1}_reqs\""
	eval "for _have_run_program in $_have_run_p ; do have \$_have_run_program && return $true ; done"
	return $false
}

# Extract an archive file to the current directory.
# Usage: extract <file> <types>...
have_extract() {
	have_run "extract_$1"
}
extract() {
	_extract_file="$1" ; shift
	
	while true ; do
		
		_extract_missing=''
		for _extract_type ; do
			
			if "have_extract_${_extract_type}" ; then
				"extract_${_extract_type}" "$_extract_file" && return "$true"
			else
				list_merge _extract_missing extract_${_extract_type}_reqs
			fi
			
		done
		
		_extract_msg="${white}Could not extract $(basename "$_extract_file")${reset}"
		[ -z "$_extract_missing" ] \
			|| _extract_msg="$_extract_msg

Please install one or more of the following:
$(print_help_list " - $dim_pink" _extract_missing)"
		
		[ $batch = 1 ] && die "Error: $_extract_msg"
		
		dialog_retry "$_extract_msg"
		case $dialog_retry_choice in
			abort)  die "Error extracting files" ;;
			retry)  continue ;;
			ignore) return $true ;;
		esac
		
	done
	
}

# Extract a .zip file to the current directory.
# Usage: extract_zip <zipfile>
have_extract_zip() { have_extract zip ; }
extract_zip() {
	
	if have bsdtar ; then
		printf 'Extracting %s using bsdtar\n' "$1"
		bsdtar xvf "$1"
		return $?
	fi
	
	if have unzip ; then
		puts 'unzip: '
		unzip "$1"
		return $?
	fi
	
	for _extract_zip_sz in 7za 7z ; do
		if have $_extract_zip_sz ; then
			$_extract_zip_sz x "$1"
			return $?
		fi
	done
	
	die "no program to extract $1"
}

# Mount a .iso file or CDROM using fuseiso if available  or normal mount if root.
# Usage: mount_cdrom <cdromfile> <mountpoint>
have_mount_cdrom() {
	have fuseiso && have fusermount && return $true
	have mount && have umount && [ $is_root = 1 ] && return $true
	return $false
}
mount_cdrom() {
	
	if have fuseiso && have fusermount &&  fuseiso "$1" "$2" ; then
		printf 'Mounted %s at %s using fuseiso\n' "$1" "$2"
		return $true
	fi
	
	if have mount && have umount && [ $is_root = 1 ] && mount -o loop,ro "$1" "$2" ; then
		printf 'Mounted %s at %s\n' "$1" "$2"
		return $true
	fi
	
	die "no program to extract $1"
}

# Unmount a CDROM that was mounted using mount_cdrom.
# Usage: unmount_cdrom <mountpoint>
unmount_cdrom() {
	have fusermount && fusermount -u "$1" > /dev/null 2>&1
	have umount && [ $is_root = 1 ] && umount "$1" > /dev/null 2>&1
}

# isoinfo wrapper to extract all files to the current directory
extract_isoinfo() {
	_extract_isoinfo_file="$1"
	
	# Get a list of all files in the ISO image
	_extract_isoinfo_files="$(
		isoinfo -i "$_extract_isoinfo_file" -J -f | grep ';1$' | lines_to_list
	)"
	[ -z "$_extract_isoinfo_files" ] && return $false
	
	eval "set -- $_extract_isoinfo_files"
	for _extract_isoinfo_e ; do
		
		# Remove leading / and trailing ;1 from filenames
		_extract_isoinfo_f="$(print "$_extract_isoinfo_e" | sed 's:^/*::;s:;1$::')"
		[ -z "$_extract_isoinfo_f" ] && continue
		printf ' - %s\n' "$_extract_isoinfo_f"
		
		# Create subdirectories as needed
		_extract_isoinfo_d="$(dirname "$_extract_isoinfo_f")"
		[ -z "$_extract_isoinfo_d" ] || mkdir -p "$_extract_isoinfo_d" || return $false
		
		# Extract the file
		isoinfo -i "$_extract_isoinfo_file" -J -x "$_extract_isoinfo_e" \
			> "$_extract_isoinfo_f" || return $false
		
		# Don't rely on isoinfo setting a non-zero return code, check that we got something
		[ -s "$_extract_isoinfo_f" ] || return $false
		
	done
	
	return $true
}

# Extract a .iso file or CDROM to the current directory.
# Usage: extract_iso <cdromfile>
have_extract_iso() { have_extract iso ; }
extract_iso() {
	
	if have isoinfo ; then
		printf 'Extracting %s using isoinfo\n' "$1"
		extract_isoinfo "$1"
		return $?
	fi
	
	ret=$false
	
	if have bsdtar ; then
		printf 'Extracting %s using bsdtar\n' "$1"
		bsdtar xvf "$1"
		ret="$?"
		
		# Older versions of bsdtar don't always get the names right - fix them
		_extrac_cdrom_f="$(find "$PWD" -depth -iname '*;1' | lines_to_list)"
		if [ ! -z "$_extrac_cdrom_f" ] ; then
			eval "for _extract_iso_f in $_extrac_cdrom_f ; do mv -f \"\$_extract_iso_f\" \"\$(print \"\$_extract_iso_f\" | sed 's:;1$::')\" ; done"
		fi
		
	elif have 7z ; then
		7z x -tiso "$1"
		ret="$?"
	fi
	
	# For some iso files bsdtar just does nothing - at least let the user know
	if [ ! -d "$PWD/bin" ] || ! icontains "$PWD/bin" 'arx.ttf' ; then
		_extract_iso_err="${yellow}It looks like bsdtar/p7zip didn't do what it was supposed to - this will likely fail!${reset}

You might have better luck with ${dim_pink}isoinfo${reset} or ${dim_pink}fuseiso${reset}, or by manually mounting the CD/ISO."
		printf '%s\n' "$_extract_iso_err"
		[ $batch = 0 ] && dialog_message "$_extract_iso_err"
	fi
	
	return $ret
}

# Extract a microsoft .cab or .exe file to the current directory.
# Usage: extract_ms_cab <cabfile>
extract_cab_check_bsdtar() {
	if have bsdtar ; then
		case "$(bsdtar --version)" in
			# These versions have bugs that cause corrupted files
			'') ;;
			*'libarchive 1.'*) ;;
			*'libarchive 2.'*) ;;
			*'libarchive 3.0') ;;
			*'libarchive 3.0.'*) ;;
			# Newer versions should work fine
			*)
			return $true
		esac
	fi
	return $false
}
have_extract_ms_cab() {
	extract_cab_check_bsdtar && return $true
	have cabextract || have 7za || have 7z
}
extract_ms_cab() {
	
	if extract_cab_check_bsdtar ; then
		printf 'Extracting %s using bsdtar\n' "$1"
		bsdtar xvf "$1"
		return $?
	fi
	
	if have cabextract ; then
		puts 'cabextract: '
		cabextract "$1"
		return $?
	fi
	
	for _extract_ms_cab_sz in 7za 7z ; do
		if have $_extract_ms_cab_sz ; then
			$_extract_ms_cab_sz x "$1"
			return $?
		fi
	done
	
	die "no program to extract $1"
}

# Extract an InstallShield .cab or .exe file to the current directory.
# Usage: extract_installshield <cabfile>
have_extract_installshield() { have_extract installshield ; }
extract_installshield() {
	
	if have unshield ; then
		puts 'unshield: '
		unshield x "$1"
		return $?
	fi
	
	die "no program to extract $1"
}

# Extract a RAR .rar or .exe file to the current directory.
# Usage: extract_rar <rarfile>
have_extract_rar() { have_extract rar ; }
extract_rar() {
	
	if have unrar ; then
		puts 'unrar: '
		unrar x -y -o "$1"
		return $?
	fi
	
	die "no program to extract $1"
}

# Extract an ACE .ace or .exe file to the current directory.
# Usage: extract_ace <acefile>
have_extract_ace() { have_extract ace ; }
extract_ace() {
	
	if have unace ; then
		puts 'unace '
		unace x -y -o "$1"
		return $?
	fi
	
	die "no program to extract $1"
}

# Extract an Inno Setup .exe file to the current directory.
# Usage: extract_innosetup <exefile>
have_extract_innosetup() { have innoextract ; }
innosetup_language=''
extract_innosetup() {
	
	if have innoextract ; then
		puts 'innoextract: '
		if [ -z "$innosetup_language" ]
			then innoextract --color=off "$1" ; return $?
			else innoextract --color=off --language="$innosetup_language" "$1" ; return $?
		fi
	fi
	
	die "no program to extract $1"
}

# Extract all .cab files in a directory.
# Usage: extract_cab_files <sourcedir> <destdir>
extract_cab_files() {
	
	_extract_cab_files_i=0
	_extract_cab_files_files="$(
		find "$1/" -mindepth 1 -type f -iname '*.cab' -print \
		| lines_to_list
	)"
	eval "set -- $_extract_cab_files_files"
	for _extract_cab_files_cabfile ; do
		[ -z "$_extract_cab_files_cabfile" ] && continue
		
		while true ; do
			_extract_cab_files_cabdir="$sourcedir/cab.$_extract_cab_files_i"
			if [ -e "$_extract_cab_files_cabdir" ] ; then
				_extract_cab_files_i=$((_extract_cab_files_i + 1))
				continue
			fi
			create_dir "$_extract_cab_files_cabdir" "cab #$i work"
			break
		done
		
		cd "$_extract_cab_files_cabdir"
		case "$(basename "$_extract_cab_files_cabfile")" in
			data*) extract "$_extract_cab_files_cabfile" installshield ms_cab ;;
			*)     extract "$_extract_cab_files_cabfile" ms_cab installshield ;;
		esac
		
		_extract_cab_files_i=$((_extract_cab_files_i + 1))
	done
	
}

# Download a file.
# Usage: download_file <url> <destination>
have_download() { have_run download ; }
download_impl() {
	
	if have wget ; then
		wget -O "$2" "$1"
		return $?
	fi
	
	if have curl ; then 
		curl --location --fail -o "$2" "$1"
		return $?
	fi
	
	if have fetch ; then
		fetch -o "$2" "$1"
		return $?
	fi
	
	die "no program to download $1"
}
download_file() {
	
	download_impl "$1" "$2" || return $false
	
	# Check that we got something useful
	case "$(file --dereference --brief --mime-type "$2" 2> /dev/null)" in
		*/html*) ;;
		*/xml*) ;;
		*) return $true
	esac
	
	rm -f "$2"
	return $false;
}

# Download a file from a list of mirrors.
# Usage: download <callback> <name> <names> <urls> <destdir>
download() {
	_download_callback="$1"
	_download_name="$2"
	_download_names="$3"
	_download_urls="$4"
	_download_dest="$5"
	
	while true ; do
		
		probe_files "$_download_callback" "$_download_names" && return $true
		
		_download_missing=''
		if have_download ; then
			eval "for _download_url in $_download_urls ; do download_file \"\$_download_url\" \"\$_download_dest\" && \$_download_callback \"\$_download_dest\" && return $true ; done"
		else
			list_merge _download_missing download_reqs
		fi
		
		_download_msg="${white}Could not download ${_download_name}${reset}"
		[ -z "$_download_missing" ] \
			|| _download_msg="$_download_msg

Please install one of the following:
$(print_help_list " - $dim_pink" _download_missing)"
		
		_download_msg="$_download_msg

You can download the file manually from
$(print_help_list " - $dim_blue" _download_urls)

and put it in one of these locations:
$(print_help_list " - $dim_white" probe_file_dirs)"
		
		[ $batch = 1 ] && die "Error: $_download_msg"
		
		dialog_retry "$_download_msg"
		case $dialog_retry_choice in
			abort)  die "Error downloading files" ;;
			retry)  continue ;;
			ignore) return $true ;;
		esac
		
	done
	
}


##########################################################################################
# Unpack source

workdir=''
cleanup_workdir() {
	[ ! -z "$workdir" ] && [ -e "$workdir" ] && rm -rf "$workdir"
}
# Create the work directory if it doesn't exist.
# Also regiter a cleanup function to remove the work directory on exit.
# Usage: create_workdir
create_workdir() {
	[ -z "$workdir" ] || return $true
	selected_stuff=1
	workdir="$datadir/$command-temp"
	cleanup_workdir
	on_exit cleanup_workdir
	create_dir "$workdir" 'work'
}

# Extract setup*.cab files from a source directory into $sourcedir/cab.*.
# Usage: extract_source_dir <sourcedir>
extract_source_dir() {
	extract_cab_files "$1" "$sourcedir"
}

# Extract a source executable into $sourcedir/exe.
# Usage: extract_source_exe <sourcefile>
extract_source_exe() {
	
	sourcedir_exe="$sourcedir/exe"
	create_dir "$sourcedir_exe" 'exe work'
	
	cd "$sourcedir_exe" || die
	case "$(basename "$1")" in
		arx_demo_english.exe)
			extract "$1" rar ace ms_cab installshield innosetup ;; # English demo
		arx_demo_german.exe)
			extract "$1" ace rar ms_cab installshield innosetup ;; # German demo
		arx_jpn_*.exe)
			extract "$1" ms_cab installshield innosetup rar ace ;; # Japanese demo
		*)
			extract "$1" innosetup ms_cab installshield rar ace ;; # GOG.com setup
	esac
	
	extract_source_dir "$sourcedir_exe"
	
}

# Wrap mount_cdrom et al so we can use them with extract()
extract_mount_cdrom_reqs=''
list_merge extract_mount_cdrom_reqs mount_cdrom_reqs
have_extract_mount_cdrom() { have_mount_cdrom ; }
extract_mount_cdrom() {
	# Ignore the current working directory, always mount to $cdromdir
	if mount_cdrom "$1" "$cdromdir" ; then
		# Pretent the mountpoint is the original source
		sourcefile="$cdromdir"
		sourcedir_cdrom="$cdromdir"
		return $true
	else
		return $false
	fi
}

cdromdir=''
cleanup_cdrom() {
	[ ! -z "$cdromdir" ] && [ -e "$cdromdir" ] && unmount_cdrom "$cdromdir"
}
# Mount a source CDROM/ISO and adjust $sourcefile or extract it into $sourcedir/cdrom.
# Usage: extract_source_cdrom <sourcefile>
extract_source_cdrom() {
	
	# Try to mount the cdrom to avoid unneeded copies
	# Keep the mount point out of $sourcedir so we don't try to mv files from it
	cdromdir="$workdir/cdrom"
	cleanup_cdrom
	on_exit cleanup_cdrom
	create_dir "$cdromdir" 'mount work'
	
	# Otherwise, extract the files from the CDROM if we have the required tools
	sourcedir_cdrom="$sourcedir/cdrom"
	create_dir "$sourcedir_cdrom" 'cdrom work'
	cd "$sourcedir_cdrom" || die
	
	extract "$1" mount_cdrom iso
	
	# Extract any cab files on the CDROM
	extract_source_dir "$sourcedir_cdrom"
}

# Extract a source executable into $sourcedir/zip.
# Also extracts contained setup*.cab files into $sourcedir/cab.*.
# Usage: extract_source_zip <sourcefile>
extract_source_zip() {
	
	sourcedir_zip="$sourcedir/zip"
	create_dir "$sourcedir_zip" 'zip work'
	
	cd "$sourcedir_zip" || die
	extract "$1" zip
	
	extract_source_dir "$sourcedir_zip"
}

sourcedir=''
# Extract the source file or directory if it hansn't been extracted already.
# Usage: extract_source
extract_source() {
	[ -z "$sourcedir" ] || return $true
	
	status --temp "${white}Extracting source...${reset}"
	
	create_workdir
	sourcedir="$workdir/source"
	create_dir "$sourcedir" 'source work'
	
	if [ -d "$sourcefile" ] ; then
		extract_source_dir "$sourcefile"
	elif [ -f "$sourcefile" ] ; then
		case "$sourcefile" in
			*.zip) extract_source_zip "$sourcefile" ;;
			*.exe) extract_source_exe "$sourcefile" ;;
			*.iso) extract_source_cdrom "$sourcefile" ;;
			*)
			case "$(file --dereference --brief --mime-type "$sourcefile" 2> /dev/null)" in
				application/zip)             extract_source_zip "$sourcefile" ;;
				application/x-dosexec)       extract_source_exe "$sourcefile" ;;
				application/x-iso9660-image) extract_source_cdrom "$sourcefile" ;;
				*) die "Unknown source file type: $sourcefile"
			esac
		esac
	else
		extract_source_cdrom "$sourcefile"
	fi
	
	print
}


##########################################################################################
# Detect data language

find_file_impl() {
	_patchable="$1"
	
	# Search for both file.ext and file_default.ext
	_file="$(escape "$(basename "$2")")"
	_file_d="$(print "$_file" | sed 's/^\(.*\)\(\.[^.]*\)$/\1_default\2/')"
	
	# Prefer files from the patch - if availbale, they are most likely the correct ones
	set -- -iname "$_file" -print -o -iname "$_file_d" -print
	[ "$_patchable" = 1 ] && [ ! -z "$patchdir" ]  && find "$patchdir" "$@"
	
	# Find the file in the source
	[ ! -z "$sourcedir" ] && find "$sourcedir" "$@"
	[ -d "$sourcefile" ]  && find "$sourcefile" "$@"
	
	# Also find the file if it is already in the data directory, but don't ignore case
	find "$datadir" -path '*-temp' -prune -o \
		-name "$_file" -print -o -name "$_file_d" -print
	
}
# Find a file in patch, source and data directories.
# Usage: find_file <is-patchable> <return-list-var> <filename/path>
find_file() {
	eval "$2=\"\$(find_file_impl \"\$1\" \"\$3\" | lines_to_list)\""
}

# Copy/move a file to the data diectory.
# Usage: use_file <file> <data-path>
# Action taken depends on the source directory.
use_file() {
	_in="$1"
	_out="$datadir/$2"
	
	# Don't change anything on verify-only mode
	[ $install = 0 ] && [ $patch = 0 ] && return $true
	
	# Don't try to copy/move a file onto itself
	[ "$_in" = "$_out" ] && return $true
	
	# Create directories as needed
	_outdir="$(dirname "$_out")"
	create_dir "$_outdir" 'output'
	
	# Copy or move the file
	if [ "${_in#"$sourcefile"}" = "$_in" ]
		then mv -f "$_in" "$_out" || die "Could not move $_in to $_out!"
		else cp -f "$_in" "$_out" || die "Could not copy $_in to $_out!"
	fi
	installed_stuff=1
	
	# Fix permissions
	chmod --reference="$_outdir" "$_out" > /dev/null 2>&1
	chmod -x "$_out" > /dev/null 2>&1
}

data_lang=''      # Data language/tipe ID
data_lang_desc='' # Friendly data language/type label

# Detect the data language and type
# Usage: detect_data_langauge <callback>
# <callback> receives a speech.pak checksum and sets data_lang and data_lang_desc
detect_data_langauge() {
	callback="$1"
	
	if [ $install = 1 ]
		then _detect_data_langauge_status=40
		else _detect_data_langauge_status=10
	fi
	[ $gui = 0 ] || status $_detect_data_langauge_status 'Detecting data language...'
	puts "${white}Detecting data language..."
	
	_speech_checksums=''
	
	find_file 0 _speech_files 'speech.pak'
	eval "set -- $_speech_files"
	for _speech_file ; do
		
		checksum _speech_checksum "$_speech_file"
		
		data_lang=''
		"$callback" "$_speech_checksum"
		
		if [ -z "$data_lang" ] ; then
			list_append _speech_checksums "$_speech_checksum"
		else
			use_file "$_speech_file" 'speech.pak'
			break
		fi
		
	done
	
	if [ -z "$data_lang" ] ; then
		printf '\n'
		case "$_speech_checksums" in
			'') die "speech*.pak not found" ;;
			*)  die "Unsupported data language - speech*.pak checksum: $_speech_checksums" ;;
		esac
	fi
	
	printf " ${green}%s${reset}\n\n" "${data_lang_desc}"
	
}


##########################################################################################
# Get the patch file

# Warn if the user-supplied patch file has a suspicious name.
# Usage: patch_check_file_name <file> <expected-name-list>
patch_check_file_name() {
	_user_patch_name="$(basename "$1")"
	_patch_check_file_names="$2"
	if ! list_contains _patch_check_file_names "$_user_patch_name" ; then
		printf "${yellow}Warning: unexpected patch file name: %s\n" "$_user_patch_name" >&2
		printf "Expected %s${reset}\n" "$(print_help_or _patch_check_file_names)" >&2
	fi
}

# Find or download a patch file.
# Usage: probe_patch_file_impl <callback> <name> <user-supplied-file> \
#                              <expected-name-list> <url-list> \
# <callback> will be called with the patch file
probe_patch_file_impl() {
	
	_patch_found="$1"
	_patch_name="$2"
	_patch_file="$3"
	_patch_names="$4"
	_patch_urls="$5"
	
	if [ ! -z "$_patch_file" ] ; then
		
		# Check the filename of user-supplied patchse
		patch_check_file_name "$_patch_file" "$_patch_names"
		
		"$_patch_found" "$_patch_file"
		return $true
	fi
	
	[ $probe_patch = 0 ] && return $true
	
	# Probe local files now so we don't lie abut downloading it
	probe_files "$_patch_found" "$_patch_names" && return $true
	
	status --temp "${white}Downloading patch ${blue}${_patch_name}${reset}..."
	
	create_workdir
	download "$_patch_found" "$_patch_name" \
	         "$_patch_names" "$_patch_urls" "${workdir}/${_patch_name}" \
		&& return $true
	
}

# Callback for the japanese patch.
patch_jp_found() {
	patchfile_jp="$(abspath "$1")"
	printf "Using Japanese %s patch: ${blue}%s${reset}\n" \
		"$patch_jp_ver" "$patchfile_jp"
	return $true
}

# Callback for the main patch.
patch_found() {
	patchfile="$(abspath "$1")"
	printf "Using %s patch: ${blue}%s${reset}\n" "$patch_ver" "$patchfile"
	return $true
}

# Find the patch file(s) for a specific language.
# Usage: probe_patch_file <language>
# If patchfile is already set, uses that.
probe_patch_file() {
	
	_patch_file_lang=''
	case "$1" in
		'german')   _patch_file_lang='GE' ;;
		'english')  _patch_file_lang='EN' ;;
		'spanish')  _patch_file_lang='ES' ;;
		'french')   _patch_file_lang='FR' ;;
		'italian')  _patch_file_lang='IT' ;;
		'russian')  _patch_file_lang='RU' ;;
		'japanese')
			_patch_jp_names=''
			list_append _patch_jp_names "$patch_jp_name"
			probe_patch_file_impl patch_jp_found "$patch_jp_name" "$patchfile_jp" \
			                      "$_patch_jp_names" "$patch_jp_urls"
		;;
	esac
	_patch_names=''
	list_append _patch_names "$patch_name"
	if [ ! -z "$_patch_file_lang" ] ; then
		list_append _patch_names "$(printf "$patch_name_localized" "$_patch_file_lang")"
	fi
	
	probe_patch_file_impl patch_found "$patch_name" "$patchfile" \
	                      "$_patch_names" "$patch_urls"
	
}

patchdir='' # Directory where the patch file(s) are extracted

# Extract all patch files for a specific language.
# Usage: extract_patch <language>
# Does nothing if the patch files are already extracted.
extract_patch() {
	[ -z "$patchdir" ] || return $true
	_extract_patch_lang="$1"
	
	print
	
	# Search for and download the patch files if needed
	probe_patch_file "$_extract_patch_lang"
	
	status --temp "${white}Extracting patch...${reset}"
	
	create_workdir
	patchdir="$workdir/patch"
	create_dir "$patchdir" 'patch work'
	
	# Extract the main patch file
	if [ ! -z "$patchfile" ] ; then
		_patchdir_main="$patchdir/main"
		create_dir "$_patchdir_main" 'main patch work'
		cd "$_patchdir_main"
		innosetup_language="$_extract_patch_lang"
		extract "$patchfile" innosetup
		innosetup_language=''
	fi
	
	if [ ! -z "$patchfile_jp" ] ; then
		
		# Extract the Japanese patch file
		_patchdir_jp="$patchdir/main"
		create_dir "$_patchdir_jp" 'jp patch work'
		cd "$_patchdir_jp"
		extract "$patchfile_jp" ms_cab
		
		# Also extract contained files
		extract_cab_files "$_patchdir_jp" "$patchdir"
		
	fi
	
	print
	status --temp
}


##########################################################################################
# Copy and verify files

checksum_failed=0 # Was there any mismatched checksum or missing file so far?

# Handle a required file: find, compare checksum and copy/move if needed.
# Usage: required_file <is-patchable> <filepath> <checksums>
required_file() {
	
	_patchable="$1"
	_name="$2"
	_valid="$3"
	
	find_file 1 _files "$_name"
	eval "set -- $_files"
	_checksums=''
	for _file ; do
		checksum _checksum "$_file"
		
		if list_contains _valid "$_checksum" ; then
			# We found a match - use it
			printf ' - %s\n' "$_name"
			use_file "$_file" "$_name"
			return $true
		fi
		
		# Remember mismatched checksums so we can output debug info if none matched
		list_append _checksums "$_checksum"
		continue
		
	done
	
	# No matching file found!
	
	# If we didn't use the patch yet, fetch it and try again!
	if [ $patch = 1 ] && [ $_patchable = 1 ] && [ -z "$patchdir" ] ; then
		extract_patch "$data_lang"
		if [ ! -z "$patchdir" ] ; then
			required_file "$_patchable" "$_name" "$_valid"
			return $?
		fi
	fi
	
	# Let the user know that something is wrong!
	if [ -z "$_checksums" ] ; then
		printf "${red}Missing ${dim_red}%s${red}!${reset}\n" "$_name" >&2
	else
		printf "${red}Checksum failed for ${dim_red}%s${reset}:\n" "$_name" >&2
		printf "  expected: ${dim_red}%s${reset}\n" "$(print_help_or _valid)" >&2
		printf "  actual:   ${dim_red}%s${reset}\n" "$(print_help_or _checksums)" >&2
	fi
	
	# Be optimistic, copy the first result even if the checksum doesn't match!
	# We will display an error at the end (end exit with $false), but it may still work.
	eval "set -- $_files"
	for _file ; do
		use_file "$_file" "$_name"
		break
	done
	
	checksum_failed=1
	return $false
}

# Handle an optional file: find and copy/move if it exists.
# Usage: optional_file <filepath>
optional_file() {
	_name="$1"
	find_file 1 _files "$_name"
	eval "set -- $_files"
	for _file ; do
		# There is no checksum, just copy the first file
		printf ' - %s\n' "$_name"
		use_file "$_file" "$_name"
		break
	done
}


##########################################################################################
# Setup

# Select source file / directory
if [ $install = 1 ] ; then
	probe_source_files
	if [ $batch = 0 ] ; then
		list_append sourcefiles 'Patch existing install' ''
		list_append sourcefiles 'Verify existing install only' ''
	fi
	user_select_entry --existing --any sourcefiles sourcefile \
		"source file or directory to install from" "$green" "$dim_green" 'install from'
	case "$sourcefile" in
		'Patch existing install')       install=0 ; patch=1 ;;
		'Verify existing install only') install=0 ; patch=0 ;;
		*) [ -e "$sourcefile" ] || die "Missing source file: $sourcefile"
	esac
	set_append probe_file_dirs "$(dirname "$sourcefile")"
fi

# Select destination data directory
probe_data_dirs
if [ $install = 1 ] ; then
	verb='install to' ; access=--writable
elif [ $patch = 1 ] ; then
	verb='patch'      ; access=--existing
else
	verb='verify'     ; access=--existing
fi
user_select_entry $access --dir datadirs datadir \
	"data directory to $verb" "$cyan" "$dim_cyan" "$verb"
[ -z "$datadir" ] && die "Missing data dir."
if [ $install = 1 ] ; then
	create_dir "$datadir" 'data'
else
	[ -d "$datadir" ] || die "Missing data dir: $datadir"
fi

# Extract source files
if [ $install = 1 ] ; then
	printf "\nInstalling Arx Fatalis data files \nfrom %s\nto   %s\n\n" \
		"${green}$sourcefile${reset}" "${cyan}$datadir${reset}"
	extract_source
else
	printf "\nVerifying Arx Fatalis data files \nin %s\n\n" "${cyan}$datadir${reset}"
fi


##########################################################################################
# Required files

# Detect language
determine_language() {
	speech_checksum="$1" # speech.pak
	
	case "$speech_checksum" in
		'4e8f962d8204bcfd79ce6f3226d6d6de') data_lang='english'       ;;
		'4c3fdb1f702700255924afde49081b6e') data_lang='german'        ;;
		'ab8a93161688d793a7c78fbefd7d133e') data_lang='german'        ;;
		'2f88c67ae1537919e69386d27583125b') data_lang='spanish'       ;;
		'4edf9f8c799190590b4cd52cfa5f91b1') data_lang='french'        ;;
		'81f05dea47c52d43f01c9b44dd8fe962') data_lang='italian'       ;;
		'677163bc319cd1e9aa1b53b5fb3e9402') data_lang='russian'       ;;
		'235b86700fc80b3eb86731d748013a38') data_lang='japanese'      ;;
		'62ca7b1751c0615ee131a94f0856b389') data_lang='english-demo'  ;;
		'09038e43508232c44537c162f9e3ecde') data_lang='french-demo'   ;;
		'a424fcfc46dd4f11b04030efac15a668') data_lang='german-demo'   ;;
		'eeacbd9a845ecc00054934e82e9d7dd3') data_lang='japanese-demo' ;;
	esac
	
	case "$data_lang" in
		'english')       data_lang_desc='English' ;;
		'german')        data_lang_desc='German' ;;
		'spanish')       data_lang_desc='Spanish' ;;
		'french')        data_lang_desc='French' ;;
		'italian')       data_lang_desc='Italian' ;;
		'russian')       data_lang_desc='Russian' ;;
		'japanese')      data_lang_desc='Japanese' ;;
		'english-demo')  data_lang_desc='English (demo)' ;;
		'french-demo')   data_lang_desc='French (demo)' ;;
		'german-demo')   data_lang_desc='German (demo)' ;;
		'japanese-demo') data_lang_desc='Japanese (demo)' ;;
	esac
	
}
detect_data_langauge determine_language

if [ $install = 1 ] ; then
	progress=50
	status $progress "${white}Copying and verifying files...${reset}"
	case "$data_lang" in *-demo) increment=8 ;; *) increment=1 ;; esac
else
	progress=15
	status $progress "${white}Verifying files...${reset}"
	case "$data_lang" in *-demo) increment=14 ;; *) increment=2 ;; esac
fi
print " - speech.pak"

# Usage: f <is-patchable> <file> <checksums>...
f() {
	
	# Update progress bar
	progress=$((progress + increment))
	status $progress
	
	# Verify & copy file
	required_file "$@"
}

# speech.pak - already copied in detect_data_langauge

# loc.pak contains the localized text, so it's different for each language!
case "$data_lang" in
	german)        loc_checksum='31bc35bca48e430e108db1b8bcc2621d' ;;
	english)       loc_checksum='a47b192493afb5794e2161a62d35b69f' ;;
	spanish)       loc_checksum='121f99608814a2c9c5857cfadb665553' ;;
	french)        loc_checksum='f8fc448fea12469ed94f417c313fe5ea' ;;
	italian)       loc_checksum='a9e162f2916f5737a95bd8c5bd8a979e' ;;
	russian)       loc_checksum='a131bf2398ee70a9c22a2bbffd9d0d99' ;;
	japanese)      loc_checksum='9dcb0f5d7a517be4f1d9190419900892' ;;
	english-demo)  loc_checksum='2ae16d3925c597dca70f960f175def3a' ;;
	french-demo)   loc_checksum='4a8ac68341d4758a32d9cd04955b115e' ;;
	german-demo)   loc_checksum='87accec0658aa109a3efa8b41aab61df' ;;
	japanese-demo) loc_checksum='9d84cede805b13fdf7fce856ecc15b19' ;;
	*)             loc_checksum=''
esac
if [ ! -z "$loc_checksum" ] ; then
	f 1 'loc.pak' "$loc_checksum"
fi

# misc/arx.ttf is the same for everything except japanese
# there are also separate misc/arx_russian.ttf and misc/arx_taiwanese.ttf handled later
case "$data_lang" in
	japanese*)    font_checksum='58eab00842d8adea8d553ae1f66b0c9b' ;;
	*)            font_checksum='9a95ff96795c034524ba1c2e94ea12c7' ;;
esac
if [ ! -z "$font_checksum" ] ; then
	f 1 'misc/arx.ttf' "$font_checksum"
fi

case "$data_lang" in
	
	english-demo)
	f 0 'data2.pak'                                        958b78f8f370b06d769843137138c461
	f 0 'data.pak'                                         5d7ba6e6c79ebf7fbb232eaced9e8ad9
	f 0 'misc/logo.bmp'                                    aa3dfbd4bc9c863d10a0c5345ae5a4c9
	f 0 'sfx.pak'                                          ea1b3e6d6f4906905d4a34f07e9a59ac
	;;
	
	french-demo)
	f 0 'data2.pak'                                        8dc1d1b3e85d4a41ae320aa3fa9c649a
	f 0 'data.pak'                                         5d7ba6e6c79ebf7fbb232eaced9e8ad9
	f 0 'misc/logo.bmp'                                    aa3dfbd4bc9c863d10a0c5345ae5a4c9
	f 0 'sfx.pak'                                          ea1b3e6d6f4906905d4a34f07e9a59ac
	;;
	
	german-demo)
	f 0 'data2.pak'                                        143ba491a357263a2dfad9936a66eeb6
	f 0 'data.pak'                                         5d7ba6e6c79ebf7fbb232eaced9e8ad9
	f 0 'misc/logo.bmp'                                    aa3dfbd4bc9c863d10a0c5345ae5a4c9
	f 0 'sfx.pak'                                          ea1b3e6d6f4906905d4a34f07e9a59ac
	;;
	
	japanese-demo)
	f 0 'data2.pak'                                        958b78f8f370b06d769843137138c461
	f 0 'data.pak'                                         903dfe1878a0cedff3b941fd3aa22ba9
	f 0 'misc/logo.bmp'                                    aa3dfbd4bc9c863d10a0c5345ae5a4c9
	f 0 'sfx.pak'                                          ea1b3e6d6f4906905d4a34f07e9a59ac
	;;
	
	*) # full game
	
	f 1 'graph/interface/misc/arkane.bmp'                  afff1099c01ffeb03b9a351f7b5966b6
	f 1 'graph/interface/misc/quit1.bmp'                   41445d3792a1f8818d950aca47254488
	f 1 'graph/obj3d/textures/fixinter_barrel.jpg'         8419274acbff7346c3661b18d6aad6dc
	f 1 'graph/obj3d/textures/fixinter_bell.bmp'           5743b9047c9ad65540c318dfcc98123a
	f 1 'graph/obj3d/textures/fixinter_metal_door.jpg'     f246eff6b19c9c710313b4a4dce96a69
	f 1 'graph/obj3d/textures/fixinter_public_notice.bmp'  f81394abbb9006ce0950843b7909db33
	f 1 'graph/obj3d/textures/item_bread.bmp'              544448f8eedc912aa231a6a04fffb7c5
	f 1 'graph/obj3d/textures/item_club.jpg'               7e26c4199ddaca494c8b369294306b0b
	f 1 'graph/obj3d/textures/item_long_sword.jpg'         3a6196fe9b7666c7d80d82be06f6de86
	f 1 'graph/obj3d/textures/item_mauld_sabre.jpg'        18492c25ebac02f83e2f0ebda61ecb00
	f 1 'graph/obj3d/textures/item_mauldsword.jpg'         503a5c2f23668040c675aefdde6dbbe5
	f 1 'graph/obj3d/textures/item_mirror.jpg'             c0a22b4f7a7a6461da68206e94928637
	f 1 'graph/obj3d/textures/item_ring_casting.bmp'       348f9add709bacee08556d1f8cf10f3f
	f 1 'graph/obj3d/textures/item_rope.bmp'               ff05de281c8b380ee98f6e123d3d51cb
	f 1 'graph/obj3d/textures/item_spell_sheet.jpg'        024ccbb520020f92fba5a5a4f0270cea
	f 1 'graph/obj3d/textures/item_torch2.jpg'             027951899b4829599ca611010ea3484f
	f 1 'graph/obj3d/textures/item_torch.jpg'              9ada166f23ddcb775ac20836e752187e
	f 1 'graph/obj3d/textures/item_zohark.bmp'             cd206a4027f86c6e57b7710c94049efa
	f 1 'graph/obj3d/textures/l7_dwarf_[wood]_board08.jpg' 79ccc81adb7c37b98f40b478ef1fccd4
	f 1 'graph/obj3d/textures/l7_dwarf_[wood]_board80.jpg' 691611087b13d38ef02bb9dfd6a2518e
	f 1 'graph/obj3d/textures/npc_dog.bmp'                 116bd374c14ae8c387a4da1899e1dca7
	f 1 'graph/obj3d/textures/npc_pig.bmp'                 b7a4d0d3d230b2d1470176909004e38b
	f 1 'graph/obj3d/textures/npc_pig_dirty.bmp'           76034d8d74056c8a982479d36321c228
	f 1 'graph/obj3d/textures/npc_rat_base.bmp'            00c585ec9ebe8006d7ca72993de7b51b
	f 1 'graph/obj3d/textures/npc_rat_base_cm.bmp'         cae38facbf77db742180b9e58d0eb42f
	f 1 'graph/obj3d/textures/npc_worm_body_part1.jpg'     0b220bffaedc89fa663f08d12630c342
	f 1 'graph/obj3d/textures/npc_worm_body_part2.bmp'     20797cb78f6393a0fb5405969ba9f805
	f 1 'graph/obj3d/textures/[wood]_light_door.jpg'       00d0b018e995e7d013d6e52e92126901
	f 1 'misc/arx_russian.ttf'                             921561e83786efcd25f92147b60a13db
	f 1 'misc/arx_taiwanese.ttf'                           da59198061cef0761c6b2fca113f76f6
	f 1 'misc/logo.avi'                                    63ed31a4eb3d226c23e58cfaa974d484
	f 1 'misc/logo.bmp'                                    afff1099c01ffeb03b9a351f7b5966b6
	f 1 'data2.pak'                                        f7e0ce700bf963429ac535ca86f8a7b4
	
	f 0 'sfx.pak'                                          2efc9a74c517fd1ee9919900cf4091d2
	
	# data.pak is censored in some versions (presumably has less gore)
	# At least the original german and italian CDs have the censored version.
	# The censored version has different level files and a different
	# human_female_villager model.
	# There are also minor differences in the scripts, but those are
	# overwritten by data2.pak from the 1.21 patch.
	# A third data.pak variant can be found on the original French Arx Fatalis CD:
	# It is almost identical to the preceding censored version, but has different level
	# files for level 1 (.llf only) and 3 (.llf and .fts).
	data_checksum_original='a91a0b39a046233debbb10b4850e13eb'
	data_checksum_censored='a88d239dc7919ab113ff45483cb4ad46'
	data_checksum_frenchcd='7ae3632eef92700cd6c5e143aa0fe67b'
	f 0 'data.pak' "$data_checksum_original $data_checksum_censored $data_checksum_frenchcd"
	
esac

# Optional files - we don't need them, but copy them anyway if available
optional_file 'manual.pdf'
optional_file 'map.pdf'
optional_file 'arx_handbuch.pdf'

print


##########################################################################################
# Print a summary

if [ $install = 1 ] ; then verb='Installed' ; else verb='Verified' ; fi
printf "${white}%s Arx Fatalis %s data: ${green}%s${reset}\n" "$verb" \
	"$patch_ver" "$data_lang_desc"

if [ $checksum_failed = 1 ] ; then
	[ $gui = 0 ] || status 100 "Error!"
	die "There are wrong or missing files!${reset}

The game may run fine, or it may fail - good luck!" >&2
fi

status 100 "${dim_green}All good!${reset}"
if [ $install = 1 ] ; then verb='Installation' ; else verb='Verification' ; fi
dialog_message "$verb complete: $data_lang_desc

Have fun playing Arx Fatalis!"

quit $true