]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/install/files/install-sh
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / install / files / install-sh
1 #!/bin/sh
2 # install - install a program, script, or datafile
3
4 scriptversion=2004-10-22.00
5
6 # This originates from X11R5 (mit/util/scripts/install.sh), which was
7 # later released in X11R6 (xc/config/util/install.sh) with the
8 # following copyright and license.
9 #
10 # Copyright (C) 1994 X Consortium
11 #
12 # Permission is hereby granted, free of charge, to any person obtaining a copy
13 # of this software and associated documentation files (the "Software"), to
14 # deal in the Software without restriction, including without limitation the
15 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 # sell copies of the Software, and to permit persons to whom the Software is
17 # furnished to do so, subject to the following conditions:
18 #
19 # The above copyright notice and this permission notice shall be included in
20 # all copies or substantial portions of the Software.
21 #
22 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25 # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27 # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #
29 # Except as contained in this notice, the name of the X Consortium shall not
30 # be used in advertising or otherwise to promote the sale, use or other deal-
31 # ings in this Software without prior written authorization from the X Consor-
32 # tium.
33 #
34 #
35 # FSF changes to this file are in the public domain.
36 #
37 # Calling this script install-sh is preferred over install.sh, to prevent
38 # `make' implicit rules from creating a file called install from it
39 # when there is no Makefile.
40 #
41 # This script is compatible with the BSD install script, but was written
42 # from scratch.  It can only install one file at a time, a restriction
43 # shared with many OS's install programs.
44
45 # set DOITPROG to echo to test this script
46
47 # Don't use :- since 4.3BSD and earlier shells don't like it.
48 doit="${DOITPROG-}"
49
50 # put in absolute paths if you don't have them in your path; or use env. vars.
51
52 mvprog="${MVPROG-mv}"
53 cpprog="${CPPROG-cp}"
54 chmodprog="${CHMODPROG-chmod}"
55 chownprog="${CHOWNPROG-chown}"
56 chgrpprog="${CHGRPPROG-chgrp}"
57 stripprog="${STRIPPROG-strip}"
58 rmprog="${RMPROG-rm}"
59 mkdirprog="${MKDIRPROG-mkdir}"
60
61 chmodcmd="$chmodprog 0755"
62 chowncmd=
63 chgrpcmd=
64 stripcmd=
65 rmcmd="$rmprog -f"
66 mvcmd="$mvprog"
67 src=
68 dst=
69 dir_arg=
70 preserve_arg=
71 dstarg=
72 no_target_directory=
73
74 usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
75    or: $0 [OPTION]... SRCFILES... DIRECTORY
76    or: $0 [OPTION]... -t DIRECTORY SRCFILES...
77    or: $0 [OPTION]... -d DIRECTORIES...
78
79 In the 1st form, copy SRCFILE to DSTFILE.
80 In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
81 In the 4th, create DIRECTORIES.
82
83 Options:
84 -c         (ignored)
85 -D         (ignored)
86 -d         create directories instead of installing files.
87 -g GROUP   $chgrpprog installed files to GROUP.
88 -m MODE    $chmodprog installed files to MODE.
89 -o USER    $chownprog installed files to USER.
90 -p         apply access/modification times of SRCFILE files
91            to corresponding DSTFILE files
92 -s         $stripprog installed files.
93 -t DIRECTORY  install into DIRECTORY.
94 -T         report an error if DSTFILE is a directory.
95 --help     display this help and exit.
96 --version  display version info and exit.
97
98 Environment variables override the default commands:
99   CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
100 "
101
102 while test -n "$1"; do
103   case $1 in
104     -c) shift
105         continue;;
106
107     -D) shift
108         continue;;
109
110     -d) dir_arg=true
111         shift
112         continue;;
113
114     -g) chgrpcmd="$chgrpprog $2"
115         shift
116         shift
117         continue;;
118
119     --help) echo "$usage"; exit 0;;
120
121     -m) chmodcmd="$chmodprog $2"
122         shift
123         shift
124         continue;;
125
126     -o) chowncmd="$chownprog $2"
127         shift
128         shift
129         continue;;
130
131     -p) preserve_arg="-p"
132         shift
133         continue;;
134
135     -s) stripcmd=$stripprog
136         shift
137         continue;;
138
139     -t) dstarg=$2
140         shift
141         shift
142         continue;;
143
144     -T) no_target_directory=true
145         shift
146         continue;;
147
148     --version) echo "$0 $scriptversion"; exit 0;;
149
150     *)  # When -d is used, all remaining arguments are directories to create.
151         # When -t is used, the destination is already specified.
152         test -n "$dir_arg$dstarg" && break
153         # Otherwise, the last argument is the destination.  Remove it from $@.
154         for arg
155         do
156           if test -n "$dstarg"; then
157             # $@ is not empty: it contains at least $arg.
158             set fnord "$@" "$dstarg"
159             shift # fnord
160           fi
161           shift # arg
162           dstarg=$arg
163         done
164         break;;
165   esac
166 done
167
168 if test -z "$1"; then
169   if test -z "$dir_arg"; then
170     echo "$0: no input file specified." >&2
171     exit 1
172   fi
173   # It's OK to call `install-sh -d' without argument.
174   # This can happen when creating conditional directories.
175   exit 0
176 fi
177
178 for src
179 do
180   # Protect names starting with `-'.
181   case $src in
182     -*) src=./$src ;;
183   esac
184
185   if test -n "$dir_arg"; then
186     dst=$src
187     src=
188
189     if test -d "$dst"; then
190       mkdircmd=:
191       chmodcmd=
192     else
193       mkdircmd=$mkdirprog
194     fi
195   else
196     # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
197     # might cause directories to be created, which would be especially bad
198     # if $src (and thus $dsttmp) contains '*'.
199     if test ! -f "$src" && test ! -d "$src"; then
200       echo "$0: $src does not exist." >&2
201       exit 1
202     fi
203
204     if test -z "$dstarg"; then
205       echo "$0: no destination specified." >&2
206       exit 1
207     fi
208
209     dst=$dstarg
210     # Protect names starting with `-'.
211     case $dst in
212       -*) dst=./$dst ;;
213     esac
214
215     # If destination is a directory, append the input filename; won't work
216     # if double slashes aren't ignored.
217     if test -d "$dst"; then
218       if test -n "$no_target_directory"; then
219         echo "$0: $dstarg: Is a directory" >&2
220         exit 1
221       fi
222       dst=$dst/`basename "$src"`
223     fi
224   fi
225
226   # This sed command emulates the dirname command.
227   dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
228
229   # Make sure that the destination directory exists.
230
231   # Skip lots of stat calls in the usual case.
232   if test ! -d "$dstdir"; then
233     defaultIFS='
234          '
235     IFS="${IFS-$defaultIFS}"
236
237     oIFS=$IFS
238     # Some sh's can't handle IFS=/ for some reason.
239     IFS='%'
240     set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
241     shift
242     IFS=$oIFS
243
244     pathcomp=
245
246     while test $# -ne 0 ; do
247       pathcomp=$pathcomp$1
248       shift
249       if test ! -d "$pathcomp"; then
250         $mkdirprog "$pathcomp"
251         # mkdir can fail with a `File exist' error in case several
252         # install-sh are creating the directory concurrently.  This
253         # is OK.
254         test -d "$pathcomp" || exit
255       fi
256       pathcomp=$pathcomp/
257     done
258   fi
259
260   if test -n "$dir_arg"; then
261     $doit $mkdircmd "$dst" \
262       && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
263       && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
264       && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
265       && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
266
267   else
268     dstfile=`basename "$dst"`
269
270     # Make a couple of temp file names in the proper directory.
271     dsttmp=$dstdir/_inst.$$_
272     rmtmp=$dstdir/_rm.$$_
273
274     # Trap to clean up those temp files at exit.
275     trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
276     trap '(exit $?); exit' 1 2 13 15
277
278     # Copy the file name to the temp name.
279     $doit $cpprog $preserve_arg "$src" "$dsttmp" &&
280
281     # and set any options; do chmod last to preserve setuid bits.
282     #
283     # If any of these fail, we abort the whole thing.  If we want to
284     # ignore errors from any of these, just make sure not to ignore
285     # errors from the above "$doit $cpprog $src $dsttmp" command.
286     #
287     { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
288       && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
289       && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
290       && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
291
292     # Now rename the file to the real destination.
293     { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
294       || {
295            # The rename failed, perhaps because mv can't rename something else
296            # to itself, or perhaps because mv is so ancient that it does not
297            # support -f.
298
299            # Now remove or move aside any old file at destination location.
300            # We try this two ways since rm can't unlink itself on some
301            # systems and the destination file might be busy for other
302            # reasons.  In this case, the final cleanup might fail but the new
303            # file should still install successfully.
304            {
305              if test -f "$dstdir/$dstfile"; then
306                $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
307                || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
308                || {
309                  echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
310                  (exit 1); exit
311                }
312              else
313                :
314              fi
315            } &&
316
317            # Now rename the file to the real destination.
318            $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
319          }
320     }
321   fi || { (exit 1); exit; }
322 done
323
324 # The final little trick to "correctly" pass the exit status to the exit trap.
325 {
326   (exit 0); exit
327 }
328
329 # Local variables:
330 # eval: (add-hook 'write-file-hooks 'time-stamp)
331 # time-stamp-start: "scriptversion="
332 # time-stamp-format: "%:y-%02m-%02d.%02H"
333 # time-stamp-end: "$"
334 # End: