.SILENT: help

#
# all configuration is set in the Makefile.config file.
#

VERSION = 0.0.1
APP = simpleskycam

-include Makefile.config
-include Makefile.rules

OBJECTS := $(OBJECTS) gui.oo main.oo error.oo debug.oo \
		   video.oo videoframe.oo \
		   videodev.oo videodev-dumpfile.oo videodev-simulation.oo \
		   convert.oo output.oo detect.oo histogram.oo pid.oo \
		   posctl.oo json.oo configuration.oo debayer.oo
DISTNAME=simpleskycam-$(VERSION)

#
# add configuration for debug_angles
#
ifeq ($(DEBUG_ANGLES),1)
CPPFLAGS := $(CPPFLAGS)
OBJECTS := $(OBJECTS) debug-angles.oo
endif
#

#
# compile and use DNG files 
#
ifeq ($(USE_DNG),1)
LDFLAGS := $(LDFLAGS) -ltiff
OBJECTS := $(OBJECTS) dng.oo
endif
#

#
# compile and use DNG files 
#
ifeq ($(USE_SER),1)
OBJECTS := $(OBJECTS) ser.oo
endif
#





ifeq ($(TARGET),)
noconfig: defaultconfig help
endif

all: Makefile.rules $(TARGET)

defaultconfig:
ifeq ($(MAKEFILE_CONFIG),)
	echo "MAKEFILE_CONFIG = 1" > Makefile.config
	echo "" >> Makefile.config
	echo "USE_SVBONY = 1" >> Makefile.config
	echo "USE_DNG = 1" >> Makefile.config
	echo "USE_SER = 1" >> Makefile.config
	echo "" >> Makefile.config
	echo "DEBUG_ANGLES = 0" >> Makefile.config
	echo "DEBUG_POSCTL = 0" >> Makefile.config
endif

help:
	echo "set up configuration"
	echo "   make configlinux      to generate the linix build"
	echo "   make configcross      to generate the windows build using cross tools."
	echo "   make configwindows    to generate the windows build using msys2."
	echo " "
	echo "   make checkdumpfile    to create a test tool for videodumps."

checkdumpfile: checkdumpfile.cc
	c++ -o checkdumpfile checkdumpfile.cc

configlinux: clean
	cp -f Makefile.rules.linux Makefile.rules
	make config

configwindows: clean
	cp -f Makefile.rules.windows Makefile.rules
	make config

configcross: clean
	cp -f Makefile.rules.crosswindows Makefile.rules
	make config

config: Makefile.rules
	echo "#ifndef _CONFIG_H_" > config.h
	echo "#define _CONFIG_H_" >> config.h
	echo "" >> config.h
	echo "#define VERSION \"$(VERSION)\"" >> config.h
ifeq ($(USE_SVBONY),1)
	echo "#define USE_SVBONY" >> config.h 
endif
ifeq ($(USE_SER),1)
	echo "#define USE_SER" >> config.h 
endif
ifeq ($(USE_DNG),1)
	echo "#define USE_DNG" >> config.h 
endif
ifeq ($(USE_V4L2),1)
	echo "#define USE_V4L2" >> config.h 
endif
ifeq ($(USE_VFW),1)
	echo "#define USE_VFW" >> config.h 
endif
	echo "" >> config.h
ifeq ($(DEBUG_ANGLES),1)
	echo "#define DEBUG_ANGLES" >> config.h 
endif
ifeq ($(DEBUG_POSCTL),1)
	echo "#define DEBUG_POSCTL" >> config.h 
endif
	echo "" >> config.h
	echo "#endif" >> config.h


buildwindows: clean
	make configcross
	make $(TARGET) -j 9
	mkdir SimpleSkyCam-$(VERSION)
	cp *.exe SimpleSkyCam-$(VERSION)/
	cp simpleskycam.ui SimpleSkyCam-$(VERSION)/
	cp README.md SimpleSkyCam-$(VERSION)/
	cp ChangeLog SimpleSkyCam-$(VERSION)/
	cp LICENSE SimpleSkyCam-$(VERSION)/
	./copydlls.sh
	./copydlls.sh
	./copyshare.sh
	mv *.dll SimpleSkyCam-$(VERSION)/
	mv share SimpleSkyCam-$(VERSION)/
	cp /usr/lib/gcc/x86_64-w64-mingw32/12-win32/*.dll SimpleSkyCam-$(VERSION)/
	cp /opt/W64-cross-compile/lib/libtiff4.dll SimpleSkyCam-$(VERSION)/libtiff-6.dll
	cp /opt/W64-cross-compile/lib/SVBCamera* SimpleSkyCam-$(VERSION)/


packwindows:
	rm -rf SimpleSkyCam-$(VERSION) *.bz2
	mkdir SimpleSkyCam-$(VERSION)
	cp *.exe SimpleSkyCam-$(VERSION)/
	cp simpleskycam.ui SimpleSkyCam-$(VERSION)/
	cp README.md SimpleSkyCam-$(VERSION)/
	cp ChangeLog SimpleSkyCam-$(VERSION)/
	cp LICENSE SimpleSkyCam-$(VERSION)/
	for i in `ldd simpleskycam.exe |grep -vi "/c/windows" | cut -d" " -f3`; do cp $$i SimpleSkyCam-$(VERSION)/; done
	tar cvohif SimpleSkyCam-0.0.1-win64.tar.bz2 SimpleSkyCam-0.0.1/

$(TARGET): $(OBJECTS)
	$(CPP) -o $(TARGET) $(OBJECTS) $(LDFLAGS) $(LIBS)

.SUFFIXES:
.SUFFIXES:	.c .cc .C .cpp .oo

-include $(DEPENDFILE)

.cc.oo : $(INCLUDES)
	$(CPP) -o $@ -c $(CPPFLAGS) $<

cleanall: clean
	rm -rf Makefile.config

clean:
	rm -f *.o *.oo *.c~ *.h~ *.cc~ *.ui~ $(APP) Makefile~
	rm -rf config.h
	rm -rf *.dll
	rm -rf *.exe
	rm -rf Makefile.rules
	rm -rf checkdumpfile
	rm -rf U2SM200C-AST_Cfg_A.bin
	rm -rf U2SM200C-AST_Cfg_SAVE.bin
	rm -rf SimpleSkyCam-$(VERSION)

dist: cleanall
	rm -rf $(DISTNAME)
	mkdir $(DISTNAME)
	cp Makefile* $(DISTNAME)
	cp Readme $(DISTNAME)
	cp COPYING $(DISTNAME)
	cp Changelog $(DISTNAME)
	cp *.ui $(DISTNAME)
	cp -rf *.h $(DISTNAME)
	cp -rf *.cc $(DISTNAME) 
	tar cvzf $(DISTNAME).tgz $(DISTNAME)
	rm -rf $(DISTNAME)

.PHONY: all
.PHONY: count
.PHONY: clean
