]> git.ipfire.org Git - thirdparty/cups.git/blame - install-sh
The PPD compiler's #if/#elif/#else/#endif did not work for undefined variables
[thirdparty/cups.git] / install-sh
CommitLineData
ee1fd936 1#!/bin/sh
2#
7bbfe47b 3# "$Id$"
ee1fd936 4#
7bbfe47b 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)
ee1fd936 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.
ee1fd936 32
33# set DOITPROG to echo to test this script
ee1fd936 34# Don't use :- since 4.3BSD and earlier shells don't like it.
35doit="${DOITPROG-}"
36
7bbfe47b 37# Force umask to 022...
38umask 022
ee1fd936 39
40# put in absolute paths if you don't have them in your path; or use env. vars.
ee1fd936 41mvprog="${MVPROG-mv}"
42cpprog="${CPPROG-cp}"
43chmodprog="${CHMODPROG-chmod}"
44chownprog="${CHOWNPROG-chown}"
45chgrpprog="${CHGRPPROG-chgrp}"
46stripprog="${STRIPPROG-strip}"
47rmprog="${RMPROG-rm}"
48mkdirprog="${MKDIRPROG-mkdir}"
49
50transformbasename=""
51transform_arg=""
52instcmd="$mvprog"
53chmodcmd="$chmodprog 0755"
54chowncmd=""
55chgrpcmd=""
56stripcmd=""
57rmcmd="$rmprog -f"
58mvcmd="$mvprog"
59src=""
60dst=""
61dir_arg=""
62
63while [ x"$1" != x ]; do
7bbfe47b 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 ;;
ee1fd936 103
7bbfe47b 104 *)
105 if [ x"$src" = x ]; then
106 src="$1"
107 else
108 dst="$1"
109 fi
110 shift
111 continue
112 ;;
113 esac
ee1fd936 114done
115
7bbfe47b 116if [ x"$src" = x ]; then
117 echo "install-sh: No input file specified"
ee1fd936 118 exit 1
ee1fd936 119fi
120
121if [ x"$dir_arg" != x ]; then
7bbfe47b 122 dst="$src"
ee1fd936 123 src=""
124
7bbfe47b 125 if [ -d "$dst" ]; then
ee1fd936 126 instcmd=:
ee1fd936 127 else
04d23db5 128 instcmd=$mkdirprog
ee1fd936 129 fi
130else
7bbfe47b 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"
ee1fd936 136 exit 1
137 fi
138
7bbfe47b 139 if [ x"$dst" = x ]; then
140 echo "install: No destination specified"
ee1fd936 141 exit 1
ee1fd936 142 fi
143
7bbfe47b 144 # If destination is a directory, append the input filename.
145 if [ -d "$dst" ]; then
146 dst="$dst/`basename $src`"
ee1fd936 147 fi
148fi
149
150## this sed command emulates the dirname command
7bbfe47b 151dstdir="`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`"
ee1fd936 152
153# Make sure that the destination directory exists.
7bbfe47b 154# This part is taken from Noah Friedman's mkinstalldirs script
ee1fd936 155
156# Skip lots of stat calls in the usual case.
157if [ ! -d "$dstdir" ]; then
7bbfe47b 158 defaultIFS='
04d23db5 159 '
7bbfe47b 160 IFS="${IFS-${defaultIFS}}"
ee1fd936 161
7bbfe47b 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}"
ee1fd936 167
7bbfe47b 168 pathcomp=''
ee1fd936 169
7bbfe47b 170 while [ $# -ne 0 ] ; do
171 pathcomp="${pathcomp}${1}"
172 shift
ee1fd936 173
7bbfe47b 174 if [ ! -d "${pathcomp}" ]; then $doit $mkdirprog "${pathcomp}"; fi
ee1fd936 175
7bbfe47b 176 pathcomp="${pathcomp}/"
177 done
ee1fd936 178fi
179
7bbfe47b 180if [ x"$dir_arg" != x ]; then
181 # Make a directory...
182 $doit $instcmd $dst || exit 1
ee1fd936 183
7bbfe47b 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
ee1fd936 188else
7bbfe47b 189 # Install a file...
190 dstfile="`basename $dst`"
ee1fd936 191
7bbfe47b 192 # Check the destination file - for libraries just use the "-x" option
193 # to strip...
e545002d 194 case "$dstfile" in
195 *.a | *.dylib | *.sl | *.sl.* | *.so | *.so.*)
196 stripopt="-x"
197 ;;
198 *)
199 stripopt=""
200 ;;
201 esac
202
7bbfe47b 203 # Make a temp file name in the proper directory.
204 dsttmp="$dstdir/#inst.$$#"
ee1fd936 205
7bbfe47b 206 # Move or copy the file name to the temp name
207 $doit $instcmd $src $dsttmp || exit 1
ee1fd936 208
7bbfe47b 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"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp" || echo "warning: Unable to change owner of $dst!"; fi
213 if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp" || echo "warning: Unable to change group of $dst!"; fi
ee1fd936 214
215 trap "rm -f ${dsttmp}" 0 &&
7bbfe47b 216 if [ x"$stripcmd" != x ]; then $doit $stripcmd $stripopt "$dsttmp"; fi &&
217 if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; fi &&
218 $doit $rmcmd -f "$dstdir/$dstfile" &&
219 $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
220fi
ee1fd936 221
222exit 0