#!/bin/bash
# Install additional packages if necessary

set -euEo pipefail

# We check file on current root partition because this is the
# only place where you can put file before the upgrade as /ngcp-fallback
# partition will be formated before this step.
PACKAGES_FILE='/etc/ngcp_additional_packages'

declare -a PACKAGES
echo "Checking '${PACKAGES_FILE}'"
if [ -e "${PACKAGES_FILE}" ]; then
  mapfile -t PACKAGES <"${PACKAGES_FILE}"
fi

if [[ -n "${PACKAGES[*]}" ]]; then
  echo "Following additional packages '${PACKAGES[*]}' were found"
  echo "installing them to /ngcp-fallback system..."
  # shellcheck disable=SC2086
  chroot /ngcp-fallback ${APT_CMD} ${APT_OPTS} install "${PACKAGES[@]}"
fi
