#!/bin/bash

#########################################################################
## Find-NonRPM (find-nonrpm) -- Version 1.0 -- 4/30/97
#########################################################################

#########################################################################
## This was written by Kirk Bauer, 4/30/97
## 
## This script looks in all the directories listed and looks for files
## that do *not* belong to a package.
##
## Also, the '--double' switch will list files that are owned by
## more than one package.
##
## If you have *ANY* questions/comments/suggestions/complements/etc
## *please* send them to me.  One of the reasons I release scripts to
## people is to get positive feedback.  You can contact me through one
## of the following email addresses:
##    kirk@kaybee.org
##		kirk@gt.ed.net
##    gt5918a@prism.gatech.edu
##
## Revision History:
##     4/30/97 Version 1.0 -- Initial Release
##
#########################################################################

do_file() {
   rpm -qf $1 >/dev/null 2>/dev/null
   if [ $? != 0 ] ; then
      echo $1
   fi
}

do_loop() {
   for i in $*
   do
      if [ ! -z $RECURSIVE ] ; then
         if [ -d $i ] ; then
            for j in `find $i -path "$EXCLUDE" -prune -o -print`
            do
               do_file $j
            done
         else
            do_file $i
         fi
      else
         do_file $i
      fi
   done
}

if [ $# = 0 ] ; then
   echo "Usage:"
   echo "   $0 --double: checks for files owned by more than one package."
   echo "   $0 [-r] [--exclude \"<dirname> <dirname>...\"]"
   echo "                      <filename|dirname> <filename|dirname> ....."
   echo "      Checks every file and directory listed and lists any that"
   echo "      don't belong to a package.  The -r is for recursive, and this"
   echo "      script will then follow those directories until they end."
   echo "      --exclude must be followed by a list of dirs to ignore"
   echo '        *enclosed in quotes!* (i.e. they have to be 1 argument)'
   echo
   echo "Example:  $0 -r --exclude \"/usr/local\" /usr"
   echo
elif [ "$1" = "--double" ] ; then
   rpm -qal | sort | uniq -d
elif [ "$1" = "--exclude" ] ; then
   shift
   EXCLUDE="$1"
   shift
   if [ "$1" = "-r" ] ; then
      RECURSIVE="Y"
      shift
   fi
   do_loop $*
elif [ "$1" = "-r" ] ; then
   RECURSIVE="Y"
   shift
   if [ "$1" = "--exclude" ] ; then
      shift
      EXCLUDE="$1"
      shift
   fi
   do_loop $*
else
   do_loop $*
fi
