#!/bin/bash
dosubst() {
    [[ $tpl =~ /etc/omix/([^/]+)/tpl ]] && vars=/etc/omix/vars/${BASH_REMATCH[1]} || return 0
    ([ -f "$vars" ] || return 0
    set -a; . $vars; set +a
    envsubst <$tpl >${tpl/tpl/conf}
)}
dogen() {
    [[ $tpl =~ /etc/omix/([^/]+)/script ]] && vars=/etc/omix/vars/${BASH_REMATCH[1]} || return 0
    ([ -f "$vars" ] || return 0
    set -a; . $vars; set +a
    $tpl >${tpl/script/conf}
)}
for tpl in $(find /etc/omix -path '/etc/omix/*/tpl/*'); do dosubst; done
for tpl in $(find /etc/omix -path '/etc/omix/*/script/*'); do dogen; done
