diff options
Diffstat (limited to 'libinstall/template.sh')
-rw-r--r-- | libinstall/template.sh | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/libinstall/template.sh b/libinstall/template.sh new file mode 100644 index 0000000..7254d8b --- /dev/null +++ b/libinstall/template.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +# Pkgself builds self-extracting installers +# Copyright (C) 2018 Aaron Ball <nullspoon@oper.io> +# +# This program 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. +# +# This program 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 this program. If not, see <https://www.gnu.org/licenses/>. + +template() { + # Disable trace output in case it is enabled + #set +x + local _src="${1:-}" + local _dest="${2:-}" + local varname # Name of the variable, without double braces + local delim=$'\001' + + local oldifs=${IFS} + export IFS=$'\n' + + # Create the list of all variables referenced in the file + local _vars=($(grep -o -h '{{ [a-Z0-9_\-]\+ }}' ${_src} | sort | uniq)) + + printf "Parsing template %s -> %s\n" "${_src}" "${_dest}" + install -D "${_src}" "${_dest}" + + for _var in ${_vars[@]}; do + printf "Parsing variable %s\n" "${var}" + _varref="$(printf ${_var} | cut -d ' ' -f 2)" + if [ -z "${!_varref:-}" ]; then + printf "Template %s variable '%s' is not set. Skipping.\n" \ + "$(basename ${_src})" \ + "${_varref}" + continue + fi + + echo sed -i "s${delim}{{ ${_varref} }}${delim}${!_varref}${delim}g" "${_dest}" + sed -i "s${delim}{{ ${_varref} }}${delim}${!_varref}${delim}g" "${_dest}" + done + + export IFS=${oldifs} + #set -x +} |