#!/usr/bin/env bash


###
#
# Options Section
#
###

set -e
set -u
set -o pipefail


###
#
# Variables Section
#
###

NAME=$(/usr/bin/basename "${0}")
ARGUMENT="${1:---help}"
ROOT="/var/trash/${USER}"
TRASH="${ROOT}/$(/usr/bin/date '+%d.%m.%Y')"
RENETENTIONDAYS="${2:-30}"


###
#
# Function Section
#
###

function clean () {
  for ROOT in '/var/trash/'*'/'*; do
    /usr/bin/find "${ROOT}" -maxdepth 0 -mtime +"${RENETENTIONDAYS}" -exec /usr/bin/rm --force --recursive {} \;;
  done
}


###
#
# Runtime Environment
#
###

if [[ ! -d "${TRASH}" ]]; then
  /usr/bin/install --directory --mode='0777' '/var/trash'
  /usr/bin/install --directory --mode='0750' "${TRASH}"
fi

if [[ -z "${ARGUMENT}" ]] || \
   [[ "${ARGUMENT}" == '--help' ]]; then
  /usr/bin/echo -n -e 'Usage: ${NAME} [File]\n\n'
  /usr/bin/echo -n -e 'File Types:\n'
  /usr/bin/echo -n -e '[F] - Regular File\n'
  /usr/bin/echo -n -e '[B] - Block Device\n'
  /usr/bin/echo -n -e '[C] - Character Device\n'
  /usr/bin/echo -n -e '[D] - Directory\n'
  /usr/bin/echo -n -e '[L] - Symbolic Link\n'
  /usr/bin/echo -n -e '[S] - Socket\n'
  /usr/bin/echo -n -e '[P] - Named Pipe\n'
  exit 1
elif [[ "${ARGUMENT}" == '--clean' ]]; then
  clean
  exit 0
fi

if [[ ! -L "${HOME}/.trash" ]]; then
  /usr/bin/ln --symbolic "${ROOT}" "${HOME}/.trash"
fi

INPUT_NUMBER='1'

for INPUT in "${@}"; do
  unset BASENAME
  unset REALPATH
  if [[ "${INPUT}" != '-'* ]]; then
    BASENAME=$(/usr/bin/basename "${INPUT}")
    REALPATH=$(/usr/bin/realpath --canonicalize-existing --quiet --no-symlinks "${INPUT}")
    BASENAME_NUMBER='1'
    while [[ -e "${TRASH}/${BASENAME}" ]]; do
      BASENAME="${BASENAME//.[[:digit:]]/}"
      BASENAME="${BASENAME}.${BASENAME_NUMBER}"
      (( BASENAME_NUMBER++ ))
    done
    if [[ "${TRASH}" == "${REALPATH}"* ]]; then
      if ! /usr/bin/rm --force --recursive "${ROOT:?}"; then
        /usr/bin/echo -n -e 'Error: The Recycle Bin could not be cleared.\n'
        exit 1
      fi
      exit 0
    fi
    if [[ -f "${REALPATH}" ]]; then
      /usr/bin/echo -n -e "[F] ${INPUT_NUMBER}.\t${REALPATH}\n"
    elif [[ -b "${REALPATH}" ]]; then
      /usr/bin/echo -n -e "[B] ${INPUT_NUMBER}.\t${REALPATH}\n"
    elif [[ -c "${REALPATH}" ]]; then
      /usr/bin/echo -n -e "[C] ${INPUT_NUMBER}.\t${REALPATH}\n"
    elif [[ -d "${REALPATH}" ]]; then
      /usr/bin/echo -n -e "[D] ${INPUT_NUMBER}.\t${REALPATH}\n"
    elif [[ -L "${REALPATH}" ]]; then
      /usr/bin/echo -n -e "[L] ${INPUT_NUMBER}.\t${REALPATH}\n"
    elif [[ -S "${REALPATH}" ]]; then
      /usr/bin/echo -n -e "[S] ${INPUT_NUMBER}.\t${REALPATH}\n"
    elif [[ -p "${REALPATH}" ]]; then
      /usr/bin/echo -n -e "[P] ${INPUT_NUMBER}.\t${REALPATH}\n"
    fi
    if [[ -f "${REALPATH}" ]] || \
       [[ -d "${REALPATH}" ]] || \
       [[ -L "${REALPATH}" ]]; then
      if ! /usr/bin/mv --no-clobber "${REALPATH}" "${TRASH}/${BASENAME}" &> '/dev/null'; then
        if [[ -f "${REALPATH}" ]]; then
          /usr/bin/echo -n -e "\r\r[ERROR] [F] ${INPUT_NUMBER}.\t${REALPATH}\n"
        elif [[ -d "${REALPATH}" ]]; then
          /usr/bin/echo -n -e "\r\r[ERROR] [D] ${INPUT_NUMBER}.\t${REALPATH}\n"
        elif [[ -L "${REALPATH}" ]]; then
          /usr/bin/echo -n -e "\r\r[ERROR] [L] ${INPUT_NUMBER}.\t${REALPATH}\n"
        fi
      fi
    elif [[ -b "${REALPATH}" ]] || \
         [[ -c "${REALPATH}" ]] || \
         [[ -S "${REALPATH}" ]] || \
         [[ -p "${REALPATH}" ]]; then
      if ! /usr/bin/rm --force "${REALPATH}" &> '/dev/null'; then
        if [[ -b "${REALPATH}" ]]; then
          /usr/bin/echo -n -e "\r\r[ERROR] [B] ${INPUT_NUMBER}.\t${REALPATH}\n"
        elif [[ -c "${REALPATH}" ]]; then
          /usr/bin/echo -n -e "\r\r[ERROR] [C] ${INPUT_NUMBER}.\t${REALPATH}\n"
        elif [[ -S "${REALPATH}" ]]; then
          /usr/bin/echo -n -e "\r\r[ERROR] [S] ${INPUT_NUMBER}.\t${REALPATH}\n"
        elif [[ -p "${REALPATH}" ]]; then
          /usr/bin/echo -n -e "\r\r[ERROR] [P] ${INPUT_NUMBER}.\t${REALPATH}\n"
        fi
      fi
    fi
    (( INPUT_NUMBER++ ))
  fi
done
