# TestDisk Makefile
# http://www.cgsecurity.org/testdisk.html
#
STRIP=strip
CC=gcc
REN=mv
# You can get UPX at http://upx.sourceforge.net/ if you want to compress TestDisk binary
UPX=upx
VER_E2FSPROGS=1.34
VER_PROGSREISERFS=0.3.1-rc8
VER_NTFSPROGS=1.8.2
LIBREISER=../progsreiserfs-$(VER_PROGSREISERFS)/libreiserfs/.libs/libreiserfs.a
INCREISER=../progsreiserfs-$(VER_PROGSREISERFS)/include
LIBEXT=../e2fsprogs-$(VER_E2FSPROGS)/lib/ext2fs/libext2fs.a
INCNTFS=../ntfsprogs-$(VER_NTFSPROGS)
LIBNTFS=../ntfsprogs-$(VER_NTFSPROGS)/libntfs/.libs/libntfs.a
CFLAGS=-Wall -MD -Wpointer-arith -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wshadow -Wwrite-strings -W -Wcast-align -Waggregate-return -Wbad-function-cast -Wcast-qual -Wundef -Wredundant-decls -Wsign-compare -O2 -g -I. -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -Wnested-externs -Winline -DHAVE_E2FSPROGS -DHAVE_PROGSREISERFS31 -DHAVE_NTFSPROGS
#-DHAVE_PROGSREISERFS30
# Debian uses progsreiserfs 3.0
#-Wconversion
#-Wunreachable-code -Wmissing-noreturn 

LIBSA=$(LIBEXT) $(LIBREISER) $(LIBNTFS)
LIBS=

OBJS=lang.o fnctdsk.o analyse.o testdisk.o readpart.o godmode.o bsd.o common.o fat.o mbrmanip.o ext2.o bfs.o swap.o netware.o intrface.o hdaccess.o ntfs.o rfs.o lvm.o ext2_dir.o fat_dir.o md.o adv.o fat_adv.o dir.o rfs_dir.o geometry.o edit.o add_part.o chgtype.o ntfs_io.o ntfs_dir.o

default:
	@echo "If you don't have/want e2fsprogs, progsreiserfs or ntfsprogs libraries, configure this Makefile before running it"
	@echo "make linux"
	@echo "make linuxstatic"
	@echo "make dos"
	@echo "make dosstatic"
	@echo "make bsd"

$(LIBEXT):
	@echo "You can run \"make e2fsprogs\" in testdisk directory to compile this missing library"
	@exit 1;

$(LIBREISER):
	@echo "You can run \"make progsreiserfs\" in testdisk directory to compile this missing library"
	@exit 1;

$(LIBNTFS):
	@echo "You can run \"make ntfsprogs\" in testdisk directory to compile this missing library"
	@exit 1;

linux:
	echo \#define COMPILE_TIME \"`date`\" > version.h
	echo \#define COMPILE_BY \"`whoami`\" >> version.h
	echo \#define COMPILE_HOST \"`hostname`\" >> version.h
	$(MAKE) testdisk CFLAGS="$(CFLAGS) -DLINUX -DHAVE_SNPRINTF -DHAVE_VSNPRINTF -pipe"  LIBS="$(LIBS) -lncurses"

linuxstatic:
	echo \#define COMPILE_TIME \"`date`\" > version.h
	echo \#define COMPILE_BY \"`whoami`\" >> version.h
	echo \#define COMPILE_HOST \"`hostname`\" >> version.h
	$(MAKE) testdisk_static CFLAGS="$(CFLAGS) -DLINUX -DHAVE_SNPRINTF -DHAVE_VSNPRINTF -pipe -fomit-frame-pointer"  LIBS="$(LIBS) -lncurses"

dos:
	@echo > version.h
	$(MAKE) testdisk.exe LIBS="-lpdcurses"

dosstatic:
	$(MAKE) dos
	djp -s testdisk.exe

# -fms-extensions is needed for using ntfsprogs
bsd:
	echo \#define COMPILE_TIME \"`date`\" > version.h
	echo \#define COMPILE_BY \"`whoami`\" >> version.h
	echo \#define COMPILE_HOST \"`hostname`\" >> version.h
	$(MAKE) testdisk CFLAGS="$(CFLAGS) -fms-extensions -DBSD -DHAVE_SNPRINTF -DHAVE_VSNPRINTF -pipe"  LIBS="$(LIBS) -lncurses"

testdisk: $(LIBSA) $(OBJS) 
	$(CC) $(CFLAGS) -o testdisk $(OBJS) $(LIBS) $(LIBSA) $(LDFLAGS)

testdisk_static: $(LIBSA) $(OBJS) 
	$(CC) $(CFLAGS) -o testdisk_static $(OBJS) $(LIBS) $(LIBSA) $(LDFLAGS) --static
	$(STRIP) testdisk_static
	$(STRIP) -s testdisk_static
	$(STRIP) --remove-section=.comment testdisk_static
	$(STRIP) --remove-section=.note testdisk_static
# Thanks to Tiger and The Starman for the UPX idea
	$(UPX)   -9 testdisk_static

testdisk.exe: $(LIBSA) $(OBJS)
	$(CC) $(CFLAGS) -o testdisk.exe $(OBJS) $(LIBS) $(LIBSA) $(LDFLAGS)


mbrmanip.o: mbrmanip.c types.h common.h fnctdsk.h lang.h
	$(CC) $(CFLAGS) -c mbrmanip.c

readpart.o: readpart.c types.h common.h readpart.h testdisk.h fnctdsk.h analyse.h lang.h intrface.h
	$(CC) $(CFLAGS) -c readpart.c

common.o: common.c types.h common.h lang.h
	$(CC) $(CFLAGS) -c common.c

fat.o: fat.c types.h common.h fat.h lang.h fnctdsk.h testdisk.h intrface.h
	$(CC) $(CFLAGS) -c fat.c

fat_adv.o: fat_adv.c types.h common.h fat.h lang.h fnctdsk.h testdisk.h intrface.h dir.h fat_dir.h
	$(CC) $(CFLAGS) -c fat_adv.c

fat_dir.o: fat_dir.c types.h common.h fat.h lang.h fnctdsk.h testdisk.h intrface.h dir.h fat_dir.h
	$(CC) $(CFLAGS) -c fat_dir.c

dir.o: dir.c types.h common.h fat.h lang.h fnctdsk.h testdisk.h intrface.h dir.h
	$(CC) $(CFLAGS) -c dir.c

ntfs.o: ntfs.c types.h common.h intrface.h ntfs.h fnctdsk.h lang.h
	$(CC) $(CFLAGS) -c ntfs.c

bsd.o: bsd.c types.h common.h fnctdsk.h bsd.h intrface.h fnctdsk.h
	$(CC) $(CFLAGS) -c bsd.c

ext2.o: ext2.c types.h common.h ext2.h intrface.h fnctdsk.h
	$(CC) $(CFLAGS) -c ext2.c

bfs.o: bfs.c types.h common.h bfs.h intrface.h fnctdsk.h
	$(CC) $(CFLAGS) -c bfs.c

netware.o: netware.c types.h common.h netware.h 
	$(CC) $(CFLAGS) -c netware.c

swap.o: swap.c types.h common.h swap.h fnctdsk.h
	$(CC) $(CFLAGS) -c swap.c

md.o: md.c types.h common.h md.h intrface.h fnctdsk.h
	$(CC) $(CFLAGS) -c md.c

testdisk.o:  testdisk.c types.h common.h testdisk.h lang.h intrface.h godmode.h fnctdsk.h fat.h
	$(CC) $(CFLAGS) -c testdisk.c

analyse.o:  analyse.c types.h common.h fnctdsk.h analyse.h bfs.h swap.h bsd.h fat.h ntfs.h ext2.h netware.h intrface.h rfs.h lvm.h md.h
	$(CC) $(CFLAGS) -c analyse.c

godmode.o:  godmode.c types.h common.h fnctdsk.h analyse.h lang.h godmode.h testdisk.h fat.h ext2.h intrface.h md.h adv.h
	$(CC) $(CFLAGS) -c godmode.c

fnctdsk.o: fnctdsk.c types.h common.h fnctdsk.h lang.h testdisk.h intrface.h analyse.h
	$(CC) $(CFLAGS) -c fnctdsk.c

lang.o: lang.c
	$(CC) $(CFLAGS) -c lang.c

hdaccess.o:   hdaccess.c types.h common.h fnctdsk.h hdaccess.h readpart.h
	$(CC) $(CFLAGS) -c hdaccess.c

intrface.o: intrface.c types.h common.h lang.h intrface.h readpart.h godmode.h fnctdsk.h testdisk.h lang.h fat.h ext2.h rfs.h ntfs.h adv.h
	$(CC) $(CFLAGS) -c intrface.c

geometry.o: geometry.c types.h common.h intrface.h
	$(CC) $(CFLAGS) -c geometry.c

edit.o: edit.c types.h common.h intrface.h fnctdsk.h
	$(CC) $(CFLAGS) -c edit.c

add_part.o: add_part.c types.h common.h intrface.h fnctdsk.h
	$(CC) $(CFLAGS) -c add_part.c

chgtype.o: chgtype.c types.h common.h lang.h intrface.h fnctdsk.h
	$(CC) $(CFLAGS) -c chgtype.c

adv.o: adv.c types.h common.h lang.h intrface.h readpart.h fnctdsk.h testdisk.h fat.h ntfs.h adv.h
	$(CC) $(CFLAGS) -c adv.c

rfs.o: rfs.c types.h common.h rfs.h fnctdsk.h intrface.h
	$(CC) $(CFLAGS) -c rfs.c

lvm.o: lvm.c types.h common.h lvm.h intrface.h fnctdsk.h
	$(CC) $(CFLAGS) -c lvm.c

ext2_dir.o: ext2_dir.c types.h common.h intrface.h dir.h ext2_dir.h
	$(CC) $(CFLAGS) -c ext2_dir.c

rfs_dir.o: rfs_dir.c types.h common.h intrface.h rfs.h dir.h
	$(CC) $(CFLAGS) -c rfs_dir.c -I$(INCREISER)

ntfs_io.o: ntfs_io.c types.h common.h
	$(CC) $(CFLAGS) -c ntfs_io.c -I. -I$(INCNTFS)/include -I$(INCNTFS)

ntfs_dir.o: ntfs_dir.c types.h common.h intrface.h ntfs.h dir.h
	$(CC) $(CFLAGS) -c ntfs_dir.c -I. -I$(INCNTFS)/include -I$(INCNTFS)

clean:
	rm -f *.o *~ *.d core *.bak version.h

zip:
	rm -f testdisk.zip
	zip testdisk.zip *
