##
## If you have gcc, you should just be able to
##	make
##
## If you know the gcc is version 2, you can get better performance with
##	make gcc=gcc2
##
## If you don't have gcc, specify "gcc=" and tell what CC to use:
##	make gcc= CC=/bin/cc 
##
## You can add
##	warn=1
## to get compiler warnings. There are others as well... see below.
##
CC=gcc
CC_TRAD=$(CC) -traditional

## set to "gcc1", "gcc2" or leave blank
gcc=gcc1

## selecte exactly "0" (no) or "1" (yes) for the following
optimize=0
pedantic=0
kibishii=0
debug=1
warn=0
md=0

#MANPAGE_WIDTH=-r179

##
## If you know your include files (sys/ioctl.h specifically) are ANSIfied,
## you don't need to worry about COMPILE_WITH_TRAD, so you can comment
## it out if that's the case and you don't have a traditional compiler
## around.
##
COMPILE_WITH_TRAD=termset_trad.o

RANLIB=/usr/bin/ranlib

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

##
## in these defines, _* are for when GCC is not used.
##                   gcc1_* for when gcc version 1 used.
##                   gcc2_* for when gcc version 2 used.
##

KIBISHII_1      = -DKIBISHII_DEBUG

gcc1_WARN_1     = -Wall -Wshadow -Wwrite-strings
gcc2_WARN_1     = -Wall -Wshadow -Wwrite-strings -Wno-implicit

## For generic non-GNU compilers. First digit is optimize, 2nd is debug
_OPTIMIZE_1_0     = -O -DFAST_REGEXEC -DNDEBUG
_OPTIMIZE_1_1     = -O -DFAST_REGEXEC
_OPTIMIZE_0_0     = -DFAST_REGEXEC -DNDEBUG
_OPTIMIZE_0_1     = -g

## For GNU Version 1.x compilers. First digit is optimize, 2nd is debug
gcc1_OPTIMIZE_1_0 = -O -g -DFAST_REGEXEC -DNDEBUG
gcc1_OPTIMIZE_1_1 = -O -g -DFAST_REGEXEC
gcc1_OPTIMIZE_0_0 = -DFAST_REGEXEC -DNDEBUG
gcc1_OPTIMIZE_0_1 = -g

## For GNU Version 2.x compilers. First digit is optimize, 2nd is debug
gcc2_OPTIMIZE_1_0 = -O2 -funroll-loops -g -DFAST_REGEXEC -DNDEBUG
gcc2_OPTIMIZE_1_1 = -O2 -g -DFAST_REGEXEC
gcc2_OPTIMIZE_0_0 = -DFAST_REGEXEC -DNDEBUG
gcc2_OPTIMIZE_0_1 = -g

gcc1_PEDANTIC_1   = -pedantic
gcc2_PEDANTIC_1   = -pedantic

gcc2_MD_1         =-MMD

## now use the above

PEDANTIC = $($(gcc)_PEDANTIC_$(pedantic))
WARNINGS = $($(gcc)_WARN_$(warn))
OPTIMIZE = $($(gcc)_OPTIMIZE_$(optimize)_$(debug))
KIBISHII = $(KIBISHII_$(kibishii))
MD       = $($(gcc)_MD_$(md))

OPTIONS=$(PEDANTIC) $(WARNINGS) $(OPTIMIZE) $(MD) $(KIBISHII)

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

SHELL=/bin/sh

LOCAL_LIB_SRC=lib
LOCAL_LIB=jefflib.a

#OTHERLIBS=-lmach
LIBS= $(LOCAL_LIB) $(OTHERLIBS)

CFLAGS= $(OPTIONS) $(EXTRA) $(CFLAGS_EXTRA)

objs=lookup.o commands.o apply_regex.o eval.o

#all: _note_ lookup manual
all: lookup


LocalLibObs =       \
    virtfile.o      \
    euc.o           \
    fuzzkana.o      \
    index.o         \
    input.o         \
    jreadline.o     \
    jregex.o        \
    kanaid.o        \
    loadfile.o      \
    output.o        \
    replace.o       \
    romaji2kana.o   \
    std_romaji.o    \
    strsave.o       \
    termset.o       \
    xmalloc.o

$(LOCAL_LIB): $(LocalLibObs) $(COMPILE_WITH_TRAD)
	ar r $@ $?
	-if [ -f $(RANLIB) ]; then ranlib $@; else true; fi

$(COMPILE_WITH_TRAD):
	$(CC_TRAD) -c -o $@ $(CFLAGS) -I$(LOCAL_LIB_SRC)/ $(LOCAL_LIB_SRC)/$*.c

$(LocalLibObs):
	$(CC) -c -o $@ $(CFLAGS) -I$(LOCAL_LIB_SRC)/ $(LOCAL_LIB_SRC)/$*.c

_note_:
	-@echo ' '
	-@echo 'NOTE: "may be used uninitialized" warnings are OK in compile.'
	-@echo ' '

lookup: $(LOCAL_LIB) $(objs) version.c
	## Some systems (DGUX) have SH bugs that cause the link to die.
	## Can be fixed by wrapping in: csh -c "...."
	-@/bin/rm -f lookup.new
	$(CC) $(CFLAGS) -o lookup.new $(objs) version.c $(LIBS)
	/bin/mv -f lookup.new lookup

regextest: $(LOCAL_LIB) regextest.o
	$(CC) -o $@ regextest.o $(LOCAL_LIB)

commands.h: cmds.master
	perl -e 1 && perl cmds.master > temp.h && /bin/mv -f temp.h commands.h

lib/system.h: Makefile
	/bin/rm -f lib/system.h
	echo '/* this file generated by Makefile */'      > tmp;
	-echo '#ifndef __SYSTEM_H__ /*file wrapper*/'    >> tmp;
	-echo '#define __SYSTEM_H__'                     >> tmp;
	if [ -f /usr/include/strings.h ]; then\
		echo '#define _HAVE_STRINGS_H_'          >> tmp; \
	else true; fi
	if [ -f /usr/include/sys/termio.h ]; then\
		echo '#define _HAVE_SYS_TERMIO_H_'       >> tmp; \
	else true; fi
	if [ -f /usr/include/sys/stdtypes.h ]; then\
		echo '#define _HAVE_SYS_STDTYPES_H_'     >> tmp; \
	else true; fi
	if [ -f /usr/include/sys/fcntl.h ]; then\
		echo '#define _HAVE_SYS_FCNTL_H_'        >> tmp; \
	else true; fi
	if [ -f /usr/include/fcntl.h ]; then\
		echo '#define _HAVE_FCNTL_H_'            >> tmp; \
	else true; fi
	-echo '#endif /* file wrapper */'                >> tmp;
	mv tmp lib/system.h

make.sh: realclean
	@echo ':# script to make lookup'                               > tmp
	@echo '## Can set CC= and CFLAGS= on the command line, just as with make' >> tmp
	@echo 'CC="gcc"'                                             >> tmp
	@echo 'CFLAGS="-O -DFAST_REGEXEC -DNDEBUG"'                  >> tmp
	@echo '#####################################################'>> tmp
	@echo 'set -ex'                                              >> tmp
	@echo 'eval $$@'                                             >> tmp
	$(MAKE) --silent -n gcc= CC='[ -s $$@ ]||$$$$CC' CFLAGS='$$$$CFLAGS' >> tmp
	/bin/mv tmp make.sh
	chmod +x make.sh

##
## We use man.tmp so that lookup.man won't be erased if jnroff or jconv
## fails (such as if it's not even found).
##
manual: lookup.man.euc lookup.man.jis lookup.man.sjs

lookup.man.euc: doc/lookup.man doc/*.so
	@echo
	@echo "NOTE : if jnroff isn't found, manuals won't be remade."
	@echo
	cd doc; jnroff $(MANPAGE_WIDTH) -man lookup.man > man.tmp
	mv doc/man.tmp lookup.man.euc

lookup.man.jis: lookup.man.euc
	jconv -ie -oj < lookup.man.euc > man.tmp
	mv man.tmp lookup.man.jis

lookup.man.sjs: lookup.man.euc
	jconv -ie -os < lookup.man.euc > man.tmp
	mv man.tmp lookup.man.sjs

## The following is JIS, but without any highlighting.
lookup.man.raw: lookup.man.jis
	sed -e 's/.//g' < lookup.man.jis > man.tmp
	mv man.tmp lookup.man.raw

tidy:
	-@echo dummy > dummy~
	/bin/rm -f .*~ *~ */*~ core a.out man.tmp doc/*.bak

clean: tidy
	-@echo dummy > dummy.o
	/bin/rm -f \#* *.o *.d doc/#* $(LOCAL_LIB) lib/system.h

realclean: clean
	-@echo dummy > lookup.man.xxx
	/bin/rm -f lookup lookup.man.* commands.h make.sh

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

apply_regex.o: apply_regex.c lib/config.h lib/output.h lib/assert.h \
 lib/index.h lib/system.h lib/packed_list.h lib/MemItem.h \
 lib/xmalloc.h lib/virtfile.h lib/loadfile.h lib/longlinenote.h \
 lib/jregex.h lib/replace.h lookup.h
commands.o: commands.c lib/system.h lib/config.h lib/assert.h \
 lib/jreadline.h lib/jregex.h lib/loadfile.h lib/longlinenote.h \
 lib/xmalloc.h lib/index.h lib/packed_list.h lib/MemItem.h \
 lib/output.h lib/virtfile.h lib/replace.h lib/strsave.h lib/euc.h \
 lookup.h eval.h commands.h
euc.o: lib/euc.c lib/config.h lib/euc.h
eval.o: eval.c lib/config.h lib/jregex.h lib/output.h lookup.h \
 lib/virtfile.h lib/assert.h eval.h
fuzzkana.o: lib/fuzzkana.c lib/config.h lib/assert.h lib/output.h \
 lib/fuzzkana.h lib/kanaid.h
index.o: lib/index.c lib/config.h lib/system.h lib/assert.h \
 lib/output.h lib/index.h lib/packed_list.h lib/MemItem.h \
 lib/xmalloc.h lib/virtfile.h
input.o: lib/input.c lib/config.h
jreadline.o: lib/jreadline.c lib/config.h lib/assert.h lib/output.h \
 lib/jreadline.h lib/strsave.h lib/input.h lib/xmalloc.h lib/euc.h \
 lib/system.h
jregex.o: lib/jregex.c lib/config.h lib/assert.h lib/jregex.h \
 lib/output.h lib/euc.h
kanaid.o: lib/kanaid.c lib/config.h lib/kanaid.h
loadfile.o: lib/loadfile.c lib/config.h lib/assert.h lib/xmalloc.h \
 lib/strsave.h lib/system.h lib/loadfile.h lib/longlinenote.h \
 lib/index.h lib/packed_list.h lib/MemItem.h lib/output.h \
 lib/virtfile.h
lookup.o: lookup.c lib/config.h lib/assert.h lib/output.h \
 lib/fuzzkana.h lib/loadfile.h lib/longlinenote.h lib/xmalloc.h \
 lib/index.h lib/system.h lib/packed_list.h lib/MemItem.h \
 lib/virtfile.h lib/romaji2kana.h lib/jregex.h lib/strsave.h \
 lib/replace.h lib/input.h lookup.h lib/jreadline.h
output.o: lib/output.c lib/config.h lib/assert.h lib/input.h \
 lib/output.h
replace.o: lib/replace.c lib/config.h lib/assert.h lib/jregex.h \
 lib/xmalloc.h lib/replace.h
romaji2kana.o: lib/romaji2kana.c lib/config.h lib/assert.h \
 lib/romaji2kana.h lib/kanaid.h
std_romaji.o: lib/std_romaji.c lib/config.h lib/system.h \
 lib/romaji2kana.h lib/jreadline.h lib/strsave.h lib/xmalloc.h
strsave.o: lib/strsave.c lib/xmalloc.h lib/strsave.h
termset.o: lib/termset.c lib/config.h lib/system.h
termset_trad.o: lib/termset_trad.c lib/termset.c lib/config.h \
 lib/system.h
version.o: version.c
virtfile.o: lib/virtfile.c lib/config.h lib/output.h lib/xmalloc.h \
 lib/strsave.h lib/virtfile.h
xmalloc.o: lib/xmalloc.c lib/xmalloc.h
