#!/bin/bash

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-2.0-only
#

failed_count=0

# Execute all mwan scripts
for f in $(find /usr/libexec/mwan-hooks/ -type f -executable)
do
    # Execute the script and capture the output:
    # avoid unwanted output if the script is terminated with a SIGHUP
    output=$($f 2>&1)
    exit_code=$?
    if [[ $exit_code -gt 0 && $exit_code != 129 ]]; then
        ((failed_count++))
	logger -t mwan3-hook "Script $f returned $exit_code: $output"
    fi
done

exit $failed_count
