.SUFFIXES: .inc .gdb .ndisasm

all:	mkdir \
	magic_elf \
	evil_magic \
	out/readelf/evil_magic \
	out/readelf/sh \
	other_magic \
	out/check_dist/small \
	out/check_dist/big \
	entry_point \
	one_step_closer \
	additional_cs \
	doing_it_in_c 

magic_elf: \
	mkdir \
	out/magic_elf \
	out/magic_elf/strings \
	out/magic_elf/od \
	out/magic_elf/ndisasm \
	out/magic_elf/gdb \
	out/magic_elf/magic_elf_static \
	out/magic_elf/static_main.gdb \
	out/magic_elf/static_write.gdb \
	out/magic_elf/addr_of_main

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: \
	mkdir \
	out/other_magic/perl \
	out/other_magic/mem \
	out/other_magic/exe

entry_point: \
	mkdir \
	out/entry_point/readelf \
	out/entry_point/scan \
	out/entry_point/sh.gdb \
	out/entry_point/sh.ndisasm \
	out/entry_point/two.ndisasm \
	out/entry_point/three.ndisasm \
	out/entry_point/entry_point \
	out/entry_point/ldd \
	out/entry_point/nm \
	src/entry_point/__libc_start_main

one_step_closer: \
	mkdir \
	out/one_step_closer/one-test \
	out/one_step_closer/two-test \
	out/one_step_closer/three-test

additional_cs: \
	mkdir \
	out/additional_cs/od \
	out/additional_cs/test \
	out/additional_cs/readelf \
	out/additional_cs/scan

doing_it_in_c: \
	out/doing_it_in_c/get_relocate_ofs.ndisasm \
	out/doing_it_in_c/infection.inc \
	out/doing_it_in_c/begin.ndisasm \
	out/doing_it_in_c/addr \
	out/doing_it_in_c/complete.ndisasm \
	out/doing_it_in_c/test

mkdir:
	mkdir -p tmp out \
		{out,tmp}/magic_elf \
		{out,tmp}/evil_magic \
		out/readelf \
		out/check_dist \
		out/other_magic \
		{tmp,out}/one_step_closer/{one,two,three} \
		out/entry_point \
		out/additional_cs \
		tmp/additional_cs/three \
		{out,tmp}/doing_it_in_c/three \
	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 _redhat $(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 ] && rm -r _html
	mkdir _html
	cd _html && jade -t sgml -i html -d '../ldp.dsl#html' ../main.sgml

# HTML output through the default DSSSL shipped by RedHat 
redhat: out/calc.sgml
	- [ -d _redhat ] && rm -r _redhat
	mkdir _redhat
	jw -f docbook -b html -o _redhat main.sgml

mozilla: html
	mozilla $(HTML) &
netscape: html
	netscape $(HTML) &
links: html
	links $(HTML)
dvi: out/calc.sgml
	- [ -d _dvi ] && rm -r _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: 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
out/magic_elf/magic_elf_static: \
	src/magic_elf/magic_elf.c \
	src/magic_elf/cc_static.sh
	src/magic_elf/cc_static.sh > $@ 2>&1
out/magic_elf/static_main.gdb: \
	out/magic_elf/magic_elf_static \
	src/magic_elf/gdb.sh
	src/magic_elf/gdb.sh "tmp/magic_elf/magic_elf_static" > $@ 2>&1
out/magic_elf/static_write.gdb: \
	out/magic_elf/magic_elf_static \
	src/magic_elf/gdb.sh
	src/magic_elf/gdb.sh \
		"tmp/magic_elf/magic_elf_static" "__libc_write" \
	> $@ 2>&1
tmp/magic_elf/addr_of_main: src/magic_elf/addr_of_main.c
	$(CC) $(CFLAGS) -o $@ $?
out/magic_elf/addr_of_main: tmp/magic_elf/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

out/one_step_closer/infection.inc: \
	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 \
	out/one_step_closer/infection.inc
	src/one_step_closer/cc.sh "one_step_closer" "one" \
	> 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 \
	out/one_step_closer/infection.inc
	src/one_step_closer/cc.sh "one_step_closer" "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
tmp/one_step_closer/three/sh_infected: \
	src/one_step_closer/cc.sh \
	out/one_step_closer/infection.inc
	src/one_step_closer/cc.sh "one_step_closer" "three" \
	> out/one_step_closer/three/cc 2>&1
out/one_step_closer/three-test: \
	tmp/one_step_closer/three/sh_infected \
	src/one_step_closer/three-test.sh
	src/one_step_closer/three-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/sh.gdb: \
	src/entry_point/gdb.sh
	$?  > $@ 2>&1
out/entry_point/sh.ndisasm: \
	src/entry_point/ndisasm.sh
	$?  > $@ 2>&1
out/entry_point/entry_point: \
	src/entry_point/entry_point.sh \
	tmp/one_step_closer/one/sh_infected \
	tmp/one_step_closer/two/sh_infected
	src/entry_point/entry_point.sh > $@ 2>&1
out/entry_point/two.ndisasm: \
	src/entry_point/ndisasm.sh \
	tmp/one_step_closer/two/sh_infected
	src/entry_point/ndisasm.sh \
	tmp/one_step_closer/two/sh_infected \
	> $@ 2>&1
out/entry_point/ldd: \
	src/entry_point/ldd.sh
	$?  > $@ 2>&1
out/entry_point/nm: \
	src/entry_point/nm.sh
	$?  > $@ 2>&1
src/entry_point/__libc_start_main: \
	out/entry_point/nm \
	src/entry_point/get_libc_start_main.sh
	src/entry_point/get_libc_start_main.sh
out/entry_point/three.ndisasm: \
	src/entry_point/ndisasm.sh \
	tmp/one_step_closer/three/sh_infected
	src/entry_point/ndisasm.sh \
	tmp/one_step_closer/three/sh_infected \
	> $@ 2>&1

out/additional_cs/od: \
	src/additional_cs/od.sh
	$?  > $@ 2>&1
out/additional_cs/infection.inc:
	ln -s ../one_step_closer/infection.inc $@
tmp/additional_cs/three/sh_infected: \
	src/one_step_closer/cc.sh \
	out/additional_cs/infection.inc
	src/one_step_closer/cc.sh "additional_cs" "three" \
	> out/additional_cs/cc 2>&1
out/additional_cs/test: \
	tmp/additional_cs/three/sh_infected \
	src/additional_cs/test.sh
	src/additional_cs/test.sh > $@ 2>&1
out/additional_cs/readelf: \
	tmp/additional_cs/three/sh_infected
	( ls -l $?; readelf -l $? ) > $@ 2>&1
out/additional_cs/scan: \
	tmp/additional_cs/three/sh_infected \
	src/additional_cs/scan.sh
	src/additional_cs/scan.sh > $@ 2>&1

out/doing_it_in_c/infection.inc: \
	src/doing_it_in_c/infection.asm \
	src/one_step_closer/infection.sh
	src/one_step_closer/infection.sh doing_it_in_c > $@ 2>&1
tmp/doing_it_in_c/three/sh_infected: \
	src/one_step_closer/cc.sh \
	out/doing_it_in_c/infection.inc
	src/one_step_closer/cc.sh "doing_it_in_c" "three" \
	> out/doing_it_in_c/three/cc 2>&1
out/doing_it_in_c/get_relocate_ofs.ndisasm: \
	src/doing_it_in_c/ndisasm.sh \
	tmp/doing_it_in_c/three/sh_infected
	src/doing_it_in_c/ndisasm.sh > $@ 2>&1
out/doing_it_in_c/begin.ndisasm: \
	src/doing_it_in_c/ndisasm.sh \
	tmp/doing_it_in_c/three/sh_infected
	src/doing_it_in_c/ndisasm.sh \
	tmp/doing_it_in_c/three/infector "begin" > $@ 2>&1
out/doing_it_in_c/addr: \
	src/doing_it_in_c/addr.c
	$(CC) $(CFLAGS) -o tmp/doing_it_in_c/addr $? > $@ 2>&1 \
	&& tmp/doing_it_in_c/addr >> $@ 2>&1
out/doing_it_in_c/test: \
	tmp/doing_it_in_c/three/sh_infected \
	src/doing_it_in_c/test.sh
	src/doing_it_in_c/test.sh > $@ 2>&1
out/doing_it_in_c/complete.ndisasm: \
	src/doing_it_in_c/ndisasm.sh \
	tmp/doing_it_in_c/three/sh_infected
	src/doing_it_in_c/ndisasm.sh \
	tmp/doing_it_in_c/three/infector infection 3 > $@ 2>&1
