#!/bin/bash

#  Copyright 2006-2017, Matthew Welland.
# 
# This file is part of Megatest.
# 
#     Megatest is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
# 
#     Megatest is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
#     You should have received a copy of the GNU General Public License
#     along with Megatest.  If not, see <http://www.gnu.org/licenses/>.

# Configure the build

if [[ "$1"x == "x" ]];then
    PREFIX=$PWD
else
    PREFIX=$1
fi

if [[ -e /usr/bin/sw_vers ]]; then
    ARCHSTR=$(/usr/bin/sw_vers -productVersion)
else
    ARCHSTR=$(lsb_release -sr)
fi

#======================================================================
# Arch dependent stuff
#======================================================================

# IUP Arch key
case $ARCHSTR in
    18.04)
	IUPARCH=415
	CDVER=5.12
	IMVER=3.13
	IUPVER=3.28
	IUPEGGVER=":1.9.0"
	;;
    dunno)
	IUPARCH=44
	CDVER=5.12
	IMVER=3.13
	IUPVER=3.28
	;;
    *)
	IUPARCH=26g4
	CDVER=5.10
	IMVER=3.11
	IUPVER=3.19.1
	IUPEGGVER=
	;;
esac

#======================================================================
# Required software checks
#======================================================================

if [[ ! $(type fossil) ]];then
    echo "Please install fossil from http://fossil-scm.org"
    exit 1
fi

#======================================================================
# Configure stuff needed for eggs
#======================================================================

function configure_dependencies () {

    #======================================================================
    # libnanomsg
    #======================================================================

    if [[ ! $(ls /usr/lib/*/libnanomsg*) ]];then
	echo "libnanomsg build needed."
	echo "BUILD_NANOMSG=yes" >> makefile.inc
    fi

    #======================================================================
    # postgresql libraries
    #======================================================================

    if [[ ! $(ls /usr/lib/*/libpq.*) ]];then
	echo "Postgresql build needed."
	echo "BUILD_POSTGRES=yes" >> makefile.inc
    fi

    if [[ ! $(ls /usr/lib/*/libsqlite3.*) ]];then
    echo "Sqlite3 build needed."
	echo "BUILD_SQLITE3=yes" >> makefile.inc
    fi

}

#======================================================================
# Initialize makefile.inc
#======================================================================

echo "" > makefile.inc
echo "" > makefile2.inc

#======================================================================
# Do we need Chicken?
#======================================================================

CHICKEN_PREFIX=$PREFIX/bin/.$ARCHSTR
echo "CHICKEN_PREFIX=$CHICKEN_PREFIX" >> makefile.inc
echo "PREFIX=$PREFIX" >> makefile.inc
echo "ARCHSTR=$ARCHSTR" >> makefile.inc
echo "IUPARCH=$IUPARCH" >> makefile.inc
echo "CDVER=$CDVER" >> makefile.inc
echo "IMVER=$IMVER" >> makefile.inc
echo "IUPVER=$IUPVER" >> makefile.inc
echo "IUPEGGVER=$IUPEGGVER" >> makefile.inc

if [[ ! $(type csi) ]];then
    echo "Chicken build needed."
    echo "BUILD_CHICKEN=yes" >> makefile.inc
    configure_dependencies
    echo "include chicken.makefile" >> makefile2.inc
else
    echo "CSIPATH=$(which csi)" >> makefile.inc
    echo "CKPATH=$(dirname $(dirname $CSIPATH))" >> makefile.inc
fi

# Make setup scripts
echo "#!/bin/bash" > setup.sh
echo "export PATH=$CHICKEN_PREFIX/bin:\$PATH" >> setup.sh
echo "export LD_LIBRARY_PATH=$CHICKEN_PREFIX/lib" >> setup.sh
echo 'exec "$@"' >> setup.sh
chmod a+x setup.sh

echo "setenv PATH $CHICKEN_PREFIX/bin:\$PATH" > setup.csh
echo "setenv LD_LIBRARY_PATH $CHICKEN_PREFIX/lib" >> setup.csh

echo "All done creating makefile.inc, feel free to edit it!"
echo "run \"setup.sh bash\" or source setup.csh to get PATH and LD_LIBRARY_PATH adjusted"
