# Copyright 2019 Cumulus Networks, Inc.  All Rights Reserved.
#
# This file contains common variables and functions
# for the config-backup and config-restore utilities

# This group is the same in all scripts
declare -r configfile=/etc/cumulus/config_utility
declare -r rundir=/run/configutil # same in all scripts
declare -r lockfile=$rundir/flock
declare -r TMPDIR=$rundir/$prefix
declare -r control_base=$rundir/Backup_info
declare -r pkglist=$TMPDIR/Pkglist
declare -r cfgbackup=config_backup
declare -r -i config_version=1

# here because the functions using them are here
declare -i debug_en=0 quiet_en=0 verbose_en=0

# here because config file can override
declare archivedir=/var/lib/config-backup/backups
declare -i minmbfree=100 maxbackups=30 maxmbbackups=25

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/cumulus/bin

# Read the config file, and sanitize it.  Don't source it
# directly so we don't crash on config file errors.
read_config()
{
        local value
        sed -e 's/^[ \t]//g' -e 's/#.*//' -e '/=/!d' $configfile > $rundir/.config

        value=$(sed -n 's/^ARCHIVE=//p' $rundir/.config)
        [ -n "$value" ] && {
        case "$value" in
        /*/*) archivedir="$value"
            verbose Config file changed Archive directory to "$archivedir"
            ;;
        *)  # has to be at least one level below /
            warn ARCHIVE="$value" is not a valid path for the archive directory ;;
        esac
        }

        value=$(sed -n 's/^MAX_ARCHIVES=//p' $rundir/.config)
        [ -n "$value" ] && {
            [ "$value" -ge 0 ] 2>/dev/null ||
            warn MAX_ARCHIVES="$value" is not a valid number
            [ "$value" -ge 0 ] 2>/dev/null && {
                maxbackups="$value"
                verbose Config file changed maximum archives to $maxbackups
            }
        }

        value=$(sed -n 's/^MAX_ARCHIVES_SPACE=//p' $rundir/.config)
        [ -n "$value" ] && {
            [ "$value" -ge 0 ] 2>/dev/null ||
            warn MAX_ARCHIVES_SPACE="$value" is not a valid number
            [ "$value" -ge 0 ] 2>/dev/null && {
                maxmbbackups="$value"
                verbose Config file changed maximum archive space to $maxmbbackups
            }
        }

        value=$(sed -n 's/^MIN_FREE_FILESYSTEM=//p' $rundir/.config)
        [ -n "$value" ] && {
        [ "$value" -ge 0 ] 2>/dev/null ||
        warn MIN_FREE_FILESYSTEM="$value" is not a valid number
        [ "$value" -ge 0 ] 2>/dev/null && {
        minmbfree="$value"
        verbose Config file changed minimum free space to $minmbfree
        }
        }

        rm -f $rundir/.config
}

# same in all scripts; may put in "library" script that is sourced
runlock()
{
        exec 18> $lockfile
        flock -n 18 || return 1
        return 0
}

debug()
{
    [ $debug_en -ne 0 ] && echo -e "$pname DEBUG: $*" 1>&2
}

verbose()
{
    [ $verbose_en -ne 0 ] && echo -e "$pname: $*" 1>&2
}

warn()
{
    echo -e "${pname}: $*" 1>&2
}

error()
{
    warn "$@"
    exit 1
}

notify()
{
    declare arg
    [ "$1" = "-n" ] && { arg=$1 ; shift ; }
    echo $arg -e "$pname: $@"
}


checkperms()
{
    declare uid
    read uid rest <<< $(id)
    uid=${uid#uid=}
    uid=${uid%(*}
    [ "$uid" -ne 0 ] &&
        error "must be run as root or with sudo"
}

# Get the list of config files outside the /etc directory, if any.
get_configfiles()
{
    declare cfiles
    cfiles="$(dpkg-query -W -f '${Conffiles}\n' |
        sed -e '/^ \/etc\//d' -e '/^$/d' -e 's/^ //' -e 's/ .*//')"
    debug Initial config files not in /etc: "$cfiles"
    for f in $cfiles; do
        if [ -e "$f" ]; then
            nonetc_cfiles="$f $nonetc_cfiles"
        else
            debug non-etc config file "$f" not present
        fi
    done
    debug config files not in /etc: "$nonetc_cfiles"

    return 0
}

# get the list of installed packages in a standard formats,
# save in the file $pkglist
get_installed_pkgs()
{
    # sorted list of installed packages with versions
    dpkg-query -W -f '${db:Status-Abbrev} ${Package}  ${Version}\n' | \
       sed -n 's/^i[^ ]* *//p' | sort -u -o $pkglist
}
