#!/bin/bash
#
# Backup Manager tools.
#
# For suggestion, bug reports, please contact Pierre-Yves Landuré <pierre-yves@landure.fr>


SCRIPT_NAME="$(command basename ${0})"

# Print this script help.
function usage {
  command echo "
This tool ease Backup Manager administration from command line.

Usage :

  ${SCRIPT_NAME} cleanup
  ${SCRIPT_NAME} add path
  ${SCRIPT_NAME} remove path

  * cleanup : Remove invalid backup targets.
  * add path : Add the given path to backup targets.
  * remove path : Remove the given path from backup targets.
"
  exit 1
} # usage



# Get the absolute path for a file or directory.
#
# @param string $path A relative path.
#
# @return ${REALPATH} A absolute path.
REALPATH=""
function realpath {
  command test ${#} -ne 1 && exit 1
  REALPATH=$(/bin/readlink -f "${1}")
} # realpath



# Check if a binary is present
#
# @param string $binary The binary to check.
# @param string $package The package the binary come from.
#
# @return Exit with error if the binary is missing.
function check_binary {
  command test ${#} -ne 2 && exit 1

  # Test the binary presence.
  if [ -z "$(which "${1}")" ]; then
    echo "Error : '${1}' is missing. Please install package '${2}'."
    exit 1
  fi
} # check_binary


# Compute the backup targets array ids.
#
# @return void
function fix_targets_ids {
  COUNTER=0
  for LINE_NUMBER in $(command grep --line-number '^BM_TARBALL_TARGETS\[' '/etc/backup-manager.conf' \
          | command cut --delimiter=':' --fields=1); do

    command sed -i \
        -e "${LINE_NUMBER}s/^BM_TARBALL_TARGETS\[[0-9]*\]/BM_TARBALL_TARGETS[${COUNTER}]/" \
        '/etc/backup-manager.conf'

    COUNTER=$((${COUNTER} + 1))
  done
} # fix_targets_ids


# Check for binaries presence
check_binary "basename" "coreutils"
check_binary "dirname" "coreutils"
check_binary "tar" "tar"
check_binary "mktemp" "mktemp"
check_binary "sed" "sed"
check_binary "backup-manager" "backup-manager"

command test ! -e '/etc/backup-manager.conf' && exit 1

# Check if at least one args given.
command test ${#} -eq 0 && usage

case "${1}" in

  cleanup )
    command grep '^BM_TARBALL_TARGETS\[' '/etc/backup-manager.conf' \
        | command sed -e 's/^.*="\([^"]*\)".*$/\1/' \
        | command xargs -rI TARGET sh -c "test ! -e 'TARGET' && echo 'TARGET'" \
        | command xargs -rI TARGET ${0} remove 'TARGET'
    exit 0
    ;;



  add )
    # Check if valid number of arguments given (1).
    command test ${#} -ne 2 && usage

    realpath "${2}"
    TARGET_PATH="${REALPATH}"

    if [ -z "$(command grep '^BM_TARBALL_TARGETS\[' '/etc/backup-manager.conf' \
        | command sed -e 's/^.*="\([^"]*\)".*$/\1/' \
        | command grep "${TARGET_PATH}")" ]; then

      command sed -i -e "/^export BM_TARBALL_TARGETS/i\
BM_TARBALL_TARGETS[]=\"${TARGET_PATH}\"" \
          '/etc/backup-manager.conf'

      # Fix array numbers.
      fix_targets_ids

      echo "Path '${TARGET_PATH}' added to backup targets."
    else
      echo "Path '${TARGET_PATH}' is already a backup target."
    fi

    exit 0
    ;;



  remove )
    # Check if valid number of arguments given (1).
    command test ${#} -ne 2 && usage

    realpath "${2}"
    TARGET_PATH="${REALPATH}"

    if [ -n "$(command grep '^BM_TARBALL_TARGETS\[' '/etc/backup-manager.conf' \
        | command sed -e 's/^.*="\([^"]*\)".*$/\1/' \
        | command grep "${TARGET_PATH}")" ]; then

      command grep --line-number "BM_TARBALL_TARGETS\[.*[\"']${TARGET_PATH}[\"']" '/etc/backup-manager.conf' \
          | command cut --delimiter=':' --fields=1 \
          | command xargs --no-run-if-empty -iLINE sed -i -e "LINEd" \
              '/etc/backup-manager.conf'

      # Fix array numbers.
      fix_targets_ids

      echo "Path '${TARGET_PATH}' removed from backup targets."
    else
      echo "Path '${TARGET_PATH}' is not a backup target."
    fi

    exit 0
    ;;



  * )
    echo "Error : '${1}' is not a valid action."
    usage
    ;;
esac

exit 0

