Debian Codegraph @

install-sh

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