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