#!/bin/sh
#
# STEALTH: Given the argument of a directory, will compare all textfiles
#          in that directory to your current working one. Bazing.

WORD=$$

if [ -n "$1" ]
   then

if [ "$1" = "." ]
   then
   echo "[%] Doing the same directory, looking for doubles within it."
fi

if [ ! -d TRASH ]
    then
    echo "[%] Making a Trash Bin. (./TRASH)"
    mkdir TRASH
fi

rm -f TRASH/.$WORD
echo "[%] Generating a Comparative Thingy."

for each in $1/*
    do
    if [ -f $each ]
       then
       KLACK="`wc -l $each | awk '{print $1}'`"
       echo "$each ($KLACK)"
       echo "$each ($KLACK)" >>TRASH/.$WORD
    fi
    done

echo "[%] Now doing the compares...."

for file in *
    do
    if [ ! -d $file ]
       then
       KLICK="`wc -l $file | awk '{print $1}'`"
       MATCHMAN="`grep \(${KLICK}\) TRASH/.$WORD | cut -f1 -d' ' `"
       echo "Comparing $file..."
       if [ -n "${MATCHMAN}" ]
          then
       for each in ${MATCHMAN}
           do
            if [ -f "$each" ]
               then
               KLACK="`wc -l $each | awk '{print $1}'`"
               if [ "$KLACK" = "$KLICK" ]
                 then
                 RENCE="`diff -btw $each $file | wc -l`"
                 if [ "$RENCE" -eq "0" ]
                    then
                    if [ "$1" = "." ]
                       then
                       if [ ! "$each" = "./$file" ]
                          then
                          ls -l $each
                          ls -l $file
                          echo "Found and Trashed."
                          mv $file TRASH
                          break
                       fi
                     else
                     ls -l $each
                     ls -l $file
                     echo "Found and Trashed."
                     mv $file TRASH
                     break
                    fi
                fi
           fi
           else
           echo "[%] Oops... $each was already deleted. Can't compare to it."
           fi
           done
       fi
    fi
    done

    else
    echo "You have to say what directory we're comparing to."
fi
