#!/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) 2022,2025 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# Script
#     foamCleanPolyMesh
#
# Description
#    Remove the contents of the constant/polyMesh directory
#    as per the Foam::polyMesh::removeFiles() method.
#
#------------------------------------------------------------------------------
usage() {
    exec 1>&2
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat <<USAGE

Usage: ${0##*/} [OPTION]
options:
  -case <dir>           case directory, default is the cwd
  -allRegions           all mesh regions
  -region <name>        mesh region
  -dry-run | -n         report actions but do not remove
  -help                 print the usage

Remove the contents of the constant/polyMesh directory as per the
Foam::polyMesh::removeFiles() method.

USAGE
   exit 1
}

#------------------------------------------------------------------------------
# Parse options
unset caseDir regionName optAllRegions optDryRun

while [ "$#" -gt 0 ]
do
    case "$1" in
    ('')  ;;                # Ignore empty option
    (--)  shift; break ;;   # Stop option parsing

    -h | -help*)
        usage
        ;;
    -dry-run | -n)
        optDryRun="(dry-run) "
        ;;

    -case=*)
        caseDir="${1#*=}"
        ;;

    -case)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        caseDir="$2"
        shift
        ;;

    -allRegions)
        optAllRegions=true
        unset optAllRegions
        ;;

    -region=*)
        regionName="${1#*=}"
        unset optAllRegions
        ;;

    -region)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        regionName="$2"
        unset optAllRegions
        shift
        ;;

    (*)
        usage "unknown option/argument: '$*'"
        ;;
    esac
    shift
done

if [ -n "$caseDir" ]
then
    cd "$caseDir" 2>/dev/null || usage "directory does not exist: '$caseDir'"
fi


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

# Remove files (mesh itself, modifiers, snappyHexMesh ones) and subdirectories
# also remove .gz versions of the same files

removeFiles()
{
    local directory="$1"

    for i in \
        points faces \
        owner neighbour \
        boundary \
        cells \
        cellZones faceZones pointZones \
        meshModifiers \
        parallelData \
        sets \
        cellLevel pointLevel \
        level0Edge \
        refinementHistory \
        surfaceIndex \
    ;
    do
        if [ -n "$optDryRun" ]
        then
            echo "${optDryRun} rm -rf $directory/{$i,$i.gz}"
        else
            rm -rf -- "$directory/$i" "$directory/$i.gz"
        fi
    done
}

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

if [ -n "$optAllRegions" ]
then
    # Discover probable regions
    for meshDir in constant/*/polyMesh
    do
        if [ -d "$meshDir" ]
        then
            removeFiles "$meshDir"
        fi
    done
    unset regionName
fi

meshDir="constant/${regionName}${regionName:+/}polyMesh"

if [ -d "$meshDir" ]
then
    # [OK] has constant/<region>/polyMesh
    :

elif [ -n "$optAllRegions" ]
then
    # No constant/polyMesh, but not an error with -allRegions
    exit 0

elif [ -n "$caseDir" ]
then
    # Specified -case, so no extra magic...
    echo "Error: no <$meshDir> in $caseDir" 1>&2
    exit 1

else
    # Try some other combinations
    other="${meshDir#constant/}"

    if [ -d "$other" ]
    then
        # Probably already within constant/
        meshDir="$other"
    elif [ "${PWD##*/}" = polyMesh ] && [ -z "$regionName" ]
    then
        # Apparently already within polyMesh/
        meshDir=.
    fi
fi


echo "Cleaning ${caseDir:-.}/$meshDir" 1>&2

removeFiles "$meshDir"

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