install-sh revision 2f167a42
10e20ee16Smrg#!/bin/sh
20e20ee16Smrg# install - install a program, script, or datafile
33e6697d4Smrg
42f167a42Smrgscriptversion=2013-12-25.23; # UTC
53e6697d4Smrg
60e20ee16Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
70e20ee16Smrg# later released in X11R6 (xc/config/util/install.sh) with the
80e20ee16Smrg# following copyright and license.
90e20ee16Smrg#
100e20ee16Smrg# Copyright (C) 1994 X Consortium
110e20ee16Smrg#
120e20ee16Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
130e20ee16Smrg# of this software and associated documentation files (the "Software"), to
140e20ee16Smrg# deal in the Software without restriction, including without limitation the
150e20ee16Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
160e20ee16Smrg# sell copies of the Software, and to permit persons to whom the Software is
170e20ee16Smrg# furnished to do so, subject to the following conditions:
180e20ee16Smrg#
190e20ee16Smrg# The above copyright notice and this permission notice shall be included in
200e20ee16Smrg# all copies or substantial portions of the Software.
210e20ee16Smrg#
220e20ee16Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
230e20ee16Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
240e20ee16Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
250e20ee16Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
260e20ee16Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
270e20ee16Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
280e20ee16Smrg#
290e20ee16Smrg# Except as contained in this notice, the name of the X Consortium shall not
300e20ee16Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
310e20ee16Smrg# ings in this Software without prior written authorization from the X Consor-
320e20ee16Smrg# tium.
330e20ee16Smrg#
340e20ee16Smrg#
350e20ee16Smrg# FSF changes to this file are in the public domain.
360e20ee16Smrg#
370e20ee16Smrg# Calling this script install-sh is preferred over install.sh, to prevent
382f167a42Smrg# 'make' implicit rules from creating a file called install from it
390e20ee16Smrg# when there is no Makefile.
400e20ee16Smrg#
410e20ee16Smrg# This script is compatible with the BSD install script, but was written
423e6697d4Smrg# from scratch.
430e20ee16Smrg
442f167a42Smrgtab='	'
453e6697d4Smrgnl='
463e6697d4Smrg'
472f167a42SmrgIFS=" $tab$nl"
480e20ee16Smrg
492f167a42Smrg# Set DOITPROG to "echo" to test this script.
500e20ee16Smrg
513e6697d4Smrgdoit=${DOITPROG-}
522f167a42Smrgdoit_exec=${doit:-exec}
530e20ee16Smrg
543e6697d4Smrg# Put in absolute file names if you don't have them in your path;
553e6697d4Smrg# or use environment vars.
563e6697d4Smrg
573e6697d4Smrgchgrpprog=${CHGRPPROG-chgrp}
583e6697d4Smrgchmodprog=${CHMODPROG-chmod}
593e6697d4Smrgchownprog=${CHOWNPROG-chown}
603e6697d4Smrgcmpprog=${CMPPROG-cmp}
613e6697d4Smrgcpprog=${CPPROG-cp}
623e6697d4Smrgmkdirprog=${MKDIRPROG-mkdir}
633e6697d4Smrgmvprog=${MVPROG-mv}
643e6697d4Smrgrmprog=${RMPROG-rm}
653e6697d4Smrgstripprog=${STRIPPROG-strip}
663e6697d4Smrg
673e6697d4Smrgposix_mkdir=
683e6697d4Smrg
693e6697d4Smrg# Desired mode of installed file.
703e6697d4Smrgmode=0755
713e6697d4Smrg
723e6697d4Smrgchgrpcmd=
733e6697d4Smrgchmodcmd=$chmodprog
743e6697d4Smrgchowncmd=
753e6697d4Smrgmvcmd=$mvprog
763e6697d4Smrgrmcmd="$rmprog -f"
773e6697d4Smrgstripcmd=
780e20ee16Smrg
793e6697d4Smrgsrc=
803e6697d4Smrgdst=
813e6697d4Smrgdir_arg=
823e6697d4Smrgdst_arg=
830e20ee16Smrg
843e6697d4Smrgcopy_on_change=false
852f167a42Smrgis_target_a_directory=possibly
860e20ee16Smrg
873e6697d4Smrgusage="\
883e6697d4SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
893e6697d4Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
903e6697d4Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
913e6697d4Smrg   or: $0 [OPTION]... -d DIRECTORIES...
920e20ee16Smrg
933e6697d4SmrgIn the 1st form, copy SRCFILE to DSTFILE.
943e6697d4SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
953e6697d4SmrgIn the 4th, create DIRECTORIES.
960e20ee16Smrg
973e6697d4SmrgOptions:
983e6697d4Smrg     --help     display this help and exit.
993e6697d4Smrg     --version  display version info and exit.
1000e20ee16Smrg
1013e6697d4Smrg  -c            (ignored)
1023e6697d4Smrg  -C            install only if different (preserve the last data modification time)
1033e6697d4Smrg  -d            create directories instead of installing files.
1043e6697d4Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1053e6697d4Smrg  -m MODE       $chmodprog installed files to MODE.
1063e6697d4Smrg  -o USER       $chownprog installed files to USER.
1073e6697d4Smrg  -s            $stripprog installed files.
1083e6697d4Smrg  -t DIRECTORY  install into DIRECTORY.
1093e6697d4Smrg  -T            report an error if DSTFILE is a directory.
1100e20ee16Smrg
1113e6697d4SmrgEnvironment variables override the default commands:
1123e6697d4Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1133e6697d4Smrg  RMPROG STRIPPROG
1143e6697d4Smrg"
1150e20ee16Smrg
1163e6697d4Smrgwhile test $# -ne 0; do
1173e6697d4Smrg  case $1 in
1183e6697d4Smrg    -c) ;;
1190e20ee16Smrg
1203e6697d4Smrg    -C) copy_on_change=true;;
1210e20ee16Smrg
1223e6697d4Smrg    -d) dir_arg=true;;
1230e20ee16Smrg
1243e6697d4Smrg    -g) chgrpcmd="$chgrpprog $2"
1252f167a42Smrg        shift;;
1260e20ee16Smrg
1273e6697d4Smrg    --help) echo "$usage"; exit $?;;
1280e20ee16Smrg
1293e6697d4Smrg    -m) mode=$2
1302f167a42Smrg        case $mode in
1312f167a42Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1322f167a42Smrg            echo "$0: invalid mode: $mode" >&2
1332f167a42Smrg            exit 1;;
1342f167a42Smrg        esac
1352f167a42Smrg        shift;;
1360e20ee16Smrg
1373e6697d4Smrg    -o) chowncmd="$chownprog $2"
1382f167a42Smrg        shift;;
1390e20ee16Smrg
1403e6697d4Smrg    -s) stripcmd=$stripprog;;
1410e20ee16Smrg
1422f167a42Smrg    -t)
1432f167a42Smrg        is_target_a_directory=always
1442f167a42Smrg        dst_arg=$2
1452f167a42Smrg        # Protect names problematic for 'test' and other utilities.
1462f167a42Smrg        case $dst_arg in
1472f167a42Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1482f167a42Smrg        esac
1492f167a42Smrg        shift;;
1500e20ee16Smrg
1512f167a42Smrg    -T) is_target_a_directory=never;;
1520e20ee16Smrg
1533e6697d4Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1540e20ee16Smrg
1552f167a42Smrg    --) shift
1562f167a42Smrg        break;;
1570e20ee16Smrg
1582f167a42Smrg    -*) echo "$0: invalid option: $1" >&2
1592f167a42Smrg        exit 1;;
1600e20ee16Smrg
1613e6697d4Smrg    *)  break;;
1623e6697d4Smrg  esac
1633e6697d4Smrg  shift
1643e6697d4Smrgdone
1650e20ee16Smrg
1662f167a42Smrg# We allow the use of options -d and -T together, by making -d
1672f167a42Smrg# take the precedence; this is for compatibility with GNU install.
1682f167a42Smrg
1692f167a42Smrgif test -n "$dir_arg"; then
1702f167a42Smrg  if test -n "$dst_arg"; then
1712f167a42Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1722f167a42Smrg    exit 1
1732f167a42Smrg  fi
1742f167a42Smrgfi
1752f167a42Smrg
1763e6697d4Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1773e6697d4Smrg  # When -d is used, all remaining arguments are directories to create.
1783e6697d4Smrg  # When -t is used, the destination is already specified.
1793e6697d4Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1803e6697d4Smrg  for arg
1813e6697d4Smrg  do
1823e6697d4Smrg    if test -n "$dst_arg"; then
1833e6697d4Smrg      # $@ is not empty: it contains at least $arg.
1843e6697d4Smrg      set fnord "$@" "$dst_arg"
1853e6697d4Smrg      shift # fnord
1863e6697d4Smrg    fi
1873e6697d4Smrg    shift # arg
1883e6697d4Smrg    dst_arg=$arg
1892f167a42Smrg    # Protect names problematic for 'test' and other utilities.
1902f167a42Smrg    case $dst_arg in
1912f167a42Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1922f167a42Smrg    esac
1933e6697d4Smrg  done
1943e6697d4Smrgfi
1950e20ee16Smrg
1963e6697d4Smrgif test $# -eq 0; then
1973e6697d4Smrg  if test -z "$dir_arg"; then
1983e6697d4Smrg    echo "$0: no input file specified." >&2
1993e6697d4Smrg    exit 1
2003e6697d4Smrg  fi
2012f167a42Smrg  # It's OK to call 'install-sh -d' without argument.
2023e6697d4Smrg  # This can happen when creating conditional directories.
2033e6697d4Smrg  exit 0
2043e6697d4Smrgfi
2050e20ee16Smrg
2063e6697d4Smrgif test -z "$dir_arg"; then
2072f167a42Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2082f167a42Smrg    if test ! -d "$dst_arg"; then
2092f167a42Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2102f167a42Smrg      exit 1
2112f167a42Smrg    fi
2122f167a42Smrg  fi
2132f167a42Smrgfi
2142f167a42Smrg
2152f167a42Smrgif test -z "$dir_arg"; then
2162f167a42Smrg  do_exit='(exit $ret); exit $ret'
2172f167a42Smrg  trap "ret=129; $do_exit" 1
2182f167a42Smrg  trap "ret=130; $do_exit" 2
2192f167a42Smrg  trap "ret=141; $do_exit" 13
2202f167a42Smrg  trap "ret=143; $do_exit" 15
2213e6697d4Smrg
2223e6697d4Smrg  # Set umask so as not to create temps with too-generous modes.
2233e6697d4Smrg  # However, 'strip' requires both read and write access to temps.
2243e6697d4Smrg  case $mode in
2253e6697d4Smrg    # Optimize common cases.
2263e6697d4Smrg    *644) cp_umask=133;;
2273e6697d4Smrg    *755) cp_umask=22;;
2283e6697d4Smrg
2293e6697d4Smrg    *[0-7])
2303e6697d4Smrg      if test -z "$stripcmd"; then
2312f167a42Smrg        u_plus_rw=
2323e6697d4Smrg      else
2332f167a42Smrg        u_plus_rw='% 200'
2343e6697d4Smrg      fi
2353e6697d4Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2363e6697d4Smrg    *)
2373e6697d4Smrg      if test -z "$stripcmd"; then
2382f167a42Smrg        u_plus_rw=
2393e6697d4Smrg      else
2402f167a42Smrg        u_plus_rw=,u+rw
2413e6697d4Smrg      fi
2423e6697d4Smrg      cp_umask=$mode$u_plus_rw;;
2433e6697d4Smrg  esac
2443e6697d4Smrgfi
2450e20ee16Smrg
2463e6697d4Smrgfor src
2473e6697d4Smrgdo
2482f167a42Smrg  # Protect names problematic for 'test' and other utilities.
2493e6697d4Smrg  case $src in
2502f167a42Smrg    -* | [=\(\)!]) src=./$src;;
2513e6697d4Smrg  esac
2523e6697d4Smrg
2533e6697d4Smrg  if test -n "$dir_arg"; then
2543e6697d4Smrg    dst=$src
2553e6697d4Smrg    dstdir=$dst
2563e6697d4Smrg    test -d "$dstdir"
2573e6697d4Smrg    dstdir_status=$?
2583e6697d4Smrg  else
2593e6697d4Smrg
2603e6697d4Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2613e6697d4Smrg    # might cause directories to be created, which would be especially bad
2623e6697d4Smrg    # if $src (and thus $dsttmp) contains '*'.
2633e6697d4Smrg    if test ! -f "$src" && test ! -d "$src"; then
2643e6697d4Smrg      echo "$0: $src does not exist." >&2
2653e6697d4Smrg      exit 1
2663e6697d4Smrg    fi
2673e6697d4Smrg
2683e6697d4Smrg    if test -z "$dst_arg"; then
2693e6697d4Smrg      echo "$0: no destination specified." >&2
2703e6697d4Smrg      exit 1
2713e6697d4Smrg    fi
2723e6697d4Smrg    dst=$dst_arg
2730e20ee16Smrg
2743e6697d4Smrg    # If destination is a directory, append the input filename; won't work
2753e6697d4Smrg    # if double slashes aren't ignored.
2763e6697d4Smrg    if test -d "$dst"; then
2772f167a42Smrg      if test "$is_target_a_directory" = never; then
2782f167a42Smrg        echo "$0: $dst_arg: Is a directory" >&2
2792f167a42Smrg        exit 1
2803e6697d4Smrg      fi
2813e6697d4Smrg      dstdir=$dst
2823e6697d4Smrg      dst=$dstdir/`basename "$src"`
2833e6697d4Smrg      dstdir_status=0
2843e6697d4Smrg    else
2852f167a42Smrg      dstdir=`dirname "$dst"`
2863e6697d4Smrg      test -d "$dstdir"
2873e6697d4Smrg      dstdir_status=$?
2883e6697d4Smrg    fi
2893e6697d4Smrg  fi
2903e6697d4Smrg
2913e6697d4Smrg  obsolete_mkdir_used=false
2923e6697d4Smrg
2933e6697d4Smrg  if test $dstdir_status != 0; then
2943e6697d4Smrg    case $posix_mkdir in
2953e6697d4Smrg      '')
2962f167a42Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
2972f167a42Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
2982f167a42Smrg        umask=`umask`
2992f167a42Smrg        case $stripcmd.$umask in
3002f167a42Smrg          # Optimize common cases.
3012f167a42Smrg          *[2367][2367]) mkdir_umask=$umask;;
3022f167a42Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3032f167a42Smrg
3042f167a42Smrg          *[0-7])
3052f167a42Smrg            mkdir_umask=`expr $umask + 22 \
3062f167a42Smrg              - $umask % 100 % 40 + $umask % 20 \
3072f167a42Smrg              - $umask % 10 % 4 + $umask % 2
3082f167a42Smrg            `;;
3092f167a42Smrg          *) mkdir_umask=$umask,go-w;;
3102f167a42Smrg        esac
3112f167a42Smrg
3122f167a42Smrg        # With -d, create the new directory with the user-specified mode.
3132f167a42Smrg        # Otherwise, rely on $mkdir_umask.
3142f167a42Smrg        if test -n "$dir_arg"; then
3152f167a42Smrg          mkdir_mode=-m$mode
3162f167a42Smrg        else
3172f167a42Smrg          mkdir_mode=
3182f167a42Smrg        fi
3192f167a42Smrg
3202f167a42Smrg        posix_mkdir=false
3212f167a42Smrg        case $umask in
3222f167a42Smrg          *[123567][0-7][0-7])
3232f167a42Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3242f167a42Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3252f167a42Smrg            ;;
3262f167a42Smrg          *)
3272f167a42Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3282f167a42Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3292f167a42Smrg
3302f167a42Smrg            if (umask $mkdir_umask &&
3312f167a42Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3322f167a42Smrg            then
3332f167a42Smrg              if test -z "$dir_arg" || {
3342f167a42Smrg                   # Check for POSIX incompatibilities with -m.
3352f167a42Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3362f167a42Smrg                   # other-writable bit of parent directory when it shouldn't.
3372f167a42Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3382f167a42Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3392f167a42Smrg                   case $ls_ld_tmpdir in
3402f167a42Smrg                     d????-?r-*) different_mode=700;;
3412f167a42Smrg                     d????-?--*) different_mode=755;;
3422f167a42Smrg                     *) false;;
3432f167a42Smrg                   esac &&
3442f167a42Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3452f167a42Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3462f167a42Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3472f167a42Smrg                   }
3482f167a42Smrg                 }
3492f167a42Smrg              then posix_mkdir=:
3502f167a42Smrg              fi
3512f167a42Smrg              rmdir "$tmpdir/d" "$tmpdir"
3522f167a42Smrg            else
3532f167a42Smrg              # Remove any dirs left behind by ancient mkdir implementations.
3542f167a42Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3552f167a42Smrg            fi
3562f167a42Smrg            trap '' 0;;
3572f167a42Smrg        esac;;
3583e6697d4Smrg    esac
3590e20ee16Smrg
3603e6697d4Smrg    if
3613e6697d4Smrg      $posix_mkdir && (
3622f167a42Smrg        umask $mkdir_umask &&
3632f167a42Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3643e6697d4Smrg      )
3653e6697d4Smrg    then :
3663e6697d4Smrg    else
3673e6697d4Smrg
3683e6697d4Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3693e6697d4Smrg      # or it failed possibly due to a race condition.  Create the
3703e6697d4Smrg      # directory the slow way, step by step, checking for races as we go.
3713e6697d4Smrg
3723e6697d4Smrg      case $dstdir in
3732f167a42Smrg        /*) prefix='/';;
3742f167a42Smrg        [-=\(\)!]*) prefix='./';;
3752f167a42Smrg        *)  prefix='';;
3763e6697d4Smrg      esac
3773e6697d4Smrg
3783e6697d4Smrg      oIFS=$IFS
3793e6697d4Smrg      IFS=/
3802f167a42Smrg      set -f
3813e6697d4Smrg      set fnord $dstdir
3823e6697d4Smrg      shift
3832f167a42Smrg      set +f
3843e6697d4Smrg      IFS=$oIFS
3853e6697d4Smrg
3863e6697d4Smrg      prefixes=
3873e6697d4Smrg
3883e6697d4Smrg      for d
3893e6697d4Smrg      do
3902f167a42Smrg        test X"$d" = X && continue
3912f167a42Smrg
3922f167a42Smrg        prefix=$prefix$d
3932f167a42Smrg        if test -d "$prefix"; then
3942f167a42Smrg          prefixes=
3952f167a42Smrg        else
3962f167a42Smrg          if $posix_mkdir; then
3972f167a42Smrg            (umask=$mkdir_umask &&
3982f167a42Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3992f167a42Smrg            # Don't fail if two instances are running concurrently.
4002f167a42Smrg            test -d "$prefix" || exit 1
4012f167a42Smrg          else
4022f167a42Smrg            case $prefix in
4032f167a42Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4042f167a42Smrg              *) qprefix=$prefix;;
4052f167a42Smrg            esac
4062f167a42Smrg            prefixes="$prefixes '$qprefix'"
4072f167a42Smrg          fi
4082f167a42Smrg        fi
4092f167a42Smrg        prefix=$prefix/
4103e6697d4Smrg      done
4113e6697d4Smrg
4123e6697d4Smrg      if test -n "$prefixes"; then
4132f167a42Smrg        # Don't fail if two instances are running concurrently.
4142f167a42Smrg        (umask $mkdir_umask &&
4152f167a42Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4162f167a42Smrg          test -d "$dstdir" || exit 1
4172f167a42Smrg        obsolete_mkdir_used=true
4183e6697d4Smrg      fi
4193e6697d4Smrg    fi
4203e6697d4Smrg  fi
4213e6697d4Smrg
4223e6697d4Smrg  if test -n "$dir_arg"; then
4233e6697d4Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4243e6697d4Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4253e6697d4Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4263e6697d4Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4273e6697d4Smrg  else
4283e6697d4Smrg
4293e6697d4Smrg    # Make a couple of temp file names in the proper directory.
4303e6697d4Smrg    dsttmp=$dstdir/_inst.$$_
4313e6697d4Smrg    rmtmp=$dstdir/_rm.$$_
4323e6697d4Smrg
4333e6697d4Smrg    # Trap to clean up those temp files at exit.
4343e6697d4Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4353e6697d4Smrg
4363e6697d4Smrg    # Copy the file name to the temp name.
4373e6697d4Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4383e6697d4Smrg
4393e6697d4Smrg    # and set any options; do chmod last to preserve setuid bits.
4403e6697d4Smrg    #
4413e6697d4Smrg    # If any of these fail, we abort the whole thing.  If we want to
4423e6697d4Smrg    # ignore errors from any of these, just make sure not to ignore
4433e6697d4Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4443e6697d4Smrg    #
4453e6697d4Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4463e6697d4Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4473e6697d4Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4483e6697d4Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4493e6697d4Smrg
4503e6697d4Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4513e6697d4Smrg    if $copy_on_change &&
4522f167a42Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4532f167a42Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4542f167a42Smrg       set -f &&
4553e6697d4Smrg       set X $old && old=:$2:$4:$5:$6 &&
4563e6697d4Smrg       set X $new && new=:$2:$4:$5:$6 &&
4572f167a42Smrg       set +f &&
4583e6697d4Smrg       test "$old" = "$new" &&
4593e6697d4Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4603e6697d4Smrg    then
4613e6697d4Smrg      rm -f "$dsttmp"
4623e6697d4Smrg    else
4633e6697d4Smrg      # Rename the file to the real destination.
4643e6697d4Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4653e6697d4Smrg
4663e6697d4Smrg      # The rename failed, perhaps because mv can't rename something else
4673e6697d4Smrg      # to itself, or perhaps because mv is so ancient that it does not
4683e6697d4Smrg      # support -f.
4693e6697d4Smrg      {
4702f167a42Smrg        # Now remove or move aside any old file at destination location.
4712f167a42Smrg        # We try this two ways since rm can't unlink itself on some
4722f167a42Smrg        # systems and the destination file might be busy for other
4732f167a42Smrg        # reasons.  In this case, the final cleanup might fail but the new
4742f167a42Smrg        # file should still install successfully.
4752f167a42Smrg        {
4762f167a42Smrg          test ! -f "$dst" ||
4772f167a42Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4782f167a42Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4792f167a42Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4802f167a42Smrg          } ||
4812f167a42Smrg          { echo "$0: cannot unlink or rename $dst" >&2
4822f167a42Smrg            (exit 1); exit 1
4832f167a42Smrg          }
4842f167a42Smrg        } &&
4852f167a42Smrg
4862f167a42Smrg        # Now rename the file to the real destination.
4872f167a42Smrg        $doit $mvcmd "$dsttmp" "$dst"
4883e6697d4Smrg      }
4893e6697d4Smrg    fi || exit 1
4903e6697d4Smrg
4913e6697d4Smrg    trap '' 0
4923e6697d4Smrg  fi
4933e6697d4Smrgdone
4940e20ee16Smrg
4953e6697d4Smrg# Local variables:
4963e6697d4Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4973e6697d4Smrg# time-stamp-start: "scriptversion="
4983e6697d4Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
4993e6697d4Smrg# time-stamp-time-zone: "UTC"
5003e6697d4Smrg# time-stamp-end: "; # UTC"
5013e6697d4Smrg# End:
502