#!/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 . # state_should=$(cat "${__object}/parameter/state") case $state_should in (present) if cmp -s "${__object}/explorer/aliases" "${__object}/parameter/alias" then # all good! exit 0 fi if test -s "${__object}/explorer/aliases" then echo "update aliases" >>"$__messages_out" else echo "add aliases" >>"$__messages_out" fi mode=1 ;; (absent) # nothing to do if no aliases found. test -s "${__object}/explorer/aliases" || exit 0 echo "delete aliases" >>"$__messages_out" mode=0 ;; (*) printf 'Invalid --state: %s.\n' "$state_should" >&2 printf 'Acceptable values are: present, absent.\n' >&2 exit 1 esac aliases_file=$(cat "${__object}/explorer/aliases_file") test -n "${aliases_file}" || { echo 'Could not determine aliases file path.' >&2 exit 1 } # "export" variables to remote printf 'mode=%u\n' "${mode}" printf "aliases_file='%s'\n" "${aliases_file}" cat <<'EOF' test -f "${aliases_file}" || touch "${aliases_file}" awk -F ':[ \t]*' -v mode=$mode ' function sepafter(f, def, _) { # finds the separator between field $f and $(f+1) _ = substr($0, length($f)+1, index(substr($0, length($f)+1), $(f+1))-1) return _ ? _ : def } function write_aliases() { if (aliases_written) return # print aliases line printf "%s%s", ENVIRON["__object_id"], sepafter(1, ": ") while ((getline < aliases_should_file) > 0) { if (aliases_written) printf ", " printf "%s", $0 aliases_written = 1 } printf "\n" close(aliases_should_file) } BEGIN { aliases_should_file = (ENVIRON["__object"] "/parameter/alias") } /^[ \t]*\#/ { # comment line (leave alone) select = 0; cont = 0 # comments terminate alias lists and continuations print next } { # is this line a continuation line? # (the prev. line ended in a backslash or the line starts with whitespace) is_cont = /^[ \t]/ || cont # detect if the line is a line to be continued (ends with a backslash) cont = ($0 ~ /\\$/) # if it is, we drop the backslash from the line. if (cont) sub(/[ \t]*\\$/, "", $0) } is_cont { # we ignore the line as it has been rewritten previously or is not # interesting next } $1 == ENVIRON["__object_id"] { # "target" user -> rewrite aliases list select = 1 if (mode) write_aliases() next } { # other user select = 0 print } END { # if the last line was an alias, the separator will be reused (looks better) if (mode && !aliases_written) write_aliases() } ' <"${aliases_file}" >"${aliases_file}.tmp" || { rm -f "${aliases_file}.tmp" echo 'Generating new aliases file failed!' >&2 exit 1 } if ! cmp -s "${aliases_file}" "${aliases_file}.tmp" then # aliases file was modified, replace and run `newaliases`. mv "${aliases_file}.tmp" "${aliases_file}" # run newaliases if present command -v newaliases >/dev/null 2>&1 && newaliases || true else # no modifications were made, delete the temp file. rm "${aliases_file}.tmp" fi EOF