
# --------------- Makefile options, which doesn't depend on OS ----------------

# these subdirectories from /docs are copied to docs dir
# ("\" CHAR IS AFTER NAME WITHOUT SPACE)
DOCS      = default\
	    docs\
	    examples

# common files
COMMON    = $(TOPDIR)/common/gsmcomon.o \
	    $(TOPDIR)/common/gsmstate.o \
	    $(TOPDIR)/common/misc/misc.o \
	    $(TOPDIR)/common/misc/coding.o \
	    $(TOPDIR)/common/misc/cfg.o \
	    $(TOPDIR)/common/service/gsmsms.o \
	    $(TOPDIR)/common/service/gsmcal.o \
	    $(TOPDIR)/common/service/gsmwap.o \
	    $(TOPDIR)/common/service/gsmpbk.o \
	    $(TOPDIR)/common/service/gsmback.o \
	    $(TOPDIR)/common/service/gsmring.o \
	    $(TOPDIR)/common/service/gsmlogo.o \
	    $(TOPDIR)/common/service/gsmnet.o \
	    $(TOPDIR)/common/device/bluetoth/unixblue.o \
	    $(TOPDIR)/common/device/serial/unix.o \
	    $(TOPDIR)/common/device/serial/djgpp.o \
	    $(TOPDIR)/common/device/irda/irda.o \
	    $(TOPDIR)/common/protocol/at/at.o \
	    $(TOPDIR)/common/protocol/alcatel/alcabus.o \
	    $(TOPDIR)/common/protocol/nokia/mbus2.o \
	    $(TOPDIR)/common/protocol/nokia/fbus2.o \
	    $(TOPDIR)/common/protocol/nokia/fbusirda.o \
	    $(TOPDIR)/common/phone/pfunc.o \
	    $(TOPDIR)/common/phone/at/atgen.o \
	    $(TOPDIR)/common/phone/at/siemens.o \
	    $(TOPDIR)/common/phone/alcatel/alcatel.o \
	    $(TOPDIR)/common/phone/nokia/dct3/n6110.o \
	    $(TOPDIR)/common/phone/nokia/dct3/n7110.o \
	    $(TOPDIR)/common/phone/nokia/dct3/n9210.o \
	    $(TOPDIR)/common/phone/nokia/dct3/dct3func.o \
	    $(TOPDIR)/common/phone/nokia/dct4/n6510.o \
	    $(TOPDIR)/common/phone/nokia/nauto.o \
	    $(TOPDIR)/common/phone/nokia/nfunc.o

# files for command line gammu
GAMMU     = $(TOPDIR)/gammu/depend/dct3.o \
	    $(TOPDIR)/gammu/depend/dct4.o \
	    $(TOPDIR)/gammu/smsd/smsdcore.o \
	    $(TOPDIR)/gammu/smsd/s_files.o \
	    $(TOPDIR)/gammu/sniff.o \
	    $(TOPDIR)/gammu/gammu.o

LOCALE    = $(TOPDIR)/common/misc/misc.o \
	    $(TOPDIR)/common/misc/coding.o \
	    $(TOPDIR)/common/misc/cfg.o \
	    $(TOPDIR)/cfg/locale/locale.o
	    
LOCALE2   = $(TOPDIR)/cfg/locale

include $(TOPDIR)/cfg/Makefile.cfg

DESTDIR = 

# --------------------------- internal source ---------------------------------

%.o: %.c
	@( echo Making $<; \
	  cd $(@D); \
	  $(CC) -c $(CFLAGS) $(CPPFLAGS) $(<F) -o $(@F) \
	)
    
all: gammu

gammu: $(COMMON) $(GAMMU)
	@echo Linking
	@$(CC) $(COMMON) $(GAMMU) -lm -o $(TOPDIR)/gammu/gammu
	
makelib: $(COMMON) $(GAMMU)
	@echo Linking libGammu.so
	@$(CC) -shared -o $(TOPDIR)/common/libGammu.so $(COMMON)
	@echo Linking
	@$(CC) $(LDFLAGS) $(GAMMU) -lm -L$(TOPDIR)/common -lGammu -o $(TOPDIR)/gammu/gammulib

clean:
	@echo Cleaning binaries and object files
	@$(RM) $(COMMON) $(GAMMU) core $(TOPDIR)/common/libGammu.so
	@$(RM) $(TOPDIR)/gammu/gammu $(TOPDIR)/gammu/gammulib tags
	@$(RM) $(TOPDIR)/cfg/locale/locale.o $(TOPDIR)/cfg/locale/locale

makelocale: $(LOCALE)
	@echo Linking locale application
	@$(CC) $(LOCALE) -lm -o $(TOPDIR)/cfg/locale/locale
	@for xxx in $(LOCALE2); do \
		( cd $$xxx; \
		  ./locale \
		) \
	done
	@$(RM) $(TOPDIR)/cfg/locale/locale.o $(TOPDIR)/cfg/locale/locale

distclean: makelocale clean
	@echo Setting default config
	@chmod a+x $(TOPDIR)/configure
	@$(TOPDIR)/configure
	@$(RM) $(TOPDIR)/cfg/autoconf/config.cache
	
uninstall:
	@echo Uninstalling Gammu
	@$(RM) $(DESTDIR)$(INSTALL_BIN_DIR)/gammu $(DESTDIR)$(INSTALL_LIB_DIR)/libGammu.so
	@$(RM) $(DESTDIR)$(INSTALL_MAN_DIR)/gammu.1
	@$(RM) -r $(DESTDIR)$(INSTALL_DOC_DIR)/
	@$(RM) -r $(DESTDIR)$(INSTALL_LOC_DIR)/

installlocales:
	@echo Installing locales to $(INSTALL_LOC_DIR)
	@$(INSTALL) -m 0755 -d $(DESTDIR)$(INSTALL_LOC_DIR)
	@$(FIND) docs/docs/locale -type f -exec $(INSTALL) -m 0644 {} $(DESTDIR)$(INSTALL_LOC_DIR) \;

installdocs:
	@echo Installing docs to $(INSTALL_DOC_DIR)
	@$(INSTALL) -m 0755 -d $(DESTDIR)$(INSTALL_DOC_DIR)
	@for xxx in $(DOCS); do \
		( cd $(TOPDIR)/docs/$$xxx; \
		  $(FIND) . -type d \! -path "*CVS*" \
		       -exec $(INSTALL) -m 0755 -d $(DESTDIR)$(INSTALL_DOC_DIR)/$$xxx/{} \; ; \
		  $(FIND) . -type f \! -path "*CVS*" \
		       -exec $(INSTALL) -m 0644 {} $(DESTDIR)$(INSTALL_DOC_DIR)/$$xxx/{} \; \
		) \
	done
	@$(RM) -rf $(DESTDIR)$(INSTALL_DOC_DIR)/docs/locale
	@$(INSTALL) -m 0644 $(TOPDIR)/changelog $(DESTDIR)$(INSTALL_DOC_DIR)
	@$(INSTALL) -m 0644 $(TOPDIR)/readme.txt $(DESTDIR)$(INSTALL_DOC_DIR)
	@$(INSTALL) -m 0644 $(TOPDIR)/copying $(DESTDIR)$(INSTALL_DOC_DIR)
	@echo Installing man to $(DESTDIR)$(INSTALL_MAN_DIR)
	@$(INSTALL) -m 0755 -d $(DESTDIR)$(INSTALL_MAN_DIR)
	@$(INSTALL) -m 0644 $(TOPDIR)/docs/docs/english/gammu.1 $(DESTDIR)$(INSTALL_MAN_DIR)
	@$(RM) $(DESTDIR)$(INSTALL_DOC_DIR)/docs/english/gammu.1
	
install: uninstall all installdocs installlocales installonly

installonly: all
	@echo Installing binaries to $(DESTDIR)$(INSTALL_BIN_DIR)
	@$(INSTALL) -m 0755 -d $(DESTDIR)$(INSTALL_BIN_DIR)
	@$(INSTALL) -m 0755 $(TOPDIR)/gammu/gammu $(DESTDIR)$(INSTALL_BIN_DIR)

installlib: uninstall makelib installdocs installlocales installlibonly

installlibonly: all
	@echo Installing binaries to $(DESTDIR)$(INSTALL_BIN_DIR)
	@$(INSTALL) -m 0755 -d $(DESTDIR)$(INSTALL_BIN_DIR)
	@$(INSTALL) -m 0755 $(TOPDIR)/gammu/gammulib $(DESTDIR)$(INSTALL_BIN_DIR)/gammu

	@echo Installing shared library to $(DESTDIR)$(INSTALL_LIB_DIR)
	@$(INSTALL) -m 0755 -d $(DESTDIR)$(INSTALL_LIB_DIR)
	@$(INSTALL) -m 0644 $(TOPDIR)/common/libGammu.so $(DESTDIR)$(INSTALL_LIB_DIR)

tags: $(shell find . -name '*.[ch]' -o -name '*.cpp')
	@echo Generating tags file
	@ctags `find . -name '*.[ch]' -o -name '*.cpp'`

makecleantree:
	@echo Making Gammu tree snapshot
	@$(INSTALL) -d $(TEMP_DIR)/gammu-$(VERSION)/
	@for xxx in '.'; do \
		( cd $(TOPDIR)/$$xxx; \
		  $(FIND) . -type d \! -path "*CVS*" \
		       -exec $(INSTALL) -m 0755 -d $(TEMP_DIR)/gammu-$(VERSION)/{} \; ; \
		  $(FIND) . -type f \! -path "*CVS*" \
		       -exec $(INSTALL) -m 0644 {} $(TEMP_DIR)/gammu-$(VERSION)/{} \; \
		) \
	done
	@$(RM) -rf gammu-$(VERSION)/
	@$(INSTALL) -d gammu-$(VERSION)
	@cp $(TEMP_DIR)/gammu-$(VERSION)/* --target-directory=gammu-$(VERSION) -r
	@$(RM) $(TEMP_DIR)/gammu-$(VERSION) -r 
	@chmod a+rwx gammu-$(VERSION)/configure
	make -C gammu-$(VERSION) clean

makerpm: makecleantree
	@find gammu-$(VERSION)/ -exec chmod u+w {} \;
	@echo Creating $(RPM_DIR)/SOURCES/gammu-$(VERSION).tar.gz file
	@tar cvfz $(RPM_DIR)/SOURCES/gammu-$(VERSION).tar.gz gammu-$(VERSION) > /dev/null
	@echo Removing Gammu tree snapshot
	@$(RM) -rf gammu-$(VERSION)/
	@echo Installing $(RPM_DIR)/SPECS/gammu.spec
	@$(INSTALL) -m 0444 $(TOPDIR)/cfg/rpm/gammu.spec $(RPM_DIR)/SPECS/gammu.spec
	@echo Making packages
	@rpmbuild -ba -v $(RPM_DIR)/SPECS/gammu.spec
	@rm -rf $(RPM_DIR)/SPECS/gammu.spec
	@rm -rf $(RPM_DIR)/SOURCES/gammu-$(VERSION).tar.gz
	@rm -rf $(RPM_DIR)/BUILD/gammu-$(VERSION)
	@echo Source tarball is in $(RPM_DIR)/SRPMS, binary in $(RPM_DIR)/RPMS

makedeb: makecleantree
	@echo Copying debian directory
	@cp -r cfg/debian gammu-$(VERSION)/
	@find gammu-$(VERSION)/ -exec chmod u+w {} \;
	@echo Building Debian package
	@fakeroot make -C gammu-$(VERSION)/ -f debian/rules binary
	@echo Removing Gammu tree snapshot
	@$(RM) -rf gammu-$(VERSION)/
