#!/bin/bash

set -pu

function call_bash_ensure_ini_config {
    {{{ bash_ensure_ini_config("$1", "$2", "$3", "$4") | indent(4) }}}
}

is_old_bats=0

setup() {
    if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then
        BATS_TEST_TMPDIR="$(mktemp -d)"  # 1.4.0
        # shellcheck disable=SC2034
        BATS_TEARDOWN_STARTED=  # 1.3.0
        is_old_bats=1
    else
        is_old_bats=0
    fi
    pushd "${BATS_TEST_TMPDIR}" || exit 1
    mkdir -p sssd_test
}

teardown() {
    if (( is_old_bats )); then
        if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then
            >&2 echo "INTERNAL ERROR"
            exit 3
        fi
        local tmppath xpwd
        tmppath="$(readlink -f -- "${BATS_TEST_TMPDIR}")"
        if [[ ! "${tmppath}" =~ ^/tmp/ ]] || [[ ! -d "${tmppath}" ]]; then
            >&2 echo "INTERNAL ERROR"
            exit 3
        fi
        xpwd="$(readlink -f -- .)"
        if [[ "${tmppath}" != "${xpwd}" ]]; then
            >&2 echo "INTERNAL ERROR"
            exit 3
        fi
        popd || exit 1
        rm -rf -- "${tmppath}"
        BATS_TEST_TMPDIR=""
    fi
}

@test "bash_ensure_ini_config - Basic value remediation" {
    printf "[pam]\npam_cert_auth = false\n" > sssd_test/sssd.conf
    expected_output="[pam]\npam_cert_auth = true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Value remediation in multiple files" {
    printf "[pam]\npam_cert_auth = false\n" > sssd_test/sssd.conf
    printf "[pam]\npam_cert_auth = false\n" > pam_cert_auth.conf
    expected_output="[pam]\npam_cert_auth = true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf pam_cert_auth.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    run diff "pam_cert_auth.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - No remediation happened" {
    printf "[pam]\npam_cert_auth = true\n" > sssd_test/sssd.conf
    expected_output="[pam]\npam_cert_auth = true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Append section with option to empty file" {
    printf "" > sssd_test/sssd.conf
    expected_output="[pam]\npam_cert_auth = true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Create file with section and option" {
    expected_output="[pam]\npam_cert_auth = true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Append option to section" {
    printf "[pam]\n" > sssd_test/sssd.conf
    expected_output="[pam]\npam_cert_auth = true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Append option to section when section is substring of option" {
    printf "[pam]\n" > sssd_test/sssd.conf
    expected_output="[pam]\npam_verbosity = 1\npam_cert_auth = true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"
    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_verbosity" "1"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Append option to section in multiple files" {
    printf "[pam]\n" > sssd_test/sssd.conf
    printf "[pam]\n" > pam_cert_auth.conf
    expected_output="[pam]\npam_cert_auth = true\n"

    call_bash_ensure_ini_config "pam_cert_auth.conf sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]

     run diff "pam_cert_auth.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Append section with option to non-empty file" {
    printf "[section]\nkey = value\n" > sssd_test/sssd.conf
    expected_output="[section]\nkey = value\n[pam]\npam_cert_auth = true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}
