# Copyright 2024 SPC-EPFL
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------
## Code below included in the eqdsk_to_database and database_to_eqdsk scripts.
# A standalone copy is included here for debugging / testing


echo -n "Original (${#}) arguments: "
echo $@

# Grab override variables and add to temp file, which will later be added to the namelist file, and later deleted.
# override statements should be before and/or after the standard list, but not in the middle
non_override_seen=0
override_file=$(mktemp)
for var in "$@"
do
    [ ${var:0:11} == "--override_" ] && {
        # strip "--override_" from front of var and add to file
        echo ${var#--override_} >> ${override_file}
        # shift, such that the remaining $1/... logic later still works.
        if [ ${1:0:11} == "--override_" ]; then
           shift # note will not shift anymore once all first overrides have been scanned
        fi
    } || {
        # Mark the first time we see an arg not starting with --override_
        (( non_override_seen++ ))
    }
done

echo -n "Final (${non_override_seen}) arguments:    "
echo $@
