#!/bin/sh

# Search ashyterm folder
for i in /usr/lib/python3.{25..6}/site-packages/ashyterm /usr/share/ashyterm; do
    if [[ -e "$i" ]]; then
        cd "$i"
        break
    fi
done

execute_ashy="python3 __init__.py"


execute_with_memory_verification() {
    # Changes the default memory allocator to reduce GTK4 memory leaks.
    #
    # Preference order (most-compatible first):
    #   1. mimalloc  — works on Manjaro stable and testing.
    #   2. tcmalloc  — works on Manjaro stable and testing.
    #   3. jemalloc  — last resort. Skipped when ASHYTERM_DISABLE_JEMALLOC=1
    #                  is set; on glibc 2.41+ / GLib 2.86+ jemalloc 5.3.x
    #                  has been seen to abort with
    #                  "GLib-ERROR: g_malloc: failed to allocate N bytes"
    #                  during startup (heap-corruption symptom from the
    #                  allocator, not from ashyterm itself).
    #   4. system malloc fallback.
    #
    # Override: ASHYTERM_NO_PRELOAD=1 disables LD_PRELOAD entirely.
    if [[ "$ASHYTERM_NO_PRELOAD" = "1" || ! -e /usr/lib/libchildenv.so ]]; then
        exec $execute_ashy "$@"
    fi

    if [[ -e /usr/lib/libmimalloc.so ]]; then
        LD_PRELOAD="libchildenv.so:libmimalloc.so" MIMALLOC_PURGE_DELAY=0 CHILD_ENV_RULES="LD_PRELOAD,MIMALLOC_PURGE_DELAY,CHILD_ENV_RULES" exec $execute_ashy "$@"
    elif [[ -e /usr/lib/libtcmalloc.so ]]; then
        LD_PRELOAD="libchildenv.so:libtcmalloc.so" TCMALLOC_AGGRESSIVE_DECOMMIT=1 CHILD_ENV_RULES="LD_PRELOAD,TCMALLOC_AGGRESSIVE_DECOMMIT,CHILD_ENV_RULES" exec $execute_ashy "$@"
    elif [[ "$ASHYTERM_DISABLE_JEMALLOC" != "1" && -e /usr/lib/libjemalloc.so ]]; then
        LD_PRELOAD="libchildenv.so:libjemalloc.so" MALLOC_CONF="narenas:1" CHILD_ENV_RULES="LD_PRELOAD,MALLOC_CONF,CHILD_ENV_RULES" exec $execute_ashy "$@"
    else
        exec $execute_ashy "$@"
    fi
}

convert_to_ssh() {
    local path="$*"

    # Detects if it is KIO (sftp/fish)
    if [[ $path =~ /(sftp|fish)/([^/]+)(/.*)? ]]; then
        local protocol="${BASH_REMATCH[1]}"
        local user_host_port="${BASH_REMATCH[2]}"
        local kio_path="${BASH_REMATCH[3]}"

        # Checks if there is a port in the KIO format
        if [[ $user_host_port =~ ^([^@]+@[^:]+):([0-9]+)$ ]]; then
            local user_host="${BASH_REMATCH[1]}"
            local port="${BASH_REMATCH[2]}"
            execute_with_memory_verification --ssh "$user_host:$port$kio_path"
        else
            execute_with_memory_verification --ssh "$user_host_port$kio_path"
        fi

    # Detects if it is GVFS (sftp:host=,port=,user=)
    elif [[ $path =~ /gvfs/sftp:host=([^,]+),port=([^,]+),user=([^,/]+)/(.+) ]]; then
        local host="${BASH_REMATCH[1]}"
        local port="${BASH_REMATCH[2]}"
        local user="${BASH_REMATCH[3]}"
        local folder="${BASH_REMATCH[4]}"
        execute_with_memory_verification --ssh "$user@$host:$port/$folder"

    # Detects if it is GVFS without a port (sftp:host=,user=)
    elif [[ $path =~ /gvfs/sftp:host=([^,]+),user=([^,/]+)/(.+) ]]; then
        local host="${BASH_REMATCH[1]}"
        local user="${BASH_REMATCH[2]}"
        local folder="${BASH_REMATCH[3]}"
        execute_with_memory_verification --ssh "$user@$host/$folder"

    # If it is not any of the known formats, pass the parameters directly
    else
        execute_with_memory_verification "$@"
    fi
}

if [[ "$1" = "--convert-to-ssh" ]]; then
    shift  # Remove --convert-to-ssh from arguments
    convert_to_ssh "$@"
else
    execute_with_memory_verification "$@"
fi
