SUFFIX=.gz .bz2 .tar.bz2 .tgz .tar
.PHONY: distclean clean rpm hashtest tarball test

#
# OS specific definitions
#
#the following two lines will most likely fail for non-GNU make
#in this case please replace the lines by the contents of the included file
include Makefile.$(shell uname -s)
HTML2TEXT = /usr/bin/html2text
CFLAGS=-O2 -Wall -pedantic -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 $(DEFS) -std=gnu99
CFLAGS=-g3 -Wall -pedantic -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 $(DEFS) -std=gnu99

#
# INSTALLDIR indicates directory where program is to be installed.
# Suggested values are "/usr/local/bin" or "/usr/bin".
#
INSTALL = install
INSTALLDIR = $(PRE)/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 = $(PRE)/usr/share/man

#
# VERSION determines the program's version number.
#
VERSION = "$$(cat version)"
MAKFILES= Makefile Makefile.tests Makefile.Linux \
	  Makefile.CYGWIN_NT-5.1 Makefile.AIX Makefile.BSD
SRCFILES= freedup.c freedup.h sha1.c sha1.h config.c config.h \
	  my.c mp3.c mp4.c mpc.c jpg.c ogg.c auto.c \
	  my.h mp3.h mp4.h mpc.h jpg.h ogg.h auto.h
TARFILES= $(MAKFILES) demo $(SRCFILES) README.SHA \
	  freedup freedup.1 freedup.spec freedup.xinetd \
	  ChangeLog TODO README rules copyright COPYING verify
HTMLFILES=intro.html syntax.html download.html process.html \
	  examples.html hint.html questions.html offer.html
DOCFILES= freedup.css index.html foot.html head.html $(HTMLFILES)
OBJFILES= freedup.o config.o sha1.o mp3.o mp4.o mpc.o jpg.o my.o ogg.o auto.o

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

tags:	$(SRCFILES)
	ctags $(SRCFILES)
#
# ALL that needs/should be done
#
test:	Makefile.tests stest
	$(MAKE) -f $<

stest: hashtest	shatest

verify:	Makefile Makefile.$(shell uname -s)
	@echo "#!$$(which make) -f " >verify
	@sed "s:./freedup:$(INSTALLDIR)/freedup:" Makefile.$(shell uname -s) >>verify
	@sed -e '/^include/d' -e 's/:[ 	]*freedup/:/' Makefile.tests >>verify
	@chmod a+x verify

#
# INSTALLATION without using RPM
#
install: freedup freedup.1.gz
	[ "./freedup" = $(FREEDUPEXE) ] || cp $(FREEDUPEXE) freedup
	$(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 mp3.h jpg.h my.h ogg.h
freedup: $(OBJFILES)


#
# 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
	$(ECHOE) "#define VERSION\t\"$$(cat version)\"" >version.h

#
# An attempt to produce a single file documentation
#
README.html: head.html $(DOCFILES)
	-@rm -f $@
	sed -n '1,/<.BODY>/p' head.html >$@
	for n in $(HTMLFILES); do \
	sed -n '/<BODY/,/<.BODY>/p' $$n | grep -v 'BODY' >>$@ ; done
	$(ECHOE) "</BODY>\n</HTML>">>$@

README:	README.html
	@echo "Converting $< into a plain text file called $@."
	@if [ -x $(HTML2TEXT) ] ; then \
	$(ECHOE) " FFFFF RRRRR   EEEEE  EEEEE  DDDD   UU UU  PPPPP" >$@ ;\
	$(ECHOE) " FF    RR  RR  EE     EE     DD DD  UU UU  PP  PP" >>$@ ;\
	$(ECHOE) " FFFF  RRRR    EEEEE  EEEEE  DD DD  UU UU  PPPPP" >>$@ ;\
	$(ECHOE) " FF    RR RR   EE     EE     DD DD  UU UU  PP" >>$@ ;\
	$(ECHOE) " FF    RR  RR  EEEEE  EEEEE  DDDD    UUU   PP" >>$@ ;\
	$(HTML2TEXT) -nobs $< |\
	sed -e '1,3d' -e '5s:FREEDUP *::' -e '6s: :
- :g' \
	    -e 's:\*\{5\}\(.*\)\*\{5\}:
#####
#####
#####    \1
#####
#####
:' \
	    -e 's:\*\{4\}\(.*\)\*\{4\}:
\*\*\*\*\*
\*\*\*\*\* \1
\*\*\*\*\*
:' |\
	tr '\015' '\012' >> $@ ;\
	else echo "see README.html">$@ ;fi

changelog: freedup.spec version
	(echo "freedup ($$(cat version)) unstable; urgency=low"; echo) >changelog
	@sed -n '/^%changelog/,$$p' freedup.spec | grep -v '^%changelog' |\
	awk '{if($$1=="*")author="\n -- "$$6" "$$7" "$$8"  "$$2", "$$4" "$$3" "$$5" 06:00:00 +0100\n"; if($$1=="-" || $$1=="+") print " "$$0"."; if(NF==0){print author}}' |\
	sed -e 's: - :  * :g'>>changelog

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


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

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

%.html:	html/%.html
	ln $< $@

%.css:	html/%.css
	ln $< $@

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

%.html: %
	@echo "Generating $@ from plain text file $<."
	@($(ECHOE) "<?XML version=\"1.0\" encoding=\"utf-8\"?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<HTML lang=\"en\" xml:lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">\n<HEAD>\n<TITLE>Freedup $< </TITLE>\n<META HTTP-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<META NAME=\"KEYWORDS\" content=\"Freedup, systems management, tool, link, $<\" />\n<META HTTP-EQUIV=\"expires\" content=\"Fri, 31 Dec 2010 19:00:00 CET\" />\n<LINK REL=\"stylesheet\" type=\"text/css\" href=\"freedup.css\" />\n</HEAD>\n<BODY BGCOLOR=\"#DDDDFF\">\n<PRE>" ; sed -e 's:^$$:</PRE><P><PRE>:' $< ; $(ECHOE) "</PRE>\n</BODY>\n</HTML>") >$@

#
# generate the setup.hint file for CYGWIN automatically
#
SDESC=reduces the amount of used space by linking duplicate files.
LDESC=Freedup finds and eliminates duplicate files by linking them (aka deduplication), and thus reduces the amount of used disk space within one or more file systems. By default, hardlinks are used on a single device, symbolic links when the devices differ. A set of options allows you to modify the methods of file comparison, the hash functions, the linking behavior, and the reporting style. You may use batch or interactive mode. Freedup usually only considers identical files, but when comparing audio or graphics files, you may elect to ignore the tags.

setup.hint: $(MAKFILES) 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 $(SDESC)" >>setup.hint
	@echo "ldesc: $(LDESC)" >>setup.hint
	@echo "curr: $$(cat version)" >>setup.hint

DEBIAN=	control compat changelog
debian: $(DEBIAN) rules copyright
	@mkdir -p $@
	mv $(DEBIAN) $@
	cp rules copyright $@
	dpkg-buildpackage -d

control:
	@echo "Source: freedup" >control
	@echo "Section: unknown" >>control
	@echo "Priority: optional" >>control
	@echo "Maintainer: AN <AN@freedup.org>" >>control
	@echo "Build-Depends: debhelper (>= 3.0.0)" >>control
	@echo "Standards-Version: 3.5.2" >>control
	@echo "" >>control
	@echo "Package: freedup" >>control
	@echo 'Depends: $${shlibs:Depends}, $${misc:Depends}' >>control
	@echo "Architecture: $$(uname -i)" >>control
	@echo "Description: $(SDESC)" >>control
	@echo " $(LDESC)" >>control

compat:
	echo "4" >compat

#
# generate the distribution tarballs
# (avoid calling this - use make rpm instead)
#
tarball:	freedup version README ChangeLog freedup.1 $(TARFILES)
	[ "./freedup" = $(FREEDUPEXE) ] || cp $(FREEDUPEXE) freedup
	strip freedup
	if [ -d freedup-$$(cat version|cut -d- -f1) ] ; then exit -1 ;fi
	mkdir freedup-$$(cat version|cut -d- -f1)
	cp -r $(TARFILES) freedup-$$(cat version|cut -d- -f1)
	mkdir freedup-$$(cat version|cut -d- -f1)/html
	cp $(DOCFILES) freedup-$$(cat version|cut -d- -f1)/html
	tar cjf $(RPMBUILD)/SOURCES/freedup-$$(cat version)-src.tar.bz2 \
	freedup-$$(cat version|cut -d- -f1)
	cp $(RPMBUILD)/SOURCES/freedup-$$(cat version)-src.tar.bz2 .
	tar czf $(RPMBUILD)/SOURCES/freedup-$$(cat version)-src.tgz \
	freedup-$$(cat version|cut -d- -f1)

#
# generate the own distribution RPM file
#
rpm:	tarball verify
	rpmbuild -ba --sign freedup.spec
	cp $(RPMBUILD)/RPMS/i586/freedup-$$(cat version)* .
	cp $(RPMBUILD)/SRPMS/freedup-$$(cat version)* .

#
# some sanity run definitions
#
clean:	
	rm -f $(OBJFILES) freedup freedup.1.gz\
		ChangeLog ChangeLog.html changelog TODO.html \
		setup.hint version.h control core compat

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

#
# development tests
#
oggtest: ogg.c
	@gcc -Wall -g3 -DOGG_TEST -o oggtest ogg.c

jpgtest: jpg.c
	@gcc -Wall -g3 -DJPG_TEST -o jpgtest jpg.c

mp3test: mp3.c
	@gcc -Wall -g3 -DMP3_TEST -o mp3test mp3.c

mp4test: mp4.c
	@gcc -Wall -g3 -DMP4_TEST -o mp4test mp4.c

mpctest: mpc.c
	@gcc -Wall -g3 -DMPC_TEST -o mpctest mpc.c

autotest.o: auto.c
	gcc -Wall -g3 -DAUTO_TEST -o autotest.o -c auto.c

autotest: autotest.o mp3.o mp4.o mpc.o jpg.o ogg.o my.o 
	gcc -Wall -g3 -o autotest  autotest.o mp3.o mp4.o mpc.o jpg.o ogg.o my.o

sha1test: sha1.c
	@gcc -Wall -g3 -DSHA1_TEST -o sha1test sha1.c

shatest: sha1test
	@mkdir -p test11
	@$(ECHOE) "Test prepared:\t\tHash Method - internal: SHA1 by A.Saddi\t\c"
	@$(SHATESTEXE) >test11/output
	@$(ECHOE) "a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d" \
		  "\n84983e44 1c3bd26e baae4aa1 f95129e5 e54670f1" \
		  "\n34aa973c d4c4daa4 f61eeb2b dbad2731 6534016f ">test11/expected
	@diff test11/output test11/expected >/dev/null && \
	 echo "[32msuccessful[0m" || echo "[31mfailed[0m"
	@rm -r test11 $(SHATESTEXE)


hashtest:
	@sum="$$(grep '^#.*define *SHA512SUM' freedup.h|cut -d\" -f2)" ; if [ -n "$$sum" ] ; then\
	 if [ -x $$sum ] ; then $(ECHOE) "External program\t\"$$sum\" [32m\t is present and executable[0m" ; else\
	 $(ECHOE) "External program\t\"$$sum\" [31m\t is missing/not executable[0m " ; fi  ;fi
	@sum="$$(grep '^#.*define *SHA384SUM' freedup.h|cut -d\" -f2)" ; if [ -n "$$sum" ] ; then\
	 if [ -x $$sum ] ; then $(ECHOE) "External program\t\"$$sum\" [32m\t is present and executable[0m" ; else\
	 $(ECHOE) "External program\t\"$$sum\" [31m\t is missing/not executable[0m " ; fi ; fi
	@sum="$$(grep '^#.*define *SHA256SUM' freedup.h|cut -d\" -f2)" ; if [ -n "$$sum" ] ; then\
	 if [ -x $$sum ] ; then $(ECHOE) "External program\t\"$$sum\" [32m\t is present and executable[0m" ; else\
	 $(ECHOE) "External program\t\"$$sum\" [31m\t is missing/not executable[0m " ; fi ; fi
	@sum="$$(grep '^#.*define *SHA224SUM' freedup.h|cut -d\" -f2)" ; if [ -n "$$sum" ] ; then\
	 if [ -x $$sum ] ; then $(ECHOE) "External program\t\"$$sum\" [32m\t is present and executable[0m" ; else\
	 $(ECHOE) "External program\t\"$$sum\" [31m\t is missing/not executable[0m " ; fi ; fi
	@sum="$$(grep '^#.*define *SHA1SUM' freedup.h|cut -d\" -f2)" ; if [ -n "$$sum" ] ; then\
	 if [ -x $$sum ] ; then $(ECHOE) "External program\t\"$$sum\" [32m\t is present and executable[0m" ; else\
	 $(ECHOE) "External program\t\"$$sum\" [31m\t is missing/not executable[0m " ; fi ; fi
	@sum="$$(grep '^#.*define *MD5SUM' freedup.h|cut -d\" -f2)" ; if [ -n "$$sum" ] ; then\
	 if [ -x $$sum ] ; then $(ECHOE) "External program\t\"$$sum\" [32m\t is present and executable[0m" ; else\
	 $(ECHOE) "External program\t\"$$sum\" [31m\t is missing/not executable[0m " ; fi ; fi
	@sum="$$(grep '^#.*define *SUM' freedup.h|cut -d\" -f2)" ; if [ -n "$$sum" ] ; then\
	 if [ -x $$sum ] ; then $(ECHOE) "External program\t\"$$sum\" [32m\t\t is present and executable[0m" ; else\
	 $(ECHOE) "External program\t\"$$sum\" [31m\t\t is missing/not executable[0m " ; fi ; fi

