File: bootstrap.sh

package info (click to toggle)
3depict 0.0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 17,908 kB
  • sloc: cpp: 72,771; sh: 6,497; xml: 1,576; python: 326; makefile: 194; ansic: 186
file content (1967 lines) | stat: -rwxr-xr-x 49,393 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
#!/bin/bash

#Script to bootstrap 3Depict cross-compilation under debian
# and debian-like systems	
# Note that building wx requires ~8GB of ram (or swap)

# Its unlikely that this script will work first-time, or even second-time
# you will need to do quite some work to be able to make this run

#--- Determine which system we wish to build for
#HOST_VAL=x86_64-w64-mingw32 #For mingw64 (windows 64 bit)
#HOST_VAL=i686-w64-mingw32 #For mingw32 (Windows 32 bit)

#Setup clone of hg repository, if required
if [ ! -d code ] || [ ! -d code/3Depict ]  ; then
	read -p "Code dir is missing -- shall we clone it?? (y/n):" answer
	
	case $answer in
		y | Y ) 
			if [ x`which hg` == x"" ] ; then
				echo "No mercurial.. Can't clone, aborting."
				exit 1
			fi

			DEFCLONEURL="http://hg.code.sf.net/p/threedepict/code"

			read -p "Path to clone (include protocol if url...), blank for default" clonedir
		
			if [ x"$clonedir " == x"" ] ; then 
				CLONEURL=$DEFCLONEURL
			else
				CLONEURL=$clonedir	
			fi

			mkdir code  || { echo "failed to create code dir" ; exit 1 ; }
			pushd code
			
			#Clone code
			hg clone $CLONEURL  3Depict || { echo "Failed to clone" ; exit 1 ; }
			popd

			#obtain patches, and then put it into place
			cp -R code/3Depict/packaging/mingw-debian-cross/patches . || { echo "Failed to obtain patches from mingw packging dir" ; exit 1 ; }



			;; 
		n | N )
			echo "You said no."
			;;
		*) 
			echo "Aborting - please answer Y, or N, or create the \"code\" directory, with "
			exit 1
			;;
	esac #ends the case list
fi

if [ ! -f host_val ] ; then
	echo "Please select 32 or 64 bit by typing \"32\" or \"64\" (32/64)"
	read HOST_VAL
	
	case $HOST_VAL in
		32)
			HOST_VAL="i686-w64-mingw32"
			;;
		64)
			HOST_VAL="x86_64-w64-mingw32"
			;;
		
		*) 
			echo "Didn't understand HOST_VAL. You can override this by editing the script"
			exit 1
		;;
	esac

	#Save for next run
	echo $HOST_VAL > host_val
else
	HOST_VAL=`cat host_val`
fi


if [ $HOST_VAL != "x86_64-w64-mingw32" ] && [ $HOST_VAL != i686-w64-mingw32 ] ; then
	echo "Unknown HOST_VAL"
	exit 1
else 
	case $HOST_VAL in
		x86_64-w64-mingw32)
			BITS_VAL=64
			;;
		i686-w64-mingw32)
			BITS_VAL=32
			;;
		*)
			echo "Should not have got here - bug!"
			exit 1
			;;
	esac
fi

#----

if [ ! -d code/3Depict ] || [ ! -f code/3Depict/src/3Depict.cpp ] ; then
	echo "3Depict code dir, \"code/3Depict\", appears to be missing. Please place 3Depict source code in this location"
	echo "Aborting"
	exit 1
fi


BASE=`pwd`
PREFIX=/
NUM_PROCS=4

IS_RELEASE=0

if [ `id -u` -eq 0 ]; then
	echo "This script should not be run as root."
	echo " If you know what you are doing, you can disable this check, but be aware you _can_ break your system by doing this."
	exit 1;
fi
#2) own patch for fixing wx-config's lack of sysroot support
#PATCHES_WXWIDGETS_PRE="wx_changeset_76890.diff"
PATCHES_WXWIDGETS_POST="wx-config-sysroot.patch"
#1) Zlib no longer needs to explicitly link libc, and will fail if it tries
PATCHES_ZLIB="zlib-no-lc.patch"
#1) Override some configure patches to bypass false positive failures
PATCHES_FTGL="ftgl-disable-doc"
PATCHES_FTGL_POSTCONF="ftgl-override-configure-2"

#Fix compilation error with iconv, where alias2_lookup function
# is incorrectly declared as inline on non GNUC builds
PATCHES_ICONV="iconv-fix-alias2.patch"

#1) gettext-tools fails in various places, but we don't actually need it, so turn it off
PATCHES_GETTEXT="gettext-fix-configure-versions gettext-disable-tools"   

PATCHES_GLEW="glew-makefile.base glew-disable-dllentrypoint"

PATCHES_MATHGL="mathgl-disable-things"

PATCHES_QHULL="qhull-ptr.patch" # qhull-2009-configure_ac.patch"

#Vigra's cmakelists assumes you can execute compiled binaries on the host
# this obviously won't work for cross compilation
PATCHES_LIBVIGRA="vigra-cpp-version.patch"

PATCH_LIST="$PATCHES_WXWIDGETS_POST $PATCHES_GSL $PATCHES_ZLIB $PATCHES_LIBPNG $PATCHES_GETTEXT $PATCHES_FTGL $PATCHES_GLEW $PATCHES_MATHGL $PATCHES_FTGL_POSTCONF $PATCHES_ICONV $PATCHES_QHULL $PATCHES_LIBVIGRA"


BUILD_STATUS_FILE="$BASE/build-status"
PATCH_STATUS_FILE="$BASE/patch-status"
PATCH_LEVEL=1

function isBuilt()
{
	if [ x`cat ${BUILD_STATUS_FILE} | grep $ISBUILT_ARG` != x"" ]  ; then
		ISBUILT=1
	else
		ISBUILT=0
	fi
}

function applyPatches()
{
	for i in $APPLY_PATCH_ARG
	do
		if [ x"`cat $PATCH_STATUS_FILE | grep -x "$i"`" != x"" ] ; then
			echo "Patch already applied :" $i
			continue
		fi

		echo "Applying patch:" $i
		patch -tN -p$PATCH_LEVEL  < $BASE/patches/$i
		
		if [ $? -ne 0 ] ; then
			echo "Failed applying patch :" $i
			exit 1
		fi
		echo "applied patch"
		echo $i >> $PATCH_STATUS_FILE
	done
}

function install_mingw()
{

	echo "Checking mingw install"
	#install mingw and libtool (we will need it...)

	GET_PACKAGES="";
	for i in $MINGW_PACKAGES
	do
		if [ x`dpkg --get-selections | grep ^$i | awk '{print $1}'  ` != x"$i" ] ; then
			GET_PACKAGES="$GET_PACKAGES $i";
		fi
	done

	if [ x"$GET_PACKAGES" != x"" ] ; then
		echo "Requesting install of mingw :" $GET_PACKAGES
		sudo apt-get install $GET_PACKAGES libtool || { echo "Mingw install failed";  exit 1 ; }
	fi

}

function grabDeps()
{
	pushd deps 2>/dev/null

	DEB_PACKAGES="qhull expat freetype ftgl gettext gsl libxml2 mathgl tiff zlib glew libvigraimpex libpng-dev"
	if [ x$DIST_NAME == x"Ubuntu" ] || [ x$DIST_NAME == x"LinuxMint" ]  ; then 
       		LIBJPEGNAME="libjpeg6b"
	else
		#Libjpeg seems to be forked/renamed very frequently in debian
		# Likely a new libjpeg will need to be picked each time this script is run
		LIBJPEGNAME="libjpeg-turbo"
	fi
	DEB_PACKAGES="$DEB_PACKAGES $LIBJPEGNAME"

	GET_PACKAGES=""
	for i in $DEB_PACKAGES
	do

		#libpng-dev is a virtual package?
		if [ x"$i" != x"libpng-dev" ] ; then
			FNAME=`ls packages/${i}_*.orig.* 2> /dev/null`
		else
			FNAME=`ls packages/libpng*_*.orig.* 2> /dev/null`
		fi

		#If filename is empty, we will need to retreive it from
		# interwebs
		if [ x"$FNAME" == x"" ] ; then
			GET_PACKAGES="${GET_PACKAGES} $i"
		fi
	done

	#grab packages if they are not already on-disk
	if [ x"$GET_PACKAGES" != x"" ] ; then
		apt-get source $GET_PACKAGES

		if [ $? -ne 0 ] ; then
			echo "Package retrieval failed"
			echo "apt-get source failed... Maybe check internet connection, then try updating package database, then re-run?"
			echo " other possibilities could include, eg, that the required package is not available in the debian archive.."
			exit 1
		fi

		#Strip patches from the build and patch status files, 
		# if we are retriving new packages
		for i in $GET_PACKAGES
		do
			grep -v $i ../build-status > tmp
			mv tmp ../build-status
			
			grep -v $i ../patch-status > tmp
			mv tmp ../patch-status


		done
	fi


	#Move debian stuff into packages folder
	if [ x"$GET_PACKAGES" != x"" ] ; then
		mv *.orig.* *.debian.* *.dsc *.diff.* packages 
	fi

	#---
	#Download dsc package not in archive (pull from debian mentors)
	FILES_LIBVD="libvd_1.1.0+svn7-1.dsc libvd_1.1.0+svn7-1.debian.tar.xz libvd_1.1.0+svn7.orig.tar.gz"
	URL_LIBVD="https://mentors.debian.net/debian/pool/main/libv/libvd/"
	SHA_LIBVD="70ad1c77d807f0eea259ae4b88a0eb1284401467 afac97ecfc678965a241f7132d14ebb8c09d0be1 d66f48b3b2d00d3543cb1d3de572ef9c9acd4875"


	#Check SHA1sum hash to ensure integrity
	#----
	rm sha1_libvd.tmp
	for i in $FILES_LIBVD
	do
		if [ ! -f $i ] ; then
			if [ ! -f packages/$i  ] ; then
				wget ${URL_LIBVD}${i} || { echo "Unable to download libvd DSC"; exit 1; }
			else
				mv packages/$i .
			fi
		fi

		sha1sum $i | awk '{ print $1 }' >>sha1_libvd.tmp
	done

	
	SHA_DOWNLOADS=`cat sha1_libvd.tmp | tr '\n' ' '  | sed 's/ *$//'`
	if [ x"${SHA_DOWNLOADS}" != x"${SHA_LIBVD}" ] ; then
		echo "SHA1 sums were : |"${SHA_DOWNLOADS}"|"
		echo " Should be: |"${SHA_LIBVD}"|"
		exit 1
	fi
	rm sha1_libvd.tmp
	#----
	
	#extract
	if [ ! -d libvd-1.1.0+svn7 ] ; then	
		dpkg-source --no-check -x libvd*dsc || { echo "LIBVD Package extraction failed"; exit 1; }
	fi

	#Move into package folder
	mv libvd*{dsc,gz,xz} packages/
	#---


	#Check that we have untarred all the required packages
	# (eg we downloaded one, and wiped it at some stage)
	# if not, pull the package out, and re-build it
	GET_PACKAGES=""
	for i in $DEB_PACKAGES
	do
		#if we have a package file (dsc), and no folder, add it to the list of packages
		FNAME=`ls packages/$i*dsc 2> /dev/null` 
		DNAME=`ls -ld $i* | grep ^d | awk '{print $NF}'`
		if [ x"$FNAME" != x"" ] && [ x"$DNAME" == x""  ] ; then
			GET_PACKAGES="${GET_PACKAGES} $i"
		fi
	done
	
	#Unpack pre-existing package
	for i in $GET_PACKAGES
	do
		mv packages/$i*.* . || { echo "existing package extraction failed "; exit 1; } 
		dpkg-source -x $i*dsc
		#move package back
		mv ${i}_*.* packages/
		
		#wipe record of any patches for this package
		grep -v $i ../patch-status  > tmp
		mv tmp ../patch-status
		
		#wipe record of build
		grep -v $i ../build-status  > tmp
		mv tmp ../build-status
	done

	#extract libiconv if needed
	#--
	LIBICONV=libiconv-1.14
	if [ ! -f packages/${LIBICONV}.tar.gz ] ; then
		wget "http://ftp.gnu.org/gnu/libiconv/libiconv-1.14.tar.gz" || { echo "Libiconv download failed "; exit 1; }
		mv ${LIBICONV}.tar.gz packages/
	fi

	#Check for SHA1 goodness
	if [ x`sha1sum packages/${LIBICONV}.tar.gz | awk '{print $1}'` != x"be7d67e50d72ff067b2c0291311bc283add36965" ] ; then
		echo "SHA1 sum mismatch for libiconv"
		exit 1
	fi
	
	#Extract
	if [ ! -d ${LIBICONV} ] ; then
		tar -zxf packages/${LIBICONV}.tar.gz || { echo "failed extracting iconv "; exit 1; }
	fi	

	#---

	#We also need to install nsis, though it is not a strict "dependency" per-se.
	if [ x`which makensis` == x"" ] ; then
		echo "Installing nsis via apt-get";
		sudo apt-get install nsis || { echo "Failed installation"; exit 1; }
	fi

	#Download wxwidgets as needed
	#----

	WXVER=wxWidgets-3.1.0
	if [ ! -f packages/${WXVER}.tar.bz2 ] ; then
		wget "https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.0/${WXVER}.tar.bz2"
		mv ${WXVER}.tar.bz2 packages/
	fi

	#check SHA256
	if [ x`sha256sum packages/${WXVER}.tar.bz2 | awk '{print $1}'` != x'e082460fb6bf14b7dd6e8ac142598d1d3d0b08a7b5ba402fdbf8711da7e66da8' ] ; then
		echo "SHA256 sum mismatch for wxwidgets";
		exit 1
	fi
	
	#Extract
	if [ ! -d ${WXVER} ] ; then
		tar -jxf packages/${WXVER}.tar.bz2 || { echo "failed extracting wxwidgets "; exit 1; }
	fi	
	#----


	popd 2> /dev/null

}

function createBaseBinaries()
{
	pushd bin >/dev/null

	#Create symlinks to the compiler, which will be in our
	# primary path, and thus override the normal compiler
	
	#use ccache if it is around
	if [  x`which ccache` != x"" ] ; then
		echo -n "Enabling ccache support..."
		USE_CCACHE=1
	else
		echo -n "Making direct compiler symlinks..."
		USE_CCACHE=0
	fi

	for i in g++ cpp gcc c++
	do
		#Skip existing links
		if [ -f ${HOST_VAL}-$i ] || [ -L ${HOST_VAL}-$i ] ; then
			continue;
		fi

		if [ $USE_CCACHE == 1 ] ; then

			ln -s `which ccache` ${HOST_VAL}-$i
		else

			ln -s `which  ${HOST_VAL}-$i`
			
		fi
		
		if [ $? -ne 0 ] ; then
			echo "Failed when making compiler symlinks"
			exit 1
		fi
	done

	echo "done"
	popd >/dev/null
}

function fix_la_file
{
	#Need to fix .la files which have wrong path
	# due to libtool not liking cross-compile
	pushd lib/ >/dev/null
	sed -i 's@//lib@'${BASE}/lib@ ${FIX_LA_FILE_ARG}*.la
	sed -i 's@/lib/lib/@/lib/@'  ${FIX_LA_FILE_ARG}*.la
	popd >/dev/null
}

function build_zlib()
{
	ISBUILT_ARG="zlib"
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi

	pushd deps >/dev/null
	pushd zlib-* >/dev/null


	if [ $? -ne 0 ] ; then
		echo "Zlib dir missing, or duplicated?"
		exit 1
	fi
	
	make clean
	rm -f configure.log

	
	./configure  --prefix="/" || { echo "ZLib configure failed"; exit 1; } 
	
	APPLY_PATCH_ARG="$PATCHES_ZLIB"
	applyPatches

	make -j $NUM_PROCS || { echo "ZLib build failed"; exit 1; } 

	make install DESTDIR="$BASE"|| { echo "ZLib install failed"; exit 1; } 

	#Move the .so to a .dll
	mv ${BASE}/lib/libz.so ${BASE}/lib/libz.dll

	#Remove the static zlib. We don't want it
	rm $BASE/lib/libz.a

	popd >/dev/null
	popd >/dev/null

	echo "zlib" >> $BUILD_STATUS_FILE
}

function build_glew()
{
	ISBUILT_ARG="glew"
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi

	#Perform dynamic modification of patch
	if [ x"`grep HOST_VAL patches/glew-makefile.base`" == x""  -o   x"`grep BASEDIR patches/glew-makefile.base`" == x"" ] ; then
		echo "patches/glew-makefile did not contain replacement keywords"
		exit 1
	fi

	#Modify the patch appropriately
	cp patches/glew-makefile.base patches/glew-makefile
	sed -i "s@HOST_VAL@$HOST_VAL@" patches/glew-makefile
	sed -i "s@BASEDIR@$BASE@" patches/glew-makefile
	#FIXME: I patched out the bin/ subdir, around line 155
	echo "There is a patch missing to remove the tool build"
	exit 1

	pushd deps >/dev/null
	pushd glew-* >/dev/null


	if [ $? -ne 0 ] ; then
		echo "glew dir missing, or duplicated?"
		exit 1
	fi
	

	APPLY_PATCH_ARG="glew-makefile"
	applyPatches

	make clean
	rm -f configure.log
	
	LD=$CC CC=$CC CXX=$CXX LDFLAGS=-lstdc++ make -j $NUM_PROCS || { echo "glew build failed"; exit 1; } 

	make install DESTDIR="$BASE"|| { echo "glew install failed"; exit 1; } 

	popd >/dev/null
	popd >/dev/null


	#Check that glew built as a dynamic library. this should cause the init funcction
	# to be preceded by the windows dynamic link prefix, __imp__
	IMP_PREFIX=`find ./ -name \*dll | xargs x86_64-w64-mingw32-objdump -x | grep Init | grep __imp`
	if [ x"$IMP_PREFIX" == x"" ] ; then
		echo " Glew build failed, due to missing __imp__ prefix (dynamic link)"
		exit 1
	fi

	#remove static library, as this can be incorrectly caught by linker
	# leading to confusing messages
	rm ${BASE}/lib/libglew*.a

	#FIXME: The DLL and the include files are not being installed properly

	echo "glew" >> $BUILD_STATUS_FILE
}

function build_libpng()
{
	ISBUILT_ARG="libpng"
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	pushd deps >/dev/null
	pushd libpng[0-9.]*-* >/dev/null
	
	if [ $? -ne 0 ] ; then
		echo "libpng dir missing, or duplicated?"
		exit 1
	fi

	make clean

	./configure --host=$HOST_VAL --prefix=/ --enable-shared --disable-static || { echo "Libpng configure failed"; exit 1; } 

	#Hack to strip linker version script
	# eg as discussed : 
	# https://github.com/Pivosgroup/buildroot-linux/issues/2
	# The error is not 100% clear. if the build script has 
	# access to standard debian -I paths, then it goes away
	# version script documentation is sparse to non-existant.
	# the only really useful thing i've found is this:
	# http://www.akkadia.org/drepper/dsohowto.pdf
	sed -i 's/.*version-script.*//' Makefile Makefile.am

	make -j $NUM_PROCS  || { echo "libpng build failed"; exit 1; } 
	
	make install DESTDIR="$BASE"|| { echo "libpng install failed"; exit 1; } 

	#The new libpng shoudl install (when it works, which it does not for us)
	# the headers in a new subfolder, which is not compatible with most buildsystems
	# so make some symlinks
	#---
	# First simulate libpng's installer working (TODO: Why is this not working? It did before..)
	mkdir -p ../../include/libpng12/ 
	cp -p png.h pngconf.h ../../include/libpng12/
	# Then actually make the symlinks
	pushd ../../include/ > /dev/null
	ln -s libpng12/pngconf.h
	ln -s libpng12/png.h
	popd >/dev/null

	#Aaand it doesn't install the libraries, so do that too.
	cp -p .libs/*.dll $BASE/lib/
	cp -p .libs/*.la $BASE/lib/
	#----


	#Remove superseded libpng3
	rm ${BASE}/lib/libpng-3*
	ln -s ${BASE}/lib/libpng12-0.dll  ${BASE}/lib/libpng.dll

	popd >/dev/null
	popd >/dev/null

	FIX_LA_FILE_ARG=libpng
	fix_la_file
	
	echo "libpng" >> $BUILD_STATUS_FILE
}

function build_libxml2()
{
	NAME="libxml2"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi

	pushd deps >/dev/null
	LIBXMLVER=`ls -d libxml2-* | sed 's/libxml2-//'`
	pushd libxml2-* >/dev/null
	
	if [ $? -ne 0 ] ; then
		echo "libxml2 dir missing, or duplicated?"
		exit 1
	fi

	#Libxml 2.8 and up doesn't need patching.
	# note that --compare-versions returns 0 on truth, and 1 on false
	dpkg --compare-versions  $LIBXMLVER lt 2.8
	if [ $? -eq 0 ] ; then
		echo "WARNING : Previous attempts to use libxml2 < 2.8 have failed."
		echo " it is recommended to manually obtain the .dsc,.orig.tar.gz and .debian.tar.gz from"
		echo " http://packages.debian.org/source/wheezy/libxml2 . download these, then replace the .dsc,.orig.tar.gz and .debian.tar.gz in deps/packages/"
		exit 1
	fi

	make clean

	#Modifications
	#	Disable python, because sys/select.h is not in mingw
	./configure --host=$HOST_VAL --without-lzma --without-python --without-html --without-http --without-ftp --without-push --without-writer --without-push --without-legacy --without-xpath --without-iconv  --enable-shared=yes --enable-static=no --prefix=/ || { echo "Libxml2 configure failed"; exit 1; } 

	make -j $NUM_PROCS || { echo "libxml2 build failed"; exit 1; } 
	
	make install DESTDIR="$BASE"|| { echo "libxml2 install failed"; exit 1; } 

	popd >/dev/null
	popd >/dev/null

	FIX_LA_FILE_ARG=libxml2
	fix_la_file

	#For compat, link libxml2/libxml to libxml/
	pushd include >/dev/null
	ln -s libxml2/libxml libxml
	popd >/dev/null

	#The dll gets installed into bin/, link it to lib/
	pushd lib > /dev/null
	ln -s ../bin/libxml2-[0-9]*.dll 
	popd > /dev/null


	echo ${NAME} >> $BUILD_STATUS_FILE
}

function build_libjpeg()
{
		
	NAME=$LIBJPEGNAME
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	pushd deps >/dev/null
	pushd ${NAME}*[0-9]* >/dev/null
	
	if [ $? -ne 0 ] ; then
		echo "${NAME} dir missing, or duplicated?"
		exit 1
	fi

	make clean

	autoconf

	./configure --host=$HOST_VAL --enable-shared --disable-static --prefix=/ || { echo "$NAME configure failed"; exit 1; } 

	make -j $NUM_PROCS || { echo "$NAME build failed"; exit 1; } 
	
	make install DESTDIR="$BASE"|| { echo "$NAME install failed"; exit 1; } 
	
	#DLL needs to be copied into lib manually
	cp -p .libs/${NAME}-[0-9]*.dll $BASE/lib/ 

	popd >/dev/null
	popd >/dev/null

	FIX_LA_FILE_ARG=$NAME
	fix_la_file
	echo ${NAME}  >> $BUILD_STATUS_FILE
}

function build_libtiff()
{
	NAME="libtiff"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	pushd deps >/dev/null
	pushd tiff*[0-9]* >/dev/null
	
	if [ $? -ne 0 ] ; then
		echo "libtiff dir missing, or duplicated?"
		exit 1
	fi
	./configure --host=$HOST_VAL --enable-shared --disable-static --prefix=/ || { echo "Libtiff configure failed"; exit 1; } 

	make -j $NUM_PROCS || { echo "libtiff build failed"; exit 1; } 
	
	make install DESTDIR="$BASE"|| { echo "libtiff install failed"; exit 1; } 
	
	#DLL needs to be copied into lib manually
	cp -p .libs/${NAME}-[0-9]*.dll $BASE/lib/ 

	popd >/dev/null
	popd >/dev/null
	
	FIX_LA_FILE_ARG=libtiff
	fix_la_file
	echo ${NAME} >> $BUILD_STATUS_FILE
}

function build_qhull2012()
{
	NAME="libqhull"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	
	pushd deps >/dev/null
	pushd qhull-* >/dev/null
	
	if [ $? -ne 0 ] ; then
		echo "qhull dir missing, or duplicated?"
		exit 1
	fi

	make clean

	# We have an internal COMPILE_ASSERT test for ptr_intT length
	#if using win64, then ensure that there is only the correct long long version
	# of the pointer
	#if [ `egrep "typedef .* ptr_intT" src/libqhull/mem.h | wc -l` -ne 1 ] ; then
	#	echo "There appears to be multiple ptr_intT types in qhull's mem.h. Qhull normally picks the wrong one. Aborting- please fix"
	#	exit 1
	#fi

	sed -i "s/ gcc$/${HOST_VAL}-gcc/" Makefile
	sed -i "s/ g++$/${HOST_VAL}-g++/" Makefile

	make SO="dll" -j $NUM_PROCS 
	find ./ -name \*dll -exec cp {} ${BASE}/bin/	
	make SO="dll" -j $NUM_PROCS || { echo "qhull build failed"; exit 1; } 
	make SO="dll" install DESTDIR="$BASE"|| { echo "qhull install failed"; exit 1; } 

	popd >/dev/null
	popd >/dev/null

	ln -s ${BASE}/include/libqhull ${BASE}/include/qhull



	FIX_LA_FILE_ARG=libqhull
	fix_la_file
	echo ${NAME} >> $BUILD_STATUS_FILE
}

function build_qhull2015()
{
	NAME="libqhull_2015"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	
	pushd deps >/dev/null
	pushd qhull-* >/dev/null
	
	if [ $? -ne 0 ] ; then
		echo "qhull dir missing, or duplicated?"
		exit 1
	fi

	#delete the qhull directories that do NOT correspond to the version of qhull we want to build
	rm -rf src/libqhull src/libqhullcpp src/libqhullstatic*

	APPLY_PATCH_ARG="$PATCHES_QHULL"
	applyPatches
	make clean

	cp ../../patches/qhull2015-cmakefile-replacement CMakeLists.txt

	# TODO: Technically, deleting CMakeCache doesn't solve all caching problems. Cached data can remain elsewhere
	# This seems to stem from cmakes stubborn approach of "we only allow out-of-source-tree builds".
	# This is annoying, as in-tree building is very common, and natural for small projects. It also allows incremental building.
	#  IMHO, once I manually alter a cmakefile, the cache should fucking well work out that it is out-of-date, as I keep getting nasty suprises.
	# https://cmake.org/Bug/view.php?id=14820 .
	rm -f CMakeCache.txt
	cmake -DCMAKE_INSTALL_PREFIX="$BASE" -DCMAKE_TOOLCHAIN_FILE=../../patches/cmake-toolchain$BITS_VAL -DWITH_BOOST=0 -DWITH_FFTW=0 -DWITH_LEMON=0 


	#TODO: Better test (using c)
	#if using win64, then ensure that there is only the correct long long version
	# of the pointer
	if [ `egrep "typedef .* ptr_intT" src/libqhull_r/mem_r.h | wc -l` -ne 1 ] ; then
		echo "There appears to be multiple ptr_intT types in qhull's mem.h. Qhull normally picks the wrong one. Aborting- please fix"
		exit 1
	fi

	sed -i "s/ gcc$/${HOST_VAL}-gcc/" Makefile
	sed -i "s/ g++$/${HOST_VAL}-g++/" Makefile

	make SO="dll" -j $NUM_PROCS 
	find ./ -name \*dll -exec cp {} ${BASE}/bin/	
	make SO="dll" -j $NUM_PROCS || { echo "qhull build failed"; exit 1; } 
	make install DESTDIR="$BASE"|| { echo "qhull install failed"; exit 1; } 

	popd >/dev/null
	popd >/dev/null

	ln -s ${BASE}/include/libqhull ${BASE}/include/qhull



	FIX_LA_FILE_ARG=libqhull
	fix_la_file
	echo ${NAME} >> $BUILD_STATUS_FILE
}

function build_qhull2009()
{
	NAME="libqhull_2009"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	
	pushd deps >/dev/null
	pushd qhull-* >/dev/null
	
	if [ $? -ne 0 ] ; then
		echo "qhull dir missing, or duplicated?"
		exit 1
	fi


	#The as-distributed version of libtool no longer builds dlls with my gcc, it just fails silently. Rebuild
	libtoolize --install
	autoreconf
	automake --add-missing

	#FIXME: Need to change the ptrT type on 64 bit, in mem.h
	echo "Need to change ptrT, mem.h..."
	exit 1

	
	make distclean

	./configure --host=$HOST_VAL --enable-shared --disable-static --prefix=/ || { echo "$NAME configure failed"; exit 1; } 

	sed -i "s/ gcc$/${HOST_VAL}-gcc/" Makefile
	sed -i "s/ g++$/${HOST_VAL}-g++/" Makefile

	
	make SO="dll" -j $NUM_PROCS 
	find ./ -name \*dll -exec cp {} ${BASE}/bin/	
	make SO="dll" -j $NUM_PROCS || { echo "qhull build failed"; exit 1; } 
	make install DESTDIR="$BASE"|| { echo "qhull install failed"; exit 1; } 

	if [ x`find ./ -name \*.dll` == x"" ] ; then
		echo "Qhull DLL missing, after build. That's not right... (last time due to -no-undefined not given to linker flags, which libtool then decides means should do a static build. Updating libtool makes this more sane, and errors out)"
		exit 1;
	fi

	popd >/dev/null
	popd >/dev/null

	ln -s ${BASE}/include/libqhull ${BASE}/include/qhull
	
	FIX_LA_FILE_ARG=libqhull
	fix_la_file
	echo ${NAME} >> $BUILD_STATUS_FILE
}

function build_libvd()
{
	echo "FIXME: You need to disable the soname generation in the libvd makefile, otherwise the dll wont load properly"
	exit 1

	NAME="libvd"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	pushd deps >/dev/null
	pushd libvd*[0-9]* >/dev/null
	
	if [ $? -ne 0 ] ; then
		echo "${NAME} dir missing, or duplicated?"
		exit 1
	fi

	pushd src/
	#Make sure we do not pull any VS code in
	rm -rf msvc


	make clean

	#Change the hard-coded compiler as needed
	if [ x`grep ${HOST_VAL} Makefile` == x"" ] ; then

		sed -i s/gcc/${HOST_VAL}-gcc/ Makefile || { echo " compilation replacement failed" ; exit 1 ; }
		sed -i s/g++/${HOST_VAL}-g++/ Makefile || { echo " compilation replacement failed" ; exit 1 ; }
	fi

	#FIXME: Getting errors about undefined reference to pglUseProgram (and friends). These are the shader
	# definitions, which are missing under windows
	#  This is because it picks up the msvc folder. We now remove this. 
	# however I have not yet made a patch for this
	LDFLAGS="-L/home/pcuser/mingw64/lib/ -lopengl32 -lglu32 -lglew32 -lstdc++" make || { echo "libvd compilation failed"; exit 1; }

	popd

	FIX_LA_FILE_ARG=libvd
	fix_la_file
	echo ${NAME} >> $BUILD_STATUS_FILE
}

#FIXME: This does not work. Qhull uses a strange combination of cmake
# and hand makefiles, so propagating correct cross-compiling 
# parameters is quite tricky
function build_qhull2015b()
{
	NAME="libqhull"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	
	pushd deps >/dev/null
	pushd qhull-* >/dev/null
	
	if [ $? -ne 0 ] ; then
		echo "qhull dir missing, or duplicated?"
		exit 1
	fi

	rm -f CMakeCache.txt
	cmake -DCMAKE_INSTALL_PREFIX="$BASE" -DCMAKE_TOOLCHAIN_FILE=../../patches/cmake-toolchain$BITS_VAL 

	make -j $NUM_PROCS || exit 1

	make install	

	popd >/dev/null
	popd >/dev/null

	echo ${NAME} >> $BUILD_STATUS_FILE
}

function build_expat()
{
	NAME="libexpat"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	pushd deps >/dev/null
	pushd expat-** >/dev/null
	
	if [ $? -ne 0 ] ; then
		echo "expat dir missing, or duplicated?"
		exit 1
	fi

	./configure --host=$HOST_VAL --enable-shared --disable-static --prefix=/ || { echo "$NAME configure failed"; exit 1; } 

	make -j $NUM_PROCS || { echo "$NAME build failed"; exit 1; } 
	
	make install DESTDIR="$BASE"|| { echo "$NAME install failed"; exit 1; } 

	#DLL needs to be copied into lib manually
	cp -p .libs/${NAME}-[0-9]*.dll $BASE/lib/ 

	popd >/dev/null
	popd >/dev/null



	FIX_LA_FILE_ARG=libexpat
	fix_la_file
	echo ${NAME} >> $BUILD_STATUS_FILE
}

function build_gsl()
{
	NAME="libgsl"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	
	pushd deps >/dev/null
	pushd gsl-* >/dev/null
		
	if [ $? -ne 0 ] ; then
		echo "gsl dir missing, or duplicated?"
		exit 1
	fi

	make clean

	mkdir -p doc
	echo "all:" > doc/Makefile.in 
	echo "install:" >> doc/Makefile.in
	echo "clean:" >> doc/Makefile.in

	APPLY_PATCH_ARG=$PATCHES_GSL
	applyPatches


	./configure --host=$HOST_VAL --enable-shared --disable-static --prefix=/ || { echo "gsl configure failed"; exit 1; } 

	make -j $NUM_PROCS || { echo "gsl build failed"; exit 1; } 
	
	make install DESTDIR="$BASE"|| { echo "gsl install failed"; exit 1; } 

	popd >/dev/null
	popd >/dev/null


	FIX_LA_FILE_ARG=libgsl
	fix_la_file
	echo ${NAME} >> $BUILD_STATUS_FILE
}

function build_wx()
{
	NAME="libwx"
	ISBUILT_ARG=${NAME}
	isBuilt

	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	
	pushd deps >/dev/null
	pushd wx[Ww]idgets* >/dev/null
       	
	if [ $? -ne 0 ] ; then
		echo "wxwidgets dir missing, or duplicated?"
		exit 1
	fi
	WX_VER=`egrep "WX_RELEASE\s*=" Makefile.in | awk -F= '{print $2}' | sed 's/\s*//'`

	case ${WX_VER} in
		"3.0" )  
			echo "WX needs to be at least 3.1, but is 3.0..."
			echo "this is due to a propgrid bug"
			exit 1
			;;
		[0-9].[0-9])
			;;
		"" )
			echo "blah! - wx version not set!"
			exit 1
			;;
	esac

	make clean

	#It might be possible to make this more aggressive. WX includes a lot of stuff we don't have any use for.
	# Linux distributions disale their web stuff (eg disable-webkit), as there are apparently serious security issues with wx's implementation
	# as a bonus, the more we disable, the less we have to compile!
	WX_DISABLE="--disable-compat26 --disable-compat28 --disable-ipc --disable-apple_ieee --disable-zipstream --disable-protocol_ftp --disable-mshtmlhelp --disable-aui --disable-mdi --disable-postscript --disable-datepick --disable-splash --disable-wizarddlg --disable-joystick --disable-loggui --disable-debug --disable-logwin --disable-logdlg --disable-tarstream --disable-fs_archive --disable-fs_zip --disable-snglinst --disable-sound --without-regex --disable-stc --disable-bannerwindow --disable-splash --disable-mediactr --disable-webkit"

	./configure --host=$HOST_VAL --enable-shared --disable-static --with-opengl --enable-unicode $WX_DISABLE --enable-wxurl --prefix=/ || { echo "wxwidgets configure failed"; exit 1; } 

       #TODO: Where is this coming from ???
	for i in `find ./ -name Makefile | grep -v samples | grep -v wxPython`
	do
		sed -i "s@-luuid-L@ -luuid -L@" $i
	done	
       
	make -j $NUM_PROCS || { echo "wxwidgets build failed"; exit 1; } 
	make install DESTDIR="$BASE"|| { echo "wxwidgets install failed"; exit 1; } 


	
	popd >/dev/null
	popd >/dev/null

	pushd ./bin/

	if [ -L wx-config ] ; then
		unlink wx-config
	fi


	#Search for the wx-config file. It get installed into /lib/wx, but has
	# unusual naming conventions
	WX_CONFIG_FILE=`find ${BASE}/lib/wx/config/ -type f -executable -name \*release-\*`
	if [ x"$WX_CONFIG_FILE" == x"" ] ; then
		WX_CONFIG_FILE=`find ${BASE}/lib/wx/config/ -type f  -executable -name \*-unicode-\*3.1`
	fi

	if [ x"$WX_CONFIG_FILE" == x"" ] ; then
		WX_CONFIG_FILE=`find ${BASE}/lib/wx/config/ -type f  -executable -name \*mingw32-msw-unicode-\*`
	fi	

	if [ x"$WX_CONFIG_FILE" == x"" ] ; then
		echo "Couldn't find the wx-config script."
		exit 1
	fi

	cp $WX_CONFIG_FILE wx-config 
	APPLY_PATCH_ARG=$PATCHES_WXWIDGETS_POST
	PATCH_LEVEL=0
	applyPatches
	PATCH_LEVEL=1
	sed -i "s@REPLACE_BASENAME@${BASE}@" wx-config || { echo "Failed to update wx-config with build root,. Aborting";  exit 1; }
	popd
	#FIXME : We still need to fix some problems with wx-config, wx-config --rescomp returns empty

	pushd ./lib/
	ln -s wx-${WX_VER}/wx/ wx
	popd

	pushd ./include/wx-${WX_VER}/wx/msw/
	if [ x"$BITS_VAL" == x"64" ] ; then
		echo "Replacing manifest"
		cp amd64.manifest wx.manifest
	fi
	popd	



	pushd ./include
	ln -s wx-${WX_VER}/wx/ wx
	popd

	echo ${NAME} >> $BUILD_STATUS_FILE

}

function build_freetype()
{
	NAME="libfreetype"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	pushd deps >/dev/null
	pushd freetype-* >/dev/null
		
	if [ $? -ne 0 ] ; then
		echo "freetype dir missing, or duplicated?"
		exit 1
	fi

	tar -xjf freetype-[0-9]*bz2  || { echo "freetype decompress failed" ; exit 1; } 

	#For some reason, the debian package puts freetype in its own self-subdir
	pushd freetype-[0-9].[0-9].[0-9]
	make clean
	#Create out-of-tree build zone
	rm -rf build/
	mkdir build
	pushd build

	LIBS=-lpng cmake -DWITH_HarfBuzz=OFF -DWITH_PNG=ON -DWITH_ZLIB=ON -DBUILD_SHARED_LIBS:BOOL=true -DCMAKE_INSTALL_PREFIX="$BASE" -DCMAKE_TOOLCHAIN_FILE=${BASE}/patches/cmake-toolchain$BITS_VAL -DPNG_PNG_INCLUDE_DIR=${BASE}/include/  ../

	if [ $? -ne 0 ] ; then
		echo "Cmake failed"
		exit 1
	fi

	#Old configure-based build system
#	./configure --host=$HOST_VAL --enable-shared --disable-static --without-png --with-harfbuzz=no --prefix=/ || { echo "freetype configure failed"; exit 1; } 

	make -j $NUM_PROCS || { echo "freetype build failed"; exit 1; } 
	
	make install || { echo "freetype install failed"; exit 1; } 
	popd 

	popd >/dev/null
	
	popd >/dev/null
	popd >/dev/null
	
	
	FIX_LA_FILE_ARG=libfreetype
	fix_la_file

	#freetype has moved headers. Add symlink for backwards compat

	pushd include >/dev/null
	ln -s freetype2/freetype/
	popd >/dev/null

	#Freetype does not install .dll properly, do by hand
	find ./ -name libfreetype*dll -exec cp {} ${BASE}/lib/ \; || { echo "Unable to find freetype dll. Aborting" ; exit 1 ;}

	echo ${NAME} >> $BUILD_STATUS_FILE
}


function build_libiconv()
{
	NAME="iconv"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi

	pushd deps >/dev/null
	pushd libiconv-* >/dev/null
		
	if [ $? -ne 0 ] ; then
		echo "libiconv dir missing, or duplicated?"
		exit 1
	fi

	APPLY_PATCH_ARG=$PATCHES_ICONV
	applyPatches

	make clean


	./configure --host=$HOST_VAL --enable-shared --disable-static --prefix=/ || { echo "libiconv configure failed"; exit 1; } 

	make -j $NUM_PROCS || { echo "libiconv build failed"; exit 1; } 
	
	make install DESTDIR="$BASE"|| { echo "libiconv install failed"; exit 1; } 

	popd >/dev/null
	popd >/dev/null
	
	FIX_LA_FILE_ARG=libiconv
	fix_la_file
	echo ${NAME} >> $BUILD_STATUS_FILE
}

function build_gettext()
{
	NAME="gettext"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	pushd deps >/dev/null
	pushd gettext-* >/dev/null
		
	if [ $? -ne 0 ] ; then
		echo "$NAME dir missing, or duplicated?"
		exit 1
	fi

	make clean

	APPLY_PATCH_ARG=$PATCHES_GETTEXT
	applyPatches
	automake

	#The CFlags/cxxflags thing is due to a really old gettext bug
	# (who is maintaining that these days? the fix is known for years!) 
	# http://savannah.gnu.org/bugs/?36443
	CFLAGS="$CFLAGS -O2" CXXFLAGS="$CXXFLAGS -O2" ./configure --host=$HOST_VAL --disable-threads --enable-shared --disable-static --prefix=/ || { echo "$NAME configure failed"; exit 1; } 

	make -j $NUM_PROCS || { echo "$NAME build failed"; exit 1; } 
	
	make install DESTDIR="$BASE"|| { echo "$NAME install failed"; exit 1; } 

	#FIXME: I had to copy the .lib, .la and .a files manually
	# I don't know why the makefile does not do this.
	CPSUCCESS=0
	for i in `ls gettext-runtime/intl/.libs/libintl*.{la,lib,a,dll}`
	do
		cp $i ${BASE}/lib/ 
		if [ $? -eq 0 ] ; then
			CPSUCCESS=1;
		fi
	done
	if [ $CPSUCCESS -eq 0 ] ; then	
		{  echo "semi-manual copy of libintl failed"; exit 1; } 
	fi
	popd >/dev/null
	popd >/dev/null

	#FIXME: libintl.h does notget installed properly

	FIX_LA_FILE_ARG=libintl
	fix_la_file
	FIX_LA_FILE_ARG=libcharset
	fix_la_file
	
	echo ${NAME} >> $BUILD_STATUS_FILE
}

function build_mathgl()
{
	NAME="libmgl"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	pushd deps >/dev/null
	pushd mathgl-* >/dev/null
	
	if [ $? -ne 0 ] ; then
		echo "mathgl dir missing, or duplicated?"
		exit 1
	fi
	make clean

	APPLY_PATCH_ARG=$PATCHES_MATHGL
	applyPatches

	if [ -d ${BASE}/include/mgl2 ] ; then
		echo "there are mgl2 headers already installed. Abort abort!"
		exit 1
	fi

	rm -f CMakeCache.txt
	LIBS=-lpng cmake -Denable-gsl="yes" -Denable-jpeg="no" -Denable-mpi="no" -Denable-ltdl="no" -Denable-python="no"  -DCMAKE_INSTALL_PREFIX="$BASE" -DCMAKE_TOOLCHAIN_FILE=../../patches/cmake-toolchain$BITS_VAL -DPNG_PNG_INCLUDE_DIR=${BASEDIR}/include/

	make -j $NUM_PROCS

	if [ x"`find ./ -name \*dll`" == x"" ] ; then
		echo "Did cmake fail to make a DLL? Cmake rarely builds cleanly, but I should be able to find the DLL..."
		exit 1
	fi
		
	make install
	
	ln -s ${BASE}/include/mgl2 ${BASE}/include/mgl

	popd >/dev/null
	popd >/dev/null
	FIX_LA_FILE_ARG=libmgl
	fix_la_file
	
	echo ${NAME} >> $BUILD_STATUS_FILE
}

function build_libvigra()
{
	NAME="libvigra"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	pushd deps >/dev/null
	pushd libvigraimpex-* >/dev/null
	
	if [ $? -ne 0 ] ; then
		echo "$NAME dir missing, or duplicated?"
		exit 1
	fi
	make clean

	APPLY_PATCH_ARG=$PATCHES_LIBVIGRA
	applyPatches

	cmake -DCMAKE_INSTALL_PREFIX="$BASE" -DCMAKE_TOOLCHAIN_FILE=../../patches/cmake-toolchain$BITS_VAL -DPNG_PNG_INCLUDE_DIR=${BASE}/include/

	make -j $NUM_PROCS
	
	make install || { echo "$NAME install failed"; exit 1; } 


	popd >/dev/null
	popd >/dev/null
	FIX_LA_FILE_ARG=libmgl
	fix_la_file
	
	echo ${NAME} >> $BUILD_STATUS_FILE
}

function build_ftgl()
{
	NAME="libftgl"
	ISBUILT_ARG=${NAME}
	isBuilt
	if [ $ISBUILT -eq 1 ] ; then
		return;
	fi
	pushd deps >/dev/null
	pushd ftgl-* >/dev/null
	
	if [ $? -ne 0 ] ; then
		echo "ftgl dir missing, or duplicated?"
		exit 1
	fi
	make clean

	APPLY_PATCH_ARG="$PATCHES_FTGL"
	applyPatches

	autoreconf
	libtoolize

	APPLY_PATCH_ARG="$PATCHES_FTGL_POSTCONF"
	applyPatches
	#rename GLU to glu32 and gl to opengl32
	sed -i s/'\-lGLU'/-lglu32/ configure
	sed -i s/'\-lGL'/-lopengl32/ configure

	#configure tries to link against wrong prototypes. Override this
	sed -i 's/char glBegin() ;//' configure
	sed -i 's/char gluNewTess ();//' configure

	sed -i 's/return glBegin ()/return 0;/' configure
	sed -i 's/return gluNewTess ()/return 0;/' configure
	sed -i 's/return glBegin(GL_POINTS)/return 0;/' configure


	FTFLAG=-I${BASE}/include/freetype2/

	LIBS="-lfreetype -lz" CXXFLAGS=$FTFLAG CFLAG=$FTFLAG ./configure --host=$HOST_VAL --with-ft-prefix=${BASE}---enable-shared --disable-static --prefix=/ || { echo "ftgl configure failed"; exit 1; } 


	#MAkefile refers to ECHO variable for reporting completion, which does not exist
	sed -i 's/ECHO_C =/ECHO=echo/' Makefile
	sed -i "s@-I//@-I${BASE}/@" Makefile
	sed -i 's/ECHO_C =/ECHO=echo/' Makefile
	
	#HACK - find all -I// and -L// and replace them with something sane
	find ./ -name Makefile -exec sed -i "s@-I//@-I${BASE}/@" {} \;
	find ./ -name Makefile -exec sed -i "s@-L//@-L${BASE}/@" {} \;

	LIBS="-lfreetype -lz" make -j $NUM_PROCS || { echo "ftgl build failed"; exit 1; } 
	
	DESTDIR="$BASE" make install || { echo "ftgl install failed"; exit 1; } 


	#HACK: Ftgl doesn't install the dll correctly. Just do it by hand
	cp src/.libs/libftgl-*dll ${BASE}/lib/ || { echo "FTGL dll could not be installed." ; exit 1; }
	pushd $BASE/lib/
	ln -s libftgl-*dll libftgl.dll
	popd > /dev/null

	popd >/dev/null
	popd >/dev/null
	FIX_LA_FILE_ARG=libftgl
	fix_la_file
	
	echo ${NAME} >> $BUILD_STATUS_FILE
}

function createDirLayout()
{
	NEEDED_DIRS="bin lib include deps/packages"
	for i in $NEEDED_DIRS
	do
		if [ ! -d $i ] ; then
			mkdir -p $i;
		fi
	done
}

function checkPatchesExist()
{	
	echo -n "checking patches exist..."
	if [ x"$PATCH_LIST" == x"" ] ; then
		return;
	fi

	if [ ! -d patches ] ; then
		echo "Patch directory does not exist, but patches are needed"
		echo "Need : " $PATCH_LIST
		exit 1
	fi

	for i in $PATCH_LIST
	do
		if [ ! -f "patches/$i" ]  ; then
			echo "patches/$i" does not exist
			exit 1;
		fi
	done
	echo "done"
}

#Ensure we are running under  a whitelisted host
function checkHost()
{
	echo -n "Host checks... "
	if [ x`which uname` == x"" ] ; then
		echo "Uname missing... Shouldn't be... Can't determine arch - bailing out."
		exit 1
	fi

	if [ x`uname -s` != x"Linux" ] ; then
		echo "This is only meant to run under linux.. and probably only debian-like systems."
		exit 1
	fi


	HOSTTYPE=`uname -m`

	if [ x"$HOSTTYPE" != x"x86_64" ] ; then
		echo "This only works on x86_64 builder machines - you'll need to modify the script to handle other arches. Sorry."
		exit 1
	fi

	BINARIES_NEEDED="apt-get sudo patch grep sed awk"
	
	for i in $BINARIES_NEEDED 
	do
		if [ x`which $i` == x"" ] ; then
			echo "Missing binary : $i  - aborting"
			exit 1
		fi
	done
	
	if [ x`which lsb_release` != x"" ] ; then
		#Possible results
		# Debian, LinuxMint,Ubuntu,(others)
		DIST_NAME=`lsb_release -i | awk -F: '{print $2}' | sed 's/\s//g'`
		if [ x$DIST_NAME != x"Debian" ] ; then
			echo "This is meant to target debian. I'm not sure if it will work on your system. You'll need to work that out."
			echo "Sleeping for 4 seconds."
			sleep 4
		fi
	fi
	
	echo "done"
}

#Build 3Depict
function build_3Depict()
{
	pushd code/3Depict
	make distclean


	autoreconf
	automake --add-missing

	CONF_FLAG="--host=$HOST_VAL --with-libqhull-link=-lqhull_r --enable-sweep-hull --enable-libvd"
	if [ $IS_RELEASE -ne 0 ] ; then
		CONF_FLAG="$CONF_FLAG --disable-debug-checks --enable-openmp-parallel"
	fi


	FTGL_CFLAGS="-I${BASE}/include/freetype2/" CFLAGS="$CFLAGS -DUNICODE -Dqh_QHpointer" CPPFLAGS="${CPPFLAGS} -DUNICODE -Dqh_QHpointer" ./configure  $CONF_FLAG 

	if [ $? -ne 0 ] ; then
		echo "Failed 3Depict configure"
		exit 1
	fi

	#comment out the rpl_malloc and rpl_realloc calls
	sed -i 's@^#define \([a-z]*\) rpl_@//#define \1 rpl_@' config.h


	#sanity check that windres is activated
	if [ x`grep HAVE_WINDRES_TRUE config.log | grep '#' ` != x"" ] ; then
		echo "Windres appears to be commented out. Shouldn't be for windows builds"
		exit 1
	fi

	#Check that wx's manifest matches our arch
	MANIFEST=`find ../../include/ -name wx.manifest`
	if [ x"$MANIFEST" == x"" ] ; then
		echo "Didnt' find manifest!"
		exit 1
	fi
	case $BITS_VAL in
		32)
			MANIFEST_TARG=x86
			MANIFEST_NOT=amd64
			;;
		64)
			MANIFEST_TARG=amd64
			MANIFEST_NOT=x86
			;;
	esac

	if [  x"`grep -i $MANIFEST_TARG $MANIFEST`" == x"" ] ; then
		echo "Manifest arch does not match!"	
		echo " file examined: $MANIFEST"
		echo " Expected :" $MANIFEST_TARG 
		exit 1
	fi

	if [  x"`grep -i $MANIFEST_NOT $MANIFEST`" != x"" ] ; then
		echo "Manifest arch does not match!"	
		echo " file examined: $MANIFEST"
		echo " This should be missing, but isnt:" $MANIFEST_NOT 
		exit 1
	fi

       #HACK - strip all makefiles of -D_GLIBCXX_DEBUG
       #	mingw & GLIBCXX_DEBG don't play nice
	find ./ -name Makefile -exec sed -i 's/-D_GLIBCXX_DEBUG//g' {} \;
	#HACK - find all -I// and -L// and replace them with something sane
	find ./ -name Makefile -exec sed -i "s@-I//@-I${BASE}/@" {} \;
	find ./ -name Makefile -exec sed -i "s@-L//@-L${BASE}/@" {} \;
	#HACK : remove -lm, which picks up host's lib, rather than mingw build
	find ./ -name Makefile -exec sed -i "s@-lgslcblas -lm@-lgslcblas@" {} \;

	#Actually perform build	
	make -j$NUM_PROCS
	if [ $? -ne 0 ] ; then
		echo "Failed 3Depict build"
		exit 1
	fi

	#if the locales are missing, try to rebuild them
	if [ x`which msgmerge` == x"" ] ; then
		echo "Translations do not appear to be built for 3depict. Need to install translation builder, gettext."
		sudo apt-get install gettext
	fi

	pushd translations

	#change the install dir
	sed -i "s@/usr/share/@${BASE}/code/3Depict/@" makeTranslations 

	./makeTranslations install || { echo "Translation build failed."; exit 1; }
	popd

	popd > /dev/null
}

#Build the nsis package
function make_package()
{
	if [ x"$HOST_EXT" != x"win64" ] ; then
		echo "WRONG HOST EXT!"
		exit 1
	fi

	pushd ./code/3Depict 2> /dev/null

	#Check that the PDF manual has been built
	if [ ! -f docs/manual-latex/manual.pdf ] ; then
		echo "PDF Manual not built. Building"
	
		pushd docs/manual-latex
		pdflatex manual.tex && bibtex manual && pdflatex manual.tex  || { echo " Manual not pre-built, and failed to build. Aborting" ; exit 1; }
		popd
		if [ ! -f docs/manual-latex/manual.pdf ] ; then 
			echo "Failed to build manual, even though latex completed with no errors. Aborting " 
			exit 1;
		fi
	fi

	mkdir locale

	#Copy locales (eg wxstd.mo)
	SUPPORTED_LOCALS="de es fr"
	for i in $SUPPORTED_LOCALS
	do

		cp -Rp ${BASE}/share/locale/$i locale/$i || { echo "copying locale support files failed" : exit 1; }
	done

	#remove trash locale, which we don't need to ship
	find ./locale/ -name bug-gnu-gettext@gnu.org.mo -exec rm {} \;


	NSI_FILE=./windows-installer.nsi

	#copy as needed
	# Due to debian bug : #704828, makensis cannot correctly handle symlinks,
	# so don't use symlinks
	if [ ! -f `basename $NSI_FILE`  ] ; then
		cp ./packaging/mingw-debian-cross/windows-installer.nsi .
	fi

	
	if [ ! -f $NSI_FILE ] ; then
		echo "NSI file missing whilst trying to build package"
		exit 1;
	fi

	#Check NSI file has PROGRAMFILES / PROGRAMFILES64 set
	if [ x"`grep PROGRAMFILES64 $NSI_FILE`" == x"" -a $BITS_VAL == 64 ] ; then
		echo "NSI file should contain PROGRAMFILES64 output path."
		exit 1;
	else
		if [ x"`grep PROGRAMFILES64 $NSI_FILE`" != x"" -a $BITS_VAL == 32 ] ; then
			echo "NSI file contained 64 bit install dir, but this is 32"
			exit 1;
		fi
	fi
	
	
	 

	echo -n " Copying dll files... "
	SYSTEM_DLLS="(ADVAPI32.dll|COMCTL32.DLL|COMDLG32.DLL|GDI32.dll|KERNEL32.dll|ole32.dll|OLEAUT32.dll|RPCRT4.dll|SHELL32.DLL|USER32.dll|WINMM.DLL|WINSPOOL.DRV|WSOCK32.DLL|GLU32.dll|OPENGL32.dll|msvcrt.dll|WS2_32.dll|SHLWAPI.dll|VERSION.dll)"

	DLL_FILES=`${HOST_VAL}-objdump -x src/3Depict.exe | grep 'DLL Name:' | awk '{print $3}' | egrep -i -v ${SYSTEM_DLLS}`
	FOUND_DLLS=""
	SYS_DIR=/usr/lib/gcc/${HOST_VAL}/

	echo "DEBUG :" $DLL_FILES
	rm -f tmp-dlls tmp-found-dlls

	#copy the DLL files from system or 
	# from the build locations
	while [ x"$DLL_FILES" != x"" ] ;
	do
		echo $DLL_FILES | tr '\ ' '\n' > tmp-dlls
		DLL_FILES=`cat tmp-dlls | sort | uniq`

		echo "Looking for these:" $DLL_FILES
		echo "Have found these:" $FOUND_DLLS
		for i in $DLL_FILES
		do
			HAVE_DLL=0
			for j in ${BASE}/lib/ ${BASE}/bin/ $SYS_DIR /usr/${HOST_VAL}/lib/
			do
				FIND_RES=`find $j -name $i | head -n 1`
				if [ x$FIND_RES != x"" ] ; then
					HAVE_DLL=1;
					cp $FIND_RES ./src/
					FOUND_DLLS="$FOUND_DLLS `basename $FIND_RES`"
					break;
				fi
			done

			if [ $HAVE_DLL -eq 0 ] ; then
				echo "Couldnt find DLL :" 
				echo " $i "
				echo " looked in ${BASE}/lib/ and $SYS_DIR"
				exit 1;
			fi
		done

		#Update the list of dll files
		echo $FOUND_DLLS |tr '\ ' '\n' | sort | uniq > tmp-found-dlls
		DLL_FILES=`${HOST_VAL}-objdump -x ./src/3Depict.exe ./src/*dll | grep 'DLL Name:' | awk '{print $3}' | egrep -i -v ${SYSTEM_DLLS} | grep -iv -f tmp-found-dlls | sort | uniq`

	done
	echo "done."

	if [ $IS_RELEASE -ne 0 ] ; then
		#Strip debugging information
		pushd src/ > /dev/null
		strip *.dll *.exe
		popd > /dev/null
	fi

	#Check for differeent DLL types (eg 32/64)
	DLL_FILE_OUT="";
	for i in $FOUND_DLLS
	do
		J=`file $i | awk -F: '{print $2}' | sed 's/\s*//'`
		if [ x"$DLL_FILE_OUT" == x"" ] ; then	
			DLL_FILE_OUT=$j
		else
			if [ x"$DLL_FILE_OUT" != x"$j" ] ; then
				echo "DLL Mismatched file info. $i"
				exit 1
			fi
		fi
	done

	if [ x"`cat windows-installer.nsi | grep INSERT_DLLS_HERE`" == x"" ]  ||  [ x"`cat windows-installer.nsi | grep INSERT_UNINST_DLLS_HERE`" == x"" ] ; then
		echo "DLL insertion/removal tokens not found. Was looking for INSERT_DLLS_HERE and INSERT_UNINST_DLLS_HERE"
		exit 1
	fi

	#Insert DLL names automatically
	cp windows-installer.nsi tmp.nsi
	echo $FOUND_DLLS | sed 's/ /\n/g' |  sed 's@^@  File \"src\\@' | sed 's/$/\"/' > tmp-insert
	perl -ne 's/^  ;INSERT_DLLS_HERE/`cat tmp-insert$1`/e;print' tmp.nsi >tmp2.nsi
	mv tmp2.nsi tmp.nsi


	echo $FOUND_DLLS | sed 's/ /\n/g' |  sed 's@^@  Delete \"$INSTDIR\\@' | sed 's/$/\"/' > tmp-insert
	perl -ne 's/^  ;INSERT_UNINST_DLLS_HERE/`cat tmp-insert$1`/e;print' tmp.nsi > tmp2.nsi
	mv tmp2.nsi tmp.nsi

	#TODO: Why is perl converting this to dos?
	dos2unix tmp.nsi

	NSI_FILE=tmp.nsi

	makensis `basename $NSI_FILE` ||  { echo "makensis failed" ; exit 1; }

	echo "-------------------"
	VERSION=`cat $NSI_FILE | grep "define PRODUCT_VERSION " | awk '{print $3}' | sed s/\"//g | sed s/\s*$//`
	if [ $IS_RELEASE -ne 0 ] ; then
		echo "Release mode enabled:"
		TARGET_FILE=3Depict-$VERSION-$HOST_EXT.exe
	else
		echo "Release mode disabled:"
		TARGET_FILE=3Depict-${VERSION}-${HOST_EXT}-debug.exe
	fi
	
	mv Setup.exe  $TARGET_FILE
	echo "File written to : `pwd`/$TARGET_FILE"
	echo "-------------------"
	
	popd > /dev/null
}

#Run wine host - assumes you have wine already installed
function run_wine()
{
	if [ $BITS_VAL == 64 ] ; then
		export WINEPREFIX=~/.wine64/
		export WINEARCH=win64
	else
		echo "error in script, bits val not right"
		return
	fi
	if [ x"`which wine`" == x"" ] ; then 
		return
	fi

	pushd ${BASE}code/3Depict/src/

	if [ $IS_RELEASE -ne 1 ] ; then
		#Run the unit tests
		wine 3Depict.exe -t
	fi

	if [ x"${TARGET_FILE}" == x"" ] ;then

		NSI_FILE=windows-installer.nsi
		VERSION=0.0.21
		if [ $IS_RELEASE -ne 0 ] ; then
			echo "Release mode enabled:"
			TARGET_FILE=3Depict-${VERSION}-${HOST_EXT}.exe
		else
			echo "Release mode disabled:"
			TARGET_FILE=3Depict-${VERSION}-${HOST_EXT}-debug.exe
		fi
	fi

	#Remove any old code
	rm -rf "${WINEPREFIX}/drive_c/Program\ Files/3Depict/"
	#unpack installer using wine 
	wine ${BASE}/code/3Depict/${TARGET_FILE}
	#run program
	wine "${WINEPREFIX}/drive_c/Program Files/3Depict/3Depict.exe"
}

#Check that we have a suitable host system
checkHost

#Check we have the patches we need
checkPatchesExist

#build the dirs we need
createDirLayout


#Install cross compiler
#---
case ${HOST_VAL}  in
	x86_64-w64-mingw32)
		if [ x"$DIST_NAME" == x"Ubuntu" ] || [ x"$DIST_NAME" == x"LinuxMint" ] ; then
			MINGW_PACKAGES="mingw-w64-dev g++-mingw-w64-x86-64"
		else
			MINGW_PACKAGES="mingw-w64-x86-64-dev g++-mingw-w64-x86-64"
		fi
		HOST_EXT="win64"
	;;
	i686-w64-mingw32)
		MINGW_PACKAGES="gcc-mingw-w64-i686 g++-mingw-w64-i686"
		HOST_EXT="win32"
	;;
	*)
		echo "Unknown host... please install deps manually,or alter script"
		exit 1	
	;;
esac


#install the compiler
install_mingw
#---

#Create the binaries we need
createBaseBinaries

#Obtain the needed dependencies
grabDeps



#set our needed environment variables
PATH=${BASE}/bin/:/usr/$HOST_VAL/bin/:$PATH
export CXX=${HOST_VAL}-g++
export CPP=${HOST_VAL}-cpp
export CC=${HOST_VAL}-gcc
export CPPFLAGS=-I${BASE}/include/
export CFLAGS=-I${BASE}/include/
export CXXFLAGS=-I${BASE}/include/
export LDFLAGS=-L${BASE}/lib/
export RANLIB=${HOST_VAL}-ranlib
export LIBS="-L${BASE}/lib/"
DESTDIR=${BASE}

build_zlib
build_libtiff
build_libpng 
build_libjpeg
build_libxml2
build_gsl
#build_qhull2009 
#build_qhull2015
build_expat
build_freetype  
build_libiconv
build_gettext  
build_ftgl 
build_glew  
build_libvigra

build_mathgl
build_wx
build_libvd 


build_3Depict

make_package

#Normally this should be disabled
if [ 0 -eq 1 ] ; then
	run_wine
fi