#!/usr/bin/env bash

################################################################################
# 🅳🅾🆃🅵🅸🅻🅴🆂
# Script: environment.sh
# Version: 0.2.469
# Author: Sebastien Rousseau
# Copyright (c) 2015-2025. All rights reserved
# Description: Configure system environment variables and load dotfiles
# Website: https://dotfiles.io
# License: MIT
################################################################################

#-----------------------------------------------------------------------------
# Function: configure_system_info
#
# Description:
#   Configures system-related environment variables.
#
# Arguments:
#   None
#
# Returns:
#   0 on success, 1 on failure
#-----------------------------------------------------------------------------
configure_system_info() {
    # System information
    if ! HOSTNAME=$(hostname -f); then
        echo "Warning: Could not determine hostname" >&2
        return 1
    fi

    if ! OS_ARCH=$(uname -m); then
        echo "Warning: Could not determine system architecture" >&2
        return 1
    fi

    if ! OS_NAME=$(uname); then
        echo "Warning: Could not determine operating system" >&2
        return 1
    fi

    if ! OS_VERSION=$(uname -r); then
        echo "Warning: Could not determine system version" >&2
        return 1
    fi

    if ! USER=$(whoami); then
        echo "Warning: Could not determine username" >&2
        return 1
    fi

    # Export system variables
    export HOSTNAME
    export OS_ARCH
    export OS_NAME
    export OS_VERSION
    export USER
    export ARCHFLAGS="-arch ${OS_ARCH}"

    return 0
}

#-----------------------------------------------------------------------------
# Function: configure_locale_settings
#
# Description:
#   Configures locale and language settings.
#
# Arguments:
#   None
#
# Returns:
#   None
#-----------------------------------------------------------------------------
configure_locale_settings() {
    # Default language settings
    local USER_LANGUAGE="en_GB.UTF-8"

    # Export locale variables
    export LANG="${USER_LANGUAGE}"
    export LANGUAGE="${USER_LANGUAGE}"
    export LC_ALL="${USER_LANGUAGE}"
    export LC_CTYPE="${USER_LANGUAGE}"
}

#-----------------------------------------------------------------------------
# Function: configure_dotfiles_environment
#
# Description:
#   Configures dotfiles-specific environment variables.
#
# Arguments:
#   None
#
# Returns:
#   0 on success, 1 on failure
#-----------------------------------------------------------------------------
configure_dotfiles_environment() {
    # Dotfiles configuration
    export DOTFILES_VERSION='0.2.469'
    export DOTFILES="${HOME}/.dotfiles/lib"

    # Check if dotfiles directory exists
    if [[ ! -d "${DOTFILES}" ]]; then
        echo "Warning: Dotfiles directory not found: ${DOTFILES}" >&2
        return 1
    fi

    # Additional environment settings
    export INPUTRC="${HOME}/.inputrc"
    export SSL_CERT_FILE="${HOME}/cacert.pem"
    export TERM="xterm-256color"

    return 0
}

#-----------------------------------------------------------------------------
# Function: load_dotfiles
#
# Description:
#   Loads all .sh files from the dotfiles directory.
#
# Arguments:
#   None
#
# Returns:
#   0 on success, 1 if no files found
#-----------------------------------------------------------------------------
load_dotfiles() {
    local loaders_pattern="${DOTFILES}/*.sh"
    local found_files=0

    # Check if any matching files exist
    for loader in ${loaders_pattern}; do
        if [[ -f "${loader}" ]]; then
            found_files=1
            # shellcheck source=/dev/null
            if ! . "${loader}"; then
                echo "Warning: Failed to load ${loader}" >&2
            fi
        fi
    done

    if [[ ${found_files} -eq 0 ]]; then
        echo "Warning: No dotfiles found in ${DOTFILES}" >&2
        return 1
    fi

    return 0
}

#-----------------------------------------------------------------------------
# Main Execution
#-----------------------------------------------------------------------------

# Configure system information
configure_system_info || echo "Warning: System information configuration failed" >&2

# Configure locale settings
configure_locale_settings

# Configure dotfiles environment
configure_dotfiles_environment || echo "Warning: Dotfiles environment configuration failed" >&2

# Load dotfiles
load_dotfiles || echo "Warning: Dotfiles loading failed" >&2
