FILE0 =	COPYRIGHT Makefile \
	make-init.sh \
	README INSTALL README.5-6 README.7-8 README.MAKE CREDITS \
	IPv6NOTE.txt \
	DG9note.html \
	id.shtml \
	gen/README \
	CHANGES lib/README mkmkmk.c

DGDATE =`cd src;       $(MAKE) -s dgdate`
VER =	`cd src;       $(MAKE) -s ver`
DVER =	`cd src;       $(MAKE) -s ver | sed 's/\./_/g'`
SRC =	`cd src;       $(MAKE) -s files | sed "s:^:src/:"`
DOC =	`cd doc;       $(MAKE) -s files | sed "s:^:doc/:"`
PDS =	`cd pds/md5;   $(MAKE) -s files | sed "s:^:pds/md5/:"`
RARY =	`cd rary;      $(MAKE) -s files | sed "s:^:rary/:"`
BIN =	`cd bin;       $(MAKE) -s files | sed "s:^:bin/:"`
SUBIN =	`cd subin;     $(MAKE) -s files | sed "s:^:subin/:" `
TLPT =	`cd teleport;  $(MAKE) -s files | sed "s:^:teleport/:"`
FSX =	`cd fsx;       $(MAKE) -s files | sed "s:^:fsx/:"`
FLTR =	`cd filters;   $(MAKE) -s files | sed "s:^:filters/:"`
RSLV =	`cd resolvy;   $(MAKE) -s files | sed "s:^:resolvy/:"`
MIME =	`cd mimekit;   $(MAKE) -s files | sed "s:^:mimekit/:"`
SUBST =	`cd maker;     $(MAKE) -s files | sed "s:^:maker/:"`
HDRS =	`ls -d include/*.h include/Makefile include/mkcpp.c include/mkdef include/typedefs.c`

FILEA = $(PDS) $(SRC) $(RARY) $(BIN) $(SUBIN) $(DOC) $(TLPT) \
	$(FSX) $(FLTR) $(RSLV) $(SUBST) $(HDRS)

FILEB = $(FILE0) $(PDS) $(SRC) $(RARY) $(BIN) $(SUBIN) $(DOC) $(TLPT) \
	$(FSX) $(FLTR) $(RSLV) $(SUBST) $(HDRS)

FILEC =	rw.c ck.c qastrip.c srcsign.c srcsign srcvrfy newdist cksums \
	putsigned.c \
	dgcaps.h \
	rsasign \
	make-xcc make-vc.bat make-vc95.bat make-vc.sh link-wince.sh \
	make-vce.sh make-vs8ce.bat include-ce DELEGATE_CONF.vce \
	make-vs8.bat make-vs9.bat \
	src-srcsign.c \
	srcfiles \
	$(FILEB)

FILES = $(FILEC) $(MIME)

MKMKMK = mkmkmk.exe
MKMAKE = mkmake.exe

HDRDIRS = -I../gen -I../include $(CFLAGS)
LIBDIRS = -L../lib

XDG =	src/dg.exe

all:	$(MKMAKE)
	-sh make-init.sh
	@"./$(MKMAKE)" -makeat "" src dg.exe \
		"$(MAKE)" MKMAKE_SRC="" \
		SHELL="$(SHELL)" HDRDIRS="$(HDRDIRS)" LIBDIRS="$(LIBDIRS)"
	"$(XDG)" "-Fsrcmd5" -csrc -f srcfiles -type f -o gen/bldsign.h
	rm src/dg.exe
	rm src/builtin.o
	@"./$(MKMAKE)" -makeat "" src delegated \
		"$(MAKE)" MKMAKE_SRC="" \
		SHELL="$(SHELL)" HDRDIRS="$(HDRDIRS)" LIBDIRS="$(LIBDIRS)"
	@echo "================"
	-"$(XDG)" "-Fsrcmd5" -f srcfiles -type f
	-"$(XDG)" "-Fcksum" -x -c -f srcfiles
	@echo "================"

all1:	$(MKMAKE)
	@"./$(MKMAKE)" -makeat "" src dg.exe \
		"$(MAKE)" MKMAKE_SRC="" \
		SHELL="$(SHELL)" HDRDIRS="$(HDRDIRS)" LIBDIRS="$(LIBDIRS)"

wince-dg.exe:
	@"./$(MKMAKE)" -makeat "" src $@ \
		"$(MAKE)" MKMAKE_SRC="" \
		SHELL="$(SHELL)" HDRDIRS="$(HDRDIRS)" LIBDIRS="$(LIBDIRS)"
	"$(XDG)" "-Fsrcmd5" -csrc -f srcfiles -type f -o gen/bldsign.h
	rm src/$@
	rm src/builtin.o
	@"./$(MKMAKE)" -makeat "" src $@ \
		"$(MAKE)" MKMAKE_SRC="" \
		SHELL="$(SHELL)" HDRDIRS="$(HDRDIRS)" LIBDIRS="$(LIBDIRS)"
	"$(XDG)" "-Fesign" src\$@

$(MKMAKE): Makefile src/Makefile mkmkmk.c maker/mkmake.c
	$(CC) -DMKMKMK -DDEFCC=\"$(CC)\" $(HDRDIRS) $(LIBDIRS) mkmkmk.c -o $(MKMKMK)
	"./$(MKMKMK)" -mkmkmk "$(CC)" $(HDRDIRS) $(LIBDIRS) maker/mkmake.c -o $@

sslway:	$(MKMAKE)
	@"./$(MKMAKE)" -makeat "" filters filters/sslway \
		"$(MAKE)" _SSLway \
		SHELL="$(SHELL)" HDRDIRS="$(HDRDIRS)" LIBDIRS="$(LIBDIRS)"

mime: $(MKMAKE)
	"./$(MKMAKE)" -makeat "" src dg.exe \
		"$(MAKE)" MKMAKE_SRC="" \
		SHELL="$(SHELL)" HDRDIRS="$(HDRDIRS)" LIBDIRS="$(LIBDIRS)"
	"./$(MKMAKE)" -makeat "" mimekit mimekit/enMime \
		"$(MAKE)" -f Makefile.go all1 \
		SHELL="$(SHELL)" HDRDIRS="$(HDRDIRS)" LIBDIRS="$(LIBDIRS)" \
		M17NLIB="-L../lib -lcfi -lrary -lmd5 -lsubst"
	ls -l mimekit/*Mime

any2fdif:
	@"./$(MKMAKE)" -makeat "" src fsx $(MAKE) -f Makefile.go fsx
	@"./$(MKMAKE)" -makeat "" fsx any2fdif $(MAKE) -f Makefile.go

## the following target should not be "file:" to avoid loop of "make -s files"
## or "make -s srcmd5s", on error in chdir like SUBST=`cd maker; ...`
Files:;	@echo $(FILES)
filel:;	@ls -d $(FILES) | cat

ver:;	echo $(VER)
dgdate:; echo $(DGDATE)
ctar:;	tar cf core-delegate$(VER).tar $(FILEC)

DATE = `date "+%y%m%d%H"`
dtar:	tar
	mv delegateX$(VER).tar dgX$(DATE).tar
	gzip $$DTARARGS dgX$(DATE).tar
	ls -l dgX$(DATE).tar.gz
tar:
	src/delegated -Fesign -w rary/windows.c
	echo $(FILEB) $(MIME) |  tr ' ' '\n' > srcfiles
	srcsign
	tar cf delegateX$(VER).tar $(FILES)
	echo $(VER)

rtar:
	tar cf dgX$(DATE)-$(VER).tar $(FILES)
tarcf:
	@tar cf - $(FILES)

sum:
	"$(XDG)" "-Fcksum" -x -c -f srcfiles
sumv:
	"$(XDG)" "-Fcksum" -x -c -v -f srcfiles

srcmd5:
	find $(FILEB) $(MIME) -type f -print|delegated -Fsort|xargs cat|delegated -FMD5

srcmd5f:
	src/delegated -Ffindu $(FILEB) $(MIME) -type f -print|src/delegated -Fsort|src/delegated -FMD5 -c -f -

srcmd5i:
	"$(XDG)" "-Fsrcmd5" -csrc $(FILEB) $(MIME) -type f
srcmd5iv:
	"$(XDG)" "-Fsrcmd5" -v -csrc $(FILEB) $(MIME) -type f

srcmd5s:
	delegated -Fsrcmd5 -s $(FILEB) $(MIME) -type f
srcmd5sv:
	delegated -Fsrcmd5 -s $(FILEB) $(MIME) -type f -v
srcmd5v:
	delegated -Fsrcmd5 -f srcfiles -type f -v

srcmd5ss:
	delegated -Fsrcmd5 -s -f srcfiles -type f

srcfiles: Makefile
	echo $(FILEB) $(MIME) |  tr ' ' '\n' > srcfiles

bldsign:
	"$(XDG)" "-Fsrcmd5" -csrc $(FILEB) $(MIME) -type f -o gen/bldsign.h

qastrip:	qastrip.c
	$(CC) -x c++ -DQS -o qastrip -O3 -Iinclude qastrip.c -Llib -lrary -lmimekit -lsubst -lmd5

xtar:	tar
	src/delegated -Fstar f delegate$(VER).tar -e s:^:dg$(DVER)/: > dg$(DVER).tar
	rm delegate$(VER).tar

clean:;	rm -f src/Makefile.chk src/Makefile.tst \
	mkcpp src/embed src/dg.exe srcsign.o \
	mkmake.exe mkmkmk.exe \
	subin/dgbind subin/dgchroot subin/dgcpnod subin/dgpam subin/dgdate \
	*/mkmake.err */*.go */*.cpp */*.o */*.a \
	pds/md5/*.o pds/md5/*.go \
	include/randtext.c rary/randtext.c \
	maker/*.list maker/libsubst.a.mani \
	filters/mkstab filters/gzip_dl.c filters/sslway_dl.c \
	src/CONF_IS_GOT src/_builtin.c src/_.c
	cd mimekit; make clean
	cd resolvy; make clean

zip:
	cp -p src/delegated.exe dg$(DVER).exe
	zip dg$(DVER).zip \
		COPYRIGHT \
		CHANGES \
		README \
		DG9note.html \
		doc/Manual.htm \
		doc/tutor-en.htm \
		doc/tutor-jp.htm \
		dg$(DVER).exe
