install-sh revision c6a6acfb
166fe65f6Smrg#!/bin/sh
266fe65f6Smrg# install - install a program, script, or datafile
366fe65f6Smrg
4c6a6acfbSmrgscriptversion=2013-12-25.23; # UTC
566fe65f6Smrg
666fe65f6Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
766fe65f6Smrg# later released in X11R6 (xc/config/util/install.sh) with the
866fe65f6Smrg# following copyright and license.
966fe65f6Smrg#
1066fe65f6Smrg# Copyright (C) 1994 X Consortium
1166fe65f6Smrg#
1266fe65f6Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1366fe65f6Smrg# of this software and associated documentation files (the "Software"), to
1466fe65f6Smrg# deal in the Software without restriction, including without limitation the
1566fe65f6Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1666fe65f6Smrg# sell copies of the Software, and to permit persons to whom the Software is
1766fe65f6Smrg# furnished to do so, subject to the following conditions:
1866fe65f6Smrg#
1966fe65f6Smrg# The above copyright notice and this permission notice shall be included in
2066fe65f6Smrg# all copies or substantial portions of the Software.
2166fe65f6Smrg#
2266fe65f6Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2366fe65f6Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2466fe65f6Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2566fe65f6Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2666fe65f6Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2766fe65f6Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2866fe65f6Smrg#
2966fe65f6Smrg# Except as contained in this notice, the name of the X Consortium shall not
3066fe65f6Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3166fe65f6Smrg# ings in this Software without prior written authorization from the X Consor-
3266fe65f6Smrg# tium.
3366fe65f6Smrg#
3466fe65f6Smrg#
3566fe65f6Smrg# FSF changes to this file are in the public domain.
3666fe65f6Smrg#
3766fe65f6Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38c6a6acfbSmrg# 'make' implicit rules from creating a file called install from it
3966fe65f6Smrg# when there is no Makefile.
4066fe65f6Smrg#
4166fe65f6Smrg# This script is compatible with the BSD install script, but was written
4289d09728Smrg# from scratch.
4389d09728Smrg
44c6a6acfbSmrgtab='	'
4589d09728Smrgnl='
4689d09728Smrg'
47c6a6acfbSmrgIFS=" $tab$nl"
4866fe65f6Smrg
49c6a6acfbSmrg# Set DOITPROG to "echo" to test this script.
5066fe65f6Smrg
5189d09728Smrgdoit=${DOITPROG-}
52c6a6acfbSmrgdoit_exec=${doit:-exec}
5366fe65f6Smrg
5489d09728Smrg# Put in absolute file names if you don't have them in your path;
5589d09728Smrg# or use environment vars.
5689d09728Smrg
5789d09728Smrgchgrpprog=${CHGRPPROG-chgrp}
5889d09728Smrgchmodprog=${CHMODPROG-chmod}
5989d09728Smrgchownprog=${CHOWNPROG-chown}
6089d09728Smrgcmpprog=${CMPPROG-cmp}
6189d09728Smrgcpprog=${CPPROG-cp}
6289d09728Smrgmkdirprog=${MKDIRPROG-mkdir}
6389d09728Smrgmvprog=${MVPROG-mv}
6489d09728Smrgrmprog=${RMPROG-rm}
6589d09728Smrgstripprog=${STRIPPROG-strip}
6689d09728Smrg
6789d09728Smrgposix_mkdir=
6889d09728Smrg
6989d09728Smrg# Desired mode of installed file.
7089d09728Smrgmode=0755
7166fe65f6Smrg
7266fe65f6Smrgchgrpcmd=
7389d09728Smrgchmodcmd=$chmodprog
7489d09728Smrgchowncmd=
7589d09728Smrgmvcmd=$mvprog
7666fe65f6Smrgrmcmd="$rmprog -f"
7789d09728Smrgstripcmd=
7889d09728Smrg
7966fe65f6Smrgsrc=
8066fe65f6Smrgdst=
8166fe65f6Smrgdir_arg=
8289d09728Smrgdst_arg=
8389d09728Smrg
8489d09728Smrgcopy_on_change=false
85c6a6acfbSmrgis_target_a_directory=possibly
8666fe65f6Smrg
8789d09728Smrgusage="\
8889d09728SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8966fe65f6Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9066fe65f6Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9166fe65f6Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9266fe65f6Smrg
9366fe65f6SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9466fe65f6SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9566fe65f6SmrgIn the 4th, create DIRECTORIES.
9666fe65f6Smrg
9766fe65f6SmrgOptions:
9889d09728Smrg     --help     display this help and exit.
9989d09728Smrg     --version  display version info and exit.
10089d09728Smrg
10189d09728Smrg  -c            (ignored)
10289d09728Smrg  -C            install only if different (preserve the last data modification time)
10389d09728Smrg  -d            create directories instead of installing files.
10489d09728Smrg  -g GROUP      $chgrpprog installed files to GROUP.
10589d09728Smrg  -m MODE       $chmodprog installed files to MODE.
10689d09728Smrg  -o USER       $chownprog installed files to USER.
10789d09728Smrg  -s            $stripprog installed files.
10889d09728Smrg  -t DIRECTORY  install into DIRECTORY.
10989d09728Smrg  -T            report an error if DSTFILE is a directory.
11066fe65f6Smrg
11166fe65f6SmrgEnvironment variables override the default commands:
11289d09728Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
11389d09728Smrg  RMPROG STRIPPROG
11466fe65f6Smrg"
11566fe65f6Smrg
11689d09728Smrgwhile test $# -ne 0; do
11766fe65f6Smrg  case $1 in
11889d09728Smrg    -c) ;;
11989d09728Smrg
12089d09728Smrg    -C) copy_on_change=true;;
12166fe65f6Smrg
12289d09728Smrg    -d) dir_arg=true;;
12366fe65f6Smrg
12466fe65f6Smrg    -g) chgrpcmd="$chgrpprog $2"
125c6a6acfbSmrg        shift;;
12666fe65f6Smrg
12766fe65f6Smrg    --help) echo "$usage"; exit $?;;
12866fe65f6Smrg
12989d09728Smrg    -m) mode=$2
130c6a6acfbSmrg        case $mode in
131c6a6acfbSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132c6a6acfbSmrg            echo "$0: invalid mode: $mode" >&2
133c6a6acfbSmrg            exit 1;;
134c6a6acfbSmrg        esac
135c6a6acfbSmrg        shift;;
13666fe65f6Smrg
13766fe65f6Smrg    -o) chowncmd="$chownprog $2"
138c6a6acfbSmrg        shift;;
13966fe65f6Smrg
14089d09728Smrg    -s) stripcmd=$stripprog;;
14166fe65f6Smrg
142c6a6acfbSmrg    -t)
143c6a6acfbSmrg        is_target_a_directory=always
144c6a6acfbSmrg        dst_arg=$2
145c6a6acfbSmrg        # Protect names problematic for 'test' and other utilities.
146c6a6acfbSmrg        case $dst_arg in
147c6a6acfbSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148c6a6acfbSmrg        esac
149c6a6acfbSmrg        shift;;
15066fe65f6Smrg
151c6a6acfbSmrg    -T) is_target_a_directory=never;;
15266fe65f6Smrg
15366fe65f6Smrg    --version) echo "$0 $scriptversion"; exit $?;;
15466fe65f6Smrg
155c6a6acfbSmrg    --) shift
156c6a6acfbSmrg        break;;
15789d09728Smrg
158c6a6acfbSmrg    -*) echo "$0: invalid option: $1" >&2
159c6a6acfbSmrg        exit 1;;
16089d09728Smrg
16189d09728Smrg    *)  break;;
16266fe65f6Smrg  esac
16389d09728Smrg  shift
16466fe65f6Smrgdone
16566fe65f6Smrg
166c6a6acfbSmrg# We allow the use of options -d and -T together, by making -d
167c6a6acfbSmrg# take the precedence; this is for compatibility with GNU install.
168c6a6acfbSmrg
169c6a6acfbSmrgif test -n "$dir_arg"; then
170c6a6acfbSmrg  if test -n "$dst_arg"; then
171c6a6acfbSmrg    echo "$0: target directory not allowed when installing a directory." >&2
172c6a6acfbSmrg    exit 1
173c6a6acfbSmrg  fi
174c6a6acfbSmrgfi
175c6a6acfbSmrg
17689d09728Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17789d09728Smrg  # When -d is used, all remaining arguments are directories to create.
17889d09728Smrg  # When -t is used, the destination is already specified.
17989d09728Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18089d09728Smrg  for arg
18189d09728Smrg  do
18289d09728Smrg    if test -n "$dst_arg"; then
18389d09728Smrg      # $@ is not empty: it contains at least $arg.
18489d09728Smrg      set fnord "$@" "$dst_arg"
18589d09728Smrg      shift # fnord
18689d09728Smrg    fi
18789d09728Smrg    shift # arg
18889d09728Smrg    dst_arg=$arg
189c6a6acfbSmrg    # Protect names problematic for 'test' and other utilities.
190c6a6acfbSmrg    case $dst_arg in
191c6a6acfbSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192c6a6acfbSmrg    esac
19389d09728Smrg  done
19489d09728Smrgfi
19589d09728Smrg
19689d09728Smrgif test $# -eq 0; then
19766fe65f6Smrg  if test -z "$dir_arg"; then
19866fe65f6Smrg    echo "$0: no input file specified." >&2
19966fe65f6Smrg    exit 1
20066fe65f6Smrg  fi
201c6a6acfbSmrg  # It's OK to call 'install-sh -d' without argument.
20266fe65f6Smrg  # This can happen when creating conditional directories.
20366fe65f6Smrg  exit 0
20466fe65f6Smrgfi
20566fe65f6Smrg
20689d09728Smrgif test -z "$dir_arg"; then
207c6a6acfbSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208c6a6acfbSmrg    if test ! -d "$dst_arg"; then
209c6a6acfbSmrg      echo "$0: $dst_arg: Is not a directory." >&2
210c6a6acfbSmrg      exit 1
211c6a6acfbSmrg    fi
212c6a6acfbSmrg  fi
213c6a6acfbSmrgfi
214c6a6acfbSmrg
215c6a6acfbSmrgif test -z "$dir_arg"; then
216c6a6acfbSmrg  do_exit='(exit $ret); exit $ret'
217c6a6acfbSmrg  trap "ret=129; $do_exit" 1
218c6a6acfbSmrg  trap "ret=130; $do_exit" 2
219c6a6acfbSmrg  trap "ret=141; $do_exit" 13
220c6a6acfbSmrg  trap "ret=143; $do_exit" 15
22189d09728Smrg
22289d09728Smrg  # Set umask so as not to create temps with too-generous modes.
22389d09728Smrg  # However, 'strip' requires both read and write access to temps.
22489d09728Smrg  case $mode in
22589d09728Smrg    # Optimize common cases.
22689d09728Smrg    *644) cp_umask=133;;
22789d09728Smrg    *755) cp_umask=22;;
22889d09728Smrg
22989d09728Smrg    *[0-7])
23089d09728Smrg      if test -z "$stripcmd"; then
231c6a6acfbSmrg        u_plus_rw=
23289d09728Smrg      else
233c6a6acfbSmrg        u_plus_rw='% 200'
23489d09728Smrg      fi
23589d09728Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23689d09728Smrg    *)
23789d09728Smrg      if test -z "$stripcmd"; then
238c6a6acfbSmrg        u_plus_rw=
23989d09728Smrg      else
240c6a6acfbSmrg        u_plus_rw=,u+rw
24189d09728Smrg      fi
24289d09728Smrg      cp_umask=$mode$u_plus_rw;;
24389d09728Smrg  esac
24489d09728Smrgfi
24589d09728Smrg
24666fe65f6Smrgfor src
24766fe65f6Smrgdo
248c6a6acfbSmrg  # Protect names problematic for 'test' and other utilities.
24966fe65f6Smrg  case $src in
250c6a6acfbSmrg    -* | [=\(\)!]) src=./$src;;
25166fe65f6Smrg  esac
25266fe65f6Smrg
25366fe65f6Smrg  if test -n "$dir_arg"; then
25466fe65f6Smrg    dst=$src
25589d09728Smrg    dstdir=$dst
25689d09728Smrg    test -d "$dstdir"
25789d09728Smrg    dstdir_status=$?
25866fe65f6Smrg  else
25989d09728Smrg
26066fe65f6Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26166fe65f6Smrg    # might cause directories to be created, which would be especially bad
26266fe65f6Smrg    # if $src (and thus $dsttmp) contains '*'.
26366fe65f6Smrg    if test ! -f "$src" && test ! -d "$src"; then
26466fe65f6Smrg      echo "$0: $src does not exist." >&2
26566fe65f6Smrg      exit 1
26666fe65f6Smrg    fi
26766fe65f6Smrg
26889d09728Smrg    if test -z "$dst_arg"; then
26966fe65f6Smrg      echo "$0: no destination specified." >&2
27066fe65f6Smrg      exit 1
27166fe65f6Smrg    fi
27289d09728Smrg    dst=$dst_arg
27366fe65f6Smrg
27466fe65f6Smrg    # If destination is a directory, append the input filename; won't work
27566fe65f6Smrg    # if double slashes aren't ignored.
27666fe65f6Smrg    if test -d "$dst"; then
277c6a6acfbSmrg      if test "$is_target_a_directory" = never; then
278c6a6acfbSmrg        echo "$0: $dst_arg: Is a directory" >&2
279c6a6acfbSmrg        exit 1
28066fe65f6Smrg      fi
28189d09728Smrg      dstdir=$dst
28289d09728Smrg      dst=$dstdir/`basename "$src"`
28389d09728Smrg      dstdir_status=0
28489d09728Smrg    else
285c6a6acfbSmrg      dstdir=`dirname "$dst"`
28689d09728Smrg      test -d "$dstdir"
28789d09728Smrg      dstdir_status=$?
28866fe65f6Smrg    fi
28966fe65f6Smrg  fi
29066fe65f6Smrg
29189d09728Smrg  obsolete_mkdir_used=false
29289d09728Smrg
29389d09728Smrg  if test $dstdir_status != 0; then
29489d09728Smrg    case $posix_mkdir in
29589d09728Smrg      '')
296c6a6acfbSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
297c6a6acfbSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
298c6a6acfbSmrg        umask=`umask`
299c6a6acfbSmrg        case $stripcmd.$umask in
300c6a6acfbSmrg          # Optimize common cases.
301c6a6acfbSmrg          *[2367][2367]) mkdir_umask=$umask;;
302c6a6acfbSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303c6a6acfbSmrg
304c6a6acfbSmrg          *[0-7])
305c6a6acfbSmrg            mkdir_umask=`expr $umask + 22 \
306c6a6acfbSmrg              - $umask % 100 % 40 + $umask % 20 \
307c6a6acfbSmrg              - $umask % 10 % 4 + $umask % 2
308c6a6acfbSmrg            `;;
309c6a6acfbSmrg          *) mkdir_umask=$umask,go-w;;
310c6a6acfbSmrg        esac
311c6a6acfbSmrg
312c6a6acfbSmrg        # With -d, create the new directory with the user-specified mode.
313c6a6acfbSmrg        # Otherwise, rely on $mkdir_umask.
314c6a6acfbSmrg        if test -n "$dir_arg"; then
315c6a6acfbSmrg          mkdir_mode=-m$mode
316c6a6acfbSmrg        else
317c6a6acfbSmrg          mkdir_mode=
318c6a6acfbSmrg        fi
319c6a6acfbSmrg
320c6a6acfbSmrg        posix_mkdir=false
321c6a6acfbSmrg        case $umask in
322c6a6acfbSmrg          *[123567][0-7][0-7])
323c6a6acfbSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324c6a6acfbSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325c6a6acfbSmrg            ;;
326c6a6acfbSmrg          *)
327c6a6acfbSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328c6a6acfbSmrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329c6a6acfbSmrg
330c6a6acfbSmrg            if (umask $mkdir_umask &&
331c6a6acfbSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332c6a6acfbSmrg            then
333c6a6acfbSmrg              if test -z "$dir_arg" || {
334c6a6acfbSmrg                   # Check for POSIX incompatibilities with -m.
335c6a6acfbSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336c6a6acfbSmrg                   # other-writable bit of parent directory when it shouldn't.
337c6a6acfbSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338c6a6acfbSmrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339c6a6acfbSmrg                   case $ls_ld_tmpdir in
340c6a6acfbSmrg                     d????-?r-*) different_mode=700;;
341c6a6acfbSmrg                     d????-?--*) different_mode=755;;
342c6a6acfbSmrg                     *) false;;
343c6a6acfbSmrg                   esac &&
344c6a6acfbSmrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345c6a6acfbSmrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346c6a6acfbSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347c6a6acfbSmrg                   }
348c6a6acfbSmrg                 }
349c6a6acfbSmrg              then posix_mkdir=:
350c6a6acfbSmrg              fi
351c6a6acfbSmrg              rmdir "$tmpdir/d" "$tmpdir"
352c6a6acfbSmrg            else
353c6a6acfbSmrg              # Remove any dirs left behind by ancient mkdir implementations.
354c6a6acfbSmrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355c6a6acfbSmrg            fi
356c6a6acfbSmrg            trap '' 0;;
357c6a6acfbSmrg        esac;;
35889d09728Smrg    esac
35966fe65f6Smrg
36089d09728Smrg    if
36189d09728Smrg      $posix_mkdir && (
362c6a6acfbSmrg        umask $mkdir_umask &&
363c6a6acfbSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
36489d09728Smrg      )
36589d09728Smrg    then :
36689d09728Smrg    else
36766fe65f6Smrg
36889d09728Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
36989d09728Smrg      # or it failed possibly due to a race condition.  Create the
37089d09728Smrg      # directory the slow way, step by step, checking for races as we go.
37166fe65f6Smrg
37289d09728Smrg      case $dstdir in
373c6a6acfbSmrg        /*) prefix='/';;
374c6a6acfbSmrg        [-=\(\)!]*) prefix='./';;
375c6a6acfbSmrg        *)  prefix='';;
37689d09728Smrg      esac
37766fe65f6Smrg
37889d09728Smrg      oIFS=$IFS
37989d09728Smrg      IFS=/
380c6a6acfbSmrg      set -f
38189d09728Smrg      set fnord $dstdir
38266fe65f6Smrg      shift
383c6a6acfbSmrg      set +f
38489d09728Smrg      IFS=$oIFS
38589d09728Smrg
38689d09728Smrg      prefixes=
38789d09728Smrg
38889d09728Smrg      for d
38989d09728Smrg      do
390c6a6acfbSmrg        test X"$d" = X && continue
391c6a6acfbSmrg
392c6a6acfbSmrg        prefix=$prefix$d
393c6a6acfbSmrg        if test -d "$prefix"; then
394c6a6acfbSmrg          prefixes=
395c6a6acfbSmrg        else
396c6a6acfbSmrg          if $posix_mkdir; then
397c6a6acfbSmrg            (umask=$mkdir_umask &&
398c6a6acfbSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399c6a6acfbSmrg            # Don't fail if two instances are running concurrently.
400c6a6acfbSmrg            test -d "$prefix" || exit 1
401c6a6acfbSmrg          else
402c6a6acfbSmrg            case $prefix in
403c6a6acfbSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404c6a6acfbSmrg              *) qprefix=$prefix;;
405c6a6acfbSmrg            esac
406c6a6acfbSmrg            prefixes="$prefixes '$qprefix'"
407c6a6acfbSmrg          fi
408c6a6acfbSmrg        fi
409c6a6acfbSmrg        prefix=$prefix/
41089d09728Smrg      done
41189d09728Smrg
41289d09728Smrg      if test -n "$prefixes"; then
413c6a6acfbSmrg        # Don't fail if two instances are running concurrently.
414c6a6acfbSmrg        (umask $mkdir_umask &&
415c6a6acfbSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416c6a6acfbSmrg          test -d "$dstdir" || exit 1
417c6a6acfbSmrg        obsolete_mkdir_used=true
41866fe65f6Smrg      fi
41989d09728Smrg    fi
42066fe65f6Smrg  fi
42166fe65f6Smrg
42266fe65f6Smrg  if test -n "$dir_arg"; then
42389d09728Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
42489d09728Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
42589d09728Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
42689d09728Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
42766fe65f6Smrg  else
42866fe65f6Smrg
42966fe65f6Smrg    # Make a couple of temp file names in the proper directory.
43066fe65f6Smrg    dsttmp=$dstdir/_inst.$$_
43166fe65f6Smrg    rmtmp=$dstdir/_rm.$$_
43266fe65f6Smrg
43366fe65f6Smrg    # Trap to clean up those temp files at exit.
43466fe65f6Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
43566fe65f6Smrg
43666fe65f6Smrg    # Copy the file name to the temp name.
43789d09728Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
43866fe65f6Smrg
43966fe65f6Smrg    # and set any options; do chmod last to preserve setuid bits.
44066fe65f6Smrg    #
44166fe65f6Smrg    # If any of these fail, we abort the whole thing.  If we want to
44266fe65f6Smrg    # ignore errors from any of these, just make sure not to ignore
44366fe65f6Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
44466fe65f6Smrg    #
44589d09728Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
44689d09728Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
44789d09728Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
44889d09728Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
44989d09728Smrg
45089d09728Smrg    # If -C, don't bother to copy if it wouldn't change the file.
45189d09728Smrg    if $copy_on_change &&
452c6a6acfbSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453c6a6acfbSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454c6a6acfbSmrg       set -f &&
45589d09728Smrg       set X $old && old=:$2:$4:$5:$6 &&
45689d09728Smrg       set X $new && new=:$2:$4:$5:$6 &&
457c6a6acfbSmrg       set +f &&
45889d09728Smrg       test "$old" = "$new" &&
45989d09728Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
46089d09728Smrg    then
46189d09728Smrg      rm -f "$dsttmp"
46289d09728Smrg    else
46389d09728Smrg      # Rename the file to the real destination.
46489d09728Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
46589d09728Smrg
46689d09728Smrg      # The rename failed, perhaps because mv can't rename something else
46789d09728Smrg      # to itself, or perhaps because mv is so ancient that it does not
46889d09728Smrg      # support -f.
46989d09728Smrg      {
470c6a6acfbSmrg        # Now remove or move aside any old file at destination location.
471c6a6acfbSmrg        # We try this two ways since rm can't unlink itself on some
472c6a6acfbSmrg        # systems and the destination file might be busy for other
473c6a6acfbSmrg        # reasons.  In this case, the final cleanup might fail but the new
474c6a6acfbSmrg        # file should still install successfully.
475c6a6acfbSmrg        {
476c6a6acfbSmrg          test ! -f "$dst" ||
477c6a6acfbSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478c6a6acfbSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479c6a6acfbSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480c6a6acfbSmrg          } ||
481c6a6acfbSmrg          { echo "$0: cannot unlink or rename $dst" >&2
482c6a6acfbSmrg            (exit 1); exit 1
483c6a6acfbSmrg          }
484c6a6acfbSmrg        } &&
485c6a6acfbSmrg
486c6a6acfbSmrg        # Now rename the file to the real destination.
487c6a6acfbSmrg        $doit $mvcmd "$dsttmp" "$dst"
48889d09728Smrg      }
48989d09728Smrg    fi || exit 1
49089d09728Smrg
49189d09728Smrg    trap '' 0
49289d09728Smrg  fi
49366fe65f6Smrgdone
49466fe65f6Smrg
49566fe65f6Smrg# Local variables:
49666fe65f6Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
49766fe65f6Smrg# time-stamp-start: "scriptversion="
49866fe65f6Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499a9274bcdSmrg# time-stamp-time-zone: "UTC"
500a9274bcdSmrg# time-stamp-end: "; # UTC"
50166fe65f6Smrg# End:
502