install-sh revision 19d64aee
14d9b34d9Smrg#!/bin/sh
24d9b34d9Smrg# install - install a program, script, or datafile
34d9b34d9Smrg
419d64aeeSmrgscriptversion=2013-12-25.23; # UTC
54d9b34d9Smrg
64d9b34d9Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
74d9b34d9Smrg# later released in X11R6 (xc/config/util/install.sh) with the
84d9b34d9Smrg# following copyright and license.
94d9b34d9Smrg#
104d9b34d9Smrg# Copyright (C) 1994 X Consortium
114d9b34d9Smrg#
124d9b34d9Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
134d9b34d9Smrg# of this software and associated documentation files (the "Software"), to
144d9b34d9Smrg# deal in the Software without restriction, including without limitation the
154d9b34d9Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
164d9b34d9Smrg# sell copies of the Software, and to permit persons to whom the Software is
174d9b34d9Smrg# furnished to do so, subject to the following conditions:
184d9b34d9Smrg#
194d9b34d9Smrg# The above copyright notice and this permission notice shall be included in
204d9b34d9Smrg# all copies or substantial portions of the Software.
214d9b34d9Smrg#
224d9b34d9Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
234d9b34d9Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
244d9b34d9Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
254d9b34d9Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
264d9b34d9Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
274d9b34d9Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
284d9b34d9Smrg#
294d9b34d9Smrg# Except as contained in this notice, the name of the X Consortium shall not
304d9b34d9Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
314d9b34d9Smrg# ings in this Software without prior written authorization from the X Consor-
324d9b34d9Smrg# tium.
334d9b34d9Smrg#
344d9b34d9Smrg#
354d9b34d9Smrg# FSF changes to this file are in the public domain.
364d9b34d9Smrg#
374d9b34d9Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3819d64aeeSmrg# 'make' implicit rules from creating a file called install from it
394d9b34d9Smrg# when there is no Makefile.
404d9b34d9Smrg#
414d9b34d9Smrg# This script is compatible with the BSD install script, but was written
424d9b34d9Smrg# from scratch.
434d9b34d9Smrg
4419d64aeeSmrgtab='	'
454d9b34d9Smrgnl='
464d9b34d9Smrg'
4719d64aeeSmrgIFS=" $tab$nl"
484d9b34d9Smrg
4919d64aeeSmrg# Set DOITPROG to "echo" to test this script.
504d9b34d9Smrg
518512f934Smrgdoit=${DOITPROG-}
5219d64aeeSmrgdoit_exec=${doit:-exec}
534d9b34d9Smrg
544d9b34d9Smrg# Put in absolute file names if you don't have them in your path;
554d9b34d9Smrg# or use environment vars.
564d9b34d9Smrg
578512f934Smrgchgrpprog=${CHGRPPROG-chgrp}
588512f934Smrgchmodprog=${CHMODPROG-chmod}
598512f934Smrgchownprog=${CHOWNPROG-chown}
608512f934Smrgcmpprog=${CMPPROG-cmp}
618512f934Smrgcpprog=${CPPROG-cp}
628512f934Smrgmkdirprog=${MKDIRPROG-mkdir}
638512f934Smrgmvprog=${MVPROG-mv}
648512f934Smrgrmprog=${RMPROG-rm}
658512f934Smrgstripprog=${STRIPPROG-strip}
668512f934Smrg
674d9b34d9Smrgposix_mkdir=
684d9b34d9Smrg
694d9b34d9Smrg# Desired mode of installed file.
704d9b34d9Smrgmode=0755
714d9b34d9Smrg
728512f934Smrgchgrpcmd=
734d9b34d9Smrgchmodcmd=$chmodprog
744d9b34d9Smrgchowncmd=
758512f934Smrgmvcmd=$mvprog
764d9b34d9Smrgrmcmd="$rmprog -f"
778512f934Smrgstripcmd=
788512f934Smrg
794d9b34d9Smrgsrc=
804d9b34d9Smrgdst=
814d9b34d9Smrgdir_arg=
828512f934Smrgdst_arg=
838512f934Smrg
848512f934Smrgcopy_on_change=false
8519d64aeeSmrgis_target_a_directory=possibly
864d9b34d9Smrg
878512f934Smrgusage="\
888512f934SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
894d9b34d9Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
904d9b34d9Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
914d9b34d9Smrg   or: $0 [OPTION]... -d DIRECTORIES...
924d9b34d9Smrg
934d9b34d9SmrgIn the 1st form, copy SRCFILE to DSTFILE.
944d9b34d9SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
954d9b34d9SmrgIn the 4th, create DIRECTORIES.
964d9b34d9Smrg
974d9b34d9SmrgOptions:
988512f934Smrg     --help     display this help and exit.
998512f934Smrg     --version  display version info and exit.
1008512f934Smrg
1018512f934Smrg  -c            (ignored)
1028512f934Smrg  -C            install only if different (preserve the last data modification time)
1038512f934Smrg  -d            create directories instead of installing files.
1048512f934Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1058512f934Smrg  -m MODE       $chmodprog installed files to MODE.
1068512f934Smrg  -o USER       $chownprog installed files to USER.
1078512f934Smrg  -s            $stripprog installed files.
1088512f934Smrg  -t DIRECTORY  install into DIRECTORY.
1098512f934Smrg  -T            report an error if DSTFILE is a directory.
1104d9b34d9Smrg
1114d9b34d9SmrgEnvironment variables override the default commands:
1128512f934Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1138512f934Smrg  RMPROG STRIPPROG
1144d9b34d9Smrg"
1154d9b34d9Smrg
1164d9b34d9Smrgwhile test $# -ne 0; do
1174d9b34d9Smrg  case $1 in
1188512f934Smrg    -c) ;;
1198512f934Smrg
1208512f934Smrg    -C) copy_on_change=true;;
1214d9b34d9Smrg
1228512f934Smrg    -d) dir_arg=true;;
1234d9b34d9Smrg
124498372abSmrg    -g) chgrpcmd="$chgrpprog $2"
12519d64aeeSmrg        shift;;
1264d9b34d9Smrg
127498372abSmrg    --help) echo "$usage"; exit $?;;
1284d9b34d9Smrg
1294d9b34d9Smrg    -m) mode=$2
13019d64aeeSmrg        case $mode in
13119d64aeeSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
13219d64aeeSmrg            echo "$0: invalid mode: $mode" >&2
13319d64aeeSmrg            exit 1;;
13419d64aeeSmrg        esac
13519d64aeeSmrg        shift;;
1364d9b34d9Smrg
137498372abSmrg    -o) chowncmd="$chownprog $2"
13819d64aeeSmrg        shift;;
1394d9b34d9Smrg
1408512f934Smrg    -s) stripcmd=$stripprog;;
1414d9b34d9Smrg
14219d64aeeSmrg    -t)
14319d64aeeSmrg        is_target_a_directory=always
14419d64aeeSmrg        dst_arg=$2
14519d64aeeSmrg        # Protect names problematic for 'test' and other utilities.
14619d64aeeSmrg        case $dst_arg in
14719d64aeeSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14819d64aeeSmrg        esac
14919d64aeeSmrg        shift;;
1504d9b34d9Smrg
15119d64aeeSmrg    -T) is_target_a_directory=never;;
1524d9b34d9Smrg
153498372abSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1544d9b34d9Smrg
15519d64aeeSmrg    --) shift
15619d64aeeSmrg        break;;
1574d9b34d9Smrg
15819d64aeeSmrg    -*) echo "$0: invalid option: $1" >&2
15919d64aeeSmrg        exit 1;;
1604d9b34d9Smrg
1614d9b34d9Smrg    *)  break;;
1624d9b34d9Smrg  esac
1638512f934Smrg  shift
1644d9b34d9Smrgdone
1654d9b34d9Smrg
16619d64aeeSmrg# We allow the use of options -d and -T together, by making -d
16719d64aeeSmrg# take the precedence; this is for compatibility with GNU install.
16819d64aeeSmrg
16919d64aeeSmrgif test -n "$dir_arg"; then
17019d64aeeSmrg  if test -n "$dst_arg"; then
17119d64aeeSmrg    echo "$0: target directory not allowed when installing a directory." >&2
17219d64aeeSmrg    exit 1
17319d64aeeSmrg  fi
17419d64aeeSmrgfi
17519d64aeeSmrg
176498372abSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1774d9b34d9Smrg  # When -d is used, all remaining arguments are directories to create.
1784d9b34d9Smrg  # When -t is used, the destination is already specified.
1794d9b34d9Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1804d9b34d9Smrg  for arg
1814d9b34d9Smrg  do
1828512f934Smrg    if test -n "$dst_arg"; then
183498372abSmrg      # $@ is not empty: it contains at least $arg.
184498372abSmrg      set fnord "$@" "$dst_arg"
1854d9b34d9Smrg      shift # fnord
1864d9b34d9Smrg    fi
1874d9b34d9Smrg    shift # arg
1888512f934Smrg    dst_arg=$arg
18919d64aeeSmrg    # Protect names problematic for 'test' and other utilities.
190498372abSmrg    case $dst_arg in
191498372abSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192498372abSmrg    esac
1934d9b34d9Smrg  done
1944d9b34d9Smrgfi
1954d9b34d9Smrg
1964d9b34d9Smrgif test $# -eq 0; then
1974d9b34d9Smrg  if test -z "$dir_arg"; then
1984d9b34d9Smrg    echo "$0: no input file specified." >&2
1994d9b34d9Smrg    exit 1
2004d9b34d9Smrg  fi
20119d64aeeSmrg  # It's OK to call 'install-sh -d' without argument.
2024d9b34d9Smrg  # This can happen when creating conditional directories.
2034d9b34d9Smrg  exit 0
2044d9b34d9Smrgfi
2054d9b34d9Smrg
20619d64aeeSmrgif test -z "$dir_arg"; then
20719d64aeeSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
20819d64aeeSmrg    if test ! -d "$dst_arg"; then
20919d64aeeSmrg      echo "$0: $dst_arg: Is not a directory." >&2
21019d64aeeSmrg      exit 1
21119d64aeeSmrg    fi
21219d64aeeSmrg  fi
21319d64aeeSmrgfi
21419d64aeeSmrg
2154d9b34d9Smrgif test -z "$dir_arg"; then
216498372abSmrg  do_exit='(exit $ret); exit $ret'
217498372abSmrg  trap "ret=129; $do_exit" 1
218498372abSmrg  trap "ret=130; $do_exit" 2
219498372abSmrg  trap "ret=141; $do_exit" 13
220498372abSmrg  trap "ret=143; $do_exit" 15
2214d9b34d9Smrg
2224d9b34d9Smrg  # Set umask so as not to create temps with too-generous modes.
2234d9b34d9Smrg  # However, 'strip' requires both read and write access to temps.
2244d9b34d9Smrg  case $mode in
2254d9b34d9Smrg    # Optimize common cases.
2264d9b34d9Smrg    *644) cp_umask=133;;
2274d9b34d9Smrg    *755) cp_umask=22;;
2284d9b34d9Smrg
2294d9b34d9Smrg    *[0-7])
2304d9b34d9Smrg      if test -z "$stripcmd"; then
23119d64aeeSmrg        u_plus_rw=
2324d9b34d9Smrg      else
23319d64aeeSmrg        u_plus_rw='% 200'
2344d9b34d9Smrg      fi
235498372abSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2364d9b34d9Smrg    *)
2374d9b34d9Smrg      if test -z "$stripcmd"; then
23819d64aeeSmrg        u_plus_rw=
2394d9b34d9Smrg      else
24019d64aeeSmrg        u_plus_rw=,u+rw
2414d9b34d9Smrg      fi
242498372abSmrg      cp_umask=$mode$u_plus_rw;;
2434d9b34d9Smrg  esac
2444d9b34d9Smrgfi
2454d9b34d9Smrg
2464d9b34d9Smrgfor src
2474d9b34d9Smrgdo
24819d64aeeSmrg  # Protect names problematic for 'test' and other utilities.
2494d9b34d9Smrg  case $src in
250498372abSmrg    -* | [=\(\)!]) src=./$src;;
2514d9b34d9Smrg  esac
2524d9b34d9Smrg
2534d9b34d9Smrg  if test -n "$dir_arg"; then
2544d9b34d9Smrg    dst=$src
2554d9b34d9Smrg    dstdir=$dst
2564d9b34d9Smrg    test -d "$dstdir"
2574d9b34d9Smrg    dstdir_status=$?
2584d9b34d9Smrg  else
2594d9b34d9Smrg
260498372abSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2614d9b34d9Smrg    # might cause directories to be created, which would be especially bad
262498372abSmrg    # if $src (and thus $dsttmp) contains '*'.
263498372abSmrg    if test ! -f "$src" && test ! -d "$src"; then
264498372abSmrg      echo "$0: $src does not exist." >&2
2654d9b34d9Smrg      exit 1
2664d9b34d9Smrg    fi
2674d9b34d9Smrg
2688512f934Smrg    if test -z "$dst_arg"; then
2694d9b34d9Smrg      echo "$0: no destination specified." >&2
2704d9b34d9Smrg      exit 1
2714d9b34d9Smrg    fi
2728512f934Smrg    dst=$dst_arg
2734d9b34d9Smrg
2744d9b34d9Smrg    # If destination is a directory, append the input filename; won't work
2754d9b34d9Smrg    # if double slashes aren't ignored.
2764d9b34d9Smrg    if test -d "$dst"; then
27719d64aeeSmrg      if test "$is_target_a_directory" = never; then
27819d64aeeSmrg        echo "$0: $dst_arg: Is a directory" >&2
27919d64aeeSmrg        exit 1
2804d9b34d9Smrg      fi
2814d9b34d9Smrg      dstdir=$dst
282498372abSmrg      dst=$dstdir/`basename "$src"`
2834d9b34d9Smrg      dstdir_status=0
2844d9b34d9Smrg    else
28519d64aeeSmrg      dstdir=`dirname "$dst"`
2864d9b34d9Smrg      test -d "$dstdir"
2874d9b34d9Smrg      dstdir_status=$?
2884d9b34d9Smrg    fi
2894d9b34d9Smrg  fi
2904d9b34d9Smrg
2914d9b34d9Smrg  obsolete_mkdir_used=false
2924d9b34d9Smrg
2934d9b34d9Smrg  if test $dstdir_status != 0; then
2944d9b34d9Smrg    case $posix_mkdir in
2954d9b34d9Smrg      '')
29619d64aeeSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
29719d64aeeSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
29819d64aeeSmrg        umask=`umask`
29919d64aeeSmrg        case $stripcmd.$umask in
30019d64aeeSmrg          # Optimize common cases.
30119d64aeeSmrg          *[2367][2367]) mkdir_umask=$umask;;
30219d64aeeSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
30319d64aeeSmrg
30419d64aeeSmrg          *[0-7])
30519d64aeeSmrg            mkdir_umask=`expr $umask + 22 \
30619d64aeeSmrg              - $umask % 100 % 40 + $umask % 20 \
30719d64aeeSmrg              - $umask % 10 % 4 + $umask % 2
30819d64aeeSmrg            `;;
30919d64aeeSmrg          *) mkdir_umask=$umask,go-w;;
31019d64aeeSmrg        esac
31119d64aeeSmrg
31219d64aeeSmrg        # With -d, create the new directory with the user-specified mode.
31319d64aeeSmrg        # Otherwise, rely on $mkdir_umask.
31419d64aeeSmrg        if test -n "$dir_arg"; then
31519d64aeeSmrg          mkdir_mode=-m$mode
31619d64aeeSmrg        else
31719d64aeeSmrg          mkdir_mode=
31819d64aeeSmrg        fi
31919d64aeeSmrg
32019d64aeeSmrg        posix_mkdir=false
32119d64aeeSmrg        case $umask in
32219d64aeeSmrg          *[123567][0-7][0-7])
32319d64aeeSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
32419d64aeeSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
32519d64aeeSmrg            ;;
32619d64aeeSmrg          *)
32719d64aeeSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
32819d64aeeSmrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
32919d64aeeSmrg
33019d64aeeSmrg            if (umask $mkdir_umask &&
33119d64aeeSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
33219d64aeeSmrg            then
33319d64aeeSmrg              if test -z "$dir_arg" || {
33419d64aeeSmrg                   # Check for POSIX incompatibilities with -m.
33519d64aeeSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
33619d64aeeSmrg                   # other-writable bit of parent directory when it shouldn't.
33719d64aeeSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
33819d64aeeSmrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
33919d64aeeSmrg                   case $ls_ld_tmpdir in
34019d64aeeSmrg                     d????-?r-*) different_mode=700;;
34119d64aeeSmrg                     d????-?--*) different_mode=755;;
34219d64aeeSmrg                     *) false;;
34319d64aeeSmrg                   esac &&
34419d64aeeSmrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
34519d64aeeSmrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
34619d64aeeSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
34719d64aeeSmrg                   }
34819d64aeeSmrg                 }
34919d64aeeSmrg              then posix_mkdir=:
35019d64aeeSmrg              fi
35119d64aeeSmrg              rmdir "$tmpdir/d" "$tmpdir"
35219d64aeeSmrg            else
35319d64aeeSmrg              # Remove any dirs left behind by ancient mkdir implementations.
35419d64aeeSmrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
35519d64aeeSmrg            fi
35619d64aeeSmrg            trap '' 0;;
35719d64aeeSmrg        esac;;
3584d9b34d9Smrg    esac
3594d9b34d9Smrg
3604d9b34d9Smrg    if
3614d9b34d9Smrg      $posix_mkdir && (
36219d64aeeSmrg        umask $mkdir_umask &&
36319d64aeeSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3644d9b34d9Smrg      )
3654d9b34d9Smrg    then :
3664d9b34d9Smrg    else
3674d9b34d9Smrg
3684d9b34d9Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3694d9b34d9Smrg      # or it failed possibly due to a race condition.  Create the
3704d9b34d9Smrg      # directory the slow way, step by step, checking for races as we go.
3714d9b34d9Smrg
3724d9b34d9Smrg      case $dstdir in
37319d64aeeSmrg        /*) prefix='/';;
37419d64aeeSmrg        [-=\(\)!]*) prefix='./';;
37519d64aeeSmrg        *)  prefix='';;
3764d9b34d9Smrg      esac
3774d9b34d9Smrg
3784d9b34d9Smrg      oIFS=$IFS
3794d9b34d9Smrg      IFS=/
38019d64aeeSmrg      set -f
3814d9b34d9Smrg      set fnord $dstdir
3824d9b34d9Smrg      shift
38319d64aeeSmrg      set +f
3844d9b34d9Smrg      IFS=$oIFS
3854d9b34d9Smrg
3864d9b34d9Smrg      prefixes=
3874d9b34d9Smrg
3884d9b34d9Smrg      for d
3894d9b34d9Smrg      do
39019d64aeeSmrg        test X"$d" = X && continue
39119d64aeeSmrg
39219d64aeeSmrg        prefix=$prefix$d
39319d64aeeSmrg        if test -d "$prefix"; then
39419d64aeeSmrg          prefixes=
39519d64aeeSmrg        else
39619d64aeeSmrg          if $posix_mkdir; then
39719d64aeeSmrg            (umask=$mkdir_umask &&
39819d64aeeSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
39919d64aeeSmrg            # Don't fail if two instances are running concurrently.
40019d64aeeSmrg            test -d "$prefix" || exit 1
40119d64aeeSmrg          else
40219d64aeeSmrg            case $prefix in
40319d64aeeSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
40419d64aeeSmrg              *) qprefix=$prefix;;
40519d64aeeSmrg            esac
40619d64aeeSmrg            prefixes="$prefixes '$qprefix'"
40719d64aeeSmrg          fi
40819d64aeeSmrg        fi
40919d64aeeSmrg        prefix=$prefix/
4104d9b34d9Smrg      done
4114d9b34d9Smrg
4124d9b34d9Smrg      if test -n "$prefixes"; then
41319d64aeeSmrg        # Don't fail if two instances are running concurrently.
41419d64aeeSmrg        (umask $mkdir_umask &&
41519d64aeeSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
41619d64aeeSmrg          test -d "$dstdir" || exit 1
41719d64aeeSmrg        obsolete_mkdir_used=true
4184d9b34d9Smrg      fi
4194d9b34d9Smrg    fi
4204d9b34d9Smrg  fi
4214d9b34d9Smrg
4224d9b34d9Smrg  if test -n "$dir_arg"; then
423498372abSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424498372abSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425498372abSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426498372abSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4274d9b34d9Smrg  else
4284d9b34d9Smrg
4294d9b34d9Smrg    # Make a couple of temp file names in the proper directory.
430498372abSmrg    dsttmp=$dstdir/_inst.$$_
431498372abSmrg    rmtmp=$dstdir/_rm.$$_
4324d9b34d9Smrg
4334d9b34d9Smrg    # Trap to clean up those temp files at exit.
434498372abSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4354d9b34d9Smrg
4364d9b34d9Smrg    # Copy the file name to the temp name.
437498372abSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4384d9b34d9Smrg
4394d9b34d9Smrg    # and set any options; do chmod last to preserve setuid bits.
4404d9b34d9Smrg    #
4414d9b34d9Smrg    # If any of these fail, we abort the whole thing.  If we want to
4424d9b34d9Smrg    # ignore errors from any of these, just make sure not to ignore
443498372abSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4444d9b34d9Smrg    #
445498372abSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446498372abSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447498372abSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448498372abSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4498512f934Smrg
4508512f934Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4518512f934Smrg    if $copy_on_change &&
45219d64aeeSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
45319d64aeeSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
45419d64aeeSmrg       set -f &&
455498372abSmrg       set X $old && old=:$2:$4:$5:$6 &&
456498372abSmrg       set X $new && new=:$2:$4:$5:$6 &&
45719d64aeeSmrg       set +f &&
458498372abSmrg       test "$old" = "$new" &&
459498372abSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4608512f934Smrg    then
4618512f934Smrg      rm -f "$dsttmp"
4628512f934Smrg    else
4638512f934Smrg      # Rename the file to the real destination.
464498372abSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4658512f934Smrg
4668512f934Smrg      # The rename failed, perhaps because mv can't rename something else
4678512f934Smrg      # to itself, or perhaps because mv is so ancient that it does not
4688512f934Smrg      # support -f.
4698512f934Smrg      {
47019d64aeeSmrg        # Now remove or move aside any old file at destination location.
47119d64aeeSmrg        # We try this two ways since rm can't unlink itself on some
47219d64aeeSmrg        # systems and the destination file might be busy for other
47319d64aeeSmrg        # reasons.  In this case, the final cleanup might fail but the new
47419d64aeeSmrg        # file should still install successfully.
47519d64aeeSmrg        {
47619d64aeeSmrg          test ! -f "$dst" ||
47719d64aeeSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
47819d64aeeSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
47919d64aeeSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
48019d64aeeSmrg          } ||
48119d64aeeSmrg          { echo "$0: cannot unlink or rename $dst" >&2
48219d64aeeSmrg            (exit 1); exit 1
48319d64aeeSmrg          }
48419d64aeeSmrg        } &&
48519d64aeeSmrg
48619d64aeeSmrg        # Now rename the file to the real destination.
48719d64aeeSmrg        $doit $mvcmd "$dsttmp" "$dst"
4888512f934Smrg      }
4898512f934Smrg    fi || exit 1
4904d9b34d9Smrg
4914d9b34d9Smrg    trap '' 0
4924d9b34d9Smrg  fi
4934d9b34d9Smrgdone
4944d9b34d9Smrg
4954d9b34d9Smrg# Local variables:
4964d9b34d9Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4974d9b34d9Smrg# time-stamp-start: "scriptversion="
4984d9b34d9Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
4998512f934Smrg# time-stamp-time-zone: "UTC"
5008512f934Smrg# time-stamp-end: "; # UTC"
5014d9b34d9Smrg# End:
502