diff options
author | 2014-01-27 15:44:29 +0100 | |
---|---|---|
committer | 2014-01-27 15:44:29 +0100 | |
commit | 9d30cbf1007f192a943f88aaa7c1f51c11146e68 (patch) | |
tree | e0ef8e43fee6221173710d4e9e6f38907ee23b53 /bin | |
parent | move query_config code to roverlay/ (diff) | |
download | R_overlay-9d30cbf1007f192a943f88aaa7c1f51c11146e68.tar.gz R_overlay-9d30cbf1007f192a943f88aaa7c1f51c11146e68.tar.bz2 R_overlay-9d30cbf1007f192a943f88aaa7c1f51c11146e68.zip |
genfiles.sh helper script
Recursively converts template files from <src tree> to real files in <dest tree>
(using "query-config --from-file ... --outfile ...").
Diffstat (limited to 'bin')
l--------- | bin/genfiles.bash | 1 | ||||
-rwxr-xr-x | bin/invoke_script.bash | 21 | ||||
-rwxr-xr-x | bin/scripts/genfiles.sh | 93 |
3 files changed, 115 insertions, 0 deletions
diff --git a/bin/genfiles.bash b/bin/genfiles.bash new file mode 120000 index 0000000..9c90d9e --- /dev/null +++ b/bin/genfiles.bash @@ -0,0 +1 @@ +invoke_script.bash
\ No newline at end of file diff --git a/bin/invoke_script.bash b/bin/invoke_script.bash new file mode 100755 index 0000000..4a5b4dc --- /dev/null +++ b/bin/invoke_script.bash @@ -0,0 +1,21 @@ +#!/bin/bash +readonly SCRIPT="$(readlink -f "${BASH_SOURCE[0]?}")" +readonly SCRIPT_NAME="${BASH_SOURCE[0]##*/}" +readonly SCRIPT_DIR="${SCRIPT%/*}" + +readonly PRJROOT="${SCRIPT_DIR%/*}" +readonly REAL_SCRIPT="${SCRIPT_DIR}/scripts/${SCRIPT_NAME%.*}.sh" + +export ROVERLAY_PRJROOT="${PRJROOT}" +export X_RV_QUERY_CONFIG="${PRJROOT}/bin/query_config" + +cd "${PRJROOT}" || exit + +if [[ -x "${REAL_SCRIPT}" ]]; then + exec ${REAL_SCRIPT} "$@" +elif [[ -f "${REAL_SCRIPT}" ]]; then + exec ${SHELL:-sh} ${REAL_SCRIPT} "$@" +else + echo "script not found: ${REAL_SCRIPT}" 1>&2 + exit 9 +fi diff --git a/bin/scripts/genfiles.sh b/bin/scripts/genfiles.sh new file mode 100755 index 0000000..61ee3ab --- /dev/null +++ b/bin/scripts/genfiles.sh @@ -0,0 +1,93 @@ +#!/bin/sh -u +# Usage: genfiles.sh <src tree> <dest tree> [query-config args...] +# +# Converts template files from <src tree> to real files in <dest tree>. +# + +# die ( [message], [exit_code] ), raises exit() +die() { echo "${1:+died: }${1:-died.}" 1>&2; exit ${2:-2}; } +autodie() { "$@" || die "command '$*' returned ${?}." ${?}; } +OUT_OF_BOUNDS() { die "shift returned non-zero."; } +dodir() { [ -d "${1-}" ] || mkdir -p -- "${1}"; } + + +# genfile ( infile, destdir_relpath, *query_config_args ) +# +genfile() { + local infile destdir destfile fname + + infile="${1}" + [ -n "${2}" ] && destdir="${DEST_TREE%/}/${2}" || destdir="${DEST_TREE}" + fname="${1##*/}"; fname="${fname%.in}" + destfile="${destdir%/}/${fname}" + + shift 2 || OUT_OF_BOUNDS + + if [ "${infile}" = "${destfile}" ]; then + die "infile = outfile: ${infile}" + else + autodie dodir "${destdir}" + echo "creating ${destfile}" + if ${X_RV_QUERY_CONFIG} "$@" -f "${infile}" -O "${destfile}"; then + # chmod, ...? + true + else + echo "!!! failed to create '${destfile}' (rc=${?})" 1>&2 + fail=$(( ${fail?} + 1 )) + fi + fi +} + +# genfiles_recursive ( root, root_relpath, *query_config_args ) +# +genfiles_recursive() { + local root relpath item + + root="${1}" + relpath="${2#/}" + + shift 2 || OUT_OF_BOUNDS + + for item in "${root}/"*; do + if [ -d "${item}" ]; then + genfiles_recursive "${item}" "${relpath}/${item##*/}" "$@" + elif [ -f "${item}" ]; then + # converts file symlinks to files + genfile "${item}" "${relpath}" "$@" + else + echo "cannot handle '${item}'" 1>&2 + fi + done +} + +# --- + +: ${X_RV_QUERY_CONFIG:=/usr/bin/roverlay-query-config} + +case "${1-}" in + '-h'|'--help') + echo "Usage: genfiles.sh <src tree> <dest tree< [<query-config arg>...]" + exit 0 + ;; +esac + +[ -n "${1-}" ] || die "missing src tree arg." +[ -n "${2-}" ] || die "missing dest tree arg." + +SRC_TREE="$(readlink -f "${1}")" +DEST_TREE="$(readlink -f "${2}")" + +[ -n "${SRC_TREE}" ] && [ -d "${SRC_TREE}" ] || die "src tree does not exist." +[ -n "${DEST_TREE}" ] || die "invalid dest tree." + +shift 2 + +# globbing is essential, make sure that "noglob" is disabled +set +f +fail=0 +genfiles_recursive "${SRC_TREE}" "" "$@" +if [ ${fail} -gt 0 ]; then + exit 3 +else + exit 0 +fi |