install-sh revision 26372658
14b0ead49Smrg#!/bin/sh
24b0ead49Smrg# install - install a program, script, or datafile
34b0ead49Smrg
426372658Smrgscriptversion=2013-12-25.23; # UTC
54b0ead49Smrg
64b0ead49Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
74b0ead49Smrg# later released in X11R6 (xc/config/util/install.sh) with the
84b0ead49Smrg# following copyright and license.
94b0ead49Smrg#
104b0ead49Smrg# Copyright (C) 1994 X Consortium
114b0ead49Smrg#
124b0ead49Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
134b0ead49Smrg# of this software and associated documentation files (the "Software"), to
144b0ead49Smrg# deal in the Software without restriction, including without limitation the
154b0ead49Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
164b0ead49Smrg# sell copies of the Software, and to permit persons to whom the Software is
174b0ead49Smrg# furnished to do so, subject to the following conditions:
184b0ead49Smrg#
194b0ead49Smrg# The above copyright notice and this permission notice shall be included in
204b0ead49Smrg# all copies or substantial portions of the Software.
214b0ead49Smrg#
224b0ead49Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
234b0ead49Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
244b0ead49Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
254b0ead49Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
264b0ead49Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
274b0ead49Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
284b0ead49Smrg#
294b0ead49Smrg# Except as contained in this notice, the name of the X Consortium shall not
304b0ead49Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
314b0ead49Smrg# ings in this Software without prior written authorization from the X Consor-
324b0ead49Smrg# tium.
334b0ead49Smrg#
344b0ead49Smrg#
354b0ead49Smrg# FSF changes to this file are in the public domain.
364b0ead49Smrg#
374b0ead49Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3826372658Smrg# 'make' implicit rules from creating a file called install from it
394b0ead49Smrg# when there is no Makefile.
404b0ead49Smrg#
414b0ead49Smrg# This script is compatible with the BSD install script, but was written
4252b01e16Smrg# from scratch.
4352b01e16Smrg
4426372658Smrgtab='	'
4552b01e16Smrgnl='
4652b01e16Smrg'
4726372658SmrgIFS=" $tab$nl"
484b0ead49Smrg
4926372658Smrg# Set DOITPROG to "echo" to test this script.
504b0ead49Smrg
5152b01e16Smrgdoit=${DOITPROG-}
5226372658Smrgdoit_exec=${doit:-exec}
534b0ead49Smrg
5452b01e16Smrg# Put in absolute file names if you don't have them in your path;
5552b01e16Smrg# or use environment vars.
5652b01e16Smrg
5752b01e16Smrgchgrpprog=${CHGRPPROG-chgrp}
5852b01e16Smrgchmodprog=${CHMODPROG-chmod}
5952b01e16Smrgchownprog=${CHOWNPROG-chown}
6052b01e16Smrgcmpprog=${CMPPROG-cmp}
6152b01e16Smrgcpprog=${CPPROG-cp}
6252b01e16Smrgmkdirprog=${MKDIRPROG-mkdir}
6352b01e16Smrgmvprog=${MVPROG-mv}
6452b01e16Smrgrmprog=${RMPROG-rm}
6552b01e16Smrgstripprog=${STRIPPROG-strip}
6652b01e16Smrg
6752b01e16Smrgposix_mkdir=
6852b01e16Smrg
6952b01e16Smrg# Desired mode of installed file.
7052b01e16Smrgmode=0755
714b0ead49Smrg
724b0ead49Smrgchgrpcmd=
7352b01e16Smrgchmodcmd=$chmodprog
7452b01e16Smrgchowncmd=
7552b01e16Smrgmvcmd=$mvprog
764b0ead49Smrgrmcmd="$rmprog -f"
7752b01e16Smrgstripcmd=
7852b01e16Smrg
794b0ead49Smrgsrc=
804b0ead49Smrgdst=
814b0ead49Smrgdir_arg=
8252b01e16Smrgdst_arg=
8352b01e16Smrg
8452b01e16Smrgcopy_on_change=false
8526372658Smrgis_target_a_directory=possibly
864b0ead49Smrg
8752b01e16Smrgusage="\
8852b01e16SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
894b0ead49Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
904b0ead49Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
914b0ead49Smrg   or: $0 [OPTION]... -d DIRECTORIES...
924b0ead49Smrg
934b0ead49SmrgIn the 1st form, copy SRCFILE to DSTFILE.
944b0ead49SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
954b0ead49SmrgIn the 4th, create DIRECTORIES.
964b0ead49Smrg
974b0ead49SmrgOptions:
9852b01e16Smrg     --help     display this help and exit.
9952b01e16Smrg     --version  display version info and exit.
10052b01e16Smrg
10152b01e16Smrg  -c            (ignored)
10252b01e16Smrg  -C            install only if different (preserve the last data modification time)
10352b01e16Smrg  -d            create directories instead of installing files.
10452b01e16Smrg  -g GROUP      $chgrpprog installed files to GROUP.
10552b01e16Smrg  -m MODE       $chmodprog installed files to MODE.
10652b01e16Smrg  -o USER       $chownprog installed files to USER.
10752b01e16Smrg  -s            $stripprog installed files.
10852b01e16Smrg  -t DIRECTORY  install into DIRECTORY.
10952b01e16Smrg  -T            report an error if DSTFILE is a directory.
1104b0ead49Smrg
1114b0ead49SmrgEnvironment variables override the default commands:
11252b01e16Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
11352b01e16Smrg  RMPROG STRIPPROG
1144b0ead49Smrg"
1154b0ead49Smrg
11652b01e16Smrgwhile test $# -ne 0; do
1174b0ead49Smrg  case $1 in
11852b01e16Smrg    -c) ;;
11952b01e16Smrg
12052b01e16Smrg    -C) copy_on_change=true;;
1214b0ead49Smrg
12252b01e16Smrg    -d) dir_arg=true;;
1234b0ead49Smrg
1244b0ead49Smrg    -g) chgrpcmd="$chgrpprog $2"
12526372658Smrg        shift;;
1264b0ead49Smrg
1274b0ead49Smrg    --help) echo "$usage"; exit $?;;
1284b0ead49Smrg
12952b01e16Smrg    -m) mode=$2
13026372658Smrg        case $mode in
13126372658Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
13226372658Smrg            echo "$0: invalid mode: $mode" >&2
13326372658Smrg            exit 1;;
13426372658Smrg        esac
13526372658Smrg        shift;;
1364b0ead49Smrg
1374b0ead49Smrg    -o) chowncmd="$chownprog $2"
13826372658Smrg        shift;;
1394b0ead49Smrg
14052b01e16Smrg    -s) stripcmd=$stripprog;;
1414b0ead49Smrg
14226372658Smrg    -t)
14326372658Smrg        is_target_a_directory=always
14426372658Smrg        dst_arg=$2
14526372658Smrg        # Protect names problematic for 'test' and other utilities.
14626372658Smrg        case $dst_arg in
14726372658Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14826372658Smrg        esac
14926372658Smrg        shift;;
1504b0ead49Smrg
15126372658Smrg    -T) is_target_a_directory=never;;
1524b0ead49Smrg
1534b0ead49Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1544b0ead49Smrg
15526372658Smrg    --) shift
15626372658Smrg        break;;
15752b01e16Smrg
15826372658Smrg    -*) echo "$0: invalid option: $1" >&2
15926372658Smrg        exit 1;;
16052b01e16Smrg
16152b01e16Smrg    *)  break;;
1624b0ead49Smrg  esac
16352b01e16Smrg  shift
1644b0ead49Smrgdone
1654b0ead49Smrg
16626372658Smrg# We allow the use of options -d and -T together, by making -d
16726372658Smrg# take the precedence; this is for compatibility with GNU install.
16826372658Smrg
16926372658Smrgif test -n "$dir_arg"; then
17026372658Smrg  if test -n "$dst_arg"; then
17126372658Smrg    echo "$0: target directory not allowed when installing a directory." >&2
17226372658Smrg    exit 1
17326372658Smrg  fi
17426372658Smrgfi
17526372658Smrg
17652b01e16Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17752b01e16Smrg  # When -d is used, all remaining arguments are directories to create.
17852b01e16Smrg  # When -t is used, the destination is already specified.
17952b01e16Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18052b01e16Smrg  for arg
18152b01e16Smrg  do
18252b01e16Smrg    if test -n "$dst_arg"; then
18352b01e16Smrg      # $@ is not empty: it contains at least $arg.
18452b01e16Smrg      set fnord "$@" "$dst_arg"
18552b01e16Smrg      shift # fnord
18652b01e16Smrg    fi
18752b01e16Smrg    shift # arg
18852b01e16Smrg    dst_arg=$arg
18926372658Smrg    # Protect names problematic for 'test' and other utilities.
1904e7590efSmrg    case $dst_arg in
1914e7590efSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1924e7590efSmrg    esac
19352b01e16Smrg  done
19452b01e16Smrgfi
19552b01e16Smrg
19652b01e16Smrgif test $# -eq 0; then
1974b0ead49Smrg  if test -z "$dir_arg"; then
1984b0ead49Smrg    echo "$0: no input file specified." >&2
1994b0ead49Smrg    exit 1
2004b0ead49Smrg  fi
20126372658Smrg  # It's OK to call 'install-sh -d' without argument.
2024b0ead49Smrg  # This can happen when creating conditional directories.
2034b0ead49Smrg  exit 0
2044b0ead49Smrgfi
2054b0ead49Smrg
20626372658Smrgif test -z "$dir_arg"; then
20726372658Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
20826372658Smrg    if test ! -d "$dst_arg"; then
20926372658Smrg      echo "$0: $dst_arg: Is not a directory." >&2
21026372658Smrg      exit 1
21126372658Smrg    fi
21226372658Smrg  fi
21326372658Smrgfi
21426372658Smrg
21552b01e16Smrgif test -z "$dir_arg"; then
2164e7590efSmrg  do_exit='(exit $ret); exit $ret'
2174e7590efSmrg  trap "ret=129; $do_exit" 1
2184e7590efSmrg  trap "ret=130; $do_exit" 2
2194e7590efSmrg  trap "ret=141; $do_exit" 13
2204e7590efSmrg  trap "ret=143; $do_exit" 15
22152b01e16Smrg
22252b01e16Smrg  # Set umask so as not to create temps with too-generous modes.
22352b01e16Smrg  # However, 'strip' requires both read and write access to temps.
22452b01e16Smrg  case $mode in
22552b01e16Smrg    # Optimize common cases.
22652b01e16Smrg    *644) cp_umask=133;;
22752b01e16Smrg    *755) cp_umask=22;;
22852b01e16Smrg
22952b01e16Smrg    *[0-7])
23052b01e16Smrg      if test -z "$stripcmd"; then
23126372658Smrg        u_plus_rw=
23252b01e16Smrg      else
23326372658Smrg        u_plus_rw='% 200'
23452b01e16Smrg      fi
23552b01e16Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23652b01e16Smrg    *)
23752b01e16Smrg      if test -z "$stripcmd"; then
23826372658Smrg        u_plus_rw=
23952b01e16Smrg      else
24026372658Smrg        u_plus_rw=,u+rw
24152b01e16Smrg      fi
24252b01e16Smrg      cp_umask=$mode$u_plus_rw;;
24352b01e16Smrg  esac
24452b01e16Smrgfi
24552b01e16Smrg
2464b0ead49Smrgfor src
2474b0ead49Smrgdo
24826372658Smrg  # Protect names problematic for 'test' and other utilities.
2494b0ead49Smrg  case $src in
2504e7590efSmrg    -* | [=\(\)!]) src=./$src;;
2514b0ead49Smrg  esac
2524b0ead49Smrg
2534b0ead49Smrg  if test -n "$dir_arg"; then
2544b0ead49Smrg    dst=$src
25552b01e16Smrg    dstdir=$dst
25652b01e16Smrg    test -d "$dstdir"
25752b01e16Smrg    dstdir_status=$?
2584b0ead49Smrg  else
25952b01e16Smrg
2604b0ead49Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2614b0ead49Smrg    # might cause directories to be created, which would be especially bad
2624b0ead49Smrg    # if $src (and thus $dsttmp) contains '*'.
2634b0ead49Smrg    if test ! -f "$src" && test ! -d "$src"; then
2644b0ead49Smrg      echo "$0: $src does not exist." >&2
2654b0ead49Smrg      exit 1
2664b0ead49Smrg    fi
2674b0ead49Smrg
26852b01e16Smrg    if test -z "$dst_arg"; then
2694b0ead49Smrg      echo "$0: no destination specified." >&2
2704b0ead49Smrg      exit 1
2714b0ead49Smrg    fi
27252b01e16Smrg    dst=$dst_arg
2734b0ead49Smrg
2744b0ead49Smrg    # If destination is a directory, append the input filename; won't work
2754b0ead49Smrg    # if double slashes aren't ignored.
2764b0ead49Smrg    if test -d "$dst"; then
27726372658Smrg      if test "$is_target_a_directory" = never; then
27826372658Smrg        echo "$0: $dst_arg: Is a directory" >&2
27926372658Smrg        exit 1
2804b0ead49Smrg      fi
28152b01e16Smrg      dstdir=$dst
28252b01e16Smrg      dst=$dstdir/`basename "$src"`
28352b01e16Smrg      dstdir_status=0
28452b01e16Smrg    else
28526372658Smrg      dstdir=`dirname "$dst"`
28652b01e16Smrg      test -d "$dstdir"
28752b01e16Smrg      dstdir_status=$?
2884b0ead49Smrg    fi
2894b0ead49Smrg  fi
2904b0ead49Smrg
29152b01e16Smrg  obsolete_mkdir_used=false
29252b01e16Smrg
29352b01e16Smrg  if test $dstdir_status != 0; then
29452b01e16Smrg    case $posix_mkdir in
29552b01e16Smrg      '')
29626372658Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
29726372658Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
29826372658Smrg        umask=`umask`
29926372658Smrg        case $stripcmd.$umask in
30026372658Smrg          # Optimize common cases.
30126372658Smrg          *[2367][2367]) mkdir_umask=$umask;;
30226372658Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
30326372658Smrg
30426372658Smrg          *[0-7])
30526372658Smrg            mkdir_umask=`expr $umask + 22 \
30626372658Smrg              - $umask % 100 % 40 + $umask % 20 \
30726372658Smrg              - $umask % 10 % 4 + $umask % 2
30826372658Smrg            `;;
30926372658Smrg          *) mkdir_umask=$umask,go-w;;
31026372658Smrg        esac
31126372658Smrg
31226372658Smrg        # With -d, create the new directory with the user-specified mode.
31326372658Smrg        # Otherwise, rely on $mkdir_umask.
31426372658Smrg        if test -n "$dir_arg"; then
31526372658Smrg          mkdir_mode=-m$mode
31626372658Smrg        else
31726372658Smrg          mkdir_mode=
31826372658Smrg        fi
31926372658Smrg
32026372658Smrg        posix_mkdir=false
32126372658Smrg        case $umask in
32226372658Smrg          *[123567][0-7][0-7])
32326372658Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
32426372658Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
32526372658Smrg            ;;
32626372658Smrg          *)
32726372658Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
32826372658Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
32926372658Smrg
33026372658Smrg            if (umask $mkdir_umask &&
33126372658Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
33226372658Smrg            then
33326372658Smrg              if test -z "$dir_arg" || {
33426372658Smrg                   # Check for POSIX incompatibilities with -m.
33526372658Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
33626372658Smrg                   # other-writable bit of parent directory when it shouldn't.
33726372658Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
33826372658Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
33926372658Smrg                   case $ls_ld_tmpdir in
34026372658Smrg                     d????-?r-*) different_mode=700;;
34126372658Smrg                     d????-?--*) different_mode=755;;
34226372658Smrg                     *) false;;
34326372658Smrg                   esac &&
34426372658Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
34526372658Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
34626372658Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
34726372658Smrg                   }
34826372658Smrg                 }
34926372658Smrg              then posix_mkdir=:
35026372658Smrg              fi
35126372658Smrg              rmdir "$tmpdir/d" "$tmpdir"
35226372658Smrg            else
35326372658Smrg              # Remove any dirs left behind by ancient mkdir implementations.
35426372658Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
35526372658Smrg            fi
35626372658Smrg            trap '' 0;;
35726372658Smrg        esac;;
35852b01e16Smrg    esac
3594b0ead49Smrg
36052b01e16Smrg    if
36152b01e16Smrg      $posix_mkdir && (
36226372658Smrg        umask $mkdir_umask &&
36326372658Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
36452b01e16Smrg      )
36552b01e16Smrg    then :
36652b01e16Smrg    else
3674b0ead49Smrg
36852b01e16Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
36952b01e16Smrg      # or it failed possibly due to a race condition.  Create the
37052b01e16Smrg      # directory the slow way, step by step, checking for races as we go.
3714b0ead49Smrg
37252b01e16Smrg      case $dstdir in
37326372658Smrg        /*) prefix='/';;
37426372658Smrg        [-=\(\)!]*) prefix='./';;
37526372658Smrg        *)  prefix='';;
37652b01e16Smrg      esac
3774b0ead49Smrg
37852b01e16Smrg      oIFS=$IFS
37952b01e16Smrg      IFS=/
38026372658Smrg      set -f
38152b01e16Smrg      set fnord $dstdir
3824b0ead49Smrg      shift
38326372658Smrg      set +f
38452b01e16Smrg      IFS=$oIFS
38552b01e16Smrg
38652b01e16Smrg      prefixes=
38752b01e16Smrg
38852b01e16Smrg      for d
38952b01e16Smrg      do
39026372658Smrg        test X"$d" = X && continue
39126372658Smrg
39226372658Smrg        prefix=$prefix$d
39326372658Smrg        if test -d "$prefix"; then
39426372658Smrg          prefixes=
39526372658Smrg        else
39626372658Smrg          if $posix_mkdir; then
39726372658Smrg            (umask=$mkdir_umask &&
39826372658Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
39926372658Smrg            # Don't fail if two instances are running concurrently.
40026372658Smrg            test -d "$prefix" || exit 1
40126372658Smrg          else
40226372658Smrg            case $prefix in
40326372658Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
40426372658Smrg              *) qprefix=$prefix;;
40526372658Smrg            esac
40626372658Smrg            prefixes="$prefixes '$qprefix'"
40726372658Smrg          fi
40826372658Smrg        fi
40926372658Smrg        prefix=$prefix/
41052b01e16Smrg      done
41152b01e16Smrg
41252b01e16Smrg      if test -n "$prefixes"; then
41326372658Smrg        # Don't fail if two instances are running concurrently.
41426372658Smrg        (umask $mkdir_umask &&
41526372658Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
41626372658Smrg          test -d "$dstdir" || exit 1
41726372658Smrg        obsolete_mkdir_used=true
4184b0ead49Smrg      fi
41952b01e16Smrg    fi
4204b0ead49Smrg  fi
4214b0ead49Smrg
4224b0ead49Smrg  if test -n "$dir_arg"; then
42352b01e16Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
42452b01e16Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
42552b01e16Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
42652b01e16Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4274b0ead49Smrg  else
4284b0ead49Smrg
4294b0ead49Smrg    # Make a couple of temp file names in the proper directory.
4304b0ead49Smrg    dsttmp=$dstdir/_inst.$$_
4314b0ead49Smrg    rmtmp=$dstdir/_rm.$$_
4324b0ead49Smrg
4334b0ead49Smrg    # Trap to clean up those temp files at exit.
4344b0ead49Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4354b0ead49Smrg
4364b0ead49Smrg    # Copy the file name to the temp name.
43752b01e16Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4384b0ead49Smrg
4394b0ead49Smrg    # and set any options; do chmod last to preserve setuid bits.
4404b0ead49Smrg    #
4414b0ead49Smrg    # If any of these fail, we abort the whole thing.  If we want to
4424b0ead49Smrg    # ignore errors from any of these, just make sure not to ignore
4434b0ead49Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4444b0ead49Smrg    #
44552b01e16Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
44652b01e16Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
44752b01e16Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
44852b01e16Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
44952b01e16Smrg
45052b01e16Smrg    # If -C, don't bother to copy if it wouldn't change the file.
45152b01e16Smrg    if $copy_on_change &&
45226372658Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
45326372658Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
45426372658Smrg       set -f &&
45552b01e16Smrg       set X $old && old=:$2:$4:$5:$6 &&
45652b01e16Smrg       set X $new && new=:$2:$4:$5:$6 &&
45726372658Smrg       set +f &&
45852b01e16Smrg       test "$old" = "$new" &&
45952b01e16Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
46052b01e16Smrg    then
46152b01e16Smrg      rm -f "$dsttmp"
46252b01e16Smrg    else
46352b01e16Smrg      # Rename the file to the real destination.
46452b01e16Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
46552b01e16Smrg
46652b01e16Smrg      # The rename failed, perhaps because mv can't rename something else
46752b01e16Smrg      # to itself, or perhaps because mv is so ancient that it does not
46852b01e16Smrg      # support -f.
46952b01e16Smrg      {
47026372658Smrg        # Now remove or move aside any old file at destination location.
47126372658Smrg        # We try this two ways since rm can't unlink itself on some
47226372658Smrg        # systems and the destination file might be busy for other
47326372658Smrg        # reasons.  In this case, the final cleanup might fail but the new
47426372658Smrg        # file should still install successfully.
47526372658Smrg        {
47626372658Smrg          test ! -f "$dst" ||
47726372658Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
47826372658Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
47926372658Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
48026372658Smrg          } ||
48126372658Smrg          { echo "$0: cannot unlink or rename $dst" >&2
48226372658Smrg            (exit 1); exit 1
48326372658Smrg          }
48426372658Smrg        } &&
48526372658Smrg
48626372658Smrg        # Now rename the file to the real destination.
48726372658Smrg        $doit $mvcmd "$dsttmp" "$dst"
48852b01e16Smrg      }
48952b01e16Smrg    fi || exit 1
49052b01e16Smrg
49152b01e16Smrg    trap '' 0
49252b01e16Smrg  fi
4934b0ead49Smrgdone
4944b0ead49Smrg
4954b0ead49Smrg# Local variables:
4964b0ead49Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4974b0ead49Smrg# time-stamp-start: "scriptversion="
4984b0ead49Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
49952b01e16Smrg# time-stamp-time-zone: "UTC"
50052b01e16Smrg# time-stamp-end: "; # UTC"
5014b0ead49Smrg# End:
502