#====//====//====//====//====//====//====//====//====//====//
#
#    Project: YATE (Yet Another Text Editor)
#    File: “Makefile”
#
#    Copyright © 1996-1997 Acid Software, All Rights Reserved.
#
#====//====//====//====//====//====//====//====//====//====//

APPNAME         = yate

AUTODEPEND      = TRUE
DEBUG           = FALSE

## Files
SOURCES =   cb_list.cpp     \
			Hash.cpp Schemes.cpp     \
			SMap.cpp        \
			BMapView.cpp    \
			BSBar.cpp       \
			YateWindow.cpp  \
			NTree.cpp UBuffer.cpp  BufList.cpp  \
			Yate.cpp About.cpp   \
			ViewHex.cpp ViewText.cpp ViewHTML.cpp ViewBase.cpp\
			FFont.cpp

ifeq ($(DEBUG),TRUE)
	OBJDRAW     = _DebugObj
	EXTRAS      = -O3 -sym full -d DEBBUILD
	LD          = mwld -sym full -map $(APPNAME).xMAP
else
	OBJDRAW     = _ReleaseObj
	EXTRAS      = -w9 -O7 -maxerrors 3 -d RELBUILD
	LD          = mwld -map $(APPNAME).xMAP
endif

OPTIONS         = -w9 -maxerrors 3
INCLUDES        = -I/boot/develop/headers
CC              = mwcc
SHELL           = /bin/sh
CFLAGS          = $(OPTIONS) $(EXTRAS) $(INCLUDES)
ifeq ($OBJDRAW,)
	OBJDRAW     = .
endif

OBJS=$(addprefix $(OBJDRAW)/,$(SOURCES:.cpp=.o))

.PHONY: counter more checkfolder


## top level calls...
all:  $(OBJDRAW) compile more

run: all
	$(APPNAME)

debug: all
	/boot/apps/Metrowerks/debugger/MWDebug-Be $(APPNAME)  &

clean:
	@echo Removing all Objects and Binaries
	rm -f $(APPNAME).rsrc
	rm -f $(APPNAME).xSYM
	rm -f $(APPNAME).xMAP
	rm -f $(OBJDRAW)/*.o
	rm -f $(OBJDRAW)/*.d
	rm -f yak
	rm -f $(APPNAME)
	@echo All clean, type 'make all' to rebuild


## A place holder for the counter
compile: counter $(APPNAME)
	-@echo $(APPNAME), built ` cat $(APPNAME).count ` times
	-@awk "BEGIN{ srand();if (rand()<0.01){print \"Have A Nice Day\"} }"

$(APPNAME): $(OBJS) $(APPNAME).rsrc
	@echo Linking...
	@$(LD) -o $(APPNAME) $(OBJS)
	@echo ...'copyres' and 'mimeset'...
	@copyres $(APPNAME).rsrc $(APPNAME)
	@rmattr BEOS:TYPE $(APPNAME)
	@mimeset $(APPNAME)
#    @cp yate yate2
#    @echo $(APPNAME) executable created successfully

ifeq ($(APPNAME),yate)
more: yak

yak: yak.cpp
	@echo "ReBuilding YAK (Yet Another Keyboard)"
	@$(CC) $(CFLAGS) yak.cpp
	@echo "Yak ReBuilt successfully"
else
more:

endif

ifeq ($APPNAME),skid)
lscripty.h : lscripty.cpp.h
	cmp -s lscripty.cpp.h lscripty.h || cp lscripty.cpp.h lscripty.h

lscripty.cpp lscripty.cpp.h : lscript.y
	bison -v -o lscripty.cpp -d lscript.y

lscriptl.cpp : lscript.l lscripty.h
	flex -olscriptl.cpp lscript.l
endif


## the "?" should be a Semicolon
$(OBJDRAW)/%.o : %.cpp
	@echo Compiling $<...
	@$(CC) $(CFLAGS) -o $@ -c $<

yate.res/yate.r.out : yate.res/TOKE.1.YATE?tokens.txt yate.res/TOKE.2.YATE?html.txt
	cd yate.res;mwbres yate.r

#rebuild those resources..
$(APPNAME).rsrc:  $(APPNAME).res/$(APPNAME).r.out
	cp $(APPNAME).res/$(APPNAME).r.out $(APPNAME).rsrc

checkfolder: $(OBJDRAW)/

#create a new folder
$(OBJDRAW)/:
	mkdir "$(OBJDRAW)"

counter : $(APPNAME).count
	@awk 'BEGIN{print ARGV[1] +1}' `cat $(APPNAME).count` > $(APPNAME).count
#    awk '{print $$1+1}' < $(APPNAME).count > $(APPNAME).count
#    expr ` cat $(APPNAME).cnt` \+ 1 > $(APPNAME).cnt

$(APPNAME).count:
	@echo 0 > $(APPNAME).count


ifeq ($(AUTODEPEND),TRUE)
$(OBJDRAW)/%.d: %.cpp
#        @echo Rebuilding dependancies for $<
		@echo Rebuilding dependancies for $(basename $(notdir $@)).cpp
		@$(CC) $(CGLAGS) -make $(basename $(notdir $@)).cpp > /tmp/YateMakeFileBuilder
		@echo -e -n "\n$(OBJDRAW)/" | cat - /tmp/YateMakeFileBuilder >$@
#        $(SHELL) -ec '$(CC) -make $< \  #for gcc they tell me..
#                    | sed '\''s/\($*\)\.o[ :]*/\1 $@/g'\'' > $(OBJDRAW)/$@'
else
$(OBJDRAW)/%.d:
endif


-include $(addprefix $(OBJDRAW)/,$(SOURCES:.cpp=.d))
