#!/bin/bash

# Copyright (c) 2010-2024, Lawrence Livermore National Security, LLC. Produced
# at the Lawrence Livermore National Laboratory. All Rights reserved. See files
# LICENSE and NOTICE for details. LLNL-CODE-806117.
#
# This file is part of the MFEM library. For more information and source code
# availability visit https://mfem.org.
#
# MFEM is free software; you can redistribute it and/or modify it under the
# terms of the BSD-3 license. We welcome feedback and contributions, see file
# CONTRIBUTING.md for details.

option=${1:-""}

if [[ "${option}" == "--help" ]]; then
  echo "This script runs checks on the repository."
  echo "It has 2 modes: with and without an option."
  echo ""
  echo "Options are used in GitHub Actions and can be:"
  echo "  --copyright"
  echo "  --license"
  echo "  --release"
  echo "  --math"
  echo "  --style"
  echo "  --history"
  echo ""
  echo "As a githook, the script is used without options."
  echo "In that case, it will run all the checks except style."
  echo ""
  echo "Use --help to print this help message."
fi

cd $(git rev-parse --show-toplevel)

# copyright check
copyright=true
if [[ "${option}" == "--copyright" || "${option}" == "" ]]; then
  if git grep -n "^\(#\|//\).*Copyright.*2010-20\(2[^4]\|[^2].\)" > matches.txt; then
    echo "Please update the following files to Copyright (c) 2010-2024:"
    cat matches.txt
    copyright=false
  fi
fi

# license check
license=true
if [[ "${option}" == "--license" || "${option}" == "" ]]; then
  if git grep -li "^\(#\|//\).*GNU\ Lesser\ General\ Public\ License" > matches.txt; then
    echo "Please update the following files to the BSD-3 license:"
    cat matches.txt
    license=false
  fi
fi

# release check
release=true
if [[ "${option}" == "--release" || "${option}" == "" ]]; then
  if git grep -l "^\(#\|//\).*LLNL\-CODE\-443211" > matches.txt
  then
    echo "Please update the following files to LLNL-CODE-806117:"
    cat matches.txt
    release=false
  fi
fi

# math in doxygen check
math=true
if [[ "${option}" == "--math" || "${option}" == "" ]]; then
  if grep '\\f' -R doc/CodeDocumentation.dox mfem.hpp config general linalg mesh fem examples miniapps | grep -v '\\frac' | grep -v fem/picojson.h | grep -v config/githooks/pre-push > matches.txt
  then
    echo "Please use $..$ and \$\$..\$\$ for LaTeX formulas in the following"
    echo "comments instead of the Doxygen style \f$..\f$, \f[..\f], etc."
    cat matches.txt
    math=false
  fi
fi

# wrap-up
code=0
if ! $copyright ; then
  echo "copyright check failed, unroll log for details"
  code=1
fi
if ! $license ; then
  echo "license check failed, unroll log for details"
  code=1
fi
if ! $release ; then
  echo "release check failed, unroll log for details"
  code=1
fi
if ! $math ; then
  echo "math in doxygen check failed, unroll log for details"
  code=1
fi

# `code-style` is not just a check, it will actually reformat the code if
# necessary. This means that if one pushes while the repo is in dirty state
# (changes not staged), those changes may be mixed with format changes.
# To activate this, you will need to hard-copy this hook script in the hook
# directory and uncomment only then. (See README.md)
#
## style check
#if [[ "${option}" == "--style" || "${option}" == "" ]]; then
if [[ "${option}" == "--style" ]]; then
  if which astyle && [[ "$(astyle --version)" == "Artistic Style Version 3.1" ]]; then
    cd tests/scripts
    if ! ./runtest code-style; then code=1; fi
    cd -
  else
    echo "Warning: astyle not found or version is not 3.1"
  fi
fi

# branch-history
if [[ "${option}" == "--history" || "${option}" == "" ]]; then
  git fetch origin master:master
  cd tests/scripts
  if ! ./runtest branch-history; then code=1; fi
  cd -
fi

exit $code
