123a0898aSmrg#!/bin/sh
223a0898aSmrg# install - install a program, script, or datafile
323a0898aSmrg
4a97c3f35Smrgscriptversion=2014-09-12.12; # UTC
523a0898aSmrg
623a0898aSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
723a0898aSmrg# later released in X11R6 (xc/config/util/install.sh) with the
823a0898aSmrg# following copyright and license.
923a0898aSmrg#
1023a0898aSmrg# Copyright (C) 1994 X Consortium
1123a0898aSmrg#
1223a0898aSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1323a0898aSmrg# of this software and associated documentation files (the "Software"), to
1423a0898aSmrg# deal in the Software without restriction, including without limitation the
1523a0898aSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1623a0898aSmrg# sell copies of the Software, and to permit persons to whom the Software is
1723a0898aSmrg# furnished to do so, subject to the following conditions:
1823a0898aSmrg#
1923a0898aSmrg# The above copyright notice and this permission notice shall be included in
2023a0898aSmrg# all copies or substantial portions of the Software.
2123a0898aSmrg#
2223a0898aSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2323a0898aSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2423a0898aSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2523a0898aSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2623a0898aSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2723a0898aSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2823a0898aSmrg#
2923a0898aSmrg# Except as contained in this notice, the name of the X Consortium shall not
3023a0898aSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3123a0898aSmrg# ings in this Software without prior written authorization from the X Consor-
3223a0898aSmrg# tium.
3323a0898aSmrg#
3423a0898aSmrg#
3523a0898aSmrg# FSF changes to this file are in the public domain.
3623a0898aSmrg#
3723a0898aSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38b020570bSmrg# 'make' implicit rules from creating a file called install from it
3923a0898aSmrg# when there is no Makefile.
4023a0898aSmrg#
4123a0898aSmrg# This script is compatible with the BSD install script, but was written
4223a0898aSmrg# from scratch.
4323a0898aSmrg
44121a5469Smrgtab='	'
4523a0898aSmrgnl='
4623a0898aSmrg'
47121a5469SmrgIFS=" $tab$nl"
4823a0898aSmrg
49121a5469Smrg# Set DOITPROG to "echo" to test this script.
5023a0898aSmrg
5123a0898aSmrgdoit=${DOITPROG-}
52121a5469Smrgdoit_exec=${doit:-exec}
5323a0898aSmrg
5423a0898aSmrg# Put in absolute file names if you don't have them in your path;
5523a0898aSmrg# or use environment vars.
5623a0898aSmrg
5723a0898aSmrgchgrpprog=${CHGRPPROG-chgrp}
5823a0898aSmrgchmodprog=${CHMODPROG-chmod}
5923a0898aSmrgchownprog=${CHOWNPROG-chown}
6023a0898aSmrgcmpprog=${CMPPROG-cmp}
6123a0898aSmrgcpprog=${CPPROG-cp}
6223a0898aSmrgmkdirprog=${MKDIRPROG-mkdir}
6323a0898aSmrgmvprog=${MVPROG-mv}
6423a0898aSmrgrmprog=${RMPROG-rm}
6523a0898aSmrgstripprog=${STRIPPROG-strip}
6623a0898aSmrg
6723a0898aSmrgposix_mkdir=
6823a0898aSmrg
6923a0898aSmrg# Desired mode of installed file.
7023a0898aSmrgmode=0755
7123a0898aSmrg
7223a0898aSmrgchgrpcmd=
7323a0898aSmrgchmodcmd=$chmodprog
7423a0898aSmrgchowncmd=
7523a0898aSmrgmvcmd=$mvprog
7623a0898aSmrgrmcmd="$rmprog -f"
7723a0898aSmrgstripcmd=
7823a0898aSmrg
7923a0898aSmrgsrc=
8023a0898aSmrgdst=
8123a0898aSmrgdir_arg=
8223a0898aSmrgdst_arg=
8323a0898aSmrg
8423a0898aSmrgcopy_on_change=false
85121a5469Smrgis_target_a_directory=possibly
8623a0898aSmrg
8723a0898aSmrgusage="\
8823a0898aSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8923a0898aSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9023a0898aSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9123a0898aSmrg   or: $0 [OPTION]... -d DIRECTORIES...
9223a0898aSmrg
9323a0898aSmrgIn the 1st form, copy SRCFILE to DSTFILE.
9423a0898aSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9523a0898aSmrgIn the 4th, create DIRECTORIES.
9623a0898aSmrg
9723a0898aSmrgOptions:
9823a0898aSmrg     --help     display this help and exit.
9923a0898aSmrg     --version  display version info and exit.
10023a0898aSmrg
10123a0898aSmrg  -c            (ignored)
10223a0898aSmrg  -C            install only if different (preserve the last data modification time)
10323a0898aSmrg  -d            create directories instead of installing files.
10423a0898aSmrg  -g GROUP      $chgrpprog installed files to GROUP.
10523a0898aSmrg  -m MODE       $chmodprog installed files to MODE.
10623a0898aSmrg  -o USER       $chownprog installed files to USER.
10723a0898aSmrg  -s            $stripprog installed files.
10823a0898aSmrg  -t DIRECTORY  install into DIRECTORY.
10923a0898aSmrg  -T            report an error if DSTFILE is a directory.
11023a0898aSmrg
11123a0898aSmrgEnvironment variables override the default commands:
11223a0898aSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
11323a0898aSmrg  RMPROG STRIPPROG
11423a0898aSmrg"
11523a0898aSmrg
11623a0898aSmrgwhile test $# -ne 0; do
11723a0898aSmrg  case $1 in
11823a0898aSmrg    -c) ;;
11923a0898aSmrg
12023a0898aSmrg    -C) copy_on_change=true;;
12123a0898aSmrg
12223a0898aSmrg    -d) dir_arg=true;;
12323a0898aSmrg
12423a0898aSmrg    -g) chgrpcmd="$chgrpprog $2"
125121a5469Smrg        shift;;
12623a0898aSmrg
12723a0898aSmrg    --help) echo "$usage"; exit $?;;
12823a0898aSmrg
12923a0898aSmrg    -m) mode=$2
130121a5469Smrg        case $mode in
131121a5469Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132121a5469Smrg            echo "$0: invalid mode: $mode" >&2
133121a5469Smrg            exit 1;;
134121a5469Smrg        esac
135121a5469Smrg        shift;;
13623a0898aSmrg
13723a0898aSmrg    -o) chowncmd="$chownprog $2"
138121a5469Smrg        shift;;
13923a0898aSmrg
14023a0898aSmrg    -s) stripcmd=$stripprog;;
14123a0898aSmrg
142121a5469Smrg    -t)
143121a5469Smrg        is_target_a_directory=always
144121a5469Smrg        dst_arg=$2
145121a5469Smrg        # Protect names problematic for 'test' and other utilities.
146121a5469Smrg        case $dst_arg in
147121a5469Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148121a5469Smrg        esac
149121a5469Smrg        shift;;
15023a0898aSmrg
151121a5469Smrg    -T) is_target_a_directory=never;;
15223a0898aSmrg
15323a0898aSmrg    --version) echo "$0 $scriptversion"; exit $?;;
15423a0898aSmrg
155121a5469Smrg    --) shift
156121a5469Smrg        break;;
15723a0898aSmrg
158121a5469Smrg    -*) echo "$0: invalid option: $1" >&2
159121a5469Smrg        exit 1;;
16023a0898aSmrg
16123a0898aSmrg    *)  break;;
16223a0898aSmrg  esac
16323a0898aSmrg  shift
16423a0898aSmrgdone
16523a0898aSmrg
166121a5469Smrg# We allow the use of options -d and -T together, by making -d
167121a5469Smrg# take the precedence; this is for compatibility with GNU install.
168121a5469Smrg
169121a5469Smrgif test -n "$dir_arg"; then
170121a5469Smrg  if test -n "$dst_arg"; then
171121a5469Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172121a5469Smrg    exit 1
173121a5469Smrg  fi
174121a5469Smrgfi
175121a5469Smrg
17623a0898aSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17723a0898aSmrg  # When -d is used, all remaining arguments are directories to create.
17823a0898aSmrg  # When -t is used, the destination is already specified.
17923a0898aSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18023a0898aSmrg  for arg
18123a0898aSmrg  do
18223a0898aSmrg    if test -n "$dst_arg"; then
18323a0898aSmrg      # $@ is not empty: it contains at least $arg.
18423a0898aSmrg      set fnord "$@" "$dst_arg"
18523a0898aSmrg      shift # fnord
18623a0898aSmrg    fi
18723a0898aSmrg    shift # arg
18823a0898aSmrg    dst_arg=$arg
189b020570bSmrg    # Protect names problematic for 'test' and other utilities.
190b020570bSmrg    case $dst_arg in
191b020570bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192b020570bSmrg    esac
19323a0898aSmrg  done
19423a0898aSmrgfi
19523a0898aSmrg
19623a0898aSmrgif test $# -eq 0; then
19723a0898aSmrg  if test -z "$dir_arg"; then
19823a0898aSmrg    echo "$0: no input file specified." >&2
19923a0898aSmrg    exit 1
20023a0898aSmrg  fi
201b020570bSmrg  # It's OK to call 'install-sh -d' without argument.
20223a0898aSmrg  # This can happen when creating conditional directories.
20323a0898aSmrg  exit 0
20423a0898aSmrgfi
20523a0898aSmrg
206121a5469Smrgif test -z "$dir_arg"; then
207121a5469Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208121a5469Smrg    if test ! -d "$dst_arg"; then
209121a5469Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210121a5469Smrg      exit 1
211121a5469Smrg    fi
212121a5469Smrg  fi
213121a5469Smrgfi
214121a5469Smrg
21523a0898aSmrgif test -z "$dir_arg"; then
216b020570bSmrg  do_exit='(exit $ret); exit $ret'
217b020570bSmrg  trap "ret=129; $do_exit" 1
218b020570bSmrg  trap "ret=130; $do_exit" 2
219b020570bSmrg  trap "ret=141; $do_exit" 13
220b020570bSmrg  trap "ret=143; $do_exit" 15
22123a0898aSmrg
22223a0898aSmrg  # Set umask so as not to create temps with too-generous modes.
22323a0898aSmrg  # However, 'strip' requires both read and write access to temps.
22423a0898aSmrg  case $mode in
22523a0898aSmrg    # Optimize common cases.
22623a0898aSmrg    *644) cp_umask=133;;
22723a0898aSmrg    *755) cp_umask=22;;
22823a0898aSmrg
22923a0898aSmrg    *[0-7])
23023a0898aSmrg      if test -z "$stripcmd"; then
231121a5469Smrg        u_plus_rw=
23223a0898aSmrg      else
233121a5469Smrg        u_plus_rw='% 200'
23423a0898aSmrg      fi
23523a0898aSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23623a0898aSmrg    *)
23723a0898aSmrg      if test -z "$stripcmd"; then
238121a5469Smrg        u_plus_rw=
23923a0898aSmrg      else
240121a5469Smrg        u_plus_rw=,u+rw
24123a0898aSmrg      fi
24223a0898aSmrg      cp_umask=$mode$u_plus_rw;;
24323a0898aSmrg  esac
24423a0898aSmrgfi
24523a0898aSmrg
24623a0898aSmrgfor src
24723a0898aSmrgdo
248b020570bSmrg  # Protect names problematic for 'test' and other utilities.
24923a0898aSmrg  case $src in
250b020570bSmrg    -* | [=\(\)!]) src=./$src;;
25123a0898aSmrg  esac
25223a0898aSmrg
25323a0898aSmrg  if test -n "$dir_arg"; then
25423a0898aSmrg    dst=$src
25523a0898aSmrg    dstdir=$dst
25623a0898aSmrg    test -d "$dstdir"
25723a0898aSmrg    dstdir_status=$?
25823a0898aSmrg  else
25923a0898aSmrg
26023a0898aSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26123a0898aSmrg    # might cause directories to be created, which would be especially bad
26223a0898aSmrg    # if $src (and thus $dsttmp) contains '*'.
26323a0898aSmrg    if test ! -f "$src" && test ! -d "$src"; then
26423a0898aSmrg      echo "$0: $src does not exist." >&2
26523a0898aSmrg      exit 1
26623a0898aSmrg    fi
26723a0898aSmrg
26823a0898aSmrg    if test -z "$dst_arg"; then
26923a0898aSmrg      echo "$0: no destination specified." >&2
27023a0898aSmrg      exit 1
27123a0898aSmrg    fi
27223a0898aSmrg    dst=$dst_arg
27323a0898aSmrg
27423a0898aSmrg    # If destination is a directory, append the input filename; won't work
27523a0898aSmrg    # if double slashes aren't ignored.
27623a0898aSmrg    if test -d "$dst"; then
277121a5469Smrg      if test "$is_target_a_directory" = never; then
278121a5469Smrg        echo "$0: $dst_arg: Is a directory" >&2
279121a5469Smrg        exit 1
28023a0898aSmrg      fi
28123a0898aSmrg      dstdir=$dst
28223a0898aSmrg      dst=$dstdir/`basename "$src"`
28323a0898aSmrg      dstdir_status=0
28423a0898aSmrg    else
285121a5469Smrg      dstdir=`dirname "$dst"`
28623a0898aSmrg      test -d "$dstdir"
28723a0898aSmrg      dstdir_status=$?
28823a0898aSmrg    fi
28923a0898aSmrg  fi
29023a0898aSmrg
29123a0898aSmrg  obsolete_mkdir_used=false
29223a0898aSmrg
29323a0898aSmrg  if test $dstdir_status != 0; then
29423a0898aSmrg    case $posix_mkdir in
29523a0898aSmrg      '')
296121a5469Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297121a5469Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298121a5469Smrg        umask=`umask`
299121a5469Smrg        case $stripcmd.$umask in
300121a5469Smrg          # Optimize common cases.
301121a5469Smrg          *[2367][2367]) mkdir_umask=$umask;;
302121a5469Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303121a5469Smrg
304121a5469Smrg          *[0-7])
305121a5469Smrg            mkdir_umask=`expr $umask + 22 \
306121a5469Smrg              - $umask % 100 % 40 + $umask % 20 \
307121a5469Smrg              - $umask % 10 % 4 + $umask % 2
308121a5469Smrg            `;;
309121a5469Smrg          *) mkdir_umask=$umask,go-w;;
310121a5469Smrg        esac
311121a5469Smrg
312121a5469Smrg        # With -d, create the new directory with the user-specified mode.
313121a5469Smrg        # Otherwise, rely on $mkdir_umask.
314121a5469Smrg        if test -n "$dir_arg"; then
315121a5469Smrg          mkdir_mode=-m$mode
316121a5469Smrg        else
317121a5469Smrg          mkdir_mode=
318121a5469Smrg        fi
319121a5469Smrg
320121a5469Smrg        posix_mkdir=false
321121a5469Smrg        case $umask in
322121a5469Smrg          *[123567][0-7][0-7])
323121a5469Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324121a5469Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325121a5469Smrg            ;;
326121a5469Smrg          *)
327a97c3f35Smrg            # $RANDOM is not portable (e.g. dash);  use it when possible to
328a97c3f35Smrg            # lower collision chance
329121a5469Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
330a97c3f35Smrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
331121a5469Smrg
332a97c3f35Smrg            # As "mkdir -p" follows symlinks and we work in /tmp possibly;  so
333a97c3f35Smrg            # create the $tmpdir first (and fail if unsuccessful) to make sure
334a97c3f35Smrg            # that nobody tries to guess the $tmpdir name.
335121a5469Smrg            if (umask $mkdir_umask &&
336a97c3f35Smrg                $mkdirprog $mkdir_mode "$tmpdir" &&
337a97c3f35Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
338121a5469Smrg            then
339121a5469Smrg              if test -z "$dir_arg" || {
340121a5469Smrg                   # Check for POSIX incompatibilities with -m.
341121a5469Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
342121a5469Smrg                   # other-writable bit of parent directory when it shouldn't.
343121a5469Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
344a97c3f35Smrg                   test_tmpdir="$tmpdir/a"
345a97c3f35Smrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
346121a5469Smrg                   case $ls_ld_tmpdir in
347121a5469Smrg                     d????-?r-*) different_mode=700;;
348121a5469Smrg                     d????-?--*) different_mode=755;;
349121a5469Smrg                     *) false;;
350121a5469Smrg                   esac &&
351a97c3f35Smrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
352a97c3f35Smrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
353121a5469Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
354121a5469Smrg                   }
355121a5469Smrg                 }
356121a5469Smrg              then posix_mkdir=:
357121a5469Smrg              fi
358a97c3f35Smrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
359121a5469Smrg            else
360121a5469Smrg              # Remove any dirs left behind by ancient mkdir implementations.
361a97c3f35Smrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
362121a5469Smrg            fi
363121a5469Smrg            trap '' 0;;
364121a5469Smrg        esac;;
36523a0898aSmrg    esac
36623a0898aSmrg
36723a0898aSmrg    if
36823a0898aSmrg      $posix_mkdir && (
369121a5469Smrg        umask $mkdir_umask &&
370121a5469Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37123a0898aSmrg      )
37223a0898aSmrg    then :
37323a0898aSmrg    else
37423a0898aSmrg
37523a0898aSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
37623a0898aSmrg      # or it failed possibly due to a race condition.  Create the
37723a0898aSmrg      # directory the slow way, step by step, checking for races as we go.
37823a0898aSmrg
37923a0898aSmrg      case $dstdir in
380121a5469Smrg        /*) prefix='/';;
381121a5469Smrg        [-=\(\)!]*) prefix='./';;
382121a5469Smrg        *)  prefix='';;
38323a0898aSmrg      esac
38423a0898aSmrg
38523a0898aSmrg      oIFS=$IFS
38623a0898aSmrg      IFS=/
387121a5469Smrg      set -f
38823a0898aSmrg      set fnord $dstdir
38923a0898aSmrg      shift
390121a5469Smrg      set +f
39123a0898aSmrg      IFS=$oIFS
39223a0898aSmrg
39323a0898aSmrg      prefixes=
39423a0898aSmrg
39523a0898aSmrg      for d
39623a0898aSmrg      do
397121a5469Smrg        test X"$d" = X && continue
398121a5469Smrg
399121a5469Smrg        prefix=$prefix$d
400121a5469Smrg        if test -d "$prefix"; then
401121a5469Smrg          prefixes=
402121a5469Smrg        else
403121a5469Smrg          if $posix_mkdir; then
404121a5469Smrg            (umask=$mkdir_umask &&
405121a5469Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
406121a5469Smrg            # Don't fail if two instances are running concurrently.
407121a5469Smrg            test -d "$prefix" || exit 1
408121a5469Smrg          else
409121a5469Smrg            case $prefix in
410121a5469Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
411121a5469Smrg              *) qprefix=$prefix;;
412121a5469Smrg            esac
413121a5469Smrg            prefixes="$prefixes '$qprefix'"
414121a5469Smrg          fi
415121a5469Smrg        fi
416121a5469Smrg        prefix=$prefix/
41723a0898aSmrg      done
41823a0898aSmrg
41923a0898aSmrg      if test -n "$prefixes"; then
420121a5469Smrg        # Don't fail if two instances are running concurrently.
421121a5469Smrg        (umask $mkdir_umask &&
422121a5469Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
423121a5469Smrg          test -d "$dstdir" || exit 1
424121a5469Smrg        obsolete_mkdir_used=true
42523a0898aSmrg      fi
42623a0898aSmrg    fi
42723a0898aSmrg  fi
42823a0898aSmrg
42923a0898aSmrg  if test -n "$dir_arg"; then
43023a0898aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
43123a0898aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
43223a0898aSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
43323a0898aSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
43423a0898aSmrg  else
43523a0898aSmrg
43623a0898aSmrg    # Make a couple of temp file names in the proper directory.
43723a0898aSmrg    dsttmp=$dstdir/_inst.$$_
43823a0898aSmrg    rmtmp=$dstdir/_rm.$$_
43923a0898aSmrg
44023a0898aSmrg    # Trap to clean up those temp files at exit.
44123a0898aSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
44223a0898aSmrg
44323a0898aSmrg    # Copy the file name to the temp name.
44423a0898aSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
44523a0898aSmrg
44623a0898aSmrg    # and set any options; do chmod last to preserve setuid bits.
44723a0898aSmrg    #
44823a0898aSmrg    # If any of these fail, we abort the whole thing.  If we want to
44923a0898aSmrg    # ignore errors from any of these, just make sure not to ignore
45023a0898aSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
45123a0898aSmrg    #
45223a0898aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
45323a0898aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
45423a0898aSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
45523a0898aSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
45623a0898aSmrg
45723a0898aSmrg    # If -C, don't bother to copy if it wouldn't change the file.
45823a0898aSmrg    if $copy_on_change &&
459121a5469Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
460121a5469Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
461121a5469Smrg       set -f &&
46223a0898aSmrg       set X $old && old=:$2:$4:$5:$6 &&
46323a0898aSmrg       set X $new && new=:$2:$4:$5:$6 &&
464121a5469Smrg       set +f &&
46523a0898aSmrg       test "$old" = "$new" &&
46623a0898aSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
46723a0898aSmrg    then
46823a0898aSmrg      rm -f "$dsttmp"
46923a0898aSmrg    else
47023a0898aSmrg      # Rename the file to the real destination.
47123a0898aSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
47223a0898aSmrg
47323a0898aSmrg      # The rename failed, perhaps because mv can't rename something else
47423a0898aSmrg      # to itself, or perhaps because mv is so ancient that it does not
47523a0898aSmrg      # support -f.
47623a0898aSmrg      {
477121a5469Smrg        # Now remove or move aside any old file at destination location.
478121a5469Smrg        # We try this two ways since rm can't unlink itself on some
479121a5469Smrg        # systems and the destination file might be busy for other
480121a5469Smrg        # reasons.  In this case, the final cleanup might fail but the new
481121a5469Smrg        # file should still install successfully.
482121a5469Smrg        {
483121a5469Smrg          test ! -f "$dst" ||
484121a5469Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
485121a5469Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
486121a5469Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
487121a5469Smrg          } ||
488121a5469Smrg          { echo "$0: cannot unlink or rename $dst" >&2
489121a5469Smrg            (exit 1); exit 1
490121a5469Smrg          }
491121a5469Smrg        } &&
492121a5469Smrg
493121a5469Smrg        # Now rename the file to the real destination.
494121a5469Smrg        $doit $mvcmd "$dsttmp" "$dst"
49523a0898aSmrg      }
49623a0898aSmrg    fi || exit 1
49723a0898aSmrg
49823a0898aSmrg    trap '' 0
49923a0898aSmrg  fi
50023a0898aSmrgdone
50123a0898aSmrg
50223a0898aSmrg# Local variables:
50323a0898aSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
50423a0898aSmrg# time-stamp-start: "scriptversion="
50523a0898aSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
50641c30155Smrg# time-stamp-time-zone: "UTC"
50741c30155Smrg# time-stamp-end: "; # UTC"
50823a0898aSmrg# End:
509