#!/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.


# (C) 2016 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=15

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
    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"
		;;
	    *)	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

    dbtarget=$((dbtarget + 1))
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
