Patch-ID: bash32-032
[bash.git] / support / install.sh
blob0cac004e6dce38456fc54e6e7dac614caa662e12
1 #!/bin/sh
3 # install - install a program, script, or datafile
4 # This comes from X11R5.
6 # $XConsortium: install.sh,v 1.2 89/12/18 14:47:22 jim Exp $
8 # Copyright 1991 by the Massachusetts Institute of Technology
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.
20 # This script is compatible with the BSD install script, but was written
21 # from scratch.
24 # set DOITPROG to echo to test this script
26 # Don't use :- since 4.3BSD and earlier shells don't like it.
27 doit="${DOITPROG-}"
30 # put in absolute paths if you don't have them in your path; or use env. vars.
32 mvprog="${MVPROG-mv}"
33 cpprog="${CPPROG-cp}"
34 chmodprog="${CHMODPROG-chmod}"
35 chownprog="${CHOWNPROG-chown}"
36 chgrpprog="${CHGRPPROG-chgrp}"
37 stripprog="${STRIPPROG-strip}"
38 rmprog="${RMPROG-rm}"
39 mkdirprog="${MKDIRPROG-mkdir}"
41 tranformbasename=""
42 transform_arg=""
43 instcmd="$mvprog"
44 chmodcmd="$chmodprog 0755"
45 chowncmd=""
46 chgrpcmd=""
47 stripcmd=""
48 rmcmd="$rmprog -f"
49 mvcmd="$mvprog"
50 src=""
51 dst=""
52 dir_arg=""
54 while [ x"$1" != x ]; do
55 case $1 in
56 -c) instcmd="$cpprog"
57 shift
58 continue;;
60 -d) dir_arg=true
61 shift
62 continue;;
64 -m) chmodcmd="$chmodprog $2"
65 shift
66 shift
67 continue;;
69 -o) chowncmd="$chownprog $2"
70 shift
71 shift
72 continue;;
74 -g) chgrpcmd="$chgrpprog $2"
75 shift
76 shift
77 continue;;
79 -s) stripcmd="$stripprog"
80 shift
81 continue;;
83 -t=*) transformarg=`echo $1 | sed 's/-t=//'`
84 shift
85 continue;;
87 -b=*) transformbasename=`echo $1 | sed 's/-b=//'`
88 shift
89 continue;;
91 *) if [ x"$src" = x ]
92 then
93 src=$1
94 else
95 # this colon is to work around a 386BSD /bin/sh bug
97 dst=$1
99 shift
100 continue;;
101 esac
102 done
104 if [ x"$src" = x ]
105 then
106 echo "install: no input file specified"
107 exit 1
108 else
109 true
112 if [ x"$dir_arg" != x ]; then
113 dst=$src
114 src=""
116 if [ -d $dst ]; then
117 instcmd=:
118 else
119 instcmd=mkdir
121 else
123 # Waiting for this to be detected by the "$instcmd $src $dsttmp" command
124 # might cause directories to be created, which would be especially bad
125 # if $src (and thus $dsttmp) contains '*'.
127 if [ -f $src -o -d $src ]
128 then
129 true
130 else
131 echo "install: $src does not exist"
132 exit 1
135 if [ x"$dst" = x ]
136 then
137 echo "install: no destination specified"
138 exit 1
139 else
140 true
143 # If destination is a directory, append the input filename; if your system
144 # does not like double slashes in filenames, you may need to add some logic
146 if [ -d $dst ]
147 then
148 dst="$dst"/`basename $src`
149 else
150 true
154 ## this sed command emulates the dirname command
155 dstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
157 # Make sure that the destination directory exists.
158 # this part is taken from Noah Friedman's mkinstalldirs script
160 # Skip lots of stat calls in the usual case.
161 if [ ! -d "$dstdir" ]; then
162 defaultIFS='
164 IFS="${IFS-${defaultIFS}}"
166 oIFS="${IFS}"
167 # Some sh's can't handle IFS=/ for some reason.
168 IFS='%'
169 set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
170 IFS="${oIFS}"
172 pathcomp=''
174 while [ $# -ne 0 ] ; do
175 pathcomp="${pathcomp}${1}"
176 shift
178 if [ ! -d "${pathcomp}" ] ;
179 then
180 $mkdirprog "${pathcomp}"
181 else
182 true
185 pathcomp="${pathcomp}/"
186 done
189 if [ x"$dir_arg" != x ]
190 then
191 $doit $instcmd $dst &&
193 if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
194 if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
195 if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
196 if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
197 else
199 # If we're going to rename the final executable, determine the name now.
201 if [ x"$transformarg" = x ]
202 then
203 dstfile=`basename $dst`
204 else
205 dstfile=`basename $dst $transformbasename |
206 sed $transformarg`$transformbasename
209 # don't allow the sed command to completely eliminate the filename
211 if [ x"$dstfile" = x ]
212 then
213 dstfile=`basename $dst`
214 else
215 true
218 # Make a temp file name in the proper directory.
220 dsttmp=$dstdir/#inst.$$#
222 # Move or copy the file name to the temp name
224 $doit $instcmd $src $dsttmp &&
226 trap "rm -f ${dsttmp}" 0 &&
228 # and set any options; do chmod last to preserve setuid bits
230 # If any of these fail, we abort the whole thing. If we want to
231 # ignore errors from any of these, just make sure not to ignore
232 # errors from the above "$doit $instcmd $src $dsttmp" command.
234 if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
235 if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
236 if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
237 if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
239 # Now rename the file to the real destination.
241 $doit $rmcmd -f $dstdir/$dstfile &&
242 $doit $mvcmd $dsttmp $dstdir/$dstfile
244 fi &&
247 exit 0