#!/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="arrow"
PKG_RPM_NAME="arrow"
PKG_CSW_NAME="arrow"
PKG_BREW_NAME="apache-arrow"
PKG_TEST_HEADER="<arrow/api.h>"
PKG_LIBS=""

# Use pkg-config if available
pkg-config --version >/dev/null 2>&1
if [ $? -eq 0 ]; then
  PKGCONFIG_CFLAGS=$(pkg-config --cflags --silence-errors arrow)
  if [ $? -ne 0 ]; then
    echo "Apache Arrow C++ was not found using pkg-config"
    exit 1
  fi
  PKGCONFIG_LIBS=$(pkg-config --libs arrow)
  PKGCONFIG_CFLAGS_PARQUET=$(pkg-config --cflags --silence-errors parquet)
  if [ $? -eq 0 ]; then
    PKGCONFIG_CFLAGS="${PKGCONFIG_CFLAGS} ${PKGCONFIG_CFLAGS_PARQUET} -DARROW_R_WITH_PARQUET"
    PKGCONFIG_LIBS="${PKGCONFIG_LIBS} $(pkg-config --libs parquet)"
  fi
else
  PKG_LIBS="-larrow -lparquet"
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"
elif [ "$PKGCONFIG_CFLAGS" ] || [ "$PKGCONFIG_LIBS" ]; then
  echo "Found pkg-config cflags and libs!"
  PKG_CFLAGS=${PKGCONFIG_CFLAGS}
  PKG_LIBS=${PKGCONFIG_LIBS}
elif [[ "$OSTYPE" == "darwin"* ]]; then
  if [ "$(command -v brew)" ]; then
    BREWDIR=$(brew --prefix)
  else
    curl -sfL "https://jeroen.github.io/autobrew/$PKG_BREW_NAME" > autobrew
    source autobrew
  fi
  PKG_CFLAGS="-I$BREWDIR/opt/$PKG_BREW_NAME/include"
  PKG_LIBS="-L$BREWDIR/opt/$PKG_BREW_NAME/lib $PKG_LIBS"
fi

# For debugging
echo "PKG_CFLAGS=$PKG_CFLAGS"
echo "PKG_LIBS=$PKG_LIBS"

# 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
PKG_CXXFLAGS="$C_VISIBILITY"
if [ "$ARROW_USE_OLD_CXXABI" ]; then
  PKG_CXXFLAGS="$PKG_CXXFLAGS -D_GLIBCXX_USE_CXX11_ABI=0"
fi

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

# Customize the error
if [ $? -ne 0 ]; then
  echo "------------------------- ANTICONF ERROR ---------------------------"
  echo "Configuration failed because $PKG_CONFIG_NAME was not found. Try installing:"
  echo " * deb: $PKG_DEB_NAME (Debian, Ubuntu, etc)"
  echo " * rpm: $PKG_RPM_NAME (Fedora, CentOS, RHEL)"
  echo " * csw: $PKG_CSW_NAME (Solaris)"
  echo " * brew: $PKG_BREW_NAME (Mac OSX)"
  echo "If $PKG_CONFIG_NAME is already installed, check that 'pkg-config' is in your"
  echo "PATH and PKG_CONFIG_PATH contains a $PKG_CONFIG_NAME.pc file. If pkg-config"
  echo "is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:"
  echo "R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'"
  echo "--------------------------------------------------------------------"
  exit 1;
fi

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

# Success
exit 0
