#!/bin/bash

set -ue
PATH="/usr/bin:/bin"
export PATH

echo Debug: running pre_install_bacula-workarounds

## part one

# work around issue in piuparts in the salsa pipeline, see
# https://salsa.debian.org/salsa-ci-team/pipeline/-/issues/357
debconf-set-selections <<EOF
bacula-director-mysql    bacula-director-mysql/dbconfig-remove    boolean true
bacula-director-pgsql    bacula-director-pgsql/dbconfig-remove    boolean true
bacula-director-sqlite3  bacula-director-sqlite3/dbconfig-remove  boolean true
bacula-director-mysql    bacula-director-mysql/purge    boolean true
bacula-director-pgsql    bacula-director-pgsql/purge    boolean true
bacula-director-sqlite3  bacula-director-sqlite3/purge  boolean true
EOF
echo Debug: debconf preseeded for bacula database purge on removal

## part two

# copied and adapted from the roundcube package, thanks to Guilhem Moulin

# this emulates
#
#     /etc/piuparts/scripts/pre_install_database-server
#
# as a workaround for https://bugs.debian.org/1015732
declare -a PACKAGES=()
for arg in $PIUPARTS_OBJECTS; do
    arg="${arg##*/}"
    case "${arg%%[=_]*}" in
        bacula-director-mysql) PACKAGES+=( "default-mysql-server" );;
        bacula-director-pgsql) PACKAGES+=( "postgresql" );;
    esac
done

[ ${#PACKAGES[@]} -gt 0 ] || exit 0
export DEBIAN_FRONTEND="noninteractive"
exec apt-get install --no-install-recommends -y -- "${PACKAGES[@]}"
# end of script, the exec above hands over control to apt-get
