#!/bin/bash
# -*- coding: utf-8 -*-
# Timestamp: "2025-09-26 18:00:21 (ywatanabe)"
# File: ./paper/scripts/shell/modules/compilation_structure_tex_to_compiled_tex.sh

ORIG_DIR="$(pwd)"
THIS_DIR="$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)"
LOG_PATH="$THIS_DIR/.$(basename $0).log"
echo > "$LOG_PATH"

GIT_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)"

GRAY='\033[0;90m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo_info() { echo -e "${GRAY}INFO: $1${NC}"; }
log_info() {
    if [ "${SCITEX_LOG_LEVEL:-1}" -ge 2 ]; then
        echo -e "  \033[0;90m→ $1\033[0m"
    fi
}
echo_success() { echo -e "${GREEN}SUCC: $1${NC}"; }
echo_warning() { echo -e "${YELLOW}WARN: $1${NC}"; }
echo_error() { echo -e "${RED}ERRO: $1${NC}"; }
echo_header() { echo_info "=== $1 ==="; }
# ---------------------------------------

# Configurations
source ./config/load_config.sh $SCITEX_WRITER_DOC_TYPE

# Logging
touch "$LOG_PATH" >/dev/null 2>&1
echo
log_info "Running $0 ..."

gather_tex_contents() {
    # Use fast Python-based recursive expansion (O(n) instead of O(n²))
    if python3 ./scripts/python/compile_tex_structure.py \
        "$SCITEX_WRITER_BASE_TEX" \
        "$SCITEX_WRITER_COMPILED_TEX" \
        --quiet; then
        echo_success "    $SCITEX_WRITER_COMPILED_TEX compiled"
    else
        echo_error "    Failed to compile TeX structure"
        return 1
    fi
}

gather_tex_contents

# EOF