SHELL = /bin/sh


subdirs = @subdirs@
top_srcdir = .
srcdir = .
prefix = /usr/local
exec_prefix = ${prefix}
bindir = $(exec_prefix)/bin
infodir = $(prefix)/info
libdir = $(prefix)/lib/gnudl
mandir = $(prefix)/man/man1

CC = gcc
CPPFLAGS = 
CFLAGS = -Wall -pedantic -Wextra -O2 -fvisibility=hidden -DIGUANAIR_EXPORTS
LDFLAGS = 
LIBS = 
INSTALL = /usr/bin/install -c

SHAREEXT = so
LIBNAME=libiguanaIR.$(SHAREEXT).0
SHAREARG = -shared
LINKARGS = -Wl,-soname=$(LIBNAME),--version-script=ig.ver
ifeq ($(shell uname -m),x86_64)
  LIBDIR  = lib64
  CFLAGS += -fpic
else
  LIBDIR  = lib

  ifeq ($(shell uname -m),ppc)
    CFLAGS += -fpic
  else
    ifeq ($(shell uname -m),Power Macintosh)
      LIBDIR  = lib
      CFLAGS += -fPIC
      CPPFLAGS += -I/sw/include
      LDFLAGS += -L/sw/lib
      SHAREEXT = dylib
      LIBNAME=libiguanaIR.$(SHAREEXT)
      SHAREARG = -dynamiclib
      LINKARGS = 
    endif
  endif
endif


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

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

igdaemon: daemon.o client-interface.o device-interface.o usbclient.o \
          list.o protocol-versions.o support.o pipes.o dataPackets.o compat.o
	$(CC) $(LDFLAGS) $^ -lpopt -lusb -lpthread $(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 $@

# 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.5,)
all: _iguanaIR.$(SHAREEXT)
install: install-python install-reflasher
.PHONY: install-python
install-python:
	install -D _iguanaIR.$(SHAREEXT) $(DESTDIR)/usr/lib/python2.5/site-packages/_iguanaIR.$(SHAREEXT)
	install -D iguanaIR.py $(DESTDIR)/usr/lib/python2.5/site-packages/iguanaIR.py

.PHONY: install-reflasher
install-reflasher:
	make -C reflasher install

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

.PHONY: uninstall-reflasher
uninstall-reflasher:
	make -C reflasher uninstall

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

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

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

# change ownership unless we're building an rpm
ifeq ($(RPM_OS),)
  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)/usr/$(LIBDIR)
	ln -s $(LIBNAME) $(DESTDIR)/usr/$(LIBDIR)/libiguanaIR.$(SHAREEXT)
endif

.PHONY: install
install: all
	install -D igclient $(DESTDIR)/usr/bin/igclient
	install -D igdaemon $(DESTDIR)/usr/bin/igdaemon
	install -D $(LIBNAME) $(DESTDIR)/usr/$(LIBDIR)/$(LIBNAME)
	install -D iguanaIR.init $(DESTDIR)/etc/init.d/iguanaIR
	install -D --mode 644 iguanaIR.options $(DESTDIR)/etc/default/iguanaIR
	install -D iguanaIR.h $(DESTDIR)/usr/include/iguanaIR.h
	install -D --mode 644 plug-trigger/udev/iguanaIR.rules \
	                      $(DESTDIR)/etc/udev/rules.d/iguanaIR.rules
	install --mode 755 $(SETOWNER) -d $(DESTDIR)/lib/udev/devices/iguanaIR
	install --mode 755 $(SETOWNER) -d $(DESTDIR)/dev/iguanaIR
#########################################################################
# This is for fairly old versions of Fedora....
#	install -D --mode 644 plug-trigger/hotplug/iguanaIR.usermap \
#	                      $(DESTDIR)/etc/hotplug/usb/iguanaIR.usermap
#	install -D --mode 755 plug-trigger/hotplug/iguanaIR \
#	                      $(DESTDIR)/etc/hotplug/usb/iguanaIR
#########################################################################

.PHONY: uninstall
uninstall:
	/etc/init.d/iguanaIR stop || true
	rm -f $(DESTDIR)/usr/bin/igclient
	rm -f $(DESTDIR)/usr/bin/igdaemon
	rm -f $(DESTDIR)/usr/$(LIBDIR)/$(LIBNAME)
	rm -f $(DESTDIR)/etc/init.d/iguanaIR
	rm -f $(DESTDIR)/etc/default/iguanaIR
	rm -f $(DESTDIR)/usr/include/iguanaIR.h
	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

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

.PHONY: distclean
distclean: clean
	rm -f Makefile config.h
	make -C packaging clean

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

.PHONY: package
package: distclean
	make -C packaging





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 autoconf/aclocal.m4, $(wildcard autoconf/*.m4))
autoconf/aclocal.m4: $(M4S)
	cd autoconf; $(ACLOCAL)

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

config.h.in: autoconf/configure.ac
	cd autoconf; autoheader configure.ac; mv config.h.in ..

config.status: configure
	./config.status --recheck

Makefile: Makefile.in config.status
	./config.status

config.h: autoconf/stamp-h
autoconf/stamp-h: config.h.in config.status
	./config.status
