#!/bin/sh

# This script creates the files needed by dbc for database creation
# and updates.

# This script needs to be augmented whenever there is a new database update,
# in particular "lastdbversion" and the case statements need to be reviewed.

# Due to upstream changes in Bacula 11, upgrade extractions for 7.2.0
# and later are now handled by hand with the help of
# extract-dbupgrade-v13 until someone can find a solution to automate
# them again. Relevant code is commented with #!#
# - leo@debian.org, 2022-03-24

# (C) 2016-2024 Carsten Leonhardt <leo@debian.org>

# License: expat

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:

#     The above copyright notice and this permission notice shall be
#     included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# first and last target database version covered by this script
dbtarget=11
lastdbversion=1024

variants="mysql pgsql sqlite3"

dbc="usr/share/dbconfig-common/data"
extractor="debian/scripts/extract-dbupgrade.awk"

set_longdb() {
    if [ $db = pgsql ] ; then
	longdb=postgresql
    else
	longdb=$db
    fi
}


# handle older db upgrades
while [ $dbtarget -le 12 ]; do
    for db in $variants; do
	set_longdb

	case $dbtarget in
	    11)	baculaversion="3.0.0"
		sourcefile="updatedb/update_"$longdb"_tables_10_to_11"
		;;
	    12)	baculaversion="5.0.0"
		sourcefile="updatedb/update_"$longdb"_tables_11_to_12"
		;;
	esac

	targetfile="debian/bacula-director-"$db"/"$dbc"/bacula-director-"$db"/upgrade/"$db"/"$baculaversion;

	mkdir -p `dirname $targetfile`
	awk -v version=ignore -f $extractor $sourcefile >> $targetfile
    done

    dbtarget=$((dbtarget + 1))
done

# handle newer db upgrades
#!#while [ $dbtarget -le $lastdbversion ]; do # further updates are not covered by this script at the moment
while [ $dbtarget -le $14 ]; do
    for db in $variants; do
	set_longdb

	case $dbtarget in
	    13)	baculaversion="5.2.0"
		;;
	    14)	baculaversion="5.2.0"
		;;
	    15)	baculaversion="7.2.0"
		;;
	    16)	baculaversion="9.0.0"
		;;
	    1017) baculaversion="11.0.0~1017"
		;;
	    1018 | 1019 | 1020 | 1021 | 1022) baculaversion="11.0.0"
					      ;;
	    1023 | 1024) baculaversion="13.0.0"
			 ;;
	    *)	echo "$0: Unknown database target version"
		exit 1
		;;
	esac

	sourcefile="src/cats/update_"$longdb"_tables"
	targetfile="debian/bacula-director-"$db"/"$dbc"/bacula-director-"$db"/upgrade/"$db"/"$baculaversion;

	mkdir -p `dirname $targetfile`
	awk -v version=$dbtarget -f $extractor $sourcefile >> $targetfile
    done

    if [ $dbtarget = 16 ]
    then
	# special case, a jump from 16 to 1017
	dbtarget=1017
    else
	dbtarget=$((dbtarget + 1))
    fi
done

# handle db creation
for db in $variants; do
    set_longdb

    sourcefile="src/cats/make_"$longdb"_tables"
    targetfile="debian/bacula-director-"$db"/"$dbc"/bacula-director-"$db"/install/"$db;

    mkdir -p `dirname $targetfile`
    awk -v version=ignore -f $extractor $sourcefile >> $targetfile
done

# check if this script needs to be updated
# we check for all db types just to be on the safe side
for db in $variants; do
    set_longdb

    sourcefile="src/cats/make_"$longdb"_tables"
    # this looks where the current database version is inserted in the make_(db)_tables script and extracts it
    current_bacula_db_version=`sed -n 's/INSERT INTO Version (VersionId) VALUES (\([0-9]*\));/\1/ip' $sourcefile`
    if [  $current_bacula_db_version -ne $lastdbversion ]; then
	echo "error: unsupported database version $current_bacula_db_version, please update $0!"
	exit 1
    fi
done
