# 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.

R_BIN="$R_HOME/bin/R"
RSCRIPT_BIN="$R_HOME/bin/Rscript"

# Run bootstrap.R. This will have already run if we are installing a source
# package built with pkgbuild::build() with pkgbuild >1.4.0; however, we
# run it again in case this is R CMD INSTALL on a directory or
# devtools::load_all(). This will vendor files from elsewhere in the
# ADBC repo into this package. If the file doesn't exist, we're installing
# from a pre-built tarball.
if [ -f bootstrap.R ]; then
  "$RSCRIPT_BIN" bootstrap.R
fi

# Find the go binary so that we can go build!
if [ -z "$GO_BIN" ]; then
  GO_BIN=`which go`
fi

if [ -z "$GO_BIN"]; then
  if [ ! -f src/go/tmp/go/bin/go ]; then
    echo ""
    echo "Downloading and extracting Go into the package source directory:"
    echo "This may take a few minutes. To eliminate this step, install Go"
    echo "from your faviourite package manager or set the GO_BIN environment variable:"
    echo "- apt-get install golang"
    echo "- brew install golang"
    echo "- dnf install golang"
    echo "- apk add go"
    echo "- pacman -S go"

    "$RSCRIPT_BIN" tools/download-go.R
  fi

  GO_BIN="`pwd`/src/go/tmp/go/bin/go"
fi

echo "Trying 'go version' with GO_BIN at '$GO_BIN'"
"$GO_BIN" version
if [ $? -ne 0 ]; then
  echo "go --version had a non-zero exit code"
  exit 1
fi

# Get the CC and CXX compilers
CC=`"$R_BIN" CMD config CC`
CXX=`"$R_BIN" CMD config CXX`

# clang and gcc use different symbol-hiding syntax and we need to
# make sure to hide any Adbc* symbols that might conflict with another
# driver.
if "$R_BIN" CMD config CC | grep -e "clang" ; then
  SYMBOL_ARGS="-Wl,-exported_symbol,_adbcflightsql_c_flightsql -Wl,-exported_symbol,_R_init_adbcflightsql"
elif "$R_BIN" CMD config CC | grep -e "gcc" ; then
  SYMBOL_ARGS="-Wl,--version-script=go/symbols.map"
fi

# On OSX we need -framework Security because of some dependency somewhere
if [ `uname` = "Darwin" ]; then
  PKG_LIBS="-framework Security -lresolv $PKG_LIBS"
fi

PKG_LIBS="$PKG_LIBS $SYMBOL_ARGS"

sed \
  -e "s|@gobin@|$GO_BIN|" \
  -e "s|@libs@|$PKG_LIBS|" \
  -e "s|@cc@|$CC|" \
  -e "s|@cxx@|$CXX|" \
  src/Makevars.in > src/Makevars

if [ -f "src/go/adbc/pkg/flightsql/driver.go" ]; then
  echo "Found vendored ADBC FlightSQL driver"
  exit 0
fi

echo "Vendored ADBC FlightSQL driver was not found."
echo "This source package was probably built incorrectly and it's probably not your fault"
exit 1
