#!/bin/bash
# Script for compile, test from uimacpp source code
# Author: Lic. Roberto Carlos Toledano Gómez
# 
# Thu, 25 Jun 2015 17:51:00 -0400


_me="${BASH_SOURCE[0]}"
script="$_me"
while [ -h "$script" ] ; do
 lst=$(ls -ld "$script")
 lnk=$(expr "$lst" : '.*-> \(.*\)$')
 if expr "$lnk" : '/.*' > /dev/null; then
   script="$lnk"
 else
   script=$(dirname "$script")/"$lnk"
 fi
done
_BIN=$(dirname "$script")

scriptname=`echo "$0"|awk -F / '{print $NF}'`;


actual_dir=`pwd`;

exit_iferror=0;

function myhelp()
{
    echo "Script for compile and install";
    echo "Usage $0 -w <uimacpp source directory>  [-p <installation prefix:/usr>]" >&2 ;
    echo "	Ex:" >&2 ;
    echo "	" >&2 ;
    echo "		$scriptname -w /home/uimacpp-2.4.1/ -p /opt/"  >&2 ;
}

TEMP=`getopt -o w:p:e -n '$0' -- "$@"`

if [ $? != 0 ] ; then 
    myhelp;
    exit 1 ; 
fi;

packprefix="/usr";

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP";
while true ; do
    case "$1" in
	-w) project_dir="${2}" ; shift 2 ;;
	-p) packprefix="${2}" ; shift 2 ;;
	-e) exit_iferror=1 ; shift 1 ;;
	-h) myhelp; exit 0;;
	--) shift; break ;;
        *) echo "Illegal arguments!" ; exit 1 ;;
    esac;
done;



if [ ! -d "${project_dir}" ];then
 echo "CI: Error: ‘${project_dir}‘: No such directory";
 exit 1;
fi;

actualdir="$PWD";

#make room for binaries.
mkdir -p "$packprefix";

cd $project_dir;

chmod  +x configure *.sh;

jdk_include=`dpkg -S jni.h|grep jdk|head -n 1|awk '{print $2}'|awk -F "jni.h" '{print $1}'`;

#figure out version from configure.ac
version=`grep "AC_INIT" $project_dir/configure.ac|tail -n 1|grep -o "\([0-9]*\.\)\{2\}[0-9]*"`;
VERSION_MAJOR=`grep "AC_INIT" configure.ac|tail -n 1|grep -o "\([0-9]*\.\)\{2\}[0-9]*"|sed "s/\(.*\)\..*\..*/\1/"`;
VERSION_MINOR=`grep "AC_INIT" configure.ac|tail -n 1|grep -o "\([0-9]*\.\)\{2\}[0-9]*"|sed "s/.*\.\(.*\)\..*/\1/"`;
VERSION_PATCH=`grep "AC_INIT" configure.ac|tail -n 1|grep -o "\([0-9]*\.\)\{2\}[0-9]*"|sed "s/.*\..*\.\(.*\)/\1/"`;

activemq_prefix=`activemqcpp-config  --prefix`;
icu_prefix=`icu-config --prefix`;
apr_prefix=`apr-config --includedir| awk -F "/include" '{print $1}'`;

apr_regex_arg=`apr-config --version|sed -e 's/\./\\\\\\\./g'`;

#actualizando archivo de configuracion
sed -e "s/\(^APR_VER_REGEXES=\)\(.*\)/\1\[\"$apr_regex_arg\"\]/" "${project_dir}/configure.ac" > /tmp/~configure.ac.tmp;
if [ $? -eq 0 ] ; then 
    echo "Info: Updating config file ... ${project_dir}/configure.ac";
    cp /tmp/~configure.ac.tmp ${project_dir}/configure.ac
    rm /tmp/~configure.ac.tmp;    
else
    echo "Error: Updating configure.ac"; exit 1;
fi;

./autogen.sh

echo "./configure --enable-static=yes --enable-shared=yes --prefix=\"$packprefix\" --with-activemq=$activemq_prefix --with-icu=$icu_prefix --with-apr=$apr_prefix --with-jdk=$jdk_include"
./configure --enable-static=yes --enable-shared=yes --prefix="$packprefix" --with-activemq=$activemq_prefix --with-icu=$icu_prefix --with-apr=$apr_prefix --with-jdk=$jdk_include CXXFLAGS="-Wall" && make && make install;

if [ $? -ne 0 ];then
	echo "Error: building uimacpp";
	exit 1;
fi;


