]> git.ipfire.org Git - thirdparty/cups.git/blame - install-sh
Fix cancel of jobs with multiple documents (<rdar://problem/24854834>)
[thirdparty/cups.git] / install-sh
CommitLineData
ef416fc2 1#!/bin/sh
2#
e07d4801 3# "$Id$"
ef416fc2 4#
e07d4801
MS
5# Install a program, script, or datafile.
6#
82cc1f9a 7# Copyright 2008-2012 by Apple Inc.
e07d4801
MS
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)
ef416fc2 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.
ef416fc2 32
33# set DOITPROG to echo to test this script
ef416fc2 34# Don't use :- since 4.3BSD and earlier shells don't like it.
35doit="${DOITPROG-}"
36
e07d4801
MS
37# Force umask to 022...
38umask 022
ef416fc2 39
40# put in absolute paths if you don't have them in your path; or use env. vars.
ef416fc2 41mvprog="${MVPROG-mv}"
42cpprog="${CPPROG-cp}"
43chmodprog="${CHMODPROG-chmod}"
44chownprog="${CHOWNPROG-chown}"
45chgrpprog="${CHGRPPROG-chgrp}"
46stripprog="${STRIPPROG-strip}"
47rmprog="${RMPROG-rm}"
48mkdirprog="${MKDIRPROG-mkdir}"
82cc1f9a 49gzipprog="${GZIPPROG-gzip}"
ef416fc2 50
51transformbasename=""
52transform_arg=""
53instcmd="$mvprog"
54chmodcmd="$chmodprog 0755"
55chowncmd=""
56chgrpcmd=""
57stripcmd=""
58rmcmd="$rmprog -f"
59mvcmd="$mvprog"
60src=""
61dst=""
62dir_arg=""
63
a29fd7dd 64gzipcp() {
82cc1f9a
MS
65 # gzipcp from to
66 $gzipprog -9 <"$1" >"$2"
67}
68
ef416fc2 69while [ x"$1" != x ]; do
e07d4801
MS
70 case $1 in
71 -c)
72 instcmd="$cpprog"
73 shift
74 continue
75 ;;
76
77 -d)
78 dir_arg=true
79 shift
80 continue
81 ;;
82
83 -m)
84 chmodcmd="$chmodprog $2"
85 shift
86 shift
87 continue
88 ;;
89
90 -o)
91 chowncmd="$chownprog $2"
92 shift
93 shift
94 continue
95 ;;
96
97 -g)
98 chgrpcmd="$chgrpprog $2"
99 shift
100 shift
101 continue
102 ;;
103
104 -s)
105 stripcmd="$stripprog"
106 shift
107 continue
108 ;;
ef416fc2 109
82cc1f9a
MS
110 -z)
111 instcmd="gzipcp"
112 shift
113 continue
114 ;;
115
e07d4801
MS
116 *)
117 if [ x"$src" = x ]; then
118 src="$1"
119 else
120 dst="$1"
121 fi
122 shift
123 continue
124 ;;
125 esac
ef416fc2 126done
127
e07d4801
MS
128if [ x"$src" = x ]; then
129 echo "install-sh: No input file specified"
ef416fc2 130 exit 1
ef416fc2 131fi
132
133if [ x"$dir_arg" != x ]; then
e07d4801 134 dst="$src"
ef416fc2 135 src=""
82cc1f9a 136
e07d4801 137 if [ -d "$dst" ]; then
ef416fc2 138 instcmd=:
ef416fc2 139 else
140 instcmd=$mkdirprog
141 fi
142else
e07d4801 143 # Waiting for this to be detected by the "$instcmd $src $dsttmp" command
82cc1f9a 144 # might cause directories to be created, which would be especially bad
e07d4801
MS
145 # if $src (and thus $dsttmp) contains '*'.
146 if [ ! -f "$src" -a ! -d "$src" ]; then
147 echo "install: $src does not exist"
ef416fc2 148 exit 1
149 fi
82cc1f9a 150
e07d4801
MS
151 if [ x"$dst" = x ]; then
152 echo "install: No destination specified"
ef416fc2 153 exit 1
ef416fc2 154 fi
155
e07d4801
MS
156 # If destination is a directory, append the input filename.
157 if [ -d "$dst" ]; then
158 dst="$dst/`basename $src`"
ef416fc2 159 fi
160fi
161
162## this sed command emulates the dirname command
e07d4801 163dstdir="`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`"
ef416fc2 164
165# Make sure that the destination directory exists.
e07d4801 166# This part is taken from Noah Friedman's mkinstalldirs script
ef416fc2 167
168# Skip lots of stat calls in the usual case.
169if [ ! -d "$dstdir" ]; then
e07d4801 170 defaultIFS='
ef416fc2 171 '
e07d4801 172 IFS="${IFS-${defaultIFS}}"
ef416fc2 173
e07d4801
MS
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}"
ef416fc2 179
e07d4801 180 pathcomp=''
ef416fc2 181
e07d4801
MS
182 while [ $# -ne 0 ] ; do
183 pathcomp="${pathcomp}${1}"
184 shift
ef416fc2 185
e07d4801 186 if [ ! -d "${pathcomp}" ]; then $doit $mkdirprog "${pathcomp}"; fi
ef416fc2 187
e07d4801
MS
188 pathcomp="${pathcomp}/"
189 done
ef416fc2 190fi
191
e07d4801
MS
192if [ x"$dir_arg" != x ]; then
193 # Make a directory...
194 $doit $instcmd $dst || exit 1
ef416fc2 195
e07d4801
MS
196 # Allow chown/chgrp to fail, but log a warning
197 if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst || echo "warning: Unable to change owner of $dst!"; fi
198 if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst || echo "warning: Unable to change group of $dst!"; fi
199 if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst || exit 1; fi
ef416fc2 200else
e07d4801
MS
201 # Install a file...
202 dstfile="`basename $dst`"
ef416fc2 203
e07d4801
MS
204 # Check the destination file - for libraries just use the "-x" option
205 # to strip...
839a51c8
MS
206 case "$dstfile" in
207 *.a | *.dylib | *.sl | *.sl.* | *.so | *.so.*)
208 stripopt="-x"
209 ;;
210 *)
211 stripopt=""
212 ;;
213 esac
214
e07d4801
MS
215 # Make a temp file name in the proper directory.
216 dsttmp="$dstdir/#inst.$$#"
ef416fc2 217
e07d4801
MS
218 # Move or copy the file name to the temp name
219 $doit $instcmd $src $dsttmp || exit 1
ef416fc2 220
e07d4801
MS
221 # Update permissions and strip as needed, then move to the final name.
222 # If the chmod, strip, rm, or mv commands fail, remove the installed
223 # file...
97c9a8d7 224 if [ x"$stripcmd" != x ]; then $doit $stripcmd $stripopt "$dsttmp" || echo "warning: Unable to strip $dst!"; fi
e07d4801
MS
225 if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp" || echo "warning: Unable to change owner of $dst!"; fi
226 if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp" || echo "warning: Unable to change group of $dst!"; fi
ef416fc2 227
228 trap "rm -f ${dsttmp}" 0 &&
e07d4801
MS
229 if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; fi &&
230 $doit $rmcmd -f "$dstdir/$dstfile" &&
231 $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
232fi
ef416fc2 233
234exit 0