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

# Used by cpp/src/arrow/flight/sql/odbc/install/mac/postinstall

set -euo pipefail

# Admin privilege is needed to add ODBC driver registration
if [ $EUID -ne 0 ]; then
  echo "Please run this script with sudo"
  exit 1
fi

ODBC_64BIT="$1"

if [[ -z "$ODBC_64BIT" ]]; then
  echo "error: 64-bit driver is not specified. Call format: install_odbc abs_path_to_64_bit_driver"
  exit 1
fi

if [ ! -f "$ODBC_64BIT" ]; then
  echo "64-bit driver can not be found: $ODBC_64BIT"
  echo "Call format: install_odbc abs_path_to_64_bit_driver"
  exit 1
fi

case "$(uname)" in
  Linux)
    SYSTEM_ODBCINST_FILE="/etc/odbcinst.ini"
    ;;
  *)
    # macOS
    SYSTEM_ODBCINST_FILE="/Library/ODBC/odbcinst.ini"
    mkdir -p /Library/ODBC
    ;;
esac

DRIVER_NAME="Apache Arrow Flight SQL ODBC Driver"

touch "$SYSTEM_ODBCINST_FILE"

if grep -q "^\[$DRIVER_NAME\]" "$SYSTEM_ODBCINST_FILE"; then
  echo "Driver [$DRIVER_NAME] already exists in odbcinst.ini"
else
  echo "Adding [$DRIVER_NAME] to odbcinst.ini..."
  echo "
[$DRIVER_NAME]
Description=An ODBC Driver for Apache Arrow Flight SQL
Driver=$ODBC_64BIT
" >>"$SYSTEM_ODBCINST_FILE"
fi

# Check if [ODBC Drivers] section exists
if grep -q '^\[ODBC Drivers\]' "$SYSTEM_ODBCINST_FILE"; then
  # Section exists: check if driver entry exists
  if ! grep -q "^${DRIVER_NAME}=" "$SYSTEM_ODBCINST_FILE"; then
    # Driver entry does not exist, add under [ODBC Drivers]

    awk -v driver="$DRIVER_NAME" '
      $0 ~ /^\[ODBC Drivers\]/ && !inserted {
        print
        print driver "=Installed"
        inserted=1
        next
      }
      { print }
    ' "$SYSTEM_ODBCINST_FILE" > "${SYSTEM_ODBCINST_FILE}.tmp" && mv "${SYSTEM_ODBCINST_FILE}.tmp" "$SYSTEM_ODBCINST_FILE"
  fi
else
  # Section doesn't exist, append both section and driver entry at end
  {
    echo ""
    echo "[ODBC Drivers]"
    echo "${DRIVER_NAME}=Installed"
  } >>"$SYSTEM_ODBCINST_FILE"
fi
