#!/bin/bash
#
# Copyright 2018 Intel Corporation
#
# 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.
# ------------------------------------------------------------------------------

top_dir=$(cd $(dirname $(dirname $0)) && pwd)

dirs="
    $top_dir/
    $top_dir/examples/intkey_rust
    $top_dir/examples/xo_rust
"

exitcode=0

rustfmt --version

for dir in $dirs; do
    cd $dir
    echo "$dir"

    echo "-- rustfmt"

    diff=$(cargo fmt -- --check)
    rustfmt_exit=$?

    # rustfmt --check returns the following exit codes:
    #
    # 0 = No errors
    # 1 = Formatted code differs from existing code (check only)

    if [[ $rustfmt_exit != 0 ]]; then
        exitcode=1
        if [[ $rustfmt_exit == 1 ]]; then
          echo "Incorrect formatting: $dir (error code: $rustfmt_exit)"
          echo "$diff"
        else
            echo "rustfmt encountered an operational error"
        fi
    fi

done

exit $exitcode
