From 904dc7ed095f3402a2d1e8e771d53245a42ac880 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Mon, 20 May 2019 18:06:29 +0200
Subject: [PATCH] upgrade from version prior to 14 working (version without
 dbcommons-config)

---
 debian/create-debian-pkg.sh |  1 +
 debian/template/config      | 30 ++++++++++++++++++++++++++++++
 debian/template/postinst    |  6 +++++-
 3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/debian/create-debian-pkg.sh b/debian/create-debian-pkg.sh
index 8888e10f73..b77a52a7b6 100755
--- a/debian/create-debian-pkg.sh
+++ b/debian/create-debian-pkg.sh
@@ -98,6 +98,7 @@ sed -i "s/__DB_SCRIPT_DIR__/$DB_SCRIPT_DIR/g" common.sh
 
 sed -i "s/__MAX_DB_VERSION_FOR_MIGRTION__/$MAX_DB_VERSION_FOR_MIGRTION/g" common.sh
 
+sed -i -e "1r common.sh" debian/config
 sed -i -e "1r common.sh" debian/postinst
 sed -i -e "1r common.sh" debian/postrm
 sed -i -e "1r common.sh" debian/preinst
diff --git a/debian/template/config b/debian/template/config
index 1288fd1ae1..b8549e0e4b 100644
--- a/debian/template/config
+++ b/debian/template/config
@@ -1,12 +1,42 @@
 #!/bin/sh
 
+log "Running config" $1 $2;
+
 set -e
 
 . /usr/share/debconf/confmodule
 
 db_version 2.0
 
+if [ ! -d "/etc/minerva/" ]
+then
+  mkdir /etc/minerva/
+fi
+
 if [ -f /usr/share/dbconfig-common/dpkg/config.pgsql ]; then
         . /usr/share/dbconfig-common/dpkg/config.pgsql
+        DB_HOST="localhost"
+        DB_PORT="5432"
+        DB_DATABASE_NAME="map_viewer"
+        DB_USERNAME="map_viewer"
+        DB_PASSWORD="123qweasdzxc"
+
+        if [ -f /etc/minerva/db.properties ]; then
+          DB_PROPERTIES_FILE="/etc/minerva/db.properties"
+          DB_HOST=`cat $DB_PROPERTIES_FILE |grep "uri" |cut -f3 -d"/" |cut -f1 -d":"`
+          DB_PORT=`cat $DB_PROPERTIES_FILE |grep "uri" |cut -f3 -d"/" |cut -f2 -d":"`
+          DB_DATABASE_NAME=`cat $DB_PROPERTIES_FILE |grep "uri" |cut -f4 -d"/"`
+          DB_USERNAME=`cat $DB_PROPERTIES_FILE |grep "username" |cut -f2 -d"="`
+          DB_PASSWORD=`cat $DB_PROPERTIES_FILE |grep "password" |cut -f2 -d"="`
+        fi
+        echo "dbserver=$DB_HOST" > /etc/minerva/old.sh
+        echo "dbport=$DB_PORT" >> /etc/minerva/old.sh
+        echo "dbname=$DB_DATABASE_NAME" >> /etc/minerva/old.sh
+        echo "dbuser=$DB_USERNAME" >> /etc/minerva/old.sh
+        echo "dbpass=$DB_PASSWORD" >> /etc/minerva/old.sh
+				
+        dbc_first_version="14.0.0~alpha.0"
+        dbc_load_include="sh:/etc/minerva/old.sh"
+
         dbc_go minerva $@
 fi
diff --git a/debian/template/postinst b/debian/template/postinst
index 3069e7597b..8544e91a78 100644
--- a/debian/template/postinst
+++ b/debian/template/postinst
@@ -39,6 +39,8 @@ case "$1" in
     dbc_generate_include_owner="root:root"
     dbc_generate_include_perms="0640"
     dbc_generate_include=sh:/etc/minerva/db.sh
+    dbc_first_version="14.0.0~alpha.0"
+    dbc_load_include="sh:/etc/minerva/old.sh"
 
     # Need to start postgresql, so it doesn't fail on the installer
     invoke-rc.d --force postgresql restart || true
@@ -46,10 +48,11 @@ case "$1" in
 
     # if there is a config file generated by db_config_common (on upgrade this file might not be generated)
   	if [ -f /etc/minerva/db.sh ]
+  	then
       . /etc/minerva/db.sh
     
       # when configuration was cancelled don't create config file
-  	  if [ -z "$dbuser" ]
+  	  if [ ! -z "$dbuser" ]
   	  then
         if [ -z "$dbport" ]
         then
@@ -64,6 +67,7 @@ case "$1" in
       fi
       rm /etc/minerva/db.sh
     fi
+    rm /etc/minerva/old.sh
 
     #we have to restart tomcat, because application was started on the old database and it might crash during deployment
     #for instance in the new war file there was init script that uses some new database features, but they are not
-- 
GitLab