1 |
commit: 9d30cbf1007f192a943f88aaa7c1f51c11146e68 |
2 |
Author: André Erdmann <dywi <AT> mailerd <DOT> de> |
3 |
AuthorDate: Mon Jan 27 14:44:29 2014 +0000 |
4 |
Commit: André Erdmann <dywi <AT> mailerd <DOT> de> |
5 |
CommitDate: Mon Jan 27 14:44:29 2014 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=9d30cbf1 |
7 |
|
8 |
genfiles.sh helper script |
9 |
|
10 |
Recursively converts template files from <src tree> to real files in <dest tree> |
11 |
(using "query-config --from-file ... --outfile ..."). |
12 |
|
13 |
--- |
14 |
bin/genfiles.bash | 1 + |
15 |
bin/invoke_script.bash | 21 +++++++++++ |
16 |
bin/scripts/genfiles.sh | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ |
17 |
3 files changed, 115 insertions(+) |
18 |
|
19 |
diff --git a/bin/genfiles.bash b/bin/genfiles.bash |
20 |
new file mode 120000 |
21 |
index 0000000..9c90d9e |
22 |
--- /dev/null |
23 |
+++ b/bin/genfiles.bash |
24 |
@@ -0,0 +1 @@ |
25 |
+invoke_script.bash |
26 |
\ No newline at end of file |
27 |
|
28 |
diff --git a/bin/invoke_script.bash b/bin/invoke_script.bash |
29 |
new file mode 100755 |
30 |
index 0000000..4a5b4dc |
31 |
--- /dev/null |
32 |
+++ b/bin/invoke_script.bash |
33 |
@@ -0,0 +1,21 @@ |
34 |
+#!/bin/bash |
35 |
+readonly SCRIPT="$(readlink -f "${BASH_SOURCE[0]?}")" |
36 |
+readonly SCRIPT_NAME="${BASH_SOURCE[0]##*/}" |
37 |
+readonly SCRIPT_DIR="${SCRIPT%/*}" |
38 |
+ |
39 |
+readonly PRJROOT="${SCRIPT_DIR%/*}" |
40 |
+readonly REAL_SCRIPT="${SCRIPT_DIR}/scripts/${SCRIPT_NAME%.*}.sh" |
41 |
+ |
42 |
+export ROVERLAY_PRJROOT="${PRJROOT}" |
43 |
+export X_RV_QUERY_CONFIG="${PRJROOT}/bin/query_config" |
44 |
+ |
45 |
+cd "${PRJROOT}" || exit |
46 |
+ |
47 |
+if [[ -x "${REAL_SCRIPT}" ]]; then |
48 |
+ exec ${REAL_SCRIPT} "$@" |
49 |
+elif [[ -f "${REAL_SCRIPT}" ]]; then |
50 |
+ exec ${SHELL:-sh} ${REAL_SCRIPT} "$@" |
51 |
+else |
52 |
+ echo "script not found: ${REAL_SCRIPT}" 1>&2 |
53 |
+ exit 9 |
54 |
+fi |
55 |
|
56 |
diff --git a/bin/scripts/genfiles.sh b/bin/scripts/genfiles.sh |
57 |
new file mode 100755 |
58 |
index 0000000..61ee3ab |
59 |
--- /dev/null |
60 |
+++ b/bin/scripts/genfiles.sh |
61 |
@@ -0,0 +1,93 @@ |
62 |
+#!/bin/sh -u |
63 |
+# Usage: genfiles.sh <src tree> <dest tree> [query-config args...] |
64 |
+# |
65 |
+# Converts template files from <src tree> to real files in <dest tree>. |
66 |
+# |
67 |
+ |
68 |
+# die ( [message], [exit_code] ), raises exit() |
69 |
+die() { echo "${1:+died: }${1:-died.}" 1>&2; exit ${2:-2}; } |
70 |
+autodie() { "$@" || die "command '$*' returned ${?}." ${?}; } |
71 |
+OUT_OF_BOUNDS() { die "shift returned non-zero."; } |
72 |
+dodir() { [ -d "${1-}" ] || mkdir -p -- "${1}"; } |
73 |
+ |
74 |
+ |
75 |
+# genfile ( infile, destdir_relpath, *query_config_args ) |
76 |
+# |
77 |
+genfile() { |
78 |
+ local infile destdir destfile fname |
79 |
+ |
80 |
+ infile="${1}" |
81 |
+ [ -n "${2}" ] && destdir="${DEST_TREE%/}/${2}" || destdir="${DEST_TREE}" |
82 |
+ fname="${1##*/}"; fname="${fname%.in}" |
83 |
+ destfile="${destdir%/}/${fname}" |
84 |
+ |
85 |
+ shift 2 || OUT_OF_BOUNDS |
86 |
+ |
87 |
+ if [ "${infile}" = "${destfile}" ]; then |
88 |
+ die "infile = outfile: ${infile}" |
89 |
+ else |
90 |
+ autodie dodir "${destdir}" |
91 |
+ echo "creating ${destfile}" |
92 |
+ if ${X_RV_QUERY_CONFIG} "$@" -f "${infile}" -O "${destfile}"; then |
93 |
+ # chmod, ...? |
94 |
+ true |
95 |
+ else |
96 |
+ echo "!!! failed to create '${destfile}' (rc=${?})" 1>&2 |
97 |
+ fail=$(( ${fail?} + 1 )) |
98 |
+ fi |
99 |
+ fi |
100 |
+} |
101 |
+ |
102 |
+# genfiles_recursive ( root, root_relpath, *query_config_args ) |
103 |
+# |
104 |
+genfiles_recursive() { |
105 |
+ local root relpath item |
106 |
+ |
107 |
+ root="${1}" |
108 |
+ relpath="${2#/}" |
109 |
+ |
110 |
+ shift 2 || OUT_OF_BOUNDS |
111 |
+ |
112 |
+ for item in "${root}/"*; do |
113 |
+ if [ -d "${item}" ]; then |
114 |
+ genfiles_recursive "${item}" "${relpath}/${item##*/}" "$@" |
115 |
+ elif [ -f "${item}" ]; then |
116 |
+ # converts file symlinks to files |
117 |
+ genfile "${item}" "${relpath}" "$@" |
118 |
+ else |
119 |
+ echo "cannot handle '${item}'" 1>&2 |
120 |
+ fi |
121 |
+ done |
122 |
+} |
123 |
+ |
124 |
+# --- |
125 |
+ |
126 |
+: ${X_RV_QUERY_CONFIG:=/usr/bin/roverlay-query-config} |
127 |
+ |
128 |
+case "${1-}" in |
129 |
+ '-h'|'--help') |
130 |
+ echo "Usage: genfiles.sh <src tree> <dest tree< [<query-config arg>...]" |
131 |
+ exit 0 |
132 |
+ ;; |
133 |
+esac |
134 |
+ |
135 |
+[ -n "${1-}" ] || die "missing src tree arg." |
136 |
+[ -n "${2-}" ] || die "missing dest tree arg." |
137 |
+ |
138 |
+SRC_TREE="$(readlink -f "${1}")" |
139 |
+DEST_TREE="$(readlink -f "${2}")" |
140 |
+ |
141 |
+[ -n "${SRC_TREE}" ] && [ -d "${SRC_TREE}" ] || die "src tree does not exist." |
142 |
+[ -n "${DEST_TREE}" ] || die "invalid dest tree." |
143 |
+ |
144 |
+shift 2 |
145 |
+ |
146 |
+# globbing is essential, make sure that "noglob" is disabled |
147 |
+set +f |
148 |
+fail=0 |
149 |
+genfiles_recursive "${SRC_TREE}" "" "$@" |
150 |
+if [ ${fail} -gt 0 ]; then |
151 |
+ exit 3 |
152 |
+else |
153 |
+ exit 0 |
154 |
+fi |