1301ea0f4Smrg#!/bin/sh
2301ea0f4Smrg# install - install a program, script, or datafile
3301ea0f4Smrg
4d94624a0Smrgscriptversion=2014-09-12.12; # UTC
5301ea0f4Smrg
6301ea0f4Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7301ea0f4Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8301ea0f4Smrg# following copyright and license.
9301ea0f4Smrg#
10301ea0f4Smrg# Copyright (C) 1994 X Consortium
11301ea0f4Smrg#
12301ea0f4Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13301ea0f4Smrg# of this software and associated documentation files (the "Software"), to
14301ea0f4Smrg# deal in the Software without restriction, including without limitation the
15301ea0f4Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16301ea0f4Smrg# sell copies of the Software, and to permit persons to whom the Software is
17301ea0f4Smrg# furnished to do so, subject to the following conditions:
18301ea0f4Smrg#
19301ea0f4Smrg# The above copyright notice and this permission notice shall be included in
20301ea0f4Smrg# all copies or substantial portions of the Software.
21301ea0f4Smrg#
22301ea0f4Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23301ea0f4Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24301ea0f4Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25301ea0f4Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26301ea0f4Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27301ea0f4Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28301ea0f4Smrg#
29301ea0f4Smrg# Except as contained in this notice, the name of the X Consortium shall not
30301ea0f4Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31301ea0f4Smrg# ings in this Software without prior written authorization from the X Consor-
32301ea0f4Smrg# tium.
33301ea0f4Smrg#
34301ea0f4Smrg#
35301ea0f4Smrg# FSF changes to this file are in the public domain.
36301ea0f4Smrg#
37301ea0f4Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3818552c8aSmrg# 'make' implicit rules from creating a file called install from it
39301ea0f4Smrg# when there is no Makefile.
40301ea0f4Smrg#
41301ea0f4Smrg# This script is compatible with the BSD install script, but was written
420cc67336Smrg# from scratch.
430cc67336Smrg
4497ee6427Smrgtab='	'
450cc67336Smrgnl='
460cc67336Smrg'
4797ee6427SmrgIFS=" $tab$nl"
48301ea0f4Smrg
4997ee6427Smrg# Set DOITPROG to "echo" to test this script.
50301ea0f4Smrg
510cc67336Smrgdoit=${DOITPROG-}
5297ee6427Smrgdoit_exec=${doit:-exec}
53301ea0f4Smrg
540cc67336Smrg# Put in absolute file names if you don't have them in your path;
550cc67336Smrg# or use environment vars.
560cc67336Smrg
570cc67336Smrgchgrpprog=${CHGRPPROG-chgrp}
580cc67336Smrgchmodprog=${CHMODPROG-chmod}
590cc67336Smrgchownprog=${CHOWNPROG-chown}
600cc67336Smrgcmpprog=${CMPPROG-cmp}
610cc67336Smrgcpprog=${CPPROG-cp}
620cc67336Smrgmkdirprog=${MKDIRPROG-mkdir}
630cc67336Smrgmvprog=${MVPROG-mv}
640cc67336Smrgrmprog=${RMPROG-rm}
650cc67336Smrgstripprog=${STRIPPROG-strip}
660cc67336Smrg
670cc67336Smrgposix_mkdir=
680cc67336Smrg
690cc67336Smrg# Desired mode of installed file.
700cc67336Smrgmode=0755
71301ea0f4Smrg
72301ea0f4Smrgchgrpcmd=
730cc67336Smrgchmodcmd=$chmodprog
740cc67336Smrgchowncmd=
750cc67336Smrgmvcmd=$mvprog
76301ea0f4Smrgrmcmd="$rmprog -f"
770cc67336Smrgstripcmd=
780cc67336Smrg
79301ea0f4Smrgsrc=
80301ea0f4Smrgdst=
81301ea0f4Smrgdir_arg=
820cc67336Smrgdst_arg=
830cc67336Smrg
840cc67336Smrgcopy_on_change=false
8597ee6427Smrgis_target_a_directory=possibly
86301ea0f4Smrg
870cc67336Smrgusage="\
880cc67336SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89301ea0f4Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90301ea0f4Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91301ea0f4Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92301ea0f4Smrg
93301ea0f4SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94301ea0f4SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95301ea0f4SmrgIn the 4th, create DIRECTORIES.
96301ea0f4Smrg
97301ea0f4SmrgOptions:
980cc67336Smrg     --help     display this help and exit.
990cc67336Smrg     --version  display version info and exit.
1000cc67336Smrg
1010cc67336Smrg  -c            (ignored)
1020cc67336Smrg  -C            install only if different (preserve the last data modification time)
1030cc67336Smrg  -d            create directories instead of installing files.
1040cc67336Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1050cc67336Smrg  -m MODE       $chmodprog installed files to MODE.
1060cc67336Smrg  -o USER       $chownprog installed files to USER.
1070cc67336Smrg  -s            $stripprog installed files.
1080cc67336Smrg  -t DIRECTORY  install into DIRECTORY.
1090cc67336Smrg  -T            report an error if DSTFILE is a directory.
110301ea0f4Smrg
111301ea0f4SmrgEnvironment variables override the default commands:
1120cc67336Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1130cc67336Smrg  RMPROG STRIPPROG
114301ea0f4Smrg"
115301ea0f4Smrg
1160cc67336Smrgwhile test $# -ne 0; do
117301ea0f4Smrg  case $1 in
1180cc67336Smrg    -c) ;;
1190cc67336Smrg
1200cc67336Smrg    -C) copy_on_change=true;;
121301ea0f4Smrg
1220cc67336Smrg    -d) dir_arg=true;;
123301ea0f4Smrg
124301ea0f4Smrg    -g) chgrpcmd="$chgrpprog $2"
12597ee6427Smrg        shift;;
126301ea0f4Smrg
127301ea0f4Smrg    --help) echo "$usage"; exit $?;;
128301ea0f4Smrg
1290cc67336Smrg    -m) mode=$2
13097ee6427Smrg        case $mode in
13197ee6427Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
13297ee6427Smrg            echo "$0: invalid mode: $mode" >&2
13397ee6427Smrg            exit 1;;
13497ee6427Smrg        esac
13597ee6427Smrg        shift;;
136301ea0f4Smrg
137301ea0f4Smrg    -o) chowncmd="$chownprog $2"
13897ee6427Smrg        shift;;
139301ea0f4Smrg
1400cc67336Smrg    -s) stripcmd=$stripprog;;
141301ea0f4Smrg
14297ee6427Smrg    -t)
14397ee6427Smrg        is_target_a_directory=always
14497ee6427Smrg        dst_arg=$2
14597ee6427Smrg        # Protect names problematic for 'test' and other utilities.
14697ee6427Smrg        case $dst_arg in
14797ee6427Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14897ee6427Smrg        esac
14997ee6427Smrg        shift;;
150301ea0f4Smrg
15197ee6427Smrg    -T) is_target_a_directory=never;;
152301ea0f4Smrg
153301ea0f4Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154301ea0f4Smrg
15597ee6427Smrg    --) shift
15697ee6427Smrg        break;;
1570cc67336Smrg
15897ee6427Smrg    -*) echo "$0: invalid option: $1" >&2
15997ee6427Smrg        exit 1;;
1600cc67336Smrg
1610cc67336Smrg    *)  break;;
162301ea0f4Smrg  esac
1630cc67336Smrg  shift
164301ea0f4Smrgdone
165301ea0f4Smrg
16697ee6427Smrg# We allow the use of options -d and -T together, by making -d
16797ee6427Smrg# take the precedence; this is for compatibility with GNU install.
16897ee6427Smrg
16997ee6427Smrgif test -n "$dir_arg"; then
17097ee6427Smrg  if test -n "$dst_arg"; then
17197ee6427Smrg    echo "$0: target directory not allowed when installing a directory." >&2
17297ee6427Smrg    exit 1
17397ee6427Smrg  fi
17497ee6427Smrgfi
17597ee6427Smrg
1760cc67336Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1770cc67336Smrg  # When -d is used, all remaining arguments are directories to create.
1780cc67336Smrg  # When -t is used, the destination is already specified.
1790cc67336Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1800cc67336Smrg  for arg
1810cc67336Smrg  do
1820cc67336Smrg    if test -n "$dst_arg"; then
1830cc67336Smrg      # $@ is not empty: it contains at least $arg.
1840cc67336Smrg      set fnord "$@" "$dst_arg"
1850cc67336Smrg      shift # fnord
1860cc67336Smrg    fi
1870cc67336Smrg    shift # arg
1880cc67336Smrg    dst_arg=$arg
18918552c8aSmrg    # Protect names problematic for 'test' and other utilities.
190213fdd94Smrg    case $dst_arg in
191213fdd94Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192213fdd94Smrg    esac
1930cc67336Smrg  done
1940cc67336Smrgfi
1950cc67336Smrg
1960cc67336Smrgif test $# -eq 0; then
197301ea0f4Smrg  if test -z "$dir_arg"; then
198301ea0f4Smrg    echo "$0: no input file specified." >&2
199301ea0f4Smrg    exit 1
200301ea0f4Smrg  fi
20118552c8aSmrg  # It's OK to call 'install-sh -d' without argument.
202301ea0f4Smrg  # This can happen when creating conditional directories.
203301ea0f4Smrg  exit 0
204301ea0f4Smrgfi
205301ea0f4Smrg
20697ee6427Smrgif test -z "$dir_arg"; then
20797ee6427Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
20897ee6427Smrg    if test ! -d "$dst_arg"; then
20997ee6427Smrg      echo "$0: $dst_arg: Is not a directory." >&2
21097ee6427Smrg      exit 1
21197ee6427Smrg    fi
21297ee6427Smrg  fi
21397ee6427Smrgfi
21497ee6427Smrg
2150cc67336Smrgif test -z "$dir_arg"; then
216213fdd94Smrg  do_exit='(exit $ret); exit $ret'
217213fdd94Smrg  trap "ret=129; $do_exit" 1
218213fdd94Smrg  trap "ret=130; $do_exit" 2
219213fdd94Smrg  trap "ret=141; $do_exit" 13
220213fdd94Smrg  trap "ret=143; $do_exit" 15
2210cc67336Smrg
2220cc67336Smrg  # Set umask so as not to create temps with too-generous modes.
2230cc67336Smrg  # However, 'strip' requires both read and write access to temps.
2240cc67336Smrg  case $mode in
2250cc67336Smrg    # Optimize common cases.
2260cc67336Smrg    *644) cp_umask=133;;
2270cc67336Smrg    *755) cp_umask=22;;
2280cc67336Smrg
2290cc67336Smrg    *[0-7])
2300cc67336Smrg      if test -z "$stripcmd"; then
23197ee6427Smrg        u_plus_rw=
2320cc67336Smrg      else
23397ee6427Smrg        u_plus_rw='% 200'
2340cc67336Smrg      fi
2350cc67336Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2360cc67336Smrg    *)
2370cc67336Smrg      if test -z "$stripcmd"; then
23897ee6427Smrg        u_plus_rw=
2390cc67336Smrg      else
24097ee6427Smrg        u_plus_rw=,u+rw
2410cc67336Smrg      fi
2420cc67336Smrg      cp_umask=$mode$u_plus_rw;;
2430cc67336Smrg  esac
2440cc67336Smrgfi
2450cc67336Smrg
246301ea0f4Smrgfor src
247301ea0f4Smrgdo
24818552c8aSmrg  # Protect names problematic for 'test' and other utilities.
249301ea0f4Smrg  case $src in
250213fdd94Smrg    -* | [=\(\)!]) src=./$src;;
251301ea0f4Smrg  esac
252301ea0f4Smrg
253301ea0f4Smrg  if test -n "$dir_arg"; then
254301ea0f4Smrg    dst=$src
2550cc67336Smrg    dstdir=$dst
2560cc67336Smrg    test -d "$dstdir"
2570cc67336Smrg    dstdir_status=$?
258301ea0f4Smrg  else
2590cc67336Smrg
260301ea0f4Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261301ea0f4Smrg    # might cause directories to be created, which would be especially bad
262301ea0f4Smrg    # if $src (and thus $dsttmp) contains '*'.
263301ea0f4Smrg    if test ! -f "$src" && test ! -d "$src"; then
264301ea0f4Smrg      echo "$0: $src does not exist." >&2
265301ea0f4Smrg      exit 1
266301ea0f4Smrg    fi
267301ea0f4Smrg
2680cc67336Smrg    if test -z "$dst_arg"; then
269301ea0f4Smrg      echo "$0: no destination specified." >&2
270301ea0f4Smrg      exit 1
271301ea0f4Smrg    fi
2720cc67336Smrg    dst=$dst_arg
273301ea0f4Smrg
274301ea0f4Smrg    # If destination is a directory, append the input filename; won't work
275301ea0f4Smrg    # if double slashes aren't ignored.
276301ea0f4Smrg    if test -d "$dst"; then
27797ee6427Smrg      if test "$is_target_a_directory" = never; then
27897ee6427Smrg        echo "$0: $dst_arg: Is a directory" >&2
27997ee6427Smrg        exit 1
280301ea0f4Smrg      fi
2810cc67336Smrg      dstdir=$dst
2820cc67336Smrg      dst=$dstdir/`basename "$src"`
2830cc67336Smrg      dstdir_status=0
2840cc67336Smrg    else
28597ee6427Smrg      dstdir=`dirname "$dst"`
2860cc67336Smrg      test -d "$dstdir"
2870cc67336Smrg      dstdir_status=$?
288301ea0f4Smrg    fi
289301ea0f4Smrg  fi
290301ea0f4Smrg
2910cc67336Smrg  obsolete_mkdir_used=false
2920cc67336Smrg
2930cc67336Smrg  if test $dstdir_status != 0; then
2940cc67336Smrg    case $posix_mkdir in
2950cc67336Smrg      '')
29697ee6427Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
29797ee6427Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
29897ee6427Smrg        umask=`umask`
29997ee6427Smrg        case $stripcmd.$umask in
30097ee6427Smrg          # Optimize common cases.
30197ee6427Smrg          *[2367][2367]) mkdir_umask=$umask;;
30297ee6427Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
30397ee6427Smrg
30497ee6427Smrg          *[0-7])
30597ee6427Smrg            mkdir_umask=`expr $umask + 22 \
30697ee6427Smrg              - $umask % 100 % 40 + $umask % 20 \
30797ee6427Smrg              - $umask % 10 % 4 + $umask % 2
30897ee6427Smrg            `;;
30997ee6427Smrg          *) mkdir_umask=$umask,go-w;;
31097ee6427Smrg        esac
31197ee6427Smrg
31297ee6427Smrg        # With -d, create the new directory with the user-specified mode.
31397ee6427Smrg        # Otherwise, rely on $mkdir_umask.
31497ee6427Smrg        if test -n "$dir_arg"; then
31597ee6427Smrg          mkdir_mode=-m$mode
31697ee6427Smrg        else
31797ee6427Smrg          mkdir_mode=
31897ee6427Smrg        fi
31997ee6427Smrg
32097ee6427Smrg        posix_mkdir=false
32197ee6427Smrg        case $umask in
32297ee6427Smrg          *[123567][0-7][0-7])
32397ee6427Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
32497ee6427Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
32597ee6427Smrg            ;;
32697ee6427Smrg          *)
327d94624a0Smrg            # $RANDOM is not portable (e.g. dash);  use it when possible to
328d94624a0Smrg            # lower collision chance
32997ee6427Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
330d94624a0Smrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
33197ee6427Smrg
332d94624a0Smrg            # As "mkdir -p" follows symlinks and we work in /tmp possibly;  so
333d94624a0Smrg            # create the $tmpdir first (and fail if unsuccessful) to make sure
334d94624a0Smrg            # that nobody tries to guess the $tmpdir name.
33597ee6427Smrg            if (umask $mkdir_umask &&
336d94624a0Smrg                $mkdirprog $mkdir_mode "$tmpdir" &&
337d94624a0Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
33897ee6427Smrg            then
33997ee6427Smrg              if test -z "$dir_arg" || {
34097ee6427Smrg                   # Check for POSIX incompatibilities with -m.
34197ee6427Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
34297ee6427Smrg                   # other-writable bit of parent directory when it shouldn't.
34397ee6427Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
344d94624a0Smrg                   test_tmpdir="$tmpdir/a"
345d94624a0Smrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
34697ee6427Smrg                   case $ls_ld_tmpdir in
34797ee6427Smrg                     d????-?r-*) different_mode=700;;
34897ee6427Smrg                     d????-?--*) different_mode=755;;
34997ee6427Smrg                     *) false;;
35097ee6427Smrg                   esac &&
351d94624a0Smrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
352d94624a0Smrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
35397ee6427Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
35497ee6427Smrg                   }
35597ee6427Smrg                 }
35697ee6427Smrg              then posix_mkdir=:
35797ee6427Smrg              fi
358d94624a0Smrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
35997ee6427Smrg            else
36097ee6427Smrg              # Remove any dirs left behind by ancient mkdir implementations.
361d94624a0Smrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
36297ee6427Smrg            fi
36397ee6427Smrg            trap '' 0;;
36497ee6427Smrg        esac;;
3650cc67336Smrg    esac
366301ea0f4Smrg
3670cc67336Smrg    if
3680cc67336Smrg      $posix_mkdir && (
36997ee6427Smrg        umask $mkdir_umask &&
37097ee6427Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3710cc67336Smrg      )
3720cc67336Smrg    then :
3730cc67336Smrg    else
374301ea0f4Smrg
3750cc67336Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3760cc67336Smrg      # or it failed possibly due to a race condition.  Create the
3770cc67336Smrg      # directory the slow way, step by step, checking for races as we go.
378301ea0f4Smrg
3790cc67336Smrg      case $dstdir in
38097ee6427Smrg        /*) prefix='/';;
38197ee6427Smrg        [-=\(\)!]*) prefix='./';;
38297ee6427Smrg        *)  prefix='';;
3830cc67336Smrg      esac
384301ea0f4Smrg
3850cc67336Smrg      oIFS=$IFS
3860cc67336Smrg      IFS=/
38797ee6427Smrg      set -f
3880cc67336Smrg      set fnord $dstdir
389301ea0f4Smrg      shift
39097ee6427Smrg      set +f
3910cc67336Smrg      IFS=$oIFS
3920cc67336Smrg
3930cc67336Smrg      prefixes=
3940cc67336Smrg
3950cc67336Smrg      for d
3960cc67336Smrg      do
39797ee6427Smrg        test X"$d" = X && continue
39897ee6427Smrg
39997ee6427Smrg        prefix=$prefix$d
40097ee6427Smrg        if test -d "$prefix"; then
40197ee6427Smrg          prefixes=
40297ee6427Smrg        else
40397ee6427Smrg          if $posix_mkdir; then
40497ee6427Smrg            (umask=$mkdir_umask &&
40597ee6427Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
40697ee6427Smrg            # Don't fail if two instances are running concurrently.
40797ee6427Smrg            test -d "$prefix" || exit 1
40897ee6427Smrg          else
40997ee6427Smrg            case $prefix in
41097ee6427Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
41197ee6427Smrg              *) qprefix=$prefix;;
41297ee6427Smrg            esac
41397ee6427Smrg            prefixes="$prefixes '$qprefix'"
41497ee6427Smrg          fi
41597ee6427Smrg        fi
41697ee6427Smrg        prefix=$prefix/
4170cc67336Smrg      done
4180cc67336Smrg
4190cc67336Smrg      if test -n "$prefixes"; then
42097ee6427Smrg        # Don't fail if two instances are running concurrently.
42197ee6427Smrg        (umask $mkdir_umask &&
42297ee6427Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
42397ee6427Smrg          test -d "$dstdir" || exit 1
42497ee6427Smrg        obsolete_mkdir_used=true
425301ea0f4Smrg      fi
4260cc67336Smrg    fi
427301ea0f4Smrg  fi
428301ea0f4Smrg
429301ea0f4Smrg  if test -n "$dir_arg"; then
4300cc67336Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4310cc67336Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4320cc67336Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4330cc67336Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
434301ea0f4Smrg  else
435301ea0f4Smrg
436301ea0f4Smrg    # Make a couple of temp file names in the proper directory.
437301ea0f4Smrg    dsttmp=$dstdir/_inst.$$_
438301ea0f4Smrg    rmtmp=$dstdir/_rm.$$_
439301ea0f4Smrg
440301ea0f4Smrg    # Trap to clean up those temp files at exit.
441301ea0f4Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
442301ea0f4Smrg
443301ea0f4Smrg    # Copy the file name to the temp name.
4440cc67336Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
445301ea0f4Smrg
446301ea0f4Smrg    # and set any options; do chmod last to preserve setuid bits.
447301ea0f4Smrg    #
448301ea0f4Smrg    # If any of these fail, we abort the whole thing.  If we want to
449301ea0f4Smrg    # ignore errors from any of these, just make sure not to ignore
450301ea0f4Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
451301ea0f4Smrg    #
4520cc67336Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4530cc67336Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4540cc67336Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4550cc67336Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4560cc67336Smrg
4570cc67336Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4580cc67336Smrg    if $copy_on_change &&
45997ee6427Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
46097ee6427Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
46197ee6427Smrg       set -f &&
4620cc67336Smrg       set X $old && old=:$2:$4:$5:$6 &&
4630cc67336Smrg       set X $new && new=:$2:$4:$5:$6 &&
46497ee6427Smrg       set +f &&
4650cc67336Smrg       test "$old" = "$new" &&
4660cc67336Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4670cc67336Smrg    then
4680cc67336Smrg      rm -f "$dsttmp"
4690cc67336Smrg    else
4700cc67336Smrg      # Rename the file to the real destination.
4710cc67336Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4720cc67336Smrg
4730cc67336Smrg      # The rename failed, perhaps because mv can't rename something else
4740cc67336Smrg      # to itself, or perhaps because mv is so ancient that it does not
4750cc67336Smrg      # support -f.
4760cc67336Smrg      {
47797ee6427Smrg        # Now remove or move aside any old file at destination location.
47897ee6427Smrg        # We try this two ways since rm can't unlink itself on some
47997ee6427Smrg        # systems and the destination file might be busy for other
48097ee6427Smrg        # reasons.  In this case, the final cleanup might fail but the new
48197ee6427Smrg        # file should still install successfully.
48297ee6427Smrg        {
48397ee6427Smrg          test ! -f "$dst" ||
48497ee6427Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
48597ee6427Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
48697ee6427Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
48797ee6427Smrg          } ||
48897ee6427Smrg          { echo "$0: cannot unlink or rename $dst" >&2
48997ee6427Smrg            (exit 1); exit 1
49097ee6427Smrg          }
49197ee6427Smrg        } &&
49297ee6427Smrg
49397ee6427Smrg        # Now rename the file to the real destination.
49497ee6427Smrg        $doit $mvcmd "$dsttmp" "$dst"
4950cc67336Smrg      }
4960cc67336Smrg    fi || exit 1
4970cc67336Smrg
4980cc67336Smrg    trap '' 0
4990cc67336Smrg  fi
500301ea0f4Smrgdone
501301ea0f4Smrg
502301ea0f4Smrg# Local variables:
503301ea0f4Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
504301ea0f4Smrg# time-stamp-start: "scriptversion="
505301ea0f4Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
506ef6fb4a5Smrg# time-stamp-time-zone: "UTC"
507ef6fb4a5Smrg# time-stamp-end: "; # UTC"
508301ea0f4Smrg# End:
509