#!/bin/bash
# ============================================================
# Script: script_alineamiento_WN16_a_WN30.sh
# Description: Executes in sequence two Python programs needed
# to align  WN16 into WN30, generating the file wn_s.pl.
#
# Automatically activates the 'venv' virtual environment if present.
# ============================================================

set -euo pipefail

# ------------------------------
# Helper function
# ------------------------------
run_py() {
    local script="$1"
    shift
    echo "→ Running: $script"
    python3 "$script" "$@" || { echo "ERROR: $script failed"; deactivate 2>/dev/null; exit 1; }
    echo "✓ Completed: $script"
}

# ------------------------------
# Activate virtual environment
# ------------------------------
if [ -d "venv" ]; then
    echo "=== Activating virtual environment (venv) ==="
    source venv/bin/activate || echo "Warning: Could not activate virtual environment."
else
    echo "Virtual environment 'venv' not found. Using global Python."
fi
echo

# ------------------------------
# 1) Process corpus files
# ------------------------------
echo "=== Step 1: Mapping WN1.6 into WN3.0 ==="
run_py 1-map_wn16_to_wn30.py --wn16 index_sense16  --wn30 index_sense30

echo "=== Step 1 completed successfully ==="
echo

# ------------------------------
# 2) Mapping
# ------------------------------
echo "=== Step 2: Translating lemmas in rough_mapping.csv to Spanish ==="
run_py 2-sp_convert_mapping.py
echo
echo "=== Step 2 completed successfully ==="
echo

# ------------------------------
# Deactivate virtual environment
# ------------------------------
deactivate 2>/dev/null || true

echo "=== All processes completed successfully ==="
