#
#   libmodem library, a modem control facility.
#
#   Makefile
#
#   Copyright (C) 1994,1995,1996,1997,1998,1999  Riccardo Facchetti
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Library General Public
#   License as published by the Free Software Foundation; either
#   version 2 of the License, or (at your option) any later version.
#
#   This library 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
#   Library General Public License for more details.
#
#   You should have received a copy of the GNU Library General Public
#   License along with this library; if not, write to the Free
#   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#   You can contact the author at this e-mail address:
#
#   fizban@tin.it
#
#
# Main Makefile
#

LIB_VERSION= 1.5
LIB_VERSION_MAJOR= 1

include CONFIG.MAKE

CFLAGS= -I. $(CCFLAGS) $(DEFINES) $(DEBUG) -DLIBRARY -DLIB_VERSION=$(LIB_VERSION)
MANFIN= $(MANDIR)/man$(MANEXT)

# for the library

LIB_STATIC=	libmodem.a

OBJS=	modems.o mdmerrno.o line_manage.o mdmlock.o mdmlog.o mdmalloc.o
SRCS=	modems.c mdmerrno.c line_manage.c mdmlock.c mdmlog.c mdmalloc.c

ifdef WITHSHARED
LIB_SHARED=	libmodem.so.$(LIB_VERSION)
LIB_SH_SONAME=	libmodem.so.$(LIB_VERSION_MAJOR)
SHOBJS=	modems.so mdmerrno.so line_manage.so mdmlock.so mdmlog.so mdmalloc.so
endif

ALL_LIBS=	$(LIB_STATIC) $(LIB_SHARED)


#
# stuff to build
#

.c.o:
	$(CC) $(CFLAGS) -c $*.c -o $*.o
ifdef WITHSHARED
	$(CC) -fPIC $(CFLAGS) -c $*.c -o $*.so
endif

all:	dial/modems.h $(ALL_LIBS) doc/modems.$(MANEXT)

dep depend .depend:
	$(CPP) -M $(CFLAGS) *.c >.depend

dial/modems.h: dial/modems.h.in
	cat dial/modems.h.in | \
	sed -e 's|_LIB_VERSION_|\"$(LIB_VERSION)\"|g' \
	    -e 's|_LOCK_PATH_|\"$(LOCK_PATH)\"|g' > dial/modems.h

doc/modems.$(MANEXT): doc/modems.man
	cat doc/modems.man | \
	sed -e 's|_LIB_VERSION_|$(LIB_VERSION)|g' \
	    -e 's|_LOCK_PATH_|$(LOCK_PATH)|g' > doc/modems.$(MANEXT)
	nroff -man doc/modems.$(MANEXT) > doc/modems.raw
	cat doc/modems.raw | \
	sed -e 's|.||g' > doc/modems.txt
	rm doc/modems.raw

$(LIB_STATIC): $(OBJS)
	$(RM) $@.bak
	-$(MV) $@ $@.bak
	$(AR) $@ $(OBJS)
	$(RANLIB) $@

ifdef WITHSHARED
$(LIB_SHARED): $(SHOBJS)
	$(RM) $@.bak
	-$(MV) $@ $@.bak
	$(CC) -shared -Wl,-soname -Wl,$(LIB_SH_SONAME) $(CFLAGS) -o $(LIB_SHARED) $(SHOBJS)
endif

test: all
	$(CC) $(CFLAGS) -o call call.c -L./ -lmodem

install: $(ALL_LIBS)
	$(INSTALL) -m 755 -d $(USRLIBDIR)
	$(INSTALL) -m 755 -d $(MANFIN)
	$(INSTALL) -m 755 -d $(INCDIR)
	$(INSTALL) -o bin -g bin -m 644 $(LIB_STATIC) $(USRLIBDIR)
ifdef WITHSHARED
	$(INSTALL) -o bin -g bin -m 755 $(LIB_SHARED) $(LIBDIR)
	$(RM) $(LIBDIR)/libmodem.so
	$(LN) -s $(LIBDIR)/$(LIB_SHARED) $(LIBDIR)/libmodem.so
	$(LDCONFIG)
endif
	$(INSTALL) -o bin -g bin -m 644 dial/modems.h $(INCDIR)
	$(INSTALL) -o bin -g bin -m 644 dial/mdmerrno.h $(INCDIR)
	$(INSTALL) -o bin -g bin -m 644 doc/modems.$(MANEXT) $(MANFIN)/modems.$(MANEXT)

dist: distclean
	( cp libmodem.lsm .. && \
	  cd .. && \
	  rm -f libmodem-$(LIB_VERSION).tar libmodem-$(LIB_VERSION).tar.gz \
	  libmodem-$(LIB_VERSION).lsm && \
	  tar cfv libmodem-$(LIB_VERSION).tar libmodem-$(LIB_VERSION) && \
	  gzip -9 libmodem-$(LIB_VERSION).tar && \
	  cat libmodem.lsm | \
	  sed -e "s|_DATE_|`date +%d%b%y`|g" \
	   -e "s|_LIB_VERSION_|$(LIB_VERSION)|g" \
	   -e "s|_T_S_|`find libmodem-$(LIB_VERSION).tar.gz -printf \"%k\"`|g"\
	   -e "s|_L_S_|`find libmodem.lsm -printf "%2k"`|g" > \
	      libmodem-$(LIB_VERSION).lsm && \
	  rm -f libmodem.lsm \
	)

clean:
	$(RM) *.o *.so *~ \#* *.bak core call
	(cd etc; rm -f *~ \#* core *.bak)
	(cd dial; rm -f modems.h *~ \#* core *.bak)
	(cd doc; rm -f *~ \#* core *.bak modems.$(MANEXT) modems.raw \
	modems.txt)

distclean: clean
	$(RM) $(ALL_LIBS) .depend

ifeq (.depend,$(wildcard .depend))
include .depend
endif
