#!/usr/bin/env bash # Pkgself builds self-extracting installers # Copyright (C) 2018 Aaron Ball # # 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 . 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 _varref="$(printf ${_var} | cut -d ' ' -f 2)" if [ -z "${!_varref+x}" ]; then printf "Template %s variable '%s' is not set. Skipping.\n" \ "$(basename ${_src})" \ "${_varref}" continue fi sed -i "s${delim}{{ ${_varref} }}${delim}${!_varref}${delim}g" "${_dest}" done export IFS=${oldifs} #set -x }