SRCS=main.c file.c ipc.c buffer.c display.c etc.c search.c linein.c table.c local.c \
	form.c map.c frame.c rc.c menu.c mailcap.c\
	func.c cookie.c history.c backend.c $(KEYBIND_SRC)
OBJS=main.o file.o ipc.o buffer.o display.o etc.o search.o linein.o table.o local.o\
	form.o map.o frame.o rc.o menu.o mailcap.o\
	func.o cookie.o history.o backend.o $(KEYBIND_OBJ)
LSRCS=terms.c conv.c url.c ftp.c anchor.c mimehead.c parsetagx.c\
	istream.c
LOBJS=terms.o conv.o url.o ftp.o anchor.o mimehead.o parsetagx.o\
	istream.o
LLOBJS=version.o
ALIBOBJS=Str.o indep.o $(ALIEN_OBJS) regex.o textlist.o parsetag.o myctype.o entity.o conv.o
ALIB=libindep.a
ALLOBJS=$(OBJS) $(LOBJS) $(LLOBJS)

TARGET=w3m$(SUFFIX)$(EXT)
BOOKMARKER=w3mbookmark$(EXT)
HELPER=w3mhelperpanel$(EXT)
HELP_ALLFILES=w3mhelp-w3m_en.html w3mhelp-w3m_ja.html \
	w3mhelp-lynx_en.html w3mhelp-lynx_ja.html
IMGDISPLAY=w3mimgdisplay$(EXT)
MAN1_TARGET=w3m.1
MAN1=doc/w3m.1
MAN1_JA=doc-jp/w3m.1
TARGETS=$(TARGET) $(LIB_TARGETS) $(AUXBIN_TARGETS)
HELP_TARGET=w3mhelp.html
TAGGED=main.c rc.c rc.h

SCRIPTSUBDIRS = scripts scripts/multipart scripts/w3mman
SUBDIRS = $(SCRIPTSUBDIRS)

INCLUDES=-I.

WARNFLAGS=
DEFS=$(INCLUDES) # -DDEBUG
CFLAGS=$(MYCFLAGS) $(WARNFLAGS) $(DEFS) 
LIBS=-L. -lindep $(LOCAL_LIBRARIES) $(GCLIB) $(MATHLIB) $(SYS_LIBRARIES)
LIBS2=-L. -lindep $(LOCAL_LIBRARIES) $(GCLIB)
INSTALL=sh install-sh
INSTALL2=sh ../install-sh
INSTALL_PROGRAM=$(INSTALL) -m 755
INSTALL_SCRIPT=$(INSTALL) -m 755
INSTALL_DATA=$(INSTALL) -m 644
AR=ar

HOMEPAGE=http://pub.ks-and-ks.ne.jp/prog/w3mmee/
HOMEPAGE_JA=$(HOMEPAGE)index.shtml.ja
CONFPAGE_JA=$(HOMEPAGE)config.shtml.ja
HOMEPAGE_EN=$(HOMEPAGE)index.shtml.en
CONFPAGE_EN=$(HOMEPAGE)config.shtml.en

all: $(TARGETS) all-scripts
	if [ -n "$(LOCALE_DIR)" -a -d po/. ]; then \
		cd po ; \
		for po in *.po; do \
			msgfmt -o "`basename $$po .po`.mo" $$po; \
		done ; \
	fi

$(TARGET): $(ALLOBJS) $(ALIB) $(GCTARGET)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $(TARGET) $(ALLOBJS) $(LIBS)

$(ALIB): $(ALIBOBJS)
	$(AR) rv $(ALIB) $(ALIBOBJS)
	$(RANLIB) $(ALIB)

$(OBJS): fm.h funcname1.h

parsetagx.o: html.c tagtable.h tagattrtab.h
url.o: schemetab.h defaultguess.h
rc.o: rc_name.h http_auto_header.h http_version.h
file.o: content_encoding.h ce_tab.h media_tab.h headertab.h headerv.h
func.o: funcname.c funcname.h
keybind.o: funcname2.h
keybind_lynx.o: funcname2.h
indep.o: entity.h entity_tab.h

funcname.c: funcname.tab funcname0.awk
	awk -f funcname0.awk funcname.tab > funcname.c

funcname1.h: funcname.tab funcname1.awk
	awk -f funcname1.awk funcname.tab > funcname1.h

funcname2.h: funcname.tab funcname2.awk
	awk -f funcname2.awk funcname.tab > funcname2.h

funcname.mk_btri.h: funcname1.h funcname.tab funcname3.awk
	awk -f funcname3.awk funcname.tab > funcname.mk_btri.h

tagtable.mk_btri.h: tagtable.tab tagtable.awk
	awk -f tagtable.awk tagtable.tab > tagtable.mk_btri.h

$(BOOKMARKER): w3mbookmark.o $(ALIB) $(GCTARGET)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $(BOOKMARKER) w3mbookmark.o  $(LIBS2)

$(HELPER): w3mhelperpanel.o $(ALIB) $(GCTARGET)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $(HELPER) w3mhelperpanel.o  $(LIBS2)

$(IMGDISPLAY): w3mimgdisplay.o $(IMGOBJS)
	$(CC) $(CFLAGS) $(IMGCFLAGS) $(LDFLAGS) -o $(IMGDISPLAY) \
		w3mimgdisplay.o $(IMGOBJS) $(IMGLDFLAGS)

w3mimgdisplay.o: w3mimgdisplay.c w3mimgdisplay.h
	$(CC) $(CFLAGS) $(IMGCFLAGS) -c w3mimgdisplay.c

w3mimg/w3mimg.o: w3mimg/w3mimg.c
	cd w3mimg && $(CC) $(CFLAGS) $(IMGCFLAGS) -I.. -c w3mimg.c

w3mimg/x11/x11_w3mimg.o: w3mimg/x11/x11_w3mimg.c
	cd w3mimg/x11 && $(CC) $(CFLAGS) $(IMGCFLAGS) -I../.. -c x11_w3mimg.c

#w3mimg/fb/fb_w3mimg.o: w3mimg/fb/fb_w3mimg.c
#	cd w3mimg/fb && $(CC) $(CFLAGS) $(IMGCFLAGS) -I../.. -c fb_w3mimg.c
#
#w3mimg/fb/fb.o: w3mimg/fb/fb.c
#	cd w3mimg/fb && $(CC) $(CFLAGS) $(IMGCFLAGS) -I../.. -c fb.c
#
#w3mimg/fb/fb_img.o: w3mimg/fb/fb_img.c w3mimg/fb/fb_gdkpixbuf.c w3mimg/fb/fb_imlib2.c
#	cd w3mimg/fb && $(CC) $(CFLAGS) $(IMGCFLAGS) -I../.. -c fb_img.c

gc/gc.a:
	cd gc; $(MAKE) CC='$(CC)' CFLAGS='$(GCCFLAGS) $(GC_CFLAGS)' SPECIALCFLAGS='-I./\$(srcdir)/include $(GC_CFLAGS)'

mk_btri: btri.c btri.h altmalloc.o
	$(CC) -I. $(CFLAGS) -DMAIN -o mk_btri btri.c altmalloc.o

$(LANG_POs): $(TAGGED)
	echo xgettext --keyword=_ --keyword=N_ --default-domain="`echo $@ | sed 's/.po$$/'`" $(TAGGED)
	touch $@

install: install-core install-scripts

install-core: $(TARGETS)
	-$(MKDIR) $(DESTDIR)$(BIN_DIR)
	-$(MKDIR) $(DESTDIR)$(AUXBIN_DIR)
	-$(MKDIR) $(DESTDIR)$(ETC_DIR)
	-$(MKDIR) $(DESTDIR)$(LIB_DIR)
	-$(MKDIR) $(DESTDIR)$(HELP_DIR)
	-$(MKDIR) $(DESTDIR)$(MAN1_DIR)
	-$(MKDIR) $(DESTDIR)$(MAN1_JA_DIR)
	$(INSTALL_PROGRAM) $(TARGET) $(DESTDIR)$(BIN_DIR)/$(TARGET)
	$(INSTALL_DATA) $(HELP_FILE) $(DESTDIR)$(HELP_DIR)/$(HELP_TARGET)
	$(INSTALL_DATA) $(MAN1) $(DESTDIR)$(MAN1_DIR)/$(MAN1_TARGET)
	$(INSTALL_DATA) $(MAN1_JA) $(DESTDIR)$(MAN1_JA_DIR)/$(MAN1_TARGET)
	for d in $(AUXBIN_TARGETS); do \
		case $$d in \
		$(IMGDISPLAY)) $(INSTALL_W3MIMGDISPLAY) $$d $(DESTDIR)$(AUXBIN_DIR)/$$d;; \
		*) $(INSTALL_PROGRAM) $$d $(DESTDIR)$(AUXBIN_DIR)/$$d;; \
		esac; \
	done
	for d in $(LIB_TARGETS); do $(INSTALL_PROGRAM) $$d $(DESTDIR)$(LIB_DIR)/$$d; done
	for f in w3mconfig* w3mmenu* browsecap locale2mime; do \
		if [ -r $$f ]; then $(INSTALL_DATA) $$f $(DESTDIR)$(ETC_DIR)/$$f; else :; fi; \
	done
	echo "$(ROMAJI_FILTER)" | sed -ne '/.*migemo.*/s//process_charset {migemo}euc-jp/p' >> $(DESTDIR)$(ETC_DIR)/w3mconfig
	for x in cn tw kr jp; do \
		if [ \! -r $(DESTDIR)$(ETC_DIR)/w3mconfig_$$x.euc -a \
			-r $(DESTDIR)$(ETC_DIR)/w3mconfig.euc$$x ]; then \
			ln -sv w3mconfig.euc$$x $(DESTDIR)$(ETC_DIR)/w3mconfig_$$x.euc; \
		else \
			:; \
		fi; \
	done
	for f in w3mmessages*; do \
		if [ -r $$f ]; then $(INSTALL_DATA) $$f $(DESTDIR)$(HELP_DIR)/$$f; else :; fi; \
	done
	$(INSTALL_DATA) doc/README.mee.config.html $(DESTDIR)$(HELP_DIR)/config.shtml.en
	$(INSTALL_DATA) doc-jp/README.mee.config.html $(DESTDIR)$(HELP_DIR)/config.shtml.ja
	if [ -n "$(LOCALE_DIR)" -a -d po/. ]; then \
		( cd po ; for mo in *.mo; do \
		lang=`basename $$mo .mo`; \
		$(MKDIR) $(DESTDIR)$(LOCALE_DIR) ; \
		$(MKDIR) $(DESTDIR)$(LOCALE_DIR)/$$lang; \
		$(MKDIR) $(DESTDIR)$(LOCALE_DIR)/$$lang/LC_MESSAGES; \
		$(INSTALL2) -m 644 $$mo $(DESTDIR)$(LOCALE_DIR)/$$lang/LC_MESSAGES/w3m$(SUFFIX).mo ; \
		done ) ; \
	fi

install-helpfile:
	-$(MKDIR) $(DESTDIR)$(HELP_DIR)
	for file in $(HELP_ALLFILES); \
	do \
		$(INSTALL_DATA) $$file $(DESTDIR)$(HELP_DIR)/$$file; \
	done

all-scripts:
	for dir in $(SCRIPTSUBDIRS);	\
	do	\
		(cd $$dir; \
		$(MAKE) PERL='$(PERL)' BIN_DIR='$(BIN_DIR)' AUXBIN_DIR='$(AUXBIN_DIR)' LIB_DIR='$(LIB_DIR)' HELP_DIR='$(HELP_DIR)' \
			KEYBIND_SRC='$(KEYBIND_SRC)' RC_DIR='$(RC_DIR)' SUFFIX='$(SUFFIX)');	\
	done

install-scripts: all-scripts
	topdir=`pwd`; \
	for dir in $(SCRIPTSUBDIRS);	\
	do	\
		(cd $$dir; \
		$(MAKE) PERL='$(PERL)' BIN_DIR='$(BIN_DIR)' AUXBIN_DIR='$(AUXBIN_DIR)' LIB_DIR='$(LIB_DIR)' HELP_DIR='$(HELP_DIR)' \
			MAN_DIR='$(MAN_DIR)' KEYBIND_SRC='$(KEYBIND_SRC)' SUFFIX='$(SUFFIX)' DESTDIR='$(DESTDIR)' \
			INSTALL="sh $$topdir/install-sh" install);	\
	done

uninstall:
	-rm -f $(BIN_DIR)/$(TARGET)
	-for file in $(AUXBIN_TARGETS); \
	do \
		rm -f $(AUXBIN_DIR)/$$file; \
	done
	-for file in $(LIB_TARGETS); \
	do \
		rm -f $(LIB_DIR)/$$file; \
	done
	-for file in $(HELP_ALLFILES); \
	do \
		rm -f $(HELP_DIR)/$$file; \
	done
	-rm -f $(HELP_DIR)/$(HELP_TARGET)
	-rm -f $(MAN1_DIR)/$(MAN1_TARGET)
	-rm -f $(MAN1_JA_DIR)/$(MAN1_TARGET)
	-for dir in $(SCRIPTSUBDIRS);	\
	do	\
		(cd $$dir; \
		$(MAKE) BIN_DIR='$(BIN_DIR)' LIB_DIR='$(LIB_DIR)' \
			HELP_DIR='$(HELP_DIR)' MAN_DIR='$(MAN_DIR)' SUFFIX='$(SUFFIX)' uninstall); \
	done

#clean: sweep # Booooooy
clean:
	-for f in *.mk_btri.h; do rm -f `expr $$f : '\(.*\)\.mk_btri\.h$$'`.h; done
	-rm -f *.o *.a $(TARGETS) mk_btri
	-rm -f funcname.mk_btri.h tagtable.mk_btri.h
	-rm -f funcname.c funcname1.h funcname2.h tagtable.c
	-rm -f w3mimg/*.o w3mimg/*/*.o 
	-for dir in $(SCRIPTSUBDIRS);	\
	do	\
		(cd $$dir; $(MAKE) SUFFIX='$(SUFFIX)' clean); \
	done

sweep:
	-rm -f core *~ *.bak *.orig *.rej

depend: 
	makedepend $(CFLAGS) *.c

dist:
	cd ..; mv w3m w3m-$(VERSION); tar cvfz w3m-$(VERSION).tar.gz w3m-$(VERSION); mv w3m-$(VERSION) w3m

bindist:
	cd ..; mv w3m w3m-$(VERSION); tar cvfz w3m-$(VERSION)-$(MODEL).tar.gz w3m-$(VERSION)/{w3m*,doc*,Bonus*,README,scripts}; mv w3m-$(VERSION) w3m

plaindocs: doc/README.mee doc/README.mee.config doc-jp/README.mee doc-jp/README.mee.config

doc/README.mee: doc/README.mee.html doc/CreatorOfMee.html all
	W3MLANG=en_US.ASCII ; export W3MLANG ; \
	./w3m$(SUFFIX) -o link_num_url=$(HOMEPAGE_EN) -o anchor_num_style='[*%d]' doc/README.mee.html > $@ ; \
	./w3m$(SUFFIX) doc/CreatorOfMee.html >> $@

doc/README.mee.config: doc/README.mee.config.html doc/CreatorOfMee.html all
	W3MLANG=en_US.ASCII ; export W3MLANG ; \
	./w3m$(SUFFIX) -o link_num_url=$(CONFPAGE_EN) -o anchor_num_style='[*%d]' doc/README.mee.config.html > $@ ; \
	./w3m$(SUFFIX) doc/CreatorOfMee.html >> $@

doc-jp/README.mee: doc-jp/README.mee.html doc-jp/CreatorOfMee.html all
	W3MLANG=ja_JP.eucJP ; export W3MLANG ; \
	./w3m$(SUFFIX) -o link_num_url=$(HOMEPAGE_JA) -o anchor_num_style='[*%d]' doc-jp/README.mee.html > $@ ; \
	./w3m$(SUFFIX) doc-jp/CreatorOfMee.html >> $@

doc-jp/README.mee.config: doc-jp/README.mee.config.html doc-jp/CreatorOfMee.html all
	W3MLANG=ja_JP.eucJP ; export W3MLANG ; \
	./w3m$(SUFFIX) -o link_num_url=$(HOMEPAGE_JA) -o anchor_num_style='[*%d]' doc-jp/README.mee.config.html > $@ ; \
	./w3m$(SUFFIX) doc-jp/CreatorOfMee.html >> $@

# DO NOT DELETE
