# Copyright (C) 2001-2006  Simon Baldwin (simon_baldwin@yahoo.com)
# Copyright (C) 2011       Kamil Ignacak (acerion@wp.pl)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

include ../../Makefile.inc
CC	= $(AC_CC)
CFLAGS	= $(AC_CFLAGS) $(AC_CFLAG_PIC) $(AC_DEFS)
AWK	= $(AC_AWK)
RANLIB	= $(AC_RANLIB)
LD	= $(AC_LD)
LN_S	= $(AC_LN_S)
GZIP	= $(AC_GZIP)
LDCONFIG= $(AC_LDCONFIG)
CC_LINKS_SO	= $(AC_CC_LINKS_SO)
LD_LINKS_SO	= $(AC_LD_LINKS_SO)

SHELL	= /bin/sh

COMMON_LIBS = -lm -lpthread -lasound

default: all

all: libcw.a libcw.so.3.0.0 libcw.so.3 libcw.so libcw.pc libcw.3 libcwtest

# Build the static and shared libraries.
libcw.a: libcw.o
	rm -f libcw.a; ar -cr libcw.a libcw.o
	$(RANLIB) libcw.a

libcw.so.3.0.0: libcw.o
	if [ "$(CC_LINKS_SO)" = "yes" ]; then				\
		$(CC) -shared -Wl,-soname,libcw.so.3			\
			-o libcw.so.3.0.0 libcw.o $(COMMON_LIBS);	\
	else								\
		if [ "$(LD_LINKS_SO)" = "yes" ]; then			\
			$(LD) -G -Wl,-soname,libcw.so.3			\
				-o libcw.so.3.0.0 libcw.o $(COMMON_LIBS); \
		fi							\
	fi

libcw.so.3: libcw.so.3.0.0
	$(LN_S) -f libcw.so.3.0.0 libcw.so.3

libcw.so: libcw.so.3.0.0
	$(LN_S) -f libcw.so.3.0.0 libcw.so

# Build the library man page.
libcw.3: libcw.3.m4 libcw.c
	$(AWK) -f libdoc.awk <libcw.c | $(AWK) -f libsigs.awk >signatures
	$(AWK) -f libdoc.awk <libcw.c | $(AWK) -f libfuncs.awk >functions
	$(AWK) -f include.awk <libcw.3.m4 >libcw.3
	rm -f signatures functions

# Build the unit test.
libcwtest: libcwtest.o
	$(CC) -o libcwtest libcwtest.o -L. -lcw

# Build the pkgconfig metadata file.
libcw.pc:
	$(AWK) -vprefix="$(prefix)" -v exec_prefix="$(exec_prefix)"	\
	       -vlibdir="$(libdir)" -v includedir="$(includedir)"	\
	       -vstrip="$$RPM_BUILD_ROOT"				\
	       -vconfig_h="../config.h" -f libpc.awk >libcw.pc

# Define dependencies related to header/include files.
libcw.o: Makefile libcw.h

libcwtest.o: Makefile libcw.h

# Install targets.
install: all
	$(INSTALL) -d $(includedir) $(libdir) $(mandir)/man3 $(mandir)/man7 \
		$(libdir)/pkgconfig
	$(INSTALL_DATA) libcw.h $(includedir)/libcw.h
	$(INSTALL_DATA) libcw.a $(libdir)/libcw.a
	if [ "$(CC_LINKS_SO)" = "yes" -o "$(LD_LINKS_SO)" = "yes" ]; then \
		$(INSTALL_PROGRAM) libcw.so.3.0.0 $(libdir)/libcw.so.3.0.0; \
		$(LN_S) -f libcw.so.3.0.0 $(libdir)/libcw.so.3; \
		$(LN_S) -f libcw.so.3.0.0 $(libdir)/libcw.so; \
		$(LDCONFIG) -n $(libdir); \
	fi
	$(INSTALL_DATA) libcw.3 $(mandir)/man3/libcw.3
	$(GZIP) -f -9 $(mandir)/man3/libcw.3
	$(INSTALL_DATA) cw.7 $(mandir)/man7/cw.7
	$(GZIP) -f -9 $(mandir)/man7/cw.7
	$(INSTALL_DATA) cw.7 $(mandir)/man7/CW.7
	$(GZIP) -f -9 $(mandir)/man7/CW.7
	$(INSTALL_DATA) libcw.pc $(libdir)/pkgconfig/libcw.pc

install-strip:
	$(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' install

uninstall:
	rm -f $(includedir)/libcw.h
	rm -f $(libdir)/libcw.a
	rm -f $(libdir)/libcw.so $(libdir)/libcw.so.3 $(libdir)/libcw.so.3.0.0
	$(LDCONFIG) -n $(libdir)
	rm -f $(mandir)/man3/libcw.3 $(mandir)/man3/libcw.3.gz
	rm -f $(mandir)/man7/cw.7 $(mandir)/man7/cw.7.gz
	rm -f $(mandir)/man7/CW.7 $(mandir)/man7/CW.7.gz
	rm -f $(libdir)/pkgconfig/libcw.pc

# Cleanup targets.
clean:
	rm -f libcw.a libcw.so libcw.so.3 libcw.so.3.0.0 *.s libcw.pc *.o
	rm -f libcw.3 signatures functions
	rm -f libcwtest
	rm -f core core.*

distclean: clean
	rm -f Makefile.port

mostlyclean: clean

maintainer-clean: distclean

# Test targets.
check: all libcwtest
	-LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH ./libcwtest

# Unimplemented targets.
TAGS:
info:
dvi:
dist:
