#! /bin/bash

# This file is part of t8code.
# t8code is a C library to manage a collection (a forest) of multiple
# connected adaptive space-trees of general element classes in parallel.
#
# Copyright (C) 2015 the developers
#
# t8code is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# t8code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with t8code; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


# This is the GNU style for indent 2.2.9 (according to man page)
#
#"$INDENT" \
#    -nbad -bap -nbc -bbo -bl -bli2 -bls -ncdb -nce -cp1 -cs -di2 \
#    -ndj -nfc1 -nfca -hnl -i2 -ip5 -lp -pcs -nprs -psl -saf -sai \
#    -saw -nsc -nsob
#    "$@"

# This is our modification
#
# blank line after procedure body
# braces indent 0 (or do you want -bl2 here and -bl below?)
# braces to right of control statements (or do you want -bl here?)
# no tabs
# put the return type of a function on a separate line
# swallow optional blank lines
INDENT_BASE_OPTIONS="-bap -bli0 -br -nut -psl -sob -di20"

# Indent has problems with user defined data types
# and some other data types (i.e. size_t).
# We can tell indent manually which datatypes we defined and use.
# For t8code these datatypes are listed in scripts/t8indent_custom_datatypes.txt
# If you encounter an indenting problem with a (user defined)
# type, add it to the file.
#
# Example without -T t8_cmesh_t:
#   static	t8_cmesh_t
#   afunction (int aparameter) {
#
# Note the undesired whitespaces between 'static' and 't8_cmesh_t'.
#
# Example with -T t8_cmesh_t:
#   static t8_cmesh_t
#   afunction (int aparameter) {

# Get the path of the file t8indent_custom_datatypes.txt in the
# /scripts subfolder of the repo.

repo_main_dir=`git rev-parse --show-toplevel`
additional_data_types_file="${repo_main_dir}/scripts/t8indent_custom_datatypes.txt"


if [ ! -f $additional_data_types_file ]
then
  echo Error: File $additional_data_types_file does not exist. >&2
  echo Exiting because of error.
  exit 1
fi

T8CODE_ADDITIONAL_DATATYPES=
for x in `cat ${additional_data_types_file}`
do
  T8CODE_ADDITIONAL_DATATYPES="$T8CODE_ADDITIONAL_DATATYPES -T $x"
done

INDENT_OPTIONS="$INDENT_BASE_OPTIONS $T8CODE_ADDITIONAL_DATATYPES"

INDENT=`which gnuindent 2> /dev/null`
if test -z "$INDENT" ; then
	INDENT=`which gindent`
fi
if test -z "$INDENT" ; then
	INDENT=`which indent`
fi

for arg in "$@" ; do
  if [ "x$arg" == "x-o" ]; then
    WANTSOUT=1
  fi
done
if [ -z "$WANTSOUT" ]; then
  for NAME in "$@" ; do
    $INDENT $INDENT_OPTIONS "$NAME"
  done
else
  $INDENT $INDENT_OPTIONS $@
fi
