.SUFFIXES: .inc

all:	mkdir \
	magic_elf \
	out/addr_of_main \
	evil_magic \
	out/readelf/evil_magic \
	out/readelf/sh \
	other_magic \
	out/check_dist/small \
	out/check_dist/big \
	out/one_step_closer/one-test \
	out/one_step_closer/two-test \
	entry_point

magic_elf: \
	mkdir \
	out/magic_elf \
	out/magic_elf/strings \
	out/magic_elf/od \
	out/magic_elf/ndisasm.asm \
	out/magic_elf/gdb

evil_magic: \
	mkdir \
	out/evil_magic/nasm \
	out/evil_magic/cc \
	out/evil_magic/od \
	out/evil_magic/evil_magic.asm \
	out/evil_magic/evil_magic.c

other_magic: \
	out/other_magic/perl \
	out/other_magic/mem \
	out/other_magic/exe

entry_point: \
	out/entry_point/readelf \
	out/entry_point/scan \
	out/entry_point/gdb \
	out/entry_point/ndisasm

mkdir:
	mkdir -p tmp out \
		out/magic_elf \
		tmp/magic_elf \
		out/evil_magic \
		tmp/evil_magic \
		out/readelf \
		out/check_dist \
		out/other_magic \
		tmp/one_step_closer \
		tmp/one_step_closer/{one,two} \
		out/one_step_closer/{one,two} \
		out/entry_point \
	2> /dev/null

PWD := $(shell pwd)
DIRNAME := $(shell basename $(PWD))
CFLAGS := $(CFLAGS) -Wall -O2
HTML := file://$(PWD)/_html/index.html
DVI := file://$(PWD)/_dvi/$(DIRNAME).dvi
TODAY := $(shell date "+%Y-%m-%d")

clean:
	rm -rf tmp out _dvi _html $(DIRNAME).junk
	find . -type f -name '*.bak' \
		-o -name '*~' \
		-o -name '*.infected' \
		-o -name core \
	| xargs rm -f

out/calc.sgml: all /bin/sh src/calc.pl 
	src/calc.pl > $@ 2>&1

html: out/calc.sgml
	[ -d _html ] || mkdir _html
	jw -f docbook -b html -o _html main.sgml
mozilla: html
	mozilla $(HTML) &
netscape: html
	netscape $(HTML) &
links: html
	links $(HTML)
dvi: out/calc.sgml
	[ -d _dvi ] || mkdir _dvi
	jw -f docbook -b dvi -o _dvi main.sgml
xdvi: dvi
	xdvi $(DVI)

dist_html: html
	tar -C .. -zcf $(DIRNAME)-$(TODAY).html.tar.gz \
		$(DIRNAME)/_html \
		--exclude-from=tar-exclude
dist: dist_html clean
	tar -C .. -zcf $(DIRNAME)-$(TODAY).tar.gz \
		$(DIRNAME) \
		--exclude-from=tar-exclude

out/magic_elf/magic_elf: src/magic_elf/magic_elf.c src/magic_elf/cc.sh
	src/magic_elf/cc.sh > $@ 2>&1
out/magic_elf/strings: out/magic_elf/magic_elf src/magic_elf/strings.sh
	src/magic_elf/strings.sh > $@ 2>&1
out/magic_elf/od: out/magic_elf/magic_elf src/magic_elf/od.sh
	src/magic_elf/od.sh > $@ 2>&1
out/magic_elf/ndisasm.asm: out/magic_elf/magic_elf src/magic_elf/ndisasm.sh
	src/magic_elf/ndisasm.sh > $@ 2>&1
out/magic_elf/gdb: out/magic_elf/magic_elf src/magic_elf/gdb.sh
	src/magic_elf/gdb.sh > $@ 2>&1

tmp/addr_of_main: src/addr_of_main.c
	$(CC) $(CFLAGS) -o $@ $?
out/addr_of_main: tmp/addr_of_main
	$? > $@ 2>&1

out/evil_magic/nasm: src/evil_magic/evil_magic.asm src/evil_magic/nasm.sh
	src/evil_magic/nasm.sh > $@ 2>&1
out/evil_magic/od: out/evil_magic/nasm src/evil_magic/od.sh
	src/evil_magic/od.sh > $@ 2>&1
out/evil_magic/evil_magic.asm: out/evil_magic/nasm src/evil_magic/ndisasm.sh
	src/evil_magic/ndisasm.sh > $@ 2>&1
out/evil_magic/evil_magic.c: \
	out/evil_magic/evil_magic.asm \
	src/evil_magic/ndisasm.pl
	src/evil_magic/ndisasm.pl < out/evil_magic/evil_magic.asm > $@ 2>&1
out/evil_magic/cc: out/evil_magic/evil_magic.c src/evil_magic/cc.sh
	src/evil_magic/cc.sh > $@ 2>&1

out/readelf/evil_magic: tmp/evil_magic src/readelf/evil_magic.sh
	src/readelf/evil_magic.sh > $@ 2>&1
out/readelf/sh: src/readelf/sh.sh
	$? > $@ 2>&1

out/other_magic/perl: src/other_magic/perl.pl
	$? > $@ 2>&1
out/other_magic/mem: src/other_magic/mem.sh
	$? > $@ 2>&1
out/other_magic/exe: src/other_magic/exe.sh
	$? > $@ 2>&1

out/check_dist/small: \
	src/check_dist/small.sh \
	src/check_dist/check_dist.pl
	src/check_dist/small.sh > $@ 2>&1
out/check_dist/big: \
	src/check_dist/big.sh \
	src/check_dist/check_dist.pl
	src/check_dist/big.sh > $@ 2>&1

src/one_step_closer/infection.inc: \
	Makefile \
	src/one_step_closer/infection.asm \
	src/one_step_closer/infection.sh
	src/one_step_closer/infection.sh > $@ 2>&1
tmp/one_step_closer/one/sh_infected: \
	src/one_step_closer/cc.sh \
	src/one_step_closer/infection.inc
	src/one_step_closer/cc.sh > out/one_step_closer/one/cc 2>&1
out/one_step_closer/one-test: \
	tmp/one_step_closer/one/sh_infected \
	src/one_step_closer/one-test.sh
	src/one_step_closer/one-test.sh > $@ 2>&1
tmp/one_step_closer/two/sh_infected: \
	src/one_step_closer/cc.sh \
	src/one_step_closer/infection.inc
	src/one_step_closer/cc.sh "two" > out/one_step_closer/two/cc 2>&1
out/one_step_closer/two-test: \
	tmp/one_step_closer/two/sh_infected \
	src/one_step_closer/two-test.sh
	src/one_step_closer/two-test.sh > $@ 2>&1

out/entry_point/readelf: \
	tmp/one_step_closer/one/sh_infected \
	src/entry_point/readelf.sh
	src/entry_point/readelf.sh > $@ 2>&1
out/entry_point/scan: \
	tmp/one_step_closer/one/sh_infected \
	src/entry_point/scan.sh 
	src/entry_point/scan.sh > $@ 2>&1
out/entry_point/gdb: \
	src/entry_point/gdb.sh
	$?  > $@ 2>&1
out/entry_point/ndisasm: \
	src/entry_point/ndisasm.sh
	$?  > $@ 2>&1
