#!/bin/bash

versionCompLte() {
    [  "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}


versionCompLt() {
    [ "$1" = "$2" ] && return 1 || versionCompLte $1 $2
}

# file with bash code generating DB schema diffs for different versions
dbschemadiff=management_scripts/db_schema_and_diff.sh

#Where source file of the debian package should be placed
SRC_DIR=debian/src

#where the documentation files are stored
DOC_DIR=doc

#File that should be deployed on tomcat
TOMCAT_FILE=web/target/web-1.0.war

#Directory with database schema
DB_SCHEMA_DIR=persist/src/main/resources/db/migration

ROOT_DIR=`pwd`


# where dn script files should be stored on the debian system
DB_SCRIPT_DIR="\/usr\/share\/minerva\/schema"
LOG_FILE="\/var\/log\/minerva-install.log"

#clean build directories
rm -rf $SRC_DIR
rm -rf debian/minerva*

#recreate directory
mkdir $SRC_DIR

#copy  war file
cp $TOMCAT_FILE $SRC_DIR/minerva.war

#copy manual documentation
cp $DOC_DIR/description.txt $SRC_DIR/minerva.txt
gzip -n $SRC_DIR/minerva.txt

#copy configuration of logrotate
cp debian/logrotate/* $SRC_DIR/

cp debian/scripts/common.sh $SRC_DIR/

#copy changelog file
cp CHANGELOG $ROOT_DIR/debian/template/changelog

# set $versions and $current_version; generate upgrade scripts in SRC_DIR
DBSCRIPT_DEST_DIR=$SRC_DIR
MAX_DB_VERSION_FOR_MIGRTION=12.1.0~alpha
source $dbschemadiff

current_version=`cat CHANGELOG |grep minerva |head -1 | cut -f2 -d'(' | cut -f1 -d')'`

echo "Current version: " $current_version
echo "Flyway was introduced in version: " $MAX_DB_VERSION_FOR_MIGRTION

#default connection params (if /etc/minerva/db.properties is not defined)
DB_HOST=`cat persist/src/main/resources/db.properties |grep "uri" |cut -f3 -d"/" |cut -f1 -d":"`
DB_PORT=`cat persist/src/main/resources/db.properties |grep "uri" |cut -f3 -d"/" |cut -f2 -d":"`
DB_DATABASE_NAME=`cat persist/src/main/resources/db.properties |grep "uri" |cut -f4 -d"/"`
DB_USERNAME=`cat persist/src/main/resources/db.properties |grep "username" |cut -f2 -d"="`
DB_PASSWORD=`cat persist/src/main/resources/db.properties |grep "password" |cut -f2 -d"="`

#------------------------------------------
# Now we have all db schema diff files. Let's start preparing 
# debian package
#------------------------------------------

#dh_make requires directory name to be like <package>-<version>
# this points to the directory
DEBIAN_DIR=debian/minerva-$current_version/

mkdir $DEBIAN_DIR

# copy all source files to this directory
cp $SRC_DIR/* $DEBIAN_DIR

#because dh_make works only from within directory where
#package data will be generated we need to change it
cd $DEBIAN_DIR
#this should be improved (to include src properly)
dh_make -s --createorig -e piotr.gawron@uni.lu -y -t $ROOT_DIR/debian/template 

echo "1.0" > debian/source/format

#remove example files generated by dh_make
find -name '*.ex' ! -name 'minerva*' -type f -exec rm -f {} +

#our pre/post inst/rm scripts use __CURRENT_VERSION__ as a version of currently
#installed package

sed -i "s/__CURRENT_VERSION__/$current_version/g" common.sh
sed -i "s/__LOG_FILE__/$LOG_FILE/g" common.sh
#__DB_SCRIPT_DIR__ in our scripts points to place where db schema files are placed
#in the filesystem where package is installed
sed -i "s/__DB_SCRIPT_DIR__/$DB_SCRIPT_DIR/g" common.sh

sed -i "s/__DB_HOST__/$DB_HOST/g" common.sh
sed -i "s/__DB_PORT__/$DB_PORT/g" common.sh
sed -i "s/__DB_DATABASE_NAME__/$DB_DATABASE_NAME/g" common.sh
sed -i "s/__DB_USERNAME__/$DB_USERNAME/g" common.sh
sed -i "s/__DB_PASSWORD__/$DB_PASSWORD/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/postinst
sed -i -e "1r common.sh" debian/postrm
sed -i -e "1r common.sh" debian/preinst
sed -i -e "1r common.sh" debian/prerm




#put scripts into $DB_SCRIPT_DIR (it's a bit different than varaiable because it's not
#escaped
echo db_0_to_$MAX_DB_VERSION_FOR_MIGRTION".sql" /usr/share/minerva/schema >> debian/install
for version in ${versions[*]}
do
	echo db_"$version"_to_$MAX_DB_VERSION_FOR_MIGRTION".sql" /usr/share/minerva/schema >> debian/install;
done

#set proper architecture
arch=`dpkg --print-architecture`
echo "Architecture: $arch" >>debian/control

#set manual file
echo minerva.txt.gz /usr/share/doc/minerva/minerva.txt.gz >> debian/install;

#logrotate configuration for minerva log4j connector
echo minerva /etc/logrotate.d >> debian/install;

#remove some sample file
rm debian/*.EX
rm debian/*.ex

#build debian package
debuild -us -uc

# remove temporary changelog
rm $ROOT_DIR/debian/template/changelog