#HISTORY_DB = history.db
HISTORY_DB = history.dat

SCRIPTS=\
history-ls.pl \
history-list.pl \
history-db-list.pl \
show-history.pl \
show-history-ebw3-result.pl

CC=gcc
CFLAGS=-g -Wall -DHISTORY_DB=\"$(HISTORY_DB)\"
#
# for installed db-1.85.tar.gz in /usr/local
#CPPFLAGS=-I/usr/local/include
#LDLIBS=-L/usr/local/lib -ldb
#
# for installed db-1.85.tar.gz in /opt/local
#CPPFLAGS=-I/opt/local/include
#LDLIBS=-L/opt/local/lib -ldb
#
# for installed db-1.85.tar.gz on some Linux distributions
#CPPFLAGS=-I/usr/include/db1
#LDLIBS=-ldb1
#
# for built db-1.85.tar.gz here on Solaris
CPPFLAGS=-I./db.1.85/PORT/sunos.5.2/include
LDLIBS=./db.1.85/PORT/sunos.5.2/libdb.a
#
# for built db-2.7.7.tar.gz here. I tried it but it didn't work for netscape4
#CPPFLAGS=-I./db-2.7.7/build_unix -DNO_DB185
#LDLIBS=./db-2.7.7/build_unix/libdb.a
#
# for built db-3.0.55.tar.gz here. I tried it but it didn't work for netscape4
#CPPFLAGS=-I./db-3.0.55/build_unix -DNO_DB185 -Ddbopen=__db185_open
#LDLIBS=./db-3.0.55/build_unix/libdb.a
#
# for built db-3.1.17.tar.gz here. I tried it but it didn't work for netscape4
#CPPFLAGS=-I./db-3.1.17/build_unix -DNO_DB185
#LDLIBS=./db-3.1.17/build_unix/libdb.a
#
# for built db-3.2.3b.tar.gz here. I tried it but it didn't work for netscape4
#CPPFLAGS=-I./db-3.2.3b/build_unix -DNO_DB185
#LDLIBS=./db-3.2.3b/build_unix/libdb.a

######################################################################
# at Compiling db-1.85.tar.gz on Solaris, Linux and so on,
# you had better modify db.1.85/PORT/<os>/Makefile as follows:
#
# % diff -u ./db.1.85/PORT/<os>/Makefile.org ./db.1.85/PORT/<os>/Makefile
# --- ./db.1.85/PORT/<os>/Makefile.org       Thu Jul 14 10:43:36 1994
# +++ ./db.1.85/PORT/<os>/Makefile   Wed Nov  1 22:26:31 2000
# @@ -15,8 +15,9 @@
#  
#  ${LIBDB}: ${OBJ1} ${OBJ2} ${OBJ3} ${OBJ4} ${OBJ5} ${MISC}
#         rm -f $@
# -       ar cq $@ \
# -           `lorder ${OBJ1} ${OBJ2} ${OBJ3} ${OBJ4} ${OBJ5} ${MISC} | tsort`
# +#      ar cq $@ \
# +#          `lorder ${OBJ1} ${OBJ2} ${OBJ3} ${OBJ4} ${OBJ5} ${MISC} | tsort`
# +       ar cq $@ ${OBJ1} ${OBJ2} ${OBJ3} ${OBJ4} ${OBJ5} ${MISC}
#         ranlib $@

SRCS=\
history-db-list.c \
history-db-write.c

EXES=$(SRCS:%.c=%)

all: $(EXES)

#
# Examples
#
examples: $(HISTORY_DB) htmls
HISTORY_DB_PATH = $(HOME)/.netscape/$(HISTORY_DB)

HTMLS=\
history-list.linux.html \
history-list.solaris.html \
history-list.ac.jp.html \
history-list.edu.html \
history-list.chaos.html
#private-dictionary.html \
#private-dictionary-19990320.html \
#history-list.ebw3-result.html \

htmls: $(HTMLS)

#
# As on-line usage,
# please ln -s show-history.pl show-history.cgi and use via HTTP.
#

history-list.linux.html: $(HISTORY_DB_PATH)
	show-history.pl -g '[Li]inux' -s +1 > $@

history-list.ebw3-result.html: $(HISTORY_DB_PATH)
	show-history.pl -g ebw3 -e Result -s +1 > $@

#
# As off-line usage,
# please create HTML files as follows:
#

history-list.solaris.html: history-list.all
	grep '[Ss]olaris' $< | show-history.pl - > $@

history-list.ac.jp.html: history-list.all
	grep '\.ac\.jp/' $< | show-history.pl - > $@

history-list.edu.html: history-list.all
	grep '\.edu/' $< | show-history.pl - > $@

history-list.chaos.html: history-list.all
	grep '\([Cc]haos\|カオス\)' $< | show-history.pl - > $@

history-list.all: $(HISTORY_DB_PATH)
	history-db-list -a | sort +3 -t '	' > $@

#
# To create private dictionary,
# these applications are only for ebw3 server.
#

private-dictionary.html: history-list.all
	show-history-ebw3-result.pl -a 1999/05/21 - < history-list.all > $@

private-dictionary-19990320.html: history-list.all
	show-history-ebw3-result.pl -a 1999/03/20 -t 1999/05/20 - < history-list.all > $@

#
# diet history.db, truncating untitled URLs without -T option.
#
history.db: $(HOME)/.netscape/history.db
	history-db-list -a $(HOME)/.netscape/history.db | \
	sort +3 -t '	'  > history-list.all
	history-db-write history.db < history-list.all

#
# diet history.dat, truncating untitled URLs without -T option
# and miscellaneous URLs by 'grep -v' as the following usage.
# change endian flag '-C' is necessary for history.dat on SPARC Solaris.
#
history.dat: $(HOME)/.netscape/history.dat
	history-db-list -a -C $(HOME)/.netscape/history.dat | \
	grep -v '\(400\)* *Bad Request'| \
	grep -v '\(401\)* *Authorization Required'| \
	grep -v '\(403\)* *Forbidden'| \
	grep -v '\(404\)* *[nN]ot [fF]ound' | \
	grep -v '\(500\)* *Internal Server Error'| \
	grep -v 'Error [45]0[0-9]' | \
	grep -v '\(GIF\|PNG\|JPEG\) 画像 .* \(ピクセル\|ドット\)' | \
	grep -v '^file:' | \
	sort +3 -t '	' > history-list.all
	history-db-write -C history.dat < history-list.all


clean:
	rm -f $(EXES)
	rm -f $(HTMLS) history-list.all
	rm -f history.db history.dat
	rm -f *~


distribute: history-db-0.6.tar.gz
	if [ ! -d $(HOME)/public_html/netscape/history-db ]; then mkdir $(HOME)/public_html/netscape/history-db; fi
	cp -p index-j.html show-history-ebw3-result.pl.sample.gif history-db-0.6.tar.gz $(HOME)/public_html/netscape/history-db

history-db-0.6.tar.gz: makefile $(SCRIPTS) $(SRCS) index-j.html show-history-ebw3-result.pl.sample.gif
	cd ..; tar cvzf history-db/$@ $(addprefix history-db/, makefile $(SCRIPTS) $(SRCS) index-j.html show-history-ebw3-result.pl.sample.gif)


test:
	if [ ! -d tmp ]; then mkdir tmp; fi
	history-ls.pl > tmp/history-ls.pl.out
	history-list.pl > tmp/history-list.pl.out
	# diff tmp/history-ls.pl.out tmp/history-list.pl.out
	history-list.pl -a > tmp/history-list.pl.a.out
	sort tmp/history-list.pl.a.out > tmp/history-list.pl.a.out.s
	history-db-list.pl > tmp/history-db-list.pl.out
	sort tmp/history-db-list.pl.out > tmp/history-db-list.pl.out.s
	history-db-list.pl -a > tmp/history-db-list.pl.a.out
	sort tmp/history-db-list.pl.a.out > tmp/history-db-list.pl.a.out.s
	# diff tmp/history-list.pl.a.out.s tmp/history-db-list.pl.a.out.s
	sort +1 tmp/history-db-list.pl.a.out > tmp/history-db-list.pl.a.out.s1
	history-db-list.pl -a -T > tmp/history-db-list.pl.a.T.out
	sort +1 tmp/history-db-list.pl.a.T.out > tmp/history-db-list.pl.a.T.out.s1
	# diff tmp/history-db-list.pl.a.out.s1 tmp/history-db-list.pl.a.T.out.s1
	history-db-list > tmp/history-db-list.out
	sort tmp/history-db-list.out > tmp/history-db-list.out.s
	# diff tmp/history-db-list.pl.out.s tmp/history-db-list.out.s
	history-db-list -a > tmp/history-db-list.a.out
	sort tmp/history-db-list.a.out > tmp/history-db-list.a.out.s
	# diff tmp/history-db-list.pl.a.out.s tmp/history-db-list.a.out.s
	history-db-list -a -T > tmp/history-db-list.a.T.out
	sort +1 tmp/history-db-list.a.T.out > tmp/history-db-list.a.T.out.s1
	# diff tmp/history-db-list.pl.a.T.out.s1 tmp/history-db-list.a.T.out.s1

test2:
	if [ ! -d tmp ]; then mkdir tmp; fi
	history-db-list -a -T $(HOME)/.netscape/history.db | sort > tmp/history-db-list.out
	history-db-write -C tmp/history.dat < tmp/history-db-list.out
	history-db-list -a -T -C tmp/history.dat | sort > tmp/history-dat-list.out
	# diff tmp/history-db-list.out tmp/history-dat-list.out
	history-db-list -a -T -C $(HOME)/.netscape/history.dat | sort > tmp/history-dat-list.out.a
	history-db-write -C -A tmp/history.dat < tmp/history-dat-list.out.a
	history-db-list -a -T -C tmp/history.dat | sort > tmp/history-dat-list.out.A
	# diff tmp/history-db-list.out tmp/history-dat-list.out.A

clean.test:
	rm -rf tmp
