#!/bin/bash
# Move /var/lib/<program> to /ngcp-data/<program>/ and symlink it back, in the
# context of installation through second partition (mr9.5+).
#
# This is based on a step from mr9.1, but modified to take into account that the
# destination to create the symlink is under /ngcp-fallback/var/lib/<program>,
# and also the fact that we might be upgrading from a release before the program
# is installed (so for example a user owning the data does not exist in the
# "from" system, so we have to chown within the "to" system), and similar
# adaptations.

set -e

data_prog="$1"
data_user="${2:-$1}"

if [[ -z "${data_prog}" ]]; then
  echo "Error: program name for data directory undefined. Cannot continue" >&2
  exit 1
fi

if [[ ! -d "/ngcp-fallback" ]]; then
  echo "Error: /ngcp-fallback not a directory" >&2
  exit 1
fi

data_orig="/var/lib/${data_prog}"
data_part="/ngcp-data/${data_prog}"

if [[ ! -L "${data_orig}" ]] && [[ -d "${data_orig}" ]]; then
  if [[ -d "${data_part}" ]]; then
    echo "Remove original ${data_prog} storage directory and subdirectories..."
    rmdir "${data_orig}"/* 2>/dev/null || true

    # ensure we don't leave any empty directories behind that might have
    # been created temporarily, like /var/lib/redis/.cache +
    # /var/lib/redis/.config, and also get rid of empty "${data_orig}"
    find "${data_orig}" -type d -empty -delete
  else
    echo "Moving ${data_prog} storage to 'data' partition/folder..."
    mv "${data_orig}" "${data_part}"
    # chown recursively so the data is owned by the correct users in the new
    # system.
    #
    # even if the user-owners of data should exist in the old system (unlike in
    # the "elif" case below, in this case the dirs *do* exist in the old system,
    # so in general the users should exist and be the same); IDs might be
    # different in some cases, and they are given as an argument to this step so
    # they should be correct.
    chroot /ngcp-fallback chown -R "${data_user}:${data_user}" "${data_part}"
  fi
elif [[ ! -d "${data_part}" ]]; then
  echo "Creating ${data_prog} storage to 'data' partition/folder..."
  mkdir -p "${data_part}"
  # chown needs to happen inside the new root filesystem, the old one might not
  # have the programs installed and the users might not exist, this is an actual
  # problem happening with prometheus when upgrading mr8.5->mr9.5.  but we need
  # to create the data partition because this is done by the installer, so the
  # upgrade has to create it as well.
  chroot /ngcp-fallback chown -R "${data_user}:${data_user}" "${data_part}"
else
  echo "NGCP ${data_prog} storage on 'data' partition/folder is already available."
fi

newroot_data_orig="/ngcp-fallback/${data_orig}"
if [[ ! -L "${newroot_data_orig}" ]]; then
  echo "Creating ${newroot_data_orig} backwards compatibility 'data' symlink..."
  # in a newly installed system (as it happens when installing through second
  # partition), the location in /var/lib/<program> contains the default and
  # mostly-empty directories, they need to be removed before being able to
  # create the symlink to the location in /ngcp-data
  rm -rf "${newroot_data_orig}"
  ln -f -s -T "${data_part}" "${newroot_data_orig}"
fi
