#!/bin/ash

# usage: borgmatic mount --archive latest --mount-point /mnt/fuse [-h]

# Check archives for consistency

# check arguments:
#   --progress    Display progress for each file as it is checked
#   --repair      Attempt to repair any inconsistencies found (experimental and
#                 only for interactive use)
#   --only CHECK  Run a particular consistency check (repository, archives,
#                 data, or extract) instead of configured checks; can specify
#                 flag multiple times
#   -h, --help    Show this help message and exit

usage()
{
  echo
  echo "Usage: $PROG_NAME [-p] [-c/-r] [-o] "
  echo
  echo " -p      =   Displays the progress (default)."
  echo "             If omitted, progress displayed."
  echo " -c      =   Checks only, without trying to repair."
  echo " -r      =   Checks and Repairs."
  echo "             Attempt to repair any inconsistencies found (experimental and"
  echo "             only for interactive use)."
  echo "             This is a potentially dangerous function."
  echo "             check --repair might lead to data loss (for kinds of corruption it is not"
  echo "             capable of dealing with). BE VERY CAREFUL!"
  echo " -o only     repository|archives|data|extract"
  echo "             Run a particular consistency check."
  echo ""
  echo "EXAMPLES:"
  echo
  echo "To check the repository(ies), the archive(s), the data and the extract without repairing"
  echo "  $PROG_NAME -c"
  exit 0
}

borgmatic_mount() {
  echo "$PROG_NAME: Starting unmounting repository ..."
  borgmatic umount --mount-point /mnt/fuse 
  exit 0
}

set_variable()
{
  local varname=$1
  shift
  if [ -z "${!varname}" ]; then
    eval "$varname=\"$@\""
  else
    echo "Error: $varname already set"
    usage
  fi
}

####################################################################
# Main script starts here

#set_variable PROG_NAME "$(basename "$0")"

# if [[ $1 == "" ]]; then
#     usage;
#     exit;
# else
#     while getopts 'pcro:?h' c
#     do
#     case $c in
#         p) set_variable CHECK_PROGRESS --progress ;;
#         c) set_variable CHECK --check ;;
#         r) set_variable CHECK_REPAIR --repair ;;
#         o) set_variable CHECK_ONLY "$OPTARG" ;;
#         h|?) usage ;; esac
#     done
# fi


borgmatic_mount
