#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

#set -x

PILER_HOSTNAME="${PILER_HOSTNAME:-archive.example.com}"
MYSQL_ROOT_PASSWORD="abcde123"
# shellcheck disable=SC2034
MYSQL_PILER_PASSWORD="piler123"
ENCRYPT_MYSQL_DATA_AT_REST=1
NODE_TYPE="${NODE_TYPE:-MASTER}"
MULTITENANCY="${MULTITENANCY:-0}"
MULTINODES="${MULTINODES:-0}"
MASTER_NODE="${MASTER_NODE:-10.1.1.2}"
SERVER_ID="${SERVER_ID:-0}"
USE_SMTP_GATEWAY="${USE_SMTP_GATEWAY:-0}"
SPHINX_WORKER_LISTEN_ADDRESS="${SPHINX_WORKER_LISTEN_ADDRESS:-}"
DISTRO="noble"
PILER_VERSION="2.0.0"
# shellcheck disable=SC2034
RT=1
ARCH="${ARCH:-amd64}"
# shellcheck disable=SC2034
SELF_SIGNED_CERT=0
# shellcheck disable=SC2034
DYNAMIC_CONFIG=1
# shellcheck disable=SC2034
MANTICORE_HOST="127.0.0.1"
# shellcheck disable=SC2034
MYSQL_HOSTNAME="localhost"
# shellcheck disable=SC2034
MYSQL_DATABASE="piler"
# shellcheck disable=SC2034
MYSQL_USERNAME="piler"
# shellcheck disable=SC2034
MANTICORE_WORKERS="" ## Only in multinodes environment!

# shellcheck disable=SC2034
TIKA_VERSION="3.2.1"
# shellcheck disable=SC2034
TRAEFIK_VERSION="v3.3.3"
DOWNLOAD_URL="https://download.mailpiler.com"
COMMIT_ID="$(curl -k -s ${DOWNLOAD_URL}/piler-ee/piler-ee-v2.tag)"
PILER_DEB="piler-ee_${PILER_VERSION}-${DISTRO}-${COMMIT_ID}_${ARCH}.deb"
# shellcheck disable=SC2034
PILER_USER="piler"
PILER_CONF="/etc/piler/piler.conf"
# shellcheck disable=SC2034
PILER_UI_CONF="/var/piler/ui/.env"

export DEBIAN_FRONTEND=noninteractive

install_prerequisites() {
   wget https://repo.manticoresearch.com/manticore-repo.noarch.deb
   dpkg -i manticore-repo.noarch.deb
   apt-get update
   apt-get install -y manticore manticore-columnar-lib manticore-extra
   apt-get -y --no-install-recommends install wget rsyslog openssl sysstat unzip sudo libtre5 libzip4t64 zip libmariadb-dev mariadb-server cron openjdk-21-jre redis netcat-openbsd jq
   systemctl stop manticore
}


fix_mysql_settings() {
   cat > /etc/mysql/mariadb.conf.d/99-piler.cnf << PILER_CNF
[mysqld]

innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=64M
innodb_log_file_size=64M
innodb_read_io_threads=4
innodb_write_io_threads=4
innodb_log_files_in_group=2

innodb_file_per_table
PILER_CNF
}

start_mysql() {
   fix_mysql_settings
   [[ $ENCRYPT_MYSQL_DATA_AT_REST -eq 1 ]] && setup_mysql_encryption_ubuntu

   service mariadb restart
   mysqladmin -u root password "${MYSQL_ROOT_PASSWORD}"
}


install_piler() {
   wget -q --no-check-cert "${DOWNLOAD_URL}/piler-ee/${PILER_VERSION}/${PILER_DEB}"
   dpkg -i "$PILER_DEB"
}

curl -o common.sh https://download.mailpiler.com/common.sh
# shellcheck disable=SC1091
source common.sh

install_prerequisites
install_onnx_lib

start_mysql

install_piler
install_piler_ui

[[ -f "$PILER_CONF" ]] || fix_piler_conf

init_db

save_node_type
write_ui_config

save_multinodes_settings

setup_traefik

if [[ ! -f .dockerenv ]]; then add_systemd_services; fi

systemctl start pilersearch

[[ $MULTITENANCY -eq 1 ]] || create_default_tenant

get_the_licence
