SUFFIX=.gz .bz2 .tar.bz2 .tgz
CFLAGS=-O2 -Wall -pedantic -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -std=gnu99
CFLAGS=-g3 -Wall -pedantic -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -std=gnu99

#
# INSTALLDIR indicates directory where program is to be installed.
# Suggested values are "/usr/local/bin" or "/usr/bin".
#
INSTALL = install
INSTALLDIR = /usr/bin
INSTBINMODE = 755
INSTDOCMODE = 755
INSTALLOWN = root
INSTALLGRP = root

#
# MANPAGEDIR indicates directory where the fdupes man page is to be
# installed. Suggested values are "/usr/local/man" or "/usr/man".
#
MANPAGEDIR = /usr/share/man

#
# VERSION determines the program's version number.
#
VERSION = "$$(cat version)"

#
# ALL that needs/should be done
#
all:	freedup test

#
# INSTALLATION without using RPM
#
install: freedup freedup.1.gz
	$(INSTALL) -d $(INSTALLDIR)
	$(INSTALL) -p -s -m $(INSTBINMODE) freedup $(INSTALLDIR)/freedup
	$(INSTALL) -d $(MANPAGEDIR)/man1
	$(INSTALL) -p -m $(INSTDOCMODE) freedup.1.gz $(MANPAGEDIR)/man1/

#
# FREEDUP direct dependencies
#
freedup.o: freedup.c freedup.h version.h

version.h: freedup.spec

#
# VERSION is now only specified in freedup.spec
# this action generates a reliable "version" file
# that will be used troughout this package
#
version: freedup.spec
	awk '{if($$1=="%define"){if($$2=="version")v=$$3;if($$2=="release")r=$$3;}};END{printf"%s-%s",v,r;}' \
	freedup.spec >version

version.h: version
	echo -e "#define VERSION\t\"$$(cat version)\"" >version.h

#
# An attempt to produce a single file documentation
#
full.html: head.html syntax.html download.html intro.html process.html examples.html questions.html
	-@rm -f full.html .full.html
	for n in head.html intro.html syntax.html download.html process.html examples.html questions.html ; do \
	sed -n '/<BODY/,/<.BODY>/p' $$n | grep -v 'BODY' >>.full.html ; done
	./encap .full.html full.html
	rm .full.html

README:	full.html
	html2text -nobs -o $@ $<

ChangeLog: freedup.spec
	sed -n '/^%changelog/,$$p' freedup.spec | grep -v '^%changelog' >ChangeLog

ChangeLog.html: ChangeLog
	./encap ChangeLog ChangeLog.html

TODO.html: TODO
	./encap TODO TODO.html

#
# automatically generate all published file formats
#
%.tar.bz2:	%.tgz
	gzip -cd $< | bzip2 -c >$@

%.tgz:	%.tar.bz2
	bzip2 -cd $< | gzip -c >$@

%.gz:	%
	gzip -c $< >$@

#
# generate the setup.hint file for CYGWIN automatically
#
setup.hint: Makefile version freedup.spec
	echo "# This file is generated by make" >setup.hint
	echo "category: System Utils" >>setup.hint
	echo "requires: cygwin" >>setup.hint
	echo "sdesc: Freedup reduces the amount of used space by linking duplicate files." >>setup.hint
	echo "ldesc: Freedup walks through the specified file trees (directories). When it finds two identical files, it hard links them together. In this case two or more files still exist in their respective directories, but only one copy of the data is stored on disk."  >>setup.hint
	echo "curr: $$(cat version)" >>setup.hint

#
# generate the distribution tarballs
# (avoid calling this - use make rpm instead)
#
tarball:	freedup version README ChangeLog freedup.1.gz
	strip freedup
	if [ -d freedup-$$(cat version|cut -d- -f1) ] ; then exit -1 ;fi
	mkdir freedup-$$(cat version|cut -d- -f1)
	mkdir freedup-$$(cat version|cut -d- -f1)/html
	cp Makefile freedup.spec freedup freedup.c freedup.h \
	   ChangeLog freedup.1.gz TODO README encap \
	freedup-$$(cat version|cut -d- -f1)
	cp freedup.css upper.html foot.html head.html index.html intro.html hint.html \
	   download.html examples.html process.html questions.html syntax.html \
	freedup-$$(cat version|cut -d- -f1)/html
	tar cjf /usr/src/packages/SOURCES/freedup-$$(cat version)-src.tar.bz2 \
	freedup-$$(cat version|cut -d- -f1)
	cp /usr/src/packages/SOURCES/freedup-$$(cat version)-src.tar.bz2 .
	make freedup-$$(cat version)-src.tgz

#
# generate the own distribution RPM file
#
rpm:	tarball
	rpmbuild -bb freedup.spec
	cp /usr/src/packages/RPMS/i586/freedup-$$(cat version)* .

#
#
#
clean:	
	rm -f freedup ChangeLog ChangeLog.html TODO.html freedup.1.gz setup.hint version.h core freedup.o

distclean: clean
	rm -f freedup-$$(cat version)-src.tar.bz2 freedup-$$(cat version)-src.tgz
	rm -fr freedup-$$(cat version|cut -d- -f1)

#
# a long list of test routines follows
# The intention is to verify that at least some basic functions work well (hopefully)
#
test:	test0 test1 test2 test3 test4 test5 test6 test7 test10
alltest:	test0 test1 test2 test3 test4 test5 test6 test7 test8 test10

test0:	freedup
	@mkdir test
	@cp freedup test/test1
	@dd bs=1 count=$$(/bin/ls -l freedup | awk '{printf"%1.0f\n",$$5/2+0}') if=freedup of=test/test2 2>/dev/null
	@dd bs=1 count=$$(/bin/ls -l freedup | awk '{printf"%1.0f\n",$$5/3+0}') if=freedup of=test/test3 2>/dev/null
	@chmod 5777 test/test?
	@echo -e "Test prepared:\t\tSame Content - Different Size\t\t\c"
	@./freedup test >/dev/null 2>&1
	@[ $$(ls -i test/test1|awk '{print $$1}') -ne $$(ls -i test/test2|awk '{print $$1}') ] && \
	 [ $$(ls -i test/test2|awk '{print $$1}') -ne $$(ls -i test/test3|awk '{print $$1}') ] && \
	 echo "successful"
	@rm -r test

test1:	freedup
	@mkdir test
	@cp freedup test/test1
	@cp freedup test/test2
	@cp freedup test/test3
	@dd bs=1 count=$$(/bin/ls -l freedup | awk '{print $$5}') if=/dev/zero of=test/test4 2>/dev/null
	@dd bs=1 count=$$(/bin/ls -l freedup | awk '{print $$5}') if=/dev/zero of=test/test5 2>/dev/null
	@echo -e "Test prepared:\t\tSame Size - Different Content\t\t\c"
	@./freedup test >/dev/null 2>&1
	@[ $$(ls -i test/test1|awk '{print $$1}') -eq $$(ls -i test/test2|awk '{print $$1}') ] && \
	 [ $$(ls -i test/test2|awk '{print $$1}') -eq $$(ls -i test/test3|awk '{print $$1}') ] && \
	 [ $$(ls -i test/test4|awk '{print $$1}') -eq $$(ls -i test/test5|awk '{print $$1}') ] && \
	 [ $$(ls -i test/test1|awk '{print $$1}') -ne $$(ls -i test/test5|awk '{print $$1}') ] && \
	 echo "successful"
	@rm -r test

test2:	freedup
	@mkdir test
	@cp freedup test/test\ 1
	@cp freedup test/tes\ t\ 2
	@cp freedup test/t\ e\ s\ t\ 3
	@dd bs=1 count=$$(/bin/ls -l freedup | awk '{print $$5}') if=/dev/zero of=test/test\ 4 2>/dev/null
	@dd bs=1 count=$$(/bin/ls -l freedup | awk '{print $$5}') if=/dev/zero of=test/test\ 5 2>/dev/null
	@echo -e "Test prepared:\t\tSame Size - Spaces in Names\t\t\c"
	@./freedup test >/dev/null 2>&1
	@[ $$(ls -i test/test\ 1|awk '{print $$1}') -eq $$(ls -i test/tes\ t\ 2|awk '{print $$1}') ] && \
	 [ $$(ls -i test/test\ 1|awk '{print $$1}') -eq $$(ls -i test/t\ e\ s\ t\ 3|awk '{print $$1}') ] && \
	 [ $$(ls -i test/test\ 4|awk '{print $$1}') -eq $$(ls -i test/test\ 5|awk '{print $$1}') ] && \
	 [ $$(ls -i test/test\ 1|awk '{print $$1}') -ne $$(ls -i test/test\ 5|awk '{print $$1}') ] && \
	 echo "successful"
	@rm -r test

test3:	freedup
	@mkdir -p te\ st/te\ st/te\ st
	@cp freedup te\ st/test\ 1
	@cp freedup te\ st/te\ st/test\ 2
	@cp freedup te\ st/te\ st/te\ st/test\ 3
	@echo -e "Test prepared:\t\tSame Size - Spaces in Dirs\t\t\c"
	@./freedup te\ st >/dev/null 2>&1
	@[ $$(ls -i te\ st/test\ 1|awk '{print $$1}') -eq $$(ls -i te\ st/te\ st/test\ 2 |awk '{print $$1}') ] && \
	 [ $$(ls -i te\ st/test\ 1|awk '{print $$1}') -eq $$(ls -i te\ st/te\ st/te\ st/test\ 3 |awk '{print $$1}') ] && \
	 echo "successful"
	@rm -r te\ st

test4:	freedup
	@mkdir test
	@cp freedup test/test1
	@cp freedup test/test2
	@cp freedup test/test3
	@ln test/test1 test/test4
	@ln test/test2 test/test5
	@ln test/test3 test/test6
	@ln test/test1 test/test7
	@ln test/test1 test/test8
	@ln test/test2 test/test9
	@ln test/test3 test/testA
	@ln test/test2 test/testB
	@ln test/test1 test/testC
	@ln test/test3 test/testD
	@ln test/test3 test/testE
	@ln test/test3 test/testF
	@echo -e "Test prepared:\t\tSame File - Different Link Clusters\t\c"
	@./freedup test >/dev/null 2>&1
	@r="successful";m=$$(ls -i test/test1 |awk '{print $$1}') ; \
	 for n in test/test? ; do  [ $$(ls -i $$n |awk '{print $$1}') -eq $$m ] || r="failed" ; done ; echo $$r
	@rm -r test

test5:	freedup
	@mkdir test
	@cp freedup test/test1
	@cp freedup test/test2
	@cp freedup test/test3
	@echo -e "Test prepared:\t\tSame File - Testing standard input\t\c"
	@(echo test/test1 ; echo test/test2 ; echo test/test3 ; echo freedup ) | ./freedup >/dev/null 2>&1
	@r="successful";m=$$(ls -i test/test1 |awk '{print $$1}') ; \
	 for n in test/test? ; do  [ $$(ls -i $$n |awk '{print $$1}') -eq $$m ] || r="failed" ; done ; echo $$r
	@rm -r test

test6:	freedup
	@mkdir test1 test2
	@cp freedup test1/test1
	@cp freedup test1/test2
	@cp freedup test2/test3
	@cp freedup test2/test1
	@echo -e "Test prepared:\t\tSame File - Different Trees\t\t\c"
	@./freedup test1 test2 >/dev/null 2>&1
	@[ $$(ls -i test1/test1|awk '{print $$1}') -eq $$(ls -i test1/test2|awk '{print $$1}') ] && \
	 [ $$(ls -i test1/test2|awk '{print $$1}') -eq $$(ls -i test2/test3|awk '{print $$1}') ] && \
	 [ $$(ls -i test2/test1|awk '{print $$1}') -eq $$(ls -i test2/test3|awk '{print $$1}') ] && \
	 echo "successful" || echo "failed"
	@rm -r test1 test2

test7:	freedup
	@mkdir test7
	@touch test7/a test7/b test7/c test7/d
	@cp freedup test7/x
	@cp freedup test7/y
	@echo -e "Test prepared:\t\tEmpty Files - only link non-empty ones\t\c"
	@./freedup -0 test7 >/dev/null 2>&1
	@[ $$(ls -i test7/a |awk '{print $$1}') -ne $$(ls -i test7/b |awk '{print $$1}') ] && \
	 [ $$(ls -i test7/a |awk '{print $$1}') -ne $$(ls -i test7/c |awk '{print $$1}') ] && \
	 [ $$(ls -i test7/a |awk '{print $$1}') -ne $$(ls -i test7/d |awk '{print $$1}') ] && \
	 [ $$(ls -i test7/b |awk '{print $$1}') -ne $$(ls -i test7/c |awk '{print $$1}') ] && \
	 [ $$(ls -i test7/b |awk '{print $$1}') -ne $$(ls -i test7/d |awk '{print $$1}') ] && \
	 [ $$(ls -i test7/c |awk '{print $$1}') -ne $$(ls -i test7/d |awk '{print $$1}') ] && \
	 [ $$(ls -i test7/x |awk '{print $$1}') -eq $$(ls -i test7/y |awk '{print $$1}') ] && \
	 echo "successful" || echo "failed"
	@rm -r test7/y
	@cp freedup test7/y
	@echo -e "Test prepared:\t\tEmpty Files - also link empty ones\t\c"
	@./freedup test7 >/dev/null 2>&1
	@[ $$(ls -i test7/a |awk '{print $$1}') -eq $$(ls -i test7/b |awk '{print $$1}') ] && \
	 [ $$(ls -i test7/a |awk '{print $$1}') -eq $$(ls -i test7/c |awk '{print $$1}') ] && \
	 [ $$(ls -i test7/a |awk '{print $$1}') -eq $$(ls -i test7/d |awk '{print $$1}') ] && \
	 [ $$(ls -i test7/x |awk '{print $$1}') -eq $$(ls -i test7/y |awk '{print $$1}') ] && \
	 echo "successful" || echo "failed"
	@rm -r test7

test8:	freedup
	@if [ $$(df .|tail -1|awk '{print $$4}') -lt 4198400 ];then echo "Test lacks file space ... skipping";exit;fi
	@mkdir test
	@echo -e "Test is being prepared:\t\t\t\t\t please be patient\t\t"
	@dd if=/dev/zero bs=2050 count=1048576 of=test/test1 2>/dev/null
	@date >>test/test1
	@cp test/test1 test/test2
	@echo -e "Test prepared:\t\tBig Size - More than 2GB\t\t\c"
	@./freedup test >/dev/null 2>&1
	@[ $$(ls -i test/test1|awk '{print $$1}') -eq $$(ls -i test/test2 |awk '{print $$1}') ] && \
	 echo "successful"
	@rm -r test

test9:	freedup
	@mkdir -p te\:st/te\'st/te\&st
	@cp freedup te\:st/test\ 1
	@cp freedup te\:st/te\'st/test\`2
	@cp freedup te\:st/te\'st/te\&st/test3
	@echo -e "Test prepared:\t\tSame Size - Special Chars\t\t\c"
	@./freedup te\:st >/dev/null 2>&1
	@[ $$(ls -i te\:st/test\ 1|awk '{print $$1}') -eq $$(ls -i te\:st/te\'st/test\`2 |awk '{print $$1}') ] && \
	 [ $$(ls -i te\:st/test\ 1|awk '{print $$1}') -eq $$(ls -i te\:st/te\'st/te\&st/test3 |awk '{print $$1}') ] && \
	 echo "successful"
	@rm -r te\:st

test10:	freedup
	@mkdir test
	@cp freedup test/test1
	@cp freedup test/test2
	@cp freedup test/test3
	@cp freedup test/test4
	@echo -e "Test prepared:\t\tInteractive Mode - Link & Delete\t\c"
	@echo -e "13ac" | ./freedup -i test >/dev/null 2>&1
	@[ $$(ls -i test/test1|awk '{print $$1}') -eq $$(ls -i test/test3|awk '{print $$1}') ] && \
	 [ ! -f test/test2 ] && [ ! -f test/test4 ] && \
	 echo "successful" || echo "failed"
	@rm -r test

