]> git.ipfire.org Git - thirdparty/cups.git/blob - install-sh
Merge changes from CUPS 1.5svn-r9385.
[thirdparty/cups.git] / install-sh
1 #!/bin/sh
2 #
3 # "$Id$"
4 #
5 # Install a program, script, or datafile.
6 #
7 # Copyright 2008-2009 by Apple Inc.
8 #
9 # This script is not compatible with BSD (or any other) install program, as it
10 # allows owner and group changes to fail with a warning and makes sure that the
11 # destination directory permissions are as specified - BSD install and the
12 # original X11 install script did not change permissions of existing
13 # directories. It also does not support the transform options since CUPS does
14 # not use them...
15 #
16 # Original script from X11R5 (mit/util/scripts/install.sh)
17 # Copyright 1991 by the Massachusetts Institute of Technology
18 #
19 # Permission to use, copy, modify, distribute, and sell this software and its
20 # documentation for any purpose is hereby granted without fee, provided that
21 # the above copyright notice appear in all copies and that both that
22 # copyright notice and this permission notice appear in supporting
23 # documentation, and that the name of M.I.T. not be used in advertising or
24 # publicity pertaining to distribution of the software without specific,
25 # written prior permission. M.I.T. makes no representations about the
26 # suitability of this software for any purpose. It is provided "as is"
27 # without express or implied warranty.
28 #
29 # Calling this script install-sh is preferred over install.sh, to prevent
30 # `make' implicit rules from creating a file called install from it
31 # when there is no Makefile.
32
33 # set DOITPROG to echo to test this script
34 # Don't use :- since 4.3BSD and earlier shells don't like it.
35 doit="${DOITPROG-}"
36
37 # Force umask to 022...
38 umask 022
39
40 # put in absolute paths if you don't have them in your path; or use env. vars.
41 mvprog="${MVPROG-mv}"
42 cpprog="${CPPROG-cp}"
43 chmodprog="${CHMODPROG-chmod}"
44 chownprog="${CHOWNPROG-chown}"
45 chgrpprog="${CHGRPPROG-chgrp}"
46 stripprog="${STRIPPROG-strip}"
47 rmprog="${RMPROG-rm}"
48 mkdirprog="${MKDIRPROG-mkdir}"
49
50 transformbasename=""
51 transform_arg=""
52 instcmd="$mvprog"
53 chmodcmd="$chmodprog 0755"
54 chowncmd=""
55 chgrpcmd=""
56 stripcmd=""
57 rmcmd="$rmprog -f"
58 mvcmd="$mvprog"
59 src=""
60 dst=""
61 dir_arg=""
62
63 while [ x"$1" != x ]; do
64 case $1 in
65 -c)
66 instcmd="$cpprog"
67 shift
68 continue
69 ;;
70
71 -d)
72 dir_arg=true
73 shift
74 continue
75 ;;
76
77 -m)
78 chmodcmd="$chmodprog $2"
79 shift
80 shift
81 continue
82 ;;
83
84 -o)
85 chowncmd="$chownprog $2"
86 shift
87 shift
88 continue
89 ;;
90
91 -g)
92 chgrpcmd="$chgrpprog $2"
93 shift
94 shift
95 continue
96 ;;
97
98 -s)
99 stripcmd="$stripprog"
100 shift
101 continue
102 ;;
103
104 *)
105 if [ x"$src" = x ]; then
106 src="$1"
107 else
108 dst="$1"
109 fi
110 shift
111 continue
112 ;;
113 esac
114 done
115
116 if [ x"$src" = x ]; then
117 echo "install-sh: No input file specified"
118 exit 1
119 fi
120
121 if [ x"$dir_arg" != x ]; then
122 dst="$src"
123 src=""
124
125 if [ -d "$dst" ]; then
126 instcmd=:
127 else
128 instcmd=$mkdirprog
129 fi
130 else
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 if [ ! -f "$src" -a ! -d "$src" ]; then
135 echo "install: $src does not exist"
136 exit 1
137 fi
138
139 if [ x"$dst" = x ]; then
140 echo "install: No destination specified"
141 exit 1
142 fi
143
144 # If destination is a directory, append the input filename.
145 if [ -d "$dst" ]; then
146 dst="$dst/`basename $src`"
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}" ]; then $doit $mkdirprog "${pathcomp}"; fi
175
176 pathcomp="${pathcomp}/"
177 done
178 fi
179
180 if [ x"$dir_arg" != x ]; then
181 # Make a directory...
182 $doit $instcmd $dst || exit 1
183
184 # Allow chown/chgrp to fail, but log a warning
185 if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst || echo "warning: Unable to change owner of $dst!"; fi
186 if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst || echo "warning: Unable to change group of $dst!"; fi
187 if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst || exit 1; fi
188 else
189 # Install a file...
190 dstfile="`basename $dst`"
191
192 # Check the destination file - for libraries just use the "-x" option
193 # to strip...
194 case "$dstfile" in
195 *.a | *.dylib | *.sl | *.sl.* | *.so | *.so.*)
196 stripopt="-x"
197 ;;
198 *)
199 stripopt=""
200 ;;
201 esac
202
203 # Make a temp file name in the proper directory.
204 dsttmp="$dstdir/#inst.$$#"
205
206 # Move or copy the file name to the temp name
207 $doit $instcmd $src $dsttmp || exit 1
208
209 # Update permissions and strip as needed, then move to the final name.
210 # If the chmod, strip, rm, or mv commands fail, remove the installed
211 # file...
212 if [ x"$stripcmd" != x ]; then $doit $stripcmd $stripopt "$dsttmp" || echo "warning: Unable to strip $dst!"; fi
213 if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp" || echo "warning: Unable to change owner of $dst!"; fi
214 if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp" || echo "warning: Unable to change group of $dst!"; fi
215
216 trap "rm -f ${dsttmp}" 0 &&
217 if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; fi &&
218 $doit $rmcmd -f "$dstdir/$dstfile" &&
219 $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
220 fi
221
222 exit 0