#!/bin/bash

set -u #exit the script if an uninitialised variable is used
set -e #exit the script if any statement returns a non-true return value
set -o pipefail #exit if there's an error before the pipe

exitprocedure()
{
 echo "User break!"
 exit 1
}

trap "exitprocedure" 2 3 15

# Set locale to C so that the log files are in English:
LC_ALL=C
export LC_ALL

# Source the script with variables that define paths and arch:
source build_vars.sh

sudo mkdir -p $loc_gdal_plugins

src_gdal=gdal-stable
src_grass=grass64

cd ${dir_bld}/${src_gdal}/frmts/grass/pkg

ln -sf ${dir_bld}/${src_gdal}/frmts/grass/*.cpp .
ln -sf ${dir_bld}/${src_gdal}/ogr/ogrsf_frmts/grass/*.cpp .
ln -sf ${dir_bld}/${src_gdal}/ogr/ogrsf_frmts/grass/*.h .

./configure --prefix=/usr/local --with-grass=$loc_grass --with-autoload=$loc_gdal_plugins --with-ld-shared="g++ -shared"

make 2>&1 | tee log.make

rev_grass=`grep "Checked out revision [[:digit:]]\{1,\}\." ${dir_bld}/${src_grass}/log.svn | grep -o "[[:digit:]]\{1,\}"`
rev_gdal=`grep "Checked out revision [[:digit:]]\{1,\}\." ${dir_bld}/${src_gdal}/log.svn | grep -o "[[:digit:]]\{1,\}"`
ver=`date +%F`"-${rev_gdal}-${rev_grass}"

sudo checkinstall -D -y --pkgname=gdal-grass --pkgversion=$ver --strip=no --stripso=no --pakdir=$dir_deb --deldoc=yes --deldesc=yes --delspec=yes --backup=no --fstrans=no --install=yes 2>&1 | tee log.checkinstall
sudo ldconfig

