#!/bin/sh

#   $Revision: 1.20 $
#   $Author: faust $
#   $Date: 2010/04/14 08:58:43 $
######################################################

OS=unknown
sys=`uname -s`
release=`uname -r`
major=`printf "%s" "$release" | cut -d. -f1`
BUILD_DIR=`pwd`
CONFFILE="../../Makefile.conf"
PREFIX="/"
BIN_MODE=0755
DATA_MODE=0644
DIR_MODE=0755
OWNER=root
VAR_DIR="./inst/var/stargazer"

if [ "$1" = "debug" ]
then
   DEFS="$DEFS -DDEBUG"
   MAKEOPTS="$MAKEOPTS -j1"
   CFLAGS="$CFLAGS -ggdb3 -W -Wall -Wextra"
   CXXFLAGS="$CXXFLAGS -ggdb3 -W -Wall -Wextra"
   DEBUG="yes"
else
   DEFS="$DEFS -DNDEBUG"
   DEBUG="no"
fi

CFLAGS="$CFLAGS -ggdb3 -W -Wall -I/usr/local/include"
CXXFLAGS="$CXXFLAGS -ggdb3 -W -Wall -I/usr/local/include"
LDFLAGS="$LDFLAGS -L/usr/local/lib"

if [ "$sys" = "Linux" ]
then
    OS=linux
    MAKE="make"
fi

if [ "$sys" = "FreeBSD" ]
then
    case $major in
        4) OS=bsd;;
        5) OS=bsd5;;
        6) OS=bsd5;;
        7) OS=bsd7;;
        *) OS=bsd7;;
    esac
    MAKE="gmake"
fi

if [ "$sys" = "Darwin" ]
then
    OS=darwin
    ETC_DIR="./inst/freebsd/etc/stargazer"
    MAKE="gmake"
fi

if [ "$OS" = "unknown" ]
then
    printf "#############################################################################\n"
    printf "# Sorry, but sgconv is currently supported by Linux, FreeBSD and Darwin.    #\n"
    printf "#############################################################################\n"
    exit 1
fi

printf "#############################################################################\n"
printf "       Building sgconv for $sys $release\n"
printf "#############################################################################\n"

STG_LIBS="logger.lib
          crypto.lib
          common.lib
          conffiles.lib
          dotconfpp.lib"

PLUGINS="store/files"

if [ "$OS" = "linux" ]
then
    DEFS="$DEFS -DLINUX"
    LIB_THREAD=-lpthread
else
    LIBICONV="-liconv"
    if [ "$OS" = "bsd" ]
    then
        DEFS="$DEFS -DFREE_BSD"
        LIB_THREAD=-lc_r
    else
        if [ "$OS" = "bsd7" ]
        then
            DEFS="$DEFS -DFREE_BSD5"
            LIB_THREAD=-lpthread
        else
            if [ "$OS" == "darwin" ]
            then
                DEFS="$DEFS -DDARWIN"
                LIB_THREAD=-lpthread
                LIBICONV=""
            else
                LIB_THREAD=-lc_r
            fi
        fi
    fi
fi

if [ -z "$CC" ]
then
    CC=gcc
fi

if [ -z "$CXX" ]
then
    CXX=g++
fi

printf "Checking CC... "
$CC --version > /dev/null 2> /dev/null
if [ $? != 0 ]
then
    printf "FAIL!\n"
    printf "$CC not found\n"
    exit;
fi
printf "found\n"
printf "Checking CXX... "
$CXX --version > /dev/null 2> /dev/null
if [ $? != 0 ]
then
    printf "FAIL!\n"
    printf "$CXX not found\n"
    exit;
fi
printf "found\n"

printf "Checking endianess... "
printf "int main() { int probe = 0x00000001; return *(char *)&probe; }\n" > build_check.c
$CC $CFLAGS $LDFLAGS -L/usr/lib/mysql -L/usr/local/lib/mysql build_check.c -o fake > /dev/null 2> /dev/null
if [ $? != 0 ]
then
    printf "FAIL!\n"
    printf "Endianess checking failed\n"
    exit;
else
    ./fake
    if [ $? = 1 ]
    then
        ARCH=le
        CXXFLAGS="$CXXFLAGS -DARCH_LE"
        CFLAGS="$CFLAGS -DARCH_LE"
        printf "Little Endian\n"
    else
        ARCH=be
        CXXFLAGS="$CXXFLAGS -DARCH_BE"
        CFLAGS="$CFLAGS -DARCH_BE"
        printf "Big Endian\n"
    fi
fi
rm -f fake

printf "Checking for iconv 2nd argument... "
printf "#include <iconv.h>\nint main(){ const char** src = 0; iconv(0, src, 0, 0, 0); }\n" > build_check.c
$CC $CFLAGS $LDFLAGS -Werror build_check.c $LIBICONV -o fake > /dev/null 2> /dev/null
if [ $? != 0 ]
then
    printf "non-const\n"
else
    DEFS="$DEFS -DCONST_ICONV"
    printf "const\n"
fi
rm -f fake

printf "Checking for fb_config... "
FB_VERSION=`fb_config --version 2> /dev/null`
if [ "$?" != "0" ]
then
    printf "no\n"
    printf "Checking for -lfbclient... "
    printf "int main() { return 0; }\n" > build_check.c
    $CC $CFLAGS $LDFLAGS build_check.c -lfbclient $LIB_THREAD -o fake > /dev/null 2> /dev/null
    if [ $? != 0 ]
    then
        CHECK_FBCLIENT=no
        printf "no\n"
    else
        FB_CFLAGS=""
        FB_LDFLAGS="-lfbclient"
        CHECK_FBCLIENT=yes
        printf "yes\n"
    fi
    rm -f fake
else
    printf "yes\n"
    printf "Checking for fb_config --cflags... "
    FB_CFLAGS=`fb_config --cflags`
    if [ "$?" != "0" ]
    then
        CHECK_FBCLIENT=no
        printf "no\n"
    else
        printf "[$FB_CFLAGS]\n"
        printf "Checking for fb_config --libs "
        FB_LDFLAGS=`fb_config --libs`
        if [ "$?" != "0" ]
        then
            CHECK_FBCLIENT=no
            printf "no\n"
        else
            CHECK_FBCLIENT=yes
            printf "[$FB_LDFLAGS]\n"
        fi
    fi
fi

printf "Checking for mysql_config... "
printf "#include <mysql.h>\nint main() { return 0; }\n" > build_check.c
MYSQL_VERSION=`mysql_config --version 2> /dev/null`
if [ $? != 0 ]
then
    printf "no\n";
    printf "Checking for -lmysqlclient... "
    $CC $CFLAGS $LDFLAGS build_check.c -lmysqlclient_r $LIB_THREAD -o fake > /dev/null 2> /dev/null
    if [ $? != 0 ]
    then
        CHECK_MYSQLCLIENT=no
        printf "no\n"
    else
        CHECK_MYSQLCLIENT=yes
        printf "yes\n"
    fi
    rm -f fake
else
    printf "yes\n"
    printf "Checking for mysql_config --cflags... "
    MYSQL_CFLAGS=`mysql_config --cflags 2> /dev/null`
    if [ $? != 0 ]
    then
        CHECK_MYSQLCLIENT=no
        printf "no\n"
    else
        printf "[$MYSQL_CFLAGS]\n"
        printf "Checking for mysql_config --libs_r... "
        MYSQL_LDFLAGS=`mysql_config --libs_r 2> /dev/null`
        if [ $? != 0 ]
        then
            CHECK_MYSQLCLIENT=no
            printf "no\n"
        else
            CHECK_MYSQLCLIENT=yes
            printf "[$MYSQL_LDFLAGS]\n"
        fi
    fi
fi

printf "Checking for pg_config... "
printf "#include <libpq-fe.h>\nint main() { return 0; }\n" > build_check.c
PG_VERSION=`pg_config --version 2> /dev/null`
if [ $? != 0 ]
then
    printf "no\n";
    printf "Checking for -lpq... "
    $CC $CFLAGS $LDFLAGS build_check.c -lpq $LIB_THREAD -o fake > /dev/null 2> /dev/null
    if [ $? != 0 ]
    then
        CHECK_PQ=no
        printf "no\n"
    else
        CHECK_PQ=yes
        printf "yes\n"
    fi
    rm -f fake
else
    printf "yes\n";
    printf "Checking for pg_config --includedir... "
    PG_CFLAGS="-I"`pg_config --includedir 2> /dev/null`
    if [ $? != 0 ]
    then
        CHECK_PQ=no
        printf "no\n"
    else
        printf "[$PG_CFLAGS]\n"
        printf "Checking for pg_config --libdir... "
        PG_LDFLAGS="-L"`pg_config --libdir 2> /dev/null`
        if [ $? != 0 ]
        then
            CHECK_PQ=no
            printf "no\n"
        else
            CHECK_PQ=yes
            printf "[$PG_LDFLAGS]\n"
        fi
    fi
fi

rm -f build_check.c

if [ "$CHECK_FBCLIENT" = "yes" ]
then
    STG_LIBS="$STG_LIBS
          ibpp.lib"
    PLUGINS="$PLUGINS
         store/firebird"
fi

if [ "$CHECK_PQ" = "yes" ]
then
    PLUGINS="$PLUGINS
             store/postgresql"
fi

if [ "$CHECK_MYSQLCLIENT" = "yes" ]
then
    PLUGINS="$PLUGINS
         store/mysql"
fi

printf "OS=$OS\n" > $CONFFILE
printf "STG_TIME=yes\n" >> $CONFFILE
printf "DEBUG=$DEBUG\n" >> $CONFFILE
printf "DIR_BUILD=$BUILD_DIR\n" >> $CONFFILE
printf "DIR_LIB=\$(DIR_BUILD)/../../lib\n" >> $CONFFILE
printf "DIR_LIBSRC=\$(DIR_BUILD)/../../stglibs\n" >> $CONFFILE
printf "DIR_INCLUDE=\$(DIR_BUILD)/../../include\n" >> $CONFFILE
printf "DIR_MOD=\$(DIR_BUILD)/../stargazer/modules\n" >> $CONFFILE
printf "DIR_PLUGINS=\$(DIR_BUILD)/../stargazer/plugins\n" >> $CONFFILE
printf "ARCH=$ARCH\n" >> $CONFFILE
printf "CHECK_FBCLIENT=$CHECK_FBCLIENT\n" >> $CONFFILE
if [ "$CHECK_FBCLIENT" = "yes" ]
then
    printf "FB_CFLAGS=$FB_CFLAGS\n" >> $CONFFILE
    printf "FB_LDFLAGS=$FB_LDFLAGS\n" >> $CONFFILE
fi
printf "CHECK_MYSQLCLIENT=$CHECK_MYSQLCLIENT\n" >> $CONFFILE
if [ "$CHECK_MYSQLCLIENT" = "yes" ]
then
    printf "MYSQL_CFLAGS=$MYSQL_CFLAGS\n" >> $CONFFILE
    printf "MYSQL_LDFLAGS=$MYSQL_LDFLAGS\n" >> $CONFFILE
fi
printf "CHECK_PQ=$CHECK_PQ\n" >> $CONFFILE
if [ "$CHECK_PQ" = "yes" ]
then
    printf "PG_CFLAGS=$PG_CFLAGS\n" >> $CONFFILE
    printf "PG_LDFLAGS=$PG_LDFLAGS\n" >> $CONFFILE
fi
printf "DEFS=$DEFS\n" >> $CONFFILE
printf "STG_LIBS=" >> $CONFFILE
for lib in $STG_LIBS
do
    printf "$lib " >> $CONFFILE
done
printf "\n" >> $CONFFILE
printf "PLUGINS=" >> $CONFFILE
for plugin in $PLUGINS
do
    printf "$plugin " >> $CONFFILE
done
printf "\n" >> $CONFFILE
printf "CC=$CC\n" >> $CONFFILE
printf "CXX=$CXX\n" >> $CONFFILE
printf "CXXFLAGS=$CXXFLAGS\n" >> $CONFFILE
printf "CFLAGS=$CFLAGS\n" >> $CONFFILE
printf "LDFLAGS=$LDFLAGS\n" >> $CONFFILE
printf "LIB_THREAD=$LIB_THREAD\n" >> $CONFFILE
printf "LIBICONV=$LIBICONV\n" >> $CONFFILE
printf "PREFIX=$PREFIX\n" >> $CONFFILE
printf "BIN_MODE=$BIN_MODE\n" >> $CONFFILE
printf "DATA_MODE=$DATA_MODE\n" >> $CONFFILE
printf "DIR_MODE=$DIR_MODE\n" >> $CONFFILE
printf "OWNER=$OWNER\n" >> $CONFFILE
printf "VAR_DIR=$VAR_DIR\n" >> $CONFFILE

mkdir -p ../stargazer/modules

if [ "$1" != "debug" ]
then
    $MAKE $MAKEOPTS
else
    printf "\n\n\nDebug build. Type $MAKE explicitly\n"
fi