#!/usr/bin/sh
#----------------------------------------------------------------------------
# /var/install/config.d/dnscrypt-proxy-update.sh - update or generate new dnscrypt-_proxy configuration
#
# Creation:     2025-11-17  hbfl
# Last Update:  $Id$
#
# Copyright (c) 2021-@@YEAR@@ the eisfair team, team(at)eisfair(dot)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 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

# set package name
package_name='dnscrypt-proxy'

# include configib
. /var/install/include/configlib

VERSION=3.6.0

# ---------------------------------------------------------------------------
# set the default values for configuration
# ---------------------------------------------------------------------------
get_defaults()
{
    START_DNSCRYPT_PROXY='no'
}

# ---------------------------------------------------------------------------
# read old configuration
# ---------------------------------------------------------------------------
rename_old_variables()
{
    # read old values
    if [ -f /etc/config.d/${package_name} ]
    then
        /var/install/bin/backup-file --quiet ${package_name}
        . /etc/config.d/${package_name}
    fi
}

# ---------------------------------------------------------------------------
# write config and default files
# ---------------------------------------------------------------------------
make_config_file()
{
    internal_conf_file=${1}

    {
    #------------------------------------------------------------------------
    printgpl --conf ${package_name} '2025-11-17' 'hbfl'
    #------------------------------------------------------------------------
    echo "# dnscrypt-proxy-VERSION ${VERSION}"

    #------------------------------------------------------------------------
    printgroup 'Basic configuration'
    #------------------------------------------------------------------------

    printvar 'START_DNSCRYPT_PROXY'
    printcomment            'start dnscrypt-proxy: yes or no'

    #------------------------------------------------------------------------
    printend
    #------------------------------------------------------------------------

    } > ${internal_conf_file}
    # Set rights
    chmod 0600 ${internal_conf_file}
    chown root ${internal_conf_file}

}
# ---------------------------------------------------------------------------
# create check.d file
# ---------------------------------------------------------------------------
make_check_file()
{
printgpl --check ${package_name} '2025-11-17' 'hbfl' >/etc/check.d/${package_name}
cat >>/etc/check.d/${package_name} <<EOF
# Variable            OPT_VARIABLE        VARIABLE_N    VALUE
START_DNSCRYPT_PROXY  -                   -             YESNO
EOF

    # Set rights for check file
    chmod 0600 /etc/check.d/${package_name}
    chown root /etc/check.d/${package_name}
}
# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
main()
{
    get_defaults

    make_config_file /etc/default.d/${package_name}

    # update from old version
    rename_old_variables

    # write new config file
    make_config_file /etc/config.d/${package_name}

    # write check.d file
    make_check_file

    exit 0
}

# ---------------------------------------------------------------------------
# call function main
# ---------------------------------------------------------------------------
main "${@}"

# ---------------------------------------------------------------------------
# end
# ---------------------------------------------------------------------------
