#!/usr/bin/make -f

OLD=old
NEW=new
FINDOPTS=! -path '*/CVS*'
DIFFOPTS=-u

all: diff common removed added different.files same.files dir2file file2dir

old.files:
	(cd $(OLD); find -type f $(FINDOPTS)) | sort | cut -c3- > $@
old.dirs:
	(cd $(OLD); find -type d $(FINDOPTS)) | sort | tail -n +2 | cut -c3- > $@
new.files:
	(cd $(NEW); find -type f $(FINDOPTS)) | sort | cut -c3- > $@
new.dirs:
	(cd $(NEW); find -type d $(FINDOPTS)) | sort | tail -n +2 | cut -c3- > $@
old: old.files old.dirs
	sort -m old.files old.dirs > $@
new: new.files new.dirs
	sort -m new.files new.dirs > $@

entries: old new
	comm $^ > $@
common: entries
	-< $< kut 3 | grep -v '^$$' > $@
removed: entries
	-< $< kut 1 | grep -v '^$$' > $@
added: entries
	-< $< kut 2 | grep -v '^$$' > $@

dir2file: old.dirs new.files
	comm -1 -2 $^ > $@

file2dir: old.files new.dirs
	comm -1 -2 $^ > $@

comm.files: old.files new.files
	comm $^ > $@

comm.dirs: old.dirs new.dirs
	comm $^ > $@

common.files: comm.files
	-< $< kut 3 | grep -v '^$$' > $@

common.dirs: comm.dirs
	-< $< kut 3 | grep -v '^$$' > $@

diff: common.dirs common.files
	mkdir $@
	(cd $@; xargs mkdir) < common.dirs
	-for FILE in `< common.files`; do\
		diff $(DIFFOPTS) $(OLD)/$$FILE $(NEW)/$$FILE > diff/$$FILE;\
	done
	-find diff -size 0 | xargs rm 2>/dev/null
	-find diff -type d | sort -r | dock 1 | xargs rmdir 2>/dev/null

different.files: diff
	(cd $<; find -type f) | sort | cut -c3- > $@

same.files: common.files different.files
	comm -2 -3 $^ > $@
