#!/bin/bash

# Exit immediately if a command exits with a non-zero status
set -e

# Set up some colors for use in the script
reset='\e[0m'
green='\e[38;5;78m'
gold='\e[38;5;214m'
red='\e[38;5;124m'

# Change to the directory containing the script
cd "$(dirname "$0")"

# Check if config.mud exists
config_file="$(pwd)/config.mud"
if [[ ! -f "$config_file" ]]; then
  echo -e "${red}[ERROR]${reset} ${config_file} does not exist."
  exit 1
fi

# Record the top of the repository directory
repo_root=$(cd ../.. && pwd)

# Absolute path to the driver executable
driver="${repo_root}/adm/dist/bin/driver"

# Pull FluffOS

# Ensure the FluffOS driver source is present and tracking master
if [ ! -d "fluffos" ] || [ -z "$(ls -A fluffos)" ]; then
  # fluffos/ doesn't exist or is empty - clone it fresh
  git clone https://github.com/fluffos/fluffos.git fluffos 1>/dev/null
fi

cd fluffos
git fetch origin
git checkout master
git reset --hard origin/master

# Copy our custom local_options to the driver for compilation
cp -v ../local_options src/local_options

# Compile the driver
rm -rf build
mkdir build
cd build

# The build directory is thrown away every run, so ccache is what keeps repeat
# builds quick. Use it when it is installed, and carry on without it when it is
# not — nothing here depends on it.
ccache_args=()
if command -v ccache >/dev/null 2>&1; then
  ccache_args=(
    -DCMAKE_C_COMPILER_LAUNCHER=ccache
    -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
  )
  echo -e "${gold}[INFO]${reset} ccache found - using it to speed up compilation."
else
  echo -e "${gold}[INFO]${reset} ccache not found - compiling without it."
fi

# OXIDUS_CMAKE_EXTRA lets callers append cmake flags without editing this script.
# It is empty by default (local builds stay -march=native). The Docker build sets
# it to "-DMARCH_NATIVE=OFF" so the published driver runs on any x86-64 CPU.
cmake .. \
  -DPACKAGE_MUDLIB_STATS=OFF \
  -DPACKAGE_DB_MYSQL=0 \
  -DPACKAGE_DB=1 \
  -DPACKAGE_DB_SQLITE=3 \
  -DPACKAGE_UIDS=OFF \
  "${ccache_args[@]}" \
  ${OXIDUS_CMAKE_EXTRA:-}

make -j "$(nproc)" install

# Copy the compiled binaries to the target directory
cp -vf bin/driver ${repo_root}/adm/dist/bin/
cp -vf bin/lpcc ${repo_root}/adm/dist/bin/
cp -vf bin/o2json ${repo_root}/adm/dist/bin/
cp -vf bin/json2o ${repo_root}/adm/dist/bin/

# Copy the driver headers to the target include directory
mkdir -p ${repo_root}/include/driver
cp -avf bin/include/*.h ${repo_root}/include/driver/

# Restore the original source back to the original state
cd ..
git reset --hard HEAD > /dev/null

# Update config.mud with the correct absolute paths
mudlib_path="${repo_root}"
log_path="${repo_root}/log"

# Escape slashes for sed
escaped_mudlib_path=$(echo $mudlib_path | sed 's/\//\\\//g')
escaped_log_path=$(echo $log_path | sed 's/\//\\\//g')

# Update the config.mud file
sed -i "s|^mudlib directory.*|mudlib directory : $escaped_mudlib_path|" $config_file
sed -i "s|^log directory.*|log directory : $escaped_log_path|" $config_file

echo -e ""
echo -e "${green}[SUCCESS]${reset} FluffOS Driver has been compiled and is ready."
echo -e ""
echo -e "${green}[SUCCESS]${reset} You can execute ${green}${repo_root}/adm/dist/run${reset} to start the driver and game."
