| |
1 #!@l_prefix@/lib/openpkg/bash |
| |
2 ## |
| |
3 ## openpkg -- OpenPKG Tool Chain |
| |
4 ## Copyright (c) 2000-2007 OpenPKG Foundation e.V. <http://openpkg.net/> |
| |
5 ## Copyright (c) 2000-2007 Ralf S. Engelschall <http://engelschall.com/> |
| |
6 ## |
| |
7 ## Permission to use, copy, modify, and distribute this software for |
| |
8 ## any purpose with or without fee is hereby granted, provided that |
| |
9 ## the above copyright notice and this permission notice appear in all |
| |
10 ## copies. |
| |
11 ## |
| |
12 ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| |
13 ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| |
14 ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| |
15 ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR |
| |
16 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| |
17 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| |
18 ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| |
19 ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| |
20 ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| |
21 ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| |
22 ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| |
23 ## SUCH DAMAGE. |
| |
24 ## |
| |
25 ## openpkg.sh: Execution Frontend (Language: Bourne-Shell) |
| |
26 ## |
| |
27 |
| |
28 ## |
| |
29 ## command line parsing |
| |
30 ## |
| |
31 |
| |
32 # command line options defaults |
| |
33 opt_prefix="" |
| |
34 opt_tools="" |
| |
35 opt_version=no |
| |
36 opt_help=no |
| |
37 |
| |
38 # process command line options by iterating over arguments |
| |
39 for opt |
| |
40 do |
| |
41 case "${opt}" in |
| |
42 -*=*) arg=`echo "${opt}" | sed 's/^[-_a-zA-Z0-9]*=//'` ;; |
| |
43 *) arg='' ;; |
| |
44 esac |
| |
45 case "${opt}" in |
| |
46 -v|--version ) opt_version=yes; shift ;; |
| |
47 -h|--help ) opt_help=yes; shift ;; |
| |
48 --prefix=* ) opt_prefix="${arg}"; shift ;; |
| |
49 --tools=* ) opt_tools="${arg}"; shift ;; |
| |
50 -* ) echo "openpkg:ERROR: Invalid command-line option \"${opt}\"." 1>&2 |
| |
51 echo "openpkg:ERROR: Run \"${0} --help\" for list of valid options" 1>&2; exit 1 ;; |
| |
52 * ) break ;; |
| |
53 esac |
| |
54 done |
| |
55 |
| |
56 ## |
| |
57 ## determine OpenPKG locations |
| |
58 ## |
| |
59 |
| |
60 # determine path to OpenPKG instance |
| |
61 openpkg_prefix="@l_prefix@" |
| |
62 if [ ".${OPENPKG_PREFIX}" != . ]; then |
| |
63 openpkg_prefix="${OPENPKG_PREFIX}" |
| |
64 fi |
| |
65 if [ ".${opt_prefix}" != . ]; then |
| |
66 openpkg_prefix="${opt_prefix}" |
| |
67 fi |
| |
68 if [ -x "${openpkg_prefix}/bin/openpkg" -a -x "${openpkg_prefix}/libexec/openpkg/rpm" ]; then |
| |
69 # OpenPKG 2.0 and higher |
| |
70 true |
| |
71 elif [ -f "${openpkg_prefix}/bin/rpm" -a -x "${openpkg_prefix}/lib/openpkg/rpm" ]; then |
| |
72 # OpenPKG 1.x |
| |
73 echo "openpkg:ERROR: OpenPKG 1.x instance found under \"${openpkg_prefix}\" (not supported)" 1>&2 |
| |
74 exit 1 |
| |
75 else |
| |
76 echo "openpkg:ERROR: no OpenPKG instance found under \"${openpkg_prefix}\"" 1>&2 |
| |
77 exit 1 |
| |
78 fi |
| |
79 |
| |
80 # allow convenient all-in-one specification of OpenPKG Tool Chain locations |
| |
81 # (assuming the filesystem layout of an uninstalled OpenPKG Tool Chain) |
| |
82 openpkg_tools="${OPENPKG_TOOLS}" |
| |
83 openpkg_tools_cmdpath="${OPENPKG_TOOLS_CMDPATH}" |
| |
84 openpkg_tools_apipath="${OPENPKG_TOOLS_APIPATH}" |
| |
85 if [ ".${opt_tool}" != . ]; then |
| |
86 openpkg_tools="${opt_tools}" |
| |
87 fi |
| |
88 if [ ".${openpkg_tools}" != . -a ".${openpkg_tools_cmdpath}" = . ]; then |
| |
89 openpkg_tools_cmdpath="${openpkg_tools}/cmd:@" |
| |
90 fi |
| |
91 if [ ".${openpkg_tools}" != . -a ".${openpkg_tools_apipath}" = . ]; then |
| |
92 openpkg_tools_apipath="${openpkg_tools}/api:@" |
| |
93 fi |
| |
94 |
| |
95 # determine path to OpenPKG Tool Chain commands |
| |
96 cmdpath="${openpkg_prefix}/libexec/openpkg" |
| |
97 if [ -d "${openpkg_prefix}/libexec/openpkg-tools" ]; then |
| |
98 # openpkg-tools package overrides |
| |
99 cmdpath="${openpkg_prefix}/libexec/openpkg-tools:${cmdpath}" |
| |
100 fi |
| |
101 if [ -d "${openpkg_prefix}/libexec/openpkg-audit" ]; then |
| |
102 # openpkg-audit package overrides |
| |
103 cmdpath="${openpkg_prefix}/libexec/openpkg-audit:${cmdpath}" |
| |
104 fi |
| |
105 if [ ".${openpkg_tools_cmdpath}" != . ]; then |
| |
106 # user supplied path overrides |
| |
107 cmdpath=`echo "${openpkg_tools_cmdpath}" | sed -e "s;@;${cmdpath};"` |
| |
108 fi |
| |
109 openpkg_tools_cmdpath=`echo "${cmdpath}" | sed -e 's/::/:/g' -e 's/^://' -e 's/:$//'` |
| |
110 |
| |
111 # determine path to OpenPKG Tool Chain API |
| |
112 apipath="" |
| |
113 if [ -d "${openpkg_prefix}/lib/openpkg-tools" ]; then |
| |
114 # openpkg-tools package overrides |
| |
115 apipath="${openpkg_prefix}/lib/openpkg-tools:${apipath}" |
| |
116 fi |
| |
117 if [ ".${openpkg_tools_apipath}" != . ]; then |
| |
118 # user supplied path overrides |
| |
119 apipath=`echo "${openpkg_tools_apipath}" | sed -e "s;@;${apipath};"` |
| |
120 fi |
| |
121 openpkg_tools_apipath=`echo "${apipath}" | sed -e 's/::/:/g' -e 's/^://' -e 's/:$//'` |
| |
122 |
| |
123 ## |
| |
124 ## execute stand-alone option commands in advance |
| |
125 ## |
| |
126 |
| |
127 # implement stand-alone "--help" option |
| |
128 if [ ".${opt_help}" = .yes ]; then |
| |
129 release=`${openpkg_prefix}/libexec/openpkg/rpm --eval '%{l_openpkg_release}'` |
| |
130 echo "" |
| |
131 echo "This is ${release} <http://www.openpkg.org/>" |
| |
132 echo "Cross-Platform Unix Software Packaging Facility" |
| |
133 echo "" |
| |
134 echo "Copyright (c) 2000-2007 OpenPKG Foundation e.V. <http://openpkg.net/>" |
| |
135 echo "Copyright (c) 2000-2007 Ralf S. Engelschall <http://engelschall.com/>" |
| |
136 echo "" |
| |
137 echo "The command \"${openpkg_prefix}/bin/openpkg\" is the official command-line" |
| |
138 echo "execution frontend of the OpenPKG tool chain. Its usage is:" |
| |
139 echo "" |
| |
140 echo " \$ ${openpkg_prefix}/bin/openpkg [<option> ...] \\" |
| |
141 echo " <command> [<command-option> ...] [<command-argument> ...]" |
| |
142 echo "" |
| |
143 echo "where <option> is one of the following global options:" |
| |
144 echo " -p, --prefix sets the OpenPKG instance prefix (also: \${OPENPKG_PREFIX})" |
| |
145 echo " -t, --tools sets the OpenPKG tool chain prefix (also: \${OPENPKG_TOOLS})" |
| |
146 echo " -v, --version display OpenPKG version/release" |
| |
147 echo " -h, --help display this usage help message" |
| |
148 echo "" |
| |
149 echo "where <command> is one of the following commands:" |
| |
150 echo " rpm (provided by bootstrap package)" |
| |
151 echo " rpmbuild (provided by bootstrap package)" |
| |
152 echo " rpm2cpio (provided by bootstrap package)" |
| |
153 echo " rpm-config (provided by bootstrap package)" |
| |
154 echo " uuid (provided by bootstrap package)" |
| |
155 echo " rc (provided by bootstrap package)" |
| |
156 echo " lsync (provided by bootstrap package)" |
| |
157 echo " man (provided by bootstrap package)" |
| |
158 # install command intentionally left out in above display! |
| |
159 |
| |
160 # dynamically figure out add-on commands |
| |
161 for cmd in rpm rpmbuild rpm2cpio rpm-config uuid rc lsync man install; do |
| |
162 id=`echo "${cmd}" | sed -e 's/-/_/g'` |
| |
163 eval "__cmd_seen_${id}=yes" |
| |
164 done |
| |
165 OIFS="${IFS}"; IFS=":" |
| |
166 for dir in ${openpkg_tools_cmdpath}; do |
| |
167 IFS="${OIFS}" |
| |
168 if [ ! -d ${dir} ]; then |
| |
169 continue |
| |
170 fi |
| |
171 for cmd in `cd ${dir} 2>/dev/null && echo *`; do |
| |
172 name=`echo "${cmd}" | sed -e 's/\.sh$//' -e 's/\.pl$//' \ |
| |
173 -e 's/^/X/' -e 's/^X\([a-z][a-zA-Z0-9_-]*\)$/\1/' -e 's/^X.*$//'` |
| |
174 if [ ".${name}" != . ]; then |
| |
175 if [ -f ${dir}/${cmd} ]; then |
| |
176 id=`echo "${name}" | sed -e 's/-/_/g'` |
| |
177 eval "seen=\$__cmd_seen_${id}" |
| |
178 if [ ".${seen}" != .yes ]; then |
| |
179 echo "${name}" | awk '{ printf(" %-15s (provided by add-on package)\n", $0); }' |
| |
180 eval "__cmd_seen_${id}=yes" |
| |
181 fi |
| |
182 fi |
| |
183 fi |
| |
184 done |
| |
185 done |
| |
186 IFS="${OIFS}" |
| |
187 echo "" |
| |
188 echo "where <command-option> and <command-argument> are <command> specific" |
| |
189 echo "options and arguments. Run \"${openpkg_prefix}/bin/openpkg <command> --help\"" |
| |
190 echo "and \"${openpkg_prefix}/bin/openpkg man <command>\" for more details." |
| |
191 echo "" |
| |
192 exit 0 |
| |
193 fi |
| |
194 |
| |
195 # implement stand-alone "--version" option |
| |
196 if [ ".${opt_version}" = .yes ]; then |
| |
197 release=`${openpkg_prefix}/libexec/openpkg/rpm --eval '%{l_openpkg_release}'` |
| |
198 version=`${openpkg_prefix}/libexec/openpkg/rpm -q --qf '%{version}' openpkg` |
| |
199 echo "${release} (${version})" |
| |
200 exit 0 |
| |
201 fi |
| |
202 |
| |
203 ## |
| |
204 ## determine command details and execute command appropriately |
| |
205 ## |
| |
206 |
| |
207 # command line sanity check |
| |
208 if [ ${#} -eq 0 ]; then |
| |
209 echo "openpkg:ERROR: Invalid command-line arguments." 1>&2 |
| |
210 echo "openpkg:ERROR: Run \"${openpkg_prefix}/bin/openpkg --help\" for list of valid arguments." 1>&2 |
| |
211 exit 1 |
| |
212 fi |
| |
213 |
| |
214 # search command by iterating over all command directories |
| |
215 cmd="${1}" |
| |
216 shift |
| |
217 cmd_path="" |
| |
218 cmd_shell="" |
| |
219 cmd_stack="${OPENPKG_TOOLS_CMDSTACK}" |
| |
220 OIFS="${IFS}"; IFS=":" |
| |
221 for dir in ${openpkg_tools_cmdpath}; do |
| |
222 IFS="${OIFS}" |
| |
223 |
| |
224 # skip (currently) not existing directory |
| |
225 if [ ! -d ${dir} ]; then |
| |
226 continue |
| |
227 fi |
| |
228 |
| |
229 # check for various command implementations |
| |
230 if [ -x ${dir}/${cmd} ]; then |
| |
231 # found executable stand-alone binary |
| |
232 cmd_path="${dir}/${cmd}" |
| |
233 cmd_shell="" |
| |
234 elif [ -f ${dir}/${cmd}.sh ]; then |
| |
235 # found non-executable Bourne-Shell script |
| |
236 cmd_path="${dir}/${cmd}.sh" |
| |
237 cmd_shell="${openpkg_prefix}/lib/openpkg/bash" |
| |
238 elif [ -f ${dir}/${cmd}.pl ]; then |
| |
239 # found non-executable Perl script |
| |
240 cmd_path="${dir}/${cmd}.pl" |
| |
241 if [ -x ${openpkg_prefix}/bin/perl ]; then |
| |
242 cmd_shell="${openpkg_prefix}/bin/perl" |
| |
243 else |
| |
244 cmd_shell=`${openpkg_prefix}/lib/openpkg/shtool path -p "$PATH:$openpkg_prefix/lib/openpkg" -m perl 2>&1` |
| |
245 if [ ".${cmd_shell}" = . ]; then |
| |
246 echo "openpkg:ERROR: No Perl interpreter found in \${PATH}" 1>&2 |
| |
247 exit 1 |
| |
248 fi |
| |
249 fi |
| |
250 # provide Perl module include path(s) to API |
| |
251 OIFS="${IFS}"; IFS=":" |
| |
252 for dir2 in ${openpkg_tools_apipath}; do |
| |
253 IFS="${OIFS}" |
| |
254 if [ ! -d ${dir2} ]; then |
| |
255 continue |
| |
256 fi |
| |
257 cmd_shell="${cmd_shell} -I${dir2}" |
| |
258 done |
| |
259 IFS="${OIFS}" |
| |
260 else |
| |
261 # command not found, continue searching |
| |
262 continue |
| |
263 fi |
| |
264 |
| |
265 # check whether to use this found command or to continue searching |
| |
266 # for next command implementation in sequence (in order to support |
| |
267 # flexible nested command wrapping) |
| |
268 cmd_last=`echo "${cmd_stack}" | sed -e 's;:.*$;;'` |
| |
269 if [ ".${cmd_last}" = ".${cmd}" ]; then |
| |
270 # we were last command on stack, so pop us from call |
| |
271 # stack and continue searching for next implementation |
| |
272 cmd_stack=`echo "${cmd_stack}" | sed -e 's;^[^:][^:]*:*;;'` |
| |
273 continue |
| |
274 else |
| |
275 # last command on stack was different, so stop |
| |
276 # searching because we found the implementation |
| |
277 break |
| |
278 fi |
| |
279 done |
| |
280 IFS="${OIFS}" |
| |
281 |
| |
282 # sanity check search result |
| |
283 if [ ".${cmd_path}" = . ]; then |
| |
284 echo "openpkg:ERROR: No such command \"${cmd}\" found in command path" 1>&2 |
| |
285 echo "openpkg:ERROR: (${openpkg_tools_cmdpath})." 1>&2 |
| |
286 echo "openpkg:ERROR: Set \${OPENPKG_TOOLS_CMDPATH} appropriately." 1>&2 |
| |
287 echo "openpkg:ERROR: Run \"${openpkg_prefix}/bin/openpkg --help\" for list of valid commands." 1>&2 |
| |
288 exit 1 |
| |
289 fi |
| |
290 |
| |
291 # export essential run-time information to command |
| |
292 export OPENPKG_PREFIX="$openpkg_prefix" |
| |
293 export OPENPKG_TOOLS_CMDPROG="${0}" |
| |
294 export OPENPKG_TOOLS_CMDNAME="${cmd}" |
| |
295 export OPENPKG_TOOLS_CMDSTACK=`echo "${OPENPKG_TOOLS_CMDSTACK}" | sed -e 's;^\(.\);:\1;' -e "s;^;${cmd};"` |
| |
296 |
| |
297 # execute command |
| |
298 eval "exec ${cmd_shell} ${cmd_path} \${1+\"\$@\"}" |
| |
299 |