#!/bin/sh -e # # 2020 Dennis Camera (dennis.camera@ssrq-sds-fds.ch) # # This file is part of cdist. # # cdist is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # cdist is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with cdist. If not, see . # grep_line() { echo "$2" | grep -qxF "$1"; } unquote_lines() { sed -e '/^".*"$/{s/^"//;s/"$//}' \ -e '/'"^'.*'"'$/{s/'"^'"'//;s/'"'$"'//}' } append_values() { while read -r _value do set -- "$@" --value "${_value}" done unset _value "$@" &2 exit 1 } type_is=$(cat "${__object:?}/explorer/type") type_should=$(cat "${__object:?}/parameter/type") if test -f "${__object:?}/parameter/option" then optnames_should=$( sed -e 's/=.*$//' "${__object:?}/parameter/option" | sort -u) fi if test -n "${type_is}" then if test "${type_is}" != "${type_should##*.}" then # Check if section type matches (section exists and --type provided) printf 'Section type "%s" does not match --type "%s".\n' \ "${type_is}" "${type_should}" >&2 exit 1 fi sect_type=${type_is} else sect_type=${type_should##*.} fi if test -z "${section}" then # No section exists and --match was used. # So we generate a new section identifier from $__object_id. case ${__object_id:?} in (*.*) section=${__object_id:?} ;; (*) section="${type_should%%.*}.${__object_id:?}" ;; esac fi # Make sure the section itself is present __uci "${section}" --state present --transaction "${transaction_name}" \ --value "${sect_type}" export require=__uci/"${section}" # Delete options not in "should" sed -e 's/=.*$//;s/^.*\.//' "${__object:?}/explorer/options" \ | while read -r optname do if ! grep_line "${optname}" "${optnames_should}" then __uci "${section}.${optname}" --state absent \ --transaction "${transaction_name}" &2 exit 1 ;; esac