11.1Sriastrad#!/bin/sh
21.1Sriastrad# install - install a program, script, or datafile
31.1Sriastrad
41.1Sriastradscriptversion=2016-01-11.22; # UTC
51.1Sriastrad
61.1Sriastrad# This originates from X11R5 (mit/util/scripts/install.sh), which was
71.1Sriastrad# later released in X11R6 (xc/config/util/install.sh) with the
81.1Sriastrad# following copyright and license.
91.1Sriastrad#
101.1Sriastrad# Copyright (C) 1994 X Consortium
111.1Sriastrad#
121.1Sriastrad# Permission is hereby granted, free of charge, to any person obtaining a copy
131.1Sriastrad# of this software and associated documentation files (the "Software"), to
141.1Sriastrad# deal in the Software without restriction, including without limitation the
151.1Sriastrad# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
161.1Sriastrad# sell copies of the Software, and to permit persons to whom the Software is
171.1Sriastrad# furnished to do so, subject to the following conditions:
181.1Sriastrad#
191.1Sriastrad# The above copyright notice and this permission notice shall be included in
201.1Sriastrad# all copies or substantial portions of the Software.
211.1Sriastrad#
221.1Sriastrad# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
231.1Sriastrad# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241.1Sriastrad# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
251.1Sriastrad# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
261.1Sriastrad# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
271.1Sriastrad# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
281.1Sriastrad#
291.1Sriastrad# Except as contained in this notice, the name of the X Consortium shall not
301.1Sriastrad# be used in advertising or otherwise to promote the sale, use or other deal-
311.1Sriastrad# ings in this Software without prior written authorization from the X Consor-
321.1Sriastrad# tium.
331.1Sriastrad#
341.1Sriastrad#
351.1Sriastrad# FSF changes to this file are in the public domain.
361.1Sriastrad#
371.1Sriastrad# Calling this script install-sh is preferred over install.sh, to prevent
381.1Sriastrad# 'make' implicit rules from creating a file called install from it
391.1Sriastrad# when there is no Makefile.
401.1Sriastrad#
411.1Sriastrad# This script is compatible with the BSD install script, but was written
421.1Sriastrad# from scratch.
431.1Sriastrad
441.1Sriastradtab='	'
451.1Sriastradnl='
461.1Sriastrad'
471.1SriastradIFS=" $tab$nl"
481.1Sriastrad
491.1Sriastrad# Set DOITPROG to "echo" to test this script.
501.1Sriastrad
511.1Sriastraddoit=${DOITPROG-}
521.1Sriastraddoit_exec=${doit:-exec}
531.1Sriastrad
541.1Sriastrad# Put in absolute file names if you don't have them in your path;
551.1Sriastrad# or use environment vars.
561.1Sriastrad
571.1Sriastradchgrpprog=${CHGRPPROG-chgrp}
581.1Sriastradchmodprog=${CHMODPROG-chmod}
591.1Sriastradchownprog=${CHOWNPROG-chown}
601.1Sriastradcmpprog=${CMPPROG-cmp}
611.1Sriastradcpprog=${CPPROG-cp}
621.1Sriastradmkdirprog=${MKDIRPROG-mkdir}
631.1Sriastradmvprog=${MVPROG-mv}
641.1Sriastradrmprog=${RMPROG-rm}
651.1Sriastradstripprog=${STRIPPROG-strip}
661.1Sriastrad
671.1Sriastradposix_mkdir=
681.1Sriastrad
691.1Sriastrad# Desired mode of installed file.
701.1Sriastradmode=0755
711.1Sriastrad
721.1Sriastradchgrpcmd=
731.1Sriastradchmodcmd=$chmodprog
741.1Sriastradchowncmd=
751.1Sriastradmvcmd=$mvprog
761.1Sriastradrmcmd="$rmprog -f"
771.1Sriastradstripcmd=
781.1Sriastrad
791.1Sriastradsrc=
801.1Sriastraddst=
811.1Sriastraddir_arg=
821.1Sriastraddst_arg=
831.1Sriastrad
841.1Sriastradcopy_on_change=false
851.1Sriastradis_target_a_directory=possibly
861.1Sriastrad
871.1Sriastradusage="\
881.1SriastradUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
891.1Sriastrad   or: $0 [OPTION]... SRCFILES... DIRECTORY
901.1Sriastrad   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
911.1Sriastrad   or: $0 [OPTION]... -d DIRECTORIES...
921.1Sriastrad
931.1SriastradIn the 1st form, copy SRCFILE to DSTFILE.
941.1SriastradIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
951.1SriastradIn the 4th, create DIRECTORIES.
961.1Sriastrad
971.1SriastradOptions:
981.1Sriastrad     --help     display this help and exit.
991.1Sriastrad     --version  display version info and exit.
1001.1Sriastrad
1011.1Sriastrad  -c            (ignored)
1021.1Sriastrad  -C            install only if different (preserve the last data modification time)
1031.1Sriastrad  -d            create directories instead of installing files.
1041.1Sriastrad  -g GROUP      $chgrpprog installed files to GROUP.
1051.1Sriastrad  -m MODE       $chmodprog installed files to MODE.
1061.1Sriastrad  -o USER       $chownprog installed files to USER.
1071.1Sriastrad  -s            $stripprog installed files.
1081.1Sriastrad  -t DIRECTORY  install into DIRECTORY.
1091.1Sriastrad  -T            report an error if DSTFILE is a directory.
1101.1Sriastrad
1111.1SriastradEnvironment variables override the default commands:
1121.1Sriastrad  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1131.1Sriastrad  RMPROG STRIPPROG
1141.1Sriastrad"
1151.1Sriastrad
1161.1Sriastradwhile test $# -ne 0; do
1171.1Sriastrad  case $1 in
1181.1Sriastrad    -c) ;;
1191.1Sriastrad
1201.1Sriastrad    -C) copy_on_change=true;;
1211.1Sriastrad
1221.1Sriastrad    -d) dir_arg=true;;
1231.1Sriastrad
1241.1Sriastrad    -g) chgrpcmd="$chgrpprog $2"
1251.1Sriastrad        shift;;
1261.1Sriastrad
1271.1Sriastrad    --help) echo "$usage"; exit $?;;
1281.1Sriastrad
1291.1Sriastrad    -m) mode=$2
1301.1Sriastrad        case $mode in
1311.1Sriastrad          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1321.1Sriastrad            echo "$0: invalid mode: $mode" >&2
1331.1Sriastrad            exit 1;;
1341.1Sriastrad        esac
1351.1Sriastrad        shift;;
1361.1Sriastrad
1371.1Sriastrad    -o) chowncmd="$chownprog $2"
1381.1Sriastrad        shift;;
1391.1Sriastrad
1401.1Sriastrad    -s) stripcmd=$stripprog;;
1411.1Sriastrad
1421.1Sriastrad    -t)
1431.1Sriastrad        is_target_a_directory=always
1441.1Sriastrad        dst_arg=$2
1451.1Sriastrad        # Protect names problematic for 'test' and other utilities.
1461.1Sriastrad        case $dst_arg in
1471.1Sriastrad          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1481.1Sriastrad        esac
1491.1Sriastrad        shift;;
1501.1Sriastrad
1511.1Sriastrad    -T) is_target_a_directory=never;;
1521.1Sriastrad
1531.1Sriastrad    --version) echo "$0 $scriptversion"; exit $?;;
1541.1Sriastrad
1551.1Sriastrad    --) shift
1561.1Sriastrad        break;;
1571.1Sriastrad
1581.1Sriastrad    -*) echo "$0: invalid option: $1" >&2
1591.1Sriastrad        exit 1;;
1601.1Sriastrad
1611.1Sriastrad    *)  break;;
1621.1Sriastrad  esac
1631.1Sriastrad  shift
1641.1Sriastraddone
1651.1Sriastrad
1661.1Sriastrad# We allow the use of options -d and -T together, by making -d
1671.1Sriastrad# take the precedence; this is for compatibility with GNU install.
1681.1Sriastrad
1691.1Sriastradif test -n "$dir_arg"; then
1701.1Sriastrad  if test -n "$dst_arg"; then
1711.1Sriastrad    echo "$0: target directory not allowed when installing a directory." >&2
1721.1Sriastrad    exit 1
1731.1Sriastrad  fi
1741.1Sriastradfi
1751.1Sriastrad
1761.1Sriastradif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1771.1Sriastrad  # When -d is used, all remaining arguments are directories to create.
1781.1Sriastrad  # When -t is used, the destination is already specified.
1791.1Sriastrad  # Otherwise, the last argument is the destination.  Remove it from $@.
1801.1Sriastrad  for arg
1811.1Sriastrad  do
1821.1Sriastrad    if test -n "$dst_arg"; then
1831.1Sriastrad      # $@ is not empty: it contains at least $arg.
1841.1Sriastrad      set fnord "$@" "$dst_arg"
1851.1Sriastrad      shift # fnord
1861.1Sriastrad    fi
1871.1Sriastrad    shift # arg
1881.1Sriastrad    dst_arg=$arg
1891.1Sriastrad    # Protect names problematic for 'test' and other utilities.
1901.1Sriastrad    case $dst_arg in
1911.1Sriastrad      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1921.1Sriastrad    esac
1931.1Sriastrad  done
1941.1Sriastradfi
1951.1Sriastrad
1961.1Sriastradif test $# -eq 0; then
1971.1Sriastrad  if test -z "$dir_arg"; then
1981.1Sriastrad    echo "$0: no input file specified." >&2
1991.1Sriastrad    exit 1
2001.1Sriastrad  fi
2011.1Sriastrad  # It's OK to call 'install-sh -d' without argument.
2021.1Sriastrad  # This can happen when creating conditional directories.
2031.1Sriastrad  exit 0
2041.1Sriastradfi
2051.1Sriastrad
2061.1Sriastradif test -z "$dir_arg"; then
2071.1Sriastrad  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2081.1Sriastrad    if test ! -d "$dst_arg"; then
2091.1Sriastrad      echo "$0: $dst_arg: Is not a directory." >&2
2101.1Sriastrad      exit 1
2111.1Sriastrad    fi
2121.1Sriastrad  fi
2131.1Sriastradfi
2141.1Sriastrad
2151.1Sriastradif test -z "$dir_arg"; then
2161.1Sriastrad  do_exit='(exit $ret); exit $ret'
2171.1Sriastrad  trap "ret=129; $do_exit" 1
2181.1Sriastrad  trap "ret=130; $do_exit" 2
2191.1Sriastrad  trap "ret=141; $do_exit" 13
2201.1Sriastrad  trap "ret=143; $do_exit" 15
2211.1Sriastrad
2221.1Sriastrad  # Set umask so as not to create temps with too-generous modes.
2231.1Sriastrad  # However, 'strip' requires both read and write access to temps.
2241.1Sriastrad  case $mode in
2251.1Sriastrad    # Optimize common cases.
2261.1Sriastrad    *644) cp_umask=133;;
2271.1Sriastrad    *755) cp_umask=22;;
2281.1Sriastrad
2291.1Sriastrad    *[0-7])
2301.1Sriastrad      if test -z "$stripcmd"; then
2311.1Sriastrad        u_plus_rw=
2321.1Sriastrad      else
2331.1Sriastrad        u_plus_rw='% 200'
2341.1Sriastrad      fi
2351.1Sriastrad      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2361.1Sriastrad    *)
2371.1Sriastrad      if test -z "$stripcmd"; then
2381.1Sriastrad        u_plus_rw=
2391.1Sriastrad      else
2401.1Sriastrad        u_plus_rw=,u+rw
2411.1Sriastrad      fi
2421.1Sriastrad      cp_umask=$mode$u_plus_rw;;
2431.1Sriastrad  esac
2441.1Sriastradfi
2451.1Sriastrad
2461.1Sriastradfor src
2471.1Sriastraddo
2481.1Sriastrad  # Protect names problematic for 'test' and other utilities.
2491.1Sriastrad  case $src in
2501.1Sriastrad    -* | [=\(\)!]) src=./$src;;
2511.1Sriastrad  esac
2521.1Sriastrad
2531.1Sriastrad  if test -n "$dir_arg"; then
2541.1Sriastrad    dst=$src
2551.1Sriastrad    dstdir=$dst
2561.1Sriastrad    test -d "$dstdir"
2571.1Sriastrad    dstdir_status=$?
2581.1Sriastrad  else
2591.1Sriastrad
2601.1Sriastrad    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2611.1Sriastrad    # might cause directories to be created, which would be especially bad
2621.1Sriastrad    # if $src (and thus $dsttmp) contains '*'.
2631.1Sriastrad    if test ! -f "$src" && test ! -d "$src"; then
2641.1Sriastrad      echo "$0: $src does not exist." >&2
2651.1Sriastrad      exit 1
2661.1Sriastrad    fi
2671.1Sriastrad
2681.1Sriastrad    if test -z "$dst_arg"; then
2691.1Sriastrad      echo "$0: no destination specified." >&2
2701.1Sriastrad      exit 1
2711.1Sriastrad    fi
2721.1Sriastrad    dst=$dst_arg
2731.1Sriastrad
2741.1Sriastrad    # If destination is a directory, append the input filename; won't work
2751.1Sriastrad    # if double slashes aren't ignored.
2761.1Sriastrad    if test -d "$dst"; then
2771.1Sriastrad      if test "$is_target_a_directory" = never; then
2781.1Sriastrad        echo "$0: $dst_arg: Is a directory" >&2
2791.1Sriastrad        exit 1
2801.1Sriastrad      fi
2811.1Sriastrad      dstdir=$dst
2821.1Sriastrad      dst=$dstdir/`basename "$src"`
2831.1Sriastrad      dstdir_status=0
2841.1Sriastrad    else
2851.1Sriastrad      dstdir=`dirname "$dst"`
2861.1Sriastrad      test -d "$dstdir"
2871.1Sriastrad      dstdir_status=$?
2881.1Sriastrad    fi
2891.1Sriastrad  fi
2901.1Sriastrad
2911.1Sriastrad  obsolete_mkdir_used=false
2921.1Sriastrad
2931.1Sriastrad  if test $dstdir_status != 0; then
2941.1Sriastrad    case $posix_mkdir in
2951.1Sriastrad      '')
2961.1Sriastrad        # Create intermediate dirs using mode 755 as modified by the umask.
2971.1Sriastrad        # This is like FreeBSD 'install' as of 1997-10-28.
2981.1Sriastrad        umask=`umask`
2991.1Sriastrad        case $stripcmd.$umask in
3001.1Sriastrad          # Optimize common cases.
3011.1Sriastrad          *[2367][2367]) mkdir_umask=$umask;;
3021.1Sriastrad          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3031.1Sriastrad
3041.1Sriastrad          *[0-7])
3051.1Sriastrad            mkdir_umask=`expr $umask + 22 \
3061.1Sriastrad              - $umask % 100 % 40 + $umask % 20 \
3071.1Sriastrad              - $umask % 10 % 4 + $umask % 2
3081.1Sriastrad            `;;
3091.1Sriastrad          *) mkdir_umask=$umask,go-w;;
3101.1Sriastrad        esac
3111.1Sriastrad
3121.1Sriastrad        # With -d, create the new directory with the user-specified mode.
3131.1Sriastrad        # Otherwise, rely on $mkdir_umask.
3141.1Sriastrad        if test -n "$dir_arg"; then
3151.1Sriastrad          mkdir_mode=-m$mode
3161.1Sriastrad        else
3171.1Sriastrad          mkdir_mode=
3181.1Sriastrad        fi
3191.1Sriastrad
3201.1Sriastrad        posix_mkdir=false
3211.1Sriastrad        case $umask in
3221.1Sriastrad          *[123567][0-7][0-7])
3231.1Sriastrad            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3241.1Sriastrad            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3251.1Sriastrad            ;;
3261.1Sriastrad          *)
3271.1Sriastrad            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3281.1Sriastrad            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3291.1Sriastrad
3301.1Sriastrad            if (umask $mkdir_umask &&
3311.1Sriastrad                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3321.1Sriastrad            then
3331.1Sriastrad              if test -z "$dir_arg" || {
3341.1Sriastrad                   # Check for POSIX incompatibilities with -m.
3351.1Sriastrad                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3361.1Sriastrad                   # other-writable bit of parent directory when it shouldn't.
3371.1Sriastrad                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3381.1Sriastrad                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3391.1Sriastrad                   case $ls_ld_tmpdir in
3401.1Sriastrad                     d????-?r-*) different_mode=700;;
3411.1Sriastrad                     d????-?--*) different_mode=755;;
3421.1Sriastrad                     *) false;;
3431.1Sriastrad                   esac &&
3441.1Sriastrad                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3451.1Sriastrad                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3461.1Sriastrad                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3471.1Sriastrad                   }
3481.1Sriastrad                 }
3491.1Sriastrad              then posix_mkdir=:
3501.1Sriastrad              fi
3511.1Sriastrad              rmdir "$tmpdir/d" "$tmpdir"
3521.1Sriastrad            else
3531.1Sriastrad              # Remove any dirs left behind by ancient mkdir implementations.
3541.1Sriastrad              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3551.1Sriastrad            fi
3561.1Sriastrad            trap '' 0;;
3571.1Sriastrad        esac;;
3581.1Sriastrad    esac
3591.1Sriastrad
3601.1Sriastrad    if
3611.1Sriastrad      $posix_mkdir && (
3621.1Sriastrad        umask $mkdir_umask &&
3631.1Sriastrad        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3641.1Sriastrad      )
3651.1Sriastrad    then :
3661.1Sriastrad    else
3671.1Sriastrad
3681.1Sriastrad      # The umask is ridiculous, or mkdir does not conform to POSIX,
3691.1Sriastrad      # or it failed possibly due to a race condition.  Create the
3701.1Sriastrad      # directory the slow way, step by step, checking for races as we go.
3711.1Sriastrad
3721.1Sriastrad      case $dstdir in
3731.1Sriastrad        /*) prefix='/';;
3741.1Sriastrad        [-=\(\)!]*) prefix='./';;
3751.1Sriastrad        *)  prefix='';;
3761.1Sriastrad      esac
3771.1Sriastrad
3781.1Sriastrad      oIFS=$IFS
3791.1Sriastrad      IFS=/
3801.1Sriastrad      set -f
3811.1Sriastrad      set fnord $dstdir
3821.1Sriastrad      shift
3831.1Sriastrad      set +f
3841.1Sriastrad      IFS=$oIFS
3851.1Sriastrad
3861.1Sriastrad      prefixes=
3871.1Sriastrad
3881.1Sriastrad      for d
3891.1Sriastrad      do
3901.1Sriastrad        test X"$d" = X && continue
3911.1Sriastrad
3921.1Sriastrad        prefix=$prefix$d
3931.1Sriastrad        if test -d "$prefix"; then
3941.1Sriastrad          prefixes=
3951.1Sriastrad        else
3961.1Sriastrad          if $posix_mkdir; then
3971.1Sriastrad            (umask=$mkdir_umask &&
3981.1Sriastrad             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3991.1Sriastrad            # Don't fail if two instances are running concurrently.
4001.1Sriastrad            test -d "$prefix" || exit 1
4011.1Sriastrad          else
4021.1Sriastrad            case $prefix in
4031.1Sriastrad              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4041.1Sriastrad              *) qprefix=$prefix;;
4051.1Sriastrad            esac
4061.1Sriastrad            prefixes="$prefixes '$qprefix'"
4071.1Sriastrad          fi
4081.1Sriastrad        fi
4091.1Sriastrad        prefix=$prefix/
4101.1Sriastrad      done
4111.1Sriastrad
4121.1Sriastrad      if test -n "$prefixes"; then
4131.1Sriastrad        # Don't fail if two instances are running concurrently.
4141.1Sriastrad        (umask $mkdir_umask &&
4151.1Sriastrad         eval "\$doit_exec \$mkdirprog $prefixes") ||
4161.1Sriastrad          test -d "$dstdir" || exit 1
4171.1Sriastrad        obsolete_mkdir_used=true
4181.1Sriastrad      fi
4191.1Sriastrad    fi
4201.1Sriastrad  fi
4211.1Sriastrad
4221.1Sriastrad  if test -n "$dir_arg"; then
4231.1Sriastrad    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4241.1Sriastrad    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4251.1Sriastrad    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4261.1Sriastrad      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4271.1Sriastrad  else
4281.1Sriastrad
4291.1Sriastrad    # Make a couple of temp file names in the proper directory.
4301.1Sriastrad    dsttmp=$dstdir/_inst.$$_
4311.1Sriastrad    rmtmp=$dstdir/_rm.$$_
4321.1Sriastrad
4331.1Sriastrad    # Trap to clean up those temp files at exit.
4341.1Sriastrad    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4351.1Sriastrad
4361.1Sriastrad    # Copy the file name to the temp name.
4371.1Sriastrad    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4381.1Sriastrad
4391.1Sriastrad    # and set any options; do chmod last to preserve setuid bits.
4401.1Sriastrad    #
4411.1Sriastrad    # If any of these fail, we abort the whole thing.  If we want to
4421.1Sriastrad    # ignore errors from any of these, just make sure not to ignore
4431.1Sriastrad    # errors from the above "$doit $cpprog $src $dsttmp" command.
4441.1Sriastrad    #
4451.1Sriastrad    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4461.1Sriastrad    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4471.1Sriastrad    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4481.1Sriastrad    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4491.1Sriastrad
4501.1Sriastrad    # If -C, don't bother to copy if it wouldn't change the file.
4511.1Sriastrad    if $copy_on_change &&
4521.1Sriastrad       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4531.1Sriastrad       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4541.1Sriastrad       set -f &&
4551.1Sriastrad       set X $old && old=:$2:$4:$5:$6 &&
4561.1Sriastrad       set X $new && new=:$2:$4:$5:$6 &&
4571.1Sriastrad       set +f &&
4581.1Sriastrad       test "$old" = "$new" &&
4591.1Sriastrad       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4601.1Sriastrad    then
4611.1Sriastrad      rm -f "$dsttmp"
4621.1Sriastrad    else
4631.1Sriastrad      # Rename the file to the real destination.
4641.1Sriastrad      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4651.1Sriastrad
4661.1Sriastrad      # The rename failed, perhaps because mv can't rename something else
4671.1Sriastrad      # to itself, or perhaps because mv is so ancient that it does not
4681.1Sriastrad      # support -f.
4691.1Sriastrad      {
4701.1Sriastrad        # Now remove or move aside any old file at destination location.
4711.1Sriastrad        # We try this two ways since rm can't unlink itself on some
4721.1Sriastrad        # systems and the destination file might be busy for other
4731.1Sriastrad        # reasons.  In this case, the final cleanup might fail but the new
4741.1Sriastrad        # file should still install successfully.
4751.1Sriastrad        {
4761.1Sriastrad          test ! -f "$dst" ||
4771.1Sriastrad          $doit $rmcmd -f "$dst" 2>/dev/null ||
4781.1Sriastrad          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4791.1Sriastrad            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4801.1Sriastrad          } ||
4811.1Sriastrad          { echo "$0: cannot unlink or rename $dst" >&2
4821.1Sriastrad            (exit 1); exit 1
4831.1Sriastrad          }
4841.1Sriastrad        } &&
4851.1Sriastrad
4861.1Sriastrad        # Now rename the file to the real destination.
4871.1Sriastrad        $doit $mvcmd "$dsttmp" "$dst"
4881.1Sriastrad      }
4891.1Sriastrad    fi || exit 1
4901.1Sriastrad
4911.1Sriastrad    trap '' 0
4921.1Sriastrad  fi
4931.1Sriastraddone
4941.1Sriastrad
4951.1Sriastrad# Local variables:
4961.1Sriastrad# eval: (add-hook 'write-file-hooks 'time-stamp)
4971.1Sriastrad# time-stamp-start: "scriptversion="
4981.1Sriastrad# time-stamp-format: "%:y-%02m-%02d.%02H"
4991.1Sriastrad# time-stamp-time-zone: "UTC0"
5001.1Sriastrad# time-stamp-end: "; # UTC"
5011.1Sriastrad# End:
502