#!/bin/bash

# This script installs dependencies and builds cmapi package
# Should be executed by root
# Should reside in build/ (or storage/columnstore/columnstore/build), as all the paths a relative to script's location

set -eo pipefail

SCRIPT_LOCATION=$(dirname "$0")
COLUMNSTORE_SOURCE_PATH=$(realpath "$SCRIPT_LOCATION"/../)

source "$SCRIPT_LOCATION"/utils.sh

optparse.define short=d long=distro desc="distro" variable=OS
source $(optparse.build)
echo "Arguments received: $@"

if [ "$EUID" -ne 0 ]; then
  error "Please run script as root"
  exit 1
fi

if [[ -z "${OS:-}" ]]; then
  echo "Please provide provide --distro parameter, e.g. ./build_cmapi.sh --distro ubuntu:22.04"
  exit 1
fi

select_pkg_format ${OS}

if [[ "$(arch)" == "arm64" || "$(arch)" == "aarch64" ]]; then
  export CC=gcc
fi

on_exit() {
  if [[ $? -eq 0 ]]; then
    echo "Cmapi package has been build successfully."
  else
    echo "Cmapi package build failed!"
  fi
}
trap on_exit EXIT

get_cmapi_git_revision() {
  # Prefer explicit CMAPI_GIT_REVISION; fallback to DRONE_COMMIT; otherwise read current repo revision
  local rev="${CMAPI_GIT_REVISION:-${DRONE_COMMIT:-}}"
  if [[ -z "$rev" ]]; then
    rev="$(git -C "$COLUMNSTORE_SOURCE_PATH" rev-parse --short=12 HEAD 2>/dev/null || echo unknown)"
  fi
  echo "$rev"
}

install_deps() {
  echo "Installing dependencies..."

  cd "$COLUMNSTORE_SOURCE_PATH"/cmapi

  if is_rocky_version $OS 9; then
    retry_eval 5 "dnf install -q -y libxcrypt-compat yum-utils"
    retry_eval 5 "dnf config-manager --set-enabled devel && dnf update -q -y" #to make redhat-lsb-core available for rocky 9
  fi

  #no redhat-lsb-release for rockylinux >=10
  if is_rocky_version $OS && ! is_rocky_version_ge $OS 10; then
    retry_eval 5 "dnf update -q -y && dnf install -q -y redhat-lsb-core"
  fi


  if is_rocky_version_ge $OS 10; then
    retry_eval 5 "dnf update -q -y && dnf install -q -y libxcrypt-compat"
  fi

  if [[ "$PKG_FORMAT" == "rpm" ]]; then
    retry_eval 5 "dnf update -q -y && dnf install -q -y epel-release wget zstd findutils gcc cmake make rpm-build libarchive"
  else
    retry_eval 5 "apt-get update -qq -o Dpkg::Use-Pty=0 && apt-get install -qq -o Dpkg::Use-Pty=0 wget zstd findutils gcc cmake make dpkg-dev lsb-release"
  fi

  if [ "$(arch)" == "x86_64" ]; then
    PYTHON_URL="https://github.com/astral-sh/python-build-standalone/releases/download/20260127/cpython-3.13.11+20260127-x86_64_v2-unknown-linux-gnu-pgo+lto-full.tar.zst"
    PYTHON_SHA="b5d915b716609b9f60e17ab8654cdf0bd810f52f710b743d0fd75bb85db1cbac"
  elif [[ "$(arch)" == "arm64" || "$(arch)" == "aarch64" ]]; then
    PYTHON_URL="https://github.com/astral-sh/python-build-standalone/releases/download/20260127/cpython-3.13.11+20260127-aarch64-unknown-linux-gnu-pgo+lto-full.tar.zst"
    PYTHON_SHA="c0065a0e7afe48bad9471b7f9daafe3331afa900e19051510ef7696926df759e"
  else
    echo "Unsupported architecture: $(arch)"
    exit 1
  fi
  PYTHON_FILE=PYTHON_FILE="${PYTHON_URL##*/}"

  rm -rf python pp
  wget -q "$PYTHON_URL" -O "${PYTHON_FILE}" || { echo "Download failed"; exit 1; }
  echo "Checking sha256"
  echo "${PYTHON_SHA}  ${PYTHON_FILE}" | sha256sum -c || { echo "Sha256 does not match, exiting"; exit 1; }
  tar --use-compress-program=unzstd -xf "${PYTHON_FILE}" -C ./
  rm "${PYTHON_FILE}"

  mv python pp
  mv pp/install python
  chown -R root:root python

  python/bin/pip3 install -t deps --only-binary :all -r requirements.txt
  cp cmapi_server/cmapi_server.conf cmapi_server/cmapi_server.conf.default
}

build_cmapi() {
  cd "$COLUMNSTORE_SOURCE_PATH"/cmapi
  ./cleanup.sh
  CMAPI_GIT_REVISION_ARG="$(get_cmapi_git_revision)"
  cmake -D"${PKG_FORMAT^^}"=1 -DCMAPI_GIT_REVISION="$CMAPI_GIT_REVISION_ARG" . && make package
}
install_deps
build_cmapi
