Newer
Older
trm / trm.sh
@tundra tundra on 27 Oct 2016 3 KB added help screen
#!/bin/sh
# trm.sh - Safer 'rm' And Backup Utility
# Copyright (c) 2016 TundraWare Inc., Des Plaines, IL USA
# All Rights Reserved
# Permission granted for the free use of this program without restriction

GRAVEYARD="${HOME}/.graveyard"
INTERPROMPT="Do You Want To Remove(Copy):"
TESTING="Test Mode ..."

mkdir -p $GRAVEYARD


#####
# Display usage information
#####

trm_usage()
{
  echo "usage: trm.sh -cehistvx -g graveyard [file|dir] ..."
  echo "       where,"
  echo "              -c            copy targets to graveyard, don't remove them"
  echo "              -e            empty the current graveyard (permanent removal"
  echo "              -h            display this help screen"
  echo "              -g graveyard  use named graveyard instead of default"
  echo "              -i            interactive removal/copy"
  echo "              -s            don't generate serial number suffixes"
  echo "              -t            test mode, just show what would be done"
  echo "              -v            verbose mode - noisy output"
  echo "              -x            execute, overrides previous -t"

}


#####
# This function is called at the bottom of this file or it can
# be embedded in a shell startup script.
#####

trm()
{

  # Parse command line args

  OPTLIST='ceg:histvx' # List of all legal command line options
  OPTIND=1             # in case getopts was previously used in this context

  # Defaults

  INTERACTIVE=""
  OPERATOR="mv"        # Can be overriden with -c option
  SERIALNO="Yes"       # Generate serial numbers
  TESTMODE=""
  VERBOSE=""

  while getopts ${OPTLIST} opt
  do
    case $opt
    in

      # Copy, don't move, targets to graveyard
      c)
        OPERATOR="cp -pr"
      ;;

      # Empty the graveyard
      e)
        rm -rf  ${VERBOSE} ${GRAVEYARD}/* ${GRAVEYARD}/.[-z]*
      ;;

      # Name your own graveyard
      g)
        GRAVEYARD=${OPTARG}
      ;;

      h)
          trm_usage
          return 0
      ;;

      i)
        INTERACTIVE="Yes"
      ;;

      # Turn of serial number generation
      s)
        SERIALNO=""
      ;;

      # Don't do anything, just show what you would do
      t)
        TESTMODE=${TESTING}
      ;;

      # Be noisy
      v)
        VERBOSE="-v"
      ;;

      # Actually execute
      x)
          TESTMODE=""
      ;;

      *)
          trm_usage
          return 1
      ;;
    esac
  done

  # Notify if in test mode
 echo ${TESTMODE}

  # Process arguments

  shift $((OPTIND-1))
  while [ $# -gt 0 ]
  do

    # Symlinks require special care
    if [ -L "$1" ]
    then

      # Bare symlink itelf
      REALPATH=$(readlink -f $(dirname  "$1"))
      if [ "${REALPATH}" = '/' ]
      then
        REALPATH=""
      fi

      REALPATH="${REALPATH}"/$(basename "$1")

    # Process normal files and directories
    else
      REALPATH=$(readlink -f "$1")
    fi

    # See if we want serial numbers
    SERIAL=""
    if [ -n "${SERIALNO}" ]
    then
        SERIAL=".$(date +%Y%m%d%H%M%S)"
    fi

    SRCDIR=$(dirname "${REALPATH}")
    if [ "${SRCDIR}" = '/' ]
    then
      SRCDIR=""
    fi

    SRCFIL=$(basename "${REALPATH}")
    DESDIR="${GRAVEYARD}${SRCDIR}"
    DESFIL="${SRCFIL}${SERIAL}"

    CMD="${OPERATOR} $VERBOSE '${REALPATH}' '${DESDIR}/${DESFIL}'"
    if [ -n "${TESTMODE}" ]
    then
      echo ${CMD}
    else

      # Handle interactive requests
      if [ -n "${INTERACTIVE}" ]
      then
        echo -n "${INTERPROMPT} ${REALPATH}? "
        read doit

        if [ "${doit}" != "y" ]  && [ "${doit}" != "Y" ]
        then
          shift
          continue
        fi

      fi

      mkdir -p "${DESDIR}"
      eval ${CMD}
    fi

    shift

  done

}


# Run now if invoked directly on the command line.  (Otherwise, we're
# just loading the function above into the current shell context.)

if [ $(basename "$0") = "trm.sh" ]
then
    trm "$@"
fi