#!/bin/sh
#
# UNIVERSE: Check all current files against what's already there. OOF

WORD=/textfiles/universe

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

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


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

for file in *
    do
    if [ ! -d $file ]
       then
       KLICK="`wc -l $file | awk '{print $1}'`"
       MATCHMAN=`grep -w ${KLICK} /textfiles/universe | awk '{print $2}'`
       echo "Comparing $file ($KLICK Lines)..."
       if [ -n "${MATCHMAN}" ]
          then
       for each in ${MATCHMAN}
           do
            if [  -f /textfiles/$each  -a  -f $file ]
               then
                 RENCE="`diff -btw /textfiles/$each $file | wc -l`"
#                 echo "Compared  /textfiles/$each : $file : $RENCE"
                  if [ "$RENCE" -eq "0" ]
                     then
                     if [ "$1" = "." ]
                       then
                       if [ ! "/textfiles/$each" = "./$file" ]
                          then
                          if [ ! "/textfiles/$each" -ef "./$file" ]
                             then
                          ls -l /textfiles/$each
                          ls -l $file
                          echo "Found and Trashed."
                          mv $file TRASH
                          break
                         fi
                       fi
                     else
                     if [ ! "/textfiles/$each" -ef "./$file" ]
                    then
                     ls -l /textfiles/$each
                     ls -l $file
                     echo "Found and Trashed."
                     mv $file TRASH
                    fi
                     fi
                     else
                     if [ "$RENCE" -le "15" -a "$RENCE" -ge "5" ]
                        then
                        ls -l /textfiles/$each
                        ls -l $file
                        echo "Hmmm. REALLY close match."
                        mv $file MAYBE
                     fi
                 fi                 
           fi
           done
       fi
    fi
    done
