# master make rules (gnu make) for building programs
#

# translate SRCS into OBJS
#
ifdef SRCS
_OBJS=$(patsubst %.c,%.o,$(SRCS))
__OBJS=$(patsubst %.cc,%.o,$(_OBJS))
OBJS=$(__OBJS)
else
OBJS=$(PROG).o
endif

# set up manpages to install
#
ifdef MAN1
MANPG += $(MAN1)
endif
ifdef MAN2
MANPG += $(MAN2)
endif
ifdef MAN3
MANPG += $(MAN3)
endif
ifdef MAN4
MANPG += $(MAN4)
endif
ifdef MAN5
MANPG += $(MAN5)
endif
ifdef MAN8
MANPG += $(MAN8)
endif
ifdef MAN
MANPG += $(MAN)
endif

ifdef MANPG
MANPAGES += $(MANPG)
endif

ifndef NOMAN
ifndef MANPAGES
ifeq ($(PROG).1,$(wildcard $(PROG).1))
MANPAGES = $(PROG).1
endif
endif
endif

ifndef INSTALL
INSTALL=install
endif
ifndef COPY
COPY= -c
endif
INSTALL_COPY=${COPY}

ifndef TOP
TOP=$(shell cd ..;pwd)
CFLAGS += -I$(TOP)/00library -I/usr/include/bsd -DDEFFILEMODE=0666 -DLINUX=1
endif


ifndef .CURDIR
.CURDIR=$(shell pwd)
endif

ifeq ($(TOP)/Make.defs,$(wildcard $(TOP)/Make.defs))
include $(TOP)/Make.defs
endif

ifndef BINDIR
BINDIR=/usr/bin
endif
export BINDIR

ifndef BINOWN
BINOWN=bin
endif
export BINOWN

ifndef BINGRP
BINGRP=bin
endif
export BINGRP

ifndef BINMODE
BINMODE=0511
endif
export BINMODE
ifndef NONBINMODE
NONBINMODE=0644
endif
export NONBINMODE

ifndef DIRMODE
DIRMODE=0755
endif
export DIRMODE

TARGETS=$(PROG)

ifdef SUBDIR

# if we have subdirs, do them (assume there are no things to build on
# this level)

all clean install::
	for x in $(SUBDIR); do \
	    CFLAGS="$(CFLAGS)" TOP=`cd $(TOP);pwd` make -C $$x $@ || exit 1; \
	done
endif

# build the application that lives on this level

ifdef PROG
all:: $(PROG)

$(PROG): $(OBJS)
	$(CC) -L$(TOP)/00library -o $(PROG) $(OBJS) $(LDADD) $(DPADD) -lmastodonbsd

clean::
	rm -f $(PROG) $(OBJS) *~ core a.out $(CLEANFILES)

else
ifdef LIB
all:: lib$(LIB).a

lib$(LIB).a: $(OBJS)
	ar crv lib$(LIB).a $(OBJS)
	ranlib lib$(LIB).a

clean::
	rm -f lib$(LIB).a $(OBJS) *~ core a.out $(CLEANFILES)

else
all clean::
endif
endif

bin-install:
ifdef LIB
	install -d -m $(DIRMODE) -o $(BINOWN) -g $(BINGRP) $(DESTDIR)$(BINDIR)
	install -c -m $(BINMODE) -o $(BINOWN) -g $(BINGRP) lib$(LIB).a $(DESTDIR)$(BINDIR)
endif
ifdef PROG
	install -d -m $(DIRMODE) -o $(BINOWN) -g $(BINGRP) $(DESTDIR)$(BINDIR)
	install -c -s -m $(BINMODE) -o $(BINOWN) -g $(BINGRP) $(PROG) $(DESTDIR)$(BINDIR)
ifdef PLINKS
	cd $(DESTDIR)$(BINDIR); set - $(PLINKS); \
	while [ $$# -gt 0 ]; do \
	    ln -f $$1 $$2; \
	    shift 2; \
	done
endif
endif
ifdef LINKS
	set - $(LINKS); \
	while [ $$# -gt 0 ]; do \
	    ln -f $(DESTDIR)/$$1 $(DESTDIR)/$$2; \
	    shift 2; \
	done
endif

maninstall man-install:
ifdef MANPAGES
	for x in $(MANPAGES); do \
	    ext=`echo $$x | sed -e 's/.*\(.\)$$/\1/'`; \
	    install -d -m 755 -o man -g man $(DESTDIR)/usr/man/man$$ext; \
	    install -c -m 444 -o man -g man $$x $(DESTDIR)/usr/man/man$$ext; \
	done
ifdef MLINKS
	set - $(MLINKS); cd $(DESTDIR)/usr/man; \
	while [ $$# -ge 2 ]; do \
	    ext=`echo $$1 | sed -e 's/.*\(.\)$$/\1/'`; \
	    ln -f man$$ext/$$1 man$$ext/$$2; \
	    shift 2; \
	done
endif
endif


install:: beforeinstall bin-install man-install afterinstall

beforeinstall afterinstall::
