CFLAGS = -std=gnu89 -march=i386 -O3 -Wall -Wextra -Werror -Wno-long-long -pedantic -Wformat-security
CLIBS = watt32/bin/386watt.a -lemu

#all: fdnpkg.exe test-ver.exe test-url.exe testhttp.exe test-unz.exe
all: fdnpkg.exe

# This target produces a fdnpkg0.exe that is not compressed and do not have cwsdpmi embedded
fdnpkg0.exe: fdnpkg.o crc32.o kprintf.o kittendj.o fileexst.o http.o libgz.o libunzip.o loadconf.o lsm.o net.o lzmadec.o pkgdb.o pkginst.o pkgsrch.o pkgrem.o showinst.o getdelim.o rtrim.o parsecmd.o parseurl.o snprintf.o helpers.o tinfl.o
	gcc fdnpkg.o crc32.o kprintf.o kittendj.o fileexst.o http.o libgz.o libunzip.o loadconf.o lsm.o net.o lzmadec.o pkgdb.o pkginst.o pkgsrch.o pkgrem.o showinst.o getdelim.o rtrim.o parsecmd.o parseurl.o snprintf.o helpers.o tinfl.o $(CLIBS) -o fdnpkg0.exe

# Here I strip fdnpkg0.exe, add the cwsdpmi stub and compress the whole thing with UPX
fdnpkg.exe: fdnpkg0.exe
	exe2coff fdnpkg0.exe
	copy /B cwsdstub.bin+fdnpkg0 fdnpkg.exe
	del fdnpkg0
	upx -9 fdnpkg.exe

fdnpkg.o: fdnpkg.c
	gcc -c fdnpkg.c -o fdnpkg.o $(CFLAGS)

htget.o: htget.c
	gcc -c htget.c -o htget.o $(CFLAGS) -I watt32/inc

loadconf.o: loadconf.c
	gcc -c loadconf.c -o loadconf.o $(CFLAGS)

lsm.o: lsm.c
	gcc -c lsm.c -o lsm.o $(CFLAGS)

net.o: net.c
	gcc -c net.c -o net.o -I watt32/inc $(CFLAGS)

parseurl.o: parseurl.c
	gcc -c parseurl.c -o parseurl.o $(CFLAGS)

pkgdb.o: pkgdb.c
	gcc -c pkgdb.c -o pkgdb.o $(CFLAGS)

pkgsrch.o: pkgsrch.c
	gcc -c pkgsrch.c -o pkgsrch.o $(CFLAGS)

pkgrem.o: pkgrem.c
	gcc -c pkgrem.c -o pkgrem.o $(CFLAGS)

pkginst.o: pkginst.c
	gcc -c pkginst.c -o pkginst.o $(CFLAGS)

showinst.o: showinst.c
	gcc -c showinst.c -o showinst.o $(CFLAGS)

getdelim.o: getdelim.c
	gcc -c getdelim.c -o getdelim.o $(CFLAGS)

fileexst.o: fileexst.c
	gcc -c fileexst.c -o fileexst.o $(CFLAGS)

http.o: http.c
	gcc -c http.c -o http.o $(CFLAGS)

kittendj.o: kittendj.c
	gcc -c kittendj.c -o kittendj.o $(CFLAGS)

kprintf.o: kprintf.c
	gcc -c kprintf.c -o kprintf.o $(CFLAGS)

rtrim.o: rtrim.c
	gcc -c rtrim.c -o rtrim.o $(CFLAGS)

parsecmd.o: parsecmd.c
	gcc -c parsecmd.c -o parsecmd.o $(CFLAGS)

snprintf.o: snprintf.c
	gcc -c snprintf.c -o snprintf.o $(CFLAGS)

libunzip.o: libunzip.c
	gcc -c libunzip.c -o libunzip.o $(CFLAGS)

lzmadec.o: lzmadec.c
	gcc -c lzmadec.c -o lzmadec.o $(CFLAGS)

helpers.o: helpers.c
	gcc -c helpers.c -o helpers.o $(CFLAGS)

crc32.o: crc32.c
	gcc -c crc32.c -o crc32.o $(CFLAGS)

tinfl.o: tinfl.c
	gcc -c tinfl.c -o tinfl.o $(CFLAGS)

# targets below are useful only for testing purposes
test-url.exe: test-url.c parseurl.o
	gcc test-url.c parseurl.o -o test-url.exe $(CFLAGS)

test-ver.exe: test-ver.c kprintf.o kittendj.o helpers.o lsm.o rtrim.o snprintf.o
	gcc test-ver.c kprintf.o kittendj.o helpers.o lsm.o rtrim.o snprintf.o -o test-ver.exe $(CFLAGS)

testhttp.exe: testhttp.c helpers.o http.o parseurl.o net.o
	gcc testhttp.c helpers.o http.o parseurl.o net.o watt32/bin/386watt.a -o testhttp.exe $(CFLAGS)

test-unz.exe: test-unz.c crc32.o kittendj.o kprintf.o libunzip.o lzmadec.o tinfl.o
	gcc -g test-unz.c crc32.o kittendj.o kprintf.o libunzip.o lzmadec.o tinfl.o -o test-unz.exe $(CFLAGS)

test-gz.exe: test-gz.c crc32.o libgz.o tinfl.o
	gcc -g test-gz.c crc32.o libgz.o tinfl.o -o test-gz.exe $(CFLAGS)

pkg: fdnpkg.exe
	mkdir appinfo
	mkdir bin
	mkdir doc
	mkdir doc\fdnpkg
	mkdir source
	mkdir source\fdnpkg
	copy fdnpkg.lsm appinfo
	copy fdnpkg.exe bin
	copy fdnpkg.cfg bin
	copy fdnpkg.txt doc\fdnpkg
	copy history.txt doc\fdnpkg
	copy license.txt doc\fdnpkg
	copy *.c source\fdnpkg
	copy *.h source\fdnpkg
	copy *.txt source\fdnpkg
	copy makefile source\fdnpkg
	copy *.a source\fdnpkg
	copy *.cfg source\fdnpkg
	copy *.bin source\fdnpkg
	del fdnpkg.zip
	zip -r -k -9 fdnpkg.zip appinfo bin doc nls source
	deltree /Y appinfo
	deltree /Y bin
	deltree /Y doc
	deltree /Y source
	echo "fdnpkg.zip ready!"

clean:
	del *.o
	del *.exe
