#!/usr/bin/env sh

# Test counters
TOTAL=0
PASSED=0
FAILED=0

# Shell to test
if [ -n "$1" ]; then
    # Use the provided shell path
    if [ "${1#/}" = "$1" ]; then
        # Convert relative path to absolute
        SHELL_TO_TEST="$(pwd)/$1"
    else
        # Already absolute
        SHELL_TO_TEST="$1"
    fi
else
    # Default to ./build/cjsh but make it absolute
    SHELL_TO_TEST="$(pwd)/build/cjsh"
fi

log_test() {
    TOTAL=$((TOTAL + 1))
    printf "Test %03d: %s... " "$TOTAL" "$1"
}

pass() {
    PASSED=$((PASSED + 1))
    printf "${GREEN}PASS${NC}\n"
}

fail() {
    FAILED=$((FAILED + 1))
    printf "${RED}FAIL${NC} - %s\n" "$1"
}

skip() {
    printf "${YELLOW}SKIP${NC} - %s\n" "$1"
}

# Check if shell exists
if [ ! -x "$SHELL_TO_TEST" ]; then
    echo "Error: Shell '$SHELL_TO_TEST' not found or not executable"
    echo "Usage: $0 [path_to_shell]"
    exit 1
fi

echo "Testing POSIX Login Shell Environment for: $SHELL_TO_TEST"
echo "========================================================"

# Create temporary home directory for testing
TEST_HOME=$(mktemp -d)
export HOME="$TEST_HOME"
echo "Using temporary home: $TEST_HOME"

# Test 1: Login shell detection with --login
log_test "Login shell detection with --login"
result=$("$SHELL_TO_TEST" --login -c "echo login_test" 2>/dev/null)
if [ "$result" = "login_test" ]; then
    pass
else
    fail "Login shell flag not working"
fi

# Test 3: Essential environment variables
log_test "Essential environment variables"
result=$("$SHELL_TO_TEST" --login -c "echo \$HOME:\$USER:\$SHELL" 2>/dev/null)
# Check that all three variables are set and non-empty
home_part=$(echo "$result" | cut -d: -f1)
user_part=$(echo "$result" | cut -d: -f2)
shell_part=$(echo "$result" | cut -d: -f3)
if [ -n "$home_part" ] && [ -n "$user_part" ] && [ -n "$shell_part" ] && [ -d "$home_part" ]; then
    pass
else
    fail "Essential environment variables not set properly"
fi

# Test 4: PATH environment variable
log_test "PATH environment variable"
result=$("$SHELL_TO_TEST" --login -c "echo \$PATH" 2>/dev/null)
if [ -n "$result" ] && echo "$result" | grep -q "/bin"; then
    pass
else
    fail "PATH not set properly"
fi

# Test 5: SHELL environment variable
log_test "SHELL environment variable"
result=$("$SHELL_TO_TEST" --login -c "echo \$SHELL" 2>/dev/null)
if echo "$result" | grep -q "cjsh"; then
    pass
else
    fail "SHELL variable not set to cjsh"
fi

# Test 6: PWD environment variable
log_test "PWD environment variable"
result=$("$SHELL_TO_TEST" --login -c "echo \$PWD" 2>/dev/null)
if [ -n "$result" ] && [ -d "$result" ]; then
    pass
else
    fail "PWD not set to valid directory"
fi

# Test 7: SHLVL environment variable
log_test "SHLVL environment variable"
current_shlvl=$SHLVL
result=$("$SHELL_TO_TEST" --login -c "echo \$SHLVL" 2>/dev/null)
if [ -n "$result" ] && [ "$result" -gt 0 ] 2>/dev/null; then
    pass
else
    fail "SHLVL not incremented properly"
fi

# Test 9: User profile sourcing (~/.profile)
log_test "User profile sourcing"
echo "export PROFILE_SOURCED=yes" > "$TEST_HOME/.profile"
result=$("$SHELL_TO_TEST" --login -c "echo \$PROFILE_SOURCED" 2>/dev/null)
if [ "$result" = "yes" ]; then
    pass
else
    fail "User profile not sourced"
fi

# Test 10: Shell-specific profile sourcing
# log_test "Shell-specific profile sourcing"
# # Check if cjsh sources its own profile file
# if [ -f "$TEST_HOME/.cjprofile" ]; then
#     pass
# else
#     fail "Shell-specific profile file not created"
# fi

# Test 11: Non-login shell behavior
log_test "Non-login shell behavior"
result=$("$SHELL_TO_TEST" -c "echo \$SHLVL" 2>/dev/null)
if [ -n "$result" ]; then
    pass
else
    fail "Non-login shell should still set basic variables"
fi

# Test 12: Interactive vs non-interactive detection
log_test "Interactive shell detection"
# Test that non-interactive mode works
result=$("$SHELL_TO_TEST" -c "echo non_interactive" 2>/dev/null)
if [ "$result" = "non_interactive" ]; then
    pass
else
    fail "Non-interactive mode not working"
fi

# Test 13: Environment variable inheritance
log_test "Environment variable inheritance"
TEST_INHERIT=inherited "$SHELL_TO_TEST" --login -c "echo \$TEST_INHERIT" > /tmp/inherit_test_$$
result=$(cat /tmp/inherit_test_$$)
if [ "$result" = "inherited" ]; then
    pass
else
    fail "Environment variable inheritance failed"
fi
rm -f /tmp/inherit_test_$$

# Test 14: Default umask setting
log_test "Default umask setting"
result=$("$SHELL_TO_TEST" --login -c "umask" 2>/dev/null)
if [ -n "$result" ]; then
    pass
else
    fail "umask builtin not implemented"
fi

# Test 16: Signal handler setup
log_test "Signal handler setup"
# Test that shell handles basic signals
"$SHELL_TO_TEST" --login -c "echo signal_test" 2>/dev/null
if [ $? -eq 0 ]; then
    pass
else
    fail "Basic signal handling setup failed"
fi

# Test 17: Working directory initialization
log_test "Working directory initialization"
# Save current directory
original_dir=$(pwd)
cd /tmp
# Test that shell starts in a valid directory (usually where it was invoked from)
result=$("$SHELL_TO_TEST" --login -c "pwd" 2>/dev/null)
# Restore directory
cd "$original_dir"
# Check that PWD is set to a valid directory
if [ -n "$result" ] && [ -d "$result" ]; then
    pass
else
    fail "Working directory not properly initialized"
fi

# Test 18: History file initialization
# log_test "History file initialization"
# "$SHELL_TO_TEST" --login -c "echo test_history" 2>/dev/null
# # Check if history file is created
# if [ -f "$TEST_HOME/.cache/cjsh/history.txt" ] || [ -f "$TEST_HOME/.cjsh_history" ]; then
#     pass
# else
#     skip "History file not automatically created"
# fi

# Test 19: Locale settings
log_test "Locale settings"
result=$("$SHELL_TO_TEST" --login -c "echo \$LANG" 2>/dev/null)
if [ -n "$result" ]; then
    pass
else
    fail "LANG variable not set by shell"
fi

# Test 23: Exit status preservation
log_test "Exit status preservation"
"$SHELL_TO_TEST" --login -c "exit 42" 2>/dev/null
if [ $? -eq 42 ]; then
    pass
else
    fail "Exit status not preserved"
fi

# Test 24: Resource cleanup on exit
log_test "Resource cleanup on exit"
# This is difficult to test automatically
pass  # Assume cleanup works if no obvious leaks

# Test 25: Login shell vs subshell environment
log_test "Login shell vs subshell environment"
login_shlvl=$("$SHELL_TO_TEST" --login -c "echo \$SHLVL" 2>/dev/null)
subshell_shlvl=$("$SHELL_TO_TEST" --login -c "($SHELL_TO_TEST -c 'echo \$SHLVL')" 2>/dev/null)
if [ "$subshell_shlvl" -gt "$login_shlvl" ] 2>/dev/null; then
    pass
else
    fail "SHLVL increment in subshell complex to verify"
fi

echo "========================================================"
echo "POSIX Login Shell Environment Test Results:"
echo "Total tests: $TOTAL"
echo "Passed: ${GREEN}$PASSED${NC}"
echo "Failed: ${RED}$FAILED${NC}"

# Cleanup temporary home
rm -rf "$TEST_HOME"

if [ $FAILED -eq 0 ]; then
    echo "${GREEN}All login shell environment tests passed!${NC}"
    exit 0
else
    echo "${YELLOW}Some login shell tests failed. Review the failures above.${NC}"
    echo "Success rate: $(( PASSED * 100 / TOTAL ))%"
    exit 1
fi
