#!/bin/sh -f

#
# finddup 1.00 - find identical files.
#
# Oleg Kibirev * April 1995 * oleg@gd.cs.CSUFresno.EDU
#
# This code is covered by General Public License, version 2 or any later
# version of your choice. You should recieve file "COPYING" which contains
# text of the license with any distribution of this program; if you don't 
# have it, a copy is available from ftp.gnu.ai.mit.edu.
# Patch by Guillem Jover (Thanks!!!). Then Hctor Garca && Amaya Rodrigo
# improved it, just because we are older and more picky:
# Also some contributions from Diego Alvarez <diego.alvarez@entelchile.net>
# Thanks for taking the time :-)
# Credit is also due to David Andel <andel@ifi.unizh.ch> and
# <deblists@osg.saic.com> for hardlink managing.

find . -xdev -type f -printf "%i %p\n" |

sort +0 |
uniq -W1 |
cut -f 2- -d ' ' |

tr '\n' '\0' | # To avoid problems with quotes and `_'.
xargs -0 md5sum |
sort +0 -1 |
sed -e 's/  / "/' -e 's/$/"/' |
(
psum='x'
line=''
many=''
while read -r sum file; do
  if [ "$sum" != "$psum" ]; then
    if [ ! -z "$many" ]; then
      eval set x $line
      k="`du "$2"`"
      shift 2
      echo $k "$@"
    fi
    line="$file"
    psum="$sum"
    many=''
  else
    line="$line $file"
    many=yes
  fi
done 

if [ ! -z "$many" ]; then
  eval set x $line
  k="`du "$2"`"
  shift 2
  echo $k "$@"
fi
) | sort +0 -1 -r -n | 
sed 's/['\''"\]/\\&/g' # Escape properly for xargs.
