#!/bin/bash

set -e
set -u

die() {
  echo "ERROR: $*" >&2
  exit 1
}

fatal_missing_var() {
  local var_name="$1"
  if [[ ! -v "${var_name}" ]] ; then
    die "Missing mandatory environment variable '\$${var_name}', exiting."
  fi

  local var_value="${!var_name}"
  if [[ -z "${var_value}" ]] ; then
    die "Empty mandatory environment variable '\$${var_name}', exiting."
  fi
}

request_confirmation() {
  if [[ "${FORCE_UPGRADE}" = "true" ]] ; then
    echo "Forcing as requested via environment variable FORCE_UPGRADE."
    return
  fi

  while true; do
    echo -n "Should the upgrade continue? (yes/no): "
    read -r answer
    case "${answer,,}" in
      yes)
        echo "Continue as requested."
        break
        ;;
      no)
        die "Aborted as requested."
        ;;
      * )
        echo "Please answer 'yes' or 'no'."
        ;;
    esac
    unset answer
  done
}

echo_fix_by_hand() {
  local prefix="$1" # align with previous text's indentation
  {
    echo
    echo "  # make a backup of the current home, for example:"
    echo "  cp -av ~sipwise '${BACKUP_HOME}'"
    echo
    echo "and then"
    echo
    echo "  mkdir -v --parents /ngcp-data/home/ && usermod --move-home --home /ngcp-data/home/sipwise sipwise"
    echo
    echo "or"
    echo
    echo "  mkdir -v --parents /ngcp-data/home/ && mv -v ~sipwise /ngcp-data/home/sipwise"
    echo "  # ... and then edit /etc/passwd to change home of user sipwise by hand"
    echo
  } | sed "s/^/${prefix}/"
}

fatal_missing_var FORCE_UPGRADE
fatal_missing_var BACKUP

OLD_CANONICAL_HOME=/var/sipwise
NEW_CANONICAL_HOME=/ngcp-data/home/sipwise
# shellcheck disable=SC2116
CURRENT_HOME="$(echo ~sipwise)"
BACKUP_HOME="${BACKUP}/home-of-user-sipwise"

if dpkg-query -s ngcp-support-noaccess &>/dev/null && ! id -u sipwise &>/dev/null; then
  echo "The package ngcp-support-noaccess is installed and no user 'sipwise' so skip moving"
  echo "If you need some files from old home directory - please move them by hand"
  request_confirmation
  exit 0
fi

if [[ "${CURRENT_HOME}" = "${NEW_CANONICAL_HOME}" ]]; then
  echo "INFO: Nothing to do, home of user sipwise already the canonical '${NEW_CANONICAL_HOME}'"
  exit 0
elif [[ "${CURRENT_HOME}" != "${OLD_CANONICAL_HOME}" ]]; then
  echo "WARNING: Home of user sipwise not the new canonical '${NEW_CANONICAL_HOME}'"
  echo "         but also not the old canonical '${OLD_CANONICAL_HOME}',"
  echo "         the current home was detected as: '${CURRENT_HOME}'"
  echo
  echo "         Please move by hand the current home to '${NEW_CANONICAL_HOME}',"
  echo "         you can try with:"
  echo_fix_by_hand "         "
  die "Aborting now to avoid damage"
else
  echo "INFO: Home of user sipwise is the old canonical '${OLD_CANONICAL_HOME}',"
  echo "      can try to move to '${NEW_CANONICAL_HOME}'."
  echo
  echo "      If you want to do this, please reply 'yes' to the question below,"
  echo "      otherwise say 'no' and resolve the situation by hand.  You can do it"
  echo "      with something like this:"
  echo_fix_by_hand "      "
  request_confirmation
fi

# confirmed that the move goes ahead
echo "Creating main backup dir if missing..."
mkdir -pv "${BACKUP}"
echo "Creating backup of home of user 'sipwise'..."
cp -av ~sipwise "${BACKUP_HOME}"
echo

echo -n "Creating /ngcp-data/home/ if missing..."
if [[ ! -d /ngcp-data/home/ ]]; then
  echo
  mkdir -v --parents /ngcp-data/home/
  chown -v root:root /ngcp-data/home/
  chmod -v 0755 /ngcp-data/home/
  echo
else
  echo " not missing, nothing to create."
  echo
fi

echo -n "Moving home of sipwise user to '${NEW_CANONICAL_HOME}'..."
usermod --move-home --home "${NEW_CANONICAL_HOME}" sipwise
echo " done"
echo

echo "Locking down permissions for extra safety in home dir '${NEW_CANONICAL_HOME}'..."
chmod -v 0750 "${NEW_CANONICAL_HOME}"

echo "Creating compat symlink from '${OLD_CANONICAL_HOME}' to '${NEW_CANONICAL_HOME}'..."
ln -sv "${NEW_CANONICAL_HOME}" "${OLD_CANONICAL_HOME}"

echo "Checking entry of user 'sipwise' in /etc/password:"
getent passwd sipwise
echo "Home dir is:"
getent passwd sipwise | cut -d: -f6
echo

echo "Checking new dir location:"
ls -ld "${NEW_CANONICAL_HOME}"
echo

echo "Checking new dir contents:"
ls -lA --color=auto -F  "${NEW_CANONICAL_HOME}"
echo

echo "Checking old location:"
ls -lA --color=auto -F  "${OLD_CANONICAL_HOME}"
echo

echo "All OK"
