1fd0c672fSmrg#!/bin/sh
2fd0c672fSmrg# install - install a program, script, or datafile
3fd0c672fSmrg
443ecf206Smrgscriptversion=2020-11-14.01; # UTC
5fd0c672fSmrg
6fd0c672fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7fd0c672fSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8fd0c672fSmrg# following copyright and license.
9fd0c672fSmrg#
10fd0c672fSmrg# Copyright (C) 1994 X Consortium
11fd0c672fSmrg#
12fd0c672fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13fd0c672fSmrg# of this software and associated documentation files (the "Software"), to
14fd0c672fSmrg# deal in the Software without restriction, including without limitation the
15fd0c672fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16fd0c672fSmrg# sell copies of the Software, and to permit persons to whom the Software is
17fd0c672fSmrg# furnished to do so, subject to the following conditions:
18fd0c672fSmrg#
19fd0c672fSmrg# The above copyright notice and this permission notice shall be included in
20fd0c672fSmrg# all copies or substantial portions of the Software.
21fd0c672fSmrg#
22fd0c672fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23fd0c672fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24fd0c672fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25fd0c672fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26fd0c672fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27fd0c672fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28fd0c672fSmrg#
29fd0c672fSmrg# Except as contained in this notice, the name of the X Consortium shall not
30fd0c672fSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31fd0c672fSmrg# ings in this Software without prior written authorization from the X Consor-
32fd0c672fSmrg# tium.
33fd0c672fSmrg#
34fd0c672fSmrg#
35fd0c672fSmrg# FSF changes to this file are in the public domain.
36fd0c672fSmrg#
37fd0c672fSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38953c684bSmrg# 'make' implicit rules from creating a file called install from it
39fd0c672fSmrg# when there is no Makefile.
40fd0c672fSmrg#
41fd0c672fSmrg# This script is compatible with the BSD install script, but was written
4248e69166Smrg# from scratch.
4348e69166Smrg
448846b520Smrgtab='	'
4548e69166Smrgnl='
4648e69166Smrg'
478846b520SmrgIFS=" $tab$nl"
48fd0c672fSmrg
498846b520Smrg# Set DOITPROG to "echo" to test this script.
50fd0c672fSmrg
5148e69166Smrgdoit=${DOITPROG-}
528846b520Smrgdoit_exec=${doit:-exec}
53fd0c672fSmrg
5448e69166Smrg# Put in absolute file names if you don't have them in your path;
5548e69166Smrg# or use environment vars.
5648e69166Smrg
5748e69166Smrgchgrpprog=${CHGRPPROG-chgrp}
5848e69166Smrgchmodprog=${CHMODPROG-chmod}
5948e69166Smrgchownprog=${CHOWNPROG-chown}
6048e69166Smrgcmpprog=${CMPPROG-cmp}
6148e69166Smrgcpprog=${CPPROG-cp}
6248e69166Smrgmkdirprog=${MKDIRPROG-mkdir}
6348e69166Smrgmvprog=${MVPROG-mv}
6448e69166Smrgrmprog=${RMPROG-rm}
6548e69166Smrgstripprog=${STRIPPROG-strip}
6648e69166Smrg
6748e69166Smrgposix_mkdir=
6848e69166Smrg
6948e69166Smrg# Desired mode of installed file.
7048e69166Smrgmode=0755
71fd0c672fSmrg
7243ecf206Smrg# Create dirs (including intermediate dirs) using mode 755.
7343ecf206Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7443ecf206Smrgmkdir_umask=22
7543ecf206Smrg
7643ecf206Smrgbackupsuffix=
77fd0c672fSmrgchgrpcmd=
7848e69166Smrgchmodcmd=$chmodprog
7948e69166Smrgchowncmd=
8048e69166Smrgmvcmd=$mvprog
81fd0c672fSmrgrmcmd="$rmprog -f"
8248e69166Smrgstripcmd=
8348e69166Smrg
84fd0c672fSmrgsrc=
85fd0c672fSmrgdst=
86fd0c672fSmrgdir_arg=
8748e69166Smrgdst_arg=
8848e69166Smrg
8948e69166Smrgcopy_on_change=false
908846b520Smrgis_target_a_directory=possibly
91fd0c672fSmrg
9248e69166Smrgusage="\
9348e69166SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94fd0c672fSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95fd0c672fSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96fd0c672fSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97fd0c672fSmrg
98fd0c672fSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99fd0c672fSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100fd0c672fSmrgIn the 4th, create DIRECTORIES.
101fd0c672fSmrg
102fd0c672fSmrgOptions:
10348e69166Smrg     --help     display this help and exit.
10448e69166Smrg     --version  display version info and exit.
10548e69166Smrg
10648e69166Smrg  -c            (ignored)
10743ecf206Smrg  -C            install only if different (preserve data modification time)
10848e69166Smrg  -d            create directories instead of installing files.
10948e69166Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11048e69166Smrg  -m MODE       $chmodprog installed files to MODE.
11148e69166Smrg  -o USER       $chownprog installed files to USER.
11243ecf206Smrg  -p            pass -p to $cpprog.
11348e69166Smrg  -s            $stripprog installed files.
11443ecf206Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11548e69166Smrg  -t DIRECTORY  install into DIRECTORY.
11648e69166Smrg  -T            report an error if DSTFILE is a directory.
117fd0c672fSmrg
118fd0c672fSmrgEnvironment variables override the default commands:
11948e69166Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12048e69166Smrg  RMPROG STRIPPROG
12143ecf206Smrg
12243ecf206SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12343ecf206Smrgit's up to you to specify -f if you want it.
12443ecf206Smrg
12543ecf206SmrgIf -S is not specified, no backups are attempted.
12643ecf206Smrg
12743ecf206SmrgEmail bug reports to bug-automake@gnu.org.
12843ecf206SmrgAutomake home page: https://www.gnu.org/software/automake/
129fd0c672fSmrg"
130fd0c672fSmrg
13148e69166Smrgwhile test $# -ne 0; do
132fd0c672fSmrg  case $1 in
13348e69166Smrg    -c) ;;
13448e69166Smrg
13548e69166Smrg    -C) copy_on_change=true;;
136fd0c672fSmrg
13748e69166Smrg    -d) dir_arg=true;;
138fd0c672fSmrg
139fd0c672fSmrg    -g) chgrpcmd="$chgrpprog $2"
1408846b520Smrg        shift;;
141fd0c672fSmrg
142fd0c672fSmrg    --help) echo "$usage"; exit $?;;
143fd0c672fSmrg
14448e69166Smrg    -m) mode=$2
1458846b520Smrg        case $mode in
1468846b520Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1478846b520Smrg            echo "$0: invalid mode: $mode" >&2
1488846b520Smrg            exit 1;;
1498846b520Smrg        esac
1508846b520Smrg        shift;;
151fd0c672fSmrg
152fd0c672fSmrg    -o) chowncmd="$chownprog $2"
1538846b520Smrg        shift;;
154fd0c672fSmrg
15543ecf206Smrg    -p) cpprog="$cpprog -p";;
15643ecf206Smrg
15748e69166Smrg    -s) stripcmd=$stripprog;;
158fd0c672fSmrg
15943ecf206Smrg    -S) backupsuffix="$2"
16043ecf206Smrg        shift;;
16143ecf206Smrg
1628846b520Smrg    -t)
1638846b520Smrg        is_target_a_directory=always
1648846b520Smrg        dst_arg=$2
1658846b520Smrg        # Protect names problematic for 'test' and other utilities.
1668846b520Smrg        case $dst_arg in
1678846b520Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1688846b520Smrg        esac
1698846b520Smrg        shift;;
170fd0c672fSmrg
1718846b520Smrg    -T) is_target_a_directory=never;;
172fd0c672fSmrg
173fd0c672fSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174fd0c672fSmrg
1758846b520Smrg    --) shift
1768846b520Smrg        break;;
17748e69166Smrg
1788846b520Smrg    -*) echo "$0: invalid option: $1" >&2
1798846b520Smrg        exit 1;;
18048e69166Smrg
18148e69166Smrg    *)  break;;
182fd0c672fSmrg  esac
18348e69166Smrg  shift
184fd0c672fSmrgdone
185fd0c672fSmrg
1868846b520Smrg# We allow the use of options -d and -T together, by making -d
1878846b520Smrg# take the precedence; this is for compatibility with GNU install.
1888846b520Smrg
1898846b520Smrgif test -n "$dir_arg"; then
1908846b520Smrg  if test -n "$dst_arg"; then
1918846b520Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1928846b520Smrg    exit 1
1938846b520Smrg  fi
1948846b520Smrgfi
1958846b520Smrg
19648e69166Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19748e69166Smrg  # When -d is used, all remaining arguments are directories to create.
19848e69166Smrg  # When -t is used, the destination is already specified.
19948e69166Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20048e69166Smrg  for arg
20148e69166Smrg  do
20248e69166Smrg    if test -n "$dst_arg"; then
20348e69166Smrg      # $@ is not empty: it contains at least $arg.
20448e69166Smrg      set fnord "$@" "$dst_arg"
20548e69166Smrg      shift # fnord
20648e69166Smrg    fi
20748e69166Smrg    shift # arg
20848e69166Smrg    dst_arg=$arg
209953c684bSmrg    # Protect names problematic for 'test' and other utilities.
210953c684bSmrg    case $dst_arg in
211953c684bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212953c684bSmrg    esac
21348e69166Smrg  done
21448e69166Smrgfi
21548e69166Smrg
21648e69166Smrgif test $# -eq 0; then
217fd0c672fSmrg  if test -z "$dir_arg"; then
218fd0c672fSmrg    echo "$0: no input file specified." >&2
219fd0c672fSmrg    exit 1
220fd0c672fSmrg  fi
221953c684bSmrg  # It's OK to call 'install-sh -d' without argument.
222fd0c672fSmrg  # This can happen when creating conditional directories.
223fd0c672fSmrg  exit 0
224fd0c672fSmrgfi
225fd0c672fSmrg
2268846b520Smrgif test -z "$dir_arg"; then
2278846b520Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2288846b520Smrg    if test ! -d "$dst_arg"; then
2298846b520Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2308846b520Smrg      exit 1
2318846b520Smrg    fi
2328846b520Smrg  fi
2338846b520Smrgfi
2348846b520Smrg
23548e69166Smrgif test -z "$dir_arg"; then
236953c684bSmrg  do_exit='(exit $ret); exit $ret'
237953c684bSmrg  trap "ret=129; $do_exit" 1
238953c684bSmrg  trap "ret=130; $do_exit" 2
239953c684bSmrg  trap "ret=141; $do_exit" 13
240953c684bSmrg  trap "ret=143; $do_exit" 15
24148e69166Smrg
24248e69166Smrg  # Set umask so as not to create temps with too-generous modes.
24348e69166Smrg  # However, 'strip' requires both read and write access to temps.
24448e69166Smrg  case $mode in
24548e69166Smrg    # Optimize common cases.
24648e69166Smrg    *644) cp_umask=133;;
24748e69166Smrg    *755) cp_umask=22;;
24848e69166Smrg
24948e69166Smrg    *[0-7])
25048e69166Smrg      if test -z "$stripcmd"; then
2518846b520Smrg        u_plus_rw=
25248e69166Smrg      else
2538846b520Smrg        u_plus_rw='% 200'
25448e69166Smrg      fi
25548e69166Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25648e69166Smrg    *)
25748e69166Smrg      if test -z "$stripcmd"; then
2588846b520Smrg        u_plus_rw=
25948e69166Smrg      else
2608846b520Smrg        u_plus_rw=,u+rw
26148e69166Smrg      fi
26248e69166Smrg      cp_umask=$mode$u_plus_rw;;
26348e69166Smrg  esac
26448e69166Smrgfi
26548e69166Smrg
266fd0c672fSmrgfor src
267fd0c672fSmrgdo
268953c684bSmrg  # Protect names problematic for 'test' and other utilities.
269fd0c672fSmrg  case $src in
270953c684bSmrg    -* | [=\(\)!]) src=./$src;;
271fd0c672fSmrg  esac
272fd0c672fSmrg
273fd0c672fSmrg  if test -n "$dir_arg"; then
274fd0c672fSmrg    dst=$src
27548e69166Smrg    dstdir=$dst
27648e69166Smrg    test -d "$dstdir"
27748e69166Smrg    dstdir_status=$?
27843ecf206Smrg    # Don't chown directories that already exist.
27943ecf206Smrg    if test $dstdir_status = 0; then
28043ecf206Smrg      chowncmd=""
28143ecf206Smrg    fi
282fd0c672fSmrg  else
28348e69166Smrg
284fd0c672fSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285fd0c672fSmrg    # might cause directories to be created, which would be especially bad
286fd0c672fSmrg    # if $src (and thus $dsttmp) contains '*'.
287fd0c672fSmrg    if test ! -f "$src" && test ! -d "$src"; then
288fd0c672fSmrg      echo "$0: $src does not exist." >&2
289fd0c672fSmrg      exit 1
290fd0c672fSmrg    fi
291fd0c672fSmrg
29248e69166Smrg    if test -z "$dst_arg"; then
293fd0c672fSmrg      echo "$0: no destination specified." >&2
294fd0c672fSmrg      exit 1
295fd0c672fSmrg    fi
29648e69166Smrg    dst=$dst_arg
297fd0c672fSmrg
29843ecf206Smrg    # If destination is a directory, append the input filename.
299fd0c672fSmrg    if test -d "$dst"; then
3008846b520Smrg      if test "$is_target_a_directory" = never; then
3018846b520Smrg        echo "$0: $dst_arg: Is a directory" >&2
3028846b520Smrg        exit 1
303fd0c672fSmrg      fi
30448e69166Smrg      dstdir=$dst
30543ecf206Smrg      dstbase=`basename "$src"`
30643ecf206Smrg      case $dst in
30743ecf206Smrg	*/) dst=$dst$dstbase;;
30843ecf206Smrg	*)  dst=$dst/$dstbase;;
30943ecf206Smrg      esac
31048e69166Smrg      dstdir_status=0
31148e69166Smrg    else
3128846b520Smrg      dstdir=`dirname "$dst"`
31348e69166Smrg      test -d "$dstdir"
31448e69166Smrg      dstdir_status=$?
315fd0c672fSmrg    fi
316fd0c672fSmrg  fi
317fd0c672fSmrg
31843ecf206Smrg  case $dstdir in
31943ecf206Smrg    */) dstdirslash=$dstdir;;
32043ecf206Smrg    *)  dstdirslash=$dstdir/;;
32143ecf206Smrg  esac
32243ecf206Smrg
32348e69166Smrg  obsolete_mkdir_used=false
32448e69166Smrg
32548e69166Smrg  if test $dstdir_status != 0; then
32648e69166Smrg    case $posix_mkdir in
32748e69166Smrg      '')
3288846b520Smrg        # With -d, create the new directory with the user-specified mode.
3298846b520Smrg        # Otherwise, rely on $mkdir_umask.
3308846b520Smrg        if test -n "$dir_arg"; then
3318846b520Smrg          mkdir_mode=-m$mode
3328846b520Smrg        else
3338846b520Smrg          mkdir_mode=
3348846b520Smrg        fi
3358846b520Smrg
3368846b520Smrg        posix_mkdir=false
33743ecf206Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33843ecf206Smrg	# here however when possible just to lower collision chance.
33943ecf206Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34043ecf206Smrg
34143ecf206Smrg	trap '
34243ecf206Smrg	  ret=$?
34343ecf206Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34443ecf206Smrg	  exit $ret
34543ecf206Smrg	' 0
34643ecf206Smrg
34743ecf206Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
34843ecf206Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34943ecf206Smrg	# directory is successfully created first before we actually test
35043ecf206Smrg	# 'mkdir -p'.
35143ecf206Smrg	if (umask $mkdir_umask &&
35243ecf206Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35343ecf206Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35443ecf206Smrg	then
35543ecf206Smrg	  if test -z "$dir_arg" || {
35643ecf206Smrg	       # Check for POSIX incompatibilities with -m.
35743ecf206Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35843ecf206Smrg	       # other-writable bit of parent directory when it shouldn't.
35943ecf206Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36043ecf206Smrg	       test_tmpdir="$tmpdir/a"
36143ecf206Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36243ecf206Smrg	       case $ls_ld_tmpdir in
36343ecf206Smrg		 d????-?r-*) different_mode=700;;
36443ecf206Smrg		 d????-?--*) different_mode=755;;
36543ecf206Smrg		 *) false;;
36643ecf206Smrg	       esac &&
36743ecf206Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36843ecf206Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36943ecf206Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37043ecf206Smrg	       }
37143ecf206Smrg	     }
37243ecf206Smrg	  then posix_mkdir=:
37343ecf206Smrg	  fi
37443ecf206Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37543ecf206Smrg	else
37643ecf206Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
37743ecf206Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37843ecf206Smrg	fi
37943ecf206Smrg	trap '' 0;;
38048e69166Smrg    esac
381fd0c672fSmrg
38248e69166Smrg    if
38348e69166Smrg      $posix_mkdir && (
3848846b520Smrg        umask $mkdir_umask &&
3858846b520Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38648e69166Smrg      )
38748e69166Smrg    then :
38848e69166Smrg    else
389fd0c672fSmrg
39043ecf206Smrg      # mkdir does not conform to POSIX,
39148e69166Smrg      # or it failed possibly due to a race condition.  Create the
39248e69166Smrg      # directory the slow way, step by step, checking for races as we go.
393fd0c672fSmrg
39448e69166Smrg      case $dstdir in
3958846b520Smrg        /*) prefix='/';;
3968846b520Smrg        [-=\(\)!]*) prefix='./';;
3978846b520Smrg        *)  prefix='';;
39848e69166Smrg      esac
399fd0c672fSmrg
40048e69166Smrg      oIFS=$IFS
40148e69166Smrg      IFS=/
4028846b520Smrg      set -f
40348e69166Smrg      set fnord $dstdir
404fd0c672fSmrg      shift
4058846b520Smrg      set +f
40648e69166Smrg      IFS=$oIFS
40748e69166Smrg
40848e69166Smrg      prefixes=
40948e69166Smrg
41048e69166Smrg      for d
41148e69166Smrg      do
4128846b520Smrg        test X"$d" = X && continue
4138846b520Smrg
4148846b520Smrg        prefix=$prefix$d
4158846b520Smrg        if test -d "$prefix"; then
4168846b520Smrg          prefixes=
4178846b520Smrg        else
4188846b520Smrg          if $posix_mkdir; then
41943ecf206Smrg            (umask $mkdir_umask &&
4208846b520Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4218846b520Smrg            # Don't fail if two instances are running concurrently.
4228846b520Smrg            test -d "$prefix" || exit 1
4238846b520Smrg          else
4248846b520Smrg            case $prefix in
4258846b520Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4268846b520Smrg              *) qprefix=$prefix;;
4278846b520Smrg            esac
4288846b520Smrg            prefixes="$prefixes '$qprefix'"
4298846b520Smrg          fi
4308846b520Smrg        fi
4318846b520Smrg        prefix=$prefix/
43248e69166Smrg      done
43348e69166Smrg
43448e69166Smrg      if test -n "$prefixes"; then
4358846b520Smrg        # Don't fail if two instances are running concurrently.
4368846b520Smrg        (umask $mkdir_umask &&
4378846b520Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4388846b520Smrg          test -d "$dstdir" || exit 1
4398846b520Smrg        obsolete_mkdir_used=true
440fd0c672fSmrg      fi
44148e69166Smrg    fi
442fd0c672fSmrg  fi
443fd0c672fSmrg
444fd0c672fSmrg  if test -n "$dir_arg"; then
44548e69166Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44648e69166Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44748e69166Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44848e69166Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449fd0c672fSmrg  else
450fd0c672fSmrg
451fd0c672fSmrg    # Make a couple of temp file names in the proper directory.
45243ecf206Smrg    dsttmp=${dstdirslash}_inst.$$_
45343ecf206Smrg    rmtmp=${dstdirslash}_rm.$$_
454fd0c672fSmrg
455fd0c672fSmrg    # Trap to clean up those temp files at exit.
456fd0c672fSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457fd0c672fSmrg
458fd0c672fSmrg    # Copy the file name to the temp name.
45943ecf206Smrg    (umask $cp_umask &&
46043ecf206Smrg     { test -z "$stripcmd" || {
46143ecf206Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46243ecf206Smrg	 # which would cause strip to fail.
46343ecf206Smrg	 if test -z "$doit"; then
46443ecf206Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46543ecf206Smrg	 else
46643ecf206Smrg	   $doit touch "$dsttmp"
46743ecf206Smrg	 fi
46843ecf206Smrg       }
46943ecf206Smrg     } &&
47043ecf206Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471fd0c672fSmrg
472fd0c672fSmrg    # and set any options; do chmod last to preserve setuid bits.
473fd0c672fSmrg    #
474fd0c672fSmrg    # If any of these fail, we abort the whole thing.  If we want to
475fd0c672fSmrg    # ignore errors from any of these, just make sure not to ignore
476fd0c672fSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477fd0c672fSmrg    #
47848e69166Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47948e69166Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48048e69166Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48148e69166Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48248e69166Smrg
48348e69166Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48448e69166Smrg    if $copy_on_change &&
4858846b520Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4868846b520Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4878846b520Smrg       set -f &&
48848e69166Smrg       set X $old && old=:$2:$4:$5:$6 &&
48948e69166Smrg       set X $new && new=:$2:$4:$5:$6 &&
4908846b520Smrg       set +f &&
49148e69166Smrg       test "$old" = "$new" &&
49248e69166Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49348e69166Smrg    then
49448e69166Smrg      rm -f "$dsttmp"
49548e69166Smrg    else
49643ecf206Smrg      # If $backupsuffix is set, and the file being installed
49743ecf206Smrg      # already exists, attempt a backup.  Don't worry if it fails,
49843ecf206Smrg      # e.g., if mv doesn't support -f.
49943ecf206Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
50043ecf206Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50143ecf206Smrg      fi
50243ecf206Smrg
50348e69166Smrg      # Rename the file to the real destination.
50448e69166Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50548e69166Smrg
50648e69166Smrg      # The rename failed, perhaps because mv can't rename something else
50748e69166Smrg      # to itself, or perhaps because mv is so ancient that it does not
50848e69166Smrg      # support -f.
50948e69166Smrg      {
5108846b520Smrg        # Now remove or move aside any old file at destination location.
5118846b520Smrg        # We try this two ways since rm can't unlink itself on some
5128846b520Smrg        # systems and the destination file might be busy for other
5138846b520Smrg        # reasons.  In this case, the final cleanup might fail but the new
5148846b520Smrg        # file should still install successfully.
5158846b520Smrg        {
5168846b520Smrg          test ! -f "$dst" ||
51743ecf206Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
5188846b520Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51943ecf206Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5208846b520Smrg          } ||
5218846b520Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5228846b520Smrg            (exit 1); exit 1
5238846b520Smrg          }
5248846b520Smrg        } &&
5258846b520Smrg
5268846b520Smrg        # Now rename the file to the real destination.
5278846b520Smrg        $doit $mvcmd "$dsttmp" "$dst"
52848e69166Smrg      }
52948e69166Smrg    fi || exit 1
53048e69166Smrg
53148e69166Smrg    trap '' 0
53248e69166Smrg  fi
533fd0c672fSmrgdone
534fd0c672fSmrg
535fd0c672fSmrg# Local variables:
53643ecf206Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537fd0c672fSmrg# time-stamp-start: "scriptversion="
538fd0c672fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53943ecf206Smrg# time-stamp-time-zone: "UTC0"
54048e69166Smrg# time-stamp-end: "; # UTC"
541fd0c672fSmrg# End:
542