xref: /freebsd/contrib/ntp/sntp/libevent/build-aux/install-sh (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1*2b15cb3dSCy Schubert#!/bin/sh
2*2b15cb3dSCy Schubert# install - install a program, script, or datafile
3*2b15cb3dSCy Schubert
4*2b15cb3dSCy Schubertscriptversion=2009-04-28.21; # UTC
5*2b15cb3dSCy Schubert
6*2b15cb3dSCy Schubert# This originates from X11R5 (mit/util/scripts/install.sh), which was
7*2b15cb3dSCy Schubert# later released in X11R6 (xc/config/util/install.sh) with the
8*2b15cb3dSCy Schubert# following copyright and license.
9*2b15cb3dSCy Schubert#
10*2b15cb3dSCy Schubert# Copyright (C) 1994 X Consortium
11*2b15cb3dSCy Schubert#
12*2b15cb3dSCy Schubert# Permission is hereby granted, free of charge, to any person obtaining a copy
13*2b15cb3dSCy Schubert# of this software and associated documentation files (the "Software"), to
14*2b15cb3dSCy Schubert# deal in the Software without restriction, including without limitation the
15*2b15cb3dSCy Schubert# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16*2b15cb3dSCy Schubert# sell copies of the Software, and to permit persons to whom the Software is
17*2b15cb3dSCy Schubert# furnished to do so, subject to the following conditions:
18*2b15cb3dSCy Schubert#
19*2b15cb3dSCy Schubert# The above copyright notice and this permission notice shall be included in
20*2b15cb3dSCy Schubert# all copies or substantial portions of the Software.
21*2b15cb3dSCy Schubert#
22*2b15cb3dSCy Schubert# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23*2b15cb3dSCy Schubert# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24*2b15cb3dSCy Schubert# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25*2b15cb3dSCy Schubert# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26*2b15cb3dSCy Schubert# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27*2b15cb3dSCy Schubert# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28*2b15cb3dSCy Schubert#
29*2b15cb3dSCy Schubert# Except as contained in this notice, the name of the X Consortium shall not
30*2b15cb3dSCy Schubert# be used in advertising or otherwise to promote the sale, use or other deal-
31*2b15cb3dSCy Schubert# ings in this Software without prior written authorization from the X Consor-
32*2b15cb3dSCy Schubert# tium.
33*2b15cb3dSCy Schubert#
34*2b15cb3dSCy Schubert#
35*2b15cb3dSCy Schubert# FSF changes to this file are in the public domain.
36*2b15cb3dSCy Schubert#
37*2b15cb3dSCy Schubert# Calling this script install-sh is preferred over install.sh, to prevent
38*2b15cb3dSCy Schubert# `make' implicit rules from creating a file called install from it
39*2b15cb3dSCy Schubert# when there is no Makefile.
40*2b15cb3dSCy Schubert#
41*2b15cb3dSCy Schubert# This script is compatible with the BSD install script, but was written
42*2b15cb3dSCy Schubert# from scratch.
43*2b15cb3dSCy Schubert
44*2b15cb3dSCy Schubertnl='
45*2b15cb3dSCy Schubert'
46*2b15cb3dSCy SchubertIFS=" ""	$nl"
47*2b15cb3dSCy Schubert
48*2b15cb3dSCy Schubert# set DOITPROG to echo to test this script
49*2b15cb3dSCy Schubert
50*2b15cb3dSCy Schubert# Don't use :- since 4.3BSD and earlier shells don't like it.
51*2b15cb3dSCy Schubertdoit=${DOITPROG-}
52*2b15cb3dSCy Schubertif test -z "$doit"; then
53*2b15cb3dSCy Schubert  doit_exec=exec
54*2b15cb3dSCy Schubertelse
55*2b15cb3dSCy Schubert  doit_exec=$doit
56*2b15cb3dSCy Schubertfi
57*2b15cb3dSCy Schubert
58*2b15cb3dSCy Schubert# Put in absolute file names if you don't have them in your path;
59*2b15cb3dSCy Schubert# or use environment vars.
60*2b15cb3dSCy Schubert
61*2b15cb3dSCy Schubertchgrpprog=${CHGRPPROG-chgrp}
62*2b15cb3dSCy Schubertchmodprog=${CHMODPROG-chmod}
63*2b15cb3dSCy Schubertchownprog=${CHOWNPROG-chown}
64*2b15cb3dSCy Schubertcmpprog=${CMPPROG-cmp}
65*2b15cb3dSCy Schubertcpprog=${CPPROG-cp}
66*2b15cb3dSCy Schubertmkdirprog=${MKDIRPROG-mkdir}
67*2b15cb3dSCy Schubertmvprog=${MVPROG-mv}
68*2b15cb3dSCy Schubertrmprog=${RMPROG-rm}
69*2b15cb3dSCy Schubertstripprog=${STRIPPROG-strip}
70*2b15cb3dSCy Schubert
71*2b15cb3dSCy Schubertposix_glob='?'
72*2b15cb3dSCy Schubertinitialize_posix_glob='
73*2b15cb3dSCy Schubert  test "$posix_glob" != "?" || {
74*2b15cb3dSCy Schubert    if (set -f) 2>/dev/null; then
75*2b15cb3dSCy Schubert      posix_glob=
76*2b15cb3dSCy Schubert    else
77*2b15cb3dSCy Schubert      posix_glob=:
78*2b15cb3dSCy Schubert    fi
79*2b15cb3dSCy Schubert  }
80*2b15cb3dSCy Schubert'
81*2b15cb3dSCy Schubert
82*2b15cb3dSCy Schubertposix_mkdir=
83*2b15cb3dSCy Schubert
84*2b15cb3dSCy Schubert# Desired mode of installed file.
85*2b15cb3dSCy Schubertmode=0755
86*2b15cb3dSCy Schubert
87*2b15cb3dSCy Schubertchgrpcmd=
88*2b15cb3dSCy Schubertchmodcmd=$chmodprog
89*2b15cb3dSCy Schubertchowncmd=
90*2b15cb3dSCy Schubertmvcmd=$mvprog
91*2b15cb3dSCy Schubertrmcmd="$rmprog -f"
92*2b15cb3dSCy Schubertstripcmd=
93*2b15cb3dSCy Schubert
94*2b15cb3dSCy Schubertsrc=
95*2b15cb3dSCy Schubertdst=
96*2b15cb3dSCy Schubertdir_arg=
97*2b15cb3dSCy Schubertdst_arg=
98*2b15cb3dSCy Schubert
99*2b15cb3dSCy Schubertcopy_on_change=false
100*2b15cb3dSCy Schubertno_target_directory=
101*2b15cb3dSCy Schubert
102*2b15cb3dSCy Schubertusage="\
103*2b15cb3dSCy SchubertUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104*2b15cb3dSCy Schubert   or: $0 [OPTION]... SRCFILES... DIRECTORY
105*2b15cb3dSCy Schubert   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106*2b15cb3dSCy Schubert   or: $0 [OPTION]... -d DIRECTORIES...
107*2b15cb3dSCy Schubert
108*2b15cb3dSCy SchubertIn the 1st form, copy SRCFILE to DSTFILE.
109*2b15cb3dSCy SchubertIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110*2b15cb3dSCy SchubertIn the 4th, create DIRECTORIES.
111*2b15cb3dSCy Schubert
112*2b15cb3dSCy SchubertOptions:
113*2b15cb3dSCy Schubert     --help     display this help and exit.
114*2b15cb3dSCy Schubert     --version  display version info and exit.
115*2b15cb3dSCy Schubert
116*2b15cb3dSCy Schubert  -c            (ignored)
117*2b15cb3dSCy Schubert  -C            install only if different (preserve the last data modification time)
118*2b15cb3dSCy Schubert  -d            create directories instead of installing files.
119*2b15cb3dSCy Schubert  -g GROUP      $chgrpprog installed files to GROUP.
120*2b15cb3dSCy Schubert  -m MODE       $chmodprog installed files to MODE.
121*2b15cb3dSCy Schubert  -o USER       $chownprog installed files to USER.
122*2b15cb3dSCy Schubert  -s            $stripprog installed files.
123*2b15cb3dSCy Schubert  -t DIRECTORY  install into DIRECTORY.
124*2b15cb3dSCy Schubert  -T            report an error if DSTFILE is a directory.
125*2b15cb3dSCy Schubert
126*2b15cb3dSCy SchubertEnvironment variables override the default commands:
127*2b15cb3dSCy Schubert  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128*2b15cb3dSCy Schubert  RMPROG STRIPPROG
129*2b15cb3dSCy Schubert"
130*2b15cb3dSCy Schubert
131*2b15cb3dSCy Schubertwhile test $# -ne 0; do
132*2b15cb3dSCy Schubert  case $1 in
133*2b15cb3dSCy Schubert    -c) ;;
134*2b15cb3dSCy Schubert
135*2b15cb3dSCy Schubert    -C) copy_on_change=true;;
136*2b15cb3dSCy Schubert
137*2b15cb3dSCy Schubert    -d) dir_arg=true;;
138*2b15cb3dSCy Schubert
139*2b15cb3dSCy Schubert    -g) chgrpcmd="$chgrpprog $2"
140*2b15cb3dSCy Schubert	shift;;
141*2b15cb3dSCy Schubert
142*2b15cb3dSCy Schubert    --help) echo "$usage"; exit $?;;
143*2b15cb3dSCy Schubert
144*2b15cb3dSCy Schubert    -m) mode=$2
145*2b15cb3dSCy Schubert	case $mode in
146*2b15cb3dSCy Schubert	  *' '* | *'	'* | *'
147*2b15cb3dSCy Schubert'*	  | *'*'* | *'?'* | *'['*)
148*2b15cb3dSCy Schubert	    echo "$0: invalid mode: $mode" >&2
149*2b15cb3dSCy Schubert	    exit 1;;
150*2b15cb3dSCy Schubert	esac
151*2b15cb3dSCy Schubert	shift;;
152*2b15cb3dSCy Schubert
153*2b15cb3dSCy Schubert    -o) chowncmd="$chownprog $2"
154*2b15cb3dSCy Schubert	shift;;
155*2b15cb3dSCy Schubert
156*2b15cb3dSCy Schubert    -s) stripcmd=$stripprog;;
157*2b15cb3dSCy Schubert
158*2b15cb3dSCy Schubert    -t) dst_arg=$2
159*2b15cb3dSCy Schubert	shift;;
160*2b15cb3dSCy Schubert
161*2b15cb3dSCy Schubert    -T) no_target_directory=true;;
162*2b15cb3dSCy Schubert
163*2b15cb3dSCy Schubert    --version) echo "$0 $scriptversion"; exit $?;;
164*2b15cb3dSCy Schubert
165*2b15cb3dSCy Schubert    --)	shift
166*2b15cb3dSCy Schubert	break;;
167*2b15cb3dSCy Schubert
168*2b15cb3dSCy Schubert    -*)	echo "$0: invalid option: $1" >&2
169*2b15cb3dSCy Schubert	exit 1;;
170*2b15cb3dSCy Schubert
171*2b15cb3dSCy Schubert    *)  break;;
172*2b15cb3dSCy Schubert  esac
173*2b15cb3dSCy Schubert  shift
174*2b15cb3dSCy Schubertdone
175*2b15cb3dSCy Schubert
176*2b15cb3dSCy Schubertif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177*2b15cb3dSCy Schubert  # When -d is used, all remaining arguments are directories to create.
178*2b15cb3dSCy Schubert  # When -t is used, the destination is already specified.
179*2b15cb3dSCy Schubert  # Otherwise, the last argument is the destination.  Remove it from $@.
180*2b15cb3dSCy Schubert  for arg
181*2b15cb3dSCy Schubert  do
182*2b15cb3dSCy Schubert    if test -n "$dst_arg"; then
183*2b15cb3dSCy Schubert      # $@ is not empty: it contains at least $arg.
184*2b15cb3dSCy Schubert      set fnord "$@" "$dst_arg"
185*2b15cb3dSCy Schubert      shift # fnord
186*2b15cb3dSCy Schubert    fi
187*2b15cb3dSCy Schubert    shift # arg
188*2b15cb3dSCy Schubert    dst_arg=$arg
189*2b15cb3dSCy Schubert  done
190*2b15cb3dSCy Schubertfi
191*2b15cb3dSCy Schubert
192*2b15cb3dSCy Schubertif test $# -eq 0; then
193*2b15cb3dSCy Schubert  if test -z "$dir_arg"; then
194*2b15cb3dSCy Schubert    echo "$0: no input file specified." >&2
195*2b15cb3dSCy Schubert    exit 1
196*2b15cb3dSCy Schubert  fi
197*2b15cb3dSCy Schubert  # It's OK to call `install-sh -d' without argument.
198*2b15cb3dSCy Schubert  # This can happen when creating conditional directories.
199*2b15cb3dSCy Schubert  exit 0
200*2b15cb3dSCy Schubertfi
201*2b15cb3dSCy Schubert
202*2b15cb3dSCy Schubertif test -z "$dir_arg"; then
203*2b15cb3dSCy Schubert  trap '(exit $?); exit' 1 2 13 15
204*2b15cb3dSCy Schubert
205*2b15cb3dSCy Schubert  # Set umask so as not to create temps with too-generous modes.
206*2b15cb3dSCy Schubert  # However, 'strip' requires both read and write access to temps.
207*2b15cb3dSCy Schubert  case $mode in
208*2b15cb3dSCy Schubert    # Optimize common cases.
209*2b15cb3dSCy Schubert    *644) cp_umask=133;;
210*2b15cb3dSCy Schubert    *755) cp_umask=22;;
211*2b15cb3dSCy Schubert
212*2b15cb3dSCy Schubert    *[0-7])
213*2b15cb3dSCy Schubert      if test -z "$stripcmd"; then
214*2b15cb3dSCy Schubert	u_plus_rw=
215*2b15cb3dSCy Schubert      else
216*2b15cb3dSCy Schubert	u_plus_rw='% 200'
217*2b15cb3dSCy Schubert      fi
218*2b15cb3dSCy Schubert      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219*2b15cb3dSCy Schubert    *)
220*2b15cb3dSCy Schubert      if test -z "$stripcmd"; then
221*2b15cb3dSCy Schubert	u_plus_rw=
222*2b15cb3dSCy Schubert      else
223*2b15cb3dSCy Schubert	u_plus_rw=,u+rw
224*2b15cb3dSCy Schubert      fi
225*2b15cb3dSCy Schubert      cp_umask=$mode$u_plus_rw;;
226*2b15cb3dSCy Schubert  esac
227*2b15cb3dSCy Schubertfi
228*2b15cb3dSCy Schubert
229*2b15cb3dSCy Schubertfor src
230*2b15cb3dSCy Schubertdo
231*2b15cb3dSCy Schubert  # Protect names starting with `-'.
232*2b15cb3dSCy Schubert  case $src in
233*2b15cb3dSCy Schubert    -*) src=./$src;;
234*2b15cb3dSCy Schubert  esac
235*2b15cb3dSCy Schubert
236*2b15cb3dSCy Schubert  if test -n "$dir_arg"; then
237*2b15cb3dSCy Schubert    dst=$src
238*2b15cb3dSCy Schubert    dstdir=$dst
239*2b15cb3dSCy Schubert    test -d "$dstdir"
240*2b15cb3dSCy Schubert    dstdir_status=$?
241*2b15cb3dSCy Schubert  else
242*2b15cb3dSCy Schubert
243*2b15cb3dSCy Schubert    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244*2b15cb3dSCy Schubert    # might cause directories to be created, which would be especially bad
245*2b15cb3dSCy Schubert    # if $src (and thus $dsttmp) contains '*'.
246*2b15cb3dSCy Schubert    if test ! -f "$src" && test ! -d "$src"; then
247*2b15cb3dSCy Schubert      echo "$0: $src does not exist." >&2
248*2b15cb3dSCy Schubert      exit 1
249*2b15cb3dSCy Schubert    fi
250*2b15cb3dSCy Schubert
251*2b15cb3dSCy Schubert    if test -z "$dst_arg"; then
252*2b15cb3dSCy Schubert      echo "$0: no destination specified." >&2
253*2b15cb3dSCy Schubert      exit 1
254*2b15cb3dSCy Schubert    fi
255*2b15cb3dSCy Schubert
256*2b15cb3dSCy Schubert    dst=$dst_arg
257*2b15cb3dSCy Schubert    # Protect names starting with `-'.
258*2b15cb3dSCy Schubert    case $dst in
259*2b15cb3dSCy Schubert      -*) dst=./$dst;;
260*2b15cb3dSCy Schubert    esac
261*2b15cb3dSCy Schubert
262*2b15cb3dSCy Schubert    # If destination is a directory, append the input filename; won't work
263*2b15cb3dSCy Schubert    # if double slashes aren't ignored.
264*2b15cb3dSCy Schubert    if test -d "$dst"; then
265*2b15cb3dSCy Schubert      if test -n "$no_target_directory"; then
266*2b15cb3dSCy Schubert	echo "$0: $dst_arg: Is a directory" >&2
267*2b15cb3dSCy Schubert	exit 1
268*2b15cb3dSCy Schubert      fi
269*2b15cb3dSCy Schubert      dstdir=$dst
270*2b15cb3dSCy Schubert      dst=$dstdir/`basename "$src"`
271*2b15cb3dSCy Schubert      dstdir_status=0
272*2b15cb3dSCy Schubert    else
273*2b15cb3dSCy Schubert      # Prefer dirname, but fall back on a substitute if dirname fails.
274*2b15cb3dSCy Schubert      dstdir=`
275*2b15cb3dSCy Schubert	(dirname "$dst") 2>/dev/null ||
276*2b15cb3dSCy Schubert	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277*2b15cb3dSCy Schubert	     X"$dst" : 'X\(//\)[^/]' \| \
278*2b15cb3dSCy Schubert	     X"$dst" : 'X\(//\)$' \| \
279*2b15cb3dSCy Schubert	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280*2b15cb3dSCy Schubert	echo X"$dst" |
281*2b15cb3dSCy Schubert	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282*2b15cb3dSCy Schubert		   s//\1/
283*2b15cb3dSCy Schubert		   q
284*2b15cb3dSCy Schubert		 }
285*2b15cb3dSCy Schubert		 /^X\(\/\/\)[^/].*/{
286*2b15cb3dSCy Schubert		   s//\1/
287*2b15cb3dSCy Schubert		   q
288*2b15cb3dSCy Schubert		 }
289*2b15cb3dSCy Schubert		 /^X\(\/\/\)$/{
290*2b15cb3dSCy Schubert		   s//\1/
291*2b15cb3dSCy Schubert		   q
292*2b15cb3dSCy Schubert		 }
293*2b15cb3dSCy Schubert		 /^X\(\/\).*/{
294*2b15cb3dSCy Schubert		   s//\1/
295*2b15cb3dSCy Schubert		   q
296*2b15cb3dSCy Schubert		 }
297*2b15cb3dSCy Schubert		 s/.*/./; q'
298*2b15cb3dSCy Schubert      `
299*2b15cb3dSCy Schubert
300*2b15cb3dSCy Schubert      test -d "$dstdir"
301*2b15cb3dSCy Schubert      dstdir_status=$?
302*2b15cb3dSCy Schubert    fi
303*2b15cb3dSCy Schubert  fi
304*2b15cb3dSCy Schubert
305*2b15cb3dSCy Schubert  obsolete_mkdir_used=false
306*2b15cb3dSCy Schubert
307*2b15cb3dSCy Schubert  if test $dstdir_status != 0; then
308*2b15cb3dSCy Schubert    case $posix_mkdir in
309*2b15cb3dSCy Schubert      '')
310*2b15cb3dSCy Schubert	# Create intermediate dirs using mode 755 as modified by the umask.
311*2b15cb3dSCy Schubert	# This is like FreeBSD 'install' as of 1997-10-28.
312*2b15cb3dSCy Schubert	umask=`umask`
313*2b15cb3dSCy Schubert	case $stripcmd.$umask in
314*2b15cb3dSCy Schubert	  # Optimize common cases.
315*2b15cb3dSCy Schubert	  *[2367][2367]) mkdir_umask=$umask;;
316*2b15cb3dSCy Schubert	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317*2b15cb3dSCy Schubert
318*2b15cb3dSCy Schubert	  *[0-7])
319*2b15cb3dSCy Schubert	    mkdir_umask=`expr $umask + 22 \
320*2b15cb3dSCy Schubert	      - $umask % 100 % 40 + $umask % 20 \
321*2b15cb3dSCy Schubert	      - $umask % 10 % 4 + $umask % 2
322*2b15cb3dSCy Schubert	    `;;
323*2b15cb3dSCy Schubert	  *) mkdir_umask=$umask,go-w;;
324*2b15cb3dSCy Schubert	esac
325*2b15cb3dSCy Schubert
326*2b15cb3dSCy Schubert	# With -d, create the new directory with the user-specified mode.
327*2b15cb3dSCy Schubert	# Otherwise, rely on $mkdir_umask.
328*2b15cb3dSCy Schubert	if test -n "$dir_arg"; then
329*2b15cb3dSCy Schubert	  mkdir_mode=-m$mode
330*2b15cb3dSCy Schubert	else
331*2b15cb3dSCy Schubert	  mkdir_mode=
332*2b15cb3dSCy Schubert	fi
333*2b15cb3dSCy Schubert
334*2b15cb3dSCy Schubert	posix_mkdir=false
335*2b15cb3dSCy Schubert	case $umask in
336*2b15cb3dSCy Schubert	  *[123567][0-7][0-7])
337*2b15cb3dSCy Schubert	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338*2b15cb3dSCy Schubert	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339*2b15cb3dSCy Schubert	    ;;
340*2b15cb3dSCy Schubert	  *)
341*2b15cb3dSCy Schubert	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342*2b15cb3dSCy Schubert	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343*2b15cb3dSCy Schubert
344*2b15cb3dSCy Schubert	    if (umask $mkdir_umask &&
345*2b15cb3dSCy Schubert		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346*2b15cb3dSCy Schubert	    then
347*2b15cb3dSCy Schubert	      if test -z "$dir_arg" || {
348*2b15cb3dSCy Schubert		   # Check for POSIX incompatibilities with -m.
349*2b15cb3dSCy Schubert		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350*2b15cb3dSCy Schubert		   # other-writeable bit of parent directory when it shouldn't.
351*2b15cb3dSCy Schubert		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352*2b15cb3dSCy Schubert		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353*2b15cb3dSCy Schubert		   case $ls_ld_tmpdir in
354*2b15cb3dSCy Schubert		     d????-?r-*) different_mode=700;;
355*2b15cb3dSCy Schubert		     d????-?--*) different_mode=755;;
356*2b15cb3dSCy Schubert		     *) false;;
357*2b15cb3dSCy Schubert		   esac &&
358*2b15cb3dSCy Schubert		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359*2b15cb3dSCy Schubert		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360*2b15cb3dSCy Schubert		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361*2b15cb3dSCy Schubert		   }
362*2b15cb3dSCy Schubert		 }
363*2b15cb3dSCy Schubert	      then posix_mkdir=:
364*2b15cb3dSCy Schubert	      fi
365*2b15cb3dSCy Schubert	      rmdir "$tmpdir/d" "$tmpdir"
366*2b15cb3dSCy Schubert	    else
367*2b15cb3dSCy Schubert	      # Remove any dirs left behind by ancient mkdir implementations.
368*2b15cb3dSCy Schubert	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369*2b15cb3dSCy Schubert	    fi
370*2b15cb3dSCy Schubert	    trap '' 0;;
371*2b15cb3dSCy Schubert	esac;;
372*2b15cb3dSCy Schubert    esac
373*2b15cb3dSCy Schubert
374*2b15cb3dSCy Schubert    if
375*2b15cb3dSCy Schubert      $posix_mkdir && (
376*2b15cb3dSCy Schubert	umask $mkdir_umask &&
377*2b15cb3dSCy Schubert	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378*2b15cb3dSCy Schubert      )
379*2b15cb3dSCy Schubert    then :
380*2b15cb3dSCy Schubert    else
381*2b15cb3dSCy Schubert
382*2b15cb3dSCy Schubert      # The umask is ridiculous, or mkdir does not conform to POSIX,
383*2b15cb3dSCy Schubert      # or it failed possibly due to a race condition.  Create the
384*2b15cb3dSCy Schubert      # directory the slow way, step by step, checking for races as we go.
385*2b15cb3dSCy Schubert
386*2b15cb3dSCy Schubert      case $dstdir in
387*2b15cb3dSCy Schubert	/*) prefix='/';;
388*2b15cb3dSCy Schubert	-*) prefix='./';;
389*2b15cb3dSCy Schubert	*)  prefix='';;
390*2b15cb3dSCy Schubert      esac
391*2b15cb3dSCy Schubert
392*2b15cb3dSCy Schubert      eval "$initialize_posix_glob"
393*2b15cb3dSCy Schubert
394*2b15cb3dSCy Schubert      oIFS=$IFS
395*2b15cb3dSCy Schubert      IFS=/
396*2b15cb3dSCy Schubert      $posix_glob set -f
397*2b15cb3dSCy Schubert      set fnord $dstdir
398*2b15cb3dSCy Schubert      shift
399*2b15cb3dSCy Schubert      $posix_glob set +f
400*2b15cb3dSCy Schubert      IFS=$oIFS
401*2b15cb3dSCy Schubert
402*2b15cb3dSCy Schubert      prefixes=
403*2b15cb3dSCy Schubert
404*2b15cb3dSCy Schubert      for d
405*2b15cb3dSCy Schubert      do
406*2b15cb3dSCy Schubert	test -z "$d" && continue
407*2b15cb3dSCy Schubert
408*2b15cb3dSCy Schubert	prefix=$prefix$d
409*2b15cb3dSCy Schubert	if test -d "$prefix"; then
410*2b15cb3dSCy Schubert	  prefixes=
411*2b15cb3dSCy Schubert	else
412*2b15cb3dSCy Schubert	  if $posix_mkdir; then
413*2b15cb3dSCy Schubert	    (umask=$mkdir_umask &&
414*2b15cb3dSCy Schubert	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415*2b15cb3dSCy Schubert	    # Don't fail if two instances are running concurrently.
416*2b15cb3dSCy Schubert	    test -d "$prefix" || exit 1
417*2b15cb3dSCy Schubert	  else
418*2b15cb3dSCy Schubert	    case $prefix in
419*2b15cb3dSCy Schubert	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420*2b15cb3dSCy Schubert	      *) qprefix=$prefix;;
421*2b15cb3dSCy Schubert	    esac
422*2b15cb3dSCy Schubert	    prefixes="$prefixes '$qprefix'"
423*2b15cb3dSCy Schubert	  fi
424*2b15cb3dSCy Schubert	fi
425*2b15cb3dSCy Schubert	prefix=$prefix/
426*2b15cb3dSCy Schubert      done
427*2b15cb3dSCy Schubert
428*2b15cb3dSCy Schubert      if test -n "$prefixes"; then
429*2b15cb3dSCy Schubert	# Don't fail if two instances are running concurrently.
430*2b15cb3dSCy Schubert	(umask $mkdir_umask &&
431*2b15cb3dSCy Schubert	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432*2b15cb3dSCy Schubert	  test -d "$dstdir" || exit 1
433*2b15cb3dSCy Schubert	obsolete_mkdir_used=true
434*2b15cb3dSCy Schubert      fi
435*2b15cb3dSCy Schubert    fi
436*2b15cb3dSCy Schubert  fi
437*2b15cb3dSCy Schubert
438*2b15cb3dSCy Schubert  if test -n "$dir_arg"; then
439*2b15cb3dSCy Schubert    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440*2b15cb3dSCy Schubert    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441*2b15cb3dSCy Schubert    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442*2b15cb3dSCy Schubert      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443*2b15cb3dSCy Schubert  else
444*2b15cb3dSCy Schubert
445*2b15cb3dSCy Schubert    # Make a couple of temp file names in the proper directory.
446*2b15cb3dSCy Schubert    dsttmp=$dstdir/_inst.$$_
447*2b15cb3dSCy Schubert    rmtmp=$dstdir/_rm.$$_
448*2b15cb3dSCy Schubert
449*2b15cb3dSCy Schubert    # Trap to clean up those temp files at exit.
450*2b15cb3dSCy Schubert    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451*2b15cb3dSCy Schubert
452*2b15cb3dSCy Schubert    # Copy the file name to the temp name.
453*2b15cb3dSCy Schubert    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454*2b15cb3dSCy Schubert
455*2b15cb3dSCy Schubert    # and set any options; do chmod last to preserve setuid bits.
456*2b15cb3dSCy Schubert    #
457*2b15cb3dSCy Schubert    # If any of these fail, we abort the whole thing.  If we want to
458*2b15cb3dSCy Schubert    # ignore errors from any of these, just make sure not to ignore
459*2b15cb3dSCy Schubert    # errors from the above "$doit $cpprog $src $dsttmp" command.
460*2b15cb3dSCy Schubert    #
461*2b15cb3dSCy Schubert    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462*2b15cb3dSCy Schubert    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463*2b15cb3dSCy Schubert    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464*2b15cb3dSCy Schubert    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465*2b15cb3dSCy Schubert
466*2b15cb3dSCy Schubert    # If -C, don't bother to copy if it wouldn't change the file.
467*2b15cb3dSCy Schubert    if $copy_on_change &&
468*2b15cb3dSCy Schubert       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469*2b15cb3dSCy Schubert       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470*2b15cb3dSCy Schubert
471*2b15cb3dSCy Schubert       eval "$initialize_posix_glob" &&
472*2b15cb3dSCy Schubert       $posix_glob set -f &&
473*2b15cb3dSCy Schubert       set X $old && old=:$2:$4:$5:$6 &&
474*2b15cb3dSCy Schubert       set X $new && new=:$2:$4:$5:$6 &&
475*2b15cb3dSCy Schubert       $posix_glob set +f &&
476*2b15cb3dSCy Schubert
477*2b15cb3dSCy Schubert       test "$old" = "$new" &&
478*2b15cb3dSCy Schubert       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479*2b15cb3dSCy Schubert    then
480*2b15cb3dSCy Schubert      rm -f "$dsttmp"
481*2b15cb3dSCy Schubert    else
482*2b15cb3dSCy Schubert      # Rename the file to the real destination.
483*2b15cb3dSCy Schubert      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484*2b15cb3dSCy Schubert
485*2b15cb3dSCy Schubert      # The rename failed, perhaps because mv can't rename something else
486*2b15cb3dSCy Schubert      # to itself, or perhaps because mv is so ancient that it does not
487*2b15cb3dSCy Schubert      # support -f.
488*2b15cb3dSCy Schubert      {
489*2b15cb3dSCy Schubert	# Now remove or move aside any old file at destination location.
490*2b15cb3dSCy Schubert	# We try this two ways since rm can't unlink itself on some
491*2b15cb3dSCy Schubert	# systems and the destination file might be busy for other
492*2b15cb3dSCy Schubert	# reasons.  In this case, the final cleanup might fail but the new
493*2b15cb3dSCy Schubert	# file should still install successfully.
494*2b15cb3dSCy Schubert	{
495*2b15cb3dSCy Schubert	  test ! -f "$dst" ||
496*2b15cb3dSCy Schubert	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497*2b15cb3dSCy Schubert	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498*2b15cb3dSCy Schubert	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499*2b15cb3dSCy Schubert	  } ||
500*2b15cb3dSCy Schubert	  { echo "$0: cannot unlink or rename $dst" >&2
501*2b15cb3dSCy Schubert	    (exit 1); exit 1
502*2b15cb3dSCy Schubert	  }
503*2b15cb3dSCy Schubert	} &&
504*2b15cb3dSCy Schubert
505*2b15cb3dSCy Schubert	# Now rename the file to the real destination.
506*2b15cb3dSCy Schubert	$doit $mvcmd "$dsttmp" "$dst"
507*2b15cb3dSCy Schubert      }
508*2b15cb3dSCy Schubert    fi || exit 1
509*2b15cb3dSCy Schubert
510*2b15cb3dSCy Schubert    trap '' 0
511*2b15cb3dSCy Schubert  fi
512*2b15cb3dSCy Schubertdone
513*2b15cb3dSCy Schubert
514*2b15cb3dSCy Schubert# Local variables:
515*2b15cb3dSCy Schubert# eval: (add-hook 'write-file-hooks 'time-stamp)
516*2b15cb3dSCy Schubert# time-stamp-start: "scriptversion="
517*2b15cb3dSCy Schubert# time-stamp-format: "%:y-%02m-%02d.%02H"
518*2b15cb3dSCy Schubert# time-stamp-time-zone: "UTC"
519*2b15cb3dSCy Schubert# time-stamp-end: "; # UTC"
520*2b15cb3dSCy Schubert# End:
521