]> diplodocus.org Git - nmh/blob - install-sh
Changed from returning void to returning int so that main()s who call done() at
[nmh] / install-sh
1 #! /bin/sh
2 #
3 # install -- install a program, script, or datafile
4 # This comes from X11R5.
5 #
6 # Calling this script install-sh is preferred over install.sh, to prevent
7 # `make' implicit rules from creating a file called install from it
8 # when there is no Makefile.
9 #
10 # This script is compatible with the BSD install script, but was written
11 # from scratch.
12 #
13
14 # set DOITPROG to echo to test this script
15
16 # Don't use :- since 4.3BSD and earlier shells don't like it.
17 doit="${DOITPROG-}"
18
19
20 # put in absolute paths if you don't have them in your path; or use env. vars.
21
22 mvprog="${MVPROG-mv}"
23 cpprog="${CPPROG-cp}"
24 chmodprog="${CHMODPROG-chmod}"
25 chownprog="${CHOWNPROG-chown}"
26 chgrpprog="${CHGRPPROG-chgrp}"
27 stripprog="${STRIPPROG-strip}"
28 rmprog="${RMPROG-rm}"
29 mkdirprog="${MKDIRPROG-mkdir}"
30
31 transformbasename=""
32 transform_arg=""
33 instcmd="$mvprog"
34 chmodcmd="$chmodprog 0755"
35 chowncmd=""
36 chgrpcmd=""
37 stripcmd=""
38 rmcmd="$rmprog -f"
39 mvcmd="$mvprog"
40 src=""
41 dst=""
42 dir_arg=""
43
44 while [ x"$1" != x ]; do
45 case $1 in
46 -c) instcmd="$cpprog"
47 shift
48 continue;;
49
50 -d) dir_arg=true
51 shift
52 continue;;
53
54 -m) chmodcmd="$chmodprog $2"
55 shift
56 shift
57 continue;;
58
59 -o) chowncmd="$chownprog $2"
60 shift
61 shift
62 continue;;
63
64 -g) chgrpcmd="$chgrpprog $2"
65 shift
66 shift
67 continue;;
68
69 -s) stripcmd="$stripprog"
70 shift
71 continue;;
72
73 -t=*) transformarg=`echo $1 | sed 's/-t=//'`
74 shift
75 continue;;
76
77 -b=*) transformbasename=`echo $1 | sed 's/-b=//'`
78 shift
79 continue;;
80
81 *) if [ x"$src" = x ]
82 then
83 src=$1
84 else
85 # this colon is to work around a 386BSD /bin/sh bug
86 :
87 dst=$1
88 fi
89 shift
90 continue;;
91 esac
92 done
93
94 if [ x"$src" = x ]
95 then
96 echo "install: no input file specified"
97 exit 1
98 else
99 true
100 fi
101
102 if [ x"$dir_arg" != x ]; then
103 dst=$src
104 src=""
105
106 if [ -d $dst ]; then
107 instcmd=:
108 else
109 instcmd=mkdir
110 fi
111 else
112
113 # Waiting for this to be detected by the "$instcmd $src $dsttmp" command
114 # might cause directories to be created, which would be especially bad
115 # if $src (and thus $dsttmp) contains '*'.
116
117 if [ -f $src -o -d $src ]
118 then
119 true
120 else
121 echo "install: $src does not exist"
122 exit 1
123 fi
124
125 if [ x"$dst" = x ]
126 then
127 echo "install: no destination specified"
128 exit 1
129 else
130 true
131 fi
132
133 # If destination is a directory, append the input filename; if your system
134 # does not like double slashes in filenames, you may need to add some logic
135
136 if [ -d $dst ]
137 then
138 dst="$dst"/`basename $src`
139 else
140 true
141 fi
142 fi
143
144 ## this sed command emulates the dirname command
145 dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
146
147 # Make sure that the destination directory exists.
148 # this part is taken from Noah Friedman's mkinstalldirs script
149
150 # Skip lots of stat calls in the usual case.
151 if [ ! -d "$dstdir" ]; then
152 defaultIFS='
153 '
154 IFS="${IFS-${defaultIFS}}"
155
156 oIFS="${IFS}"
157 # Some sh's can't handle IFS=/ for some reason.
158 IFS='%'
159 set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
160 IFS="${oIFS}"
161
162 pathcomp=''
163
164 while [ $# -ne 0 ] ; do
165 pathcomp="${pathcomp}${1}"
166 shift
167
168 if [ ! -d "${pathcomp}" ] ;
169 then
170 $mkdirprog "${pathcomp}"
171 else
172 true
173 fi
174
175 pathcomp="${pathcomp}/"
176 done
177 fi
178
179 if [ x"$dir_arg" != x ]
180 then
181 $doit $instcmd $dst &&
182
183 if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
184 if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
185 if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
186 if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
187 else
188
189 # If we're going to rename the final executable, determine the name now.
190
191 if [ x"$transformarg" = x ]
192 then
193 dstfile=`basename $dst`
194 else
195 dstfile=`basename $dst $transformbasename |
196 sed $transformarg`$transformbasename
197 fi
198
199 # don't allow the sed command to completely eliminate the filename
200
201 if [ x"$dstfile" = x ]
202 then
203 dstfile=`basename $dst`
204 else
205 true
206 fi
207
208 # Make a temp file name in the proper directory.
209
210 dsttmp=$dstdir/#inst.$$#
211
212 # Move or copy the file name to the temp name
213
214 $doit $instcmd $src $dsttmp &&
215
216 trap "rm -f ${dsttmp}" 0 &&
217
218 # and set any options; do chmod last to preserve setuid bits
219
220 # If any of these fail, we abort the whole thing. If we want to
221 # ignore errors from any of these, just make sure not to ignore
222 # errors from the above "$doit $instcmd $src $dsttmp" command.
223
224 if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
225 if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
226 if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
227 if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
228
229 # Now rename the file to the real destination.
230
231 $doit $rmcmd -f $dstdir/$dstfile &&
232 $doit $mvcmd $dsttmp $dstdir/$dstfile
233
234 fi &&
235
236
237 exit 0