# the makefile for yabbs

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

# options
# -DNONEWUSER will disable new users from being created
OPTIONS=

# BSDish setup
CC=gcc
CFLAGS=-DBSD -DTERMIO=0 -I../include -O6
MAKE=make
MFLAGS=
STRSTR=
# if the loader can't find strstr and strsep add this line
# STRSTR=strstr.o strsep.o

# sysVish and Linux setup (untested)
#CC=cc
#CFLAGS=-DTERMIO=1 -I../include $(OPTIONS)
#MAKE=make
#MFLAGS=
#STRSTR=strstr.o strsep.o

# --------------------------------------
# ---- OTHER USER SETABLE VARIABLES ----
# --------------------------------------
# the address of the yabbs server (enclosed like \"name.of.server\").
SERVER=\"localhost\"
# use cp if you don't have install
INSTALL=install -c
#INSTALL=cp
# where you want it installed
BIN=../bin

# ---------------------------------------------------
# ---- shouldn't need to change stuff under here ----
# ---------------------------------------------------
TARDIR=/usr/tmp
TD=$(TARDIR)
MFLAGSLONG=$(MFLAGS) "MAKE=$(MAKE)" "MFLAGS=$(MFLAGS)" "CC=$(CC)" "CFLAGS=$(CFLAGS)" "STRSTR=$(STRSTR)" "SERVER=$(SERVER)" "INSTALL=$(INSTALL)" "BIN=$(BIN)" "TARDIR=$(TARDIR)"

# 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: yabbs/bbs_struct.h
	@-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/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
	@-for dir in $(SUBDIRS); do												\
		echo "removing tabs in $$dir:";										\
		find $(TARDIR)/yabbs/$$dir -name '*.[ch]' -print -exec untab 4 {} \;;\
	done
	@echo "generalizing $(TARDIR)/yabbs/config.h"
	@(awk '{																\
		if (($$1 ~ "#define") && ($$2 ~ "D_SUBS")) 							\
			printf("%s %s %s\n", $$1, $$2, "0x08ffffff"); 					\
		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 == "MAKE")													\
			print "MAKE=make";												\
		else if ($$1 == "CFLAGS")											\
			print "CFLAGS=-DBSD -DTERMIO=0 -I../include -O6";				\
		else print $$0;														\
		}' < $(TD)/yabbs/Makefile > $(TD)/yabbs/Makefile.tmp;				\
		mv -f $(TD)/yabbs/Makefile.tmp $(TD)/yabbs/Makefile)
	@echo "generalizing $(TARDIR)/yabbs/yabbs/Makefile"
	@(awk -F= '{															\
		if (($$1 == "MAKE"))												\
			print "MAKE=make";												\
		else print $$0;														\
		}' < $(TD)/yabbs/yabbs/Makefile > $(TD)/yabbs/yabbs/Makefile.tmp;	\
		mv -f $(TD)/yabbs/yabbs/Makefile.tmp $(TD)/yabbs/yabbs/Makefile)
	@echo "building system tar"
	@(cd $(TARDIR); tar cvfz $(TARDIR)/yabbsnet.tar.gz yabbs);
	@echo "building client tar"
	@(cd $(TARDIR)/yabbs; tar cvfz $(TARDIR)/yabbscli.tar.gz yabbs);
	mv $(TARDIR)/yabbs???.tar.gz .
	@echo "removing tmp dir"
	@rm -rf $(TARDIR)/yabbs
	@echo "distribution tars:"
	@ls -l yabbs???.tar.gz
