#!/bin/sh
## Emacs please make this -*- mode: shell-mode; -*-
##
##  configure -- Unix build preparation system
##
##  Copyright (C) 2015 - 2021  Dirk Eddelbuettel and Jeroen Ooms.
##
##  This file is part of Rblpapi
##
##  Rblpapi 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 2 of the License, or
##  (at your option) any later version.
##
##  Rblpapi 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 Rblpapi.  If not, see <http://www.gnu.org/licenses/>.

## Check that we are on Unix
if ! [ -x $(command -v uname) ]; then
    echo "You do not have 'uname' so this is unlikely to be a Unix system. Exiting."
    exit 1
fi

## Check for Linux or OSX
: ${R_HOME=$(R RHOME)}
sysname=$(${R_HOME}/bin/Rscript -e 'cat(Sys.info()["sysname"])')
if [ ${sysname} = "Linux" ]; then
    platform="linux"
elif [ ${sysname} = "Darwin" ]; then
    platform="osx"
else
    echo "Unsupported platform: $sysname"
    echo "Check https://www.bloomberg.com/professional/support/api-library/ for possible support first."
    echo "Contributions welcome, see https://github.com/Rblp/blp for integration with Rblapi."
    exit 2
fi

## Populate Makevars
rpath=$(${R_HOME}/bin/Rscript -e 'cat(file.path(.libPaths()[1], "Rblpapi", "blp"))')
arch=$(uname -m)
if [ "${arch}" = "x86_64" ]; then
    echo "Setting up compilation for a ${platform} 64-bit system"
    sed -e"s/@config@/blpapi3_64/" -e"s|@rpath@|"${rpath}"|" src/Makevars.in > src/Makevars
    flavour="64"
elif [ "${arch}" = "i686" ]; then
    echo "Setting up compilation for a ${platform} 32-bit system"
    sed -e"s/@config@/blpapi3_32/" -e"s|@rpath@|"${rpath}"|" src/Makevars.in > src/Makevars
    flavour="32"
else
    echo "Unknown architecture: ${arch}. Exiting."
    exit 3
fi

## helper function to not rely on curl which at least on OS X fails to follow redirects
download() {
    url=${1}
    ## sadly, for Travis we cannot rely on R as it lacks libcurl
    libcurl=$(${R_HOME}/bin/Rscript -e 'cat(capabilities()[["libcurl"]])')
    ## so when we have libcurl in R, use it -- else fall back to curl
    if [ ${libcurl} = "TRUE" ]; then
        file=$(basename ${url})
        ${R_HOME}/bin/Rscript -e "download.file(\"${url}\", \"${file}\", quiet=TRUE, method='libcurl')"
    else
        curl -s -k -L -O ${url}
    fi
}

## Get and install header files
cwd=$(pwd)
mkdir -p blp/${platform}
cd blp/${platform}
if [ ! -f blpHeaders.tar.gz ]; then
    download https://github.com/Rblp/blp/raw/master/headers/${platform}/blpHeaders.tar.gz
    tar xfz blpHeaders.tar.gz -C ../../inst
fi
cd ${cwd}

## Get and install precompiled shared library
mkdir -p inst/blp
cd blp/${platform}
if [ ! -f blpLibrary.tar.gz ]; then
    download https://github.com/Rblp/blp/raw/master/${platform}${flavour}/blpLibrary.tar.gz
    tar xfz blpLibrary.tar.gz -C ../../inst/blp/ libblpapi3_${flavour}.so
fi
cd ${cwd}

exit 0
