#!/bin/bash
set -e

if [ $# -eq 0 ]; then
    echo "Usage: $0 <language1> [language2]..."
    exit 1
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Remove language files
for lang in "$@"; do
    rm -f "$SCRIPT_DIR/src_c/stem_UTF_8_${lang}.c"
    rm -f "$SCRIPT_DIR/src_c/stem_UTF_8_${lang}.h"
done

# Regenerate modules.h based on remaining language files
cd "$SCRIPT_DIR"

# Get sorted list of remaining languages for header comment
LANGS=$(ls src_c/stem_UTF_8_*.h 2>/dev/null | sed 's/.*stem_UTF_8_\(.*\)\.h/\1/' | sort | tr '\n' ', ' | sed 's/, $//' || echo "")

{
    echo "/* libstemmer/modules.h: List of stemming modules."
    echo " *"
    echo " * This file is generated by mkmodules.pl from a list of module names."
    echo " * Do not edit manually."
    echo " *"
    if [ -n "$LANGS" ]; then
        echo " * Modules included by this file are: $LANGS"
    else
        echo " * Modules included by this file are: (none)"
    fi
    echo " */"
    echo
    
    # Include headers for remaining languages
    for f in src_c/stem_UTF_8_*.h; do
        [ -e "$f" ] || continue
        lang=$(basename "$f" .h | sed 's/stem_UTF_8_//')
        echo "#include \"../src_c/stem_UTF_8_${lang}.h\""
    done
    
    echo
    echo "typedef enum {"
    echo "  ENC_UNKNOWN=0,"
    echo "  ENC_UTF_8"
    echo "} stemmer_encoding_t;"
    echo
    echo "struct stemmer_encoding {"
    echo "  const char * name;"
    echo "  stemmer_encoding_t enc;"
    echo "};"
    echo "static const struct stemmer_encoding encodings[] = {"
    echo "  {\"UTF_8\", ENC_UTF_8},"
    echo "  {0,ENC_UNKNOWN}"
    echo "};"
    echo
    echo "struct stemmer_modules {"
    echo "  const char * name;"
    echo "  stemmer_encoding_t enc;"
    echo "  struct SN_env * (*create)(void);"
    echo "  void (*close)(struct SN_env *);"
    echo "  int (*stem)(struct SN_env *);"
    echo "};"
    echo "static const struct stemmer_modules modules[] = {"
    
    # Generate entries based on modules.txt but only for languages we still have
    while IFS= read -r line; do
        # Skip comments and empty lines
        [[ "$line" =~ ^[[:space:]]*# ]] && continue
        [[ "$line" =~ ^[[:space:]]*$ ]] && continue
        
        # Parse: algorithm UTF_8 aliases
        alg=$(echo "$line" | awk '{print $1}')
        aliases=$(echo "$line" | awk '{print $3}')
        
        # Check if we still have this language file
        if [[ -f "src_c/stem_UTF_8_${alg}.h" ]]; then
            # Split aliases and create entries
            IFS=',' read -ra ALIAS_ARRAY <<< "$aliases"
            for alias in "${ALIAS_ARRAY[@]}"; do
                echo "  {\"${alias}\", ENC_UTF_8, ${alg}_UTF_8_create_env, ${alg}_UTF_8_close_env, ${alg}_UTF_8_stem},"
            done
        fi
    done < libstemmer/modules.txt
    
    echo "  {0,ENC_UNKNOWN,0,0,0}"
    echo "};"
    echo "static const char * algorithm_names[] = {"
    
    for f in src_c/stem_UTF_8_*.h; do
        [ -e "$f" ] || continue
        lang=$(basename "$f" .h | sed 's/stem_UTF_8_//')
        echo "  \"${lang}\", "
    done
    
    echo "  0"
    echo "};"
    
} > libstemmer/modules.h

# Update CMakeLists.txt with remaining language sources
echo "Updating CMakeLists.txt..."
{
    echo "# Snowball stemming library"
    echo "# Build the libstemmer C library"
    echo
    echo "set(SNOWBALL_SOURCE_DIR \${CMAKE_CURRENT_SOURCE_DIR})"
    echo
    echo "# Source files for libstemmer"
    echo "set(LIBSTEMMER_SOURCES"
    echo "  \${SNOWBALL_SOURCE_DIR}/libstemmer/libstemmer.c"
    echo "  \${SNOWBALL_SOURCE_DIR}/runtime/api.c"
    echo "  \${SNOWBALL_SOURCE_DIR}/runtime/utilities.c"
    echo ")"
    echo
    echo "# Generated stemmer sources (UTF-8 versions for supported languages)"
    echo "set(STEMMER_SOURCES"
    for f in src_c/stem_UTF_8_*.c; do
        [ -e "$f" ] || continue
        lang=$(basename "$f" .c | sed 's/stem_UTF_8_//')
        echo "  \${SNOWBALL_SOURCE_DIR}/src_c/stem_UTF_8_${lang}.c"
    done
    echo ")"
    echo
    echo "# Create the snowball library"
    echo "add_library(snowball STATIC \${LIBSTEMMER_SOURCES} \${STEMMER_SOURCES})"
    echo
    echo "# Set include directories"
    echo "target_include_directories(snowball PUBLIC" 
    echo "  \${SNOWBALL_SOURCE_DIR}/include"
    echo "  \${SNOWBALL_SOURCE_DIR}"
    echo ")"
    echo
    echo "# Set compile flags to match the original build"
    echo "target_compile_options(snowball PRIVATE -w) # Suppress warnings from third-party code"
    echo
    echo "# Export the target"
    echo "set_target_properties(snowball PROPERTIES"
    echo "  POSITION_INDEPENDENT_CODE ON"
    echo "  CXX_STANDARD 20"
    echo ")"
} > CMakeLists.txt

echo "Removed languages: $*"
echo "Updated modules.h and CMakeLists.txt to exclude removed languages"
