Setting the right directory as non-empty

Hi guys,

I've been trying to get this part of my script to work for ages and now it's getting me annoyed!! So i thought a fresh group of eyes could see where i'm going wrong!

The idea is getting a directory and all its files moved to a temp folder, but similar to the rm -ir command where it asks if i want certain files/direcories moved etc.

Now the case i have is this: i've already said no to a file i don't want moved. Then when the process comes to the directory that this file is in and asks to move the directory, if i say yes then it'll tell me that i can't as it's non-empty. Then continues with the rest of the task.

I've got it working to a degree, but it takes the first non-empty directory, and any child directory is automatically set as non-empty too, even if they're not. Ideally it should set the lowest non-empty child directory, as then all the parent directories would automatically be set as non-empty too.

Here's the code i'm stuck with anyway:

function directoryDelete () {

if [[ -d "$1"  &&  "$FLAG_R" == "R" && "$FLAG_F_I" == "i" ]] ; then
   echo -n "saferm: descend into directory \`$1'?"
   read ANSWER
   if [[ "$ANSWER" = [Yy] ]] ; then
      echo  "removing all entries in directory \`$1'"
      for FILE in $1/*
      do
         if [ -d $FILE ] ; then
            directoryDelete $FILE
         else
            fileDelete $FILE
         fi
      done
      if [ "$DIR_NOT_EMPTY" = "y" ] ; then
         echo -n "saferm: remove directory \`$1' (might be nonempty)?"
         read ANSWER
         if [[ "$ANSWER" = [Yy] ]] ; then
            echo "saferm: cannot remove directory \`$1': Directory non-empty"
         fi
      else
         echo -n "saferm: remove directory \`$1'?"
         read ANSWER
         if [[ "$ANSWER" = [Yy] ]] ; then
            mv  $1 $TRASH 2>/dev/null
            echo "saferm: removing directory itself: \`$1'"
         fi
      fi
   fi
else
   fileDelete $1
fi
}


function fileDelete() {

if [ "$FLAG_F_I" = "i" ] &&  [ -w  "$1" ] ; then
   interactive $1
elif [ "$FLAG_F_I" = "f"  ] ; then
   force $1
elif [ "$FLAG_R" = "R"  ] ; then
   remove $1
else
   remove $1
fi
}

function interactive () {
echo -n "saferm: remove $1 ?"
read ANSWER
if [[ "$ANSWER" = [Yy] ]] ; then
   remove $1
else
   DIR_NOT_EMPTY=y
fi
}

Apologies if it's not clear, it's the interactive bit i'm stuck on. Any help would get this done and let my brain rest!!

Many thanks in advance

Oliver