#!/bin/sh

#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the Common Development
# and Distribution License Version 1.0 (the "License").
#
# You can obtain a copy of the license at
# http://www.opensource.org/licenses/CDDL-1.0.  See the License for the
# specific language governing permissions and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each file and
# include the License file in a prominent location with the name LICENSE.CDDL.
# If applicable, add the following below this CDDL HEADER, with the fields
# enclosed by brackets "[]" replaced with your own identifying information:
#
# Portions Copyright (c) [yyyy] [name of copyright owner]. All rights reserved.
#
# CDDL HEADER END
#

#
# Copyright (c) 2013--2020, Regents of the University of Minnesota.
# All rights reserved.
#
# Contributors:
#    Ryan S. Elliott
#

#
# Release: This file is part of the kim-api.git repository.
#


# filter file list  # NOTE: keep in sync with verion in git-hooks/pre-commit
filter_file_list () {
  local file_list="$1"
  local ignore_file="`git rev-parse --show-toplevel`/.format-ignore"
  files_to_check_format=""

  for file in ${file_list}; do
    local keep=`printf ${file} | sed -e 's/^.*\.f90$//'`
    if test -f "${ignore_file}"; then
      if test `grep -c "^${file}$" "${ignore_file}"` -gt 0; then
        # ignore
        printf "File '${file}' is in format-ignore file: ignoring.\n"
        keep=no
      fi
    fi

    if test x"${keep}" = x""; then
      files_to_check_format="${files_to_check_format} ${file}"
    fi
  done
}

# goto the top level repo directory
if THE_GIT_DIR="`git rev-parse --show-toplevel 2> /dev/null`"; then
  cd "${THE_GIT_DIR}"
else
  printf "Not in the git repo.  Exiting...\n"
  exit 1
fi

files=`find .    -name "*.f90" |  \
       sed -e 's|^\./||'`

printf "Executing fprettify on all f90 files in directory ${PWD}.\n"

files_to_check_format=""
filter_file_list "${files}"

fprettify ${files_to_check_format}
