#!/bin/sh

init_variables() {
  COMMAND=${0##*/}

  HDD1_UUID='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
  HDD2_UUID='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

  ALL=false
}

parse_command_line() {
  while getopts a OPT; do
    # shellcheck disable=SC2249
    case "${OPT}" in
      a) ALL=true ;;
      ?) exit_error ;;
    esac
  done

  shift "$((OPTIND - 1))"

  HDD="$1"
}

validate_command_line() {
  if [ -z "${HDD}" ]; then
    exit_error 'Missing HDD'
  fi

  if [ "${HDD}" != 'hdd1' ] \
    && [ "${HDD}" != 'hdd2' ]; then
    exit_error "Invalid HDD '${HDD}'"
  fi
}

exit_error() {
  [ -z "$1" ] || echo "${COMMAND}: $1"
  exit 1
} >&2

init_hdd() {
  HDD_BASE_DIR="$(
    lsblk -n -o UUID,MOUNTPOINT \
      | grep "^$1 " \
      | awk '{ print $2 }'
  )"

  if [ -z "${HDD_BASE_DIR}" ]; then
    exit_error "HDD is not connected"
  fi
}

dirsum() (
  cd "$1" || return 1

  TOTAL_SIZE="$(du -sb . | cut -f1)"

  find . \
    | LC_ALL=C sort \
    | tar --no-recursion -cf - -T - \
    | pv -s "${TOTAL_SIZE}" -eb \
    | md5sum \
    | cut -d' ' -f1
)

check_dir() {
  # shellcheck disable=SC2312
  printf "%s  %s\n" "$1" "$(dirsum "${HDD_BASE_DIR}/$1")"
}

init_variables
parse_command_line "$@"
validate_command_line

if [ "${HDD}" = 'hdd1' ]; then
  init_hdd "${HDD1_UUID}"
elif [ "${HDD}" = 'hdd2' ]; then
  init_hdd "${HDD2_UUID}"
fi

check_dir Bookmarks
check_dir Books
check_dir Contacts
check_dir Documents
check_dir Infra
check_dir Mail
check_dir Messages
check_dir Notes
check_dir Pictures
check_dir Projects
check_dir Todo

if [ "${ALL}" = 'true' ]; then
  check_dir Desktop
  check_dir Music
  check_dir Packages
  check_dir Repositories
  check_dir Videos
  check_dir Work
fi
