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