#!/bin/sh
# 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.
# If this check succeeds, the file is overwritten, 
# otherwize an error message is printed.

# find a suitable getopt: i.e. that returns 4 with 'getopt -T'
GETOPT=""
if [ "$WFINDENT_GETOPT" ]; then
   GETOPT="$WFINDENT_GETOPT"
else
   a=`getopt -T`
   if [ "$?" -eq 4 ] ; then
      GETOPT=getopt
   else
      tries="/usr/local/opt/gnu-getopt/bin/getopt /usr/local/bin/getopt /usr/local/bin/gnugetopt"
      for i in $tries ; do
         if [ -x "$i" ] ; then
            a=`"$i" -T`
            if [ "$?" -eq 4 ] ; then
               GETOPT="$i"
               break
            fi
         fi
      done
      if [ -z "$GETOPT" ] ; then
         echo "$0: No suitable getopt found. Tried:"
         echo " - getopt"
         for i in $tries ; do
            echo " - $i"
         done
         echo "if you have available a suitable gnu-compatible getopt, you can set"
         echo "the environment variable WFINDENT_GETOPT, like:"
         echo "export WFINDENT_GETOPT=\"$HOME/bin/gnu-getopt\""
         echo "No files indented."
         exit 1
      fi
   fi
fi

# 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`
if [ "$GETOPT" != getopt ] ; then
   echo "Using for getopt: $GETOPT"
fi

# left-outs are: -q, -lastindent, -lastusable, --deps
parms=$( "$GETOPT" -o a:b:c:C:d:e:E:f:F:hHi:I:j:k:l:L:m:M:o:r:R:s:t:vw:x: \
   -l help                                                \
   -l manpage                                             \
   -l readme                                              \
   -l version                                             \
   -l makefdeps                                           \
   -l vim_help               -l vim-help                  \
   -l vim_fortran            -l vim-fortran               \
   -l vim_findent            -l vim-findent               \
   -l gedit_help             -l gedit-help                \
   -l gedit_external         -l gedit-external            \
   -l gedit_plugin           -l gedit-plugin              \
   -l gedit_plugin_py        -l gedit-plugin-py           \
   -l emacs_help             -l emacs-help                \
   -l emacs_findent          -l emacs-findent             \
   -l continuation:                                       \
   -l include_left:          -l include-left:             \
   -l label_left:            -l label-left:               \
   -l input_format:          -l input-format:             \
   -l indent:                                             \
   -l input_line_length:     -l input-line-length:        \
   -l max_indent:            -l max-indent:               \
   -l output_format:         -l output-format:            \
   -l openmp:                                             \
   -l refactor_procedures::  -l refactor-procedures::     \
   -l start_indent:          -l start-indent:             \
   -l indent_associate:      -l indent-associate:         \
   -l indent_block:          -l indent-block:             \
   -l indent_do:             -l indent-do:                \
   -l indent_if:             -l indent-if:                \
   -l indent_enum:           -l indent-enum:              \
   -l indent_forall:         -l indent-forall:            \
   -l indent_interface:      -l indent-interface:         \
   -l indent_module:         -l indent-module:            \
   -l indent_procedure:      -l indent-procedure:         \
   -l indent_select:         -l indent-select:            \
   -l indent_type:           -l indent-type:              \
   -l indent_where:          -l indent-where:             \
   -l indent_critical:       -l indent-critical:          \
   -l indent_changeteam:     -l indent-changeteam:        \
   -l indent_contains:       -l indent-contains:          \
   -l indent_continuation:   -l indent-continuation:      \
   -l indent_case:           -l indent-case:              \
   -l indent_entry:          -l indent-entry:             \
   -- "$@" )

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
         exit $?
         ;;
      "-h"|"-H"|"--help"|"--manpage") 
         echo "wfindent - Indents and optionally converts Fortran program sources"
         echo
         echo "wfindent [OPTION]... [FILE]..."
         echo "examples:"
         echo "$ wfindent -i4 -Rr *.f90"
         echo "$ wfindent -i3 prog.f90 sub.f"
         echo
         echo "see below for possible options"
         echo "Note: these options can only be used with findent (not wfindent):"
         echo "   -q, --query_fix_free, -lastindent, --last_indent"
         echo "   -lastusable, --last_usable, --deps"
         echo
         "$FINDENT" "$1"
         exit $?
         ;;
      --readme|--makefdeps|--vim*|--gedit*|--emacs*)
         "$FINDENT" "$1"
         exit $?
         ;;
      --refactor*)    # special: optional argument must be preceded with '='
         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
