12007c8b2Snia#!/bin/sh
22007c8b2Snia# install - install a program, script, or datafile
32007c8b2Snia
42007c8b2Sniascriptversion=2013-12-25.23; # UTC
52007c8b2Snia
62007c8b2Snia# This originates from X11R5 (mit/util/scripts/install.sh), which was
72007c8b2Snia# later released in X11R6 (xc/config/util/install.sh) with the
82007c8b2Snia# following copyright and license.
92007c8b2Snia#
102007c8b2Snia# Copyright (C) 1994 X Consortium
112007c8b2Snia#
122007c8b2Snia# Permission is hereby granted, free of charge, to any person obtaining a copy
132007c8b2Snia# of this software and associated documentation files (the "Software"), to
142007c8b2Snia# deal in the Software without restriction, including without limitation the
152007c8b2Snia# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
162007c8b2Snia# sell copies of the Software, and to permit persons to whom the Software is
172007c8b2Snia# furnished to do so, subject to the following conditions:
182007c8b2Snia#
192007c8b2Snia# The above copyright notice and this permission notice shall be included in
202007c8b2Snia# all copies or substantial portions of the Software.
212007c8b2Snia#
222007c8b2Snia# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
232007c8b2Snia# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
242007c8b2Snia# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
252007c8b2Snia# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
262007c8b2Snia# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
272007c8b2Snia# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
282007c8b2Snia#
292007c8b2Snia# Except as contained in this notice, the name of the X Consortium shall not
302007c8b2Snia# be used in advertising or otherwise to promote the sale, use or other deal-
312007c8b2Snia# ings in this Software without prior written authorization from the X Consor-
322007c8b2Snia# tium.
332007c8b2Snia#
342007c8b2Snia#
352007c8b2Snia# FSF changes to this file are in the public domain.
362007c8b2Snia#
372007c8b2Snia# Calling this script install-sh is preferred over install.sh, to prevent
382007c8b2Snia# 'make' implicit rules from creating a file called install from it
392007c8b2Snia# when there is no Makefile.
402007c8b2Snia#
412007c8b2Snia# This script is compatible with the BSD install script, but was written
422007c8b2Snia# from scratch.
432007c8b2Snia
442007c8b2Sniatab='	'
452007c8b2Snianl='
462007c8b2Snia'
472007c8b2SniaIFS=" $tab$nl"
482007c8b2Snia
492007c8b2Snia# Set DOITPROG to "echo" to test this script.
502007c8b2Snia
512007c8b2Sniadoit=${DOITPROG-}
522007c8b2Sniadoit_exec=${doit:-exec}
532007c8b2Snia
542007c8b2Snia# Put in absolute file names if you don't have them in your path;
552007c8b2Snia# or use environment vars.
562007c8b2Snia
572007c8b2Sniachgrpprog=${CHGRPPROG-chgrp}
582007c8b2Sniachmodprog=${CHMODPROG-chmod}
592007c8b2Sniachownprog=${CHOWNPROG-chown}
602007c8b2Sniacmpprog=${CMPPROG-cmp}
612007c8b2Sniacpprog=${CPPROG-cp}
622007c8b2Sniamkdirprog=${MKDIRPROG-mkdir}
632007c8b2Sniamvprog=${MVPROG-mv}
642007c8b2Sniarmprog=${RMPROG-rm}
652007c8b2Sniastripprog=${STRIPPROG-strip}
662007c8b2Snia
672007c8b2Sniaposix_mkdir=
682007c8b2Snia
692007c8b2Snia# Desired mode of installed file.
702007c8b2Sniamode=0755
712007c8b2Snia
722007c8b2Sniachgrpcmd=
732007c8b2Sniachmodcmd=$chmodprog
742007c8b2Sniachowncmd=
752007c8b2Sniamvcmd=$mvprog
762007c8b2Sniarmcmd="$rmprog -f"
772007c8b2Sniastripcmd=
782007c8b2Snia
792007c8b2Sniasrc=
802007c8b2Sniadst=
812007c8b2Sniadir_arg=
822007c8b2Sniadst_arg=
832007c8b2Snia
842007c8b2Sniacopy_on_change=false
852007c8b2Sniais_target_a_directory=possibly
862007c8b2Snia
872007c8b2Sniausage="\
882007c8b2SniaUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
892007c8b2Snia   or: $0 [OPTION]... SRCFILES... DIRECTORY
902007c8b2Snia   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
912007c8b2Snia   or: $0 [OPTION]... -d DIRECTORIES...
922007c8b2Snia
932007c8b2SniaIn the 1st form, copy SRCFILE to DSTFILE.
942007c8b2SniaIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
952007c8b2SniaIn the 4th, create DIRECTORIES.
962007c8b2Snia
972007c8b2SniaOptions:
982007c8b2Snia     --help     display this help and exit.
992007c8b2Snia     --version  display version info and exit.
1002007c8b2Snia
1012007c8b2Snia  -c            (ignored)
1022007c8b2Snia  -C            install only if different (preserve the last data modification time)
1032007c8b2Snia  -d            create directories instead of installing files.
1042007c8b2Snia  -g GROUP      $chgrpprog installed files to GROUP.
1052007c8b2Snia  -m MODE       $chmodprog installed files to MODE.
1062007c8b2Snia  -o USER       $chownprog installed files to USER.
1072007c8b2Snia  -s            $stripprog installed files.
1082007c8b2Snia  -t DIRECTORY  install into DIRECTORY.
1092007c8b2Snia  -T            report an error if DSTFILE is a directory.
1102007c8b2Snia
1112007c8b2SniaEnvironment variables override the default commands:
1122007c8b2Snia  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1132007c8b2Snia  RMPROG STRIPPROG
1142007c8b2Snia"
1152007c8b2Snia
1162007c8b2Sniawhile test $# -ne 0; do
1172007c8b2Snia  case $1 in
1182007c8b2Snia    -c) ;;
1192007c8b2Snia
1202007c8b2Snia    -C) copy_on_change=true;;
1212007c8b2Snia
1222007c8b2Snia    -d) dir_arg=true;;
1232007c8b2Snia
1242007c8b2Snia    -g) chgrpcmd="$chgrpprog $2"
1252007c8b2Snia        shift;;
1262007c8b2Snia
1272007c8b2Snia    --help) echo "$usage"; exit $?;;
1282007c8b2Snia
1292007c8b2Snia    -m) mode=$2
1302007c8b2Snia        case $mode in
1312007c8b2Snia          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1322007c8b2Snia            echo "$0: invalid mode: $mode" >&2
1332007c8b2Snia            exit 1;;
1342007c8b2Snia        esac
1352007c8b2Snia        shift;;
1362007c8b2Snia
1372007c8b2Snia    -o) chowncmd="$chownprog $2"
1382007c8b2Snia        shift;;
1392007c8b2Snia
1402007c8b2Snia    -s) stripcmd=$stripprog;;
1412007c8b2Snia
1422007c8b2Snia    -t)
1432007c8b2Snia        is_target_a_directory=always
1442007c8b2Snia        dst_arg=$2
1452007c8b2Snia        # Protect names problematic for 'test' and other utilities.
1462007c8b2Snia        case $dst_arg in
1472007c8b2Snia          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1482007c8b2Snia        esac
1492007c8b2Snia        shift;;
1502007c8b2Snia
1512007c8b2Snia    -T) is_target_a_directory=never;;
1522007c8b2Snia
1532007c8b2Snia    --version) echo "$0 $scriptversion"; exit $?;;
1542007c8b2Snia
1552007c8b2Snia    --) shift
1562007c8b2Snia        break;;
1572007c8b2Snia
1582007c8b2Snia    -*) echo "$0: invalid option: $1" >&2
1592007c8b2Snia        exit 1;;
1602007c8b2Snia
1612007c8b2Snia    *)  break;;
1622007c8b2Snia  esac
1632007c8b2Snia  shift
1642007c8b2Sniadone
1652007c8b2Snia
1662007c8b2Snia# We allow the use of options -d and -T together, by making -d
1672007c8b2Snia# take the precedence; this is for compatibility with GNU install.
1682007c8b2Snia
1692007c8b2Sniaif test -n "$dir_arg"; then
1702007c8b2Snia  if test -n "$dst_arg"; then
1712007c8b2Snia    echo "$0: target directory not allowed when installing a directory." >&2
1722007c8b2Snia    exit 1
1732007c8b2Snia  fi
1742007c8b2Sniafi
1752007c8b2Snia
1762007c8b2Sniaif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1772007c8b2Snia  # When -d is used, all remaining arguments are directories to create.
1782007c8b2Snia  # When -t is used, the destination is already specified.
1792007c8b2Snia  # Otherwise, the last argument is the destination.  Remove it from $@.
1802007c8b2Snia  for arg
1812007c8b2Snia  do
1822007c8b2Snia    if test -n "$dst_arg"; then
1832007c8b2Snia      # $@ is not empty: it contains at least $arg.
1842007c8b2Snia      set fnord "$@" "$dst_arg"
1852007c8b2Snia      shift # fnord
1862007c8b2Snia    fi
1872007c8b2Snia    shift # arg
1882007c8b2Snia    dst_arg=$arg
1892007c8b2Snia    # Protect names problematic for 'test' and other utilities.
1902007c8b2Snia    case $dst_arg in
1912007c8b2Snia      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1922007c8b2Snia    esac
1932007c8b2Snia  done
1942007c8b2Sniafi
1952007c8b2Snia
1962007c8b2Sniaif test $# -eq 0; then
1972007c8b2Snia  if test -z "$dir_arg"; then
1982007c8b2Snia    echo "$0: no input file specified." >&2
1992007c8b2Snia    exit 1
2002007c8b2Snia  fi
2012007c8b2Snia  # It's OK to call 'install-sh -d' without argument.
2022007c8b2Snia  # This can happen when creating conditional directories.
2032007c8b2Snia  exit 0
2042007c8b2Sniafi
2052007c8b2Snia
2062007c8b2Sniaif test -z "$dir_arg"; then
2072007c8b2Snia  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2082007c8b2Snia    if test ! -d "$dst_arg"; then
2092007c8b2Snia      echo "$0: $dst_arg: Is not a directory." >&2
2102007c8b2Snia      exit 1
2112007c8b2Snia    fi
2122007c8b2Snia  fi
2132007c8b2Sniafi
2142007c8b2Snia
2152007c8b2Sniaif test -z "$dir_arg"; then
2162007c8b2Snia  do_exit='(exit $ret); exit $ret'
2172007c8b2Snia  trap "ret=129; $do_exit" 1
2182007c8b2Snia  trap "ret=130; $do_exit" 2
2192007c8b2Snia  trap "ret=141; $do_exit" 13
2202007c8b2Snia  trap "ret=143; $do_exit" 15
2212007c8b2Snia
2222007c8b2Snia  # Set umask so as not to create temps with too-generous modes.
2232007c8b2Snia  # However, 'strip' requires both read and write access to temps.
2242007c8b2Snia  case $mode in
2252007c8b2Snia    # Optimize common cases.
2262007c8b2Snia    *644) cp_umask=133;;
2272007c8b2Snia    *755) cp_umask=22;;
2282007c8b2Snia
2292007c8b2Snia    *[0-7])
2302007c8b2Snia      if test -z "$stripcmd"; then
2312007c8b2Snia        u_plus_rw=
2322007c8b2Snia      else
2332007c8b2Snia        u_plus_rw='% 200'
2342007c8b2Snia      fi
2352007c8b2Snia      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2362007c8b2Snia    *)
2372007c8b2Snia      if test -z "$stripcmd"; then
2382007c8b2Snia        u_plus_rw=
2392007c8b2Snia      else
2402007c8b2Snia        u_plus_rw=,u+rw
2412007c8b2Snia      fi
2422007c8b2Snia      cp_umask=$mode$u_plus_rw;;
2432007c8b2Snia  esac
2442007c8b2Sniafi
2452007c8b2Snia
2462007c8b2Sniafor src
2472007c8b2Sniado
2482007c8b2Snia  # Protect names problematic for 'test' and other utilities.
2492007c8b2Snia  case $src in
2502007c8b2Snia    -* | [=\(\)!]) src=./$src;;
2512007c8b2Snia  esac
2522007c8b2Snia
2532007c8b2Snia  if test -n "$dir_arg"; then
2542007c8b2Snia    dst=$src
2552007c8b2Snia    dstdir=$dst
2562007c8b2Snia    test -d "$dstdir"
2572007c8b2Snia    dstdir_status=$?
2582007c8b2Snia  else
2592007c8b2Snia
2602007c8b2Snia    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2612007c8b2Snia    # might cause directories to be created, which would be especially bad
2622007c8b2Snia    # if $src (and thus $dsttmp) contains '*'.
2632007c8b2Snia    if test ! -f "$src" && test ! -d "$src"; then
2642007c8b2Snia      echo "$0: $src does not exist." >&2
2652007c8b2Snia      exit 1
2662007c8b2Snia    fi
2672007c8b2Snia
2682007c8b2Snia    if test -z "$dst_arg"; then
2692007c8b2Snia      echo "$0: no destination specified." >&2
2702007c8b2Snia      exit 1
2712007c8b2Snia    fi
2722007c8b2Snia    dst=$dst_arg
2732007c8b2Snia
2742007c8b2Snia    # If destination is a directory, append the input filename; won't work
2752007c8b2Snia    # if double slashes aren't ignored.
2762007c8b2Snia    if test -d "$dst"; then
2772007c8b2Snia      if test "$is_target_a_directory" = never; then
2782007c8b2Snia        echo "$0: $dst_arg: Is a directory" >&2
2792007c8b2Snia        exit 1
2802007c8b2Snia      fi
2812007c8b2Snia      dstdir=$dst
2822007c8b2Snia      dst=$dstdir/`basename "$src"`
2832007c8b2Snia      dstdir_status=0
2842007c8b2Snia    else
2852007c8b2Snia      dstdir=`dirname "$dst"`
2862007c8b2Snia      test -d "$dstdir"
2872007c8b2Snia      dstdir_status=$?
2882007c8b2Snia    fi
2892007c8b2Snia  fi
2902007c8b2Snia
2912007c8b2Snia  obsolete_mkdir_used=false
2922007c8b2Snia
2932007c8b2Snia  if test $dstdir_status != 0; then
2942007c8b2Snia    case $posix_mkdir in
2952007c8b2Snia      '')
2962007c8b2Snia        # Create intermediate dirs using mode 755 as modified by the umask.
2972007c8b2Snia        # This is like FreeBSD 'install' as of 1997-10-28.
2982007c8b2Snia        umask=`umask`
2992007c8b2Snia        case $stripcmd.$umask in
3002007c8b2Snia          # Optimize common cases.
3012007c8b2Snia          *[2367][2367]) mkdir_umask=$umask;;
3022007c8b2Snia          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3032007c8b2Snia
3042007c8b2Snia          *[0-7])
3052007c8b2Snia            mkdir_umask=`expr $umask + 22 \
3062007c8b2Snia              - $umask % 100 % 40 + $umask % 20 \
3072007c8b2Snia              - $umask % 10 % 4 + $umask % 2
3082007c8b2Snia            `;;
3092007c8b2Snia          *) mkdir_umask=$umask,go-w;;
3102007c8b2Snia        esac
3112007c8b2Snia
3122007c8b2Snia        # With -d, create the new directory with the user-specified mode.
3132007c8b2Snia        # Otherwise, rely on $mkdir_umask.
3142007c8b2Snia        if test -n "$dir_arg"; then
3152007c8b2Snia          mkdir_mode=-m$mode
3162007c8b2Snia        else
3172007c8b2Snia          mkdir_mode=
3182007c8b2Snia        fi
3192007c8b2Snia
3202007c8b2Snia        posix_mkdir=false
3212007c8b2Snia        case $umask in
3222007c8b2Snia          *[123567][0-7][0-7])
3232007c8b2Snia            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3242007c8b2Snia            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3252007c8b2Snia            ;;
3262007c8b2Snia          *)
3272007c8b2Snia            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3282007c8b2Snia            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3292007c8b2Snia
3302007c8b2Snia            if (umask $mkdir_umask &&
3312007c8b2Snia                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3322007c8b2Snia            then
3332007c8b2Snia              if test -z "$dir_arg" || {
3342007c8b2Snia                   # Check for POSIX incompatibilities with -m.
3352007c8b2Snia                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3362007c8b2Snia                   # other-writable bit of parent directory when it shouldn't.
3372007c8b2Snia                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3382007c8b2Snia                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3392007c8b2Snia                   case $ls_ld_tmpdir in
3402007c8b2Snia                     d????-?r-*) different_mode=700;;
3412007c8b2Snia                     d????-?--*) different_mode=755;;
3422007c8b2Snia                     *) false;;
3432007c8b2Snia                   esac &&
3442007c8b2Snia                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3452007c8b2Snia                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3462007c8b2Snia                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3472007c8b2Snia                   }
3482007c8b2Snia                 }
3492007c8b2Snia              then posix_mkdir=:
3502007c8b2Snia              fi
3512007c8b2Snia              rmdir "$tmpdir/d" "$tmpdir"
3522007c8b2Snia            else
3532007c8b2Snia              # Remove any dirs left behind by ancient mkdir implementations.
3542007c8b2Snia              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3552007c8b2Snia            fi
3562007c8b2Snia            trap '' 0;;
3572007c8b2Snia        esac;;
3582007c8b2Snia    esac
3592007c8b2Snia
3602007c8b2Snia    if
3612007c8b2Snia      $posix_mkdir && (
3622007c8b2Snia        umask $mkdir_umask &&
3632007c8b2Snia        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3642007c8b2Snia      )
3652007c8b2Snia    then :
3662007c8b2Snia    else
3672007c8b2Snia
3682007c8b2Snia      # The umask is ridiculous, or mkdir does not conform to POSIX,
3692007c8b2Snia      # or it failed possibly due to a race condition.  Create the
3702007c8b2Snia      # directory the slow way, step by step, checking for races as we go.
3712007c8b2Snia
3722007c8b2Snia      case $dstdir in
3732007c8b2Snia        /*) prefix='/';;
3742007c8b2Snia        [-=\(\)!]*) prefix='./';;
3752007c8b2Snia        *)  prefix='';;
3762007c8b2Snia      esac
3772007c8b2Snia
3782007c8b2Snia      oIFS=$IFS
3792007c8b2Snia      IFS=/
3802007c8b2Snia      set -f
3812007c8b2Snia      set fnord $dstdir
3822007c8b2Snia      shift
3832007c8b2Snia      set +f
3842007c8b2Snia      IFS=$oIFS
3852007c8b2Snia
3862007c8b2Snia      prefixes=
3872007c8b2Snia
3882007c8b2Snia      for d
3892007c8b2Snia      do
3902007c8b2Snia        test X"$d" = X && continue
3912007c8b2Snia
3922007c8b2Snia        prefix=$prefix$d
3932007c8b2Snia        if test -d "$prefix"; then
3942007c8b2Snia          prefixes=
3952007c8b2Snia        else
3962007c8b2Snia          if $posix_mkdir; then
3972007c8b2Snia            (umask=$mkdir_umask &&
3982007c8b2Snia             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3992007c8b2Snia            # Don't fail if two instances are running concurrently.
4002007c8b2Snia            test -d "$prefix" || exit 1
4012007c8b2Snia          else
4022007c8b2Snia            case $prefix in
4032007c8b2Snia              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4042007c8b2Snia              *) qprefix=$prefix;;
4052007c8b2Snia            esac
4062007c8b2Snia            prefixes="$prefixes '$qprefix'"
4072007c8b2Snia          fi
4082007c8b2Snia        fi
4092007c8b2Snia        prefix=$prefix/
4102007c8b2Snia      done
4112007c8b2Snia
4122007c8b2Snia      if test -n "$prefixes"; then
4132007c8b2Snia        # Don't fail if two instances are running concurrently.
4142007c8b2Snia        (umask $mkdir_umask &&
4152007c8b2Snia         eval "\$doit_exec \$mkdirprog $prefixes") ||
4162007c8b2Snia          test -d "$dstdir" || exit 1
4172007c8b2Snia        obsolete_mkdir_used=true
4182007c8b2Snia      fi
4192007c8b2Snia    fi
4202007c8b2Snia  fi
4212007c8b2Snia
4222007c8b2Snia  if test -n "$dir_arg"; then
4232007c8b2Snia    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4242007c8b2Snia    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4252007c8b2Snia    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4262007c8b2Snia      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4272007c8b2Snia  else
4282007c8b2Snia
4292007c8b2Snia    # Make a couple of temp file names in the proper directory.
4302007c8b2Snia    dsttmp=$dstdir/_inst.$$_
4312007c8b2Snia    rmtmp=$dstdir/_rm.$$_
4322007c8b2Snia
4332007c8b2Snia    # Trap to clean up those temp files at exit.
4342007c8b2Snia    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4352007c8b2Snia
4362007c8b2Snia    # Copy the file name to the temp name.
4372007c8b2Snia    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4382007c8b2Snia
4392007c8b2Snia    # and set any options; do chmod last to preserve setuid bits.
4402007c8b2Snia    #
4412007c8b2Snia    # If any of these fail, we abort the whole thing.  If we want to
4422007c8b2Snia    # ignore errors from any of these, just make sure not to ignore
4432007c8b2Snia    # errors from the above "$doit $cpprog $src $dsttmp" command.
4442007c8b2Snia    #
4452007c8b2Snia    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4462007c8b2Snia    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4472007c8b2Snia    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4482007c8b2Snia    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4492007c8b2Snia
4502007c8b2Snia    # If -C, don't bother to copy if it wouldn't change the file.
4512007c8b2Snia    if $copy_on_change &&
4522007c8b2Snia       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4532007c8b2Snia       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4542007c8b2Snia       set -f &&
4552007c8b2Snia       set X $old && old=:$2:$4:$5:$6 &&
4562007c8b2Snia       set X $new && new=:$2:$4:$5:$6 &&
4572007c8b2Snia       set +f &&
4582007c8b2Snia       test "$old" = "$new" &&
4592007c8b2Snia       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4602007c8b2Snia    then
4612007c8b2Snia      rm -f "$dsttmp"
4622007c8b2Snia    else
4632007c8b2Snia      # Rename the file to the real destination.
4642007c8b2Snia      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4652007c8b2Snia
4662007c8b2Snia      # The rename failed, perhaps because mv can't rename something else
4672007c8b2Snia      # to itself, or perhaps because mv is so ancient that it does not
4682007c8b2Snia      # support -f.
4692007c8b2Snia      {
4702007c8b2Snia        # Now remove or move aside any old file at destination location.
4712007c8b2Snia        # We try this two ways since rm can't unlink itself on some
4722007c8b2Snia        # systems and the destination file might be busy for other
4732007c8b2Snia        # reasons.  In this case, the final cleanup might fail but the new
4742007c8b2Snia        # file should still install successfully.
4752007c8b2Snia        {
4762007c8b2Snia          test ! -f "$dst" ||
4772007c8b2Snia          $doit $rmcmd -f "$dst" 2>/dev/null ||
4782007c8b2Snia          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4792007c8b2Snia            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4802007c8b2Snia          } ||
4812007c8b2Snia          { echo "$0: cannot unlink or rename $dst" >&2
4822007c8b2Snia            (exit 1); exit 1
4832007c8b2Snia          }
4842007c8b2Snia        } &&
4852007c8b2Snia
4862007c8b2Snia        # Now rename the file to the real destination.
4872007c8b2Snia        $doit $mvcmd "$dsttmp" "$dst"
4882007c8b2Snia      }
4892007c8b2Snia    fi || exit 1
4902007c8b2Snia
4912007c8b2Snia    trap '' 0
4922007c8b2Snia  fi
4932007c8b2Sniadone
4942007c8b2Snia
4952007c8b2Snia# Local variables:
4962007c8b2Snia# eval: (add-hook 'write-file-hooks 'time-stamp)
4972007c8b2Snia# time-stamp-start: "scriptversion="
4982007c8b2Snia# time-stamp-format: "%:y-%02m-%02d.%02H"
4992007c8b2Snia# time-stamp-time-zone: "UTC"
5002007c8b2Snia# time-stamp-end: "; # UTC"
5012007c8b2Snia# End:
502