144d7874bSmrg#!/bin/sh
244d7874bSmrg# install - install a program, script, or datafile
344d7874bSmrg
4e9feffd0Smrgscriptversion=2013-12-25.23; # UTC
544d7874bSmrg
644d7874bSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
744d7874bSmrg# later released in X11R6 (xc/config/util/install.sh) with the
844d7874bSmrg# following copyright and license.
944d7874bSmrg#
1044d7874bSmrg# Copyright (C) 1994 X Consortium
1144d7874bSmrg#
1244d7874bSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1344d7874bSmrg# of this software and associated documentation files (the "Software"), to
1444d7874bSmrg# deal in the Software without restriction, including without limitation the
1544d7874bSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1644d7874bSmrg# sell copies of the Software, and to permit persons to whom the Software is
1744d7874bSmrg# furnished to do so, subject to the following conditions:
1844d7874bSmrg#
1944d7874bSmrg# The above copyright notice and this permission notice shall be included in
2044d7874bSmrg# all copies or substantial portions of the Software.
2144d7874bSmrg#
2244d7874bSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2344d7874bSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2444d7874bSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2544d7874bSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2644d7874bSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2744d7874bSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2844d7874bSmrg#
2944d7874bSmrg# Except as contained in this notice, the name of the X Consortium shall not
3044d7874bSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3144d7874bSmrg# ings in this Software without prior written authorization from the X Consor-
3244d7874bSmrg# tium.
3344d7874bSmrg#
3444d7874bSmrg#
3544d7874bSmrg# FSF changes to this file are in the public domain.
3644d7874bSmrg#
3744d7874bSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38e9feffd0Smrg# 'make' implicit rules from creating a file called install from it
3944d7874bSmrg# when there is no Makefile.
4044d7874bSmrg#
4144d7874bSmrg# This script is compatible with the BSD install script, but was written
422d861bc5Smrg# from scratch.
432d861bc5Smrg
44e9feffd0Smrgtab='	'
452d861bc5Smrgnl='
462d861bc5Smrg'
47e9feffd0SmrgIFS=" $tab$nl"
4844d7874bSmrg
49e9feffd0Smrg# Set DOITPROG to "echo" to test this script.
5044d7874bSmrg
512d861bc5Smrgdoit=${DOITPROG-}
52e9feffd0Smrgdoit_exec=${doit:-exec}
5344d7874bSmrg
542d861bc5Smrg# Put in absolute file names if you don't have them in your path;
552d861bc5Smrg# or use environment vars.
562d861bc5Smrg
572d861bc5Smrgchgrpprog=${CHGRPPROG-chgrp}
582d861bc5Smrgchmodprog=${CHMODPROG-chmod}
592d861bc5Smrgchownprog=${CHOWNPROG-chown}
602d861bc5Smrgcmpprog=${CMPPROG-cmp}
612d861bc5Smrgcpprog=${CPPROG-cp}
622d861bc5Smrgmkdirprog=${MKDIRPROG-mkdir}
632d861bc5Smrgmvprog=${MVPROG-mv}
642d861bc5Smrgrmprog=${RMPROG-rm}
652d861bc5Smrgstripprog=${STRIPPROG-strip}
662d861bc5Smrg
672d861bc5Smrgposix_mkdir=
682d861bc5Smrg
692d861bc5Smrg# Desired mode of installed file.
702d861bc5Smrgmode=0755
7144d7874bSmrg
7244d7874bSmrgchgrpcmd=
732d861bc5Smrgchmodcmd=$chmodprog
742d861bc5Smrgchowncmd=
752d861bc5Smrgmvcmd=$mvprog
7644d7874bSmrgrmcmd="$rmprog -f"
772d861bc5Smrgstripcmd=
782d861bc5Smrg
7944d7874bSmrgsrc=
8044d7874bSmrgdst=
8144d7874bSmrgdir_arg=
822d861bc5Smrgdst_arg=
832d861bc5Smrg
842d861bc5Smrgcopy_on_change=false
85e9feffd0Smrgis_target_a_directory=possibly
8644d7874bSmrg
872d861bc5Smrgusage="\
882d861bc5SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8944d7874bSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9044d7874bSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9144d7874bSmrg   or: $0 [OPTION]... -d DIRECTORIES...
9244d7874bSmrg
9344d7874bSmrgIn the 1st form, copy SRCFILE to DSTFILE.
9444d7874bSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9544d7874bSmrgIn the 4th, create DIRECTORIES.
9644d7874bSmrg
9744d7874bSmrgOptions:
982d861bc5Smrg     --help     display this help and exit.
992d861bc5Smrg     --version  display version info and exit.
1002d861bc5Smrg
1012d861bc5Smrg  -c            (ignored)
1022d861bc5Smrg  -C            install only if different (preserve the last data modification time)
1032d861bc5Smrg  -d            create directories instead of installing files.
1042d861bc5Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1052d861bc5Smrg  -m MODE       $chmodprog installed files to MODE.
1062d861bc5Smrg  -o USER       $chownprog installed files to USER.
1072d861bc5Smrg  -s            $stripprog installed files.
1082d861bc5Smrg  -t DIRECTORY  install into DIRECTORY.
1092d861bc5Smrg  -T            report an error if DSTFILE is a directory.
11044d7874bSmrg
11144d7874bSmrgEnvironment variables override the default commands:
1122d861bc5Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1132d861bc5Smrg  RMPROG STRIPPROG
11444d7874bSmrg"
11544d7874bSmrg
1162d861bc5Smrgwhile test $# -ne 0; do
11744d7874bSmrg  case $1 in
1182d861bc5Smrg    -c) ;;
1192d861bc5Smrg
1202d861bc5Smrg    -C) copy_on_change=true;;
12144d7874bSmrg
1222d861bc5Smrg    -d) dir_arg=true;;
12344d7874bSmrg
12444d7874bSmrg    -g) chgrpcmd="$chgrpprog $2"
125e9feffd0Smrg        shift;;
12644d7874bSmrg
12744d7874bSmrg    --help) echo "$usage"; exit $?;;
12844d7874bSmrg
1292d861bc5Smrg    -m) mode=$2
130e9feffd0Smrg        case $mode in
131e9feffd0Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132e9feffd0Smrg            echo "$0: invalid mode: $mode" >&2
133e9feffd0Smrg            exit 1;;
134e9feffd0Smrg        esac
135e9feffd0Smrg        shift;;
13644d7874bSmrg
13744d7874bSmrg    -o) chowncmd="$chownprog $2"
138e9feffd0Smrg        shift;;
13944d7874bSmrg
1402d861bc5Smrg    -s) stripcmd=$stripprog;;
14144d7874bSmrg
142e9feffd0Smrg    -t)
143e9feffd0Smrg        is_target_a_directory=always
144e9feffd0Smrg        dst_arg=$2
145e9feffd0Smrg        # Protect names problematic for 'test' and other utilities.
146e9feffd0Smrg        case $dst_arg in
147e9feffd0Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148e9feffd0Smrg        esac
149e9feffd0Smrg        shift;;
15044d7874bSmrg
151e9feffd0Smrg    -T) is_target_a_directory=never;;
15244d7874bSmrg
15344d7874bSmrg    --version) echo "$0 $scriptversion"; exit $?;;
15444d7874bSmrg
155e9feffd0Smrg    --) shift
156e9feffd0Smrg        break;;
1572d861bc5Smrg
158e9feffd0Smrg    -*) echo "$0: invalid option: $1" >&2
159e9feffd0Smrg        exit 1;;
1602d861bc5Smrg
1612d861bc5Smrg    *)  break;;
16244d7874bSmrg  esac
1632d861bc5Smrg  shift
16444d7874bSmrgdone
16544d7874bSmrg
166e9feffd0Smrg# We allow the use of options -d and -T together, by making -d
167e9feffd0Smrg# take the precedence; this is for compatibility with GNU install.
168e9feffd0Smrg
169e9feffd0Smrgif test -n "$dir_arg"; then
170e9feffd0Smrg  if test -n "$dst_arg"; then
171e9feffd0Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172e9feffd0Smrg    exit 1
173e9feffd0Smrg  fi
174e9feffd0Smrgfi
175e9feffd0Smrg
1762d861bc5Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1772d861bc5Smrg  # When -d is used, all remaining arguments are directories to create.
1782d861bc5Smrg  # When -t is used, the destination is already specified.
1792d861bc5Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1802d861bc5Smrg  for arg
1812d861bc5Smrg  do
1822d861bc5Smrg    if test -n "$dst_arg"; then
1832d861bc5Smrg      # $@ is not empty: it contains at least $arg.
1842d861bc5Smrg      set fnord "$@" "$dst_arg"
1852d861bc5Smrg      shift # fnord
1862d861bc5Smrg    fi
1872d861bc5Smrg    shift # arg
1882d861bc5Smrg    dst_arg=$arg
189e9feffd0Smrg    # Protect names problematic for 'test' and other utilities.
190caec6540Smrg    case $dst_arg in
191caec6540Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192caec6540Smrg    esac
1932d861bc5Smrg  done
1942d861bc5Smrgfi
1952d861bc5Smrg
1962d861bc5Smrgif test $# -eq 0; then
19744d7874bSmrg  if test -z "$dir_arg"; then
19844d7874bSmrg    echo "$0: no input file specified." >&2
19944d7874bSmrg    exit 1
20044d7874bSmrg  fi
201e9feffd0Smrg  # It's OK to call 'install-sh -d' without argument.
20244d7874bSmrg  # This can happen when creating conditional directories.
20344d7874bSmrg  exit 0
20444d7874bSmrgfi
20544d7874bSmrg
206e9feffd0Smrgif test -z "$dir_arg"; then
207e9feffd0Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208e9feffd0Smrg    if test ! -d "$dst_arg"; then
209e9feffd0Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210e9feffd0Smrg      exit 1
211e9feffd0Smrg    fi
212e9feffd0Smrg  fi
213e9feffd0Smrgfi
214e9feffd0Smrg
2152d861bc5Smrgif test -z "$dir_arg"; then
216caec6540Smrg  do_exit='(exit $ret); exit $ret'
217caec6540Smrg  trap "ret=129; $do_exit" 1
218caec6540Smrg  trap "ret=130; $do_exit" 2
219caec6540Smrg  trap "ret=141; $do_exit" 13
220caec6540Smrg  trap "ret=143; $do_exit" 15
2212d861bc5Smrg
2222d861bc5Smrg  # Set umask so as not to create temps with too-generous modes.
2232d861bc5Smrg  # However, 'strip' requires both read and write access to temps.
2242d861bc5Smrg  case $mode in
2252d861bc5Smrg    # Optimize common cases.
2262d861bc5Smrg    *644) cp_umask=133;;
2272d861bc5Smrg    *755) cp_umask=22;;
2282d861bc5Smrg
2292d861bc5Smrg    *[0-7])
2302d861bc5Smrg      if test -z "$stripcmd"; then
231e9feffd0Smrg        u_plus_rw=
2322d861bc5Smrg      else
233e9feffd0Smrg        u_plus_rw='% 200'
2342d861bc5Smrg      fi
2352d861bc5Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2362d861bc5Smrg    *)
2372d861bc5Smrg      if test -z "$stripcmd"; then
238e9feffd0Smrg        u_plus_rw=
2392d861bc5Smrg      else
240e9feffd0Smrg        u_plus_rw=,u+rw
2412d861bc5Smrg      fi
2422d861bc5Smrg      cp_umask=$mode$u_plus_rw;;
2432d861bc5Smrg  esac
2442d861bc5Smrgfi
2452d861bc5Smrg
24644d7874bSmrgfor src
24744d7874bSmrgdo
248e9feffd0Smrg  # Protect names problematic for 'test' and other utilities.
24944d7874bSmrg  case $src in
250caec6540Smrg    -* | [=\(\)!]) src=./$src;;
25144d7874bSmrg  esac
25244d7874bSmrg
25344d7874bSmrg  if test -n "$dir_arg"; then
25444d7874bSmrg    dst=$src
2552d861bc5Smrg    dstdir=$dst
2562d861bc5Smrg    test -d "$dstdir"
2572d861bc5Smrg    dstdir_status=$?
25844d7874bSmrg  else
2592d861bc5Smrg
26044d7874bSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26144d7874bSmrg    # might cause directories to be created, which would be especially bad
26244d7874bSmrg    # if $src (and thus $dsttmp) contains '*'.
26344d7874bSmrg    if test ! -f "$src" && test ! -d "$src"; then
26444d7874bSmrg      echo "$0: $src does not exist." >&2
26544d7874bSmrg      exit 1
26644d7874bSmrg    fi
26744d7874bSmrg
2682d861bc5Smrg    if test -z "$dst_arg"; then
26944d7874bSmrg      echo "$0: no destination specified." >&2
27044d7874bSmrg      exit 1
27144d7874bSmrg    fi
2722d861bc5Smrg    dst=$dst_arg
27344d7874bSmrg
27444d7874bSmrg    # If destination is a directory, append the input filename; won't work
27544d7874bSmrg    # if double slashes aren't ignored.
27644d7874bSmrg    if test -d "$dst"; then
277e9feffd0Smrg      if test "$is_target_a_directory" = never; then
278e9feffd0Smrg        echo "$0: $dst_arg: Is a directory" >&2
279e9feffd0Smrg        exit 1
28044d7874bSmrg      fi
2812d861bc5Smrg      dstdir=$dst
2822d861bc5Smrg      dst=$dstdir/`basename "$src"`
2832d861bc5Smrg      dstdir_status=0
2842d861bc5Smrg    else
285e9feffd0Smrg      dstdir=`dirname "$dst"`
2862d861bc5Smrg      test -d "$dstdir"
2872d861bc5Smrg      dstdir_status=$?
28844d7874bSmrg    fi
28944d7874bSmrg  fi
29044d7874bSmrg
2912d861bc5Smrg  obsolete_mkdir_used=false
2922d861bc5Smrg
2932d861bc5Smrg  if test $dstdir_status != 0; then
2942d861bc5Smrg    case $posix_mkdir in
2952d861bc5Smrg      '')
296e9feffd0Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297e9feffd0Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298e9feffd0Smrg        umask=`umask`
299e9feffd0Smrg        case $stripcmd.$umask in
300e9feffd0Smrg          # Optimize common cases.
301e9feffd0Smrg          *[2367][2367]) mkdir_umask=$umask;;
302e9feffd0Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303e9feffd0Smrg
304e9feffd0Smrg          *[0-7])
305e9feffd0Smrg            mkdir_umask=`expr $umask + 22 \
306e9feffd0Smrg              - $umask % 100 % 40 + $umask % 20 \
307e9feffd0Smrg              - $umask % 10 % 4 + $umask % 2
308e9feffd0Smrg            `;;
309e9feffd0Smrg          *) mkdir_umask=$umask,go-w;;
310e9feffd0Smrg        esac
311e9feffd0Smrg
312e9feffd0Smrg        # With -d, create the new directory with the user-specified mode.
313e9feffd0Smrg        # Otherwise, rely on $mkdir_umask.
314e9feffd0Smrg        if test -n "$dir_arg"; then
315e9feffd0Smrg          mkdir_mode=-m$mode
316e9feffd0Smrg        else
317e9feffd0Smrg          mkdir_mode=
318e9feffd0Smrg        fi
319e9feffd0Smrg
320e9feffd0Smrg        posix_mkdir=false
321e9feffd0Smrg        case $umask in
322e9feffd0Smrg          *[123567][0-7][0-7])
323e9feffd0Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324e9feffd0Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325e9feffd0Smrg            ;;
326e9feffd0Smrg          *)
327e9feffd0Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328e9feffd0Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329e9feffd0Smrg
330e9feffd0Smrg            if (umask $mkdir_umask &&
331e9feffd0Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332e9feffd0Smrg            then
333e9feffd0Smrg              if test -z "$dir_arg" || {
334e9feffd0Smrg                   # Check for POSIX incompatibilities with -m.
335e9feffd0Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336e9feffd0Smrg                   # other-writable bit of parent directory when it shouldn't.
337e9feffd0Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338e9feffd0Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339e9feffd0Smrg                   case $ls_ld_tmpdir in
340e9feffd0Smrg                     d????-?r-*) different_mode=700;;
341e9feffd0Smrg                     d????-?--*) different_mode=755;;
342e9feffd0Smrg                     *) false;;
343e9feffd0Smrg                   esac &&
344e9feffd0Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345e9feffd0Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346e9feffd0Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347e9feffd0Smrg                   }
348e9feffd0Smrg                 }
349e9feffd0Smrg              then posix_mkdir=:
350e9feffd0Smrg              fi
351e9feffd0Smrg              rmdir "$tmpdir/d" "$tmpdir"
352e9feffd0Smrg            else
353e9feffd0Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354e9feffd0Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355e9feffd0Smrg            fi
356e9feffd0Smrg            trap '' 0;;
357e9feffd0Smrg        esac;;
3582d861bc5Smrg    esac
35944d7874bSmrg
3602d861bc5Smrg    if
3612d861bc5Smrg      $posix_mkdir && (
362e9feffd0Smrg        umask $mkdir_umask &&
363e9feffd0Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3642d861bc5Smrg      )
3652d861bc5Smrg    then :
3662d861bc5Smrg    else
36744d7874bSmrg
3682d861bc5Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3692d861bc5Smrg      # or it failed possibly due to a race condition.  Create the
3702d861bc5Smrg      # directory the slow way, step by step, checking for races as we go.
37144d7874bSmrg
3722d861bc5Smrg      case $dstdir in
373e9feffd0Smrg        /*) prefix='/';;
374e9feffd0Smrg        [-=\(\)!]*) prefix='./';;
375e9feffd0Smrg        *)  prefix='';;
3762d861bc5Smrg      esac
37744d7874bSmrg
3782d861bc5Smrg      oIFS=$IFS
3792d861bc5Smrg      IFS=/
380e9feffd0Smrg      set -f
3812d861bc5Smrg      set fnord $dstdir
38244d7874bSmrg      shift
383e9feffd0Smrg      set +f
3842d861bc5Smrg      IFS=$oIFS
3852d861bc5Smrg
3862d861bc5Smrg      prefixes=
3872d861bc5Smrg
3882d861bc5Smrg      for d
3892d861bc5Smrg      do
390e9feffd0Smrg        test X"$d" = X && continue
391e9feffd0Smrg
392e9feffd0Smrg        prefix=$prefix$d
393e9feffd0Smrg        if test -d "$prefix"; then
394e9feffd0Smrg          prefixes=
395e9feffd0Smrg        else
396e9feffd0Smrg          if $posix_mkdir; then
397e9feffd0Smrg            (umask=$mkdir_umask &&
398e9feffd0Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399e9feffd0Smrg            # Don't fail if two instances are running concurrently.
400e9feffd0Smrg            test -d "$prefix" || exit 1
401e9feffd0Smrg          else
402e9feffd0Smrg            case $prefix in
403e9feffd0Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404e9feffd0Smrg              *) qprefix=$prefix;;
405e9feffd0Smrg            esac
406e9feffd0Smrg            prefixes="$prefixes '$qprefix'"
407e9feffd0Smrg          fi
408e9feffd0Smrg        fi
409e9feffd0Smrg        prefix=$prefix/
4102d861bc5Smrg      done
4112d861bc5Smrg
4122d861bc5Smrg      if test -n "$prefixes"; then
413e9feffd0Smrg        # Don't fail if two instances are running concurrently.
414e9feffd0Smrg        (umask $mkdir_umask &&
415e9feffd0Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416e9feffd0Smrg          test -d "$dstdir" || exit 1
417e9feffd0Smrg        obsolete_mkdir_used=true
41844d7874bSmrg      fi
4192d861bc5Smrg    fi
42044d7874bSmrg  fi
42144d7874bSmrg
42244d7874bSmrg  if test -n "$dir_arg"; then
4232d861bc5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4242d861bc5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4252d861bc5Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4262d861bc5Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
42744d7874bSmrg  else
42844d7874bSmrg
42944d7874bSmrg    # Make a couple of temp file names in the proper directory.
43044d7874bSmrg    dsttmp=$dstdir/_inst.$$_
43144d7874bSmrg    rmtmp=$dstdir/_rm.$$_
43244d7874bSmrg
43344d7874bSmrg    # Trap to clean up those temp files at exit.
43444d7874bSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
43544d7874bSmrg
43644d7874bSmrg    # Copy the file name to the temp name.
4372d861bc5Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
43844d7874bSmrg
43944d7874bSmrg    # and set any options; do chmod last to preserve setuid bits.
44044d7874bSmrg    #
44144d7874bSmrg    # If any of these fail, we abort the whole thing.  If we want to
44244d7874bSmrg    # ignore errors from any of these, just make sure not to ignore
44344d7874bSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
44444d7874bSmrg    #
4452d861bc5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4462d861bc5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4472d861bc5Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4482d861bc5Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4492d861bc5Smrg
4502d861bc5Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4512d861bc5Smrg    if $copy_on_change &&
452e9feffd0Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453e9feffd0Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454e9feffd0Smrg       set -f &&
4552d861bc5Smrg       set X $old && old=:$2:$4:$5:$6 &&
4562d861bc5Smrg       set X $new && new=:$2:$4:$5:$6 &&
457e9feffd0Smrg       set +f &&
4582d861bc5Smrg       test "$old" = "$new" &&
4592d861bc5Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4602d861bc5Smrg    then
4612d861bc5Smrg      rm -f "$dsttmp"
4622d861bc5Smrg    else
4632d861bc5Smrg      # Rename the file to the real destination.
4642d861bc5Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4652d861bc5Smrg
4662d861bc5Smrg      # The rename failed, perhaps because mv can't rename something else
4672d861bc5Smrg      # to itself, or perhaps because mv is so ancient that it does not
4682d861bc5Smrg      # support -f.
4692d861bc5Smrg      {
470e9feffd0Smrg        # Now remove or move aside any old file at destination location.
471e9feffd0Smrg        # We try this two ways since rm can't unlink itself on some
472e9feffd0Smrg        # systems and the destination file might be busy for other
473e9feffd0Smrg        # reasons.  In this case, the final cleanup might fail but the new
474e9feffd0Smrg        # file should still install successfully.
475e9feffd0Smrg        {
476e9feffd0Smrg          test ! -f "$dst" ||
477e9feffd0Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478e9feffd0Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479e9feffd0Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480e9feffd0Smrg          } ||
481e9feffd0Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482e9feffd0Smrg            (exit 1); exit 1
483e9feffd0Smrg          }
484e9feffd0Smrg        } &&
485e9feffd0Smrg
486e9feffd0Smrg        # Now rename the file to the real destination.
487e9feffd0Smrg        $doit $mvcmd "$dsttmp" "$dst"
4882d861bc5Smrg      }
4892d861bc5Smrg    fi || exit 1
4902d861bc5Smrg
4912d861bc5Smrg    trap '' 0
4922d861bc5Smrg  fi
49344d7874bSmrgdone
49444d7874bSmrg
49544d7874bSmrg# Local variables:
49644d7874bSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
49744d7874bSmrg# time-stamp-start: "scriptversion="
49844d7874bSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
4992d861bc5Smrg# time-stamp-time-zone: "UTC"
5002d861bc5Smrg# time-stamp-end: "; # UTC"
50144d7874bSmrg# End:
502