#!/bin/bash
# Script build & install MariaDB 10.2.44 trên Ubuntu 22.04
# ⚠️ Cảnh báo: 10.2 đã EOL, KHÔNG có bản vá bảo mật. Chỉ dùng khi bắt buộc.

set -e

MARIADB_VERSION="10.2.44"
INSTALL_DIR="/usr/local/mariadb-10.2"
DATA_DIR="${INSTALL_DIR}/data"
SRC_DIR="/usr/local/src/mariadb-${MARIADB_VERSION}"

echo ">>> Gỡ hoặc dừng service mariadb/mysql cũ (nếu có)..."
sudo systemctl stop mariadb || true
sudo systemctl disable mariadb || true
sudo systemctl stop mysql || true
sudo systemctl disable mysql || true

echo ">>> Cài dependency..."
sudo apt update
sudo apt install -y build-essential cmake gcc g++ bison \
  libncurses5-dev libssl-dev libboost-all-dev \
  libreadline-dev zlib1g-dev libaio-dev \
  git pkg-config wget libgnutls28-dev

echo ">>> Tải source MariaDB ${MARIADB_VERSION}..."
cd /usr/local/src
if [ ! -f "mariadb-${MARIADB_VERSION}.tar.gz" ]; then
  wget https://archive.mariadb.org/mariadb-${MARIADB_VERSION}/source/mariadb-${MARIADB_VERSION}.tar.gz
fi

if [ ! -d "${SRC_DIR}" ]; then
  tar -xzf mariadb-${MARIADB_VERSION}.tar.gz
fi

echo ">>> Build MariaDB ${MARIADB_VERSION}..."
cd ${SRC_DIR}
mkdir -p build && cd build

cmake .. \
  -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} \
  -DWITH_SSL=bundled \
  -DWITH_ZLIB=system \
  -DWITH_READLINE=ON \
  -DWITH_INNODB_MEMCACHED=ON \
  -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
  -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  -DWITH_FEDERATEDX_STORAGE_ENGINE=1

make -j$(nproc)
sudo make install

echo ">>> Tạo user & thư mục data..."
sudo useradd -r -s /bin/false mysql || true
sudo mkdir -p ${DATA_DIR}
sudo chown -R mysql:mysql ${INSTALL_DIR}

echo ">>> Khởi tạo database..."
sudo ${INSTALL_DIR}/scripts/mysql_install_db \
  --user=mysql \
  --basedir=${INSTALL_DIR} \
  --datadir=${DATA_DIR}

echo ">>> Tạo file log & phân quyền..."
sudo touch ${INSTALL_DIR}/mysql.log
sudo chown mysql:mysql ${INSTALL_DIR}/mysql.log

echo ">>> Tạo file cấu hình /etc/my.cnf..."
sudo tee /etc/my.cnf > /dev/null <<EOF
[client]
port=3306
socket=${INSTALL_DIR}/mysql.sock

[mysqld_safe]
log-error=${INSTALL_DIR}/mysql.log
pid-file=${INSTALL_DIR}/mysql.pid

[mysqld]
basedir=${INSTALL_DIR}
datadir=${DATA_DIR}
socket=${INSTALL_DIR}/mysql.sock
pid-file=${INSTALL_DIR}/mysql.pid
user=mysql
port=3306
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
max_connections=200
innodb_file_per_table=1
skip-name-resolve
EOF

echo ">>> Tạo systemd service..."
sudo tee /etc/systemd/system/mariadb-10.2.service > /dev/null <<EOF
[Unit]
Description=MariaDB 10.2
After=network.target

[Service]
Type=simple
User=mysql
Group=mysql
ExecStart=${INSTALL_DIR}/bin/mysqld \\
  --defaults-file=/etc/my.cnf \\
  --basedir=${INSTALL_DIR} \\
  --datadir=${DATA_DIR} \\
  --socket=${INSTALL_DIR}/mysql.sock \\
  --pid-file=${INSTALL_DIR}/mysql.pid
Restart=on-failure
LimitNOFILE=5000

[Install]
WantedBy=multi-user.target
EOF

echo ">>> Enable & start MariaDB..."
sudo systemctl daemon-reload
sudo systemctl enable mariadb-10.2
sudo systemctl start mariadb-10.2

echo ">>> Thêm MariaDB vào PATH..."
if ! grep -q "${INSTALL_DIR}/bin" ~/.bashrc; then
  echo "export PATH=${INSTALL_DIR}/bin:\$PATH" >> ~/.bashrc
fi
export PATH=${INSTALL_DIR}/bin:$PATH

echo ">>> DONE! MariaDB ${MARIADB_VERSION} đã được cài đặt tại ${INSTALL_DIR}"
echo ">>> Chạy lệnh sau để thiết lập mật khẩu root:"
echo "    mysql_secure_installation --socket=${INSTALL_DIR}/mysql.sock"

