I don't care about systems where atimes are broken.
[apps/madmutt.git] / install-sh
1 #!/bin/sh
2 # install - install a program, script, or datafile
3 # This originally came from X11R5 (mit/util/scripts/install.sh).
4
5 scriptversion=2003-01-17.15
6
7 # Copyright 1991 by the Massachusetts Institute of Technology
8 # (FSF changes in the public domain.)
9 #
10 # Permission to use, copy, modify, distribute, and sell this software and its
11 # documentation for any purpose is hereby granted without fee, provided that
12 # the above copyright notice appear in all copies and that both that
13 # copyright notice and this permission notice appear in supporting
14 # documentation, and that the name of M.I.T. not be used in advertising or
15 # publicity pertaining to distribution of the software without specific,
16 # written prior permission.  M.I.T. makes no representations about the
17 # suitability of this software for any purpose.  It is provided "as is"
18 # without express or implied warranty.
19 #
20 # Calling this script install-sh is preferred over install.sh, to prevent
21 # `make' implicit rules from creating a file called install from it
22 # when there is no Makefile.
23 #
24 # This script is compatible with the BSD install script, but was written
25 # from scratch.  It can only install one file at a time, a restriction
26 # shared with many OS's install programs.
27
28 # set DOITPROG to echo to test this script
29
30 # Don't use :- since 4.3BSD and earlier shells don't like it.
31 doit="${DOITPROG-}"
32
33 # put in absolute paths if you don't have them in your path; or use env. vars.
34
35 mvprog="${MVPROG-mv}"
36 cpprog="${CPPROG-cp}"
37 chmodprog="${CHMODPROG-chmod}"
38 chownprog="${CHOWNPROG-chown}"
39 chgrpprog="${CHGRPPROG-chgrp}"
40 stripprog="${STRIPPROG-strip}"
41 rmprog="${RMPROG-rm}"
42 mkdirprog="${MKDIRPROG-mkdir}"
43
44 transformbasename=
45 transform_arg=
46 instcmd="$mvprog"
47 chmodcmd="$chmodprog 0755"
48 chowncmd=
49 chgrpcmd=
50 stripcmd=
51 rmcmd="$rmprog -f"
52 mvcmd="$mvprog"
53 src=
54 dst=
55 dir_arg=
56
57 usage="Usage: $0 [OPTION]... SRCFILE DSTFILE
58    or: $0 -d DIR1 DIR2...
59
60 In the first form, install SRCFILE to DSTFILE, removing SRCFILE by default.
61 In the second, create the directory path DIR.
62
63 Options:
64 -b=TRANSFORMBASENAME
65 -c         copy source (using $cpprog) instead of moving (using $mvprog).
66 -d         create directories instead of installing files.
67 -g GROUP   $chgrp installed files to GROUP.
68 -m MODE    $chmod installed files to MODE.
69 -o USER    $chown installed files to USER.
70 -s         strip installed files (using $stripprog).
71 -t=TRANSFORM
72 --help     display this help and exit.
73 --version  display version info and exit.
74
75 Environment variables override the default commands:
76   CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
77 "
78
79 while test -n "$1"; do
80   case $1 in
81     -b=*) transformbasename=`echo $1 | sed 's/-b=//'`
82         shift
83         continue;;
84
85     -c) instcmd=$cpprog
86         shift
87         continue;;
88
89     -d) dir_arg=true
90         shift
91         continue;;
92
93     -g) chgrpcmd="$chgrpprog $2"
94         shift
95         shift
96         continue;;
97
98     --help) echo "$usage"; exit 0;;
99
100     -m) chmodcmd="$chmodprog $2"
101         shift
102         shift
103         continue;;
104
105     -o) chowncmd="$chownprog $2"
106         shift
107         shift
108         continue;;
109
110     -s) stripcmd=$stripprog
111         shift
112         continue;;
113
114     -t=*) transformarg=`echo $1 | sed 's/-t=//'`
115         shift
116         continue;;
117
118     --version) echo "$0 $scriptversion"; exit 0;;
119
120     *)  if test -z "$src"; then
121           src=$1
122         else
123           # this colon is to work around a 386BSD /bin/sh bug
124           :
125           dst=$1
126         fi
127         shift
128         continue;;
129   esac
130 done
131
132 if test -z "$src"; then
133   echo "$0: no input file specified." >&2
134   exit 1
135 fi
136
137 if test -n "$dir_arg"; then
138   dst=$src
139   src=
140
141   if test -d "$dst"; then
142     instcmd=:
143     chmodcmd=
144   else
145     instcmd=$mkdirprog
146   fi
147 else
148   # Waiting for this to be detected by the "$instcmd $src $dsttmp" command
149   # might cause directories to be created, which would be especially bad
150   # if $src (and thus $dsttmp) contains '*'.
151   if test ! -f "$src" && test ! -d "$src"; then
152     echo "$0: $src does not exist." >&2
153     exit 1
154   fi
155
156   if test -z "$dst"; then
157     echo "$0: no destination specified." >&2
158     exit 1
159   fi
160
161   # If destination is a directory, append the input filename; won't work
162   # if double slashes aren't ignored.
163   if test -d "$dst"; then
164     dst=$dst/`basename "$src"`
165   fi
166 fi
167
168 ## this sed command emulates the dirname command
169 dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
170
171 # Make sure that the destination directory exists.
172 # (this part is taken from Noah Friedman's mkinstalldirs script.)
173
174 # Skip lots of stat calls in the usual case.
175 if test ! -d "$dstdir"; then
176   defaultIFS='
177         '
178   IFS="${IFS-$defaultIFS}"
179
180   oIFS=$IFS
181   # Some sh's can't handle IFS=/ for some reason.
182   IFS='%'
183   set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
184   IFS=$oIFS
185
186   pathcomp=
187
188   while test $# -ne 0 ; do
189     pathcomp=$pathcomp$1
190     shift
191     test -d "$pathcomp" || $mkdirprog "$pathcomp"
192     pathcomp=$pathcomp/
193   done
194 fi
195
196 if test -n "$dir_arg"; then
197   $doit $instcmd "$dst" \
198     && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
199     && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
200     && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
201     && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
202
203 else
204   # If we're going to rename the final executable, determine the name now.
205   if test -z "$transformarg"; then
206     dstfile=`basename "$dst"`
207   else
208     dstfile=`basename "$dst" $transformbasename \
209              | sed $transformarg`$transformbasename
210   fi
211
212   # don't allow the sed command to completely eliminate the filename.
213   test -z "$dstfile" && dstfile=`basename "$dst"`
214
215   # Make a couple of temp file names in the proper directory.
216   dsttmp=$dstdir/#inst.$$#
217   rmtmp=$dstdir/#rm.$$#
218
219   # Trap to clean up those temp files at exit.
220   trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
221   trap '(exit $?); exit' 1 2 13 15
222
223   # Move or copy the file name to the temp name
224   $doit $instcmd "$src" "$dsttmp" &&
225
226   # and set any options; do chmod last to preserve setuid bits.
227   #
228   # If any of these fail, we abort the whole thing.  If we want to
229   # ignore errors from any of these, just make sure not to ignore
230   # errors from the above "$doit $instcmd $src $dsttmp" command.
231   #
232   { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
233     && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
234     && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
235     && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
236
237   # Now remove or move aside any old file at destination location.  We
238   # try this two ways since rm can't unlink itself on some systems and
239   # the destination file might be busy for other reasons.  In this case,
240   # the final cleanup might fail but the new file should still install
241   # successfully.
242   {
243     if test -f "$dstdir/$dstfile"; then
244       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
245       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
246       || {
247           echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
248           (exit 1); exit
249       }
250     else
251       :
252     fi
253   } &&
254
255   # Now rename the file to the real destination.
256   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
257 fi &&
258
259 # The final little trick to "correctly" pass the exit status to the exit trap.
260 {
261   (exit 0); exit
262 }
263
264 # Local variables:
265 # eval: (add-hook 'write-file-hooks 'time-stamp)
266 # time-stamp-start: "scriptversion="
267 # time-stamp-format: "%:y-%02m-%02d.%02H"
268 # time-stamp-end: "$"
269 # End: