#
#  APRX -- 2nd generation receive-only APRS-i-gate with
#          minimal requirement of esoteric facilities or
#          libraries of any kind beyond UNIX system libc.
#
# Note: This makefile uses features from GNU make

# -------------------------------------------------------------------- #

# target paths
VARRUN=		/var/run	# directory for aprx.state and pid-file
VARLOG=		/var/log/aprx	# directory for direct logfiles
CFGFILE=	@sysconfdir@/aprx.conf	# default configuration file
SBINDIR=	@sbindir@	# installation path for programs
MANDIR=		@mandir@	# installation path for manual pages

# -------------------------------------------------------------------- #

srcdir = @srcdir@
VPATH = @srcdir@
@SET_MAKE@

PROF=		# used by 'make profile'

# Compiler and flags
CC=		@CC@
CFLAGS=		@CFLAGS@ @CCPTHREAD@

# Linker and flags
LD=		@CC@
LDFLAGS=	@LDFLAGS@ $(PROF)
datarootdir=	@datarootdir@

INSTALL=	$(srcdir)/install-sh
INSTALL_PROGRAM=$(INSTALL)  -m 755
INSTALL_DATA=	$(INSTALL)  -m 644

# -------------------------------------------------------------------- #
# no user serviceable parts below 
# -------------------------------------------------------------------- #

# strip extra whitespace from paths
VARRUN:=$(strip $(VARRUN))
VARLOG:=$(strip $(VARLOG))
CFGFILE:=$(strip $(CFGFILE))
SBINDIR:=$(strip $(SBINDIR))
MANDIR:=$(strip $(MANDIR))

# generate version strings

VERSION	= $(shell echo @VERSION_STRING@ | cut -b 2-)

DATE:=$(shell date +"%Y %B %d")
RFCDATE:=$(shell date +"%a, %d %b %Y %H:%M:%S %z")

DEFS=	-DAPRXVERSION="\"$(VERSION)\"" \
	-DVARRUN="\"$(VARRUN)\"" -DVARLOG="\"$(VARLOG)\"" \
	-DCFGFILE="\"$(CFGFILE)\""

# program names
PROGAPRX=	aprx
PROGSTAT=	$(PROGAPRX)-stat

LIBS=		@LIBS@ @LIBRESOLV@ @LIBSOCKET@  @LIBM@ @LIBPTHREAD@ @LIBGETADDRINFO@ @LIBRT@
OBJSAPRX=	aprx.o ttyreader.o ax25.o aprsis.o beacon.o config.o	\
		netax25.o erlang.o aprxpolls.o telemetry.o igate.o	\
		cellmalloc.o historydb.o keyhash.o parse_aprs.o		\
		dupecheck.o  kiss.o interface.o pbuf.o digipeater.o	\
		valgrind.o filter.o dprsgw.o  crc.o  agwpesocket.o	\
		netresolver.o timercmp.o #ssl.o

OBJSSTAT=	erlang.o aprx-stat.o aprxpolls.o valgrind.o timercmp.o

# man page sources, will be installed as $(PROGAPRX).8 / $(PROGSTAT).8
MANAPRX := 	aprx.8
MANSTAT := 	aprx-stat.8

OBJS=		$(OBJSAPRX) $(OBJSSTAT)
MAN=		$(MANAPRX) $(MANSTAT)

# -------------------------------------------------------------------- #

.PHONY: 	all
all:		$(PROGAPRX) $(PROGSTAT) man aprx.conf aprx-complex.conf

valgrind:
		@echo "Did you do 'make clean' before 'make valgrind' ?"
		make all CFLAGS="${CFLAGS} -D_FOR_VALGRIND_"

profile:
		@echo "Did you do 'make clean' before 'make profile' ?"
		make all PROF="-pg"


$(PROGAPRX):	$(OBJSAPRX) VERSION Makefile
		$(LD) $(LDFLAGS) -o $@ $(OBJSAPRX) $(LIBS)

$(PROGSTAT):	$(OBJSSTAT) VERSION Makefile
		$(LD) $(LDFLAGS) -o $@ $(OBJSSTAT) $(LIBS)

.PHONY:		man
man:		$(MAN)

.PHONY:		doc html pdf
doc:		html pdf
pdf:		$(MAN:=.pdf)
html:		$(MAN:=.html)

# -------------------------------------------------------------------- #

.PHONY:	install install-deb
install: all
	$(INSTALL_PROGRAM) $(PROGAPRX) $(DESTDIR)$(SBINDIR)/$(PROGAPRX)
	$(INSTALL_PROGRAM) $(PROGSTAT) $(DESTDIR)$(SBINDIR)/$(PROGSTAT)
	$(INSTALL_DATA) $(MANAPRX) $(DESTDIR)$(MANDIR)/man8/$(PROGAPRX).8
	$(INSTALL_DATA) $(MANSTAT) $(DESTDIR)$(MANDIR)/man8/$(PROGSTAT).8
	if [ ! -f  $(DESTDIR)$(CFGFILE) ] ; then \
		$(INSTALL_DATA) aprx.conf $(DESTDIR)$(CFGFILE) ; \
	else true ; fi

.PHONY: clean
clean:
	rm -f $(PROGAPRX) $(PROGSTAT)
	rm -f $(MAN) $(MAN:=.html) $(MAN:=.ps) $(MAN:=.pdf)	\
	rm -f aprx.conf	 logrotate.aprx
	rm -f *~ *.o *.d

.PHONY: distclean
distclean: clean
	rm -f config.log config.status config.h
	rm -rf autom4te.cache *.log* doc/.~*#

# -------------------------------------------------------------------- #

%.o: %.c VERSION Makefile
	$(CC) $(CFLAGS)  $(PROF) $(DEFS) -c $<
	@$(CC) -MM $(CFLAGS) $(PROF) $(DEFS) $< > $(@:.o=.d)

$(MAN:=.html): %.html : %
	sh man-to-html.sh $< > $@

$(MAN:=.ps): %.ps : %
	groff -man $< > $@

$(MAN:=.pdf): %.pdf : %.ps
	ps2pdf $<

logrotate.aprx $(MAN) aprx-complex.conf aprx.conf: % : %.in VERSION Makefile
	perl -ne "s{\@DATEVERSION\@}{$(VERSION) - $(DATE)}g;	\
	          s{\@VARRUN\@}{$(VARRUN)}g;			\
	          s{\@VARLOG\@}{$(VARLOG)}g;			\
	          s{\@CFGFILE\@}{$(CFGFILE)}g;			\
		  print;"					\
	 < $< > $@

# -------------------------------------------------------------------- #

#
# Following is for the original author only...
#

DISTVERSION:=aprx-$(VERSION)
DISTTARGET:=../$(DISTVERSION)
RPMVERSION:=$(shell echo "${DISTVERSION}" | sed -e 's/aprx-//')
.PHONY: dist

dist:
	if [ ! -d $(DISTTARGET) ] ; then	\
		mkdir $(DISTTARGET) ;		\
	fi
	tar cf - --exclude-backups --exclude-vcs --exclude=windows --exclude=*.log* --exclude=*.conf . | (cd $(DISTTARGET) ; tar xf -)
	echo "$(DISTVERSION)" > $(DISTTARGET)/VERSION
	perl -ne "\$$ver = '$(DISTVERSION)'; 	\
		  \$$ver =~ tr/0-9.//cd;	\
		  \$$ver .= '-1';		\
		  s{\@VERSION\@}{\$$ver}g;	\
		  s{\@RFCDATE\@}{$(RFCDATE)}g;	\
		  print;"			\
		  < $(DISTTARGET)/debian/changelog.release	\
		  > $(DISTTARGET)/debian/changelog
	rm -f $(DISTTARGET)/debian/changelog.release
	rm -f $(DISTTARGET)/aprx.spec
	perl -ne "s{\@VERSION\@}{$(RPMVERSION)}g;	\
		  s{\@DATE0\@}{$(DATE0)}g;		\
		  print;"				\
		  < $(DISTTARGET)/rpm/aprx.spec.in	\
		  > $(DISTTARGET)/aprx.spec
	rm -f $(DISTTARGET)/rpm/aprx.spec.in
	make -C $(DISTTARGET) distclean
	cd .. && 	\
	tar czvf $(DISTVERSION).tar.gz $(DISTVERSION)

# -------------------------------------------------------------------- #

.PHONY: make-deb make-rpm

make-deb:
	if [ -f debian/changelog.release ] ; then \
	perl -ne "\$$ver = '$(VERSION)'; 	\
		  s{\@VERSION\@}{\$$ver}g;	\
		  s{\@RFCDATE\@}{$(RFCDATE)}g;	\
		  print;"			\
		  < debian/changelog.release	\
		  > debian/changelog ; \
	fi
	dpkg-buildpackage -b -us -uc -rfakeroot

make-rpm: # actually just a reminder of how to do it..
	rpmbuild --target i386 -ta ../$(DISTVERSION).tar.gz 

# -------------------------------------------------------------------- #

# include object depencies if available
-include $(OBJS:.o=.d)
