#!/usr/bin/env bash
# Samba active directory provision
# Tool for provision samba active directory
#
# Copyright (C) 2024 Evgenii Sozonov <arzdez@altlinux.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# shellcheck disable=SC1091

. shell-ini-config

set -euo pipefail

PATH_TO_ENTRY="/usr/share/alterator/services/service-samba-ad.service"

update_toml_field() {
    local section="$1"
    local key="$2"
    local value="$3"

    ini_config_set "$PATH_TO_ENTRY" "$section" "$key" "$value"
    sed -i 's/\t//g' "$PATH_TO_ENTRY"
    sed -i '/^\['"$section"'\]/,/^\[/{s/^\(default\s*=\s*\)\([^"]\)/\1"\2/; s/^\(default\s*=\s*"[^"]*\)\([^"]\)$/\1\2"/}' "$PATH_TO_ENTRY"
    return 0
}

update_realm() {
    local realm=
    local hostname=
    realm=$(hostname -d)
    hostname=$(hostname -s)

    if [ "$hostname" = "$realm" ]; then
        sed -i '/^\[parameters\.realm\]/,/^\[/{/^default\s*=/d}' "$PATH_TO_ENTRY"
        return 0
    fi

    update_toml_field "parameters.realm" "default" "$realm"
    return 0
}

update_hostname() {
    local hostname=
    hostname=$(hostname -s)

    update_toml_field "parameters.hostNetBiosName" "default" "$hostname"
    return 0
}

update_domain_netbios_name() {
    local domain_netbios_name=
    local hostname=
    domain_netbios_name=$(hostname -f | cut -d. -f2)
    hostname=$(hostname -s)
    if [ "$domain_netbios_name" = "$hostname" ]; then
        sed -i '/^\[parameters\.netBiosName\]/,/^\[/{/^default\s*=/d}' "$PATH_TO_ENTRY"
        return 0
    fi

    update_toml_field "parameters.netBiosName" "default" "$domain_netbios_name"
    return 0
}

add_functional_level() {
    local functional_level="$1"
    local section="parameters.configureFunctionalLevel.values.$functional_level"

    if grep -q "^\[$section\]" $PATH_TO_ENTRY; then
        return 0
    fi

    sed -i '/^\[parameters\.configureFunctionalLevel\.values\.2016\]/i \
[parameters.configureFunctionalLevel.values.'"$functional_level"']\
display_name.en = '"\"${functional_level}\""'\
display_name.ru = '"\"${functional_level}\""'' $PATH_TO_ENTRY

    return 0
}

remove_functional_level() {
    local level_to_remove="$1"

    sed -i '/^\[parameters\.configureFunctionalLevel\.values\.'"$level_to_remove"'\]/,/^\[/{/^\[parameters\.configureFunctionalLevel\.values\.'"$level_to_remove"'\]/d; /^\[/!d}' $PATH_TO_ENTRY

    return 0
}

update_functional_level_enum() {
    local current_level=

    current_level="$(get_functional_level "Domain")" || current_level="unknown"

    if [ "$current_level" = "2012_R2" ]; then
        add_functional_level "2012_R2"
    else
        remove_functional_level "2012_R2"
    fi

    return 0
}

update_configure_functional_level_enum() {
    local current_level=

    current_level="$(get_functional_level "Domain")" || current_level="unknown"

    if [ "$current_level" = "2012_R2" ]; then
        remove_functional_level "2008_R2"
    elif [ "$current_level" = "2016" ]; then
        remove_functional_level "2008_R2"
        remove_functional_level "2012_R2"
    else
        add_functional_level "2008_R2"
        add_functional_level "2012_R2"
    fi

    return 0
}

update_available_dns_backend() {
    local bind_installed=

    if bind_is_installed; then
        bind_installed=true
    else
        bind_installed=false
    fi

    if [ "$bind_installed" = false ]; then
        sed -i '/^\[parameters\.dnsSettings\.properties\.dnsBackend\.values\.BIND9_DLZ\]/,/^prototype = "bindSettings"/{s/^/#/}' "$PATH_TO_ENTRY"
    else
        sed -i '/^#\?\[parameters\.dnsSettings\.properties\.dnsBackend\.values\.BIND9_DLZ\]/,/^#\?prototype = "bindSettings"/s/^#//' "$PATH_TO_ENTRY"
    fi

    return 0
}

update_entry() {

    if [ ! -f "$PATH_TO_ENTRY" ]; then
        echo "Error: File $PATH_TO_ENTRY not found" >&2
        return 1
    fi

    update_realm
    update_hostname
    update_domain_netbios_name
    update_functional_level_enum
    update_configure_functional_level_enum
    update_available_dns_backend
    return 0
}

post_install="$1"
if [ "$post_install" = "true" ]; then
    update_entry
fi
