#!/bin/sh
# shellcheck disable=SC2034
# We expand enable_* variables via eval
set -e
set -u
pkgconf="${PKG_CONFIG-pkgconf}"

# SC2016: the variable expansion is handled by Make

prefix='/usr'
# shellcheck disable=SC2016
bindir='$(prefix)/bin'
# shellcheck disable=SC2016
datadir='$(prefix)/share'
# shellcheck disable=SC2016
libdir='$(prefix)/lib'
# shellcheck disable=SC2016
libexecdir='$(prefix)/libexec'
statedir='/var/spool/cron'
# shellcheck disable=SC2016
mandir='$(datadir)/man'
# shellcheck disable=SC2016
docdir='$(datadir)/doc/systemd-cron'
# shellcheck disable=SC2016
unitdir="$($pkgconf systemd --variable=systemdsystemunitdir)" 2>/dev/null || \
unitdir='$(libdir)/systemd/system'
# shellcheck disable=SC2016
generatordir="$($pkgconf systemd --variable=systemdsystemgeneratordir)" 2>/dev/null || \
generatordir='$(libdir)/systemd/system-generators'
# shellcheck disable=SC2016
sysusersdir="$($pkgconf systemd --variable=sysusersdir)" 2>/dev/null || \
sysusersdir='$(libdir)/sysusers.d'
# shellcheck disable=SC2016
tmpfilesdir="$($pkgconf systemd --variable=tmpfilesdir)" 2>/dev/null || \
tmpfilesdir='$(libdir)/tmpfiles.d'
libmd="$($pkgconf --cflags --libs libmd)" 2>/dev/null || \
libmd='-lmd'
libsystemd="$($pkgconf --cflags --libs libsystemd)" 2>/dev/null || \
libsystemd='-lsystemd'
enable_runparts=no
use_loglevelmax=no
part2timer=/dev/null
crond2timer=/dev/null
pamname=
read -r version < VERSION


enable_boot=yes
enable_minutely=no
enable_hourly=yes
enable_daily=yes
enable_weekly=yes
enable_monthly=yes
enable_quarterly=no
enable_semi_annually=no
enable_yearly=yes

# TODO: roll back to straight OnSuccess= if we ever bump past the systemd ≥ 236 requirement (this is 249); cf. #165
have_onsuccess="$($pkgconf systemd --max-version=249 && echo no)" 2>/dev/null || \
have_onsuccess=yes


orig_args="$*"
ARGS=$(getopt -n "$(basename "${0}")" -o '' -l '
help::,
prefix:,
bindir:,
confdir:,
datadir:,
libdir:,
libexecdir:,
statedir:,
mandir:,
docdir:,
unitdir:,
generatordir:,
sysusersdir:,
tmpfilesdir:,
enable-boot::,
enable-minutely::,
enable-hourly::,
enable-daily::,
enable-weekly::,
enable-monthly::,
enable-quarterly::,
enable-semi_annually::,
enable-yearly::,
enable-runparts::,
use-loglevelmax::,
with-part2timer:,
with-crond2timer:,
with-pamname:,
with-version:,
' -- "${@}")

usage()
{
    grep ^'### Configuration' README.md -A 50
}

# shellcheck disable=SC2181
if [ $? -ne 0 ]
then
    usage
    exit 1
fi

set_enable_flag()
{
    if [ -z "${2}" ]
    then
        eval "enable_${1}=yes"
    elif [ "${2}" = 'yes' ] || [ "${2}" = 'no' ]; then
        eval "enable_${1}=${2}"
    else
        echo "ERROR: Unknown value for enable-${1}: '${2}'. Expected 'yes' or 'no'."
    fi
}

eval set -- "${ARGS}"
while true;
do
    case "${1}" in
        '--help')
            usage
            exit 0;;

        '--prefix')
            prefix="${2}"
            shift 2;;

        '--bindir')
            bindir="${2}"
            shift 2;;

        '--confdir')
            echo "--confdir ${2} is ignored"
            shift 2;;

        '--datadir')
            datadir="${2}"
            shift 2;;

        '--libdir')
            libdir="${2}"
            shift 2;;

        '--libexecdir')
            libexecdir="${2}"
            shift 2;;

        '--statedir')
            statedir="${2}"
            shift 2;;

        '--mandir')
            mandir="${2}"
            shift 2;;

        '--docdir')
            docdir="${2}"
            shift 2;;

        '--unitdir')
            unitdir="${2}"
            shift 2;;

        '--generatordir')
            generatordir="${2}"
            shift 2;;

        '--sysusersdir')
            sysusersdir="${2}"
            shift 2;;

        '--tmpfilesdir')
            tmpfilesdir="${2}"
            shift 2;;

        '--libmd')
            libmd="${2}"
            shift 2;;

        '--enable-boot')
            set_enable_flag boot "${2}"
            shift 2;;

        '--enable-minutely')
            set_enable_flag minutely "${2}"
            shift 2;;

        '--enable-hourly')
            set_enable_flag hourly "${2}"
            shift 2;;

        '--enable-daily')
            set_enable_flag daily "${2}"
            shift 2;;

        '--enable-weekly')
            set_enable_flag weekly "${2}"
            shift 2;;

        '--enable-monthly')
            set_enable_flag monthly "${2}"
            shift 2;;

        '--enable-quarterly')
            set_enable_flag quarterly "${2}"
            shift 2;;

        '--enable-semi_annually')
            set_enable_flag semi_annually "${2}"
            shift 2;;

        '--enable-yearly')
            set_enable_flag yearly "${2}"
            shift 2;;

        '--enable-runparts')
            set_enable_flag runparts "${2}"
            shift 2;;

        '--use-loglevelmax')
            case "${2}" in
                'alert'|'crit'|'err'|'warning'|'notice'|'info'|'debug')
                    use_loglevelmax="${2}";;
                *)
                    echo "ERROR: Unknown value for use-loglevelmax: '${2}'. Expected 'alert', 'crit', 'err', 'warning', 'notice', 'info', or 'debug'.";;
            esac
            shift 2;;

        '--with-part2timer')
            part2timer="${2}"
            shift 2;;

        '--with-crond2timer')
            crond2timer="${2}"
            shift 2;;

        '--with-pamname')
            pamname="${2}"
            shift 2;;

        '--with-version')
            version="${2}"
            shift 2;;

        '--')
            shift
            break;;
    esac
done

check_sched_enabled()
{
    if eval [ "\"\${enable_${1}}\"" = \'yes\' ]; then
        schedules="${schedules} ${2}"
    else
        schedules_not="${schedules_not} ${2}"
    fi
}

schedules=""
schedules_not=""
check_sched_enabled boot          boot
check_sched_enabled minutely      minutely
check_sched_enabled hourly        hourly
check_sched_enabled daily         daily
check_sched_enabled weekly        weekly
check_sched_enabled monthly       monthly
check_sched_enabled quarterly     quarterly
check_sched_enabled semi_annually semi-annually
check_sched_enabled yearly        yearly

exec 2>&1
set -x
sed "
1i\
# Generated by $0 ${orig_args}
s|@schedules@|${schedules}|g
s|@schedules_not@|${schedules_not}|g
s|@enable_runparts@|${enable_runparts}|g
s|@prefix@|${prefix}|g
s|@bindir@|${bindir}|g
s|@datadir@|${datadir}|g
s|@libdir@|${libdir}|g
s|@libexecdir@|${libexecdir}|g
s|@statedir@|${statedir}|g
s|@mandir@|${mandir}|g
s|@docdir@|${docdir}|g
s|@unitdir@|${unitdir}|g
s|@generatordir@|${generatordir}|g
s|@sysusersdir@|${sysusersdir}|g
s|@tmpfilesdir@|${tmpfilesdir}|g
s|@libmd@|${libmd}|g
s|@libsystemd@|${libsystemd}|g
s|@use_loglevelmax@|${use_loglevelmax}|g
s|@part2timer@|${part2timer}|g
s|@crond2timer@|${crond2timer}|g
s|@pamname@|${pamname}|g
s|@version@|${version}|g
s|@have_onsuccess@|${have_onsuccess}|g
" Makefile.in > Makefile
