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

# https://github.com/jeroen/autobrew/blob/gh-pages/apache-arrow
export HOMEBREW_NO_ANALYTICS=1
export HOMEBREW_NO_AUTO_UPDATE=1

if [[ ${OSTYPE:6} -ge 20 ]]; then
  # We are on a modern enough macOS, we can use the real brew
  UPSTREAM_ORG="homebrew"
  PKG_BREW_NAME="$PKG_BREW_NAME-static"
else
  # Official Homebrew no longer supports El-Capitan
  # so we need to use the forked autobrew version of brew that supports old macOSes
  UPSTREAM_ORG="autobrew"
fi

if [ "$DISABLE_AUTOBREW" ]; then return 0; fi
AUTOBREW=${TMPDIR-/tmp}
export HOMEBREW_TEMP="$AUTOBREW/hbtmp"
BREWDIR="$AUTOBREW/build-$PKG_BREW_NAME"
BREW="$BREWDIR/bin/brew"
rm -Rf $BREWDIR
mkdir -p $BREWDIR
echo "$(date): Auto-brewing $PKG_BREW_NAME in $BREWDIR..."
curl -fsSL https://github.com/$UPSTREAM_ORG/brew/tarball/master | tar xz --strip 1 -C $BREWDIR

# Install bottle + dependencies
export HOMEBREW_CACHE="$AUTOBREW"
LOCAL_FORMULA="tools/${PKG_BREW_NAME}.rb"
if [ -f "$LOCAL_FORMULA" ]; then
  if [[ ${OSTYPE:6} -ge 20 ]]; then
    $BREW tap

    # Tap https://github.com/autobrew/homebrew-cran so that we can get dependencies from there
    $BREW tap autobrew/cran
  fi

  # Use the local brew formula and install --HEAD
  $BREW deps -n "$LOCAL_FORMULA" 2>/dev/null
  BREW_DEPS=$($BREW deps -n "$LOCAL_FORMULA" 2>/dev/null)
  $BREW install --force-bottle $BREW_DEPS 2>&1 | perl -pe 's/Warning/Note/gi'
  $BREW install -v --build-from-source --HEAD "$LOCAL_FORMULA" 2>&1 | perl -pe 's/Warning/Note/gi'
else
  $BREW install --force-bottle $BREW_DEPS $PKG_BREW_NAME 2>&1 | perl -pe 's/Warning/Note/gi'
fi

# Hardcode this for my custom autobrew build
rm -f $BREWDIR/lib/*.dylib
AWS_LIBS="-laws-cpp-sdk-config -laws-cpp-sdk-transfer -laws-cpp-sdk-identity-management -laws-cpp-sdk-cognito-identity -laws-cpp-sdk-sts -laws-cpp-sdk-s3 -laws-cpp-sdk-core -laws-c-event-stream -laws-checksums -laws-c-common -laws-crt-cpp -laws-c-io -laws-c-s3 -laws-c-auth -laws-c-http -laws-c-cal -laws-c-compression -laws-c-mqtt -lpthread -lcurl"
PKG_LIBS="-lparquet -larrow_dataset -larrow_acero -larrow -larrow_bundled_dependencies -lthrift -lbrotlienc-static -lbrotlidec-static -lbrotlicommon-static -llz4 -lsnappy -lzstd $AWS_LIBS -lssl -lcrypto"
PKG_DIRS="-L$BREWDIR/lib"

# Prevent CRAN builder from linking against old libs in /usr/local/lib
for FILE in $BREWDIR/Cellar/*/*/lib/*.a; do
  BASENAME=`basename $FILE`
  LIBNAME=`echo "${BASENAME%.*}" | cut -c4-`
  cp -f $FILE $BREWDIR/lib/libbrew$LIBNAME.a
  echo "created $BREWDIR/lib/libbrew$LIBNAME.a"
  PKG_LIBS=`echo $PKG_LIBS | sed "s/-l$LIBNAME/-lbrew$LIBNAME/g"`
done

PKG_CFLAGS="-I$BREWDIR/opt/$PKG_BREW_NAME/include -DARROW_R_WITH_PARQUET -DARROW_R_WITH_ACERO -DARROW_R_WITH_DATASET -DARROW_R_WITH_JSON -DARROW_R_WITH_S3 -DARROW_R_WITH_GCS -D_LIBCPP_DISABLE_AVAILABILITY"

unset HOMEBREW_NO_ANALYTICS
unset HOMEBREW_NO_AUTO_UPDATE
