xref: /netbsd-src/external/lgpl3/gmp/dist/install-sh (revision ce54336801cf28877c3414aa2fcb251dddd543a2)
151c586b8Smrg#!/bin/sh
251c586b8Smrg# install - install a program, script, or datafile
351c586b8Smrg
4*ce543368Smrgscriptversion=2013-12-25.23; # UTC
551c586b8Smrg
651c586b8Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
751c586b8Smrg# later released in X11R6 (xc/config/util/install.sh) with the
851c586b8Smrg# following copyright and license.
951c586b8Smrg#
1051c586b8Smrg# Copyright (C) 1994 X Consortium
1151c586b8Smrg#
1251c586b8Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1351c586b8Smrg# of this software and associated documentation files (the "Software"), to
1451c586b8Smrg# deal in the Software without restriction, including without limitation the
1551c586b8Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1651c586b8Smrg# sell copies of the Software, and to permit persons to whom the Software is
1751c586b8Smrg# furnished to do so, subject to the following conditions:
1851c586b8Smrg#
1951c586b8Smrg# The above copyright notice and this permission notice shall be included in
2051c586b8Smrg# all copies or substantial portions of the Software.
2151c586b8Smrg#
2251c586b8Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2351c586b8Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2451c586b8Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2551c586b8Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2651c586b8Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2751c586b8Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2851c586b8Smrg#
2951c586b8Smrg# Except as contained in this notice, the name of the X Consortium shall not
3051c586b8Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3151c586b8Smrg# ings in this Software without prior written authorization from the X Consor-
3251c586b8Smrg# tium.
3351c586b8Smrg#
3451c586b8Smrg#
3551c586b8Smrg# FSF changes to this file are in the public domain.
3651c586b8Smrg#
3751c586b8Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38*ce543368Smrg# 'make' implicit rules from creating a file called install from it
3951c586b8Smrg# when there is no Makefile.
4051c586b8Smrg#
4151c586b8Smrg# This script is compatible with the BSD install script, but was written
42*ce543368Smrg# from scratch.
4351c586b8Smrg
44*ce543368Smrgtab='	'
45*ce543368Smrgnl='
46*ce543368Smrg'
47*ce543368SmrgIFS=" $tab$nl"
4851c586b8Smrg
49*ce543368Smrg# Set DOITPROG to "echo" to test this script.
5051c586b8Smrg
51*ce543368Smrgdoit=${DOITPROG-}
52*ce543368Smrgdoit_exec=${doit:-exec}
5351c586b8Smrg
54*ce543368Smrg# Put in absolute file names if you don't have them in your path;
55*ce543368Smrg# or use environment vars.
5651c586b8Smrg
57*ce543368Smrgchgrpprog=${CHGRPPROG-chgrp}
58*ce543368Smrgchmodprog=${CHMODPROG-chmod}
59*ce543368Smrgchownprog=${CHOWNPROG-chown}
60*ce543368Smrgcmpprog=${CMPPROG-cmp}
61*ce543368Smrgcpprog=${CPPROG-cp}
62*ce543368Smrgmkdirprog=${MKDIRPROG-mkdir}
63*ce543368Smrgmvprog=${MVPROG-mv}
64*ce543368Smrgrmprog=${RMPROG-rm}
65*ce543368Smrgstripprog=${STRIPPROG-strip}
66*ce543368Smrg
67*ce543368Smrgposix_mkdir=
68*ce543368Smrg
69*ce543368Smrg# Desired mode of installed file.
70*ce543368Smrgmode=0755
71*ce543368Smrg
7251c586b8Smrgchgrpcmd=
73*ce543368Smrgchmodcmd=$chmodprog
74*ce543368Smrgchowncmd=
75*ce543368Smrgmvcmd=$mvprog
7651c586b8Smrgrmcmd="$rmprog -f"
77*ce543368Smrgstripcmd=
78*ce543368Smrg
7951c586b8Smrgsrc=
8051c586b8Smrgdst=
8151c586b8Smrgdir_arg=
82*ce543368Smrgdst_arg=
8351c586b8Smrg
84*ce543368Smrgcopy_on_change=false
85*ce543368Smrgis_target_a_directory=possibly
86*ce543368Smrg
87*ce543368Smrgusage="\
88*ce543368SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8951c586b8Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90*ce543368Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91*ce543368Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9251c586b8Smrg
93*ce543368SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94*ce543368SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95*ce543368SmrgIn the 4th, create DIRECTORIES.
9651c586b8Smrg
9751c586b8SmrgOptions:
9851c586b8Smrg     --help     display this help and exit.
9951c586b8Smrg     --version  display version info and exit.
10051c586b8Smrg
101*ce543368Smrg  -c            (ignored)
102*ce543368Smrg  -C            install only if different (preserve the last data modification time)
103*ce543368Smrg  -d            create directories instead of installing files.
104*ce543368Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105*ce543368Smrg  -m MODE       $chmodprog installed files to MODE.
106*ce543368Smrg  -o USER       $chownprog installed files to USER.
107*ce543368Smrg  -s            $stripprog installed files.
108*ce543368Smrg  -t DIRECTORY  install into DIRECTORY.
109*ce543368Smrg  -T            report an error if DSTFILE is a directory.
110*ce543368Smrg
11151c586b8SmrgEnvironment variables override the default commands:
112*ce543368Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113*ce543368Smrg  RMPROG STRIPPROG
11451c586b8Smrg"
11551c586b8Smrg
116*ce543368Smrgwhile test $# -ne 0; do
11751c586b8Smrg  case $1 in
118*ce543368Smrg    -c) ;;
11951c586b8Smrg
120*ce543368Smrg    -C) copy_on_change=true;;
12151c586b8Smrg
122*ce543368Smrg    -d) dir_arg=true;;
12351c586b8Smrg
12451c586b8Smrg    -g) chgrpcmd="$chgrpprog $2"
125*ce543368Smrg        shift;;
12651c586b8Smrg
127*ce543368Smrg    --help) echo "$usage"; exit $?;;
12851c586b8Smrg
129*ce543368Smrg    -m) mode=$2
130*ce543368Smrg        case $mode in
131*ce543368Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132*ce543368Smrg            echo "$0: invalid mode: $mode" >&2
133*ce543368Smrg            exit 1;;
134*ce543368Smrg        esac
135*ce543368Smrg        shift;;
13651c586b8Smrg
13751c586b8Smrg    -o) chowncmd="$chownprog $2"
138*ce543368Smrg        shift;;
13951c586b8Smrg
140*ce543368Smrg    -s) stripcmd=$stripprog;;
141*ce543368Smrg
142*ce543368Smrg    -t)
143*ce543368Smrg        is_target_a_directory=always
144*ce543368Smrg        dst_arg=$2
145*ce543368Smrg        # Protect names problematic for 'test' and other utilities.
146*ce543368Smrg        case $dst_arg in
147*ce543368Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148*ce543368Smrg        esac
149*ce543368Smrg        shift;;
150*ce543368Smrg
151*ce543368Smrg    -T) is_target_a_directory=never;;
152*ce543368Smrg
153*ce543368Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154*ce543368Smrg
155*ce543368Smrg    --) shift
156*ce543368Smrg        break;;
157*ce543368Smrg
158*ce543368Smrg    -*) echo "$0: invalid option: $1" >&2
159*ce543368Smrg        exit 1;;
160*ce543368Smrg
161*ce543368Smrg    *)  break;;
162*ce543368Smrg  esac
16351c586b8Smrg  shift
164*ce543368Smrgdone
16551c586b8Smrg
166*ce543368Smrg# We allow the use of options -d and -T together, by making -d
167*ce543368Smrg# take the precedence; this is for compatibility with GNU install.
16851c586b8Smrg
169*ce543368Smrgif test -n "$dir_arg"; then
170*ce543368Smrg  if test -n "$dst_arg"; then
171*ce543368Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172*ce543368Smrg    exit 1
173*ce543368Smrg  fi
174*ce543368Smrgfi
17551c586b8Smrg
176*ce543368Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177*ce543368Smrg  # When -d is used, all remaining arguments are directories to create.
178*ce543368Smrg  # When -t is used, the destination is already specified.
17951c586b8Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18051c586b8Smrg  for arg
18151c586b8Smrg  do
182*ce543368Smrg    if test -n "$dst_arg"; then
18351c586b8Smrg      # $@ is not empty: it contains at least $arg.
184*ce543368Smrg      set fnord "$@" "$dst_arg"
18551c586b8Smrg      shift # fnord
18651c586b8Smrg    fi
18751c586b8Smrg    shift # arg
188*ce543368Smrg    dst_arg=$arg
189*ce543368Smrg    # Protect names problematic for 'test' and other utilities.
190*ce543368Smrg    case $dst_arg in
191*ce543368Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19251c586b8Smrg    esac
19351c586b8Smrg  done
194*ce543368Smrgfi
19551c586b8Smrg
196*ce543368Smrgif test $# -eq 0; then
19751c586b8Smrg  if test -z "$dir_arg"; then
19851c586b8Smrg    echo "$0: no input file specified." >&2
19951c586b8Smrg    exit 1
20051c586b8Smrg  fi
201*ce543368Smrg  # It's OK to call 'install-sh -d' without argument.
20251c586b8Smrg  # This can happen when creating conditional directories.
20351c586b8Smrg  exit 0
20451c586b8Smrgfi
20551c586b8Smrg
206*ce543368Smrgif test -z "$dir_arg"; then
207*ce543368Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208*ce543368Smrg    if test ! -d "$dst_arg"; then
209*ce543368Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210*ce543368Smrg      exit 1
211*ce543368Smrg    fi
212*ce543368Smrg  fi
213*ce543368Smrgfi
214*ce543368Smrg
215*ce543368Smrgif test -z "$dir_arg"; then
216*ce543368Smrg  do_exit='(exit $ret); exit $ret'
217*ce543368Smrg  trap "ret=129; $do_exit" 1
218*ce543368Smrg  trap "ret=130; $do_exit" 2
219*ce543368Smrg  trap "ret=141; $do_exit" 13
220*ce543368Smrg  trap "ret=143; $do_exit" 15
221*ce543368Smrg
222*ce543368Smrg  # Set umask so as not to create temps with too-generous modes.
223*ce543368Smrg  # However, 'strip' requires both read and write access to temps.
224*ce543368Smrg  case $mode in
225*ce543368Smrg    # Optimize common cases.
226*ce543368Smrg    *644) cp_umask=133;;
227*ce543368Smrg    *755) cp_umask=22;;
228*ce543368Smrg
229*ce543368Smrg    *[0-7])
230*ce543368Smrg      if test -z "$stripcmd"; then
231*ce543368Smrg        u_plus_rw=
232*ce543368Smrg      else
233*ce543368Smrg        u_plus_rw='% 200'
234*ce543368Smrg      fi
235*ce543368Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236*ce543368Smrg    *)
237*ce543368Smrg      if test -z "$stripcmd"; then
238*ce543368Smrg        u_plus_rw=
239*ce543368Smrg      else
240*ce543368Smrg        u_plus_rw=,u+rw
241*ce543368Smrg      fi
242*ce543368Smrg      cp_umask=$mode$u_plus_rw;;
243*ce543368Smrg  esac
244*ce543368Smrgfi
245*ce543368Smrg
24651c586b8Smrgfor src
24751c586b8Smrgdo
248*ce543368Smrg  # Protect names problematic for 'test' and other utilities.
24951c586b8Smrg  case $src in
250*ce543368Smrg    -* | [=\(\)!]) src=./$src;;
25151c586b8Smrg  esac
25251c586b8Smrg
25351c586b8Smrg  if test -n "$dir_arg"; then
25451c586b8Smrg    dst=$src
255*ce543368Smrg    dstdir=$dst
256*ce543368Smrg    test -d "$dstdir"
257*ce543368Smrg    dstdir_status=$?
258*ce543368Smrg  else
25951c586b8Smrg
260*ce543368Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26151c586b8Smrg    # might cause directories to be created, which would be especially bad
26251c586b8Smrg    # if $src (and thus $dsttmp) contains '*'.
26351c586b8Smrg    if test ! -f "$src" && test ! -d "$src"; then
26451c586b8Smrg      echo "$0: $src does not exist." >&2
26551c586b8Smrg      exit 1
26651c586b8Smrg    fi
26751c586b8Smrg
268*ce543368Smrg    if test -z "$dst_arg"; then
26951c586b8Smrg      echo "$0: no destination specified." >&2
27051c586b8Smrg      exit 1
27151c586b8Smrg    fi
272*ce543368Smrg    dst=$dst_arg
27351c586b8Smrg
27451c586b8Smrg    # If destination is a directory, append the input filename; won't work
27551c586b8Smrg    # if double slashes aren't ignored.
27651c586b8Smrg    if test -d "$dst"; then
277*ce543368Smrg      if test "$is_target_a_directory" = never; then
278*ce543368Smrg        echo "$0: $dst_arg: Is a directory" >&2
279*ce543368Smrg        exit 1
280*ce543368Smrg      fi
281*ce543368Smrg      dstdir=$dst
282*ce543368Smrg      dst=$dstdir/`basename "$src"`
283*ce543368Smrg      dstdir_status=0
284*ce543368Smrg    else
285*ce543368Smrg      dstdir=`dirname "$dst"`
286*ce543368Smrg      test -d "$dstdir"
287*ce543368Smrg      dstdir_status=$?
28851c586b8Smrg    fi
28951c586b8Smrg  fi
29051c586b8Smrg
291*ce543368Smrg  obsolete_mkdir_used=false
29251c586b8Smrg
293*ce543368Smrg  if test $dstdir_status != 0; then
294*ce543368Smrg    case $posix_mkdir in
295*ce543368Smrg      '')
296*ce543368Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297*ce543368Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298*ce543368Smrg        umask=`umask`
299*ce543368Smrg        case $stripcmd.$umask in
300*ce543368Smrg          # Optimize common cases.
301*ce543368Smrg          *[2367][2367]) mkdir_umask=$umask;;
302*ce543368Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
30351c586b8Smrg
304*ce543368Smrg          *[0-7])
305*ce543368Smrg            mkdir_umask=`expr $umask + 22 \
306*ce543368Smrg              - $umask % 100 % 40 + $umask % 20 \
307*ce543368Smrg              - $umask % 10 % 4 + $umask % 2
308*ce543368Smrg            `;;
309*ce543368Smrg          *) mkdir_umask=$umask,go-w;;
310*ce543368Smrg        esac
311*ce543368Smrg
312*ce543368Smrg        # With -d, create the new directory with the user-specified mode.
313*ce543368Smrg        # Otherwise, rely on $mkdir_umask.
314*ce543368Smrg        if test -n "$dir_arg"; then
315*ce543368Smrg          mkdir_mode=-m$mode
316*ce543368Smrg        else
317*ce543368Smrg          mkdir_mode=
318*ce543368Smrg        fi
319*ce543368Smrg
320*ce543368Smrg        posix_mkdir=false
321*ce543368Smrg        case $umask in
322*ce543368Smrg          *[123567][0-7][0-7])
323*ce543368Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324*ce543368Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325*ce543368Smrg            ;;
326*ce543368Smrg          *)
327*ce543368Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328*ce543368Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329*ce543368Smrg
330*ce543368Smrg            if (umask $mkdir_umask &&
331*ce543368Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332*ce543368Smrg            then
333*ce543368Smrg              if test -z "$dir_arg" || {
334*ce543368Smrg                   # Check for POSIX incompatibilities with -m.
335*ce543368Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336*ce543368Smrg                   # other-writable bit of parent directory when it shouldn't.
337*ce543368Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338*ce543368Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339*ce543368Smrg                   case $ls_ld_tmpdir in
340*ce543368Smrg                     d????-?r-*) different_mode=700;;
341*ce543368Smrg                     d????-?--*) different_mode=755;;
342*ce543368Smrg                     *) false;;
343*ce543368Smrg                   esac &&
344*ce543368Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345*ce543368Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346*ce543368Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347*ce543368Smrg                   }
348*ce543368Smrg                 }
349*ce543368Smrg              then posix_mkdir=:
350*ce543368Smrg              fi
351*ce543368Smrg              rmdir "$tmpdir/d" "$tmpdir"
352*ce543368Smrg            else
353*ce543368Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354*ce543368Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355*ce543368Smrg            fi
356*ce543368Smrg            trap '' 0;;
357*ce543368Smrg        esac;;
358*ce543368Smrg    esac
359*ce543368Smrg
360*ce543368Smrg    if
361*ce543368Smrg      $posix_mkdir && (
362*ce543368Smrg        umask $mkdir_umask &&
363*ce543368Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364*ce543368Smrg      )
365*ce543368Smrg    then :
366*ce543368Smrg    else
367*ce543368Smrg
368*ce543368Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369*ce543368Smrg      # or it failed possibly due to a race condition.  Create the
370*ce543368Smrg      # directory the slow way, step by step, checking for races as we go.
371*ce543368Smrg
372*ce543368Smrg      case $dstdir in
373*ce543368Smrg        /*) prefix='/';;
374*ce543368Smrg        [-=\(\)!]*) prefix='./';;
375*ce543368Smrg        *)  prefix='';;
376*ce543368Smrg      esac
37751c586b8Smrg
37851c586b8Smrg      oIFS=$IFS
379*ce543368Smrg      IFS=/
380*ce543368Smrg      set -f
381*ce543368Smrg      set fnord $dstdir
382*ce543368Smrg      shift
383*ce543368Smrg      set +f
38451c586b8Smrg      IFS=$oIFS
38551c586b8Smrg
386*ce543368Smrg      prefixes=
38751c586b8Smrg
388*ce543368Smrg      for d
389*ce543368Smrg      do
390*ce543368Smrg        test X"$d" = X && continue
391*ce543368Smrg
392*ce543368Smrg        prefix=$prefix$d
393*ce543368Smrg        if test -d "$prefix"; then
394*ce543368Smrg          prefixes=
395*ce543368Smrg        else
396*ce543368Smrg          if $posix_mkdir; then
397*ce543368Smrg            (umask=$mkdir_umask &&
398*ce543368Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399*ce543368Smrg            # Don't fail if two instances are running concurrently.
400*ce543368Smrg            test -d "$prefix" || exit 1
401*ce543368Smrg          else
402*ce543368Smrg            case $prefix in
403*ce543368Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404*ce543368Smrg              *) qprefix=$prefix;;
405*ce543368Smrg            esac
406*ce543368Smrg            prefixes="$prefixes '$qprefix'"
40751c586b8Smrg          fi
408*ce543368Smrg        fi
409*ce543368Smrg        prefix=$prefix/
41051c586b8Smrg      done
411*ce543368Smrg
412*ce543368Smrg      if test -n "$prefixes"; then
413*ce543368Smrg        # Don't fail if two instances are running concurrently.
414*ce543368Smrg        (umask $mkdir_umask &&
415*ce543368Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416*ce543368Smrg          test -d "$dstdir" || exit 1
417*ce543368Smrg        obsolete_mkdir_used=true
418*ce543368Smrg      fi
419*ce543368Smrg    fi
42051c586b8Smrg  fi
42151c586b8Smrg
42251c586b8Smrg  if test -n "$dir_arg"; then
423*ce543368Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424*ce543368Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425*ce543368Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426*ce543368Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
42751c586b8Smrg  else
42851c586b8Smrg
42951c586b8Smrg    # Make a couple of temp file names in the proper directory.
43051c586b8Smrg    dsttmp=$dstdir/_inst.$$_
43151c586b8Smrg    rmtmp=$dstdir/_rm.$$_
43251c586b8Smrg
43351c586b8Smrg    # Trap to clean up those temp files at exit.
434*ce543368Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
43551c586b8Smrg
436*ce543368Smrg    # Copy the file name to the temp name.
437*ce543368Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
43851c586b8Smrg
43951c586b8Smrg    # and set any options; do chmod last to preserve setuid bits.
44051c586b8Smrg    #
44151c586b8Smrg    # If any of these fail, we abort the whole thing.  If we want to
44251c586b8Smrg    # ignore errors from any of these, just make sure not to ignore
443*ce543368Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
44451c586b8Smrg    #
445*ce543368Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446*ce543368Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447*ce543368Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448*ce543368Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
44951c586b8Smrg
450*ce543368Smrg    # If -C, don't bother to copy if it wouldn't change the file.
451*ce543368Smrg    if $copy_on_change &&
452*ce543368Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453*ce543368Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454*ce543368Smrg       set -f &&
455*ce543368Smrg       set X $old && old=:$2:$4:$5:$6 &&
456*ce543368Smrg       set X $new && new=:$2:$4:$5:$6 &&
457*ce543368Smrg       set +f &&
458*ce543368Smrg       test "$old" = "$new" &&
459*ce543368Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460*ce543368Smrg    then
461*ce543368Smrg      rm -f "$dsttmp"
462*ce543368Smrg    else
463*ce543368Smrg      # Rename the file to the real destination.
464*ce543368Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465*ce543368Smrg
46651c586b8Smrg      # The rename failed, perhaps because mv can't rename something else
46751c586b8Smrg      # to itself, or perhaps because mv is so ancient that it does not
46851c586b8Smrg      # support -f.
469*ce543368Smrg      {
47051c586b8Smrg        # Now remove or move aside any old file at destination location.
47151c586b8Smrg        # We try this two ways since rm can't unlink itself on some
47251c586b8Smrg        # systems and the destination file might be busy for other
47351c586b8Smrg        # reasons.  In this case, the final cleanup might fail but the new
47451c586b8Smrg        # file should still install successfully.
47551c586b8Smrg        {
476*ce543368Smrg          test ! -f "$dst" ||
477*ce543368Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478*ce543368Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479*ce543368Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480*ce543368Smrg          } ||
481*ce543368Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482*ce543368Smrg            (exit 1); exit 1
48351c586b8Smrg          }
48451c586b8Smrg        } &&
48551c586b8Smrg
48651c586b8Smrg        # Now rename the file to the real destination.
487*ce543368Smrg        $doit $mvcmd "$dsttmp" "$dst"
48851c586b8Smrg      }
489*ce543368Smrg    fi || exit 1
49051c586b8Smrg
491*ce543368Smrg    trap '' 0
492*ce543368Smrg  fi
493*ce543368Smrgdone
49451c586b8Smrg
49551c586b8Smrg# Local variables:
49651c586b8Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
49751c586b8Smrg# time-stamp-start: "scriptversion="
49851c586b8Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499*ce543368Smrg# time-stamp-time-zone: "UTC"
500*ce543368Smrg# time-stamp-end: "; # UTC"
50151c586b8Smrg# End:
502