#!/bin/bash # Shared utility methods function printerr() { echo -e "\e[31m[ERROR]\e[0m $1" } function msg() { if [[ $VERBOSE -gt 0 ]]; then echo "$1" fi } function buildTemplate() { local CONFIG_FILE="" local SEPARATOR="ยง" declare -A ADDITIONALS while [[ "$#" -gt 0 ]] do case $1 in -f|--input-file) CONFIG_FILE="$2" ;; -t|--template) local SOURCE_FILE="$2" ;; -o|--output) local OUTPUT_FILE="$2" ;; -s|--separator) SEPARATOR="$2" ;; --add) arrIN=(${2//=/ }) ADDITIONALS[${arrIN[0]}]=${arrIN[1]} ;; esac shift done if [[ -z $CONFIG_FILE ]]; then printerr "Impossible to call ${FUNCNAME} without passing a config file file" exit 1 else source "$CONFIG_FILE" fi if [[ -z $OUTPUT_FILE ]]; then printerr "Impossible to call ${FUNCNAME} without passing an output file" exit 1 fi if [[ -z $SOURCE_FILE ]]; then printerr "Impossible to call ${FUNCNAME} without passing a template file" exit 1 fi TMPFILE=`mktemp` cp "$SOURCE_FILE" "$OUTPUT_FILE" for config_key in "${!CONFIG[@]}"; do cp "$OUTPUT_FILE" "$TMPFILE" config_value="${CONFIG[$config_key]}" cat "$TMPFILE" | sed -e "s~${SEPARATOR}${config_key}${SEPARATOR}~${config_value}~g" > "$OUTPUT_FILE" done for config_key in "${!ADDITIONALS[@]}"; do cp "$OUTPUT_FILE" "$TMPFILE" config_value="${ADDITIONALS[$config_key]}" cat "$TMPFILE" | sed -e "s~${SEPARATOR}${config_key}${SEPARATOR}~${config_value}~g" > "$OUTPUT_FILE" done }