#!/bin/bash --norc
##
##  Title:	Find Duplicate Update RPMS and Move to Separate Directory
##  Name:	fduprpm
##  Usage:	fduprpm <rpm dir>
##  Version:	0.0.1
##  Date:	7/27/09, 1:24 PM
##  Author:	David C. Rankin, J.D.,P.E
##  Summary:
##
##  Run as:
##  Requires:
##
##
#. /home/david/linux/scripts/include/general.inc
#. /home/david/linux/scripts/include/colors.inc
##
#

usage() {
	echo -e "\n\tUsage: ${0##/*} <search dir> [dup dir]\n"
	echo -e "\tSearches <search dir> for duplicate RPMS and moves duplicate files to [dup dir]"
	echo -e "\tor <search dir>/duplicates by default.\n"
	exit 1
}

[[ ! -d $1 ]] && usage

## parse input search dir and strip trailing / and set SEARCHDIR
LNGTH=${#1}
LSTCHAR=${1:$LNGTH-1}

if [[ $LSTCHAR == / ]]; then
#     SEARCHDIR=${1:0:$LNGTH-1}  # alternate method to strip lash char
    SEARCHDIR=${1%/*}
else
    SEARCHDIR=$1
fi

## set up log filename

LOGFILE=./${SEARCHDIR}.dup.log
[[ ! -w $LOGFILE ]] && touch $LOGFILE
[[ ! -w $LOGFILE ]] && { echo "Unable to create log file $LOGFILE, exiting..."; exit 1; }
echo -e "\n\t$(date '+%F')  Find/Remove Duplicates in: ${SEARCHDIR}\n" | tee -a $LOGFILE

## set the duplicates directory name or default to "SEARCHDIR/duplicates"

DUPDIR=${2:-$SEARCHDIR/duplicates}
declare -a SARRAY DUPARRAY
SARRAY=( $(ls $SEARCHDIR) )
mkdir -p $DUPDIR
echo "SEARCHDIR: $SEARCHDIR  DUPDIR: $DUPDIR"

## cyrus-sasl-crammd5-2.1.22-140.1_140.2.x86_64.delta.rpm
##                            ufraw-0.15-10.11.x86_64.rpm

for ((i=0;i<${#SARRAY[@]};i++)); do
    echo "\${SARRAY[$i]} ${SARRAY[$i]}"

    ## set current search file variable (SFILE) to enable parameter substitution
    SFILE=${SARRAY[$i]}

    ## skip subdirectories
    [[ -d $SEARCHDIR/${SARRAY[$i]} ]] && { echo "    skipping dir ${SARRAY[$i]}"; continue; }
    ## skip nonrpm
    [[ ${SFILE##*.} != rpm ]] && { echo "    skipping nonrpm ${SFILE}"; continue; }

    ## get the base file name (SFNAME)
    SFNAME=${SFILE%%.*}
    SFNAME=${SFNAME%-*}

    ## get Search rpm archetecture
    if [[ $SFILE =~ '.delta' ]]; then
      TMP=${SFILE%.delta.rpm*}
    else
      TMP=${SFILE%.rpm*}
    fi
    SARCH=${TMP##*.}

    ## Could also be written as
#     [[ $SFILE =~ '.delta' ]] && TMP=${SFILE%.delta.rpm*} || TMP=${SFILE%.rpm*}
#     SARCH=${TMP##*.}

    ## fill the duplicate array, eliminate all non-duplicate variations of a file
#     echo "\$SEARCHDIR/\$SFILE: ${SEARCHDIR}/${SFILE}"
    ## duplicates array index
    DUPNO=0
    ## step through list of potential duplicates and eliminate non-dupes
    for k in $(ls -t ${SEARCHDIR}/${SFNAME}*); do

      ## Get duplicate architecture
      if [[ $k =~ '.delta' ]]; then
	TMP=${k%.delta.rpm*}
      else
	TMP=${k%.rpm*}
      fi
      DARCH=${TMP##*.}

      ## Get duplicates filename
      TMP=${k##*/}
      TMP=${TMP%%.*}
      TMP=${TMP%-*}

      ## if actual duplicate, then add it to the duplicates array
      [[ $TMP == $SFNAME ]] && [[ $SARCH == $DARCH ]] && {
	if [[ $DUPNO -eq 0 ]]; then
	  echo "Keeping:  $k" | tee -a $LOGFILE
	else
	  echo "$k  -->  $DUPDIR" | tee -a $LOGFILE
	  mv $k $DUPDIR
	fi
	  ((DUPNO+=1))
      }
    done

    ## advance to the next rpm
    ((i+=DUPNO-1))


done

exit 0
