#! /bin/sh
#----------------------------------------------------------------------------
# /var/install/config.d/updatedb-update.sh - paramater update script
#
# Creation   :  2007-02-28 scholze
# Last update:  $Id: updatedb-update.sh 47703 2017-12-03 22:56:10Z hbfl $
#
# Copyright (c) 2007-2009 Sebastian Scholze webmaster(at)schlotze(dot)de
# Copyright (c) 2009-2018 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.
#----------------------------------------------------------------------------

package_name=updatedb

# include configlib for using printvar
. /var/install/include/configlib

# ---------------------------------------------------------------------------
# Set the default values for configuration
# ---------------------------------------------------------------------------
get_defaults()
{
    UPDATEDB_CRON_USE='no'
    UPDATEDB_CRON_TIMES='25 0 * * *'
    UPDATEDB_CRON_RUN_AS=''
    UPDATEDB_CRON_NICE='19'
    UPDATEDB_CRON_IONICE_CLASS='2'
    UPDATEDB_CRON_IONICE_PRIORITY='7'
    UPDATEDB_SUPPRESS_CRON_MAIL='no'


    UPDATEDB_PRUNEPATH_N='5'
    UPDATEDB_PRUNEPATH_1_ACTIVE='yes'
    UPDATEDB_PRUNEPATH_1='/tmp'
    UPDATEDB_PRUNEPATH_2_ACTIVE='yes'
    UPDATEDB_PRUNEPATH_2='/usr/tmp'
    UPDATEDB_PRUNEPATH_3_ACTIVE='yes'
    UPDATEDB_PRUNEPATH_3='/var/tmp'
    UPDATEDB_PRUNEPATH_4_ACTIVE='yes'
    UPDATEDB_PRUNEPATH_4='/run'
    UPDATEDB_PRUNEPATH_5_ACTIVE='yes'
    UPDATEDB_PRUNEPATH_5='/proc'
}
# ---------------------------------------------------------------------------
# read old configuration and rename old variables
# ---------------------------------------------------------------------------
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}" '2007-05-14' 'schlotze'
    # ------------------------------------------------------------------------

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

    printvar 'UPDATEDB_CRON_USE'
    printcomment            'run scheduled updates (yes/no)'

    printvar 'UPDATEDB_CRON_TIMES'
    printcomment            'schedule (in cron syntax)'

    printvar 'UPDATEDB_CRON_RUN_AS'
    printcomment            'default run as root, but for more security'
    printcomment            'run as nobody'

    printvar 'UPDATEDB_CRON_NICE'
    printcomment            'nice value to run'

    printvar 'UPDATEDB_CRON_IONICE_CLASS'
    printcomment            'ionice class to run'

    printvar 'UPDATEDB_CRON_IONICE_PRIORITY'
    printcomment            'ionice priority to run'

    printvar 'UPDATEDB_SUPPRESS_CRON_MAIL'

    # ------------------------------------------------------------------------
    printgroup 'Directory configurations'
    # ------------------------------------------------------------------------

    printvar 'UPDATEDB_PRUNEPATH_N'
    printcomment            'number of directories to prune'

    idx=1
    while [ ${idx} -le ${UPDATEDB_PRUNEPATH_N:-0} ]
    do
        eval active='${UPDATEDB_PRUNEPATH_'${idx}_'ACTIVE}'

        if [ -z "${active}" ]
        then
            eval UPDATEDB_PRUNEPATH_${idx}_ACTIVE=yes
        fi

        printvar 'UPDATEDB_PRUNEPATH_'${idx}'_ACTIVE'
        printcomment        'Use: yes or no'

        printvar 'UPDATEDB_PRUNEPATH_'${idx}

        idx=$((${idx} + 1))
    done

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

    } > ${internal_conf_file}

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

# -------------------------------------------------------------------------
# Create the check.d file
# -------------------------------------------------------------------------
make_check_file()
{
printgpl --check "${package_name}" '2007-05-14' 'schlotze'  > /etc/check.d/${package_name}
cat >> /etc/check.d/${package_name} << EOF
# Variable                     OPT_VARIABLE  VARIABLE_N            VALUE
UPDATEDB_CRON_USE              -             -                     YESNO
UPDATEDB_CRON_TIMES            -             -                     CRONTAB
UPDATEDB_CRON_RUN_AS           -             -                     NONE
UPDATEDB_CRON_NICE             -             -                     NUMERIC
UPDATEDB_CRON_IONICE_CLASS     -             -                     NUMERIC
UPDATEDB_CRON_IONICE_PRIORITY  -             -                     NUMERIC

UPDATEDB_SUPPRESS_CRON_MAIL    -             -                     YESNO
UPDATEDB_PRUNEPATH_N           -             -                     NUMERIC
UPDATEDB_PRUNEPATH_%_ACTIVE    -             UPDATEDB_PRUNEPATH_N  YESNO
UPDATEDB_PRUNEPATH_%           -             UPDATEDB_PRUNEPATH_N  ABS_PATH
EOF

    # Set rights for check.d file
   chmod 0600 /etc/check.d/${package_name}
   chown root /etc/check.d/${package_name}
}

# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
main()
{
    get_defaults

    # write default config file
    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
#----------------------------------------------------------------------------
