#!/bin/bash

set -e

export LC_ALL=C.UTF-8

################################################################
# Load modules

configs="/etc/ngcp-installer/"
storage="/usr/share/ngcp-installer/"

if [[ ! -r "${configs}/config_deploy.inc" ]]; then
  log_die "Can not find ${configs}/config_deploy.inc file"
  exit 1
fi
# shellcheck source=./conf/config_deploy.inc
. "${configs}/config_deploy.inc"
# shellcheck source=./install/config_type.inc
. "${storage}/config_type.inc"
# shellcheck source=./install/config.inc
. "${storage}/config.inc"
# shellcheck source=./install/log.inc
. "${storage}/log.inc"

if "${DEBUG_MODE}" ; then
  log_debug "Enabling ngcp-installer debug mode..."
  set -x
  export PS4='+\t (${BASH_SOURCE##*/}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): } '
fi

# shellcheck source=./install/system.inc
. "${storage}/system.inc"
# shellcheck source=./install/packages.inc
. "${storage}/packages.inc"
# shellcheck source=./install/check.inc
. "${storage}/check.inc"

################################################################
# Check necessary defaults
if ! "${CE}" && ! "${PRO}" && ! "${CARRIER}" ; then
  log_error "NGCP system type is not defined, aborting"
  exit 1
fi

################################################################

if "${PRO}" || "${CARRIER}"; then
  # shellcheck source=./install/system_pro.inc
  . "${storage}/system_pro.inc"
fi

# MAIN

system_print_begin

check_env

system_set_version

system_create_log_path

if "${PRO}" || "${CARRIER}"; then
  system_get_uid_gid_files
  system_copy_ids
fi

packages_sourceslist
packages_aptconfig
packages_enable_policy_rc_d
packages_install_pkglist
packages_purge_uninstalled

system_remove_ngcp_config_from_git
system_create_ngcp_data_home
system_create_ngcp_data_backup
system_create_data_dirs
system_create_sipwise_user
system_create_cdr_user
system_create_backup_user
system_generate_etc_default_locale
system_allow_ssh_root

system_disable_services
system_delete_bootstrap_key

system_print_done_installation

# vim: ft=bash
