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

# This file is part of OpenTTD.
# OpenTTD 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, version 2.
# OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.

log() {
	if [ $1 = "1" ]; then
		shift
		echo "$@"
	else
		shift
	fi
	echo "$@" >> $config_log
}

set_default() {
	ignore_extra_parameters="0"
	# We set all kinds of defaults for params. Later on the user can override
	# most of them; but if they don't, this default is used.
	build=""
	host=""
	cc_build=""
	cc_host=""
	cxx_build=""
	cxx_host=""
	windres=""
	strip=""
	lipo=""
	awk="awk"
	os="DETECT"
	endian="AUTO"
	cpu_type="DETECT"
	config_log="config.log"
	prefix_dir="/usr/local"
	binary_dir="games"
	data_dir="share/games/openttd"
	doc_dir="1"
	icon_dir="share/pixmaps"
	icon_theme_dir="1"
	personal_dir="1"
	shared_dir="1"
	install_dir="/"
	man_dir="1"
	menu_dir="1"
	menu_group="Game;"
	menu_name="OpenTTD"
	binary_name="openttd"
	enable_debug="0"
	enable_desync_debug="0"
	enable_profiling="0"
	enable_lto="0"
	enable_dedicated="0"
	enable_network="1"
	enable_static="1"
	enable_translator="0"
	enable_unicode="1"
	enable_console="1";
	enable_assert="1"
	enable_strip="0"
	enable_universal="0"
	enable_osx_g5="0"
	enable_cocoa_quartz="1"
	enable_cocoa_quickdraw="1"
	with_osx_sysroot="1"
	with_application_bundle="1"
	with_menu_entry="1"
	with_allegro="1"
	with_sdl="1"
	with_cocoa="1"
	with_zlib="1"
	with_lzma="1"
	with_lzo2="1"
	with_png="1"
	enable_builtin_depend="1"
	with_makedepend="0"
	with_direct_music="1"
	with_sort="1"
	with_iconv="1"
	with_midi=""
	with_midi_arg=""
	with_libtimidity="1"
	with_freetype="1"
	with_fontconfig="1"
	with_icu="1"
	static_icu="0"
	with_psp_config="1"
	with_threads="1"
	with_distcc="1"
	with_ccache="1"
	with_nforenum="1"
	with_grfcodec="1"

	save_params_array="
		build
		host
		cc_build
		cc_host
		cxx_build
		cxx_host
		windres
		strip
		lipo
		awk
		os
		endian
		cpu_type
		config_log
		prefix_dir
		binary_dir
		data_dir
		doc_dir
		icon_dir
		icon_theme_dir
		man_dir
		menu_dir
		personal_dir
		shared_dir
		install_dir
		menu_group
		menu_name
		binary_name
		enable_debug
		enable_desync_debug
		enable_profiling
		enable_lto
		enable_dedicated
		enable_network
		enable_static
		enable_translator
		enable_unicode
		enable_console
		enable_assert
		enable_strip
		enable_universal
		enable_osx_g5
		enable_cocoa_quartz
		enable_cocoa_quickdraw
		with_osx_sysroot
		with_application_bundle
		with_allegro
		with_sdl
		with_cocoa
		with_zlib
		with_lzma
		with_lzo2
		with_png
		enable_builtin_depend
		with_makedepend
		with_direct_music
		with_sort
		with_iconv
		with_midi
		with_midi_arg
		with_libtimidity
		with_freetype
		with_fontconfig
		with_icu
		static_icu
		with_psp_config
		with_threads
		with_distcc
		with_ccache
		with_grfcodec
		with_nforenum
	CC CXX CFLAGS CXXFLAGS LDFLAGS CFLAGS_BUILD CXXFLAGS_BUILD LDFLAGS_BUILD"
}

detect_params() {
	# Walk over all params from the user and override any default settings if
	#  needed. This also handles any invalid option.
	for p in "$@"; do
		if [ -n "$prev_p" ]; then
			eval "$prev_p=\$p"
			prev_p=
			continue
		fi

		optarg=`expr "x$p" : 'x[^=]*=\(.*\)'`

		case "$p" in
			--help | -h | -\?)            showhelp; exit 0;;

			--config-log)                 prev_p="config_log";;
			--config-log=*)               config_log="$optarg";;

			--build)                      prev_p="build";;
			--build=*)                    build="$optarg";;

			--host)                       prev_p="host";;
			--host=*)                     host="$optarg";;

			--os)                         prev_p="os";;
			--os=*)                       os="$optarg";;

			--cpu-type)                   prev_p="cpu_type";;
			--cpu-type=*)                 cpu_type="$optarg";;

			--cc-build)                   prev_p="cc_build";;
			--cc-build=*)                 cc_build="$optarg";;
			--cc-host)                    prev_p="cc_host";;
			--cc-host=*)                  cc_host="$optarg";;
			--cxx-build)                  prev_p="cxx_build";;
			--cxx-build=*)                cxx_build="$optarg";;
			--cxx-host)                   prev_p="cxx_host";;
			--cxx-host=*)                 cxx_host="$optarg";;
			--windres)                    prev_p="windres";;
			--windres=*)                  windres="$optarg";;
			--awk)                        prev_p="awk";;
			--awk=*)                      awk="$optarg";;
			--strip)                      prev_p="strip";;
			--strip=*)                    strip="$optarg";;
			--lipo)                       prev_p="lipo";;
			--lipo=*)                     lipo="$optarg";;

			--endian)                     prev_p="endian";;
			--endian=*)                   endian="$optarg";;



			# Alias --prefix with --prefix-dir, for compatibility with GNU autotools
			--prefix-dir | --prefix)      prev_p="prefix_dir";;
			--prefix-dir=* | --prefix=*)  prefix_dir="$optarg";;

			--binary-dir)                 prev_p="binary_dir";;
			--binary-dir=*)               binary_dir="$optarg";;

			--data-dir)                   prev_p="data_dir";;
			--data-dir=*)                 data_dir="$optarg";;

			--doc-dir)                    prev_p="doc_dir";;
			--doc-dir=*)                  doc_dir="$optarg";;

			--icon-dir)                   prev_p="icon_dir";;
			--icon-dir=*)                 icon_dir="$optarg";;

			--icon-theme-dir)             prev_p="icon_theme_dir";;
			--icon-theme-dir=*)           icon_theme_dir="$optarg";;
			--without-icon-theme)         icon_theme_dir="";;

			--menu-dir)                   prev_p="menu_dir";;
			--menu-dir=*)                 menu_dir="$optarg";;
			--without-menu-entry)         menu_dir="";;

			--menu-name)                  prev_p="menu_name";;
			--menu-name=*)                menu_name="$optarg";;

			--binary-name)                prev_p="binary_name";;
			--binary-name=*)              binary_name="$optarg";;

			--man-dir)                    prev_p="man_dir";;
			--man-dir=*)                  man_dir="$optarg";;

			--personal-dir)               prev_p="personal_dir";;
			--personal-dir=*)             personal_dir="$optarg";;
			--without-personal-dir)       personal_dir="";;

			--shared-dir)                 prev_p="shared_dir";;
			--shared-dir=*)               shared_dir="$optarg";;
			--without-shared-dir)         shared_dir="";;

			--install-dir)                prev_p="install_dir";;
			--install-dir=*)              install_dir="$optarg";;



			--menu-group)                 prev_p="menu_group";;
			--menu-group=*)               menu_group="$optarg";;



			--enable-debug)               enable_debug="1";;
			--enable-debug=*)             enable_debug="$optarg";;
			--enable-desync-debug)        enable_desync_debug="1";;
			--enable-desync-debug=*)      enable_desync_debug="$optarg";;
			--enable-profiling)           enable_profiling="1";;
			--enable-profiling=*)         enable_profiling="$optarg";;
			--enable-lto)                 enable_lto="1";;
			--enable-lto=*)               enable_lto="$optarg";;
			--enable-ipo)                 enable_lto="1";;
			--enable-ipo=*)               enable_lto="$optarg";;
			--enable-dedicated)           enable_dedicated="1";;
			--enable-dedicated=*)         enable_dedicated="$optarg";;
			--enable-network)             enable_network="2";;
			--enable-network=*)           enable_network="$optarg";;
			--disable-network)            enable_network="0";;
			--disable-static)             enable_static="0";;
			--enable-static)              enable_static="2";;
			--enable-static=*)            enable_static="$optarg";;
			--disable-translator)         enable_translator="0";;
			--enable-translator)          enable_translator="2";;
			--enable-translator=*)        enable_translator="$optarg";;
			--disable-assert)             enable_assert="0";;
			--enable-assert)              enable_assert="2";;
			--enable-assert=*)            enable_assert="$optarg";;
			--disable-strip)              enable_strip="0";;
			--enable-strip)               enable_strip="2";;
			--enable-strip=*)             enable_strip="$optarg";;
			--disable-universal)          enable_universal="0";;
			--enable-universal)           enable_universal="i386 ppc";;
			--enable-universal=*)         enable_universal="$optarg";;
			--disable-osx-g5)             enable_osx_g5="0";;
			--enable-osx-g5)              enable_osx_g5="2";;
			--enable-osx-g5=*)            enable_osx_g5="$optarg";;
			--disable-unicode)            enable_unicode="0";;
			--enable-unicode)             enable_unicode="2";;
			--enable-unicode=*)           enable_unicode="$optarg";;
			--disable-console)            enable_console="0";;
			--enable-console)             enable_console="2";;
			--enable-console=*)           enable_console="$optarg";;

			--disable-cocoa-quartz)       enable_cocoa_quartz="0";;
			--enable-cocoa-quartz)        enable_cocoa_quartz="2";;
			--enable-cocoa-quartz=*)      enable_cocoa_quartz="$optarg";;
			--disable-cocoa-quickdraw)    enable_cocoa_quickdraw="0";;
			--enable-cocoa-quickdraw)     enable_cocoa_quickdraw="2";;
			--enable-cocoa-quickdraw=*)   enable_cocoa_quickdraw="$optarg";;

			--with-allegro)               with_allegro="2";;
			--without-allegro)            with_allegro="0";;
			--with-allegro=*)             with_allegro="$optarg";;

			--with-sdl)                   with_sdl="2";;
			--without-sdl)                with_sdl="0";;
			--with-sdl=*)                 with_sdl="$optarg";;

			--with-cocoa)                 with_cocoa="2";;
			--without-cocoa)              with_cocoa="0";;
			--with-cocoa=*)               with_cocoa="$optarg";;

			--with-zlib)                  with_zlib="2";;
			--without-zlib)               with_zlib="0";;
			--with-zlib=*)                with_zlib="$optarg";;

			--with-lzma)                  with_lzma="2";;
			--without-lzma)               with_lzma="0";;
			--with-lzma=*)                with_lzma="$optarg";;
			--with-liblzma)               with_lzma="2";;
			--without-liblzma)            with_lzma="0";;
			--with-liblzma=*)             with_lzma="$optarg";;

			--with-lzo2)                  with_lzo2="2";;
			--without-lzo2)               with_lzo2="0";;
			--with-lzo2=*)                with_lzo2="$optarg";;
			--with-liblzo2)               with_lzo2="2";;
			--without-liblzo2)            with_lzo2="0";;
			--with-liblzo2=*)             with_lzo2="$optarg";;

			--with-png)                   with_png="2";;
			--without-png)                with_png="0";;
			--with-png=*)                 with_png="$optarg";;
			--with-libpng)                with_png="2";;
			--without-libpng)             with_png="0";;
			--with-libpng=*)              with_png="$optarg";;

			--with-libtimidity)           with_libtimidity="2";;
			--without-libtimidity)        with_libtimidity="0";;
			--with-libtimidity=*)         with_libtimidity="$optarg";;

			--with-freetype)              with_freetype="2";;
			--without-freetype)           with_freetype="0";;
			--with-freetype=*)            with_freetype="$optarg";;
			--with-libfreetype)           with_freetype="2";;
			--without-libfreetype)        with_freetype="0";;
			--with-libfreetype=*)         with_freetype="$optarg";;

			--with-fontconfig)            with_fontconfig="2";;
			--without-fontconfig)         with_fontconfig="0";;
			--with-fontconfig=*)          with_fontconfig="$optarg";;
			--with-libfontconfig)         with_fontconfig="2";;
			--without-libfontconfig)      with_fontconfig="0";;
			--with-libfontconfig=*)       with_fontconfig="$optarg";;

			--with-icu)                   with_icu="2";;
			--without-icu)                with_icu="0";;
			--with-icu=*)                 with_icu="$optarg";;
			--with-libicu)                with_icu="2";;
			--without-libicu)             with_icu="0";;
			--with-libicu=*)              with_icu="$optarg";;
			--static-icu)                 static_icu="1";;
			--static-icu=*)               static_icu="$optarg";;
			--static-libicu)              static_icu="1";;
			--static-libicu=*)            static_icu="$optarg";;

			--with-psp-config)            with_psp_config="2";;
			--without-psp-config)         with_psp_config="0";;
			--with-psp-config=*)          with_psp_config="$optarg";;

			--disable-builtin-depend)     enable_builtin_depend="0";;
			--enable-builtin-depend)      enable_builtin_depend="2";;
			--enable-builtin-depend=*)    enable_builtin_depend="$optarg";;

			--with-makedepend)            with_makedepend="2";;
			--without-makedepend)         with_makedepend="0";;
			--with-makedepend=*)          with_makedepend="$optarg";;

			--with-direct-music)          with_direct_music="2";;
			--without-direct-music)       with_direct_music="0";;
			--with-direct-music=*)        with_direct_music="$optarg";;

			--with-sort)                  with_sort="2";;
			--without-sort)               with_sort="0";;
			--with-sort=*)                with_sort="$optarg";;

			--with-iconv)                 with_iconv="2";;
			--without-iconv)              with_iconv="0";;
			--with-iconv=*)               with_iconv="$optarg";;

			--with-midi=*)                with_midi="$optarg";;
			--with-midi-arg=*)            with_midi_arg="$optarg";;

			--without-distcc)             with_distcc="0";;
			--with-distcc)                with_distcc="2";;
			--with-distcc=*)              with_distcc="$optarg";;

			--without-ccache)             with_ccache="0";;
			--with-ccache)                with_ccache="2";;
			--with-ccache=*)              with_ccache="$optarg";;

			--without-nforenum)           with_nforenum="0";;
			--with-nforenum)              with_nforenum="2";;
			--with-nforenum=*)            with_nforenum="$optarg";;

			--without-grfcodec)           with_grfcodec="0";;
			--with-grfcodec)              with_grfcodec="2";;
			--with-grfcodec=*)            with_grfcodec="$optarg";;

			--without-osx-sysroot)        with_osx_sysroot="0";;
			--with-osx-sysroot)           with_osx_sysroot="2";;
			--with-osx-sysroot=*)         with_osx_sysroot="$optarg";;

			--without-application-bundle) with_application_bundle="0";;
			--with-application-bundle)    with_application_bundle="1";;
			--with-application-bundle=*)  with_application_bundle="$optarg";;

			--without-threads)            with_threads="0";;
			--with-threads)               with_threads="1";;
			--with-threads=*)             with_threads="$optarg";;

			CC=* | --CC=*)                CC="$optarg";;
			CXX=* | --CXX=*)              CXX="$optarg";;
			CFLAGS=* | --CFLAGS=*)        CFLAGS="$optarg";;
			CXXFLAGS=* | --CXXFLAGS=*)    CXXFLAGS="$optarg";;
			LDFLAGS=* | --LDFLAGS=*)      LDFLAGS="$optarg";;
			CFLAGS_BUILD=* | --CFLAGS_BUILD=* | --CFLAGS-BUILD=*)     CFLAGS_BUILD="$optarg";;
			CXXFLAGS_BUILD=* | --CXXFLAGS_BUILD=* | --CXXFLAGS-BUILD=*) CXXFLAGS_BUILD="$optarg";;
			LDFLAGS_BUILD=* | --LDFLAGS_BUILD=* | --LDFLAGS-BUILD=*)   LDFLAGS_BUILD="$optarg";;

			--ignore-extra-parameters)    ignore_extra_parameters="1";;

			--* | -*)
				if [ "$ignore_extra_parameters" = "0" ]; then
					log 1 "Unknown option $p"
					exit 1
				else
					log 1 "Unknown option $p ignored"
				fi
				;;
		esac
	done

	if [ -n "$prev_p" ]; then
		log 1 "configure: error: missing argument to --$prev_p"
		exit 1
	fi

	# Clean the logfile
	echo "" > $config_log
	log 2 "Invocation: $0 $*"
}

save_params() {
	# Here we save all params, so we can later on do an exact redo of this
	#  configuration, without having the user to re-input stuff

	echo "Running configure with following options:" >> $config_log
	echo "" >> $config_log

	configure="$CONFIGURE_EXECUTABLE --ignore-extra-parameters"
	for p in $save_params_array; do
		eval "v=\"\$$p\""
		p=`echo "$p" | sed 's@_@-@g;s@\n@@g;s@ @\\ @g'`
		# Only save those params that aren't empty
		configure="$configure --$p=\"$v\""
	done

	echo "$configure" >> $config_log
	echo "$configure" > config.cache
	echo "" >> $config_log
}

check_params() {
	# Some params want to be in full uppercase, else they might not work as
	# expected.. fix that here

	endian=`echo $endian | tr '[a-z]' '[A-Z]'`
	os=`echo $os | tr '[a-z]' '[A-Z]'`
	cpu_type=`echo $cpu_type | tr '[a-z]' '[A-Z]'`

	# Check if all params have valid values

	# Endian only allows AUTO, LE and, BE
	if [ -z "`echo $endian | egrep '^(AUTO|LE|BE|PREPROCESSOR)$'`" ]; then
		log 1 "configure: error: invalid option --endian=$endian"
		log 1 " Available options are: --endian=[AUTO|LE|BE]"
		exit 1
	fi
	if [ "$endian" = "PREPROCESSOR" ] && [ "$os" != "OSX" ]; then
		log 1 "configure: error: invalid option --endian=$endian"
		log 1 " PREPROCESSOR is only available for OSX"
		exit 1
	fi
	# OS only allows DETECT, UNIX, OSX, FREEBSD, OPENBSD, MORPHOS, BEOS, HAIKU, SUNOS, CYGWIN, MINGW, OS2, DOS, WINCE, and PSP
	if [ -z "`echo $os | egrep '^(DETECT|UNIX|OSX|FREEBSD|OPENBSD|NETBSD|HPUX|MORPHOS|BEOS|HAIKU|SUNOS|CYGWIN|MINGW|OS2|DOS|WINCE|PSP)$'`" ]; then
		log 1 "configure: error: invalid option --os=$os"
		log 1 " Available options are: --os=[DETECT|UNIX|OSX|FREEBSD|OPENBSD|NETBSD|HPUX|MORPHOS|BEOS|HAIKU|SUNOS|CYGWIN|MINGW|OS2|DOS|WINCE|PSP]"
		exit 1
	fi
	# cpu_type can be either 32 or 64
	if [ -z "`echo $cpu_type | egrep '^(32|64|DETECT)$'`" ]; then
		log 1 "configure: error: invalid option --cpu-type=$cpu_type"
		log 1 " Available options are: --cpu-type[=DETECT|32|64]"
		exit 1
	fi
	# enable_debug should be between 0 and 4
	if [ -z "`echo $enable_debug | egrep '^[0123]$'`" ]; then
		log 1 "configure: error: invalid option --enable-debug=$enable_debug"
		log 1 " Available options are: --enable-debug[=0123]"
		exit 1
	fi

	# enable_desync_debug should be between 0 and 3
	if [ -z "`echo $enable_desync_debug | egrep '^[012]$'`" ]; then
		log 1 "configure: error: invalid option --enable-desync-debug=$enable_desync_debug"
		log 1 " Available options are: --enable-desync-debug[=012]"
		exit 1
	fi

	detect_awk

	detect_os

	check_build
	check_host

	# Check for universal builds; they only make sense for OSX, so fail if enabled for another OS
	if [ "$enable_universal" = "0" ]; then
		log 1 "checking universal build... no"
	else
		if [ "$os" != "OSX" ]; then
			log 1 "configure: error: --enable-universal only works on OSX"
			exit 1
		fi
		log 1 "checking universal build... yes, for: $enable_universal"
	fi

	# Already detected by check_build
	log 1 "checking build cc... $cc_build"
	log 1 "checking host cc... $cc_host"

	check_cxx_build
	check_cxx_host
	check_windres
	if [ "$enable_strip" != "0" ]; then
		check_strip
	else
		log 1 "checking strip... disabled"
	fi
	check_lipo

	if [ "$enable_builtin_depend" != "0" ]; then
		log 1 "checking builtin depend... yes"
		makedepend="\$(SRC_OBJS_DIR)/\$(DEPEND)"
	else
		log 1 "checking builtin depend... no"
	fi

	check_makedepend
	detect_cputype

	if [ "$enable_static" = "1" ]; then
		if [ "$os" = "MINGW" ] || [ "$os" = "CYGWIN" ] || [ "$os" = "MORPHOS" ] || [ "$os" = "DOS" ]; then
			enable_static="2"
		else
			enable_static="0"
		fi
	fi

	if [ "$enable_static" != "0" ]; then
		log 1 "checking static... yes"

		if [ "$os" != "MINGW" ] && [ "$os" != "CYGWIN" ] && [ "$os" != "OSX" ] && [ "$os" != "MORPHOS" ] && [ "$os" != "DOS" ]; then
			log 1 "WARNING: static is only known to work on Windows, DOS, MacOSX and MorphOS"
			log 1 "WARNING: use static at your own risk on this platform"

			sleep 5
		fi
	else
		log 1 "checking static... no"
	fi

	if [ "$enable_unicode" = "1" ]; then
		if [ "$os" = "MINGW" ] || [ "$os" = "CYGWIN" ] || [ "$os" = "DOS" ]; then
			enable_unicode="2"
		else
			enable_unicode="0"
		fi
	fi

	if [ "$enable_unicode" != "0" ]; then
		log 1 "checking unicode... yes"
	else
		log 1 "checking unicode... no"
	fi

	# Show what we configured
	if [ "$enable_debug" = "0" ]; then
		log 1 "using debug level... no"
	elif [ "$enable_profiling" != "0" ]; then
		log 1 "using debug level... profiling (debug level $enable_debug)"
	else
		log 1 "using debug level... level $enable_debug"
	fi

	if [ "$enable_desync_debug" = "0" ]; then
		log 1 "using desync debug level... no"
	else
		log 1 "using desync debug level... level $enable_desync_debug"
		log 1 "WARNING: desync debug functions slow down the game considerably."
		log 1 "WARNING: use only when you are instructed to do so"
		log 1 "         or when you know what you are doing."

		sleep 5
	fi

	if [ "$enable_lto" != "0" ]; then
		# GCC 4.5 outputs '%{flto}', GCC 4.6 outputs '%{flto*}'
		has_lto=`($cxx_build -dumpspecs 2>&1 | grep '\%{flto') || ($cxx_build -help ipo 2>&1 | grep '\-ipo')`
		if [ -n "$has_lto" ]; then
			log 1 "using link time optimization... yes"
		else
			enable_lto="0"
			log 1 "using link time optimization... no"
			log 1 "WARNING: you selected link time optimization but it is not found."
			sleep 5
		fi
	else
		log 1 "using link time optimization... no"
	fi


	if [ "$os" != "OSX" ] && [ "$with_osx_sysroot" != "0" ]; then
		if [ "$with_osx_sysroot" = "1" ]; then
			with_osx_sysroot="0"

			log 1 "checking OSX sysroot... not OSX, skipping"
		else
			log 1 "configure: error: --with-osx-sysroot only works if OSX is the target"
			exit 1
		fi
	fi

	if [ "$with_osx_sysroot" != "0" ]; then
		if [ "$enable_universal" = "0" ] && [ "$with_osx_sysroot" != "1" ] && [ "$with_osx_sysroot" != "2" ]; then
			# Sysroot manually specified? Check for usability
			log 1 "checking OSX sysroot... $with_osx_sysroot"
			if ! check_osx_sdk "$with_osx_sysroot"; then
				log 1 "Passed sysroot not found/not functional"
				exit 1
			fi
		else
			# If autodetect and no universal, use system default
			if [ "$with_osx_sysroot" = "1" ] && [ "$enable_universal" = "0" ]; then
				log 1 "checking OSX sysroot... no (use system default)"
			else
				log 1 "checking OSX sysroot... automatically"
				detect_osx_sdk
			fi
		fi

		if [ -n "$osx_sdk_path" ]; then
			if [ "$enable_universal" != "0" ]; then
				if [ -z "$osx_sdk_104_path" ]; then
					log 1 "WARNING: Could not find a usable 10.4u SDK, the resulting"
					log 1 "WARNING: binary will only run on OSX 10.5 or later"
					osx_sdk_104_path="$osx_sdk_path"
				fi
				OSX_SYSROOT="-isysroot $osx_sdk_104_path"
				OSX_LD_SYSROOT="-Wl,-syslibroot,$osx_sdk_104_path"
			else
				OSX_SYSROOT="-isysroot $osx_sdk_path"
				OSX_LD_SYSROOT="-Wl,-syslibroot,$osx_sdk_path"
			fi
		fi
	else
		if [ "$os" = "OSX" ]; then
			log 1 "checking OSX sysroot... no (use system default)"
		fi
	fi

	detect_allegro
	detect_sdl
	detect_cocoa

	if [ "$enable_dedicated" != "0" ]; then
		log 1 "checking GDI video driver... dedicated server, skipping"
		log 1 "checking dedicated... found"

		if [ "$enable_network" = "0" ]; then
			log 1 "configure: error: building a dedicated server without network support is pointless"
			exit 1
		fi
	else
		if [ "$os" = "MINGW" ] || [ "$os" = "CYGWIN" ] || [ "$os" = "WINCE" ]; then
			log 1 "checking GDI video driver... found"
		else
			log 1 "checking GDI video driver... not Windows, skipping"
		fi

		if [ -z "$allegro_config" ] && [ -z "$sdl_config" ] && [ "$with_cocoa" = 0 ] && [ "$os" != "MINGW" ] && [ "$os" != "CYGWIN" ] && [ "$os" != "WINCE" ]; then
			log 1 "configure: error: no video driver development files found"
			log 1 " If you want a dedicated server use --enable-dedicated as parameter"
			exit 1
		else
			log 1 "checking dedicated... not selected"
		fi
	fi

	if [ "$os" != "MINGW" ] && [ "$os" != "CYGWIN" ]; then
		log 1 "checking console application... not Windows, skipping"
	elif [ "$enable_console" = "1" ] && [ "$enable_dedicated" != "0" ]; then
		log 1 "checking console application... dedicated server, enabled"
		enable_console="2"
	elif [ "$enable_console" = "1" ]; then
		log 1 "checking console application... disabled (only used when forced)"
		enable_console="0"
	elif [ "$enable_console" = "0" ]; then
		log 1 "checking console application... disabled"
	else
		log 1 "checking console application... enabled"
	fi

	if [ "$enable_network" = "1" ] && [ "$os" = "DOS" ]; then
		log 1 "checking network... DOS, skipping"
		enable_network=0
	elif [ "$enable_network" != "0" ]; then
		log 1 "checking network... found"
	else
		log 1 "checking network... disabled"
	fi

	log 1 "checking squirrel... found"
	SCRIPT_SRC_DIR="$ROOT_DIR/src/3rdparty/squirrel/include"

	if [ "$enable_translator" != "0" ]; then
		log 1 "checking translator... debug"
		# -t shows TODO items, normally they are muted
		strgen_flags="-t"
	else
		log 1 "checking translator... no"
		strgen_flags=""
	fi

	if [ "$enable_assert" != "0" ]; then
		log 1 "checking assert... enabled"
	else
		log 1 "checking assert... disabled"
	fi

	pre_detect_with_zlib=$with_zlib
	detect_zlib

	if [ "$with_zlib" = "0" ] || [ -z "$zlib" ]; then
		log 1 "WARNING: zlib was not detected or disabled"
		log 1 "WARNING: OpenTTD doesn't require zlib, but it does mean that many features"
		log 1 "WARNING: (like loading most old savegames/scenarios, loading heightmaps,"
		log 1 "WARNING: using PNG, or using fonts, ...) will be disabled."
		if [ "$pre_detect_with_zlib" = "0" ]; then
			log 1 "WARNING: We strongly suggest you to install zlib."
		else
			log 1 "configure: error: no zlib detected"
			log 1 " If you want to compile without zlib use --without-zlib as parameter"
			exit
		fi
	fi

	pre_detect_with_lzma=$with_lzma
	detect_lzma

	if [ "$with_lzma" = "0" ] || [ -z "$lzma_config" ]; then
		log 1 "WARNING: lzma was not detected or disabled"
		log 1 "WARNING: OpenTTD doesn't require lzma, but it does mean that many features"
		log 1 "WARNING: (like loading most savegames/scenarios and joining most servers)"
		log 1 "WARNING: will be disabled."
		if [ "$pre_detect_with_lzma" = "0" ]; then
			log 1 "WARNING: We strongly suggest you to install liblzma."
			log 1 "configure: error: no liblzma detected"
		else
			log 1 " If you want to compile without lzma use --without-lzma as parameter"
			exit
		fi
	fi

	pre_detect_with_lzo2=$with_lzo2
	detect_lzo2

	if [ "$with_lzo2" = "0" ] || [ -z "$lzo2" ]; then
		log 1 "WARNING: liblzo2 was not detected or disabled"
		log 1 "WARNING: OpenTTD doesn't require liblzo2, but it does mean that"
		log 1 "WARNING: loading old savegames/scenarios will be disabled."
		if [ "$pre_detect_with_lzo2" = "0" ]; then
			log 1 "WARNING: We strongly suggest you to install liblzo2."
		else
			log 1 "configure: error: no liblzo2 detected"
			log 1 " If you want to compile without liblzo2 use --without-liblzo2 as parameter"
			exit
		fi
	fi

	detect_png
	detect_freetype
	detect_fontconfig
	detect_icu
	detect_pspconfig
	detect_libtimidity

	if [ "$with_direct_music" != "0" ]; then
		if [ "$os" != "MINGW" ] && [ "$os" != "CYGWIN" ]; then
			if [ "$with_direct_music" != "1" ]; then
				log 1 "configure: error: direct-music is only supported on Win32 targets"
				exit 1
			fi
			with_direct_music="0"

			log 1 "checking direct-music... not Windows, skipping"
		else
			check_direct_music
		fi
	fi

	detect_sort

	if [ "$os" = "OSX" ] && [ "$endian" = "AUTO" ]; then
		endian="PREPROCESSOR"
	fi

	log 1 "checking endianess... $endian"

	# Suppress language errors when there is a version defined, indicating a release
	#  It just isn't pretty if any release produces warnings in the languages.
	if [ -f "$ROOT_DIR/version" ]; then
		lang_suppress="yes"
		log 1 "suppress language errors... yes"
	else
		lang_suppress=""
		log 1 "suppress language errors... no"
	fi

	if [ "$enable_debug" = "0" ] && [ "$enable_profiling" = "0" ] && [ "$enable_strip" != "0" ]; then
		if [ "$os" = "MORPHOS" ]; then
			strip_arg="--strip-all --strip-unneeded --remove-section .comment"
		elif [ "$os" = "OSX" ]; then
			strip_arg=""
		elif [ "$os" = "OS2" ]; then
			strip_arg=""
			# OS2 uses strip via gcc, because it needs to be feed to emxbind
			LDFLAGS="$LDFLAGS -s"
		elif [ "$os" = "SUNOS" ]; then
			# The GNU strip does know -s, the non-GNU doesn't
			#  So try to detect it (in a bit of an ugly way)
			strip_arg="`$strip -s strip.test 2>/dev/null && echo \"-s\"`"
		else
			strip_arg="-s"
		fi

		log 1 "checking stripping... $strip $strip_arg"
	else
		strip=""
		log 1 "checking stripping... skipped"
	fi

	if [ "$with_distcc" = "0" ]; then
		log 1 "checking distcc... no"
	elif [ "$with_distcc" = "1" ]; then
		with_distcc="0"

		log 1 "checking distcc... no (only used when forced)"
	elif [ "$with_distcc" = "2" ]; then
		distcc="distcc"
	else
		distcc="$with_distcc"
	fi
	if [ "$with_distcc" != "0" ]; then
		res="`$distcc --version 2>/dev/null | head -n 1 | cut -b 1-6`"
		if [ "$res" != "distcc" ]; then
			distcc=""
			log 1 "checking distcc... no"
			if [ "$with_distcc" = "2" ]; then
				log 1 "configure: error: no distcc detected, but was forced to be used"
				exit 1
			fi
			if [ "$with_distcc" != "1" ]; then
				log 1 "configure: error: '$with_distcc' doesn't seem a distcc to me"
				exit 1
			fi
		fi

		log 1 "checking distcc... $distcc"
	fi

	if [ "$with_ccache" = "0" ]; then
		log 1 "checking ccache... no"
	elif [ "$with_ccache" = "1" ]; then
		with_ccache="0"

		log 1 "checking ccache... no (only used when forced)"
	elif [ "$with_ccache" = "2" ]; then
		ccache="ccache"
	else
		ccache="$with_ccache"
	fi
	if [ "$with_ccache" != "0" ]; then
		res="`$ccache --version 2>/dev/null | head -n 1 | cut -b 1-6`"
		if [ "$res" != "ccache" ]; then
			ccache=""
			log 1 "checking ccache... no"
			if [ "$with_ccache" = "2" ]; then
				log 1 "configure: error: no ccache detected, but was forced to be used"
				exit 1
			fi
			if [ "$with_ccache" != "1" ]; then
				log 1 "configure: error: '$with_ccache' doesn't seem a ccache to me"
				exit 1
			fi
		fi

		log 1 "checking ccache... $ccache"
	fi

	detect_grfcodec
	detect_nforenum

	if [ -z "$grfcodec" ] && [ -n "$nforenum" ]; then
		log 1 "checking nforenum/grfcodec... nforenum needs grfcodec enabled, disabling nforenum"
		nforenum=""
	fi

	if [ -z "$nforenum" ] && [ -n "$grfcodec" ]; then
		log 1 "checking nforenum/grfcodec... grfcodec needs nforenum enabled, disabling grfcodec"
		grfcodec=""
	fi

	if [ "$os" = "DOS" ]; then
		with_threads="0"
	fi

	if [ "$os" != "OSX" ] && [ "$with_application_bundle" != "0" ]; then
		if [ "$with_application_bundle" = "1" ]; then
			with_application_bundle="0"

			log 1 "checking OSX application bundle... not OSX, skipping"
		else
			log 1 "configure: error: --with-application-bundle only works if OSX is the target"
			exit 1
		fi
	fi

	if [ "$os" = "OSX" ] && [ "$with_application_bundle" = "1" ]; then
		OSXAPP="OpenTTD.app"
	else
		OSXAPP=""
	fi

	if [ "$os" = "OSX" ]; then
		# Test on ppc970 (G5) - we can optimize there

		if [ "$enable_osx_g5" != "0" ]; then
			log 1 "detecting ppc970 (G5)... yes (forced)"
		else
			# First, are we a real OSX system, else we can't detect it
			native=`LC_ALL=C uname | tr '[A-Z]' '[a-z]' | grep darwin`
			# If $host doesn't match $build , we are cross-compiling
			if [ -n "$native" ] && [ "$build" = "$host" ]; then
				$cxx_build $SRC_DIR/os/macosx/G5_detector.cpp -o G5_detector
				res=`./G5_detector`
				rm -f G5_detector
				if [ -n "$res" ]; then
					# This is G5, add flags for it
					enable_osx_g5="2"

					log 1 "detecting ppc970 (G5)... yes"
				else
					enable_osx_g5="0"

					log 1 "detecting ppc970 (G5)... no"
				fi
			else
				enable_osx_g5="0"

				log 1 "detecting ppc970 (G5)... no (cross-compiling)"
			fi
		fi
	else
		if [ "$enable_osx_g5" != "0" ]; then
			log 1 "configure: error: ppc970 (OSX G5) selected, but not compiling for OSX"
			log 1 "configure: error: either select OSX as OS, or deselect ppc970"

			exit 1
		fi
	fi

	if [ -d "$ROOT_DIR/.svn" ] && [ -n "`svn help 2>/dev/null`" ]; then
		log 1 "checking revision... svn detection"
	elif [ -d "$ROOT_DIR/../.svn" ] && [ -n "`svn help 2>/dev/null`" ] && [ -n "`LC_ALL=C svn info $ROOT_DIR/.. | grep '^URL:.*tags$'`" ]; then
		# subversion changed its behaviour; now not all folders have a .svn folder,
		# but only the root folder. Since making tags requires a (sparse) checkout
		# of the tags folder, the folder of the tag does not have a .svn folder
		# anymore and this fails to detect the subversion repository checkout.
		log 1 "checking revision... svn detection (tag)"
	elif [ -d "$ROOT_DIR/.git" ] && [ -n "`git help 2>/dev/null`" ]; then
		log 1 "checking revision... git detection"
	elif [ -d "$ROOT_DIR/.hg" ] && [ -n "`hg help 2>/dev/null`" ]; then
		log 1 "checking revision... hg detection"
	elif [ -f "$ROOT_DIR/.ottdrev" ]; then
		log 1 "checking revision... source tarball"
	else
		log 1 "checking revision... no detection"
		log 1 "WARNING: there is no means to determine the version."
		log 1 "WARNING: please use a subversion, mercurial, or git checkout of OpenTTD."
		log 1 "WARNING: you can only join game servers that have been compiled without"
		log 1 "WARNING:   version detection."
		log 1 "WARNING: there is a great chance you desync."
		log 1 "WARNING: USE WITH CAUTION!"

		sleep 5
	fi

	if [ "$doc_dir" = "1" ]; then
		if [ "$os" = "UNIX" ] || [ "$os" = "FREEBSD" ] || [ "$os" = "OPENBSD" ] || [ "$os" = "NETBSD" ] || [ "$os" = "HPUX" ] || [ "$os" = "SUNOS" ]; then
			doc_dir="share/doc/openttd"
		else
			doc_dir="$data_dir/docs"
		fi
	else
		doc_dir="`echo $doc_dir | sed 's@\([^\]\)\\\\ @\1\\\\\\\\ @g;s@\([^\]\) @\1\\\\\\\\ @g'`"
	fi

	if [ "$icon_theme_dir" = "1" ]; then
		if [ "$os" = "UNIX" ] || [ "$os" = "FREEBSD" ] || [ "$os" = "OPENBSD" ] || [ "$os" = "NETBSD" ] || [ "$os" = "HPUX" ] || [ "$os" = "SUNOS" ]; then
			icon_theme_dir="share/icons/hicolor"
		else
			icon_theme_dir=""
		fi
	else
		icon_theme_dir="`echo $icon_theme_dir | sed 's@\([^\]\)\\\\ @\1\\\\\\\\ @g;s@\([^\]\) @\1\\\\\\\\ @g'`"
	fi

	if [ "$personal_dir" = "1" ]; then
		if [ "$os" = "MINGW" ] || [ "$os" = "CYGWIN" ] || [ "$os" = "WINCE" ] || [ "$os" = "DOS" ] || [ "$os" = "HAIKU" ]; then
			personal_dir="OpenTTD"
		elif [ "$os" = "OSX" ]; then
			personal_dir="Documents/OpenTTD"
		else
			personal_dir=".openttd"
		fi
	else
		personal_dir="`echo $personal_dir | sed 's@\([^\]\)\\\\ @\1\\\\\\\\ @g;s@\([^\]\) @\1\\\\\\\\ @g'`"
	fi

	if [ "$shared_dir" = "1" ]; then
		# we are using default values
		if [ "$os" = "OSX" ]; then
			shared_dir="/Library/Application\\\\ Support/OpenTTD"
		else
			shared_dir=""
		fi
	else
		shared_dir="`echo $shared_dir | sed 's@\([^\]\)\\\\ @\1\\\\\\\\ @g;s@\([^\]\) @\1\\\\\\\\ @g'`"
	fi

	if [ "$man_dir" = "1" ]; then
		# add manpage on UNIX systems
		if [ "$os" = "UNIX" ] || [ "$os" = "FREEBSD" ] || [ "$os" = "OPENBSD" ] || [ "$os" = "NETBSD" ] || [ "$os" = "HPUX" ] || [ "$os" = "SUNOS" ] || [ "$os" = "OSX" ]; then
			man_dir="share/man/man6"
		else
			man_dir=""
		fi
	else
		man_dir="`echo $man_dir | sed 's@\([^\]\)\\\\ @\1\\\\\\\\ @g;s@\([^\]\) @\1\\\\\\\\ @g'`"
	fi

	if [ "$menu_dir" = "1" ]; then
		# add a freedesktop menu item only for some UNIX systems
		if [ "$os" = "UNIX" ] || [ "$os" = "FREEBSD" ] || [ "$os" = "OPENBSD" ] || [ "$os" = "NETBSD" ] || [ "$os" = "HPUX" ] || [ "$os" = "SUNOS" ]; then
			menu_dir="share/applications"
		else
			menu_dir=""
		fi
	else
		menu_dir="`echo $menu_dir | sed 's@\([^\]\)\\\\ @\1\\\\\\\\ @g;s@\([^\]\) @\1\\\\\\\\ @g'`"
	fi

	detect_iconv

	if [ -n "$personal_dir" ]
	then
		log 1 "personal home directory... $personal_dir"
	else
		log 1 "personal home directory... none"
	fi

	if [ -n "$shared_dir" ]
	then
		log 1 "shared data directory... $shared_dir"
	else
		log 1 "shared data directory... none"
	fi

	if [ -n "$install_dir" ]
	then
		log 1 "installation directory... $install_dir"
	else
		log 1 "installation directory... none"
	fi

	if [ -n "$icon_theme_dir" ]
	then
		log 1 "icon theme directory... $icon_theme_dir"
	else
		log 1 "icon theme directory... none"
	fi

	if [ -n "$man_dir" ]
	then
		log 1 "manual page directory... $man_dir"
	else
		log 1 "manual page directory... none"
	fi

	if [ -n "$menu_dir" ]
	then
		log 1 "menu item directory... $menu_dir"
	else
		log 1 "menu item directory... none"
	fi
}

make_compiler_cflags() {
	# Params:
	# $1 - compiler
	# $2 - name of the cflags variable
	# $3 - name of the cxxflags variable
	# $4 - name of the ldflags variable
	# $5 - name of the features variable

	eval eval "flags=\\\$$2"
	eval eval "cxxflags=\\\$$3"
	eval eval "ldflags=\\\$$4"
	eval eval "features=\\\$$5"

	if [ `basename $1 | cut -c 1-3` = "icc" ]; then
		# Enable some things only for certain ICC versions
		cc_version=`$1 -dumpversion | cut -c 1-4 | sed s@\\\.@@g`

		flags="$flags -rdynamic"
		ldflags="$ldflags -rdynamic"

		if [ -z "$first_time_icc_check" ]; then
			first_time_icc_check=no
			if [ $cc_version -lt 90 ]; then
				log 1 "WARNING: you seem to be using a very old version of ICC"
				log 1 "WARNING: OpenTTD hasn't been tested with this version"
				sleep 5
			elif [ $cc_version -lt 120 ]; then
				log 1 "WARNING: you seem to be using an unsupported ICC version"
				log 1 "WARNING: ICC older than 12.0 is known to fail to compile OpenTTD"
				sleep 5
			fi
		fi

		flags="$flags -Wall"
		# remark #111: statement is unreachable
		flags="$flags -wd111"
		# remark #181: argument is incompatible with corresponding format string conversion
		# ICC is very picky about signedness of operands, warnings provided by GCC are enough
		flags="$flags -wd181"
		# remark #271: trailing comma is nonstandard
		flags="$flags -wd271"
		# remark #280: selector expression is constant
		flags="$flags -wd280"
		# remark #304: access control not specified ("public" by default)
		flags="$flags -wd304"
		# remark #383: value copied to temporary, reference to temporary used
		flags="$flags -wd383"
		# remark #444: destructor for base class ... is not virtual
		flags="$flags -wd444"
		# remark #593: variable ... was set but never used
		flags="$flags -wd593"
		# warning #654: overloaded virtual function ... is only partially overridden in class ...
		flags="$flags -wd654"
		# remark #810: conversion from ... to ... may lose significant bits
		flags="$flags -wd810"
		# remark #869: parameter ... was never referenced
		flags="$flags -wd869"
		# warning #873: function ... ::operator new ... has no corresponding operator delete ...
		flags="$flags -wd873"
		# remark #981: operands are evaluated in unspecified order
		flags="$flags -wd981"
		# remark #1418: external function definition with no prior declaration
		flags="$flags -wd1418"
		# remark #1419: external declaration in primary source file
		flags="$flags -wd1419"
		# remark #1572: floating-point equality and inequality
		flags="$flags -wd1572"
		# remark #1599: declaration hides variable/parameter ...
		flags="$flags -wd1599"
		# remark #1720: function ... ::operator new ... has no corresponding member operator delete ...
		flags="$flags -wd1720"

		if [ $cc_version -lt 110 ]; then
			# warns about system headers with recent glibc:
			# warning #1292: attribute "__nonnull__" ignored
			flags="$flags -wd1292"
		fi

		if [ $cc_version -ge 100 ]; then
			# warning #1899: multicharacter character literal (potential portability problem)
			flags="$flags -wd1899"
			# vec report defaults to telling where it did loop vectorisation, which is not very important
			flags="$flags -vec-report=0 "
		fi

		if [ $cc_version -ge 110 ]; then
			# remark #2259: non-pointer conversion from ... to ... may lose significant bits
			flags="$flags -wd2259"
			# Use c++0x mode so static_assert() is available
			cxxflags="$cxxflags -std=c++0x"
		fi

		if [ "$enable_lto" != "0" ]; then
			has_ipo=`$1 -help ipo | grep '\-ipo'`
			if [ -n "$has_ipo" ]; then
				# Use IPO (only if we see IPO exists and is requested)
				flags="$flags -ipo"
				features="$features lto"
			fi
		fi
	elif [ `basename $1 | grep 'clang'` ]; then
		# Enable some things only for certain clang versions
		cc_version="`$1 -v 2>&1 | head -n 1 | sed s@[^0-9]@@g | cut -c 1-2`"

		# aliasing rules are not held in openttd code
		flags="$flags -fno-strict-aliasing"

		# -W alone doesn't enable all warnings enabled by -Wall; on the other hand,
		# -Weverything enables too many useless warnings that can't be disabled (as of 3.0)
		flags="$flags -Wall -W"

		# warning: unused parameter '...'
		flags="$flags -Wno-unused-parameter"

		# warning: expression result unused
		flags="$flags -Wno-unused-value"

		# warning: multi-character character constant
		flags="$flags -Wno-multichar"

		# warning: explicitly assigning a variable of type '...' to itself
		# it happens when using the FOR_ALL_WINDOWS_FROM_BACK_FROM macro
		flags="$flags -Wno-self-assign"

		if [ "$cc_version" -lt "30" ]; then
			# warning: equality comparison with extraneous parentheses
			flags="$flags -Wno-parentheses"
			# warning: operands of ? are integers of different signs: 'unsigned int' and 'int'
			flags="$flags -Wno-sign-compare"
		fi

		if [ "$cc_version" -ge "30" ]; then
			# warning: equality comparison with extraneous parentheses
			# this warning could be useful, but it warns about code in squirrel
			flags="$flags -Wno-parentheses-equality"
		fi

		if [ "$with_ccache" != "0" -o "$with_distcc" != "0" ]; then
			# ccache and distcc run separate preprocess and compile passes,
			# both are fed with the same CFLAGS. Unfortunately, clang
			# complains about -I when compiling preprocessed files:
			# "clang: warning: argument unused during compilation: '-I /usr/include'"
			flags="$flags -Qunused-arguments"
		fi

		if [ "$enable_assert" -eq "0" ]; then
			# do not warn about unused variables when building without asserts
			flags="$flags -Wno-unused-variable"
		fi

		# rdynamic is used to get useful stack traces from crash reports.
		ldflags="$ldflags -rdynamic"
	else
		# Enable some things only for certain GCC versions
		cc_version=`$1 -dumpversion | cut -c 1,3`

		if [ $cc_version -lt 33 ]; then
			log 1 "configure: error: gcc older than 3.3 can't compile OpenTTD because of its poor template support"
			exit 1
		fi

		flags="$flags -Wall -Wno-multichar -Wsign-compare -Wundef"
		flags="$flags -Wwrite-strings -Wpointer-arith"
		flags="$flags -W -Wno-unused-parameter -Wredundant-decls"
		flags="$flags -Wformat=2 -Wformat-security"

		if [ $enable_assert -eq 0 ]; then
			# Do not warn about unused variables when building without asserts
			flags="$flags -Wno-unused-variable"
			if [ $cc_version -ge 46 ]; then
				# GCC 4.6 gives more warnings, disable them too
				flags="$flags -Wno-unused-but-set-variable"
				flags="$flags -Wno-unused-but-set-parameter"
			fi
		fi

		if [ $cc_version -ge 34 ]; then
			# Warn when a variable is used to initialise itself:
			# int a = a;
			flags="$flags -Winit-self"
		fi

		if [ $cc_version -ge 40 ]; then
			# GCC 4.0+ complains about that we break strict-aliasing.
			#  On most places we don't see how to fix it, and it doesn't
			#  break anything. So disable strict-aliasing to make the
			#  compiler all happy.
			flags="$flags -fno-strict-aliasing"
			# Warn about casting-out 'const' with regular C-style cast.
			#  The preferred way is const_cast<>() which doesn't warn.
			flags="$flags -Wcast-qual"
		fi

		if [ $cc_version -ge 42 ]; then
			# GCC 4.2+ automatically assumes that signed overflows do
			# not occur in signed arithmetics, whereas we are not
			# sure that they will not happen. It furthermore complains
			# about its own optimized code in some places.
			flags="$flags -fno-strict-overflow"
			# GCC 4.2 no longer includes -Wnon-virtual-dtor in -Wall.
			# Enable it in order to be consistent with older GCC versions.
			flags="$flags -Wnon-virtual-dtor"
		fi

		if [ $cc_version -ge 43 ]; then
			# Use gnu++0x mode so static_assert() is available.
			# Don't use c++0x, it breaks mingw (with gcc 4.4.0).
			cxxflags="$cxxflags -std=gnu++0x"
		fi

		if [ $cc_version -eq 45 ]; then
			# Prevent optimisation supposing enums are in a range specified by the standard
			# For details, see http://gcc.gnu.org/PR43680
			flags="$flags -fno-tree-vrp"
		fi

		if [ $cc_version -ge 47 ]; then
			# Disable -Wnarrowing which gives many warnings, such as:
			# warning: narrowing conversion of '...' from 'unsigned int' to 'int' inside { } [-Wnarrowing]
			# They are valid according to the C++ standard, but useless.
			cxxflags="$cxxflags -Wno-narrowing"
			# Disable bogus 'attempt to free a non-heap object' warning
			flags="$flags -Wno-free-nonheap-object"
		fi

		if [ "$enable_lto" != "0" ]; then
			# GCC 4.5 outputs '%{flto}', GCC 4.6 outputs '%{flto*}'
			has_lto=`$1 -dumpspecs | grep '\%{flto'`
			if [ -n "$has_lto" ]; then
				# Use LTO only if we see LTO exists and is requested
				if [ $cc_version -lt 46 ]; then
					flags="$flags -flto"
				else
					flags="$flags -flto=jobserver"
				fi
				ldflags="$ldflags -fwhole-program"
				features="$features lto"
			fi
		fi

		has_rdynamic=`$1 -dumpspecs | grep rdynamic`
		if [ -n "$has_rdynamic" ]; then
			# rdynamic is used to get useful stack traces from crash reports.
			flags="$flags -rdynamic"
			ldflags="$ldflags -rdynamic"
		fi
	fi

	eval "$2=\"$flags\""
	eval "$3=\"$cxxflags\""
	eval "$4=\"$ldflags\""
	eval "$5=\"$features\""
}

make_cflags_and_ldflags() {
	# General CFlags for BUILD
	CFLAGS_BUILD="$CFLAGS_BUILD"
	# Special CXXFlags for BUILD
	CXXFLAGS_BUILD="$CXXFLAGS_BUILD"
	# LDFLAGS for BUILD
	LDFLAGS_BUILD="$LDFLAGS_BUILD"
	# FEATURES for BUILD (lto)
	FEATURES_BUILD=""
	# General CFlags for HOST
	CFLAGS="$CFLAGS"
	# Special CXXFlags for HOST
	CXXFLAGS="$CXXFLAGS"
	# Libs to compile. In fact this is just LDFLAGS
	LIBS="-lstdc++"
	# LDFLAGS used for HOST
	LDFLAGS="$LDFLAGS"
	# FEATURES for HOST (lto)
	FEATURES=""

	make_compiler_cflags "$cc_build" "CFLAGS_BUILD" "CXXFLAGS_BUILD" "LDFLAGS_BUILD" "FEATURES_BUILD"
	make_compiler_cflags "$cc_host" "CFLAGS" "CXXFLAGS" "LDFLAGS" "FEATURES"

	CFLAGS="$CFLAGS -D$os"

	if [ "$enable_debug" = "0" ]; then
		# No debug, add default stuff
		OBJS_SUBDIR="release"
		if [ "$os" = "MORPHOS" ]; then
			CFLAGS="-I/gg/os-include -noixemul -fstrict-aliasing -fexpensive-optimizations -mcpu=604 -fno-inline -mstring -mmultiple $CFLAGS"
			LDFLAGS="$LDFLAGS -noixemul"
		fi

		CFLAGS="-O2 -fomit-frame-pointer $CFLAGS"
	else
		OBJS_SUBDIR="debug"

		# Each debug level reduces the optimization by a bit
		if [ $enable_debug -ge 1 ]; then
			CFLAGS="$CFLAGS -g -D_DEBUG"
			if [ "$os" = "PSP" ]; then
				CFLAGS="$CFLAGS -G0"
			fi
		fi
		if [ $enable_debug -ge 2 ]; then
			CFLAGS="$CFLAGS -fno-inline"
		fi
		if [ $enable_debug -ge 3 ]; then
			CFLAGS="$CFLAGS -O0"
		else
			CFLAGS="$CFLAGS -O2"
		fi
	fi

	if [ $enable_debug -le 2 ]; then
		if basename "$cc_host" | grep "gcc" &>/dev/null; then
			# Define only when compiling with GCC. Some GLIBC versions use GNU
			# extensions in a way that breaks build with at least ICC.
			# This requires -O1 or more, so debug level 3 (-O0) is excluded.
			CFLAGS="$CFLAGS -D_FORTIFY_SOURCE=2"

			# Just add -O1 to the tools needed for building.
			CFLAGS_BUILD="$CFLAGS_BUILD -D_FORTIFY_SOURCE=2 -O1"
		fi
	fi

	if [ "$os" = "OSX" ] && [ $cc_version -eq 40 ]; then
		# Apple's GCC 4.0 has a compiler bug for x86_64 with (higher) optimization,
		# wrongly optimizing ^= in loops. This disables the failing optimisation.
		CFLAGS="$CFLAGS -fno-expensive-optimizations"
	fi

	if [ "$enable_profiling" != "0" ]; then
		CFLAGS="$CFLAGS -p"
		LDFLAGS="$LDFLAGS -pg"
	fi

	if [ "$with_threads" = "0" ]; then
		CFLAGS="$CFLAGS -DNO_THREADS"
	fi

	if [ "`echo $1 | cut -c 1-3`" != "icc" ]; then
		if [ "$os" = "CYGWIN" ]; then
			flags="$flags -mwin32"
			LDFLAGS="$LDFLAGS -mwin32"
		fi
		if [ "$os" = "MINGW" ] || [ "$os" = "CYGWIN" ]; then
			if [ $cc_version -lt 46 ]; then
				flags="$flags -mno-cygwin"
				LDFLAGS="$LDFLAGS -mno-cygwin"
			fi

			if [ "$enable_console" != "0" ]; then
				LDFLAGS="$LDFLAGS -Wl,--subsystem,console"
			else
				LDFLAGS="$LDFLAGS -Wl,--subsystem,windows"
			fi

			LIBS="$LIBS -lws2_32 -lwinmm -lgdi32 -ldxguid -lole32 -limm32"

			if [ $cc_version -ge 44 ]; then
				LDFLAGS_BUILD="$LDFLAGS_BUILD -static-libgcc -static-libstdc++"
			fi
			if [ $cc_version -ge 47 ]; then
				CFLAGS="$CFLAGS -mno-ms-bitfields"
			fi
		fi
	fi

	if [ "$os" != "CYGWIN" ] && [ "$os" != "HAIKU" ] && [ "$os" != "FREEBSD" ] && [ "$os" != "OPENBSD" ] && [ "$os" != "MINGW" ] && [ "$os" != "MORPHOS" ] && [ "$os" != "OSX" ] && [ "$os" != "DOS" ] && [ "$os" != "WINCE" ] && [ "$os" != "PSP" ] && [ "$os" != "OS2" ]; then
		LIBS="$LIBS -lpthread"
	fi

	if [ "$os" != "CYGWIN" ] && [ "$os" != "HAIKU" ] && [ "$os" != "MINGW" ] && [ "$os" != "DOS" ] && [ "$os" != "WINCE" ]; then
		LIBS="$LIBS -lc"
	fi
	if [ "$os" = "WINCE" ]; then
		LIBS="$LIBS -lcoredll -lcorelibc -laygshell -lws2 -e WinMainCRTStartup"
	fi
	if [ "$os" = "PSP" ]; then
		CFLAGS="$CFLAGS -I`$psp_config -p`/include"
		LDFLAGS="$LDFLAGS -L`$psp_config -p`/lib"

		CFLAGS="$CFLAGS -fno-exceptions -fno-rtti -D_PSP_FW_VERSION=150"
		LIBS="$LIBS -D_PSP_FW_VERSION=150 -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspsdk -lpspnet -lpspnet_inet -lpspnet_apctl -lpspnet_resolver -lpsputility -lpspuser -lpspkernel -lm"
	fi

	if [ "$os" = "MORPHOS" ]; then
		# -Wstrict-prototypes generates much noise because of system headers
		CFLAGS="$CFLAGS -Wno-strict-prototypes"
	fi

	if [ "$os" = "OPENBSD" ]; then
		LIBS="$LIBS -pthread"
	fi

	if [ "$os" = "FREEBSD" ]; then
		LIBS="$LIBS -lpthread"
	fi

	if [ "$os" = "OSX" ]; then
		LDFLAGS="$LDFLAGS -framework Cocoa"

		# Add macports include dir which is not always set a default system dir. This avoids zillions of bogus warnings.
		CFLAGS="$CFLAGS -isystem/opt/local/include"

		if [ "$enable_dedicated" = "0" ] && ([ "$cpu_type" = "32" ] || [ "$enable_universal" != "0" ]); then
			LIBS="$LIBS -framework QuickTime"
		else
			CFLAGS="$CFLAGS -DNO_QUICKTIME"
		fi

		if [ "$enable_universal" = "0" ]; then
			# Universal builds set this elsewhere
			CFLAGS="$OSX_SYSROOT $CFLAGS"
			LDFLAGS="$OSX_LD_SYSROOT $LDFLAGS"
		fi

		if [ "$enable_universal" = "0" ] && [ $cc_version -ge 40 ]; then
			# Only set the min version when not doing an universal build.
			# Universal builds set the version elsewhere.
			if [ "$cpu_type" = "64" ]; then
				CFLAGS="$CFLAGS -mmacosx-version-min=10.5"
			else
				gcc_cpu=`$cc_host -dumpmachine`
				if [ "`echo $gcc_cpu | cut -c 1-3`" = "ppc" -o "`echo $gcc_cpu | cut -c 1-7`" = "powerpc" ]; then
					# PowerPC build can run on 10.3
					CFLAGS="$CFLAGS -mmacosx-version-min=10.3"
				else
					# Intel is only available starting from 10.4
					CFLAGS="$CFLAGS -mmacosx-version-min=10.4"
				fi
			fi
		fi
	fi

	if [ "$os" = "BEOS" ] || [ "$os" = "HAIKU" ]; then
		LIBS="$LIBS -lmidi -lbe"
	fi

	# Most targets act like UNIX, just with some additions
	if [ "$os" = "BEOS" ] || [ "$os" = "HAIKU" ] || [ "$os" = "OSX" ] || [ "$os" = "MORPHOS" ] || [ "$os" = "FREEBSD" ] || [ "$os" = "OPENBSD" ] || [ "$os" = "NETBSD" ] || [ "$os" = "HPUX" ] || [ "$os" = "SUNOS" ] || [ "$os" = "OS2" ]; then
		CFLAGS="$CFLAGS -DUNIX"
	fi
	# And others like Windows
	if [ "$os" = "MINGW" ] || [ "$os" = "CYGWIN" ] || [ "$os" = "WINCE" ]; then
		CFLAGS="$CFLAGS -DWIN"
	fi

	if [ -n "$allegro_config" ]; then
		CFLAGS="$CFLAGS -DWITH_ALLEGRO"
		CFLAGS="$CFLAGS `$allegro_config --cflags`"
		if [ "$os" != "MINGW" ] && [ "$os" != "CYGWIN" ] && [ "$os" != "WINCE" ]; then
			if [ "$enable_static" != "0" ]; then
				LIBS="$LIBS `$allegro_config --static --libs`"
			else
				LIBS="$LIBS `$allegro_config --libs`"
			fi
		fi
	fi

	if [ -n "$sdl_config" ]; then
		CFLAGS="$CFLAGS -DWITH_SDL"
		# SDL must not add _GNU_SOURCE as it breaks many platforms
		CFLAGS="$CFLAGS `$sdl_config --cflags | sed 's@-D_GNU_SOURCE[^ ]*@@'`"
		if [ "$os" != "MINGW" ] && [ "$os" != "CYGWIN" ] && [ "$os" != "WINCE" ]; then
			if [ "$enable_static" != "0" ]; then
				LIBS="$LIBS `$sdl_config --static-libs`"
			else
				LIBS="$LIBS `$sdl_config --libs`"
			fi
		fi
	fi

	if [ "$with_cocoa" != "0" ]; then
		CFLAGS="$CFLAGS -DWITH_COCOA"
		LIBS="$LIBS -F/System/Library/Frameworks -framework Cocoa -framework Carbon -framework AudioUnit -framework AudioToolbox"

		if [ "$enable_cocoa_quartz" != "0" ]; then
			CFLAGS="$CFLAGS -DENABLE_COCOA_QUARTZ"
		fi

		if [ "$enable_cocoa_quickdraw" != "0" ]; then
			CFLAGS="$CFLAGS -DENABLE_COCOA_QUICKDRAW"
		fi
	fi

	if [ "$with_zlib" != "0" ]; then
		if [ "$enable_static" != "0" ] && [ "$os" != "OSX" ]; then
			LIBS="$LIBS $zlib"
		else
			LIBS="$LIBS -lz"
		fi
		CFLAGS="$CFLAGS -DWITH_ZLIB"
	fi

	if [ -n "$lzma_config" ]; then
		CFLAGS="$CFLAGS -DWITH_LZMA"
		CFLAGS="$CFLAGS `$lzma_config --cflags | tr '\n\r' '  '`"

		if [ "$enable_static" != "0" ]; then
			CFLAGS="$CFLAGS -DLZMA_API_STATIC"
			LIBS="$LIBS `$lzma_config --libs --static | tr '\n\r' '  '`"
		else
			LIBS="$LIBS `$lzma_config --libs | tr '\n\r' '  '`"
		fi
	fi

	if [ "$with_lzo2" != "0" ]; then
		if [ "$enable_static" != "0" ] && [ "$os" != "OSX" ]; then
			LIBS="$LIBS $lzo2"
		else
			LIBS="$LIBS -llzo2"
		fi
		CFLAGS="$CFLAGS -DWITH_LZO"
	fi

	# 64bit machines need -D_SQ64
	if [ "$cpu_type" = "64" ] && [ "$enable_universal" = "0" ]; then
		CFLAGS="$CFLAGS -D_SQ64"
	fi
	CFLAGS="$CFLAGS -I$SCRIPT_SRC_DIR"

	if [ -n "$png_config" ]; then
		CFLAGS="$CFLAGS -DWITH_PNG"
		CFLAGS="$CFLAGS `$png_config --cppflags --I_opts | tr '\n\r' '  '`"

		# The extra flags are unneeded for latest libpng-config, but some versions are so broken...
		if [ "$enable_static" != "0" ]; then
			if [ "$os" = "OSX" ]; then
				LIBS="$LIBS `$png_config --prefix`/lib/libpng.a"
			else
				LIBS="$LIBS `$png_config --static --ldflags | tr '\n\r' '  '`"
			fi
		else
			LIBS="$LIBS `$png_config --ldflags | tr '\n\r' '  '`"
		fi
	fi

	if [ -n "$fontconfig_config" ]; then
		CFLAGS="$CFLAGS -DWITH_FONTCONFIG"
		CFLAGS="$CFLAGS `$fontconfig_config --cflags | tr '\n\r' '  '`"

		if [ "$enable_static" != "0" ]; then
			if [ "$os" = "OSX" ]; then
				# fontconfig_config goes via pkg-config on all systems, which doesn't know --prefix
				# Also, despite the reason we link to the .a file ourself (because we can't use -static), we do need to ask pkg-config about possible other deps
				LIBS="$LIBS `$fontconfig_config --variable=prefix`/lib/libfontconfig.a `$fontconfig_config --libs --static | sed s@-lfontconfig@@`"
			else
				LIBS="$LIBS `$fontconfig_config --libs --static | tr '\n\r' '  '`"
			fi
		else
			LIBS="$LIBS `$fontconfig_config --libs | tr '\n\r' '  '`"
		fi
	fi

	if [ -n "$freetype_config" ]; then
		CFLAGS="$CFLAGS -DWITH_FREETYPE"
		CFLAGS="$CFLAGS `$freetype_config --cflags | tr '\n\r' '  '`"

		if [ "$enable_static" != "0" ]; then
			if [ "$os" = "OSX" ]; then
				LIBS="$LIBS `$freetype_config --prefix`/lib/libfreetype.a"
			else
				# Is it possible to do static with freetype, if so: how?
				LIBS="$LIBS `$freetype_config --libs | tr '\n\r' '  '`"
			fi
		else
			LIBS="$LIBS `$freetype_config --libs | tr '\n\r' '  '`"
		fi
	fi

	if [ -n "$icu_config" ]; then
		CFLAGS="$CFLAGS -DWITH_ICU"
		CFLAGS="$CFLAGS `$icu_config --cppflags | tr '\n\r' '  '`"

		# Some icu-configs have the 'feature' of not adding a space where others do add the space
		if [ "$static_icu" != "0" ]; then
			LIBS="$LIBS `$icu_config --ldflags-searchpath` `($icu_config --ldflags-libsonly; $icu_config --ldflags-layout) | tr '\n\r' '  ' | sed s/licu/lsicu/g`"
		else
			LIBS="$LIBS `$icu_config --ldflags-searchpath` `($icu_config --ldflags-libsonly; $icu_config --ldflags-layout) | tr '\n\r' '  '`"
		fi
	fi


	if [ "$with_direct_music" != "0" ]; then
		CFLAGS="$CFLAGS -DWIN32_ENABLE_DIRECTMUSIC_SUPPORT"
		# GCC 4.0+ doesn't like the DirectX includes (gives tons of
		#  warnings on it we won't be able to fix). For now just
		#  suppress those warnings.
		if [ $cc_version -ge 40 ]; then
			CFLAGS="$CFLAGS -Wno-non-virtual-dtor"
		fi
	fi

	if [ -n "$libtimidity" ]; then
		if [ "$enable_static" != "0" ]; then
			LIBS="$LIBS $libtimidity"
		else
			LIBS="$LIBS -ltimidity"
		fi
		CFLAGS="$CFLAGS -DLIBTIMIDITY"
	fi

	if [ "$with_iconv" != "0" ]; then
		CFLAGS="$CFLAGS -DWITH_ICONV"
		if [ "$link_to_iconv" = "yes" ]; then
			LIBS="$LIBS -liconv"
			if [ "$with_iconv" != "2" ]; then
				CFLAGS="$CFLAGS -I$with_iconv/include"
				LIBS="$LIBS -L$with_iconv/lib"
			fi
		fi

		if [ "$os" != "OSX" ] && [ "$have_non_const_iconv" != "no" ]; then
			CFLAGS="$CFLAGS -DHAVE_NON_CONST_ICONV"
		fi
	fi

	if [ -n "$with_midi" ]; then
		CFLAGS="$CFLAGS -DEXTERNAL_PLAYER=\\\\\"$with_midi\\\\\""
	fi
	if [ -n "$with_midi_arg" ]; then
		CFLAGS="$CFLAGS -DMIDI_ARG=\\\\\"$with_midi_arg\\\\\""
	fi

	if [ "$enable_dedicated" != "0" ]; then
		CFLAGS="$CFLAGS -DDEDICATED"
	fi

	if [ "$enable_unicode" != "0" ]; then
		CFLAGS="$CFLAGS -DUNICODE -D_UNICODE"
	fi

	if [ "$enable_network" != "0" ]; then
		CFLAGS="$CFLAGS -DENABLE_NETWORK"

		if [ "$os" = "BEOS" ]; then
			LDFLAGS="$LDFLAGS -lbind -lsocket"
		fi

		if [ "$os" = "HAIKU" ]; then
			LDFLAGS="$LDFLAGS -lnetwork"
		fi

		if [ "$os" = "SUNOS" ]; then
			LDFLAGS="$LDFLAGS -lnsl -lsocket"
		fi
	fi

	if [ "$enable_static" != "0" ]; then
		# OSX can't handle -static in LDFLAGS
		if [ "$os" != "OSX" ]; then
			LDFLAGS="$LDFLAGS -static"
		fi
	fi

	if [ "$enable_assert" = "0" ]; then
		CFLAGS="$CFLAGS -DNDEBUG"
		CFLAGS_BUILD="$CFLAGS_BUILD -DNDEBUG"
	fi

	if [ "$enable_desync_debug" != "0" ]; then
		CFLAGS="$CFLAGS -DRANDOM_DEBUG"
	fi

	if [ "$enable_osx_g5" != "0" ]; then
		CFLAGS="$CFLAGS -mcpu=G5 -mpowerpc64 -mtune=970 -mcpu=970 -mpowerpc-gpopt"
	fi

	if [ -n "$personal_dir" ]; then
		CFLAGS="$CFLAGS -DWITH_PERSONAL_DIR -DPERSONAL_DIR=\\\\\"$personal_dir\\\\\""
	fi

	if [ -n "$shared_dir" ]; then
		CFLAGS="$CFLAGS -DWITH_SHARED_DIR -DSHARED_DIR=\\\\\"$shared_dir\\\\\""
	fi

	CFLAGS="$CFLAGS -DGLOBAL_DATA_DIR=\\\\\"$prefix_dir/$data_dir\\\\\""

	if [ "$enable_lto" != "0" ]; then
		lto_build=`echo "$FEATURES_BUILD" | grep "lto"`
		lto_host=`echo "$FEATURES" | grep "lto"`
		if [ -z "$lto_build$lto_host" ]; then
			log 1 "WARNING: you enabled LTO/IPO, but neither build nor host compiler supports it"
			log 1 "WARNING: LTO/IPO has been disabled"
		fi
		if [ -n "$lto_build" ]; then
			LDFLAGS_BUILD="$LDFLAGS_BUILD $CFLAGS_BUILD $CXXFLAGS_BUILD"
		fi
		if [ -n "$lto_host" ]; then
			LDFLAGS="$LDFLAGS $CFLAGS $CXXFLAGS"
		fi
	fi

	log 1 "using CFLAGS_BUILD... $CFLAGS_BUILD"
	log 1 "using CXXFLAGS_BUILD... $CXXFLAGS_BUILD"
	log 1 "using LDFLAGS_BUILD... $LDFLAGS_BUILD"
	log 1 "using CFLAGS... $CFLAGS"
	log 1 "using CXXFLAGS... $CXXFLAGS"
	log 1 "using LDFLAGS... $LIBS $LDFLAGS"

	# Makedepend doesn't like something like: -isysroot /OSX/blabla
	#  so convert it to: -isysroot -OSX/blabla. makedepend just ignores
	#  any - command it doesn't know, so we are pretty save.
	# Lovely hackish, not?
	# Btw, this almost always comes from outside the configure, so it is
	#  not something we can control.
	# Also make makedepend aware of compiler's built-in defines.
	if [ "$with_makedepend" != "0" ] || [ "$enable_builtin_depend" != "0" ]; then
		# Append CXXFLAGS possibly containing -std=c++0x
		cflags_makedep="`echo | $cxx_host $CXXFLAGS -E -x c++ -dM - | sed 's@.define @-D@g;s@ .*@ @g;s@(.*)@@g' | tr -d '\r\n'`"

		# Please escape ALL " within ` because e.g. "" terminates the string in some sh implementations
		cflags_makedep="$cflags_makedep `echo \"$CFLAGS\" \"$CXXFLAGS\" | sed 's@ /@ -@g;s@-I[ ]*[^ ]*@@g'`"
	else
		makedepend=""
	fi

	if [ "$with_distcc" != "0" ]; then
		cc_host="$distcc $cc_host"
		cxx_host="$distcc $cxx_host"
		log 1 ""
		log 1 " NOTICE: remind yourself to use 'make -jN' to make use of distcc"
		log 1 ""
	fi

	if [ "$with_ccache" != "0" ]; then
		cc_host="$ccache $cc_host"
		cxx_host="$ccache $cxx_host"
	fi
}

check_compiler() {
	# Params:
	# $1 - Type for message (build / host)
	# $2 - What to fill with the found compiler
	# $3 - System to try
	# $4 - Compiler to try
	# $5 - Env-setting to try
	# $6 - GCC alike to try
	# $7 - CC alike to try
	# $8 - "0" gcc, "1" g++, "2" windres, "3" strip, "4" lipo
	# $9 - What the command is to check for

	if [ -n "$3" ]; then
		# Check for system
		if [ -z "$6" ]; then
			compiler="$3"
		else
			compiler="$3-$6"
		fi
		machine=`eval $compiler $9 2>/dev/null`
		ret=$?
		eval "$2=\"$compiler\""

		log 2 "executing $compiler $9"
		log 2 "  returned $machine"
		log 2 "  exit code $ret"

		if ( [ -z "$machine" ] && [ "$8" != "3" ] ) || [ "$ret" != "0" ]; then
			log 1 "checking $1... $compiler not found"
			log 1 "I couldn't detect any $6 binary for $3"
			exit 1
		fi

		if [ "$machine" != "$3" ] && ( [ "$8" = "0" ] || [ "$8" = "1" ] ); then
			log 1 "checking $1... expected $3, found $machine"
			log 1 "the compiler suggests it doesn't build code for the machine you specified"
			exit 1
		fi
	elif [ -n "$4" ]; then
		# Check for manual compiler
		machine=`$4 $9 2>/dev/null`
		ret=$?
		eval "$2=\"$4\""

		log 2 "executing $4 $9"
		log 2 "  returned $machine"
		log 2 "  exit code $ret"

		if ( [ -z "$machine" ] && [ "$8" != "3" ] ) || [ "$ret" != "0" ]; then
			log 1 "checking $1... $4 not found"
			log 1 "the selected binary doesn't seem to be a $6 binary"
			exit 1
		fi
	else
		# Nothing given, autodetect

		if [ -n "$5" ]; then
			machine=`$5 $9 2>/dev/null`
			ret=$?
			eval "$2=\"$5\""

			log 2 "executing $5 $9"
			log 2 "  returned $machine"
			log 2 "  exit code $ret"

			# The user defined a GCC that doesn't reply to $9.. abort
			if ( [ -z "$machine" ] && [ "$8" != "3" ] ) || [ "$ret" != "0" ]; then
				log 1 "checking $1... $5 unusable"
				log 1 "the CC environment variable is set, but it doesn't seem to be a $6 binary"
				log 1 "please redefine the CC/CXX environment to a $6 binary"
				exit 1
			fi
		else
			log 2 "checking $1... CC/CXX not set (skipping)"

			# No $5, so try '$6'
			machine=`$6 $9 2>/dev/null`
			ret=$?
			eval "$2=\"$6\""

			log 2 "executing $6 $9"
			log 2 "  returned $machine"
			log 2 "  exit code $ret"

			if ( [ -z "$machine" ] && [ "$8" != "3" ] ) || [ "$ret" != "0" ]; then
				# Maybe '$7'?
				machine=`$7 $9 2>/dev/null`
				ret=$?
				eval "$2=\"$7\""

				log 2 "executing $7 $9"
				log 2 "  returned $machine"
				log 2 "  exit code $ret"

				# All failed, abort
				if [ -z "$machine" ]; then
					log 1 "checking $1... $6 not found"
					log 1 "I couldn't detect any $6 binary on your system"
					log 1 "please define the CC/CXX environment to where it is located"

					exit 1
				fi
			fi
		fi
	fi

	if [ "$8" != "0" ]; then
		eval "res=\$$2"
		log 1 "checking $1... $res"
	else
		log 1 "checking $1... $machine"
	fi
}

check_build() {
	if [ "$os" = "FREEBSD" ]; then
		# FreeBSD's C compiler does not support dump machine.
		# However, removing C support is not possible because PSP must be linked with the C compiler.
		check_compiler "build system type" "cc_build" "$build" "$cc_build" "$CXX" "g++" "c++" "0" "-dumpmachine"
	else
		check_compiler "build system type" "cc_build" "$build" "$cc_build" "$CC" "gcc" "cc" "0" "-dumpmachine"
	fi
}

check_host() {
	# By default the host is the build
	if [ -z "$host" ]; then host="$build"; fi

	if [ "$os" = "FREEBSD" ]; then
		# FreeBSD's C compiler does not support dump machine.
		# However, removing C support is not possible because PSP must be linked with the C compiler.
		check_compiler "host system type" "cc_host" "$host" "$cc_host" "$CXX" "g++" "c++" "0" "-dumpmachine"
	else
		check_compiler "host system type" "cc_host" "$host" "$cc_host" "$CC" "gcc" "cc" "0" "-dumpmachine"
	fi
}

check_cxx_build() {
	check_compiler "build c++" "cxx_build" "$build" "$cxx_build" "$CXX" "g++" "c++" 1 "-dumpmachine"
}

check_cxx_host() {
	# By default the host is the build
	if [ -z "$host" ]; then host="$build"; fi
	check_compiler "host c++" "cxx_host" "$host" "$cxx_host" "$CXX" "g++" "c++" 1 "-dumpmachine"
}

check_windres() {
	if [ "$os" = "MINGW" ] || [ "$os" = "CYGWIN" ] || [ "$os" = "WINCE" ]; then
		check_compiler "host windres" "windres" "$host" "$windres" "$WINDRES" "windres" "windres" "2" "-V"
	fi
}

check_strip() {
	if [ "$os" = "OS2" ]; then
		# OS2 via gcc is a bit weird.. stripping HAS to be done via emxbind, which is via gcc directly
		log 1 "checking host strip... using gcc -s option"
	elif [ "$os" = "OSX" ]; then
		# Most targets have -V in strip, to see if they exists... OSX doesn't.. so execute something
		echo "int main(int argc, char *argv[]) { }" > strip.test.c
		$cxx_host strip.test.c -o strip.test
		check_compiler "host strip" "strip" "$host" "$strip" "$STRIP" "strip" "strip" "3" "strip.test"
		rm -f strip.test.c strip.test
	else
		check_compiler "host strip" "strip" "$host" "$strip" "$STRIP" "strip" "strip" "3" "-V"
	fi
}

check_lipo() {
	if [ "$os" = "OSX" ] && [ "$enable_universal" != "0" ]; then
		echo "int main(int argc, char *argv[]) { }" > lipo.test.c
		$cxx_host lipo.test.c -o lipo.test
		check_compiler "host lipo" "lipo" "$host" "$lipo" "$LIPO" "lipo" "lipo" "4" "-info lipo.test"
		rm -f lipo.test.c lipo.test
	fi
}

check_osx_sdk() {
	local sysroot=""
	if [ -n "$1" ]; then
		if echo "$1" | grep -q / ; then
			# Seems to be a file system path
			osx_sdk_path="$1"
		else
			osx_sdk_path="/Developer/SDKs/MacOSX$1.sdk"
		fi
		if [ ! -d "$osx_sdk_path" ]; then
			# No directory, not present or garbage
			return 1
		fi

		# Set minimum version to 10.4 as that's when kCGBitmapByteOrder32Host was introduced
		sysroot="-isysroot $osx_sdk_path -Wl,-syslibroot,$osx_sdk_path -mmacosx-version-min=10.4"
	fi

cat > tmp.osx.mm << EOF
#include <Cocoa/Cocoa.h>
int main() {
	kCGBitmapByteOrder32Host;
	return 0;
}
EOF
	execute="$cxx_host $sysroot $CFLAGS tmp.osx.mm -framework Cocoa -o tmp.osx 2>&1"
	eval $execute > /dev/null
	ret=$?
	log 2 "executing $execute"
	log 2 "  exit code $ret"
	rm -f tmp.osx.mm tmp.osx
	return $ret
}

check_direct_music() {
	echo "
		#include <windows.h>
		#include <dmksctrl.h>
		#include <dmusici.h>
		#include <dmusicc.h>
		#include <dmusicf.h>
		int main(int argc, char *argv[]) { }" > direct_music.test.c
	$cxx_host $CFLAGS direct_music.test.c -o direct_music.test 2> /dev/null
	res=$?
	rm -f direct_music.test.c direct_music.test

	if [ "$res" != "0" ]; then
		if [ "$with_direct_music" != "1" ]; then
			log 1 "configure: error: direct-music is not available on this system"
			exit 1
		fi
		with_direct_music="0"

		log 1 "checking direct-music... not found"
	else
		log 1 "checking direct-music... found"
	fi
}

check_makedepend() {
	if [ "$enable_builtin_depend" != "0" ]; then
		with_makedepend="0"
	fi

	if [ "$with_makedepend" = "0" ]; then
		log 1 "checking makedepend... disabled"
		return
	fi

	if [ "$with_makedepend" = "1" ] || [ "$with_makedepend" = "2" ]; then
		makedepend="makedepend"
	else
		makedepend="$with_makedepend"
	fi

	rm -f makedepend.tmp
	touch makedepend.tmp
	res=`$makedepend -fmakedepend.tmp 2>/dev/null`
	res=$?
	log 2 "executing $makedepend -f makedepend.tmp"
	log 2 "  returned `cat makedepend.tmp`"
	log 2 "  exit code $ret"

	if [ ! -s makedepend.tmp ]; then
		rm -f makedepend.tmp makedepend.tmp.bak

		if [ "$with_makedepend" = "2" ]; then
			log 1 "checking makedepend... not found"

			log 1 "I couldn't detect any makedepend on your system"
			log 1 "please locate it via --makedepend=[binary]"

			exit 1
		elif [ "$with_makedepend" != "1" ]; then
			log 1 "checking makedepend... $makedepend not found"

			log 1 "the selected file doesn't seem to be a valid makedepend binary"

			exit 1
		else
			log 1 "checking makedepend... not found"

			with_makedepend="0"
			return
		fi
	fi

	rm -f makedepend.tmp makedepend.tmp.bak

	log 1 "checking makedepend... $makedepend"
}

check_version() {
	# $1 - requested version (major.minor)
	# $2 - version we got (major.minor)

	if [ -z "$2" ]; then
		return 0
	fi

	req_major=`echo $1 | cut -d. -f1`
	got_major=`echo $2 | cut -d. -f1`
	if [ $got_major -lt $req_major ]; then
		return 0
	elif [ $got_major -gt $req_major ]; then
		return 1
	fi

	req_minor=`echo $1 | cut -d. -f2`
	got_minor=`echo $2 | cut -d. -f2`
	if [ $got_minor -lt $req_minor ]; then
		return 0
	fi
	return 1
}

detect_awk() {
	# Not all awks allow gsub(), so we test for that here! It is in fact all we need...

	# These awks are known to work. Test for them explicit
	awks="gawk mawk nawk"

	awk_prefix="echo \"a.c b.c c.c\" | tr ' ' \\\\n | "
	awk_param="' { ORS = \" \" } /\.c$/   { gsub(\".c$\",   \".o\", \$0); print \$0; }' 2>/dev/null"
	awk_result="a.o b.o c.o "
	log 2 "Detecing awk..."

	log 2 "Trying: $awk_prefix $awk $awk_param"
	res=`eval $awk_prefix $awk $awk_param`
	log 2 "Result: '$res'"
	if [ "$res" != "$awk_result" ] && [ "$awk" = "awk" ]; then
		# User didn't supply his own awk, so try to detect some other known working names for an awk
		for awk in $awks; do
			log 2 "Trying: $awk_prefix $awk $awk_param"
			res=`eval $awk_prefix $awk $awk_param`
			log 2 "Result: '$res'"
			if [ "$res" = "$awk_result" ]; then break; fi
		done

		if [ "$res" != "$awk_result" ]; then
			log 1 "checking awk... not found"
			log 1 "configure: error: no awk found"
			log 1 "configure: error: please install one of the following: $awks"
			exit 1
		fi
	fi
	if [ "$res" != "$awk_result" ]; then
		log 1 "checking awk... not found"
		log 1 "configure: error: you supplied '$awk' but it doesn't seem a valid gawk or mawk"
		exit 1
	fi

	log 1 "checking awk... $awk"
}

detect_os() {
	if [ "$os" = "DETECT" ]; then
		# Detect UNIX, OSX, FREEBSD, OPENBSD, NETBSD, HPUX, MORPHOS, BEOS, SUNOS, CYGWIN, MINGW, OS2, DOS, WINCE, and PSP

		# Try first via dumpmachine, then via uname
		os=`echo "$host" | tr '[A-Z]' '[a-z]' | $awk '
					/linux/        { print "UNIX";    exit}
					/darwin/       { print "OSX";     exit}
					/freebsd/      { print "FREEBSD"; exit}
					/openbsd/      { print "OPENBSD"; exit}
					/netbsd/       { print "NETBSD";  exit}
					/hp-ux/        { print "HPUX";    exit}
					/morphos/      { print "MORPHOS"; exit}
					/beos/         { print "BEOS";    exit}
					/haiku/        { print "HAIKU";   exit}
					/sunos/        { print "SUNOS";   exit}
					/solaris/      { print "SUNOS";   exit}
					/cygwin/       { print "CYGWIN";  exit}
					/mingw/        { print "MINGW";   exit}
					/os2/          { print "OS2";     exit}
					/dos/          { print "DOS";     exit}
					/wince/        { print "WINCE";   exit}
					/psp/          { print "PSP";     exit}
		'`

		if [ -z "$os" ]; then
			os=`LC_ALL=C uname | tr '[A-Z]' '[a-z]' | $awk '
					/linux/        { print "UNIX";    exit}
					/darwin/       { print "OSX";     exit}
					/freebsd/      { print "FREEBSD"; exit}
					/openbsd/      { print "OPENBSD"; exit}
					/netbsd/       { print "NETBSD";  exit}
					/hp-ux/        { print "HPUX";    exit}
					/morphos/      { print "MORPHOS"; exit}
					/beos/         { print "BEOS";    exit}
					/haiku/        { print "HAIKU";   exit}
					/sunos/        { print "SUNOS";   exit}
					/cygwin/       { print "CYGWIN";  exit}
					/mingw/        { print "MINGW";   exit}
					/os\/2/        { print "OS2";     exit}
					/gnu/          { print "UNIX";    exit}
			'`
		fi

		if [ -z "$os" ]; then
			log 1 "detecting OS... none detected"
			log 1 "I couldn't detect your OS. Please use --os=OS to force one"
			log 1 "Allowed values are: UNIX, OSX, FREEBSD, OPENBSD, NETBSD, MORPHOS, HPUX, BEOS, HAIKU, SUNOS, CYGWIN, MINGW, OS2, DOS, WINCE, and PSP"
			exit 1
		fi

		log 1 "detecting OS... $os"
	else
		log 1 "forcing OS... $os"
	fi
}

detect_allegro() {
	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$with_allegro" = "0" ]; then
		log 1 "checking Allegro... disabled"

		allegro_config=""
		return 0
	fi

	if [ "$with_allegro" = "2" ] && [ "$with_cocoa" = "2" ]; then
		log 1 "configure: error: it is impossible to compile both Allegro and COCOA"
		log 1 "configure: error: please deselect one of them and try again"
		exit 1
	fi

	if [ "$with_allegro" = "2" ] && [ "$enable_dedicated" != "0" ]; then
		log 1 "configure: error: it is impossible to compile a dedicated with Allegro"
		log 1 "configure: error: please deselect one of them and try again"
		exit 1
	fi

	if [ "$enable_dedicated" != "0" ]; then
		log 1 "checking Allegro... dedicated server, skipping"

		allegro_config=""
		return 0
	fi

	# By default on OSX we don't use SDL. The rest is auto-detect
	if [ "$with_allegro" = "1" ] && [ "$os" = "OSX" ] && [ "$with_cocoa" != "0" ]; then
		log 1 "checking Allegro... OSX, skipping"

		allegro_config=""
		return 0
	fi

	if [ "$with_allegro" = "1" ] || [ "$with_allegro" = "" ] || [ "$with_allegro" = "2" ]; then
		allegro_config="allegro-config"
	else
		allegro_config="$with_allegro"
	fi

	version=`$allegro_config --version 2>/dev/null`
	ret=$?
	log 2 "executing $allegro_config --version"
	log 2 "  returned $version"
	log 2 "  exit code $ret"

	if [ -z "$version" ] || [ "$ret" != "0" ]; then
		log 1 "checking Allegro... not found"

		# It was forced, so it should be found.
		if [ "$with_allegro" != "1" ]; then
			log 1 "configure: error: allegro-config couldn't be found"
			log 1 "configure: error: you supplied '$with_allegro', but it seems invalid"
			exit 1
		fi

		allegro_config=""
		return 0
	fi

	log 1 "checking Allegro... found"
}


detect_sdl() {
	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$with_sdl" = "0" ]; then
		log 1 "checking SDL... disabled"

		sdl_config=""
		return 0
	fi

	if [ "$with_sdl" = "2" ] && [ "$with_cocoa" = "2" ]; then
		log 1 "configure: error: it is impossible to compile both SDL and COCOA"
		log 1 "configure: error: please deselect one of them and try again"
		exit 1
	fi

	if [ "$with_sdl" = "2" ] && [ "$enable_dedicated" != "0" ]; then
		log 1 "configure: error: it is impossible to compile a dedicated with SDL"
		log 1 "configure: error: please deselect one of them and try again"
		exit 1
	fi

	if [ "$enable_dedicated" != "0" ]; then
		log 1 "checking SDL... dedicated server, skipping"

		sdl_config=""
		return 0
	fi

	# By default on OSX we don't use SDL. The rest is auto-detect
	if [ "$with_sdl" = "1" ] && [ "$os" = "OSX" ] && [ "$with_cocoa" != "0" ]; then
		log 1 "checking SDL... OSX, skipping"

		sdl_config=""
		return 0
	fi

	if [ "$os" = "OSX" ]; then
		log 1 "WARNING: sdl is known to fail on some versions of Mac OS X"
		log 1 "WARNING: with some hardware configurations. Use at own risk!"
		sleep 5
	fi

	if [ "$with_sdl" = "1" ] || [ "$with_sdl" = "" ] || [ "$with_sdl" = "2" ]; then
		sdl_config="sdl-config"
	else
		sdl_config="$with_sdl"
	fi

	version=`$sdl_config --version 2>/dev/null`
	ret=$?
	log 2 "executing $sdl_config --version"
	log 2 "  returned $version"
	log 2 "  exit code $ret"

	if [ -z "$version" ] || [ "$ret" != "0" ]; then
		log 1 "checking SDL... not found"

		# It was forced, so it should be found.
		if [ "$with_sdl" != "1" ]; then
			log 1 "configure: error: sdl-config couldn't be found"
			log 1 "configure: error: you supplied '$with_sdl', but it seems invalid"
			exit 1
		fi

		sdl_config=""
		return 0
	fi

	log 1 "checking SDL... found"
}

detect_osx_sdk() {
	# Try to find the best SDK available. For a normal build this
	# is currently the 10.5 SDK as this is needed to compile all
	# optional code. Because such an executable won't run on 10.4
	# or lower, also check for the 10.4u SDK when doing an universal
	# build.

	# Check for the 10.5 SDK, but try 10.6 if that fails
	check_osx_sdk "10.5" ||	check_osx_sdk "10.6" || osx_sdk_path=""

	if [ -z "$osx_sdk_path" ] || [ "$enable_universal" != "0" ]; then
		# No better SDK or universal build enabled? Check 10.4u SDK as well
		local old_sdk="$osx_sdk_path"
		if check_osx_sdk "10.4u"; then
			osx_sdk_104_path="$osx_sdk_path"
		else
			osx_sdk_104_path=""
		fi
		if [ -z "$old_sdk" ]; then
			osx_sdk_path="$osx_sdk_104_path"
		else
			osx_sdk_path="$old_sdk"
		fi
	fi

	if [ -z "$osx_sdk_path" ]; then
		log 1 "Your system SDK is probably too old"
		log 1 "Please install/upgrade your Xcode to >= 2.5"

		exit 1
	fi
}

detect_cocoa() {
	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$with_cocoa" = "0" ]; then
		log 1 "checking COCOA... disabled"

		return 0
	fi

	if [ "$with_cocoa" = "2" ] && [ "$enable_dedicated" != "0" ]; then
		log 1 "configure: error: it is impossible to compile a dedicated with COCOA"
		log 1 "configure: error: please deselect one of them and try again"
		exit 1
	fi

	if [ "$enable_dedicated" != "0" ]; then
		log 1 "checking COCOA... dedicated server, skipping"

		with_cocoa="0"
		return 0
	fi

	# By default on OSX we use COCOA. The rest doesn't support it
	if [ "$with_cocoa" = "1" ] && [ "$os" != "OSX" ]; then
		log 1 "checking COCOA... not OSX, skipping"

		with_cocoa="0"
		return 0
	fi

	if [ "$os" != "OSX" ]; then
		log 1 "checking COCOA... not OSX"

		log 1 "configure: error: COCOA video driver is only supported for OSX"
		exit 1
	fi

	log 1 "checking COCOA... found"


	if [ "$enable_cocoa_quartz" != "0" ]; then
		log 1 "checking whether to enable the Quartz window subdriver... yes"
	else
		log 1 "checking whether to enable the Quartz window subdriver... no"
	fi

	detect_quickdraw
}

detect_quickdraw() {
	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$enable_cocoa_quickdraw" = "0" ]; then
		log 1 "checking Quickdraw window subdriver... disabled"
		return 0
	fi

	# Assume QuickDraw is available when doing an universal build
	if [ "$enable_universal" != "0" ]; then
		log 1 "checking Quickdraw window subdriver... found"
		return 0
	fi

	# 64 bits doesn't have quickdraw
	if [ "$cpu_type" = "64" ]; then
		enable_cocoa_quickdraw="0"
		log 1 "checking Quickdraw window subdriver... disabled (64 bits)"
		return 0
	fi

cat > tmp.osx.mm << EOF
#include <AvailabilityMacros.h>
#import <Cocoa/Cocoa.h>
int main(int argc, char *argv[]) { SetEmptyRgn(NULL); return 0; }
EOF
	execute="$cxx_host $OSX_SYSROOT $OSX_LD_SYSROOT $CFLAGS -mmacosx-version-min=10.3 tmp.osx.mm -framework Cocoa -o tmp.osx 2>&1"
	eval $execute > /dev/null
	ret=$?
	log 2 "executing $execute"
	log 2 "  exit code $ret"
	rm -f tmp.osx.mm tmp.osx
	if [ "$ret" != "0" ]; then
		log 1 "checking Quickdraw window subdriver... not found"

		# It was forced, so it should be found.
		if [ "$enable_cocoa_quickdraw" != "1" ]; then
			log 1 "configure: error: Quickdraw window driver could not be found"
			exit 1
		fi

		enable_cocoa_quickdraw=0
		return 0
	fi

	enable_cocoa_quickdraw=1
	log 1 "checking Quickdraw window subdriver... found"
}

detect_library() {
	# $1 - config-param ($with_zlib value)
	# $2 - library name ('zlib', sets $zlib)
	# $3 - static library name (libz.a)
	# $4 - header directory ()
	# $5 - header name (zlib.h)
	# $6 - force static (if non-empty)

	if [ -n "$6" ]; then force_static="1"; fi

	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$1" = "0" ]; then
		log 1 "checking $2... disabled"

		eval "$2=\"\""
		return 0
	fi

	log 2 "detecting $2"

	if [ "$1" = "1" ] || [ "$1" = "" ] || [ "$1" = "2" ]; then
		eval "$2=`ls -1 /usr/include/$4*.h 2>/dev/null | egrep \"\/$5\$\"`"
		eval "res=\$$2"
		if [ -z "$res" ]; then
			log 2 "  trying /usr/include/$4$5... no"
			eval "$2=`ls -1 /usr/local/include/$4*.h 2>/dev/null | egrep \"\/$5\$\"`"
		fi
		eval "res=\$$2"
		if [ -z "$res" ]; then
			log 2 "  trying /usr/local/include/$4$5... no"
			eval "$2=`ls -1 /mingw/include/$4*.h 2>/dev/null | egrep \"\/$5\$\"`"
		fi
		eval "res=\$$2"
		if [ -z "$res" ]; then
			log 2 "  trying /mingw/include/$4$5... no"
			eval "$2=`ls -1 /opt/local/include/$4*.h 2>/dev/null | egrep \"\/$5\$\"`"
		fi
		eval "res=\$$2"
		if [ -z "$res" ]; then
			log 2 "  trying /opt/local/include/$4$5... no"
		fi
		if [ -z "$res" ] && [ "$os" = "NETBSD" ]; then
			eval "$2=`ls -1 /usr/pkg/include/$4*.h 2>/dev/null | egrep \"\/$5\$\"`"
			eval "res=\$$2"
			if [ -z "$res" ]; then
				log 2 "  trying /usr/pkg/include/$4$5... no"
			fi
		fi
		if [ -z "$res" ] && [ "$os" = "HAIKU" ]; then
			eval "$2=`ls -1 /boot/common/include/$4*.h 2>/dev/null | egrep \"\/$5\$\"`"
			eval "res=\$$2"
			if [ -z "$res" ]; then
				log 2 "  trying /boot/common/include/$4$5... no"
			fi
		fi

		eval "res=\$$2"
		if [ -n "$res" ] && ( [ -n "$force_static" ] || ( [ "$enable_static" != "0" ] && [ "$os" != "OSX" ] ) ); then
			eval "res=\$$2"
			log 2 "  trying $res... found"
			# Now find the static lib, if needed
			eval "$2=`ls /lib/*.a 2>/dev/null | egrep \"\/$3\$\"`"
			eval "res=\$$2"
			if [ -z "$res" ]; then
				log 2 "  trying /lib/$3... no"
				eval "$2=`ls /usr/lib/*.a 2>/dev/null | egrep \"\/$3\$\"`"
			fi
			eval "res=\$$2"
			if [ -z "$res" ]; then
				log 2 "  trying /usr/lib/$3... no"
				eval "$2=`ls /usr/local/lib/*.a 2>/dev/null | egrep \"\/$3\$\"`"
			fi
			eval "res=\$$2"
			if [ -z "$res" ]; then
				log 2 "  trying /usr/local/lib/$3... no"
				eval "$2=`ls /mingw/lib/*.a 2>/dev/null | egrep \"\/$3\$\"`"
			fi
			eval "res=\$$2"
			if [ -z "$res" ]; then
				log 2 "  trying /mingw/lib/$3... no"
				log 1 "configure: error: $2 couldn't be found"
				log 1 "configure: error: you requested a static link, but I can't find $3"

				exit 1
			fi
		fi
	else
		# Make sure it exists
		if [ -f "$1" ]; then
			eval "$2=`ls $1 2>/dev/null`"
		else
			eval "$2=`ls $1/$3 2>/dev/null`"
		fi
	fi

	eval "res=\$$2"
	if [ -z "$res" ]; then
		log 1 "checking $2... not found"
		if [ "$1" = "2" ]; then
			log 1 "configure: error: $2 couldn't be found"

			exit 1
		elif [ "$1" != "1" ]; then
			log 1 "configure: error: $2 couldn't be found"
			log 1 "configure: error: you supplied '$1', but it seems invalid"

			exit 1
		fi

		eval "with_$2=0"

		return 0
	fi

	eval "res=\$$2"
	log 2 "  trying $res... found"

	log 1 "checking $2... found"
}

detect_zlib() {
	detect_library "$with_zlib" "zlib" "libz.a" "" "zlib.h"
}

detect_lzo2() {
	detect_library "$with_lzo2" "lzo2" "liblzo2.a" "lzo/" "lzo1x.h"
}

detect_libtimidity() {
	detect_library "$with_libtimidity" "libtimidity" "libtimidity.a" "" "timidity.h"
}

detect_lzma() {
	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$with_lzma" = "0" ]; then
		log 1 "checking liblzma... disabled"

		lzma_config=""
		return 0
	fi

	if [ "$with_lzma" = "1" ] || [ "$with_lzma" = "" ] || [ "$with_lzma" = "2" ]; then
		lzma_config="pkg-config liblzma"
	else
		lzma_config="$with_lzma"
	fi

	version=`$lzma_config --modversion 2>/dev/null`
	ret=$?
	log 2 "executing $lzma_config --modversion"
	log 2 "  returned $version"
	log 2 "  exit code $ret"

	if [ -z "$version" ] || [ "$ret" != "0" ]; then
		log 1 "checking liblzma... not found"

		# It was forced, so it should be found.
		if [ "$with_lzma" != "1" ]; then
			log 1 "configure: error: pkg-config liblzma couldn't be found"
			log 1 "configure: error: you supplied '$with_lzma', but it seems invalid"
			exit 1
		fi

		lzma_config=""
		return 0
	fi

	log 1 "checking liblzma... found"
}

detect_png() {
	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$with_png" = "0" ]; then
		log 1 "checking libpng... disabled"

		png_config=""
		return 0
	fi

	if [ "$with_zlib" = "0" ] || [ -z "$zlib" ]; then
		if [ "$with_png" != "1" ]; then
			log 1 "checking libpng... no zlib"
			log 1 "ERROR: libpng was forced, but zlib was not detected / disabled."
			log 1 "ERROR: libpng depends on zlib."

			exit 1
		fi

		log 1 "checking libpng... no zlib, skipping"

		png_config=""
		return 0
	fi

	if [ "$with_png" = "1" ] || [ "$with_png" = "" ] || [ "$with_png" = "2" ]; then
		png_config="libpng-config"
	else
		png_config="$with_png"
	fi

	version=`$png_config --version 2>/dev/null`
	ret=$?
	log 2 "executing $png_config --version"
	log 2 "  returned $version"
	log 2 "  exit code $ret"

	if [ -z "$version" ] || [ "$ret" != "0" ]; then
		log 1 "checking libpng... not found"

		# It was forced, so it should be found.
		if [ "$with_png" != "1" ]; then
			log 1 "configure: error: libpng-config couldn't be found"
			log 1 "configure: error: you supplied '$with_png', but it seems invalid"
			exit 1
		fi

		png_config=""
		return 0
	fi

	log 1 "checking libpng... found"
}

detect_freetype() {
	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$with_freetype" = "0" ]; then
		log 1 "checking libfreetype... disabled"

		freetype_config=""
		return 0
	fi
	if [ "$with_freetype" = "1" ] && [ "$enable_dedicated" != "0" ]; then
		log 1 "checking libfreetype... dedicated server, skipping"

		freetype_config=""
		return 0
	fi

	if [ "$with_zlib" = "0" ] || [ -z "$zlib" ]; then
		if [ "$with_freetype" != "1" ]; then
			log 1 "checking libfreetype... no zlib"
			log 1 "ERROR: libfreetype was forced, but zlib was not detected / disabled."
			log 1 "ERROR: libfreetype depends on zlib."

			exit 1
		fi

		log 1 "checking libfreetype... no zlib, skipping"

		freetype_config=""
		return 0
	fi

	if [ "$with_freetype" = "1" ] || [ "$with_freetype" = "" ] || [ "$with_freetype" = "2" ]; then
		freetype_config="freetype-config"
	else
		freetype_config="$with_freetype"
	fi

	version=`$freetype_config --version 2>/dev/null`
	ret=$?
	log 2 "executing freetype_config --version"
	log 2 "  returned $version"
	log 2 "  exit code $ret"

	if [ -z "$version" ] || [ "$ret" != "0" ]; then
		log 1 "checking libfreetype... not found"

		# It was forced, so it should be found.
		if [ "$with_freetype" != "1" ]; then
			log 1 "configure: error: freetype-config couldn't be found"
			log 1 "configure: error: you supplied '$with_freetype', but it seems invalid"
			exit 1
		fi

		freetype_config=""
		return 0
	fi

	log 1 "checking libfreetype... found"
}

detect_fontconfig() {
	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$with_fontconfig" = "0" ]; then
		log 1 "checking libfontconfig... disabled"

		fontconfig_config=""
		return 0
	fi
	if [ "$with_fontconfig" = "1" ] && [ "$enable_dedicated" != "0" ]; then
		log 1 "checking libfontconfig... dedicated server, skipping"

		fontconfig_config=""
		return 0
	fi
	if [ "$with_fontconfig" != "2" ] && [ -z "$freetype_config" ]; then
		log 1 "checking libfontconfig... no freetype, skipping"

		fontconfig_config=""
		return 0
	fi

	if [ "$os" = "MINGW" ] || [ "$os" = "CYGWIN" ] || [ "$os" = "WINCE" ]; then
		log 1 "checking libfontconfig... WIN32, skipping"
		fontconfig_config=""
		return 0
	fi

	if [ "$os" = "OSX" ]; then
		log 1 "checking libfontconfig... OSX, skipping"
		fontconfig_config=""
		return 0
	fi

	if [ "$with_fontconfig" = "1" ] || [ "$with_fontconfig" = "" ] || [ "$with_fontconfig" = "2" ]; then
		fontconfig_config="pkg-config fontconfig"
	else
		fontconfig_config="$with_fontconfig"
	fi

	version=`$fontconfig_config --modversion 2>/dev/null`
	ret=$?
	check_version '2.3' "$version"
	version_ok=$?
	log 2 "executing $fontconfig_config --modversion"
	log 2 "  returned $version"
	log 2 "  exit code $ret"

	if [ -z "$version" ] || [ "$ret" != "0" ] || [ "$version_ok" != "1" ]; then
		if [ -n "$version" ] && [ "$version_ok" != "1" ]; then
			log 1 "checking libfontconfig... needs at least version 2.3.0, fontconfig NOT enabled"
		else
			log 1 "checking libfontconfig... not found"
		fi

		# It was forced, so it should be found.
		if [ "$with_fontconfig" != "1" ]; then
			log 1 "configure: error: fontconfig-config couldn't be found"
			log 1 "configure: error: you supplied '$with_fontconfig', but it seems invalid"
			exit 1
		fi

		fontconfig_config=""
		return 0
	fi

	log 1 "checking libfontconfig... found"
}

detect_icu() {
	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$with_icu" = "0" ]; then
		log 1 "checking libicu... disabled"

		icu_config=""
		return 0
	fi
	if [ "$with_icu" = "1" ] && [ "$enable_dedicated" != "0" ]; then
		log 1 "checking libicu... dedicated server, skipping"

		icu_config=""
		return 0
	fi

	if [ "$with_icu" = "1" ] || [ "$with_icu" = "" ] || [ "$with_icu" = "2" ]; then
		icu_config="icu-config"
	else
		icu_config="$with_icu"
	fi

	version=`$icu_config --version 2>/dev/null`
	ret=$?
	check_version '3.6' "$version"
	version_ok=$?
	log 2 "executing $icu_config --version"
	log 2 "  returned $version"
	log 2 "  exit code $ret"

	if [ -z "$version" ] || [ "$ret" != "0" ] || [ "$version_ok" != "1" ]; then
		if [ -n "$version" ] && [ "$version_ok" != "1" ]; then
			log 1 "checking libicu... needs at least version 3.6.0, icu NOT enabled"
		else
			log 1 "checking libicu... not found"
		fi

		# It was forced, so it should be found.
		if [ "$with_icu" != "1" ]; then
			log 1 "configure: error: icu-config couldn't be found"
			log 1 "configure: error: you supplied '$with_icuconfig', but it seems invalid"
			exit 1
		fi

		icu_config=""
		return 0
	fi

	log 1 "checking libicu... found"
}

detect_pspconfig() {
	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$with_psp_config" = "0" ]; then
		log 1 "checking psp-config... disabled"

		psp_config=""
		return 0
	fi

	if [ "$with_psp_config" = "1" ] && [ "$os" != "PSP" ]; then
		log 1 "checking psp-config... not PSP, skipping"

		psp_config="";
		return 0
	fi

	if [ "$os" != "PSP" ]; then
		log 1 "checking psp-config... not PSP"

		log 1 "configure: error: psp-config is only supported for PSP"
		exit 1
	fi

	if [ "$with_psp_config" = "1" ] || [ "$with_psp_config" = "" ] || [ "$with_psp_config" = "2" ]; then
		psp_config="psp-config"
	else
		psp_config="$with_psp_config"
	fi

	version=`$psp_config -p 2>/dev/null`
	ret=$?
	log 2 "executing $psp_config -p"
	log 2 "  returned $version"
	log 2 "  exit code $ret"

	if [ -z "$version" ] || [ "$ret" != "0" ]; then
		log 1 "checking psp-config... not found"
		log 1 "configure: error: psp-config couldn't be found"

		# It was forced, so it should be found.
		if [ "$with_psp_config" != "1" ]; then
			log 1 "configure: error: you supplied '$with_psp_config', but it seems invalid"
		fi
		exit 1
	fi

	log 1 "checking psp-config... found"
}

detect_iconv() {
	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$with_iconv" = "0" ]; then
		log 1 "checking iconv... disabled"

		return 0
	fi

	if [ "$with_iconv" = "1" ] && [ "$os" != "OSX" ]; then
		log 1 "checking iconv... not OSX, skipping"
		with_iconv="0"

		return 0
	fi

	# Try to find iconv.h, seems to only thing to detect iconv with

	if [ "$with_iconv" = "1" ] || [ "$with_iconv" = "" ] || [ "$with_iconv" = "2" ]; then
		iconv=`ls -1 /usr/include 2>/dev/null | grep "iconv.h"`
		if [ -z "$iconv" ]; then
			iconv=`ls -1 /usr/local/include 2>/dev/null | grep "iconv.h"`
		fi
	else
		# Make sure it exists
		iconv=`ls $with_iconv/include/iconv.h 2>/dev/null`
	fi

	if [ -z "$iconv" ]; then
		log 1 "checking iconv... not found"
		if [ "$with_iconv" = "2" ]; then
			log 1 "configure: error: iconv couldn't be found"

			exit 1
		elif [ "$with_iconv" != "1" ]; then
			log 1 "configure: error: iconv couldn't be found"
			log 1 "configure: error: you supplied '$with_iconv', but I couldn't detect iconv in it"

			exit 1
		fi

		return 0
	fi

	if [ "$with_iconv" = "1" ]; then
		with_iconv="2"
	fi

	log 2 "found iconv in $iconv"

	log 1 "checking iconv... found"

	# There are different implementations of iconv. The older ones,
	# e.g. SUSv2, pass a const pointer, whereas the newer ones, e.g.
	# IEEE 1003.1 (2004), pass a non-const pointer.

	cat > tmp.iconv.cpp << EOF
#include "src/stdafx.h"
#include <iconv.h>
int main() {
	static char buf[1024];
	iconv_t convd = 0;
	const char *inbuf = "";
	char *outbuf  = buf;
	size_t outlen = 1023;
	size_t inlen  = 0;
	return iconv(convd, &inbuf, &inlen, &outbuf, &outlen);
}
EOF
	execute="$cxx_host $OSX_SYSROOT $CFLAGS -c tmp.iconv.cpp -o tmp.iconv -DTESTING 2>&1"
	eval $execute > /dev/null
	ret=$?
	log 2 "executing $execute"
	log 2 "  exit code $ret"
	if [ "$ret" = "0" ]; then have_non_const_iconv="no"; else have_non_const_iconv="yes"; fi
	log 1 "checking if iconv has non-const inbuf... $have_non_const_iconv"

	cat > tmp.iconv.cpp << EOF
#include "src/stdafx.h"
#include <iconv.h>
int main() {
	static char buf[1024];
	iconv_t convd = 0;
	char *inbuf = "";
	char *outbuf  = buf;
	size_t outlen = 1023;
	size_t inlen  = 0;
	return iconv(convd, &inbuf, &inlen, &outbuf, &outlen);
}
EOF
	execute="$cxx_host $OSX_SYSROOT $OSX_LD_SYSROOT $CFLAGS tmp.iconv.cpp -o tmp.iconv -DTESTING 2>&1"
	eval $execute > /dev/null
	ret=$?
	log 2 "executing $execute"
	log 2 "  exit code $ret"
	if [ "$ret" = "0" ]; then link_to_iconv="no"; else link_to_iconv="yes"; fi
	log 1 "checking whether to link to iconv... $link_to_iconv"
	rm -f tmp.iconv tmp.iconv.cpp
}

_detect_sort() {
	sort_test_in="d
a
c
b"

	sort_test_out="a
b
c
d"

	log 2 "running echo <array> | $1"

	if [ "`echo \"$sort_test_in\" | $1 2>/dev/null`" = "$sort_test_out" ]; then
		sort="$1"
		log 2 "  result was valid"
	else
		log 2 "  result was invalid"
	fi
}

detect_sort() {
	if [ "$with_sort" = "0" ]; then
		log 1 "checking sort... disabled"

		return
	fi

	if [ "$with_sort" = "1" ] || [ "$with_sort" = "2" ]; then
		_detect_sort "sort"
		if [ -z "$sort" ]; then _detect_sort "/sbin/sort"; fi
		if [ -z "$sort" ]; then _detect_sort "/usr/sbin/sort"; fi
		if [ -z "$sort" ]; then _detect_sort "/usr/local/sbin/sort"; fi
		if [ -z "$sort" ]; then _detect_sort "/bin/sort"; fi
		if [ -z "$sort" ]; then _detect_sort "/usr/bin/sort"; fi
		if [ -z "$sort" ]; then _detect_sort "/usr/local/bin/sort"; fi
	else
		_detect_sort "$with_sort"
	fi

	if [ -z "$sort" ]; then
		if [ "$with_sort" = "2" ]; then
			log 1 "checking sort... not found"

			log 1 "configure: error: couldn't detect sort on your system"
			exit 1
		elif [ "$with_sort" != "1" ]; then
			log 1 "checking sort... $with_sort not found"

			log 1 "configure: error: '$with_sort' doesn't look like a sort to me"
			log 1 "configure: error: please verify its location and function and try again"

			exit 1
		else
			log 1 "checking sort... not found"
		fi
	else
		log 1 "checking sort... $sort"
	fi
}

detect_grfcodec() {
	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$with_grfcodec" = "0" ]; then
		log 1 "checking grfcodec... disabled"

		grfcodec=""
		return 0
	fi

	if [ "$with_grfcodec" = "1" ] || [ "$with_grfcodec" = "" ] || [ "$with_grfcodec" = "2" ]; then
		grfcodec="grfcodec"
	else
		grfcodec="$with_grfcodec"
	fi

	version=`$grfcodec -v 2>/dev/null | $awk '{print $3}' | sed 's/[rM]//g;s/-/0/'`
	ret=$?
	log 2 "executing grfcodec -v"
	log 2 "  returned $version"
	log 2 "  exit code $ret"

	if [ -z "$version" ] || [ "$ret" != "0" ] || [ "$version" -lt "949" ]; then
		if [ -n "$version" ] && [ "$version" -lt "949" ]; then
			log 1 "checking grfcodec... needs at least version 6.0.2 (r949), disabled"
		else
			log 1 "checking grfcodec... not found"
		fi

		# It was forced, so it should be found.
		if [ "$with_grfcodec" != "1" ]; then
			log 1 "configure: error: grfcodec couldn't be found"
			log 1 "configure: error: you supplied '$with_grfcodec', but it seems invalid"
			exit 1
		fi

		grfcodec=""
		return 0
	fi

	log 1 "checking grfcodec... found"
}

detect_nforenum() {
	# 0 means no, 1 is auto-detect, 2 is force
	if [ "$with_nforenum" = "0" ]; then
		log 1 "checking nforenum... disabled"

		nforenum=""
		return 0
	fi

	if [ "$with_nforenum" = "1" ] || [ "$with_nforenum" = "" ] || [ "$with_nforenum" = "2" ]; then
		nforenum="nforenum"
	else
		nforenum="$with_nforenum"
	fi

	version=`$nforenum -v 2>/dev/null | $awk '{print $3}' | sed 's/[rM]//g;s/-/0/'`
	ret=$?
	log 2 "executing nforenum -v"
	log 2 "  returned $version"
	log 2 "  exit code $ret"

	if [ -z "$version" ] || [ "$ret" != "0" ] || [ "$version" -lt "949" ]; then
		if [ -n "$version" ] && [ "$version" -lt "949" ]; then
			log 1 "checking nforenum... needs at least version 6.0.2 (r949), disabled"
		else
			log 1 "checking nforenum... not found"
		fi

		# It was forced, so it should be found.
		if [ "$with_nforenum" != "1" ]; then
			log 1 "configure: error: nforenum couldn't be found"
			log 1 "configure: error: you supplied '$with_nforenum', but it seems invalid"
			exit 1
		fi

		nforenum=""
		return 0
	fi

	log 1 "checking nforenum... found"
}

detect_cputype() {
	if [ -n "$cpu_type" ] && [ "$cpu_type" != "DETECT" ]; then
		log 1 "forcing cpu-type... $cpu_type bits"
		return;
	fi
	echo "#define _SQ64 1" > tmp.64bit.cpp
	echo "#include \"src/stdafx.h\"" >> tmp.64bit.cpp
	echo "assert_compile(sizeof(size_t) == 8);" >> tmp.64bit.cpp
	echo "int main() { return 0; }" >> tmp.64bit.cpp
	execute="$cxx_host $CFLAGS tmp.64bit.cpp -o tmp.64bit -DTESTING 2>&1"
	cpu_type="`eval $execute 2>/dev/null`"
	ret=$?
	log 2 "executing $execute"
	log 2 "  returned $cpu_type"
	log 2 "  exit code $ret"
	if [ "$ret" = "0" ]; then cpu_type="64"; else cpu_type="32"; fi
	log 1 "detecting cpu-type... $cpu_type bits"
	rm -f tmp.64bit tmp.64bit.cpp
}

make_sed() {
	T_CFLAGS="$CFLAGS"
	T_CXXFLAGS="$CXXFLAGS"
	T_LDFLAGS="$LDFLAGS"

	SRC_OBJS_DIR="$BASE_SRC_OBJS_DIR/$OBJS_SUBDIR"

	# All the data needed to compile a single target
	#  Make sure if you compile multiple targets to
	#  use multiple OBJS_DIR, because all in-between
	#  binaries are stored in there, and nowhere else.
	SRC_REPLACE="
		s@!!CC_HOST!!@$cc_host@g;
		s@!!CXX_HOST!!@$cxx_host@g;
		s@!!CC_BUILD!!@$cc_build@g;
		s@!!CXX_BUILD!!@$cxx_build@g;
		s@!!WINDRES!!@$windres@g;
		s@!!STRIP!!@$strip $strip_arg@g;
		s@!!LIPO!!@$lipo@g;
		s@!!CFLAGS!!@$T_CFLAGS@g;
		s@!!CFLAGS_BUILD!!@$CFLAGS_BUILD@g;
		s@!!CXXFLAGS!!@$T_CXXFLAGS@g;
		s@!!CXXFLAGS_BUILD!!@$CXXFLAGS_BUILD@g;
		s@!!STRGEN_FLAGS!!@$strgen_flags@g;
		s@!!LIBS!!@$LIBS@g;
		s@!!LDFLAGS!!@$T_LDFLAGS@g;
		s@!!LDFLAGS_BUILD!!@$LDFLAGS_BUILD@g;
		s@!!BIN_DIR!!@$BIN_DIR@g;
		s@!!ROOT_DIR!!@$ROOT_DIR@g;
		s@!!MEDIA_DIR!!@$MEDIA_DIR@g;
		s@!!SOURCE_LIST!!@$SOURCE_LIST@g;
		s@!!SRC_OBJS_DIR!!@$SRC_OBJS_DIR@g;
		s@!!LANG_OBJS_DIR!!@$LANG_OBJS_DIR@g;
		s@!!GRF_OBJS_DIR!!@$GRF_OBJS_DIR@g;
		s@!!SETTING_OBJS_DIR!!@$SETTING_OBJS_DIR@g;
		s@!!SRC_DIR!!@$SRC_DIR@g;
		s@!!SCRIPT_SRC_DIR!!@$SCRIPT_SRC_DIR@g;
		s@!!OSXAPP!!@$OSXAPP@g;
		s@!!LANG_DIR!!@$LANG_DIR@g;
		s@!!TTD!!@$TTD@g;
		s@!!BINARY_DIR!!@$prefix_dir/$binary_dir@g;
		s@!!DATA_DIR!!@$prefix_dir/$data_dir@g;
		s@!!DOC_DIR!!@$prefix_dir/$doc_dir@g;
		s@!!MAN_DIR!!@$prefix_dir/$man_dir@g;
		s@!!ICON_DIR!!@$prefix_dir/$icon_dir@g;
		s@!!ICON_THEME_DIR!!@$prefix_dir/$icon_theme_dir@g;
		s@!!PERSONAL_DIR!!@$personal_dir@g;
		s@!!SHARED_DIR!!@$shared_dir@g;
		s@!!INSTALL_DIR!!@$install_dir@g;
		s@!!BINARY_NAME!!@$binary_name@g;
		s@!!STRGEN!!@$STRGEN@g;
		s@!!ENDIAN_CHECK!!@$ENDIAN_CHECK@g;
		s@!!DEPEND!!@$DEPEND@g;
		s@!!SETTINGSGEN!!@$SETTINGSGEN@g;
		s@!!ENDIAN_FORCE!!@$endian@g;
		s@!!STAGE!!@$STAGE@g;
		s@!!MAKEDEPEND!!@$makedepend@g;
		s@!!CFLAGS_MAKEDEP!!@$cflags_makedep@g;
		s@!!SORT!!@$sort@g;
		s@!!CONFIG_CACHE_COMPILER!!@config.cache.compiler@g;
		s@!!CONFIG_CACHE_LINKER!!@config.cache.linker@g;
		s@!!CONFIG_CACHE_ENDIAN!!@config.cache.endian@g;
		s@!!CONFIG_CACHE_SOURCE!!@config.cache.source@g;
		s@!!CONFIG_CACHE_VERSION!!@config.cache.version@g;
		s@!!CONFIG_CACHE_SOURCE_LIST!!@config.cache.source.list@g;
		s@!!CONFIG_CACHE_PWD!!@config.cache.pwd@g;
		s@!!LANG_SUPPRESS!!@$lang_suppress@g;
		s@!!OBJS_C!!@$OBJS_C@g;
		s@!!OBJS_CPP!!@$OBJS_CPP@g;
		s@!!OBJS_MM!!@$OBJS_MM@g;
		s@!!OBJS_RC!!@$OBJS_RC@g;
		s@!!SRCS!!@$SRCS@g;
		s@!!OS!!@$os@g;
		s@!!CONFIGURE_FILES!!@$CONFIGURE_FILES@g;
		s@!!AWK!!@$awk@g;
		s@!!DISTCC!!@$distcc@g;
		s@!!NFORENUM!!@$nforenum@g;
		s@!!GRFCODEC!!@$grfcodec@g;
	"

	if [ "$icon_theme_dir" != "" ]; then
		SRC_REPLACE="$SRC_REPLACE
			s@!!ICON_THEME_DIR!!@$prefix_dir/$icon_theme_dir@g;
		"
	else
		SRC_REPLACE="$SRC_REPLACE
			s@!!ICON_THEME_DIR!!@@g;
		"
	fi

	if [ "$man_dir" != "" ]; then
		SRC_REPLACE="$SRC_REPLACE
			s@!!MAN_DIR!!@$prefix_dir/$man_dir@g;
		"
	else
		SRC_REPLACE="$SRC_REPLACE
			s@!!MAN_DIR!!@@g;
		"
	fi

	if [ "$menu_dir" != "" ]; then
		SRC_REPLACE="$SRC_REPLACE
			s@!!MENU_DIR!!@$prefix_dir/$menu_dir@g;
		"
	else
		SRC_REPLACE="$SRC_REPLACE
			s@!!MENU_DIR!!@@g;
		"
	fi
}

generate_menu_item() {
	MENU_REPLACE="
		s@!!TTD!!@$TTD@g;
		s@!!MENU_GROUP!!@$menu_group@g;
		s@!!MENU_NAME!!@$menu_name@g
	"
	log 1 "Generating menu item..."
	mkdir -p media
	< $ROOT_DIR/media/openttd.desktop.in sed "$MENU_REPLACE" > media/openttd.desktop
}

generate_main() {
	STAGE="[MAIN]"

	make_sed

	# Create the main Makefile
	log 1 "Generating Makefile..."
	echo "# Auto-generated file from 'Makefile.in' -- DO NOT EDIT" > Makefile
	< $ROOT_DIR/Makefile.in sed "$SRC_REPLACE" >> Makefile
	cp $ROOT_DIR/Makefile.bundle.in Makefile.bundle
	echo "# Auto-generated file -- DO NOT EDIT" > Makefile.am
	echo >> Makefile.am
	# Make the copy of the source-list, so we don't trigger an unwanted recompile
	cp $SOURCE_LIST config.cache.source.list
	# Add the current directory, so we don't trigger an unwanted recompile
	echo "`pwd`" > config.cache.pwd
	# Make sure config.cache is OLDER then config.cache.source.list
	touch config.cache
	touch config.pwd

	if [ "$menu_dir" != "" ]; then
		generate_menu_item
	fi
}

generate_lang() {
	STAGE="[LANG]"

	make_sed

	# Create the language file
	mkdir -p $LANG_OBJS_DIR

	log 1 "Generating lang/Makefile..."
	echo "# Auto-generated file from 'Makefile.lang.in' -- DO NOT EDIT" > $LANG_OBJS_DIR/Makefile
	< $ROOT_DIR/Makefile.lang.in sed "$SRC_REPLACE" >> $LANG_OBJS_DIR/Makefile
	echo "DIRS += $LANG_OBJS_DIR" >> Makefile.am
	echo "LANG_DIRS += $LANG_OBJS_DIR" >> Makefile.am
}

generate_settings() {
	STAGE="[SETTING]"

	make_sed

	# Create the language file
	mkdir -p $SETTING_OBJS_DIR

	log 1 "Generating setting/Makefile..."
	echo "# Auto-generated file from 'Makefile.settings.in' -- DO NOT EDIT" > $SETTING_OBJS_DIR/Makefile
	< $ROOT_DIR/Makefile.setting.in sed "$SRC_REPLACE" >> $SETTING_OBJS_DIR/Makefile
	echo "DIRS += $SETTING_OBJS_DIR" >> Makefile.am
}

generate_grf() {
	STAGE="[BASESET]"

	make_sed

	# Create the language file
	mkdir -p $GRF_OBJS_DIR

	log 1 "Generating grf/Makefile..."
	echo "# Auto-generated file from 'Makefile.grf.in' -- DO NOT EDIT" > $GRF_OBJS_DIR/Makefile
	< $ROOT_DIR/Makefile.grf.in sed "$SRC_REPLACE" >> $GRF_OBJS_DIR/Makefile
	echo "DIRS += $GRF_OBJS_DIR" >> Makefile.am
}

generate_src_normal() {
	STAGE=$1

	make_sed

	# Create the source file
	mkdir -p $SRC_OBJS_DIR

	log 1 "Generating $2/Makefile..."
	echo "# Auto-generated file from 'Makefile.src.in' -- DO NOT EDIT" > $SRC_OBJS_DIR/Makefile
	< $ROOT_DIR/Makefile.src.in sed "$SRC_REPLACE" >> $SRC_OBJS_DIR/Makefile
	echo "DIRS += $SRC_OBJS_DIR" >> Makefile.am
	echo "SRC_DIRS += $SRC_OBJS_DIR" >> Makefile.am
}

generate_src_osx() {
	cc_host_orig="$cc_host"
	cxx_host_orig="$cxx_host"
	CFLAGS_orig="$CFLAGS"
	LDFLAGS_orig="$LDFLAGS"

	for type in $enable_universal; do

		if [ -n "$osx_sdk_104_path" ]; then
			# Use 10.4 SDK for 32-bit targets
			CFLAGS="-isysroot $osx_sdk_104_path $CFLAGS_orig"
			LDFLAGS="-Wl,-syslibroot,$osx_sdk_104_path $LDFLAGS_orig"
		fi

		# We don't want to duplicate the x86_64 stuff for each target, so do it once here
		if [ "$type" = "ppc64" ] || [ "$type" = "x86_64" ]; then
			# 64 bits is always 10.5 or higher. Furthermore it has a non const ICONV
			# and they also removed support for QuickTime/QuickDraw
			if [ -n "$osx_sdk_path" ]; then
				CFLAGS="-isysroot $osx_sdk_path $CFLAGS_orig"
				LDFLAGS="-Wl,-syslibroot,$osx_sdk_path $LDFLAGS_orig"
			fi
			CFLAGS="$CFLAGS -D_SQ64 -DNO_QUICKTIME -UENABLE_COCOA_QUICKDRAW"
			LIBS="`echo $LIBS | sed 's/-framework QuickTime//'`"
		fi

		case $type in
			ppc)
				BASE_SRC_OBJS_DIR="$OBJS_DIR/ppc"
				cc_host="$cc_host_orig -arch ppc -mmacosx-version-min=10.3"
				cxx_host="$cxx_host_orig -arch ppc -mmacosx-version-min=10.3"
				generate_src_normal "[ppc]" "objs/ppc";;
			ppc970)
				BASE_SRC_OBJS_DIR="$OBJS_DIR/ppc970"
				cc_host="$cc_host_orig -arch ppc970 -mmacosx-version-min=10.3 -mcpu=G5 -mpowerpc64 -mtune=970 -mcpu=970 -mpowerpc-gpopt"
				cxx_host="$cxx_host_orig -arch ppc970 -mmacosx-version-min=10.3 -mcpu=G5 -mpowerpc64 -mtune=970 -mcpu=970 -mpowerpc-gpopt"
				generate_src_normal "[ppc970]" "objs/ppc970";;
			i386)
				BASE_SRC_OBJS_DIR="$OBJS_DIR/i386"
				cc_host="$cc_host_orig -arch i386 -mmacosx-version-min=10.4"
				cxx_host="$cxx_host_orig -arch i386 -mmacosx-version-min=10.4"
				generate_src_normal "[i386]" "objs/i386";;
			ppc64)
				BASE_SRC_OBJS_DIR="$OBJS_DIR/ppc64"
				cc_host="$cc_host_orig -arch ppc64 -mmacosx-version-min=10.5"
				cxx_host="$cxx_host_orig -arch ppc64 -mmacosx-version-min=10.5"
				generate_src_normal "[ppc64]" "objs/ppc64";;
			x86_64)
				BASE_SRC_OBJS_DIR="$OBJS_DIR/x86_64"
				cc_host="$cc_host_orig -arch x86_64 -mmacosx-version-min=10.5"
				cxx_host="$cxx_host_orig -arch x86_64 -mmacosx-version-min=10.5"
				generate_src_normal "[x86_64]" "objs/x86_64";;
			*) log 1 "Unknown architecture requested for universal build: $type";;
		esac
	done
}

generate_src() {
	if [ "$os" = "OSX" ] && [ "$enable_universal" != "0" ]; then
		generate_src_osx
	else
		generate_src_normal "[SRC]" "objs"
	fi
}

showhelp() {
	echo "'configure' configures OpenTTD."
	echo ""
	echo "Usage: $0 [OPTION]... [VAR=VALUE]..."
	echo ""
	echo "To assign environment variables (e.g., CC, CFLAGS...), specify them as"
	echo "VAR=VALUE.  See below for descriptions of some of the useful variables."
	echo ""
	echo "Defaults for the options are specified in brackets."
	echo ""
	echo "Configuration:"
	echo "  -h, --help                     display this help and exit"
	echo ""
	echo "System types:"
	echo "  --build=BUILD                  configure for building on BUILD [guessed]"
	echo "  --host=HOST                    cross-compile to build programs to run"
	echo "                                 on HOST [BUILD]"
	echo "  --windres=WINDRES              the windres to use [HOST-windres]"
	echo "  --strip=STRIP                  the strip to use [HOST-strip]"
	echo "  --awk=AWK                      the awk to use in configure [awk]"
	echo "  --lipo=LIPO                    the lipo to use (OSX ONLY) [HOST-lipo]"
	echo "  --os=OS                        the OS we are compiling for [DETECT]"
	echo "                                 DETECT/UNIX/OSX/FREEBSD/OPENBSD/NETBSD/"
	echo "                                 MORPHOS/HPUX/BEOS/SUNOS/CYGWIN/MINGW/OS2/"
	echo "                                 DOS/WINCE/PSP/HAIKU"
	echo "  --endian=ENDIAN                set the endian of the HOST (AUTO/LE/BE)"
	echo ""
	echo "Paths:"
	echo "  --prefix-dir=dir               specifies the prefix for all installed"
	echo "                                 files [/usr/local]"
	echo "  --binary-dir=dir               location of the binary. Will be prefixed"
	echo "                                 with the prefix-dir [games]"
	echo "  --data-dir=dir                 location of data files (lang, data, gm)."
	echo "                                 Will be prefixed with the prefix-dir"
	echo "                                 [share/games/openttd]"
	echo "  --doc-dir=dir                  location of the doc files"
	echo "                                 Will be prefixed with the prefix-dir"
	echo "                                 [$doc_dir]"
	echo "  --icon-dir=dir                 location of icons. Will be prefixed"
	echo "                                 with the prefix-dir [share/pixmaps]"
	echo "  --icon-theme-dir=dir           location of icon theme."
	echo "                                 Will be prefixed with the prefix-dir"
	echo "                                 and postfixed with size-dirs [$icon_theme_dir]"
	echo "  --man-dir=dir                  location of the manual page (UNIX only)"
	echo "                                 Will be prefixed with the prefix-dir"
	echo "                                 [$man_dir]"
	echo "  --menu-dir=dir                 location of the menu item. (UNIX only, except OSX)"
	echo "                                 Will be prefixed with the prefix-dir"
	echo "                                 [share/applications]"
	echo "  --personal-dir=dir             location of the personal directory"
	echo "                                 [os-dependent default]"
	echo "  --shared-dir=dir               location of shared data files"
	echo "                                 [os-dependent default]"
	echo "  --install-dir=dir              specifies the root to install to."
	echo "                                 Useful to install into jails [/]"
	echo "  --binary-name                  the name used for the binary, icons,"
	echo "                                 desktop file, etc. when installing [openttd]"
	echo ""
	echo "Features and packages:"
	echo "  --enable-debug[=LVL]           enable debug-mode (LVL=[0123], 0 is release)"
	echo "  --enable-desync-debug=[LVL]    enable desync debug options (LVL=[012], 0 is none"
	echo "  --enable-profiling             enables profiling"
	echo "  --enable-lto                   enables GCC's Link Time Optimization (LTO)/ICC's"
	echo "                                 Interprocedural Optimization if available"
	echo "  --enable-dedicated             compile a dedicated server (without video)"
	echo "  --enable-static                enable static compile (doesn't work for"
	echo "                                 all HOSTs)"
	echo "  --enable-translator            enable extra output for translators"
	echo "  --enable-universal[=ARCH]      enable universal builds (OSX ONLY). Allowed is any combination"
	echo "                                 of architectures: i386 ppc ppc970 ppc64 x86_64"
	echo "                                 Default architectures are: i386 ppc"
	echo "  --enable-osx-g5                enables optimizations for ppc970 (G5) (OSX ONLY)"
	echo "  --disable-cocoa-quartz         disable the quartz window mode driver for Cocoa (OSX ONLY)"
	echo "  --disable-cocoa-quickdraw      disable the quickdraw window mode driver for Cocoa (OSX ONLY)"
	echo "  --disable-unicode              disable unicode support to build win9x"
	echo "                                 version (Win32 ONLY)"
	echo "  --enable-console               compile as a console application instead of as a GUI application."
	echo "                                 If this setting is active, debug output will appear in the same"
	echo "                                 console instead of opening a new window. (Win32 ONLY)"
	echo "  --disable-network              disable network support"
	echo "  --disable-assert               disable asserts (continue on errors)"
	echo "  --enable-strip                 enable any possible stripping"
	echo "  --without-osx-sysroot          disable the automatic adding of sysroot "
	echo "                                 (OSX ONLY)"
	echo "  --without-application-bundle   disable generation of application bundle"
	echo "                                 (OSX ONLY)"
	echo "  --without-menu-entry           Don't generate a menu item (Freedesktop based only)"
	echo "  --menu-group=group             Category in which the menu item will be placed (Freedesktop based only)"
	echo "  --menu-name=name               Name of the menu item when placed [OpenTTD]"
	echo "  --with-direct-music            enable direct music support (Win32 ONLY)"
	echo "  --with-sort=sort               define a non-default location for sort"
	echo "  --with-midi=midi               define which midi-player to use"
	echo "  --with-midi-arg=arg            define which args to use for the"
	echo "                                 midi-player"
	echo "  --with-libtimidity             enables libtimidity support"
	echo "  --with-allegro[=allegro-config]"
	echo "                                 enables Allegro video driver support"
	echo "  --with-cocoa                   enables COCOA video driver (OSX ONLY)"
	echo "  --with-sdl[=sdl-config]        enables SDL video driver support"
	echo "  --with-zlib[=zlib.a]           enables zlib support"
	echo "  --with-liblzma[=liblzma.a]     enables liblzma support"
	echo "  --with-liblzo2[=liblzo2.a]     enables liblzo2 support"
	echo "  --with-png[=libpng-config]     enables libpng support"
	echo "  --with-freetype[=freetype-config]"
	echo "                                 enables libfreetype support"
	echo "  --with-fontconfig[=pkg-config fontconfig]"
	echo "                                 enables fontconfig support"
	echo "  --with-icu[=icu-config]        enables icu (used for right-to-left support)"
	echo "  --static-icu                   try to link statically (libsicu instead of"
	echo "                                 libicu; can fail as the new name is guessed)"
	echo "  --with-iconv[=iconv-path]      enables iconv support"
	echo "  --with-psp-config[=psp-config] enables psp-config support (PSP ONLY)"
	echo "  --disable-builtin-depend       disable use of builtin deps finder"
	echo "  --with-makedepend[=makedepend] enables makedepend support"
	echo "  --with-ccache                  enables ccache support"
	echo "  --with-distcc                  enables distcc support"
	echo "  --without-grfcodec             disable usage of grfcodec and re-generation of base sets"
	echo ""
	echo "Some influential environment variables:"
	echo "  CC                             C compiler command"
	echo "  CXX                            C++ compiler command"
	echo "  CFLAGS                         C compiler flags"
	echo "  CXXFLAGS                       C++ compiler flags"
	echo "  WINDRES                        windres command"
	echo "  LDFLAGS                        linker flags, e.g. -L<lib dir> if you"
	echo "                                 have libraries in a nonstandard"
	echo "                                 directory <lib dir>"
	echo "  CFLAGS_BUILD                   C compiler flags for build time tool generation"
	echo "  CXXFLAGS_BUILD                 C++ compiler flags for build time tool generation"
	echo "  LDFLAGS_BUILD                  linker flags for build time tool generation"
	echo ""
	echo "Use these variables to override the choices made by 'configure' or to help"
	echo "it to find libraries and programs with nonstandard names/locations."
}