#!/bin/sh
# $Id: wfindent.tmpl 251 2018-03-04 12:37:08Z willem_vermin $
# vim: filetype=sh
#
# in-place indenting of fortran sources using findent
# example:
#   wfindent -i4 *.f90
# 
# A check is made if the output file has the same number of lines
# as the input file.
# Only if this check succeeds, the file is overwritten, 
# otherwize an error message is printed.
#

# the location of findent, replace with correct location:
FINDENT=${FINDENT:-"/usr/bin/findent"}
if ! "$FINDENT" -v >/dev/null 2>&1 ; then
   FINDENT="findent"   # try if findent is in PATH
   if ! "$FINDENT" -v >/dev/null 2>&1 ; then
      echo "$0: findent not found, exiting"
      exit 1
   fi
fi
echo "$0 using: "`"$FINDENT" -v`
parms=$( getopt -o a:b:c:C:d:e:E:f:F:hHi:I:j:k:l:L:m:o:qr:R:s:t:vw:x: \
   -l version                                     \
   -l help                                        \
   -l manpage                                     \
   -l indent:                                     \
   -l indent_associate:,indent-associate:         \
   -l indent_block:,indent-block:                 \
   -l indent_case:,indent-case:                   \
   -l indent_contains:,indent-contains:           \
   -l indent_do:,indent-do:                       \
   -l indent_entry:,indent-entry:                 \
   -l indent_enum:,indent-enum:                   \
   -l indent_if:,indent-if:                       \
   -l indent_forall:,indent-forall:               \
   -l indent_interface:,indent-interface:         \
   -l indent_continuation:,indent-continuation:   \
   -l indent_procedure:,indent-procedure:         \
   -l indent_select:,indent-select:               \
   -l indent_type:,indent-type:                   \
   -l indent_where:,indent-where:                 \
   -l indent_critical:,indent-critical:           \
   -l input_format:,input-format:                 \
   -l start_indent:,start-indent:                 \
   -l label_left:,label-left:                     \
   -l input_line_length:,input-line-length:       \
   -l indent_module:,indent-module                \
   -l output_format:,output-format:               \
   -l refactor_procedures::,refactor-procedures:: \
   -- "$@" )

if [ $? -ne 0 ] ; then
   exit 1
fi
eval set -- "$parms"
fparms=""
while [ "$1" ] ; do
   case "$1" in
      --) shift
	 break
	 ;;
      "-v"|"--version")
	 echo "wfindent using $FINDENT"
	 "$FINDENT" -v
	 echo "wfindent: indented files: 0"
	 exit $?
	 ;;
      "-h"|"-H"|"--help"|"--manpage") 
	 "$FINDENT" "$1"
	 exit $?
	 ;;
      --refactor*)
	 fparms="$fparms $1" 
	 shift
	 if [ "$1" ] ; then
	    fparms="$fparms=$1"
	 fi
	 shift
	 ;;
      *)
	 fparms="$fparms $1" 
	 shift
	 ;;
   esac
done
tmp=`mktemp`
n=0
while [ "$1" ] ; do
   if [ -e "$1" ] ; then
      norig=`wc -l < "$1"`
      # Check if file ends with newline. If not: correct number of lines
      lastchar="$(tail -c1 "$1" | od -a -An | tr -d ' ')"
      if [ "$lastchar" != "nl" ] ; then
	 norig=`expr $norig + 1`
      fi
      cat "$1" | "$FINDENT" $fparms > $tmp
      nnew=`wc -l < $tmp`
      if [ $norig -eq $nnew ] ; then
	 cp $tmp "$1"
	 n=`expr $n + 1`
      else
	 echo "***** wfindent: error while converting $1, conversion abandoned"
      fi
   fi
   shift
done
echo "wfindent: indented files: $n"
rm $tmp
