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