| 1 | #!/usr/bin/env bash |
| 2 | # SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | |
| 4 | # shellcheck disable=SC2181 |
| 5 | |
| 6 | # will stop the script for any error |
| 7 | set -e |
| 8 | |
| 9 | me="$0" |
| 10 | name="$1" |
| 11 | chart="$2" |
| 12 | conf="$3" |
| 13 | |
| 14 | can_diff=1 |
| 15 | |
| 16 | tmp1="$(mktemp)" |
| 17 | tmp2="$(mktemp)" |
| 18 | |
| 19 | myset() { |
| 20 | set | grep -v "^_=" | grep -v "^PIPESTATUS=" | grep -v "^BASH_LINENO=" |
| 21 | } |
| 22 | |
| 23 | # save 2 'set' |
| 24 | myset >"$tmp1" |
| 25 | myset >"$tmp2" |
| 26 | |
| 27 | # make sure they don't differ |
| 28 | diff "$tmp1" "$tmp2" >/dev/null 2>&1 |
| 29 | if [ $? -ne 0 ]; then |
| 30 | # they differ, we cannot do the check |
| 31 | echo >&2 "$me: cannot check with diff." |
| 32 | can_diff=0 |
| 33 | fi |
| 34 | |
| 35 | # do it again, now including the script |
| 36 | myset >"$tmp1" |
| 37 | |
| 38 | # include the plugin and its config |
| 39 | if [ -f "$conf" ]; then |
| 40 | # shellcheck source=/dev/null |
| 41 | . "$conf" |
| 42 | if [ $? -ne 0 ]; then |
| 43 | echo >&2 "$me: cannot load config file $conf" |
| 44 | rm "$tmp1" "$tmp2" |
| 45 | exit 1 |
| 46 | fi |
| 47 | fi |
| 48 | |
| 49 | # shellcheck source=/dev/null |
| 50 | . "$chart" |
| 51 | if [ $? -ne 0 ]; then |
| 52 | echo >&2 "$me: cannot load chart file $chart" |
| 53 | rm "$tmp1" "$tmp2" |
| 54 | exit 1 |
| 55 | fi |
| 56 | |
| 57 | # remove all variables starting with the plugin name |
| 58 | myset | grep -v "^$name" >"$tmp2" |
| 59 | |
| 60 | if [ $can_diff -eq 1 ]; then |
| 61 | # check if they are different |
| 62 | # make sure they don't differ |
| 63 | diff "$tmp1" "$tmp2" >&2 |
| 64 | if [ $? -ne 0 ]; then |
| 65 | # they differ |
| 66 | rm "$tmp1" "$tmp2" |
| 67 | exit 1 |
| 68 | fi |
| 69 | fi |
| 70 | |
| 71 | rm "$tmp1" "$tmp2" |
| 72 | exit 0 |