
######################################################################
# 'sall': client/servers system to display the people logged in on   #
# the machines at ENS Lyon.  Sebastien Lacour (DSM97), Vincent       #
# Danjean (DMI97).  May 2000.                                        #
######################################################################


### available targets:
# [all]: make both the client and the server on the given architecture
# debug: compile with '-g' option
# log: compile with '-g' plus the verbose debug logs enabled
# clean: cleans up all the files made by the build process
# dist: creates the compressed archive .tar.gz from the current state of the
#       working directory
# install: make and strip and install (ENSL file system / softelev) the client
#          and the server
# notrans: disable the group translation


.PHONY: clean dist all debug log notrans


### miscellaneous features:
ifeq ($(findstring notrans, $(MAKECMDGOALS)),notrans)
USE_TRANSLATE_GROUP	:=	no
else
USE_TRANSLATE_GROUP	:=	yes
endif
SYSTEM	:=	$(shell uname -s)
ARCH	:=	$(shell uname -p)
ifeq ($(SYSTEM),SunOS)
	OS	=	SOLARIS
else
	ifeq ($(SYSTEM),Linux)
		OS      =	LINUX
	else
		OS	=	UNKNOWN
	endif
endif
ifeq ($(USE_TRANSLATE_GROUP),yes)
	CFLAGS	+=	-DTRANSLATE_GROUP $(shell glib-config --cflags)
	LDLIBS	+=	$(shell glib-config --libs)
endif


### files:
BINDIR	:=	/soft/eleves/stow/sall/$(ARCH)/bin
SBINDIR	:=	/soft/eleves/stow/sall/$(ARCH)/sbin
DIST_FILES	=	sall.c daemon.c Makefile common.c sall.h daemon.h \
			common.h sall-init
PACKAGE	=	sall
ARCHIV	=	$(PACKAGE).tar.gz

### commands:
CC	=	gcc
COMPILE	=	$(CC) $(CFLAGS)
LINK	=	$(COMPILE) $(LDLIBS)
RM	=	rm -f
TAR	=	tar czvf
CP	=	cp
MKDIR	=	mkdir
STRIP	=	strip


### options:
CFLAGS	+=	-Wall -O3 #-pedantic
ifeq ($(findstring debug, $(MAKECMDGOALS)),debug)
	CFLAGS	+=	-g
endif
ifeq ($(findstring log, $(MAKECMDGOALS)),log)
	CFLAGS	+=	-g -DDEBUG
endif
CFLAGS_SOLARIS	=	-DSOLARIS
CFLAGS_LINUX	=	-DLINUX
LDLIBS	+=	-lpthread
LDLIBS_SOLARIS	=	-lsocket -lnsl #-lposix4 servait pour la smaphore
LDLIBS_LINUX	=	#-lefence

CFLAGS	+=	$(CFLAGS_$(OS))
LDLIBS  += $(LDLIBS_$(OS))


######################################################################

all: all-$(ARCH)
log: all
debug: all
notrans: all

all-$(ARCH): sall-$(ARCH) daemon-$(ARCH)

sall-$(ARCH): sall-$(ARCH).o common-$(ARCH).o
	$(LINK) -o $@ sall-$(ARCH).o common-$(ARCH).o

daemon-$(ARCH): daemon-$(ARCH).o common-$(ARCH).o
	$(LINK) -o $@ daemon-$(ARCH).o common-$(ARCH).o

sall-$(ARCH).o: sall.c sall.h
	$(COMPILE) -o $@ -c sall.c

daemon-$(ARCH).o: daemon.c daemon.h
	$(COMPILE) -o $@ -c daemon.c

common-$(ARCH).o: common.c common.h
	$(COMPILE) -o $@ -c common.c

clean:
	rm -f *-*.o sall-$(ARCH) daemon-$(ARCH)

install: all
	$(MKDIR) -p $(BINDIR)
	$(STRIP) sall-$(ARCH)
	$(CP) sall-$(ARCH) $(BINDIR)/sall
	$(MKDIR) -p $(SBINDIR)
	$(STRIP) daemon-$(ARCH)
	$(CP) daemon-$(ARCH) $(SBINDIR)/salld

dist:
	@ echo "Selecting the files for the archive \"$(ARCHIV)\":"
	@ $(MKDIR) $(PACKAGE)
	@ for file in $(DIST_FILES); do \
	     $(CP) $$file $(PACKAGE)/$$file; \
	  done
	@ $(TAR) $(ARCHIV) $(PACKAGE)
	@ $(RM) -r $(PACKAGE)

