#!/bin/bash

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# Anticonf (tm) script by Jeroen Ooms, Jim Hester (2017)
# License: MIT
#
# This script will query 'pkg-config' for the required cflags and ldflags.
# If pkg-config is unavailable or does not find the library, try setting
# INCLUDE_DIR and LIB_DIR manually via e.g:
# R CMD INSTALL --configure-vars='INCLUDE_DIR=/.../include LIB_DIR=/.../lib'

# Library settings
PKG_CONFIG_NAME="arrow parquet"
PKG_DEB_NAME="(unsuppored)"
PKG_RPM_NAME="(unsuppored)"
PKG_BREW_NAME="apache-arrow"
PKG_TEST_HEADER="<arrow/api.h>"
PKG_LIBS="-larrow -lparquet"

# generate code
if [ "$ARROW_R_DEV" == "TRUE" ]; then
  echo "*** Generating code with data-raw/codegen.R"
  ${R_HOME}/bin/Rscript data-raw/codegen.R
fi

# Note that cflags may be empty in case of success
if [ "$INCLUDE_DIR" ] || [ "$LIB_DIR" ]; then
  echo "Found INCLUDE_DIR and/or LIB_DIR!"
  PKG_CFLAGS="-I$INCLUDE_DIR $PKG_CFLAGS"
  PKG_LIBS="-L$LIB_DIR $PKG_LIBS"
else
  # Use pkg-config if available
  pkg-config --version >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    PKGCONFIG_CFLAGS=`pkg-config --cflags --silence-errors ${PKG_CONFIG_NAME}`
    PKGCONFIG_LIBS=`pkg-config --libs --silence-errors ${PKG_CONFIG_NAME}`
  fi

  if [ "$PKGCONFIG_CFLAGS" ] || [ "$PKGCONFIG_LIBS" ]; then
    echo "Arrow C++ libraries found via pkg-config"
    PKG_CFLAGS="$PKGCONFIG_CFLAGS"
    PKG_LIBS=${PKGCONFIG_LIBS}
  else
    if [[ "$OSTYPE" == "darwin"* ]]; then
      if [ "$(command -v brew)" ] && [ "$(brew ls --versions ${PKG_BREW_NAME})" != "" ]; then
        echo "Using Homebrew ${PKG_BREW_NAME}"
        BREWDIR=$(brew --prefix)
      else
        echo "Downloading ${PKG_BREW_NAME}"
        curl -sfL "https://jeroen.github.io/autobrew/$PKG_BREW_NAME" > autobrew
        if [ $? -ne 0 ]; then
          echo "Failed to download manifest for ${PKG_BREW_NAME}"
        fi
        source autobrew
        if [ $? -ne 0 ]; then
          echo "Failed to retrieve binary for ${PKG_BREW_NAME}"
        fi
      fi
      PKG_CFLAGS="-I$BREWDIR/opt/$PKG_BREW_NAME/include"
      PKG_LIBS="-L$BREWDIR/opt/$PKG_BREW_NAME/lib $PKG_LIBS"
    fi
  fi
fi

# Find compiler
CXXCPP=$("${R_HOME}"/bin/R CMD config CXXCPP)
CXX11FLAGS=$("${R_HOME}"/bin/R CMD config CXX11FLAGS)
CXX11STD=$("${R_HOME}"/bin/R CMD config CXX11STD)
CPPFLAGS=$("${R_HOME}"/bin/R CMD config CPPFLAGS)

# If libarrow uses the old GLIBCXX ABI, so we have to use it too
if [ "$ARROW_USE_OLD_CXXABI" ]; then
  $PKG_CFLAGS="$PKG_CFLAGS -D_GLIBCXX_USE_CXX11_ABI=0"
fi

# Test configuration
TEST_CMD="${CXXCPP} ${CPPFLAGS} ${PKG_CFLAGS} ${CXX11FLAGS} ${CXX11STD} -xc++ -"
echo "#include $PKG_TEST_HEADER" | ${TEST_CMD} >/dev/null 2>&1

if [ $? -eq 0 ] || [[ "$OSTYPE" == "darwin"* ]]; then
  # Always build with arrow on macOS
  PKG_CFLAGS="$PKG_CFLAGS -DARROW_R_WITH_ARROW"
  echo "PKG_CFLAGS=$PKG_CFLAGS"
  echo "PKG_LIBS=$PKG_LIBS"
else
  if [[ "$OSTYPE" == "darwin"* ]]; then
    # Just for debugging: is this possible?
    echo "Test to load header failed. Command:"
    echo "$TEST_CMD"
  fi
  echo "------------------------- NOTE ---------------------------"
  echo "After installation, please run arrow::install_arrow()"
  echo "for help installing required runtime libraries"
  echo "---------------------------------------------------------"
  PKG_LIBS=""
  PKG_CFLAGS=""
fi

# Write to Makevars
sed -e "s|@cflags@|$PKG_CFLAGS|" -e "s|@libs@|$PKG_LIBS|" src/Makevars.in > src/Makevars

# Success
exit 0
