#!/usr/bin/env sh

# 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"
PKG_DEB_NAME="(unsuppored)"
PKG_RPM_NAME="(unsuppored)"
PKG_BREW_NAME="apache-arrow"
PKG_TEST_HEADER="<arrow/api.h>"
PKG_LIBS="-larrow"

# Make some env vars case-insensitive
ARROW_R_DEV=`echo $ARROW_R_DEV | tr '[:upper:]' '[:lower:]'`
FORCE_AUTOBREW=`echo $FORCE_AUTOBREW | tr '[:upper:]' '[:lower:]'`
FORCE_BUNDLED_BUILD=`echo $FORCE_BUNDLED_BUILD | tr '[:upper:]' '[:lower:]'`
ARROW_USE_PKG_CONFIG=`echo $ARROW_USE_PKG_CONFIG | tr '[:upper:]' '[:lower:]'`
LIBARROW_MINIMAL=`echo $LIBARROW_MINIMAL | tr '[:upper:]' '[:lower:]'`
TEST_OFFLINE_BUILD=`echo $TEST_OFFLINE_BUILD | tr '[:upper:]' '[:lower:]'`
NOT_CRAN=`echo $NOT_CRAN | tr '[:upper:]' '[:lower:]'`

VERSION=`grep '^Version' DESCRIPTION | sed s/Version:\ //`
UNAME=`uname -s`

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

if [ -f "tools/apache-arrow.rb" ]; then
  # If you want to use a local apache-arrow.rb formula, do
  # $ cp ../dev/tasks/homebrew-formulae/autobrew/apache-arrow.rb tools/apache-arrow.rb
  # before R CMD build or INSTALL (assuming a local checkout of the apache/arrow repository)
  cp tools/autobrew .
  if [ "$FORCE_AUTOBREW" != "false" ]; then
    # It is possible to turn off forced autobrew if the formula is included,
    # but most likely you shouldn't because the included formula will reference
    # the C++ library at the version that matches the R package.
    FORCE_AUTOBREW="true"
  fi
fi

if [ "$FORCE_AUTOBREW" = "true" ] || [ "$FORCE_BUNDLED_BUILD" = "true" ]; then
  ARROW_USE_PKG_CONFIG="false"
fi

# Note that cflags may be empty in case of success
if [ "$ARROW_HOME" ] && [ "$FORCE_BUNDLED_BUILD" != "true" ]; then
  echo "*** Using ARROW_HOME as the source of libarrow"
  PKG_CFLAGS="-I$ARROW_HOME/include $PKG_CFLAGS"
  PKG_DIRS="-L$ARROW_HOME/lib"
elif [ "$INCLUDE_DIR" ] && [ "$LIB_DIR" ]; then
  echo "*** Using INCLUDE_DIR/LIB_DIR as the source of libarrow"
  PKG_CFLAGS="-I$INCLUDE_DIR $PKG_CFLAGS"
  PKG_DIRS="-L$LIB_DIR"
else
  # Use pkg-config to find libarrow if available and allowed
  pkg-config --version >/dev/null 2>&1
  if [ $? -eq 0 ] && [ "$ARROW_USE_PKG_CONFIG" != "false" ]; then
    # Set the search paths and compile flags
    PKGCONFIG_CFLAGS=`pkg-config --cflags --silence-errors ${PKG_CONFIG_NAME}`
    PKGCONFIG_LIBS=`pkg-config --libs-only-l --libs-only-other --silence-errors ${PKG_CONFIG_NAME}`
    PKGCONFIG_DIRS=`pkg-config --libs-only-L --silence-errors ${PKG_CONFIG_NAME}`
  fi

  if [ "$PKGCONFIG_CFLAGS" ] && [ "$PKGCONFIG_LIBS" ]; then
    FOUND_LIB_DIR=`echo $PKG_DIRS | sed -e 's/^-L//'`
    echo "*** Arrow C++ libraries found via pkg-config at $FOUND_LIB_DIR"
    PKG_CFLAGS="$PKGCONFIG_CFLAGS"
    PKG_LIBS=${PKGCONFIG_LIBS}
    PKG_DIRS=${PKGCONFIG_DIRS}

    # Check for version mismatch
    PC_LIB_VERSION=`pkg-config --modversion arrow`
    echo $PC_LIB_VERSION | grep -e 'SNAPSHOT$' >/dev/null 2>&1
    # If on a release (i.e. not SNAPSHOT) and version != R package version, warn
    if [ $? -eq 1 ] && [ "$PC_LIB_VERSION" != "$VERSION" ]; then
      echo "**** Warning: library version mismatch"
      echo "**** C++ is $PC_LIB_VERSION but R is $VERSION"
      echo "**** If installation fails, upgrade the C++ library to match"
      echo "**** or retry with ARROW_USE_PKG_CONFIG=false"
    fi
  else
    if [ "$UNAME" = "Darwin" ] && [ "$FORCE_BUNDLED_BUILD" != "true" ]; then
      if [ "$FORCE_AUTOBREW" != "true" ] && [ "`command -v brew`" ] && [ "`brew ls --versions ${PKG_BREW_NAME}`" != "" ]; then
        echo "*** Using Homebrew ${PKG_BREW_NAME}"
        BREWDIR=`brew --prefix`
        PKG_LIBS="$PKG_LIBS -larrow_bundled_dependencies"
        PKG_DIRS="-L$BREWDIR/opt/$PKG_BREW_NAME/lib $PKG_DIRS"
        PKG_CFLAGS="-I$BREWDIR/opt/$PKG_BREW_NAME/include"
      else
        echo "*** Downloading ${PKG_BREW_NAME}"
        if [ -f "autobrew" ]; then
          echo "**** Using local manifest for ${PKG_BREW_NAME}"
        else
          curl -sfL "https://autobrew.github.io/scripts/$PKG_BREW_NAME" > autobrew
          if [ $? -ne 0 ]; then
            echo "Failed to download manifest for ${PKG_BREW_NAME}"
          fi
        fi
        . autobrew
        if [ $? -ne 0 ]; then
          echo "Failed to retrieve binary for ${PKG_BREW_NAME}"
        fi
        # autobrew sets `PKG_LIBS`, `PKG_DIRS`, and `PKG_CFLAGS`
      fi
    else
      if [ "${NOT_CRAN}" = "true" ]; then
        # Set some default values
        if [ "${LIBARROW_BINARY}" = "" ]; then
          LIBARROW_BINARY=true; export LIBARROW_BINARY
        fi
        if [ "${LIBARROW_MINIMAL}" = "" ]; then
          LIBARROW_MINIMAL=false; export LIBARROW_MINIMAL
        fi
      fi

      # find openssl on macos. macOS ships with libressl. openssl is installable
      # with brew, but it is generally not linked. We can over-ride this and find
      # openssl but setting OPENSSL_ROOT_DIR (which cmake will pick up later in
      # the installation process). FWIW, arrow's cmake process uses this
      # same process to find openssl, but doing it now allows us to catch it in
      # nixlibs.R and throw a nicer error.
      if [ "$UNAME" = "Darwin" ] && [ "${OPENSSL_ROOT_DIR}" = "" ]; then
        brew --prefix openssl >/dev/null 2>&1
        if [ $? -eq 0 ]; then
          OPENSSL_ROOT_DIR="`brew --prefix openssl`"; export OPENSSL_ROOT_DIR
        fi
      fi

      if [ "${ARROW_DEPENDENCY_SOURCE}" = "" ]; then
        # TODO: BUNDLED is still default for now, but we plan to change it to AUTO
        ARROW_DEPENDENCY_SOURCE=BUNDLED; export ARROW_DEPENDENCY_SOURCE
      fi
      if [ "${ARROW_DEPENDENCY_SOURCE}" = "AUTO" ]; then
        pkg-config --version >/dev/null 2>&1
        if [ $? -ne 0 ]; then
          export ARROW_DEPENDENCY_SOURCE=BUNDLED
          echo "**** Warning: ARROW_DEPENDENCY_SOURCE set to 'AUTO' but pkg-config not installed"
          echo "**** ARROW_DEPENDENCY_SOURCE has been set to 'BUNDLED'"
        fi
      fi

      ${R_HOME}/bin/Rscript tools/nixlibs.R $VERSION
      PKG_CFLAGS="-I`pwd`/libarrow/arrow-${VERSION}/include $PKG_CFLAGS"

      LIB_DIR="libarrow/arrow-${VERSION}/lib"
      if [ -d "$LIB_DIR" ]; then
        # Enumerate the static libs, put their -l flags in BUNDLED_LIBS,
        # and put their -L location in PKG_DIRS
        #
        # If tools/nixlibs.R fails to produce libs, this dir won't exist
        # so don't try (the error message from `ls` would be misleading)
        # Assume nixlibs.R has handled and messaged about its failure already
        #
        # TODO: what about non-bundled deps?
        BUNDLED_LIBS=`cd $LIB_DIR && ls *.a`
        BUNDLED_LIBS=`echo "$BUNDLED_LIBS" | sed -e "s/\\.a lib/ -l/g" | sed -e "s/\\.a$//" | sed -e "s/^lib/-l/" | tr '\n' ' ' | sed -e "s/ $//"`
        PKG_DIRS="-L`pwd`/$LIB_DIR"

        # Use pkg-config to do static linking of libarrow's dependencies
        if [ "$ARROW_DEPENDENCY_SOURCE" = "AUTO" ] || [ "$ARROW_DEPENDENCY_SOURCE" = "SYSTEM" ]; then
          PKG_LIBS="$PKG_LIBS `PKG_CONFIG_PATH=${LIB_DIR}/pkgconfig pkg-config --libs-only-l --libs-only-other --static  --silence-errors ${PKG_CONFIG_NAME}`"
        fi

        # When using brew's openssl it is not bundled and it is not on the system
        # search path  and so we must add the lib path to BUNDLED_LIBS if we are
        # using it. Note the order is important, this must be after the arrow
        # lib path + the pkg and bundled libs above so this is why we're
        # appending to BUNDLED_LIBS and not PKG_DIRS
        if [ "$OPENSSL_ROOT_DIR" != "" ]; then
          BUNDLED_LIBS="$BUNDLED_LIBS -L$OPENSSL_ROOT_DIR/lib"
        fi
      fi
    fi
  fi
fi

# If on Raspberry Pi, need to manually link against latomic
# See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81358 for similar example
if grep raspbian /etc/os-release >/dev/null 2>&1; then
  PKG_CFLAGS="$PKG_CFLAGS -DARROW_CXXFLAGS=-latomic"
fi

# 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

# Set any user-defined CXXFLAGS
if [ "$ARROW_R_CXXFLAGS" ]; then
  PKG_CFLAGS="$PKG_CFLAGS $ARROW_R_CXXFLAGS"
fi

# Test that we can find libarrow
CXXCPP="`${R_HOME}/bin/R CMD config CXX11` -E"
if [ $? -eq 0 ]; then
  # Great, CXX11 exists for this version of R (current);
  # now let's set the other two variables
  CXX11FLAGS=`"${R_HOME}"/bin/R CMD config CXX11FLAGS`
  CXX11STD=`"${R_HOME}"/bin/R CMD config CXX11STD`
else
  # For compatibility with R < 3.4, when these were called CXX1X
  CXXCPP="`${R_HOME}/bin/R CMD config CXX1X` -E"
  CXX11FLAGS=`"${R_HOME}"/bin/R CMD config CXX1XFLAGS`
  CXX11STD=`"${R_HOME}"/bin/R CMD config CXX1XSTD`
fi
CPPFLAGS=`"${R_HOME}"/bin/R CMD config CPPFLAGS`
TEST_CMD="${CXXCPP} ${CPPFLAGS} ${PKG_CFLAGS} ${CXX11FLAGS} ${CXX11STD} -xc++ -"
echo "#include $PKG_TEST_HEADER" | ${TEST_CMD} >/dev/null 2>&1

if [ $? -eq 0 ]; then
  PKG_CFLAGS="$PKG_CFLAGS -DARROW_R_WITH_ARROW"
  # Check for features
  LIB_DIR=`echo $PKG_DIRS | sed -e 's/^-L//'`
  ARROW_OPTS_CMAKE="$LIB_DIR/cmake/arrow/ArrowOptions.cmake"
  # Check for Parquet
  grep 'set(ARROW_PARQUET "ON")' $ARROW_OPTS_CMAKE >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    PKG_CFLAGS="$PKG_CFLAGS -DARROW_R_WITH_PARQUET"
    PKG_LIBS="-lparquet $PKG_LIBS"
    # NOTE: parquet is assumed to have the same -L flag as arrow
    # so there is no need to add its location to PKG_DIRS
  fi
  # Check for Arrow Dataset subcomponent
  grep 'set(ARROW_DATASET "ON")' $ARROW_OPTS_CMAKE >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    PKG_CFLAGS="$PKG_CFLAGS -DARROW_R_WITH_DATASET"
    PKG_LIBS="-larrow_dataset $PKG_LIBS"
    # NOTE: arrow-dataset is assumed to have the same -L flag as arrow
    # so there is no need to add its location to PKG_DIRS
  fi
  # Check for S3
  grep 'set(ARROW_S3 "ON")' $ARROW_OPTS_CMAKE >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    PKG_CFLAGS="$PKG_CFLAGS -DARROW_R_WITH_S3"
    if [ "$BUNDLED_LIBS" != "" ]; then
      # We're depending on openssl/curl from the system, so they're not in the bundled deps
      BUNDLED_LIBS="$BUNDLED_LIBS -lssl -lcrypto -lcurl"
    fi
  fi
  # Check for JSON
  grep 'set(ARROW_JSON "ON")' $ARROW_OPTS_CMAKE >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    PKG_CFLAGS="$PKG_CFLAGS -DARROW_R_WITH_JSON"
  fi
  # prepend PKG_DIRS and append BUNDLED_LIBS to PKG_LIBS
  PKG_LIBS="$PKG_DIRS $PKG_LIBS $BUNDLED_LIBS"
  echo "PKG_CFLAGS=$PKG_CFLAGS"
  echo "PKG_LIBS=$PKG_LIBS"
else
  echo "------------------------- NOTE ---------------------------"
  echo "There was an issue preparing the Arrow C++ libraries."
  echo "See https://arrow.apache.org/docs/r/articles/install.html"
  echo "---------------------------------------------------------"
  PKG_LIBS=""
  PKG_CFLAGS=""
  if [ "$UNAME" != "SunOS" ] && [ "$TEST_R_WITHOUT_LIBARROW" != "TRUE" ]; then
    # We should build fine on Solaris, but because we don't have CI for it,
    # allow the build to proceed with most R package features disabled.
    # But on every other platforom stop here if libarrow was not found.
    # (also check an env var so that we can test this build configuration)
    exit 1
  fi
fi

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

# This is removed because a (bad?) CRAN check fails when arrow.so is stripped
# # Add stripping
# if [ "$R_STRIP_SHARED_LIB" != "" ]; then
#   # R_STRIP_SHARED_LIB is set in the global Renviron and should be available here
#   echo "
# strip: \$(SHLIB)
# 	$R_STRIP_SHARED_LIB \$(SHLIB) >/dev/null 2>&1 || true
#
# .phony: strip
# " >> src/Makevars
# fi

# Success
exit 0
