File: Makefile.in

package info (click to toggle)
biosig4c++ 1.9.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 6,220 kB
  • sloc: ansic: 41,218; cpp: 8,946; sh: 4,365; makefile: 1,758; python: 87; awk: 73; php: 40; perl: 36; java: 14; ruby: 7
file content (1368 lines) | stat: -rw-r--r-- 51,162 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
######## Makefile for "BioSig for C/C++" #####################
###
###  Copyright (C) 2006-2017 Alois Schloegl <alois.schloegl@ist.ac.at>
###  This file is part of the "BioSig for C/C++" repository
###  (biosig4c++) at http://biosig.sf.net/
###
##############################################################

##### Target: GNU/Linux
#
## make && make install    build and install libbiosig, but no other tools.
#             libbiosig is a prerequisite for building all the other tools below.
#
## make save2gdf   - makes converter
## make biosig_fhir   - makes converter for fhir-binary template
## make install_save2gdf   - installs converter
## make install_mma 	installs biosig's sload for Mathematica in $PREFIX/share/biosig/mathematica
## make install_mex 	installs mexbiosig for Matlab in $PREFIX/share/biosig/mex
#
## make mex4o      - makes mexSLOAD, mexSOPEN for Octave (requires Octave-headers)
## make mex4m      - makes mexSLOAD, mexSOPEN for Matlab (requires Matlab, mex -setup must be configured)
## make mexw32     - makes mexSLOAD.mexw32, mexSOPEN.mexw32 (requires that mingw32, gnumex libraries from Matlab/Win32)
## make mexw64     - makes mexSLOAD.mexw64, mexSOPEN.mexw64 (requires that mce-w32, gnumex libraries from Matlab/Win64)
## make mex        - mex4o, mex4m, mexw32, mexw64 combined
## make biosig4python - makes python interface (requires Python)
## make biosig4java - makes Java interface (experimental)
## make biosig4php - makes PHP interface (experimental)
## make biosig4perl - makes perl interface (experimental)
## make biosig4ruby - makes ruby interface (experimental)
## make biosig4tcl - makes tcl/tk interface (experimental)
##
## make win32 and make win64 are obsolete. save2gdf.exe, and libbiosig.{a,dll} for windows can now be built
## with the mingw-cross-compiler environment (mxe.cc).
##    git clone https://github.com/schloegl/mxe.git
##    make biosig
## should do what you want. Please note, that win32mma does now also rely that libbiosig is built with MXE.
## Make sure that CROSS or CROSS64 is properly defined when running `make`
##
## ???
## make sigviewer  - makes sigviewer

##### Target: Win32
## make win32      - makes save2gdf.exe,libbiosig.lib,libbiosig.dll, for MSWindows, requires MinGW
## make mexw32 mex/mexSLOAD.mexw32   - requires MinGW32 and GNUMEX libraries from Matlab/Win32
## make win32/sigviewer.exe 	- requires sources of SigViewer, and MinGW32 (mex: make suitesparse zlib qt )

##### Target: Win64
## make win64      - makes save2gdf.exe,libbiosig.lib,libbiosig.dll, for MSWindows, requires MinGW
## make mexw64 mex/mexSLOAD.mexw64   - requires MCE-W64 and GNUMEX libraries from Matlab/Win64
## make win64/sigviewer.exe 	- requires sources of SigViewer and MCE-W64, make suitesparse zlib qt 

##### Target: MacOSX w/ homebrew
## make install_homebrew	installs libbiosig and save2gdf
	# requires: brew tap homebrew/dupes
	#	    brew install libiconv

#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
ifeq (,$(findstring mingw,$(TARGET)))
    -include /usr/share/dpkg/buildflags.mk
endif

###############################
# whether dynamic or static linking is used, can be controlled with
# LIBEXT. Setting it to 'a' links statically, 'so' links dynamically
#
LIBEXT        = a
#LIBEXT	      = so
###############################

### User-specified options: its likely you want to change this
# settings for cross compiler: tested with MXE and mce-w64 with make suitesparse zlib qt ; 
CROSS   = $(HOME)/src/mxe.github.schloegl/usr/bin/i686-w64-mingw32.static
## local MXE
ifeq "$(wildcard $(CROSS)-gcc)" ""
CROSS   = $(HOME)/src/mxe.master/usr/bin/i686-w64-mingw32.static
endif
ifeq "$(wildcard $(CROSS)-gcc)" ""
CROSS         = $(shell pwd)/../mxe/usr/bin/i686-w64-mingw32.static
endif
ifeq "$(wildcard $(CROSS)-gcc)" ""
CROSS         = /usr/local/src/mxe/usr/bin/i686-w64-mingw32.static
endif

CROSS64       = $(HOME)/src/mxe.github.schloegl/usr/bin/x86_64-w64-mingw32.static
ifeq "$(wildcard $(CROSS64)-gcc)" ""
CROSS64       = $(HOME)/src/mxe.master/usr/bin/x86_64-w64-mingw32.static
endif
ifeq "$(wildcard $(CROSS64)-gcc)" ""
CROSS64       = $(HOME)/src/mxe.master/usr/bin/x86_64-w64-mingw32.static
endif

# settings for mex files
MEX_OPTION    = -largeArrayDims # turn on for 64 bit Matlab, otherwise empty
# directory for sources of sigviewer
PathToSigViewer = ../sigviewer
PathToSigViewerWIN32 = ../sigviewer4win32
PathToSigViewerWIN64 = ../sigviewer4win64

CFLAGS       += -pipe -fPIC -fno-builtin-memcmp -O2 -Wno-unused-result
CFLAGS       += -Wno-deprecated
CFLAGS       += -D_GNU_SOURCE
CFLAGS       += -D_XOPEN_SOURCE=700
CFLAGS       += -D_DEFAULT_SOURCE
CXXFLAGS     += $(CFLAGS)

prefix        = @prefix@
exec_prefix   = @exec_prefix@
datarootdir   = @datarootdir@
datadir       = @datadir@
libdir        = @libdir@
includedir    = @includedir@
bindir        = @bindir@
mandir        = @mandir@

## TARGET dependencies
ifeq (,$(TARGET))
	CC      ?= gcc
	CXX     ?= g++
	AR      := ar rcs
	PKGCONF := pkg-config
	SHAREDLIB = -shared
else ifeq (intel,$(TARGET))
	CC      := /opt/intel/bin/icc
	CXX     := /opt/intel/bin/icc
	LD      := /opt/intel/bin/xild
	AR      := /opt/intel/bin/xiar crs
else 
	PREFIX  := $(subst /bin/,/,$(dir $(shell which $(TARGET)-gcc)))
	CC      := $(TARGET)-gcc
	CXX     := $(TARGET)-g++
	LD      := $(TARGET)-ld
	AR      := $(TARGET)-ar rcs
	PKGCONF := $(TARGET)-pkg-config
	ifneq (,$(findstring mingw,$(TARGET)))
		## add gnulib's getlogin
		SOURCES += win32/getlogin.c win32/getline.c win32/getdelim.c
		OBJECTS += getlogin.o getline.o getdelim.o
		LDLIBS  += -lssp
		LDLIBS  += -liconv -liberty -lstdc++ -lws2_32
		BINEXT   = .exe
	endif
endif

SONAMEVERSION ?= 2

ifeq (Darwin,$(shell uname))
	## Homebrew:
	##	brew install gawk
	##	brew install gnu-tar
	##	brew install homebrew/dupes/libiconv
	##	brew install homebrew/science/suite-sparse
	##
	TAR	       = gtar
	LD	       = $(CXX) -shared
	CFLAGS        += -I$(prefix)/include
	CFLAGS        += -mmacosx-version-min=10.9
	LDLIBS        += -liconv -lstdc++
	SHAREDLIB      = -dylib -arch x86_64 #-macosx_version_min 10.9
	LDFLAGS       += -L$(prefix)/lib/
	DLEXT          = dylib
	FULLDLEXT      = .dylib
	SONAME_PREFIX  = -install_name # the last space character is crucial

else ifneq (,$(findstring CYGWIN, $(shell uname)))
	DLEXT = dll
	TAR	       = tar
	LD	       = $(CXX)
	LDLIBS        += -liconv -lstdc++
	FULLDLEXT      = ${SONAMEVERSION}.dll
	SONAME_PREFIX  = -Wl,-soname=

else ifneq (,$(findstring MINGW, $(shell uname)))
	## add gnulib's getlogin
	SOURCES += win32/getlogin.c win32/getline.c win32/getdelim.c
	OBJECTS += getlogin.o getline.o getdelim.o
	## exclude conflicting definition of sopen from mingw's io.h
	CC  = gcc
	CXX = g++
	CFLAGS  += -pipe -fPIC -D_REENTRANT -D=BUILD_DLL
	TAR	 = tar
	LD	 = $(CXX)
	DLEXT    = dll
	FULLDLEXT      = ${SONAMEVERSION}.dll
	SONAME_PREFIX  = -Wl,-soname=
	LDLIBS  += -lstdc++
	LDLIBS  += -liconv -lws2_32
	LDFLAGS += -Wl,-subsystem,windows
	LDFLAGS += -L$(prefix)/lib/

else
	TAR	       = tar
	LD	       = $(CXX)
	DLEXT          = so
	FULLDLEXT      = .so.${SONAMEVERSION}
	SONAME_PREFIX  = -Wl,-soname=
endif

DEFINES_ALL   = #-D=NDEBUG

DEFINES      += $(DEFINES_ALL)
#DEFINES      += -D=WITH_SON
#DEFINES      += -D=WITHOUT_SCP_DECODE
#DEFINES      += -D=WITH_TIMESTAMPEVENT
#DEFINES      += -D=WITH_TIMESTAMPCHANNEL
### Mingw,MXE, etc. must avoid name space conflict concerning SOPEN ###
#DEFINES      += -D=NO_OLDNAMES

#DEFINES      += -D=WITH_CURL
DEFINES      += -D=__4HAERTEL__
DEFINES      += -D=WITH_FAMOS
DEFINES      += -D=WITH_FIFF
DEFINES      += -D=WITHOUT_NETWORK
DEFINES      += -D=WITH_SCP3
DEFINES      += -D=WITH_FEF
#DEFINES      += -D=WITH_PDP
	### DICOM: DCMTK or GDCM, or internal
	# WITH_DCMTK is already configured by autoconf
	# WITH_GDCM is experimental
	# WITH_DICOM (internal implementation) is experimental
DEFINES      += -D=WITH_DICOM
#DEFINES      += -D=WITH_GDCM
#DEFINES      += -D=WITH_GSL
#DEFINES      += -D=WITH_EEPROBE
#DEFINES      += -D=WITH_TDMS
DEFINES      += -D=WITH_ATF
#DEFINES      += -D=WITH_AVI
#DEFINES      += -D=WITH_RIFF
#DEFINES      += -D=WITH_WAV
#DEFINES      += -D=WITH_NEV
DEFINES      += -D=MAKE_EDFLIB

ifeq (1,@HAVE_LIBZ@)
	LDLIBS     += -lz
endif
ifeq (1,@HAVE_LIBPTHREAD@)
	LDLIBS     += -lpthread
endif
ifeq (1,@HAVE_LIBCHOLMOD@)
	LDLIBS     += -lcholmod
	ifeq (Darwin,$(shell uname))
	  ## homebrew requires this in addition to cholmod
	  LDLIBS     += -lsuitesparseconfig
	endif
endif
ifeq (1,@HAVE_DCMTK@)
	DEFINES      += -D=WITH_DCMTK
	LDLIBS       += -ldcmdata -loflog -lofstd
	SOURCES      += t210/sopen_dcmtk_read.cpp
	OBJECTS      += sopen_dcmtk_read.o
	MinGWOBJECTS += win32/sopen_dcmtk_read.obj
endif
ifeq (1,@HAVE_LIBHDF5@)
	LDLIBS     += -lhdf5
endif
ifeq (1,@HAVE_LIBMATIO@)
	LDLIBS     += -lmatio
endif
ifeq (1,@HAVE_LIBM@)
	LDLIBS     += -lm
endif
ifeq (1,@HAVE_LIBTINYXML@)
	DEFINES    += -D=WITH_LIBTINYXML
	LDLIBS     += -ltinyxml
endif
ifeq (1,@HAVE_LIBTINYXML2@)
	DEFINES    += -D=WITH_LIBTINYXML2
	LDLIBS     += -ltinyxml2
endif

ifneq (,$(findstring WITH_GDCM, $(DEFINES)))
  INCPATH    += -I$(wildcard /usr/include/gdcm-2*/)
  LDLIBS     += -lgdcmCommon -lgdcmDSED -lgdcmMEXD -lgdcmMSFF -lgdcmDICT -lgdcmIOD
endif

ifneq (,$(findstring WITH_CURL, $(DEFINES)))
  LDLIBS     += -lcurl
endif
ifneq (,$(findstring stack-protector, $(CFLAGS)))
ifneq (,$(findstring CYGWIN, $(shell uname)))
  LDLIBS     += -lssp
endif 
ifneq (,$(findstring MINGW, $(shell uname)))
  LDLIBS     += -lssp
endif
endif

DELETE        = rm -f
COPY          = cp -f
DATA_DIR      = data/
DATA_DIR_CFS  = $(HOME)/L/data/test/cfs/
TEMP_DIR      = test/
SED           = sed
VERBOSE	     := -V0

##########################################################
## set Matlab and Octave variables
ifeq (undefined,$(origin MATLABDIR))
  ifeq (Darwin,$(shell uname))
    MATLABDIR := $(shell find /Applications/MATLAB* -name bin -type d -depth 1 2>/dev/null | head -1)
  else
    MATLABDIR ?= $(shell find /usr/local/MATLAB* -maxdepth 2 -name bin -type d 2>/dev/null | head -1)
  endif
endif

ifeq (,$(MATLABDIR))
  #$(warning MATLABDIR is not defined)
else
  MEX         = $(MATLABDIR)/mex
  MEX_EXT    := $(shell $(MATLABDIR)/mexext)
endif


# use environment variable to define OCTAVE_VERSION
#   e.g. export OCTAVE_VERSION=-3.6.2
# Octave - global install  (e.g. from debian package)
#OCTAVE_VERSION = 
# Octave - local install (e.g. compiled from sources) 
#OCTAVE_VERSION = -3.6.1

OCT           := mkoctfile$(OCTAVE_VERSION)
##########################################################

##########################################################
## set variables for Python
PYTHON         ?= python
PYTHONVER      := $(shell $(PYTHON) -c "import sys; print(sys.version[:3])")

##########################################################
## set variables for MinGW Crosscompiler: compile on linux binaries for windows
##
PathToMinGW   = $(dir $(CROSS))..$(nondir $(CROSS))
PathToMinGW64 = $(dir $(CROSS64))..$(nondir $(CROSS64))

MinGWCC      = $(CROSS)-gcc
MinGWCXX     = $(CROSS)-g++
MinGWDEF     = $(DEFINES) 
MinGWCFLAGS  = -pipe -fPIC  -D_REENTRANT -D=BUILD_DLL $(MinGWDEF) -Iwin32 -I$(PathToMinGW)/include/
MinGWLIBS    = win32/libbiosig.a -L$(PathToMinGW)/lib/ $(LDLIBS) # static
MinGWLINK    = $(MinGWCXX)

MinGW64CC      = $(CROSS64)-gcc
MinGW64CXX     = $(CROSS64)-g++
MinGW64CFLAGS  = -pipe -fPIC -O2 -D_REENTRANT -D=BUILD_DLL $(MinGWDEF) -Iwin64 -I$(PathToMinGW64)/include/
MinGW64LIBS    = win64/libbiosig.a -L$(PathToMinGW64)/lib/ $(LDLIBS) # static
MinGW64LINK    = $(MinGW64CXX)
##########################################################

# Versioning
MAJOR_VERSION  := $(word 3, $(shell grep '\#define BIOSIG_VERSION_MAJOR' biosig-dev.h))
MINOR_VERSION  := $(word 3, $(shell grep '\#define BIOSIG_VERSION_MINOR' biosig-dev.h))
STEPPING       := $(word 3, $(shell grep '\#define BIOSIG_PATCHLEVEL' biosig-dev.h))
BIOSIG_VERSION := ${MAJOR_VERSION}.${MINOR_VERSION}.${STEPPING}
TODAY          := $(shell date +%Y%m%d)

####### External directory
ifneq (`ls -l extern |wc -l`,1)
  EXTERN  = extern
else
  EXTERN  = ../biosig4matlab/doc
endif

####### Output directory
OBJ	      = ./obj

####### Files
SOURCES      += biosig.c \
		biosig2.c \
		t210/sopen_cfs_read.c \
		t210/sopen_heka_read.c \
		t210/sopen_igor.c \
		t210/sopen_scp_read.c \
		t210/sopen_tdms_read.c \
		t210/sopen_famos_read.c \
		t210/sopen_abf_read.c \
		t210/sopen_alpha_read.c \
		t210/sopen_axg_read.c \
		t210/scp-decode.cpp \
		t220/sopen_scp_write.c \
		t220/crc4scp.c \
		t230/sopen_hl7aecg.cpp \
		t240/sopen_fef_read.c \
		test0/sandbox.c \
		xgethostname.c \
		gdftime.c \
		mdc_ecg_codes.c \
		physicalunits.c \
		save2gdf.c \
		biosig_client.c \
		biosig_server.c

ifneq (1,@HAVE_LIBTINYXML@)
  ## build with internal TinyXML when libtinyxml is not available
  SOURCES    +=	XMLParser/tinyxml.cpp \
		XMLParser/tinyxmlparser.cpp \
		XMLParser/tinyxmlerror.cpp \
		XMLParser/tinystr.cpp 
endif

OBJECTS      += \
		biosig.o \
		biosig2.o \
		crc4scp.o \
		sopen_cfs_read.o \
		sopen_heka_read.o \
		sopen_igor.o \
		sopen_scp_read.o \
		sopen_abf_read.o \
		sopen_alpha_read.o \
		sopen_axg_read.o \
		sopen_scp_write.o \
		sopen_hl7aecg.o \
		gdftime.o \
		mdc_ecg_codes.o \
		physicalunits.o \
		sandbox.o \
		xgethostname.o

MinGWOBJECTS  = \
		win32/biosig.obj \
		win32/biosig2.obj \
		win32/crc4scp.obj \
		win32/getlogin.obj \
		win32/sopen_cfs_read.obj \
		win32/sopen_heka_read.obj \
		win32/sopen_igor.obj \
		win32/sopen_scp_read.obj \
		win32/sopen_abf_read.obj \
		win32/sopen_alpha_read.obj \
		win32/sopen_axg_read.obj \
		win32/sopen_scp_write.obj \
		win32/sopen_hl7aecg.obj \
		win32/gdftime.obj \
		win32/physicalunits.obj \
		win32/sandbox.obj \
		win32/xgethostname.obj

ifneq (,$(findstring WITH_FAMOS, $(DEFINES)))
  OBJECTS      += sopen_famos_read.o
endif
ifneq (,$(findstring WITH_FAMOS, $(MinGWDEF)))
  MinGWOBJECTS += win32/sopen_famos_read.obj
endif

ifneq (,$(findstring WITH_FEF, $(DEFINES)))
  -include t240/modules.mk
  SOURCES += $(patsubst %.c,t240/%.c,${ASN_MODULE_SOURCES})
  OBJECTS += $(patsubst %.c,%.o,${ASN_MODULE_SOURCES})
  OBJECTS      += sopen_fef_read.o
endif
ifneq (,$(findstring WITH_FEF, $(MinGWDEF)))
  MinGWOBJECTS += win32/sopen_fef_read.obj
endif

ifneq (,$(findstring WITH_FEF, $(DEFINES)))
  OBJECTS      += sopen_tdms_read.o
endif
ifneq (,$(findstring WITH_FEF, $(MinGWDEF)))
  MinGWOBJECTS += win32/sopen_tdms_read.obj
endif

ifeq (,$(findstring WITHOUT_NETWORK, $(DEFINES)))
  OBJECTS      += biosig-network.o
endif
ifeq (,$(findstring WITHOUT_NETWORK, $(MinGWDEF)))
  MinGWOBJECTS += win32/biosig-network.obj
endif

ifeq (,$(findstring WITHOUT_SCP_DECODE, $(DEFINES)))
  OBJECTS      += scp-decode.o
endif
ifeq (,$(findstring WITHOUT_SCP_DECODE, $(MinGWDEF)))
  MinGWOBJECTS += win32/scp-decode.obj
endif

ifeq (,$(findstring WITH_LIBTINYXML, $(DEFINES)))
  ## build with TinyXML when libtinyxml is not available
  OBJECTS    += tinyxml.o tinyxmlparser.o tinyxmlerror.o tinystr.o 
  MinGWOBJECTS += win32/tinyxml.obj win32/tinyxmlparser.obj win32/tinyxmlerror.obj win32/tinystr.obj 
endif


MinGW64OBJECTS  = $(patsubst win32/%.obj, win64/%.obj, $(MinGWOBJECTS))

LIB_OBJECTS  = libbiosig.a libgdf.a libphysicalunits.a libbiosig.pc
ifeq (,$(findstring mingw,$(TARGET)))
    LIB_OBJECTS += libbiosig.$(DLEXT) libgdf.$(DLEXT) libphysicalunits.$(DLEXT)
endif

first: $(IO_H_FILE2) lib tools
ifeq (1,@HAVE_LIBB64@)
  tools: save2gdf${BINEXT} pu${BINEXT} biosig_fhir${BINEXT}
else
  tools: save2gdf${BINEXT} pu${BINEXT}
endif
all:   first mex4o python R #win32 win64 #sigviewer #win32/sigviewer.exe win64/sigviewer.exe #biosig_client biosig_server mma java tcl perl php ruby #sigviewer

.PHONY : libbiosig lib python mex4m mex4o

libbiosig lib: $(LIB_OBJECTS)

#############################################################
#	Compilation: Implicit, default rules
#############################################################

vpath %.c ./:./t210:./t220:./t240:./test0:./src:./mma

vpath %.cpp ./:./t210:./t230:./XMLParser:./mex

.SUFFIXES: .o .c .cpp .cc .cxx .C


%.o: t240/%.c biosig-dev.h gdftime.h physicalunits.h
	$(CC) -c $(DEFINES) $(CFLAGS) -I t240 $(INCPATH) -o "$@" "$<"

%.c: %.h biosig-dev.h gdftime.h physicalunits.h

%.cpp: %.h biosig-dev.h gdftime.h physicalunits.h

%.o: %.c
	$(CC) -c $(DEFINES) $(CFLAGS)  $(INCPATH) -o "$@" "$<"

%.o : win32/%.c
	$(CC) -c $(DEFINES) $(CFLAGS)  $(INCPATH) -o "$@" "$<"

%.o: %.cpp biosig.h biosig-dev.h
	$(CXX) -c $(DEFINES) $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

sandbox.o: test0/sandbox.c biosig.h
	$(CXX) -c $(DEFINES) $(CXXFLAGS) $(INCPATH) -o "$@" "$<"


win32/%.obj: t240/%.c biosig-dev.h
	$(MinGWCC) -c $(DEFINES) $(MinGWCFLAGS) -I t240 -o "$@" "$<"
win32/%.obj: %.c
	$(MinGWCC) -c $(DEFINES) $(MinGWCFLAGS) -o "$@" "$<"
win32/%.obj: %.cpp
	$(MinGWCXX) -c $(DEFINES) $(MinGWCFLAGS) -o "$@" "$<"

win32/%.obj: win32/%.c 
	$(MinGWCC) -c $(DEFINES) $(MinGWCFLAGS) -I win32 -o "$@" "$<"


win64/%.obj: t240/%.c biosig-dev.h
	$(MinGW64CC) -c $(DEFINES) $(MinGW64CFLAGS) -I t240 -o "$@" "$<"
win64/%.obj: %.c
	$(MinGW64CC) -c $(DEFINES) $(MinGW64CFLAGS) -o "$@" "$<"
win64/%.obj: %.cpp
	$(MinGW64CXX) -c $(DEFINES) $(MinGW64CFLAGS) -o "$@" "$<"

win64/%.obj: win32/%.c 
	$(MinGW64CC) -c $(DEFINES) $(MinGW64CFLAGS) -I win32 -o "$@" "$<"



#############################################################
#	eventcodes and units: conversion from ascii to C code
#############################################################

biosig.c: eventcodes.i eventcodegroups.i 11073-10102-AnnexB.i biosig.h biosig-dev.h

physicalunits.c: units.i physicalunits.h

eventcodes.i eventcodegroups.i : $(EXTERN)/eventcodes.txt
	gawk -f eventcodes.awk "$<"

units.i : $(EXTERN)/units.csv
	awk -f units.awk "$<" > "$@"

11073-10102-AnnexB.i : $(EXTERN)/11073-10102-AnnexB.txt
	awk -f annotatedECG.awk "$<" > "$@"


#############################################################
#	Compilation: exceptions, explicit rules
#############################################################

gdf.o: biosig.c biosig-dev.h eventcodes.i units.i 11073-10102-AnnexB.i
	$(CC) -c -D=ONLYGDF -D=WITHOUT_NETWORK $(DEFINES) $(CFLAGS) $(INCPATH) -o "$@" "$<"

#getlogin_r.o: win32/getlogin_r.c
#	$(CC) -c $(DEFINES) $(CFLAGS) $(INCPATH) -o "$@" "$<"

########### WIN32 ##################

win32/gdf.obj: biosig.c biosig-dev.h
	$(MinGWCC) -c -D=ONLYGDF $(DEFINES) $(MinGWCFLAGS) -o "$@" "$<"

$(PathToSigViewerWIN32): 
	svn co https://sigviewer.svn.sourceforge.net/svnroot/sigviewer/trunk ../sigviewer4win32

$(PathToSigViewerWIN32)/src/src.pro: $(PathToSigViewerWIN32)
	svn up -r 557 ../sigviewer4win32

win32/sigviewer.exe: win32/libbiosig.a win32/libbiosig.dll
	#-$(COPY) ../biosig/doc/eventcodes.txt $(PathToSigViewerWIN32)/src/
	-$(DELETE) $(PathToSigViewerWIN32)/extern/include/*.h
	-$(COPY) biosig.h biosig2.h biosig-dev.h physicalunits.h $(PathToSigViewerWIN32)/extern/include
	-$(DELETE) $(PathToSigViewerWIN32)/extern/lib/lib*
	-$(COPY) win32/libbiosig.a $(PathToSigViewerWIN32)/extern/lib
	$(SED) -i 's|\([[:space:]]*-lbiosig\)\([ #\\]*\)$$|\1 -lcholmod -lz -lcurl \2|' $(PathToSigViewerWIN32)/src/src.pro
	echo 0.5.2-v${BIOSIG_VERSION} > $(PathToSigViewerWIN32)/src/version.txt
	-(cd $(PathToSigViewerWIN32)/src; $(CROSS)-qmake; make)
	#-(cd $(PathToSigViewerWIN32); svn revert -R .; svn up -r 557; patch -p0 <../biosig4c++/patches/patch_sigviewer_0.5.2.diff; cd src; $(CROSS)-qmake; $(MAKE);)
	-$(COPY) $(PathToSigViewerWIN32)/bin/release/sigviewer.exe win32/sigviewer.exe

########### WIN64 ##################

win64/gdf.obj: biosig.c biosig-dev.h
	$(MinGW64CC) -c -D=ONLYGDF $(DEFINES) $(MinGW64CFLAGS) -o "$@" "$<"

$(PathToSigViewerWIN64): 
	svn co https://sigviewer.svn.sourceforge.net/svnroot/sigviewer/trunk ../sigviewer4win64

$(PathToSigViewerWIN64)/src/src.pro: $(PathToSigViewerWIN64) 
	svn up -r 557 ../sigviewer4win64

win64/sigviewer.exe: win64/libbiosig.a win64/libbiosig.dll 
	#-$(COPY) ../biosig/doc/eventcodes.txt $(PathToSigViewerWIN64)/src/
	-$(DELETE) $(PathToSigViewerWIN64)/extern/include/*.h
	-$(COPY) biosig.h biosig2.h biosig-dev.h physicalunits.h $(PathToSigViewerWIN64)/extern/include
	-$(DELETE) $(PathToSigViewerWIN64)/extern/lib/lib*
	-$(COPY) win64/libbiosig.a $(PathToSigViewerWIN64)/extern/lib
	$(SED) -i 's|\([[:space:]]*-lbiosig\)\([ #\\]*\)$$|\1 -lcholmod -lz -lcurl\2|' $(PathToSigViewerWIN64)/src/src.pro
	echo 0.5.2-v${MAJOR_VERSION}.${MINOR_VERSION}.${STEPPING} > $(PathToSigViewerWIN64)/src/version.txt
	-(cd $(PathToSigViewerWIN64)/src; $(CROSS64)-qmake; make)
	#-(cd $(PathToSigViewerWIN64); svn revert -R .; svn up -r 557; patch -p0 <../biosig4c++/patches/patch_sigviewer_0.5.2.diff; cd src; $(CROSS64)-qmake; $(MAKE);)
	-$(COPY) $(PathToSigViewerWIN64)/bin/release/sigviewer.exe win64/sigviewer.exe


#############################################################
#	other language bindings (on Linux)
#############################################################

## biosig4python based on module extensions
biosig4python : python

python:
	make -C python build

install_python: libbiosig.$(LIBEXT)
	make -C python install


java: libbiosig.$(LIBEXT) java/biosig.i
	$(MAKE) -C java 
perl: libbiosig.$(LIBEXT) perl/biosig.i
	$(MAKE) -C perl 
php: libbiosig.$(LIBEXT) php/biosig.i
	$(MAKE) -C php 


ruby/biosig_wrap.c: ruby/biosig.i
	(cd ruby && swig -ruby biosig.i)
ruby/Makefile: ruby/extconf.rb
	ruby -C ruby extconf.rb
biosig4ruby ruby: libbiosig.$(LIBEXT) ruby/Makefile ruby/biosig_wrap.c
	$(MAKE) -C ruby 


tcl: libbiosig.$(LIBEXT) tcl/biosig.i
	$(MAKE) -C tcl 


#############################################################
#	WIN32 - BUILD
#############################################################

# libraries are built in MXE

## save2gdf, pdp2gdf, pu
win32/%.exe: %.c
	$(MinGWCXX) $(DEFINES) $(MinGWCFLAGS) "$<" $(MinGWLIBS) -o "$@"

win32: mexw32 win32mma


#############################################################
#	WIN64 - BUILD
#############################################################

# Libraries are build in MXE

## save2gdf, pdp2gdf, pu
win64/%.exe: %.c
	$(MinGW64CXX) $(DEFINES) $(MinGW64CFLAGS) "$<" $(MinGW64LIBS) -o "$@"

win64: mexw64


#############################################################
#	GNU/Linux - Shared Libaries
#############################################################

ifneq (Darwin,$(shell uname))
%.${DLEXT}:%${FULLDLEXT}
	ln -sf "$<" "$@"
endif

libbiosig${FULLDLEXT}:$(OBJECTS)
	$(LD) ${SONAME_PREFIX}"$@" $^ $(SHAREDLIB) $(LDFLAGS) $(LDLIBS) -o "$@"

libgdf${FULLDLEXT}: gdf.o gdftime.o physicalunits.o getlogin.o
	$(LD) ${SONAME_PREFIX}"$@" $^ $(SHAREDLIB) $(LDFLAGS) $(LDLIBS) -o "$@"

libgdftime${FULLDLEXT}:gdftime.o
	$(LD) ${SONAME_PREFIX}"$@" $^ $(SHAREDLIB) $(LDFLAGS) $(LDLIBS) -o "$@"

libphysicalunits${FULLDLEXT}:physicalunits.o
	$(LD) ${SONAME_PREFIX}"$@" $^ $(SHAREDLIB) $(LDFLAGS) $(LDLIBS) -o "$@"


#############################################################
#	Static Libaries
#############################################################

libphysicalunits.a: physicalunits.o
	-$(DELETE) "$@"
	$(AR) "$@" "$<"
libphysicalunits.def: physicalunits.o
	$(LD) -o libphysicalunits.dll -s -shared -fPIC "$<" $(LDLIBS) -Wl,-subsystem,windows,--output-def,libphysicalunits.def,--out-implib,libphysicalunits.dll.a

#libgdftime.a: gdftime.o
#	-$(DELETE) "$@"
#	$(AR) "$@" "$<"
#libgdftime.def: gdftime.o
#	$(CC) -o libgdftime.dll -s -shared -fPIC "$@" -Wl,--subsystem,windows,--output-def,libgdftime.def,--out-implib,libgdftime.dll.a

libgdf.a: gdf.o getlogin.o gdftime.o physicalunits.o
	-$(DELETE) "$@"
	$(AR) "$@" gdf.o getlogin.o gdftime.o physicalunits.o
libgdf.def: gdf.o getlogin.o gdftime.o physicalunits.o
	$(LD) -s -shared -fPIC -o libgdf.dll gdf.o getlogin.o gdftime.o physicalunits.o $(LDLIBS) -Wl,-subsystem,windows,--output-def,libgdf.def,--out-implib,libgdf.dll.a

libbiosig.a: $(OBJECTS) libbiosig.pc
	-$(DELETE) libbiosig.a
	$(AR) libbiosig.a $(OBJECTS)
libbiosig.def: $(OBJECTS) libbiosig.pc
	$(LD) -o libbiosig.dll -s -shared -fPIC $(OBJECTS) $(LDLIBS) -Wl,-subsystem,windows,--output-def,libbiosig.def,--out-implib,libbiosig.dll.a

libbiosig.pc :
	mkdir -p pkgconfig
	echo "# Defines libbiosig.pc"       > "$@"
	echo "prefix=$(prefix)"             >>"$@"
	echo "exec_prefix=$(prefix)"        >>"$@"
	echo "libdir=$(libdir)"         >>"$@"
	echo "includedir=$(includedir)" >>"$@"
	echo                                >>"$@"
	echo "Name: libbiosig"		    >>"$@"
	echo "Description: Biosig library"  >>"$@"
	echo "Version: ${MAJOR_VERSION}.${MINOR_VERSION}.${STEPPING}" >>"$@"
	echo "URL: http://biosig.sf.net"    >>"$@"
	#
	echo "Requires: "                   >> "$@"
	echo "Requires.private: "           >> "$@"
	echo "Cflags: $(DEFINES) -I$(includedir)"  >> "$@"
	echo "Libs: -L$(libdir) -lbiosig"      >> "$@"
	echo "Libs.private: $(LDLIBS)"      >> "$@"


## save2gdf, pdp2gdf
%${BINEXT}: %.c libbiosig.a
	$(CC) $(DEFINES) $(CFLAGS) "$<" libbiosig.a -lstdc++ $(LDLIBS) -o "$@"

pu${BINEXT} : pu.c physicalunits.o
	$(CC) $(DEFINES) $(CFLAGS) $^ $(LDFLAGS) $(LDLIBS) -o "$@"

biosig_fhir${BINEXT}: biosig_fhir.c libbiosig.a
	$(CC) $(DEFINES) $(CFLAGS) "$<" libbiosig.a -lstdc++ -lb64 $(LDLIBS) -o "$@"

bscs: biosig_client${BINEXT} biosig_server${BINEXT} sandbox.o biosig.o
biosig_client${BINEXT}: biosig_client.c libbiosig.$(LIBEXT) biosig-network.o
	$(CC) $(CFLAGS) biosig_client.c biosig-network.o libbiosig.$(LIBEXT) $(LDFLAGS) $(LDLIBS) -o "$@"

biosig_server${BINEXT}: biosig_server.c libbiosig.$(LIBEXT) biosig-network.o
	$(CC) $(CFLAGS) biosig_server.c biosig-network.o libbiosig.$(LIBEXT) $(LDFLAGS) $(LDLIBS) -o "$@"


#############################################################
#	MathLink interface to Mathematica
#############################################################

mmaall: mma/sload.tm mma/sload.c
	$(MAKE) -C mma CROSS=$(CROSS) CROSS64=$(CROSS64) LDLIBS="$(LDLIBS) -liconv" all

mma : mma/sload.exe

mma/sload.exe: mma/sload.tm mma/sload.c
	$(MAKE) -C mma CROSS=$(CROSS) CROSS64=$(CROSS64) mma

win32mma: mma/sload.tm mma/sload.c
	$(MAKE) -C mma CROSS=$(CROSS) LDLIBS="$(LDLIBS) -liconv" win32mma

win64mma: mma/sload.tm mma/sload.c
	$(MAKE) -C mma CROSS64=$(CROSS64) LDLIBS="$(LDLIBS) -liconv" win64mma


#############################################################
#	MEX-files for Octave and Matlab
#############################################################

# include directory for Win32-Matlab include
W32MAT_INC = $(HOME)/bin/win32/Matlab/R2010b/extern/include/ -I../win32
W64MAT_INC = $(HOME)/bin/win64/Matlab/R2010b/extern/include/ -I../win64
# path to GNUMEX libraries, available from here http://sourceforge.net/projects/gnumex/
GNUMEX   = $(HOME)/bin/win32/gnumex
GNUMEX64 = $(HOME)/bin/win64/gnumex

mex/mexSOPEN.cpp : mex/mexSLOAD.cpp
	echo "#define mexSOPEN" > mex/mexSOPEN.cpp
	cat mex/mexSLOAD.cpp >> mex/mexSOPEN.cpp

MEX_OBJECTS = mex/mexSLOAD.cpp mex/mexSOPEN.cpp mex/mexSSAVE.cpp

mex4o: $(patsubst mex/%.cpp, mex/%.mex, $(MEX_OBJECTS)) $(OBJECTS)
oct: $(patsubst mex/%.cpp, mex/%.oct, $(MEX_OBJECTS)) $(OBJECTS)
mexw32: $(patsubst mex/%.cpp, mex/%.mexw32, $(MEX_OBJECTS))
mexw64: $(patsubst mex/%.cpp, mex/%.mexw64, $(MEX_OBJECTS))

ifdef MEX_EXT
mex: mex4o mex4m mexw32 mexw64 $(OBJECTS)
mex4m: $(patsubst mex/%.cpp, mex/%.$(MEX_EXT), $(MEX_OBJECTS)) $(OBJECTS)

ifneq (Darwin,$(shell uname))
mex/%.$(MEX_EXT): mex/%.cpp libbiosig.a
	$(MEX) -I./ $(MEX_OPTION) $(shell $(PKGCONF) --cflags libbiosig) "$<" libbiosig.a $(LDLIBS) -output "$@"
else
mex/%.$(MEX_EXT): mex/%.cpp libbiosig.a
	$(MEX) -I./ $(shell $(PKGCONF) --cflags libbiosig) -outdir mex "$<" libbiosig.a $(LDLIBS)
endif
	-$(COPY) $@ ../biosig4matlab/t200_FileAccess/

else
mex: mex4o mexw32 mexw64
endif

mex/%.mex: mex/%.cpp libbiosig.a
	$(OCT) -I./ $(shell $(PKGCONF) --cflags libbiosig) -v -g --mex "$<" libbiosig.a $(LDLIBS) -o "$@"
	-$(COPY) $@ ../biosig4matlab/t200_FileAccess/

mex/%.oct: mex/%.cpp libbiosig.a
	$(OCT) -I./ $(shell $(PKGCONF) --cflags libbiosig) "$<" libbiosig.a $(LDLIBS) -o "$@"
	-$(COPY) $@ ../biosig4matlab/t200_FileAccess/

mex/%.mexw32: mex/%.cpp Makefile biosig.h biosig-dev.h
	## $(CROSS)-g++ is used instead of $(CXX), so it can be called from biosig as well as mxe. 
	$(CROSS)-g++ -shared $(GNUMEX)/mex.def -DMATLAB_MEX_FILE $(DEFINES) -x c++  \
		-I$(W32MAT_INC) -O2 -o "$@" -L$(GNUMEX) -s "$<" -llibmx -llibmex -lbiosig -liconv -lssp $(LDLIBS) -lws2_32
	-$(COPY) $@ ../biosig4matlab/t200_FileAccess/

mex/%.mexw64: mex/%.cpp Makefile biosig.h biosig-dev.h
	$(CROSS64)-g++ -shared $(GNUMEX64)/mex.def -DMATLAB_MEX_FILE $(DEFINES) -x c++  \
		-I$(W64MAT_INC) -O2 -o "$@" -L$(GNUMEX64) -s "$<" -llibmx -llibmex -lbiosig -liconv -lssp $(LDLIBS) -lws2_32
	-$(COPY) $@ ../biosig4matlab/t200_FileAccess/

mexbiosig: mex/mexbiosig-$(BIOSIG_VERSION).src.tar.gz


mex/mexbiosig-$(BIOSIG_VERSION).src.tar.gz: mex/*.cpp
	$(eval $@_TMP := $(shell mktemp -d /tmp/biosig.XXXXXX)/mexbiosig-$(BIOSIG_VERSION))
	echo "$($@_TMP) generated";
	$(shell mkdir -p $($@_TMP)/doc/)
	$(shell mkdir -p $($@_TMP)/inst/)
	$(shell mkdir -p $($@_TMP)/src/)
	cp mex/README $($@_TMP)/doc/
	sed -e 's#^Version.*$$#Version: $(BIOSIG_VERSION)#g' -e 's#^Date.*$$#Date: '$(shell date +%Y-%m-%d)'#g' mex/DESCRIPTION > $($@_TMP)/DESCRIPTION
	cp COPYING $($@_TMP)/
	cp mex/mex*.cpp mex/Makefile $($@_TMP)/src/
	### TODO
	#touch $($@_TMP)/src/mex{SOPEN,SLOAD,SSAVE}.m
	echo "mexSLOAD mexSOPEN mexSSAVE" > $($@_TMP)/INDEX
	$(TAR) cvfz "$@" $($@_TMP)/../mexbiosig-$(BIOSIG_VERSION)
	-(cp "$@" ~/L/public_html/biosig/prereleases/ )
	-rm -rf $(shell dirname $($@_TMP) )

#############################################################
#	R/sload
#############################################################

R: R/sload.$(DLEXT)

R/sload.$(DLEXT): R/sload.c libbiosig.$(DLEXT)
	(cd R; R CMD SHLIB sload.c -L.. -lbiosig)


#############################################################
#	SigViewer
#############################################################

sigviewer: $(PathToSigViewer)/bin/sigviewer 
	$(COPY) $(PathToSigViewer)/bin/release/sigviewer bin/sigviewer-$(TODAY)
	ln -sf sigviewer-$(TODAY) bin/sigviewer 

$(PathToSigViewer): 
	svn co https://sigviewer.svn.sourceforge.net/svnroot/sigviewer/trunk ../sigviewer

$(PathToSigViewer)/src/src.pro: $(PathToSigViewer) 
	svn up -r 557 ../sigviewer

$(PathToSigViewer)/bin/sigviewer: libbiosig.$(LIBEXT) biosig-dev.h biosig.h
	-$(DELETE) $(PathToSigViewer)/extern/include/*
	-$(COPY) biosig-dev.h biosig.h biosig2.h physicalunits.h $(PathToSigViewer)/extern/include
	-$(DELETE) $(PathToSigViewer)/extern/lib/lib*
	-$(COPY) libbiosig.a $(PathToSigViewer)/extern/lib
	$(SED) -i 's|\([[:space:]]*-lbiosig\)\([ #\\]*\)$$|\1 $(LDLIBS) \2|' $(PathToSigViewer)/src/src.pro
	echo 0.5.2-v${BIOSIG_VERSION} > $(PathToSigViewer)/src/version.txt
	(cd $(PathToSigViewer)/src; qmake; $(MAKE);)
	#-(cd $(PathToSigViewer); svn revert -R .; svn up -r 557; patch -p0 <../biosig4c++/patches/patch_sigviewer_0.5.2.diff; cd src; qmake; $(MAKE);)
	-$(COPY) $(PathToSigViewer)/bin/release/sigviewer bin/sigviewer

docs: 	docs/save2gdf.txt  docs/mexSLOAD.txt
	asciidoc -d manpage docs/save2gdf.txt
	asciidoc -d manpage docs/mexSLOAD.txt


# for backward compatibility
save2scp: save2gdf
save2aecg: save2gdf


#############################################################
#	APPLICATIONS: TTL2TRIG, FLOWMON
#############################################################

###	TTL2TRIG
ttltrig: bin/ttltrig
bin/ttl2trig : src/ttl2trig.c gdf.o gdftime.o physicalunits.o
	$(CC) -D=WITH_BIOSIG $(DEFINE) $(CFLAGS) $^ -Wall -Wextra -lasound $(LDLIBS) -o "$@"

###	FLOWMON
flowmon.o: src/flowmon2.c 
	$(CC) -c $(DEFINES) $(CFLAGS) -o "$@" "$<"
        
flowmon: bin/flowmon
bin/flowmon: flowmon.o gdf.o gdftime.o physicalunits.o
	$(CC) $^ $(LDLIBS) -o "$@"


#############################################################
#	INSTALL and DE-INSTALL
#############################################################

.PHONY: clean distclean install install_libbiosig remove install_sigviewer asc bin testscp testhl7 testbin test test6 zip

distclean: clean
	-$(DELETE) -r autom4te.cache
	-$(DELETE) aclocal.m4 config.h config.log config.status depcomp missing stamp-h1
	-$(DELETE) Makefile.am doc/Makefile.am mex/Makefile.am python/Makefile.am
	-$(DELETE) ltmain.sh
	-$(DELETE) *.lib
	-$(DELETE) *.so *.dylib
	-$(DELETE) *.so.*
	-$(DELETE) mex/mexSOPEN.cpp
	-$(DELETE) libbiosig.pc
	-$(DELETE) t5.scp t6.scp save2gdf gztest test_scp_decode biosig_server biosig_client
	-$(DELETE) t?.[bge]df* t?.hl7* t?.scp* t?.cfw* t?.gd1* t?.*.gz *.fil $(TEMP_DIR)t1.* $(DATA_DIR)t1.*
	-$(DELETE) python/swig_wrap.* python/biosig.py* python/_biosig.so python/biosig2.py* python/_biosig2.so
	-$(DELETE) python/*_wrap.*
	-$(DELETE) QMakefile
	-$(DELETE) igor/libIgor.a
	-$(DELETE) win32/*.a win32/*.lib win32/libbiosig.* win32/*.obj win32/*.exe
	-$(DELETE) win64/*.a win64/*.lib win64/libbiosig.* win64/*.obj win64/*.exe
	-$(DELETE) -rf win32/zlib
	-$(DELETE) mex/*.o mex/*.obj mex/*.o64 mex/core mex/octave-core mex/*.oct mex/*~ mex/*.mex* 
	-$(MAKE) -C java clean
	-$(MAKE) -C matlab clean
	-$(MAKE) -C mma clean
	-$(MAKE) -C perl clean
	-$(MAKE) -C php clean
	-$(MAKE) -C python clean
	-$(MAKE) -C R clean
	-$(MAKE) -C ruby clean
	-$(MAKE) -C tcl clean

clean:
	-$(DELETE) *~
	-$(DELETE) *.a
	-$(DELETE) *.def
	-$(DELETE) *.dll
	-$(DELETE) *.dll.a
	-$(DELETE) *.i
	-$(DELETE) *.o
	-$(DELETE) *.so *.dylib
	-$(DELETE) *.so.*
	-$(DELETE) *.mex*
	-$(DELETE) *.oct
	-$(DELETE) libbiosig.pc
	-$(DELETE) $(TEMP_DIR)t1.*
	-$(DELETE) python/biosig.py* _biosig.so python/biosig2.py* _biosig2.so
	-$(DELETE) python/swig_wrap.* python/biosig2_wrap.*
	-$(DELETE) win32/*.exe win32/*.o* win32/*.lib win32/*.a
	-$(DELETE) win64/*.exe win64/*.o* win64/*.lib win64/*.a
	-$(DELETE) t240/*.o*
	-$(DELETE) t240/libcnsfef.a
	-$(DELETE) libbiosig.* libbiosig2.*
	-$(DELETE) pdp2gdf save2gdf pu biosig_fhir
	-$(DELETE) mma/sload.exe mma/*/sload.exe
	-$(DELETE) mex/*.o mex/*.obj mex/*.o64 mex/core mex/octave-core mex/*.oct mex/*~ mex/*.mex* 
	-$(MAKE) -C java clean
	-$(MAKE) -C matlab clean
	-$(MAKE) -C mma clean
	-$(MAKE) -C php clean
	-$(MAKE) -C perl clean
	-$(MAKE) -C R clean
	-$(MAKE) -C ruby clean
	-$(MAKE) -C tcl clean

install_sigviewer: sigviewer
	install $(PathToSigViewer)/bin/release/sigviewer $(DESTDIR)$(bindir)

install_ttl2trig: bin/ttl2trig
	install bin/ttl2trig $(DESTDIR)$(bindir)

install_headers: biosig-dev.h biosig.h biosig2.h gdftime.h physicalunits.h
	install -d 			$(DESTDIR)$(includedir)
	install $?		    	$(DESTDIR)$(includedir)

install_libbiosig: install_libbiosig.$(DLEXT) install_libbiosig.a

install_libbiosig.a: libbiosig.a libphysicalunits.a libbiosig.pc
	install -d 			$(DESTDIR)$(libdir)/pkgconfig/
	install libbiosig.a		$(DESTDIR)$(libdir)
	install libphysicalunits.a 	$(DESTDIR)$(libdir)
	install libbiosig.pc		$(DESTDIR)$(libdir)/pkgconfig/

install_libbiosig.$(DLEXT): libbiosig$(FULLDLEXT) libbiosig.$(DLEXT)
	install -d 			$(DESTDIR)$(libdir)/pkgconfig/
	install libbiosig$(FULLDLEXT)  $(DESTDIR)$(libdir)
	(cd $(DESTDIR)$(libdir) && ln -sf libbiosig$(FULLDLEXT) libbiosig2.$(DLEXT))
ifneq (Darwin,$(shell uname))
	cd $(DESTDIR)$(libdir) && ln -sf libbiosig$(FULLDLEXT) libbiosig.$(DLEXT)
endif

install: install_libbiosig.$(DLEXT) install_libbiosig.a install_headers install_tools

install_tools: tools doc/save2gdf.1
	install -d 			$(DESTDIR)$(bindir)
	install save2gdf${BINEXT}	$(DESTDIR)$(bindir)
	install heka2itx  		$(DESTDIR)$(bindir)
	install save2aecg 		$(DESTDIR)$(bindir)
	install save2scp  		$(DESTDIR)$(bindir)
	install pu${BINEXT}		$(DESTDIR)$(bindir)
	-install biosig_fhir${BINEXT}	$(DESTDIR)$(bindir)   # not available on MacOSX because libb64 required
	mkdir -p  			$(DESTDIR)$(mandir)/man1
	install doc/*.1  		$(DESTDIR)$(mandir)/man1
	#install libbiosig.man 		$(mandir)
	#$(MAKE)  install_sigviewer
	#$(MAKE)  install_octave

uninstall_tools:
	${RM} $(DESTDIR)$(bindir)/save2gdf${BINEXT}
	${RM} $(DESTDIR)$(bindir)/pu${BINEXT}
	${RM} $(DESTDIR)$(bindir)/heka2itx
	${RM} $(DESTDIR)$(bindir)/save2aecg
	${RM} $(DESTDIR)$(bindir)/save2scp
	${RM} $(DESTDIR)$(bindir)/biosig_fhir${BINEXT}
	${RM} $(DESTDIR)$(mandir)/man1/pu.1
	${RM} $(DESTDIR)$(mandir)/man1/save2gdf.1
	${RM} $(DESTDIR)$(mandir)/man1/heka2itx.1

### Install mexbiosig for Octave
install_mexbiosig install_octave: mex/mexbiosig-$(BIOSIG_VERSION).src.tar.gz
	PKG_CONFIG_LIBDIR=$(libdir)/pkgconfig octave$(VERSION) --eval "pkg install $<"

uninstall_mexbiosig:
	-PKG_CONFIG_LIBDIR=$(libdir)/pkgconfig octave$(VERSION) --eval "pkg uninstall mexbiosig"

### Install mexbiosig for Matlab and Octave
install_mex: mex4m mex4o
	mkdir -p $(DESTDIR)$(datadir)/biosig/mex
	install mex/mex*.mex*   $(DESTDIR)$(datadir)/biosig/mex
	@echo "  mexBiosig for Matlab is installed in $(datadir)/biosig/mex";
	@echo "  Usage: Start Matlab and addpath ";
	@echo "\taddpath(\"$(datadir)/biosig/mex)";
	@echo "\tmexSLOAD";

uninstall_mex:
	${RM} $(DESTDIR)$(datadir)/biosig/mex/*
	cd $(DESTDIR)$(datadir) && rmdir -p biosig/mex

### Install Biosig for Mathematica
install_mma : mma/sload.exe
	mkdir -p $(DESTDIR)$(datadir)/biosig/mathematica
	install mma/sload.exe	$(DESTDIR)$(datadir)/biosig/mathematica/
	@echo "  Biosig for Mathematica is installed in $(datadir)/biosig/mathematica/sload.exe";
	@echo "  Usage: Start Mathematica and run";
	@echo "\tlink=Install[\"$(datadir)/biosig/mathematica/sload.exe\"];";
	@echo "\t?sload";

uninstall_mma:
	${RM} $(DESTDIR)$(datadir)/biosig/mathematica/sload.exe
	cd $(DESTDIR)$(datadir) && rmdir -p biosig/mathematica

uninstall remove: uninstall_mexbiosig uninstall_tools
	-$(DELETE) $(DESTDIR)$(bindir)/ttl2trig
	#-$(DELETE) $(DESTDIR)$(bindir)/rec2bin
	#-$(DELETE) $(DESTDIR)$(bindir)/bin2rec
	-$(DELETE) $(DESTDIR)$(bindir)/sigviewer
	-$(DELETE) $(DESTDIR)$(bindir)/eventcodes.txt
	-$(DELETE) $(DESTDIR)$(includedir)/biosig.h
	-$(DELETE) $(DESTDIR)$(includedir)/biosig-dev.h
	-$(DELETE) $(DESTDIR)$(includedir)/biosig2.h
	-$(DELETE) $(DESTDIR)$(includedir)/gdftime.h
	-$(DELETE) $(DESTDIR)$(includedir)/physicalunits.h
	-$(DELETE) $(DESTDIR)$(libdir)/libbiosig.*
	-$(DELETE) $(DESTDIR)$(libdir)/libbiosig2.*
	-$(DELETE) $(DESTDIR)$(libdir)/libphysicalunits.*
	-$(DELETE) $(DESTDIR)$(libdir)/pkgconfig/libbiosig.pc


#############################################################
#	Testing
#############################################################
testtms: $(DATA_DIR)t1.scp
#	./save2gdf -V8 ~/data/test/tms32/small_test.float32.log
	./save2gdf -f=TMSi $(DATA_DIR)t1.scp $(TEMP_DIR)t2.log
	cat $(TEMP_DIR)t2.log

fetchdata: $(TEMP_DIR)scp/redred/PFE103.scp  $(TEMP_DIR)Osas2002plusQRS.edf $(DATA_DIR)Newtest17-2048.bdf $(DATA_DIR)2003-12\ Schema/example/Example\ aECG.xml

$(TEMP_DIR)scp/redred/PFE103.scp:
	# scp example data sets
	#wget  -q -P$(DATA_DIR) http://www.openecg.net/ECGsamples.zip
	#wget  -q -P$(DATA_DIR) http://www.openecg.net/ECGsamplesc.zip
	unzip -u $(DATA_DIR)ECGsamples.zip "scp*.zip" -d $(TEMP_DIR)
	unzip -u $(DATA_DIR)ECGsamplesc.zip "scp*.zip" -d $(TEMP_DIR)
	mkdir -p $(TEMP_DIR)scp/high
	mkdir -p $(TEMP_DIR)scp/highc
	mkdir -p $(TEMP_DIR)scp/redred
	mkdir -p $(TEMP_DIR)scp/redredc
	unzip -u $(TEMP_DIR)scp_high.zip -d $(TEMP_DIR)scp/high
	unzip -u $(TEMP_DIR)scp_highc.zip -d $(TEMP_DIR)scp/highc
	unzip -u $(TEMP_DIR)scp_redred.zip -d $(TEMP_DIR)scp/redred
	unzip -u $(TEMP_DIR)scp_redredc.zip -d $(TEMP_DIR)scp/redredc
	rm -rf $(TEMP_DIR)ECGsamples*.zip
	rm -rf $(TEMP_DIR)scp*.zip

$(DATA_DIR)t1.scp: $(TEMP_DIR)scp/redred/PFE103.scp
	$(COPY) "$<" "$@"
	touch "$@"

#"$(DATA_DIR)aECG Release 1 Schema and Example.zip":
$(DATA_DIR)2003-12\ Schema/example/Example\ aECG.xml:
	# HL7aECG example data set
	wget -q -P$(DATA_DIR) https://www.hl7.org/documentcenter/public/wg/rcrim/annecg/aECG%20Release%201%20Schema%20and%20Example.zip
	unzip -u $(DATA_DIR)"aECG Release 1 Schema and Example.zip" -d $(DATA_DIR)

$(TEMP_DIR)t1.hl7: $(DATA_DIR)2003-12\ Schema/example/Example\ aECG.xml
	$(COPY) "$<" "$@"
	#rm -rf "$(TEMP_DIR)aECG Release 1 Schema and Example.zip"
	#rm -rf "$(TEMP_DIR)2003-12 Schema"

$(TEMP_DIR)Osas2002plusQRS.edf:
	# EDF+ example data set
	wget -q -P$(TEMP_DIR) http://www.edfplus.info/downloads/files/osas.zip
	unzip -u "$(TEMP_DIR)osas.zip"  -d $(TEMP_DIR)

$(DATA_DIR)t1.edf: $(TEMP_DIR)Osas2002plusQRS.edf
	$(COPY) "$<" "$@"
	touch "$@"

asc: save2gdf
	./save2gdf -f=ASCII t0.xxx $(TEMP_DIR)t1.asc

bin: save2gdf
	./save2gdf -f=BIN t0.xxx $(TEMP_DIR)t1.bin

testjson: save2gdf  $(DATA_DIR)t1.edf
	./save2gdf -JSON t0.xxx |tee $(TEMP_DIR)t1.xxx.json
	./save2gdf -JSON  $(TEMP_DIR)t1.edf |tee $(TEMP_DIR)t1.edf.json

testbin: save2gdf $(DATA_DIR)t1.edf
	./save2gdf -f=BIN $(DATA_DIR)t1.edf $(TEMP_DIR)t1.hdr
	./save2gdf -f=BIN $(TEMP_DIR)t1.hdr $(TEMP_DIR)t2.hdr
	./save2gdf -f=GDF $(TEMP_DIR)t2.hdr $(TEMP_DIR)t2.gdf

testedf: save2gdf $(TEMP_DIR)Osas2002plusQRS.edf
	./save2gdf -f=GDF $(TEMP_DIR)Osas2002plusQRS.edf $(TEMP_DIR)Osas2002plusQRS.gdf

testscp: $(DATA_DIR)t1.scp save2gdf
	# test converting SCP data
	./save2gdf -f=HL7 "$<" $(TEMP_DIR)t1.scp.hl7
	./save2gdf -f=GDF $(TEMP_DIR)t1.scp.hl7 $(TEMP_DIR)t1.scp.hl7.gdf
	./save2gdf -f=SCP $(TEMP_DIR)t1.scp.hl7.gdf $(TEMP_DIR)t1.scp.hl7.gdf.scp
	./save2gdf -f=GDF $(TEMP_DIR)t1.scp.hl7.gdf.scp $(TEMP_DIR)t1.scp.hl7.gdf.scp.gdf
	./save2gdf -f=HL7 $(TEMP_DIR)t1.scp.hl7.gdf.scp.gdf $(TEMP_DIR)t1.scp.hl7.gdf.scp.gdf.hl7
	./save2gdf -f=SCP $(TEMP_DIR)t1.scp.hl7.gdf.scp.gdf.hl7 $(TEMP_DIR)t1.scp.hl7.gdf.scp.gdf.hl7.scp
	./save2gdf -f=GDF "$<" $(TEMP_DIR)t1.scp.gdf
	./save2gdf -f=HL7 $(TEMP_DIR)t1.scp.gdf $(TEMP_DIR)t1.scp.gdf.hl7
	./save2gdf -f=SCP $(TEMP_DIR)t1.scp.gdf.hl7 $(TEMP_DIR)t1.scp.gdf.hl7.scp
	./save2gdf -f=HL7 $(TEMP_DIR)t1.scp.gdf.hl7.scp $(TEMP_DIR)t1.scp.gdf.hl7.scp.hl7
	./save2gdf -f=GDF $(TEMP_DIR)t1.scp.gdf.hl7.scp.hl7 $(TEMP_DIR)t1.scp.gdf.hl7.scp.hl7.gdf
	./save2gdf -f=SCP $(TEMP_DIR)t1.scp.gdf.hl7.scp.hl7.gdf $(TEMP_DIR)t1.scp.gdf.hl7.scp.hl7.gdf.scp

testscp2: $(DATA_DIR)t1.scp
	find test/scp -iname '*.scp' -exec ./save2gdf -V0 {} /dev/null \;

testscp3: $(DATA_DIR)t1.scp
	find test/scp -iname '*.scp' -exec ./save2gdf -f=SCP3 -V0 {} {}.scp3  \;

testhl7: $(TEMP_DIR)t1.hl7 save2gdf
	# test converting HL7aECG data
	./save2gdf -f=GDF "$<" $(TEMP_DIR)t1.hl7.gdf
	./save2gdf -f=SCP $(TEMP_DIR)t1.hl7.gdf $(TEMP_DIR)t1.hl7.gdf.scp
	./save2gdf -f=HL7 $(TEMP_DIR)t1.hl7.gdf.scp $(TEMP_DIR)t1.hl7.gdf.scp.hl7
	./save2gdf -f=SCP $(TEMP_DIR)t1.hl7.gdf.scp.hl7 $(TEMP_DIR)t1.hl7.gdf.scp.hl7.scp
	./save2gdf -f=GDF $(TEMP_DIR)t1.hl7.gdf.scp.hl7.scp $(TEMP_DIR)t1.hl7.gdf.scp.hl7.scp.gdf
	./save2gdf -f=HL7 $(TEMP_DIR)t1.hl7.gdf.scp.hl7.scp.gdf $(TEMP_DIR)t1.hl7.gdf.scp.hl7.scp.gdf.hl7
	./save2gdf -f=SCP "$<" $(TEMP_DIR)t1.hl7.scp
	./save2gdf -f=GDF $(TEMP_DIR)t1.hl7.scp $(TEMP_DIR)t1.hl7.scp.gdf
	./save2gdf -f=HL7 $(TEMP_DIR)t1.hl7.scp.gdf $(TEMP_DIR)t1.hl7.scp.gdf.hl7
	./save2gdf -f=GDF $(TEMP_DIR)t1.hl7.scp.gdf.hl7 $(TEMP_DIR)t1.hl7.scp.gdf.hl7.gdf
	./save2gdf -f=SCP $(TEMP_DIR)t1.hl7.scp.gdf.hl7.gdf $(TEMP_DIR)t1.hl7.scp.gdf.hl7.gdf.scp
	./save2gdf -f=HL7 $(TEMP_DIR)t1.hl7.scp.gdf.hl7.gdf.scp $(TEMP_DIR)t1.hl7.scp.gdf.hl7.gdf.scp.hl7

test: $(DATA_DIR)t1.scp save2scp save2aecg save2gdf
	# biosig4python
	# includes test for on-the-fly compression and decompression
	./save2gdf -z  $(DATA_DIR)t1.scp        $(TEMP_DIR)t1.scp.gdf
	./save2gdf -f=SCP -z 	$(DATA_DIR)t1.scp        $(TEMP_DIR)t1.scp.scp
	./save2gdf -f=HL7 -z	$(DATA_DIR)t1.scp        $(TEMP_DIR)t1.scp.hl7
	./save2gdf 	$(TEMP_DIR)t1.scp.gdf.gz $(TEMP_DIR)t1.scp.gdf.gdf
	./save2gdf 	$(TEMP_DIR)t1.scp.scp.gz $(TEMP_DIR)t1.scp.scp.gdf
	./save2gdf 	$(TEMP_DIR)t1.scp.hl7.gz $(TEMP_DIR)t1.scp.hl7.gdf
	./save2gdf -f=SCP 	$(TEMP_DIR)t1.scp.gdf.gz $(TEMP_DIR)t1.scp.gdf.scp
	./save2gdf -f=SCP 	$(TEMP_DIR)t1.scp.scp.gz $(TEMP_DIR)t1.scp.scp.scp
	./save2gdf -f=SCP 	$(TEMP_DIR)t1.scp.hl7.gz $(TEMP_DIR)t1.scp.hl7.scp
	./save2gdf -f=HL7	$(TEMP_DIR)t1.scp.gdf.gz $(TEMP_DIR)t1.scp.gdf.hl7
	./save2gdf -f=HL7	$(TEMP_DIR)t1.scp.scp.gz $(TEMP_DIR)t1.scp.scp.hl7
	./save2gdf -f=HL7	$(TEMP_DIR)t1.scp.hl7.gz $(TEMP_DIR)t1.scp.hl7.hl7
	# python test0/test.py

zip: $(DATA_DIR)t1.scp save2gdf
	# test for on-the-fly compression and decompression
	# on-the-fly compression of output file
	./save2gdf -z -f=GDF $(DATA_DIR)t1.scp $(TEMP_DIR)t1.gdf
	./save2gdf -z -f=GDF1 $(DATA_DIR)t1.scp $(TEMP_DIR)t1.gd1
	./save2gdf -z -f=EDF $(DATA_DIR)t1.scp $(TEMP_DIR)t1.edf
	./save2gdf -z -f=BDF $(DATA_DIR)t1.scp $(TEMP_DIR)t1.bdf
	./save2gdf -z -f=SCP $(DATA_DIR)t1.scp $(TEMP_DIR)t1.scp
	./save2gdf -z -f=CFWB $(DATA_DIR)t1.scp $(TEMP_DIR)t1.cfw
	./save2gdf -z -f=MFER $(DATA_DIR)t1.scp $(TEMP_DIR)t1.mwf
	./save2gdf -z -f=HL7 $(DATA_DIR)t1.scp $(TEMP_DIR)t1.hl7

	gzip -c $(DATA_DIR)t1.scp >$(TEMP_DIR)t1.scp.gz
	# on-the-fly decompression of input file
	./save2gdf -f=GDF $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t1.gdf
	./save2gdf -f=EDF $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t1.edf
	./save2gdf -f=BDF $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t1.bdf
	./save2gdf -f=SCP $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t1.scp
	./save2gdf -f=HL7 $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t1.hl7
	./save2gdf -f=MFER $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t1.mwf
	./save2gdf -f=CFWB $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t1.cfw

test6: $(DATA_DIR)t1.scp save2gdf
	$(COPY) $(DATA_DIR)t1.scp $(TEMP_DIR)t0.xxx
	#test7: $(DATA_DIR)t1.edf save2gdf
	#$(COPY) $(DATA_DIR)t1.edf $(TEMP_DIR)t0.xxx
	./save2gdf -z -f=GDF1 $(TEMP_DIR)t0.xxx $(TEMP_DIR)t1.gd1
	./save2gdf -z -f=GDF $(TEMP_DIR)t0.xxx $(TEMP_DIR)t1.gdf
	./save2gdf -z -f=EDF $(TEMP_DIR)t0.xxx $(TEMP_DIR)t1.edf
	./save2gdf -z -f=BDF $(TEMP_DIR)t0.xxx $(TEMP_DIR)t1.bdf
	./save2gdf -z -f=SCP $(TEMP_DIR)t0.xxx $(TEMP_DIR)t1.scp
	./save2gdf    -f=HL7 $(TEMP_DIR)t0.xxx $(TEMP_DIR)t1.hl7   # -z not supported for HL7
	gzip -f $(TEMP_DIR)t1.hl7
	./save2gdf -z -f=CFWB $(TEMP_DIR)t0.xxx $(TEMP_DIR)t1.cfw
	./save2gdf -z -f=MFER $(TEMP_DIR)t0.xxx $(TEMP_DIR)t1.mwf
	./save2gdf -f=BVA $(TEMP_DIR)t0.xxx $(TEMP_DIR)t1.bva
	./save2gdf -f=GDF1 $(TEMP_DIR)t1.gd1.gz $(TEMP_DIR)t2.gd1.gd1
	./save2gdf -f=GDF $(TEMP_DIR)t1.gd1.gz $(TEMP_DIR)t2.gd1.gdf
	./save2gdf -f=EDF $(TEMP_DIR)t1.gd1.gz $(TEMP_DIR)t2.gd1.edf
	./save2gdf -f=BDF $(TEMP_DIR)t1.gd1.gz $(TEMP_DIR)t2.gd1.bdf
	./save2gdf -f=SCP $(TEMP_DIR)t1.gd1.gz $(TEMP_DIR)t2.gd1.scp
	./save2gdf -f=HL7 $(TEMP_DIR)t1.gd1.gz $(TEMP_DIR)t2.gd1.hl7
	./save2gdf -f=CFWB $(TEMP_DIR)t1.gd1.gz $(TEMP_DIR)t2.gd1.cfw
	./save2gdf -f=GDF1 $(TEMP_DIR)t1.gdf.gz $(TEMP_DIR)t2.gdf.gd1
	./save2gdf -f=GDF $(TEMP_DIR)t1.gdf.gz $(TEMP_DIR)t2.gdf.gdf
	./save2gdf -f=EDF $(TEMP_DIR)t1.gdf.gz $(TEMP_DIR)t2.gdf.edf
	./save2gdf -f=BDF $(TEMP_DIR)t1.gdf.gz $(TEMP_DIR)t2.gdf.bdf
	./save2gdf -f=SCP $(TEMP_DIR)t1.gdf.gz $(TEMP_DIR)t2.gdf.scp
	./save2gdf -f=HL7 $(TEMP_DIR)t1.gdf.gz $(TEMP_DIR)t2.gdf.hl7
	./save2gdf -f=CFWB $(TEMP_DIR)t1.gdf.gz $(TEMP_DIR)t2.gdf.cfw
	./save2gdf -f=GDF1 $(TEMP_DIR)t1.edf.gz $(TEMP_DIR)t2.edf.gd1
	./save2gdf -f=GDF $(TEMP_DIR)t1.edf.gz $(TEMP_DIR)t2.edf.gdf
	./save2gdf -f=EDF $(TEMP_DIR)t1.edf.gz $(TEMP_DIR)t2.edf.edf
	./save2gdf -f=BDF $(TEMP_DIR)t1.edf.gz $(TEMP_DIR)t2.edf.bdf
	./save2gdf -f=SCP $(TEMP_DIR)t1.edf.gz $(TEMP_DIR)t2.edf.scp
	./save2gdf -f=HL7 $(TEMP_DIR)t1.edf.gz $(TEMP_DIR)t2.edf.hl7
	./save2gdf -f=CFWB $(TEMP_DIR)t1.edf.gz $(TEMP_DIR)t2.edf.cfw
	./save2gdf -f=GDF1 $(TEMP_DIR)t1.bdf.gz $(TEMP_DIR)t2.bdf.gd1
	./save2gdf -f=GDF $(TEMP_DIR)t1.bdf.gz $(TEMP_DIR)t2.bdf.gdf
	./save2gdf -f=EDF $(TEMP_DIR)t1.bdf.gz $(TEMP_DIR)t2.bdf.edf
	./save2gdf -f=BDF $(TEMP_DIR)t1.bdf.gz $(TEMP_DIR)t2.bdf.bdf
	./save2gdf -f=SCP $(TEMP_DIR)t1.bdf.gz $(TEMP_DIR)t2.bdf.scp
	./save2gdf -f=HL7 $(TEMP_DIR)t1.bdf.gz $(TEMP_DIR)t2.bdf.hl7
	./save2gdf -f=CFWB $(TEMP_DIR)t1.bdf.gz $(TEMP_DIR)t2.bdf.cfw
	./save2gdf -f=GDF1 $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t2.scp.gd1
	./save2gdf -f=GDF $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t2.scp.gdf
	./save2gdf -f=EDF $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t2.scp.edf
	./save2gdf -f=BDF $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t2.scp.bdf
	./save2gdf -f=SCP $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t2.scp.scp
	./save2gdf -f=HL7 $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t2.scp.hl7
	./save2gdf -f=CFWB $(TEMP_DIR)t1.scp.gz $(TEMP_DIR)t2.scp.cfw
	./save2gdf -f=GDF1 $(TEMP_DIR)t1.hl7.gz $(TEMP_DIR)t2.hl7.gd1
	./save2gdf -f=GDF $(TEMP_DIR)t1.hl7.gz $(TEMP_DIR)t2.hl7.gdf
	./save2gdf -f=EDF $(TEMP_DIR)t1.hl7.gz $(TEMP_DIR)t2.hl7.edf
	./save2gdf -f=BDF $(TEMP_DIR)t1.hl7.gz $(TEMP_DIR)t2.hl7.bdf
	./save2gdf -f=SCP $(TEMP_DIR)t1.hl7.gz $(TEMP_DIR)t2.hl7.scp
	./save2gdf -f=HL7 $(TEMP_DIR)t1.hl7.gz $(TEMP_DIR)t2.hl7.hl7
	./save2gdf -f=CFWB $(TEMP_DIR)t1.hl7.gz $(TEMP_DIR)t2.hl7.cfw
	./save2gdf -f=GDF1 $(TEMP_DIR)t1.cfw.gz $(TEMP_DIR)t2.cfw.gd1
	./save2gdf -f=GDF $(TEMP_DIR)t1.cfw.gz $(TEMP_DIR)t2.cfw.gdf
	./save2gdf -f=EDF $(TEMP_DIR)t1.cfw.gz $(TEMP_DIR)t2.cfw.edf
	./save2gdf -f=BDF $(TEMP_DIR)t1.cfw.gz $(TEMP_DIR)t2.cfw.bdf
	./save2gdf -f=SCP $(TEMP_DIR)t1.cfw.gz $(TEMP_DIR)t2.cfw.scp
	./save2gdf -f=HL7 $(TEMP_DIR)t1.cfw.gz $(TEMP_DIR)t2.cfw.hl7
	./save2gdf -f=GDF1 $(TEMP_DIR)t1.mwf.gz $(TEMP_DIR)t2.mwf.gd1
	./save2gdf -f=GDF $(TEMP_DIR)t1.mwf.gz $(TEMP_DIR)t2.mwf.gdf
	./save2gdf -f=EDF $(TEMP_DIR)t1.mwf.gz $(TEMP_DIR)t2.mwf.edf
	./save2gdf -f=BDF $(TEMP_DIR)t1.mwf.gz $(TEMP_DIR)t2.mwf.bdf
	./save2gdf -f=SCP $(TEMP_DIR)t1.mwf.gz $(TEMP_DIR)t2.mwf.scp
	./save2gdf -f=HL7 $(TEMP_DIR)t1.mwf.gz $(TEMP_DIR)t2.mwf.hl7

$(DATA_DIR)Newtest17-2048.bdf : $(DATA_DIR)BDFtestfiles.zip
	unzip -u "$<" -d $(DATA_DIR)

testbdf : $(DATA_DIR)Newtest17-2048.bdf
	./save2gdf -V4 "$<" | awk '/(NoChannels|NRec|Fs|Events\/Annotations):/ { print $$2 } '

testpybdf : $(DATA_DIR)Newtest17-2048.bdf
	$(PYTHON) < python/demo2.py
ifdef SWIG_BIOSIG4PYTHON
	$(PYTHON) python/demo.py $(TEMP_DIR)*-256.bdf
	$(PYTHON) python/example.py $(TEMP_DIR)*-256.bdf 256 && \
	$(PYTHON) python/example.py $(TEMP_DIR)*-2048.bdf 2048
endif

$(DATA_DIR)BDFtestfiles.zip :
	wget -P$(DATA_DIR) http://www.biosemi.com/download/BDFtestfiles.zip

.PHONY: stat 
stat: 
	@echo 'TODO/FIXME:  ';
	@grep -ri '\bTODO\b'  *.c *.h *.i t210/*.c t210/*.h |grep -v '\.svn' |wc -l;
	@grep -ri '\bFIXME\b' *.c *.h *.i t210/*.c t210/*.h |grep -v '\.svn' |wc -l;

test_physicalunits : units.i physicalunits.c physicalunits.h
	gcc -D=TEST_PHYSDIMTABLE_PERFORMANCE -D=WITH_PTHREAD physicalunits.c -o test_physicalunits
	./test_physicalunits
	@echo '--- end of test_physicalunits ---'


testcfs : $(DATA_DIR_CFS) save2gdf 
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)BaseDemo/Actions.CFS
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)BaseDemo/example.cfs
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)BaseDemo/Leak.cfs
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)BaseDemo/NAME_0AA.cfs
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)BaseDemo/SCANexam.CFS
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)BaseDemo/TEST.cfs
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)BaseDemo/TRIAL0AA.cfs
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)100118s1AB.dat
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)100121s1AF.dat
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)Signal/1_23456.000.cfs
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)Signal/1_23456.001.cfs
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)Signal/1_23456.002.cfs
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)Signal/1_23456.LAST.cfs
	-./save2gdf $(VERBOSE) $(DATA_DIR_CFS)/1617K2AA.DAT