# the makefile for yabbs

# ------------------------------------
# ---- SYSTEM DEPENDENT VARIABLES ----
# ------------------------------------

# file locations for "make install":
# BINDIR     - where to put the yabbs and yahoo binaries
# MANDIR     - where to put the man page for yabbs and yahoo
BINDIR=../bin
MANDIR=/usr/local/man/man1

# compiler options, etc:
# CC         - the name of your compiler (must be ANSI)
# CFLAGS     - flags to pass to the compiler
# DEFINES    - options for yabbs (listed below)
#   -DNONEWUSER  - disable creation of new users. (server)
#   -DTERMIO     - if using the unix-specific tty driver (in unix.c) this 
#                  says that termio commands should be used (usaully on Sys V
#                  systems). (client)
#   -DSGTTYB     - if using the unix-specific tty driver (in unix.c) this 
#                  says that sgttyb commands should be used (usually on BSD
#                  systems). (client)
#   -DJOBCONTROL - if you system supports job control (control-Z) this will
#                  enable yabbs to be suspended and resumed properly. (all)
#   -DAIX        - needed to compile on AIX 3.x. (all)
# LINKFLAGS  - flags to pass to the linker
# LIBS       - libraries to link against
# MAKE       - the name of your make utility
# MFLAGS     - optional flags for make
# STRSTR     - optional objects to be linked in (strstr.o and strsep.o
#              should be added if the linker complains that it can't find
#              them).
# PLATFORM   - platform to compile for (only unix is supported)
# INTERFACE  - network interface to compile for (only "socket"s are supports)

# BSDish setup
CC=cc
CFLAGS=-O -I../include
DEFINES=-DSGTTYB -DJOBCONTROL
LINKFLAGS=
LIBS=-ltermcap
MAKE=make
MFLAGS=
STRSTR=strstr.o strsep.o
PLATFORM=unix.o
INTERFACE=socket.o

# sysVish and Linux setup (untested)
#CC=cc
#LINKFLAGS=
#CFLAGS=-O
#LIBS=-ltermcap
#DEFINES=-DTERMIO -DJOBCONTROL
#MAKE=make
#MFLAGS=
#STRSTR=strstr.o strsep.o
#PLATFORM=unix.o
#INTERFACE=socket.o

# --------------------------------------
# ---- OTHER USER SETABLE VARIABLES ----
# --------------------------------------
# the address of the yabbs server (enclosed in quotes).
SERVER="localhost"
# use cp if you don't have install
INSTALL=install -c -s
#INSTALL=cp
INSTALLMAN=cp
INSTALLSCRIPT=$(INSTALLMAN)
# where the default yabbsrc file should be located (enclosed in quotes)
LOCALETC="/usr/local/etc"
# the client home directory is used when a running client detects that 
# it doesn't have one.  having a home directory is important when a public
# system spawns external editors, so that the sysop can put editor and pager
# startup files there to reduce the risk of illegal shell access.
# client home directory (enclosed like "/this/is/a/home")
CLIHOME="/usr/yabbs/data"
# the default editor and pager for clients to use (enclosed in quotes)
# specify "internal" for each to use the internal editor and pager
# WARNING: if you are compiling for a public system and you use any editor
#   or pager besides the internal ones you should test for possible security
#   holes allowing users to gain shell access.
DEFEDITOR="internal"
DEFPAGER="more"
# chmod options to use when installing yabbsd
YABBSDMODE=755

# ---------------------------------------------------
# ---- shouldn't need to change stuff under here ----
# ---------------------------------------------------
TARDIR=/usr/tmp
TD=$(TARDIR)
# isn't this ugly :)
MFL1=$(MFLAGS) CC='$(CC)' CFLAGS='$(CFLAGS)' DEFINES='$(DEFINES)'
MFL2=LINKFLAGS='$(LINKFLAGS)' LIBS='$(LIBS)' MAKE='$(MAKE)' 
MFL3=MFLAGS='$(MFLAGS)' STRSTR='$(STRSTR)' PLATFORM='$(PLATFORM)'
MFL4=INTERFACE='$(INTERFACE)' BINDIR='$(BINDIR)' MANDIR='$(MANDIR)'
MFL5=SERVER='$(SERVER)' INSTALL='$(INSTALL)' INSTALLMAN='$(INSTALLMAN)'
MFL6=INSTALLSCRIPT='$(INSTALLSCRIPT)' LOCALETC='$(LOCALETC)'
MFL7=CLIHOME='$(CLIHOME)' DEFEDITOR='$(DEFEDITOR)' DEFPAGER='$(DEFPAGER)'
MFL8=TARDIR='$(TARDIR)' YABBSDMODE='$(YABBSDMODE)'

MFLAGSLONG=$(MFL1) $(MFL2) $(MFL3) $(MFL4) $(MFL5) $(MFL6) $(MFL7) $(MFL8) 
# directories with programs to build
SUBDIRS=yabbs yabbsd utils

# directories to copy into the distributed tree
DISTDIRS=yabbs yabbsd utils include utils.broke data

# make all of the binaries
all:
	@-for dir in $(SUBDIRS); do              	\
		echo "cd $$dir";						\
		cd $$dir;	                            \
		echo "$(MAKE) $(MFLAGSLONG) all";		\
		$(MAKE) $(MFLAGSLONG) all;           	\
		cd ..;                               	\
	done

# install the binaries into the install area
install: 
	@-for dir in $(SUBDIRS); do              	\
		echo "cd $$dir";						\
		cd $$dir;	                            \
		echo "$(MAKE) $(MFLAGSLONG) install";	\
		$(MAKE) $(MFLAGSLONG) install;         	\
		cd ..;                               	\
	done

dirs:
	-mkdir bin
	-mkdir data
	-mkdir data/plan
	-mkdir data/msg
	-mkdir data/msg/email
	-mkdir data/txt
	-mkdir data/dat

clean: 
	find . -name '*core' -exec rm {} \; -print
	@-for dir in $(SUBDIRS); do              	\
		echo "cd $$dir";						\
		cd $$dir;	                            \
		echo "$(MAKE) $(MFLAGSLONG) clean";		\
		$(MAKE) $(MFLAGSLONG) clean;         	\
		cd ..;                               	\
	done

tar:
	rm -rf $(TARDIR)/yabbs
	mkdir $(TARDIR)/yabbs
	cp CHANGES README Makefile config.h COPYING-2.0 $(TARDIR)/yabbs
	@-for dir in $(DISTDIRS); do              	\
		echo "cd $$dir";						\
		cd $$dir;	                            \
		echo "mkdir $(TARDIR)/yabbs/$$dir";		\
		mkdir $(TARDIR)/yabbs/$$dir;			\
		echo "$(MAKE) $(MFLAGSLONG) tar";		\
		$(MAKE) $(MFLAGSLONG) tar;	         	\
		cd ..;                               	\
	done
	@echo "removing tabs:";
	@find $(TARDIR)/yabbs -name '*.[ch]' -print -exec untab 4 {} \;;
	@echo "generalizing $(TARDIR)/yabbs/config.h"
	@(awk '{																\
		if (($$1 ~ "#define") && ($$2 ~ "D_SUBS")) {						\
			sub($$3, "0x8ffffff");											\
			print $$0;														\
		} else if (($$1 ~ "#define") && ($$2 ~ "NONEWUSER")) {				\
			printf("#undef NONEWUSER\n");									\
		} else if (($$1 ~ "#define") && ($$2 ~ "INACTIVITYTIMEOUT")) {		\
			sub($$3, "1800");												\
			print $$0;														\
		} else print $$0;													\
		}' < $(TD)/yabbs/config.h > $(TD)/yabbs/config.h.tmp;				\
		mv -f $(TD)/yabbs/config.h.tmp $(TD)/yabbs/config.h)
	@echo "generalizing $(TARDIR)/yabbs/Makefile"
	@(awk -F= '{															\
		if ($$1 == "YABBSDMODE")											\
			print "YABBSDMODE=755";											\
		else print $$0;														\
		}' < $(TD)/yabbs/Makefile > $(TD)/yabbs/Makefile.tmp;				\
	mv -f $(TD)/yabbs/Makefile.tmp $(TD)/yabbs/Makefile)
	@echo "building system tar"
	@(cd $(TARDIR); tar cvfz $(TARDIR)/yabbsnet.tar.gz yabbs);
	@echo "building client tar"
	mv $(TARDIR)/yabbs/yabbs $(TARDIR)/yabbs/yabbscli
	@(cd $(TARDIR)/yabbs; tar cvfz $(TARDIR)/yabbscli.tar.gz yabbscli);
	mv $(TARDIR)/yabbs???.tar.gz .
	@echo "removing tmp dir"
	@rm -rf $(TARDIR)/yabbs
	@echo "renaming distribution tars:"
	@mv yabbscli.tar.gz "yabbscli-`cat yabbs/plevel.h | grep CLIENTVERSION | cut -d'\"' -f2`.tar.gz"
	@mv yabbsnet.tar.gz "yabbsnet-`cat yabbsd/patchlevel.h | grep SERVERVERSION | cut -d'\"' -f2`s-`cat yabbs/plevel.h | grep CLIENTVERSION | cut -d'\"' -f2`c.tar.gz"
	@echo "distribution tars:"
	@ls -l yabbs*.tar.gz
