#!/bin/bash



#Variables



#Is mail enabled?  Do we want a notice? 0=off 1=on

MAILER=1



#Our admins email

ADMINEMAIL=someone@somewhere.change



#Should we delete old files? 0=off 1=on

DELETEME=1



#How old should the files be in days?  Default 7 days

OLDAGE=7



#Where to hold the files

HOLDING=\"/usr/local/holding-cell/\"



#What files to find

FILES='*.mp3'



#Do we update our database or is it in cron? 0=off 1=on

UPDATED=1



#/Variables 



OURDATE=`date +%Y-%m-%d-%H`



#Check and see if our holding area exists and create it

if [ ! -d $HOLDING ]

then

  echo \"Holding area not found, creating $HOLDING\"

  mkdir -p $HOLDING

fi



#Update our database if it's not in cron so we find new files quickly

if [ $UPDATED == 1 ]

then

  echo \"Updating our file database this may take some time, disable if this is in a cron task\"

  updatedb

fi



#Make a list of our files, excluding the ones in jail already

locate -i $FILES | grep -v $HOLDING > $HOLDING/found-$OURDATE



#Email the list to our admin for review

if [ $MAILER == 1 ]

then 

  echo \"Emailing files list to admin\"

  cat $HOLDING/found-$OURDATE | mail -s\"Found $FILES on `hostname` $OURDATE\" $ADMINEMAIL

fi



#Run our list of files

for i in `cat $HOLDING/found-$OURDATE`

do



#Make sure it really is a file and we didn't screw up our $FILES

  if [ -f $i ]

  then

    

#Who's your daddy, Get our users name or number

BADUSER=`ls -l $i | awk '{print $3}'`



#See if they have been bad before

    if [ ! -d $HOLDING/$BADUSER ]

    then

      echo \"Creating a holding cell for $BADUSER\"

      mkdir -p $HOLDING/$BADUSER

    else

      echo \"Holding cell exists for $BADUSER\"

    fi



#Touch the file, this changes the mtime so we can track how long it's been in holding

    touch $i



#Move the files, making a backup if they exist already with the same name

    echo \"Moving $i\"

    mv --backup=numbered $i $HOLDING/$BADUSER/



  fi

done



#Find old files and prune them, if selected

if [ $DELETEME == 1 ]

then



#Make sure $OLDAGE is set and is a number so we don't user error something

  if [ $OLDAGE -gt 0 ]

  then

    for i in `find $HOLDING/. -mtime +$OLDAGE -type f`

    do

      echo \"deleting $i\"

      rm -f $i

    done

  fi



fi



exit 0