#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
#     Copyright (C) 2011-2016 OpenFOAM Foundation
#     Copyright (C) 2018-2019 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM 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 3 of the License, or
#     (at your option) any later version.
#
#     OpenFOAM 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 OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
#
# Script
#     foamUpdateCaseFileHeader
#
# Description
#     Updates the header of application files and removes consecutive
#     blank lines.
#
#     Uses the API value by default, but the version can also be specified
#     with the -version option.
#
#------------------------------------------------------------------------------
usage() {
    exec 1>&2
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat<<USAGE

Usage: ${0##*/} [OPTION] <file1> ... <fileN>

options:
  -version=VER      Specifies version for header (default: $FOAM_API)
  -h | -help        Print the usage

Updates the header of application files and removes consecutive blank lines.
By default, writes current OpenFOAM API number version in the header.
An alternative version can be specified with the -version option.

USAGE
    exit 1
}

#------------------------------------------------------------------------------

# parse options
while [ "$#" -gt 0 ]
do
    case "$1" in
    -h | -help*)
        usage
        ;;
    -version=*)
        version="${1#*=}"
        ;;
    -v | -version)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        version="$2"
        shift
        ;;
    -*)
        usage "unknown option: '$1'"
        ;;
    *)
        break
        ;;
    esac
    shift
done

[ $# -ge 1 ] || usage

#------------------------------------------------------------------------------

# Constant width for version.
# Default to v{FOAM_API}, project version or OPENFOAM

if [ -z "$version" ]
then
    if [ -n "$FOAM_API" ]
    then
        version="v$FOAM_API"
    else
        version="$WM_PROJECT_VERSION"
    fi
fi

version=$(printf %-38s "${version:-OPENFOAM}")

#------------------------------------------------------------------------------

printBanner()
{
    cat<<BANNER
/*--------------------------------*- C++ -*----------------------------------*\\
| =========                 |                                                 |
| \\\\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |
|  \\\\    /   O peration     | Version:  $version|
|   \\\\  /    A nd           | Website:  www.openfoam.com                      |
|    \\\\/     M anipulation  |                                                 |
\\*---------------------------------------------------------------------------*/
BANNER
}


printFoamFile()
{
echo "FoamFile"
echo "{"
echo "    version     2.0;"
echo "    format      $1;"
echo "    class       $2;"
echo "    object      $3;"
echo "}"
}


printFoamFileM4()
{
echo "FoamFile"
echo "{"
echo "    version     2.0;"
echo "    \`format'      $1;"
echo "    class       $2;"
echo "    object      $3;"
echo "}"
}



#
# Extract attribute '$1' from file '$2'
#
FoamFileAttribute()
{
    sed -n -e 's/[ ;]*$//' -e "s/^ *$1 *//p" "$2"
}


#
# Extract attribute `format' (m4) from file '$1'
#
FoamFileFormatM4()
{
    sed -n -e 's/[ ;]*$//' -e "s/^ *.format' *//p" "$1"
}


#
# Main
#

tmpFile="FoamFile.tmp$$"
for caseFile
do
    if grep -q FoamFile "$caseFile" 2>/dev/null
    then
        echo "Updating case file: $caseFile"
        sed -n '/FoamFile/,/}/p' "$caseFile" > "$tmpFile"

        format=$(FoamFileAttribute format "$tmpFile")
        class=$(FoamFileAttribute  class  "$tmpFile")
        object=$(FoamFileAttribute object "$tmpFile")
        # extract note? - needs special handling
        unset m4format note

        if [ -n "$format" ]
        then
            printBanner > "$tmpFile"
            printFoamFile "$format" "$class" "$object" >> "$tmpFile"
        else
            # No format? Could be an m4 file with `format'
            format=$(FoamFileFormatM4 "$tmpFile")

            if [ -n "$format" ]
            then
                printBanner > "$tmpFile"
                printFoamFileM4 "$format" "$class" "$object" >> "$tmpFile"
            else
                echo "Missing format: $caseFile" 1>&2
                continue
            fi
        fi

        sed '1,/}/d' "$caseFile" | sed '/./,/^$/!d' >> "$tmpFile"

        # Use cat to avoid removing/replace soft-links
        [ -s "$tmpFile" ] && cat "$tmpFile" >| "$caseFile"
        rm -f "$tmpFile"

        if [ "$format" = binary ]
        then
            echo "Changed binary file?  $caseFile" 1>&2
        fi
    else
        echo "Invalid case file: $caseFile" 1>&2
    fi
done

#------------------------------------------------------------------------------
