#!/bin/bash

set -e

FORCE=false
SKIP_NODE_CHECK=false
opts_format='hfs'

die() {
  local message="$*"

  echo "ERROR: ${message}" >&2
  exit 1
}

usage() {
  echo "
Usage: $0 <options...>

  options:
    -h      this help
    -f      don't check if ngn-in-a-box is already set
    -s      skip checking if the node is active

"
}

while getopts "${opts_format}" OPT
do
  case "${OPT}" in
    h)
      usage
      exit 0
    ;;
    f)
      FORCE=true
    ;;
    s)
      SKIP_NODE_CHECK=true
    ;;
  esac
done

cur_status=$(ngcpcfg get ngn_in_a_box.enable)

if ! "${FORCE}"; then
  if [[ "${cur_status}" == "yes" ]]; then
    echo "INFO: Nothing to do, already switched to ngn_in_a_box"
    exit 0
  fi
fi


NGCP_TYPE="$(ngcpcfg get general.ngcp_type)"
if [[ "${NGCP_TYPE}" == 'sppro' ]]; then
  if ! "${SKIP_NODE_CHECK}"; then
    if ngcp-check-active -q; then
      die "This is active node while it's supposed to run this script on standby one"
    fi
  fi
  ngcpcfg pull
elif [[ "${NGCP_TYPE}" == 'carrier' ]]; then
  die "NGN-In-a-Box cannot be activated on a Carrier systems"
else
  die "NGN-In-a-Box cannot be activated on a CE systems"
fi

# Get ngn-in-a-box configuration parameters
echo ""
echo "Please provide the following parameters to proceed with the ngn-in-a-box configuration."
echo ""
echo "IP of the local external SIP public interface (sems ip): "
read -r ngn_ext_ip
echo "Port of the local external SIP public interface (sems port): "
read -r ngn_ext_port
echo "IP of the local SIP internal interface (lb ip): "
read -r ngn_int_ip
echo "Port of the local SIP internal interface (lb port): "
read -r ngn_int_port
echo "IP of the SIP remote connected C5 system: "
read -r c5_ip
echo "Port of the SIP remote connected C5 system: "
read -r c5_port

while true ; do
  echo ""
  echo "Do you want to continue applying the ngn-in-a-box configuration (yes/no)? "
  read -r choice
  case "${choice,,}" in
    yes|y) break;;
    no|n) echo "Exiting" && exit 0;;
    *) echo "Invalid choice. Enter yes or no, please";;
  esac
done

echo ""
echo "INFO: start setting ngn-in-a-box parameters"
ngcpcfg set /etc/ngcp-config/config.yml ngn_in_a_box.enable=yes
ngcpcfg set /etc/ngcp-config/config.yml ngn_in_a_box.c5_ip="${c5_ip}"
ngcpcfg set /etc/ngcp-config/config.yml ngn_in_a_box.c5_port="${c5_port}"
ngcpcfg set /etc/ngcp-config/config.yml ngn_in_a_box.external_ip="${ngn_ext_ip}"
ngcpcfg set /etc/ngcp-config/config.yml ngn_in_a_box.internal_ip="${ngn_int_ip}"

echo "INFO: deativacting unnecessary services:
    - asterisk
    - faxserver
    - kamailio-proxy
    - mediator
    - pbx
    - prosody
    - rate-o-mat
    - panel
    - voisniff"
ngcpcfg set /etc/ngcp-config/config.yml asterisk.enable=no
ngcpcfg set /etc/ngcp-config/config.yml faxserver.enable=no
ngcpcfg set /etc/ngcp-config/config.yml kamailio.proxy.enable=no
ngcpcfg set /etc/ngcp-config/config.yml kamailio.proxy.presence.enable=no
ngcpcfg set /etc/ngcp-config/config.yml mediator.enable=no
ngcpcfg set /etc/ngcp-config/config.yml pbx.enable=no
ngcpcfg set /etc/ngcp-config/config.yml prosody.enable=no
ngcpcfg set /etc/ngcp-config/config.yml rateomat.enable=no
ngcpcfg set /etc/ngcp-config/config.yml www_admin.enable=no
ngcpcfg set /etc/ngcp-config/config.yml voisniff.admin_panel=no
ngcpcfg set /etc/ngcp-config/config.yml voisniff.daemon.enable=no

echo "INFO: tuning kamailio-lb service"
ngcpcfg set /etc/ngcp-config/config.yml kamailio.lb.enable=yes
ngcpcfg set /etc/ngcp-config/config.yml kamailio.lb.port="${ngn_int_port}"
ngcpcfg set /etc/ngcp-config/config.yml kamailio.lb.filter_content_type.enable=yes
ngcpcfg set /etc/ngcp-config/config.yml kamailio.lb.filter_content_type.action=drop
ngcpcfg set /etc/ngcp-config/config.yml kamailio.lb.filter_content_type.content_type_list.0.content_type=application/vnd.etsi.cug+xml
ngcpcfg set /etc/ngcp-config/config.yml kamailio.lb.filter_content_type.content_type_list.0.direction=all
ngcpcfg set /etc/ngcp-config/config.yml kamailio.lb.security.dos_ban_enable=no
ngcpcfg set /etc/ngcp-config/config.yml kamailio.lb.security.failed_auth_ban_enable=no

echo "INFO: tuning sems-b2b service"
ngcpcfg set /etc/ngcp-config/config.yml b2b.enable=yes
ngcpcfg set /etc/ngcp-config/config.yml b2b.bindport="${ngn_ext_port}"
ngcpcfg set /etc/ngcp-config/config.yml b2b.sbc.reset_tag_on_fork=yes
ngcpcfg set /etc/ngcp-config/config.yml b2b.vsc.enable=no

echo "INFO: tuning rtpengine service"
ngcpcfg set /etc/ngcp-config/config.yml rtpengine.enable=yes
ngcpcfg set /etc/ngcp-config/config.yml rtpengine.prefer_bind_on_internal=no
ngcpcfg set /etc/ngcp-config/config.yml rtpengine.recording.enable=no

echo "INFO: disabling cdr, etc and mysql backups"
ngcpcfg set /etc/ngcp-config/config.yml backuptools.cdrexport_backup.enable=no
ngcpcfg set /etc/ngcp-config/config.yml backuptools.etc_backup.enable=no
ngcpcfg set /etc/ngcp-config/config.yml backuptools.mysql_backup.enable=no

echo "INFO: disabling witnessd statistics collections"
ngcpcfg set /etc/ngcp-config/config.yml witnessd.gather.kamailio_concurrent_calls=no
ngcpcfg set /etc/ngcp-config/config.yml witnessd.gather.kamailio_dialog_active=no
ngcpcfg set /etc/ngcp-config/config.yml witnessd.gather.kamailio_dialog_early=no
ngcpcfg set /etc/ngcp-config/config.yml witnessd.gather.kamailio_dialog_incoming=no
ngcpcfg set /etc/ngcp-config/config.yml witnessd.gather.kamailio_dialog_local=no
ngcpcfg set /etc/ngcp-config/config.yml witnessd.gather.kamailio_dialog_outgoing=no
ngcpcfg set /etc/ngcp-config/config.yml witnessd.gather.kamailio_dialog_relay=no
ngcpcfg set /etc/ngcp-config/config.yml witnessd.gather.kamailio_pkgmem=no
ngcpcfg set /etc/ngcp-config/config.yml witnessd.gather.kamailio_shmem=no
ngcpcfg set /etc/ngcp-config/config.yml witnessd.gather.kamailio_usrloc_regdevices=no
ngcpcfg set /etc/ngcp-config/config.yml witnessd.gather.kamailio_usrloc_regusers=no

echo "INFO: configuration done. Proceeding applying changes and pushing to central node"

# Added --force-all-services to be sure to restart ALL the services
ngcpcfg apply "Activated ngn-in-a-box" --force-all-services
ngcpcfg push --shared-only

echo "

INFO: ngn-in-a-box correctly configured.
Proceed now with the following steps:
 * make this node active with the command 'ngcp-make-active'
 * pull the configuration on the other node with the command 'ngcpcfg pull'
 * apply changes on the other node with the command 'ngcpcfg apply'

When done, execute the following steps on the NGCP system where the ngn-in-a-box has to be connected:
 * add the ngn-in-a-box as external_sbc with the command 'ngcpcfg set /etc/ngcp-config/config.yml.APPEND=sip:${ngn_int_ip}:${ngn_int_port}' on the standby management node
 * apply the changes and push the config to all the other nodes following the standard procedures
 * on the peering server, that has to pass through the ngn-in-a-box, select in the "Via Route" field the 'external_sbc' ip configured in config.yml
"

exit 0
