
VERSION	= 1.0
CFLAGS	= -Wall -g
SBINDIR	= $(DESTDIR)/sbin
LIBDIR	= $(DESTDIR)/lib
INCDIR	= $(DESTDIR)/usr/include
MANDIR	= $(DESTDIR)/usr/share/man
PAMDIR	= $(DESTDIR)/lib/security
ETCDIR	= $(DESTDIR)/etc

SRV	= resmgrd
SRVOBJS	= $(addprefix .objs/,main.o session.o \
	  class.o acl.o config.o svc.o \
	  family.o file.o scsi.o usb.o paride.o \
	  utils.o)

CLT	= resmgr
CLTOBJS	= $(addprefix .objs/,resmgr.o)

PAM	= pam_resmgr.so
PAMOBJS	= $(addprefix .lobjs/,pam_resmgr.o)

LIBBASE	= libresmgr.so
LIB	= $(LIBBASE).$(VERSION)
LIBOBJS	= $(addprefix .lobjs/,conn.o client.o glob.o)

ALL	= $(SRV) $(CLT) $(LIB) $(PAM) tester

all:	$(ALL)

install: $(ALL)
	mkdir -p $(SBINDIR) $(LIBDIR) $(MANDIR)/man{1,3,5,8} \
		$(ETCDIR) $(PAMDIR) $(INCDIR)
	install -m 555 $(LIB) $(LIBDIR)
	install -m 555 $(PAM) $(PAMDIR)
	if [ ! -f $(ETCDIR)/resmgr.conf ]; then \
		install -m 644 etc/resmgr.conf $(ETCDIR); \
	fi
	install -m 444 resmgr.h $(INCDIR)
	install -m 555 -s $(CLT) $(SRV) $(SBINDIR)
	for x in 1 3 5 8; do \
		install -m 444 doc/*.$$x $(MANDIR)/man$$x; \
	done

clean distclean:
	rm -rf $(ALL) $(LIBBASE) .objs .lobjs

$(SRV):	$(SRVOBJS) $(LIB)
	$(CC) -o $@ $(SRVOBJS) -L. -lresmgr

$(CLT):	$(CLTOBJS) $(LIB)
	$(CC) -o $@ $(CLTOBJS) -L. -lresmgr

$(LIB): $(LIBOBJS)
	$(CC) -shared -Wl,-soname,$(notdir $(LIB)) -o $@ $(LIBOBJS)
	ln -sf $(LIB) $(LIBBASE)

$(PAM): $(PAMOBJS) $(LIB)
	$(CC) -shared -o $@ $(PAMOBJS) -L. -lresmgr

tester: .objs/tester.o $(LIB)
	$(CC) -o $@ .objs/tester.o -L. -lresmgr

acltest: acl.c .objs/utils.o
	$(CC) $(CFLAGS) -o $@ -DTEST acl.c .objs/utils.o

.objs/%.o: %.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -o $@ -c $< 

.lobjs/%.o: %.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -fPIC -o $@ -c $< 
