#!/bin/bash

set -euEo pipefail

command='get'
if [[ "${OLD_VERSION}" =~ mr7.5 || "${OLD_VERSION}" =~ mr8.0 ]]; then
  command='values'
fi

BUSY_CUSTOM_GREETING=$(ngcpcfg ${command} asterisk.voicemail.greeting.busy_custom_greeting)
if [[ -f "${BUSY_CUSTOM_GREETING}" ]]; then
  if [[ ! "${BUSY_CUSTOM_GREETING}" =~ ^/ngcp-data ]]; then
    echo "It was detected asterisk.voicemail.greeting.busy_custom_greeting file"
    echo "Please consider moving it to /ngcp-data partition after upgrade"
    echo "Copying it to /ngcp-fallback/${BUSY_CUSTOM_GREETING}"
    cp -a "${BUSY_CUSTOM_GREETING}" "/ngcp-fallback/${BUSY_CUSTOM_GREETING}"
  fi
fi

UNAVAIL_CUSTOM_GREETING=$(ngcpcfg ${command} asterisk.voicemail.greeting.unavail_custom_greeting)
if [[ -f "${UNAVAIL_CUSTOM_GREETING}" ]]; then
  if [[ ! "${UNAVAIL_CUSTOM_GREETING}" =~ ^/ngcp-data ]]; then
    echo "It was detected asterisk.voicemail.greeting.unavail_custom_greeting file"
    echo "Please consider moving it to /ngcp-data partition after upgrade"
    echo "Copying it to /ngcp-fallback/${UNAVAIL_CUSTOM_GREETING}"
    cp -a "${UNAVAIL_CUSTOM_GREETING}" "/ngcp-fallback/${UNAVAIL_CUSTOM_GREETING}"
  fi
fi
