#====//====//====//====//====//====//====//====//====//====//
#
#    Project: YATE (Yet Another Text Editor)
#    File: “Makefile”
#
#    Copyright © 1996-1998 Chris Blackbourn
#
#====//====//====//====//====//====//====//====//====//====//
#xres -o yate.r.out -a TOKE:1:yate_tokens .txt tokens.txt


# User definible options:  (uncomment for debug, comment for release)
#DEBUG           =  TRUE

#oops, that's all there is.. sorry :-)


ifneq ($(DEBUG),TRUE)
	DEBUG  = FALSE
endif
ifeq ($(BE_HOST_CPU),ppc)
	PLATFORM       = PPC
else
	PLATFORM        = X86
endif

APPNAME         = yate
AUTODEPEND      = TRUE

## 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
	X86_EXTRAS         =
	PPC_EXTRAS         =
	X86_LD          = cc -lbe -lroot -ltracker
	PPC_LD          = cc
else
	OBJDRAW     = _ReleaseObj
	X86_EXTRAS    = -O2 -D RELBUILD
	PPC_EXTRAS    = -O2 -d RELBUILD
	X86_LD          = cc -lbe -lroot -ltracker
	PPC_LD          = mwld -map $(APPNAME).xMAP
endif

X86_OPTIONS        =  -W
PPC_OPTIONS           =


LD                     =$($(PLATFORM)_LD)
OPTIONS           = $($(PLATFORM)_OPTIONS)
EXTRAS             = $($(PLATFORM)_EXTRAS)

INCLUDES        = -I/boot/develop/headers
CC              = cc
SHELL           = /bin/sh
CFLAGS          = $(OPTIONS) $(EXTRAS) $(INCLUDES)

PPC_HEADERS   =$(CC) $(CFLAGS) -make
X86_HEADERS   =$(CC) $(CFLAGS) -MM
HEADERS = $($(PLATFORM)_HEADERS)


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/$(APPNAME).rsrc
	rm -f $(APPNAME).xSYM
	rm -f $(APPNAME).xMAP
	rm -f $(OBJDRAW)/*.o
	rm -f $(OBJDRAW)/*.d
	rm -f yak
	rm -f yak.o
	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/$(APPNAME).rsrc
	@echo Linking...
	@$(LD) -o $(APPNAME) $(OBJS)
	@echo ...'xres' and 'mimeset'...
	@xres -o $(APPNAME) $(APPNAME)rsrc/$(APPNAME).rsrc
	@mimeset $(APPNAME)
#   @copyres $(APPNAME).rsrc $(APPNAME)
#   @rmattr BEOS:TYPE $(APPNAME)
#    @echo $(APPNAME) executable created successfully

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

yak: yak.cpp
	@echo "ReBuilding YAK (Yet Another Keyboard)"
	@$(CC) $(CFLAGS) -c yak.cpp -o yak.o
	@$(LD) -o yak yak.o
	@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).res/$(APPNAME).r.out
$(APPNAME)rsrc/$(APPNAME).rsrc:
	@rm -f yate.rsrc
	@xres -o yate.rsrc yatersrc/yatedef.rsrc
	@xres -o yate.rsrc yate.rsrc -a TOKE:1:tokens.txt yatersrc/tokens.txt
	@xres -o yate.rsrc yate.rsrc -a TOKE:2:html.txt yatersrc/html.txt
	@xres -o yate.rsrc yate.rsrc -a ICON:101:BEOS:L:STD_ICON yatersrc/IconLarge
	@xres -o yate.rsrc yate.rsrc -a MICN:101:BEOS:M:STD_ICON yatersrc/IconMedium
	@mv yate.rsrc yatersrc/yate.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) $(CFLAGS) -make $(basename $(notdir $@)).cpp > /tmp/YateMakeFileBuilder
		@$(HEADERS) $(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))
