#!/bin/bash

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

#set -x

PILER_HOSTNAME="${PILER_HOSTNAME:-archive.example.com}"
MYSQL_ROOT_PASSWORD="${MYSQL_ROOT_PASSWORD:-abcde123}"
MYSQL_PILER_PASSWORD="${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:-}"
#ENCRYPTION_KEY="$( openssl rand -base64 24 )"
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_RPM="piler-ee-${PILER_VERSION}-${COMMIT_ID}.el10.x86_64.rpm"
# shellcheck disable=SC2034
PILER_USER="piler"
PILER_CONF="/etc/piler/piler.conf"
# shellcheck disable=SC2034
PILER_UI_CONF="/var/piler/ui/.env"

install_prerequisites() {
   #yum update -y

   yum install -y tar rsyslog openssl valkey which gettext wget unzip mariadb-server mariadb-common make sysstat bzip2 java-21-openjdk curl unixODBC libzip zip libtool-ltdl nmap-ncat jq
   yum install -y https://repo.manticoresearch.com/manticore-repo.noarch.rpm
   yum install -y manticore manticore-columnar-lib manticore-extra
   systemctl stop manticore
   systemctl disable manticore

   echo "piler-ui ALL=NOPASSWD: /usr/bin/systemctl restart piler" >> /etc/sudoers

   echo "/usr/local/lib" > /etc/ld.so.conf.d/tre.conf
   ldconfig
}


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

   if [[ -f /.dockerenv ]]; then
      mysqld_safe &
      sleep 5
   else
      systemctl restart mariadb
   fi

   mysqladmin -u root password "$MYSQL_ROOT_PASSWORD"
}


install_piler() {
   groupadd piler || true
   useradd -g piler -d /var/piler -s /bin/bash  piler || true

   yum install -y "${DOWNLOAD_URL}/piler-ee/${PILER_VERSION}/${PILER_RPM}"

   ldconfig
}


centos_notes() {
   echo
   echo "Centos specific notes:"
   echo " * Be sure to either disable selinux or add the required rules"
}


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

setup_traefik

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

systemctl enable --now mariadb
systemctl enable --now valkey


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

init_db

save_node_type
write_ui_config

save_multinodes_settings

if pgrep firewalld; then
   firewall-cmd --add-service=smtp --permanent
   firewall-cmd --add-service=https --permanent
   firewall-cmd --reload
fi

systemctl start pilersearch

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

get_the_licence

centos_notes
