#!/bin/bash
## ------------------------------------------------------------------------
##
## SPDX-License-Identifier: LGPL-2.1-or-later
## Copyright (C) 2018 - 2023 by the deal.II authors
##
## This file is part of the deal.II library.
##
## Part of the source code is dual licensed under Apache-2.0 WITH
## LLVM-exception OR LGPL-2.1-or-later. Detailed license information
## governing the source code and code contributions can be found in
## LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
##
## ------------------------------------------------------------------------

#
# This script downloads and installs the clang-format binary. The
# destination directory is
#   [contrib/utilities]/programs/clang-<VERSION>/bin.
#
# This script only works on Linux (amd64) and macOS. For other
# architectures it is necessary to compile the clang-format binary by hand.
# This can be done with the compile_clang_format script.
#

VERSION=16
PRG="$(cd "$(dirname "$0")" && pwd)/programs"
CLANG_PATH="${PRG}/clang-${VERSION}"

URL="https://github.com/dealii/dealii/releases/download/v9.5.1"

# Find out which kind of OS we are running and set the appropriate settings
case "${OSTYPE}" in
  linux*)
    FILENAME="clang-format-${VERSION}-linux.tar.gz"
    CHECKSUM_CMD="sha256sum"
    CHECKSUM="e6d92ab1b385f5e4392466a3cf651a9e403a5c212f4c1c3737ee173bc6d79d93  $FILENAME"
    ;;
  darwin*)
    CHECKSUM_CMD="shasum"
    ARCH=$(uname -m)
    if [[ "$ARCH" == "arm64" ]]; then
        FILENAME="clang-format-${VERSION}-darwin-arm.tar.gz"
        CHECKSUM="8b1733414dec5a91e15060f482bebeb0723ac8e8f3115241c1ea595302758b46  $FILENAME"
    else
        FILENAME="clang-format-${VERSION}-darwin-intel.tar.gz"
        CHECKSUM="edff4341fb3b9e25c670902227538be6348d3b91ff88aa676ff8e7bb589b2ae5  $FILENAME"
    fi
    ;;
  *)
    echo "unknown: ${OSTYPE}"
    exit 1
    ;;
esac

if [ ! -d "${PRG}" ]
then
    echo "create folder ${PRG}"
    mkdir "${PRG}"
fi

if [ -d "${CLANG_PATH}" ]
then
    echo "${CLANG_PATH}  exists. Exiting."
    exit 1
fi

echo "Downloading and installing clang-format-${VERSION} from ${URL}/${FILENAME}"
mkdir "${CLANG_PATH}"

tmpdir="${TMPDIR:-/tmp}/dealiiclang${RANDOM}${RANDOM}"
mkdir -p "${tmpdir}"
cd "${tmpdir}"
if [ -x "$(command -v wget)" ]; then
  echo "Using wget to download..."
  # set progress option if available
  wget --help | grep -q '\--show-progress' && \
        _PROGRESS_OPT="--show-progress" || _PROGRESS_OPT=""

  wget -q $_PROGRESS_OPT -L "${URL}/${FILENAME}" > /dev/null
else
  if [ -x "$(command -v curl)" ]; then
    echo "Using curl to download..."
    curl --progress-bar -L "${URL}/${FILENAME}" -O > /dev/null
  else
    echo "Error: Neither wget nor curl is available..."
    exit 1
  fi
fi

if echo "${CHECKSUM}" | "${CHECKSUM_CMD}" -c; then
  tar xfz "${FILENAME}" -C "${PRG}" > /dev/null
else
  echo "*** The downloaded file has the wrong SHA256 checksum!"
  exit 1
fi
rm -r "${tmpdir}"

echo "All done. clang-format successfully installed into"
echo "    ${CLANG_PATH}/bin"
