#!/bin/bash
#set -x
#  Copyright (c) 2010 Fizians SAS. <http://www.fizians.com>
#  This file is part of Rozofs.
#  Rozofs is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published
#  by the Free Software Foundation, version 2.
#  Rozofs is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.

#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see
#  <http://www.gnu.org/licenses/>.

#______________________________________
syntax () {

  case "$1" in
    "");;
    *) echo  $1;;
  esac   
  echo " "
  printf "                         -- \033[1m ROZOFS TRASH Utility  \033[0m --\n\n"
  echo " "
  printf " \033[1mrozo_trash <enable|recursive|disable> [pathname]\033[0m  \tEnable/disable trash on directory pathname (default pathname is \".\").\n"
  echo " "
  printf " \033[1mrozo_trash root <enable|disable> [pathname]\033[0m  \t\tEnable/disable root trash on directory pathname (default pathname is \".\").\n"
  echo " "
  printf "\033[1m rozo_trash status [pathname]\033[0m            \t\tQuery trash status for a directory pathname (default pathname is \".\").\n"
  echo " "
  printf " \033[1mrozo_trash help  \033[0m                        \t\tThis help.\n"
  echo " "

  exit 

}
#______________________________________


trash_ls () {
  cd @rozofs-trash@/
  case "$1" in
   "") ls -ls ;; 
   *)  {
         if [ -d $1 ] 
	 then 
	 cd $1/@rozofs-del@/
	 ls -ls 
	 else
	 ls -ls *$1
	 fi
	 
       };;
  esac
  cd .. 2>/dev/null
}

#______________________________________
#
# rozo_trash <enable| disable|recursive| status> [pathname]
#  set/reset trash mode on the current directory or the directory given in $2
#
trash_status () {
  case "$2" in
    "") dir=".";;
    *) dir=$2;;
  esac 
  case "$1" in
    "enable") attr -s rozofs -V "trash=1" $dir > /dev/null;;
    "disable") attr -s rozofs -V "trash=0" $dir > /dev/null;;
    "recursive") attr -s rozofs -V "trash=2" $dir > /dev/null;;
  esac   
  trash_status_display $dir
}

#______________________________________
#
# rozo_trash <enable| disable|recursive| status> [pathname]
#  set/reset trash mode on the current directory or the directory given in $2
#
trash_status_display () {
  case "$1" in
    "") dir=".";;
    *) dir=$1;;
  esac 
     
  res=`attr -g rozofs $dir | awk -F':' '{if ($1=="TRASH   ") print $2;}'`
  case "$res" in 
    " YES") echo "Trash is enabled on $dir";;
    " YES (RECURSIVE)") echo "Trash is enabled in recursive mode on $dir";;
    *)    echo "Trash is disabled on $dir";;
 esac
  res=`attr -g rozofs $dir | awk '{if ($1=="R_TRASH") print $3;}'`
  case "$res" in 
    "YES") echo "Root Trash is enabled on $dir";;
    *)    echo "Root Trash is disabled on $dir";;
 esac
}

#______________________________________
#
# rozo_trash root <enable| disable | status> [pathname]
#  set/reset trash mode on the current directory or the directory given in $2
#
trash_root () {
  case "$2" in
    "") dir=".";;
    *) dir=$2;;
  esac 
  case "$1" in
    "enable") attr -s rozofs -V "root-trash=1" $dir > /dev/null;;
    "disable") attr -s rozofs -V "root-trash=0" $dir > /dev/null;;
  esac   
  trash_status_display $dir
}
#______________________________________
#
# rozo_trash purge 
#

trash_purge_internal () {
  dir=$1
  res=$2
  res1=$2
  local f=""
  cd $dir/@rozofs-trash@/

  for f in  ` ls -d $res `
  do
    echo "$f "
    if [ -d $f ]
    then
     trash_purge_internal $f "$2"
     rmdir $f
    else
     rm -f $f  
    fi 
  done
  cd ../..


}
trash_purge () {

  printf "enter a filename or wildcard: "
  read res
  trash_purge_internal "." $res
  echo "Done."
}
#______________________________________
#
# rozo_trash rm <path> 
#
trash_rm () {
  cd @rozofs-trash@/
  case "$1" in 
    "") syntax "Restore requires a parameter";;
  esac 
  for f in  ` ls *$1*`
  do
    echo "remove $f (y/n) ?"
    read res
    case "$res" in
    y|Y|yes|YES) rm -rf $f;;
    esac
   
  done
   cd ..
}
#______________________________________
#
#  restore the content of a deleted directory
trash_restore_directory(){
  src=$1
  dst=$2
  
  dir_date=`echo $src | awk -F'@' '{ print $2}'`
  cd $dst  
  for f in `ls @rozofs-trash@/@$dir_date@@* ` 
  do
    dfname=`echo $f | awk -F'@' '{ print $6}'`
    sfname=`echo $f | awk -F'/' '{ print $2}'`
    trash_restore $sfname $dfname
  done
}
#______________________________________
#
# rozo_trash restore <source> <destination> 
#
trash_restore() {

  src=$1
  shift 1
  case "$src" in 
    "") syntax "Restore requires a source file name";;
  esac  

  if [ ! -e @rozofs-trash@/$src ];
  then
     syntax "Source file name \"$src\" does not exist"
  fi
  dst=$1
  shift 1
  case "$dst" in 
    "") syntax "Restore requires a destination file name";;
  esac  

  if [ -e $dst ];
  then
     syntax "Destination file name \"$dst\" already exists"
  fi  
# restore the requested object  
  mv @rozofs-trash@/$src $dst
# check if a full directory has to be restored
  if [  -d $dst ];
  then
     trash_restore_directory  $src $dst
  fi
}
#______________________________________

case "$1" in 
  "-v") set -x; shift 1;;
esac

cmd=$1
shift 1

case "$cmd" in
#  ls)    trash_ls  "$*" 2>/dev/null;;
#  purge) trash_purge 2>/dev/null;;
#  rm)    trash_rm "$*" 2>/dev/null;;  
#  restore) trash_restore $*  2>/dev/null;;
  enable) trash_status enable $1  2>/dev/null;;  
  recursive) trash_status recursive $1  2>/dev/null;;  
  disable) trash_status disable $1 2>/dev/null;;  
  status) trash_status_display  $1 2>/dev/null ;;  
  root) trash_root  $1 $2 2>/dev/null;;  

#  dir) trash_restore_directory  $1 $2 2>/dev/null;;  
  *) syntax;;
esac

