include Make.settings

CFLAGS += -I. -fvisibility=hidden -DIGUANAIR_EXPORTS
LINKARGS = -Wl,-soname=$(LIBNAME),--version-script=$(srcdir)/ig.ver
ifeq ($(shell uname -s),Darwin)
  LINKARGS = -Wl,-dylib_current_version,1.0.0,-dylib_compatibility_version,1.0.0
endif

.PHONY: all drivers
all: igclient igdaemon drivers
igclient igdaemon: $(LIBNAME)

$(LIBNAME): iguanaIR.o support.o dataPackets.o compat.o pipes.o
	$(CC) $(SHAREARG) $(LINKARGS) $(LDFLAGS) $^ -o $@

drivers:
	$(MAKE) -C $(srcdir)/drivers prefix=$(prefix)

igdaemon: daemon.o client-interface.o device-interface.o driver.o \
          list.o protocol-versions.o pipes.o dataPackets.o compat.o \
          support.c $(OSSUPPORT)
	$(CC) -Wl,-export-dynamic $(CPPFLAGS) $(CFLAGS) -DSUPPORT_EXPORTS \
              $(LDFLAGS) $^ -lpopt -lusb -lpthread -ldl $(LIBNAME) -o $@

igclient: client.o list.o support.o compat.o
	$(CC) $(LDFLAGS) $^ -lpopt $(LIBNAME) -o $@

%.o: %.c Makefile iguanaIR.h
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@

driver.o: driver.c Makefile iguanaIR.h
	$(CC) -c $(CPPFLAGS) $(CFLAGS) -Wno-strict-aliasing $< -o $@

# need a special dependency for config.h
compat.o: compat.c config.h

# NOTE: I would rather use PYTHON_VERSION, but it doesn't work
ifneq (-I/usr/include/python2.6,)
all: _iguanaIR.$(SHAREEXT)
install: install-python install-reflasher
.PHONY: install-python
install-python:
	install -d $(DESTDIR)/usr/lib/python2.6/site-packages
	install _iguanaIR.$(SHAREEXT) $(DESTDIR)/usr/lib/python2.6/site-packages/_iguanaIR.$(SHAREEXT)
	install iguanaIR.py $(DESTDIR)/usr/lib/python2.6/site-packages/iguanaIR.py

.PHONY: install-reflasher
install-reflasher:
	$(MAKE) -C $(srcdir)/reflasher prefix=$(prefix) install

uninstall: uninstall-python uninstall-reflasher
.PHONY: uninstall-python
uninstall-python:
	rm -f $(DESTDIR)/usr/lib/python2.6/site-packages/_iguanaIR.$(SHAREEXT)
	rm -f $(DESTDIR)/usr/lib/python2.6/site-packages/iguanaIR.py

.PHONY: uninstall-reflasher
uninstall-reflasher:
	$(MAKE) -C $(srcdir)/reflasher prefix=$(prefix) uninstall

iguanaIR_wrap.c iguanaIR.py: iguanaIR.i iguanaIR.h Makefile
	swig $(CPPFLAGS) -I/usr/include/python2.6 -python -I$(srcdir) $(<F)

iguanaIR_wrap.o: iguanaIR_wrap.c Makefile
#	$(CC) -c $(CPPFLAGS) -I/usr/include/python2.6 $(CFLAGS) -Wno-strict-aliasing -Wno-unused -Wno-implicit-function-declaration -Wno-long-long $< -o $@
	$(CC) -c $(CPPFLAGS) -I/usr/include/python2.6 $(CFLAGS) -Wno-missing-field-initializers -Wno-unused -Wno-long-long -Wno-strict-aliasing $< -o $@

_iguanaIR.$(SHAREEXT): iguanaIR_wrap.o
	$(CC) $(LDFLAGS) -L/usr/lib/python2.6 -lpython2.6 $(SHAREARG) $^ $(LIBNAME) -o $@
endif

# change ownership unless we're building an rpm
ifeq ($(RPM_OS)$(DARWIN),)
  SETOWNER=--owner iguanair --group iguanair
install: adduser
.PHONY: adduser
adduser:
	/usr/sbin/useradd -u 213 -c "Iguanaworks IR Daemon" -d / -s /sbin/nologin iguanair 2>/dev/null || true

uninstall: deluser
.PHONY: deluser
deluser:
	/usr/sbin/userdel iguanair || true
else
  SETOWNER=
endif

# install a non-versioned symlink
ifneq ($(LIBNAME),libiguanaIR.$(SHAREEXT))
install: install-symlink
.PHONY: install-symlink
install-symlink:
	install -d $(DESTDIR)$(prefix)/$(LIBDIR)
	rm -f $(DESTDIR)$(prefix)/$(LIBDIR)/libiguanaIR.$(SHAREEXT)
	ln -s $(LIBNAME) $(DESTDIR)$(prefix)/$(LIBDIR)/libiguanaIR.$(SHAREEXT)
endif

.PHONY: install
install: all
	install -d $(DESTDIR)$(prefix)/bin
	install igclient $(DESTDIR)$(prefix)/bin/igclient
	install igdaemon $(DESTDIR)$(prefix)/bin/igdaemon
	install -d $(DESTDIR)$(prefix)/$(LIBDIR)/iguanaIR
	install $(LIBNAME) $(DESTDIR)$(prefix)/$(LIBDIR)/$(LIBNAME)
	install drivers/libusbpre1.$(SHAREEXT) $(DESTDIR)$(prefix)/$(LIBDIR)/iguanaIR/libusbpre1.$(SHAREEXT)
	install drivers/libusb.$(SHAREEXT) $(DESTDIR)$(prefix)/$(LIBDIR)/iguanaIR/libusb.$(SHAREEXT)
	install -d $(DESTDIR)$(prefix)/include
	install $(srcdir)/iguanaIR.h $(DESTDIR)$(prefix)/include/iguanaIR.h
ifeq ($(DARWIN),)
	install -D $(srcdir)/iguanaIR.init $(DESTDIR)/etc/init.d/iguanaIR
	install -D --mode 644 $(srcdir)/iguanaIR.options $(DESTDIR)/etc/default/iguanaIR
	install -D --mode 644 $(srcdir)/udev/iguanaIR.rules \
	                      $(DESTDIR)/lib/udev/rules.d/80-iguanaIR.rules
	install --mode 755 $(SETOWNER) -d $(DESTDIR)/lib/udev/devices/iguanaIR
	install --mode 755 $(SETOWNER) -d $(DESTDIR)/dev/iguanaIR
endif

.PHONY: uninstall
uninstall:
ifeq ($(DARWIN),)
	/etc/init.d/iguanaIR stop || true
endif
	rm -f $(DESTDIR)$(prefix)/bin/igclient
	rm -f $(DESTDIR)$(prefix)/bin/igdaemon
	rm -f $(DESTDIR)$(prefix)/$(LIBDIR)/$(LIBNAME)
	rm -f $(DESTDIR)$(prefix)/$(LIBDIR)/libiguanaIR.$(SHAREEXT)
	rm -f $(DESTDIR)$(prefix)/include/iguanaIR.h
ifeq ($(DARWIN),)
	rm -f $(DESTDIR)/etc/init.d/iguanaIR
	rm -f $(DESTDIR)/etc/default/iguanaIR
	rm -f $(DESTDIR)/etc/udev/rules.d/iguanaIR.rules
	rmdir $(DESTDIR)/lib/udev/devices/iguanaIR 2>/dev/null || true
	rmdir $(DESTDIR)/dev/iguanaIR 2>/dev/null || true
endif

.PHONY: clean drivers-clean
clean: drivers-clean
	rm -f *~ *.o *iguanaIR.$(SHAREEXT)* *libiguanaIR.* iguanaIR.py* iguanaIR_wrap.c igdaemon igclient config.log

drivers-clean:
	$(MAKE) -C $(srcdir)/drivers prefix=$(prefix) clean

.PHONY: distclean drivers-distclean
distclean: clean drivers-distclean
	rm -f Makefile config.h config.status
	$(MAKE) -C $(srcdir)/packaging prefix=$(prefix) builddir="$(builddir)" clean

drivers-distclean:
	$(MAKE) -C $(srcdir)/drivers prefix=$(prefix) distclean

.PHONY: cleanest
cleanest: distclean
	rm -rf configure autom4te.cache

.PHONY: package
package: distclean
	$(MAKE) -C $(srcdir)/packaging prefix=$(prefix) builddir="$(builddir)"





AUTOCONF=autoconf
ACLOCAL=aclocal
ifeq ($(SYSTEM),freebsd)
    AUTOCONF=autoconf259
    ACLOCAL=aclocal19
endif

.PHONY: dist devel-cleaner
dist: configure
devel-clean: distclean
	rm -rf autoconf/autom4te.cache autoconf/aclocal.m4 

# rules to rebuild the configure script
M4S=$(filter-out $(srcdir)/autoconf/aclocal.m4, $(wildcard $(srcdir)/autoconf/*.m4))
autoconf/aclocal.m4: $(M4S)
	cd $(srcdir)/autoconf; $(ACLOCAL)

configure: autoconf/configure.ac autoconf/aclocal.m4
	cd $(srcdir)/autoconf; $(AUTOCONF) -o ../configure configure.ac

config.h.in: autoconf/configure.ac
	cd $(srcdir)/autoconf; autoheader configure.ac; mv config.h.in ..

config.status: configure
	$(SHELL) ./config.status --recheck

Makefile: ./config.status Makefile.in ./Make.settings.in
	$(SHELL) $<

config.h: autoconf/stamp-h
autoconf/stamp-h: ./config.status config.h.in
	$(SHELL) $<
