#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#-------------------------------------------------------------------------------
#     Copyright (C) 2015 OpenFOAM Foundation
#     Copyright (C) 2023 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# Script
#     foamCloneCase
#
# Description
#     Create a new case directory that includes time, system and constant
#     directories from a source case.
#     The time directory is the first time directory by default
#
# Requires
#     foamListTimes
#
#------------------------------------------------------------------------------
printHelp() {
    cat <<USAGE

Usage: ${0##*/} [OPTION] <sourceCase> <targetCase>
options:
  -force              Force overwrite of existing target
  -l | -latestTime    Select the latest time directory
  -h | -help          Print the usage

Create a new <targetCase> case directory with a copy of time, system, constant
directories from <sourceCase> directory.
The time directory is the first time directory by default.

USAGE
    exit 0  # A clean exit
}

# Report error and exit
die()
{
    exec 1>&2
    echo
    echo "Error encountered:"
    while [ "$#" -ge 1 ]; do echo "    $1"; shift; done
    echo
    echo "See '${0##*/} -help' for usage"
    echo
    exit 1
}


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

# Default: use the first time, combined with -withZero this will likely
# find the 0/ directory
filter="head -1"
unset optForce

# parse options
while [ "$#" -gt 0 ]
do
    case "$1" in
    (- | --)
        shift
        break   # Stop option parsing
        ;;
    (-h | -help* | --help*)
        printHelp
        ;;

    -force)
        optForce=true
        ;;

    -l | -latest*)
        ## Also possible: opt_foamListTimes="-latestTime"
        filter="tail -1"
        ;;
    -*)
        die "unknown option: '$*'"
        ;;
    *)
        break
        ;;
    esac
    shift
done


if [ "$#" -ne 2 ]
then
    die "Incorrect number of arguments specified"
fi

srcDir="$1"
dstDir="$2"

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

if [ -d "$srcDir" ]
then
    unset missing
    for dir in constant system
    do
        if [ ! -d "$srcDir/$dir" ]
        then
            missing="$missing${missing:+, }<$dir>"
        fi
    done
    if [ -n "$missing" ]
    then
        die \
            "Source directory is missing standard directories:" \
             " -> $srcDir" \
             "    $missing"
    fi
else
    die \
        "Source directory does not exist:" \
        " -> $srcDir"
fi

command -v foamListTimes || die "Requires 'foamListTimes' (openfoam)"

if [ -e "$dstDir" ]
then
    if [ "$optForce" = true ]
    then
        echo "------------" 1>&2
        echo "Overwriting: $dstDir" 1>&2
        echo "------------" 1>&2
        rm -rf "$dstDir"  # Remove old directory
    else
        die "Destination already exists, remove and re-run" \
            " -> $dstDir"
    fi
fi

if [ "$(foamListTimes -case "$srcDir" 2>&1 >/dev/null | grep 'FATAL ERROR')" ]
then
    die "'$srcDir' does not appear to be a valid OpenFOAM case"
fi


timeDir="$(foamListTimes -withZero -case "$srcDir" | $filter)"

# Fallback for missing timeDir
if [ -z "$timeDir" ] && [ -d "$srcDir/0.orig" ]
then
    timeDir="0.orig"
fi

echo "Copying case directories" 1>&2
echo "    $srcDir" 1>&2
echo " -> $dstDir" 1>&2
if [ -n "$timeDir" ]
then
    echo "    Time: $timeDir" 1>&2
else
    echo "    No time directories" 1>&2
fi
mkdir -p "$dstDir"
echo "----" 1>&2

for dir in system constant "$timeDir"
do
    if [ -n "$dir" ] && [ -d "$srcDir/$dir" ]
    then
        echo "    .../$dir" 1>&2
        cp -r "$srcDir/$dir" "$dstDir"
    fi
done
echo "----" 1>&2


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