summaryrefslogtreecommitdiff
path: root/libinstall/template.sh
blob: 35b79b6203761c9776e698f05e2c096f26bd7889 (plain)
    1 #!/usr/bin/env bash
    2 # Pkgself builds self-extracting installers
    3 # Copyright (C) 2018  Aaron Ball <nullspoon@oper.io>
    4 #
    5 # This program is free software: you can redistribute it and/or modify
    6 # it under the terms of the GNU General Public License as published by
    7 # the Free Software Foundation, either version 3 of the License, or
    8 # (at your option) any later version.
    9 #
   10 # This program is distributed in the hope that it will be useful,
   11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 # GNU General Public License for more details.
   14 #
   15 # You should have received a copy of the GNU General Public License
   16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
   17 
   18 template() {
   19   # Disable trace output in case it is enabled
   20   #set +x
   21   local _src="${1:-}"
   22   local _dest="${2:-}"
   23   local varname # Name of the variable, without double braces
   24   local delim=$'\001'
   25 
   26   local oldifs=${IFS}
   27   export IFS=$'\n'
   28 
   29   # Create the list of all variables referenced in the file
   30   local _vars=($(grep -o -h '{{ [a-Z0-9_\-]\+ }}' ${_src} | sort | uniq))
   31 
   32   printf "Parsing template %s -> %s\n" "${_src}" "${_dest}"
   33   install -D "${_src}" "${_dest}"
   34 
   35   for _var in ${_vars[@]}; do
   36     _varref="$(printf ${_var} | cut -d ' ' -f 2)"
   37     if [ -z "${!_varref:-}" ]; then
   38       printf "Template %s variable '%s' is not set. Skipping.\n" \
   39         "$(basename ${_src})" \
   40         "${_varref}"
   41       continue
   42     fi
   43 
   44     sed -i "s${delim}{{ ${_varref} }}${delim}${!_varref}${delim}g" "${_dest}"
   45   done
   46 
   47   export IFS=${oldifs}
   48   #set -x
   49 }

Generated by cgit