#!/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:

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

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

# End of patch.

tr '\n' '\0' | # To avoid problems with quotes and `_'.
xargs -0 md5sum |
sort +0 -1 |
(
psum='x'
line=''
many=''
while read -r sum file; do
  if [ "$sum" != "$psum" ]; then
    if [ ! -z "$many" ]; then
      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
  set x $line
  k="`du $2`"
  shift 2
  echo $k "$@"
fi
) | sort +0 -1 -r -n | sed 's/['\''"\]/\\&/g' # Escape properly for xargs.
