install-sh revision fdf6a26f
1444c061aSmrg#!/bin/sh
2444c061aSmrg# install - install a program, script, or datafile
3444c061aSmrg
4fdf6a26fSmrgscriptversion=2020-11-14.01; # UTC
5444c061aSmrg
6444c061aSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7444c061aSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8444c061aSmrg# following copyright and license.
9444c061aSmrg#
10444c061aSmrg# Copyright (C) 1994 X Consortium
11444c061aSmrg#
12444c061aSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13444c061aSmrg# of this software and associated documentation files (the "Software"), to
14444c061aSmrg# deal in the Software without restriction, including without limitation the
15444c061aSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16444c061aSmrg# sell copies of the Software, and to permit persons to whom the Software is
17444c061aSmrg# furnished to do so, subject to the following conditions:
18444c061aSmrg#
19444c061aSmrg# The above copyright notice and this permission notice shall be included in
20444c061aSmrg# all copies or substantial portions of the Software.
21444c061aSmrg#
22444c061aSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23444c061aSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24444c061aSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25444c061aSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26444c061aSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27444c061aSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28444c061aSmrg#
29444c061aSmrg# Except as contained in this notice, the name of the X Consortium shall not
30444c061aSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31444c061aSmrg# ings in this Software without prior written authorization from the X Consor-
32444c061aSmrg# tium.
33444c061aSmrg#
34444c061aSmrg#
35444c061aSmrg# FSF changes to this file are in the public domain.
36444c061aSmrg#
37444c061aSmrg# Calling this script install-sh is preferred over install.sh, to prevent
389e7bcd65Smrg# 'make' implicit rules from creating a file called install from it
39444c061aSmrg# when there is no Makefile.
40444c061aSmrg#
41444c061aSmrg# This script is compatible with the BSD install script, but was written
422265a131Smrg# from scratch.
432265a131Smrg
44339a7c43Smrgtab='	'
452265a131Smrgnl='
462265a131Smrg'
47339a7c43SmrgIFS=" $tab$nl"
48444c061aSmrg
49339a7c43Smrg# Set DOITPROG to "echo" to test this script.
50444c061aSmrg
512265a131Smrgdoit=${DOITPROG-}
52339a7c43Smrgdoit_exec=${doit:-exec}
53444c061aSmrg
542265a131Smrg# Put in absolute file names if you don't have them in your path;
552265a131Smrg# or use environment vars.
562265a131Smrg
572265a131Smrgchgrpprog=${CHGRPPROG-chgrp}
582265a131Smrgchmodprog=${CHMODPROG-chmod}
592265a131Smrgchownprog=${CHOWNPROG-chown}
602265a131Smrgcmpprog=${CMPPROG-cmp}
612265a131Smrgcpprog=${CPPROG-cp}
622265a131Smrgmkdirprog=${MKDIRPROG-mkdir}
632265a131Smrgmvprog=${MVPROG-mv}
642265a131Smrgrmprog=${RMPROG-rm}
652265a131Smrgstripprog=${STRIPPROG-strip}
662265a131Smrg
672265a131Smrgposix_mkdir=
682265a131Smrg
692265a131Smrg# Desired mode of installed file.
702265a131Smrgmode=0755
71444c061aSmrg
72fdf6a26fSmrg# Create dirs (including intermediate dirs) using mode 755.
73fdf6a26fSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74fdf6a26fSmrgmkdir_umask=22
75fdf6a26fSmrg
76fdf6a26fSmrgbackupsuffix=
77444c061aSmrgchgrpcmd=
782265a131Smrgchmodcmd=$chmodprog
792265a131Smrgchowncmd=
802265a131Smrgmvcmd=$mvprog
81444c061aSmrgrmcmd="$rmprog -f"
822265a131Smrgstripcmd=
832265a131Smrg
84444c061aSmrgsrc=
85444c061aSmrgdst=
86444c061aSmrgdir_arg=
872265a131Smrgdst_arg=
882265a131Smrg
892265a131Smrgcopy_on_change=false
90339a7c43Smrgis_target_a_directory=possibly
91444c061aSmrg
922265a131Smrgusage="\
932265a131SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94444c061aSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95444c061aSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96444c061aSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97444c061aSmrg
98444c061aSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99444c061aSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100444c061aSmrgIn the 4th, create DIRECTORIES.
101444c061aSmrg
102444c061aSmrgOptions:
1032265a131Smrg     --help     display this help and exit.
1042265a131Smrg     --version  display version info and exit.
1052265a131Smrg
1062265a131Smrg  -c            (ignored)
107fdf6a26fSmrg  -C            install only if different (preserve data modification time)
1082265a131Smrg  -d            create directories instead of installing files.
1092265a131Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1102265a131Smrg  -m MODE       $chmodprog installed files to MODE.
1112265a131Smrg  -o USER       $chownprog installed files to USER.
112fdf6a26fSmrg  -p            pass -p to $cpprog.
1132265a131Smrg  -s            $stripprog installed files.
114fdf6a26fSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1152265a131Smrg  -t DIRECTORY  install into DIRECTORY.
1162265a131Smrg  -T            report an error if DSTFILE is a directory.
117444c061aSmrg
118444c061aSmrgEnvironment variables override the default commands:
1192265a131Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1202265a131Smrg  RMPROG STRIPPROG
121fdf6a26fSmrg
122fdf6a26fSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123fdf6a26fSmrgit's up to you to specify -f if you want it.
124fdf6a26fSmrg
125fdf6a26fSmrgIf -S is not specified, no backups are attempted.
126fdf6a26fSmrg
127fdf6a26fSmrgEmail bug reports to bug-automake@gnu.org.
128fdf6a26fSmrgAutomake home page: https://www.gnu.org/software/automake/
129444c061aSmrg"
130444c061aSmrg
1312265a131Smrgwhile test $# -ne 0; do
132444c061aSmrg  case $1 in
1332265a131Smrg    -c) ;;
1342265a131Smrg
1352265a131Smrg    -C) copy_on_change=true;;
136444c061aSmrg
1372265a131Smrg    -d) dir_arg=true;;
138444c061aSmrg
139444c061aSmrg    -g) chgrpcmd="$chgrpprog $2"
140339a7c43Smrg        shift;;
141444c061aSmrg
142444c061aSmrg    --help) echo "$usage"; exit $?;;
143444c061aSmrg
1442265a131Smrg    -m) mode=$2
145339a7c43Smrg        case $mode in
146339a7c43Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147339a7c43Smrg            echo "$0: invalid mode: $mode" >&2
148339a7c43Smrg            exit 1;;
149339a7c43Smrg        esac
150339a7c43Smrg        shift;;
151444c061aSmrg
152444c061aSmrg    -o) chowncmd="$chownprog $2"
153339a7c43Smrg        shift;;
154444c061aSmrg
155fdf6a26fSmrg    -p) cpprog="$cpprog -p";;
156fdf6a26fSmrg
1572265a131Smrg    -s) stripcmd=$stripprog;;
158444c061aSmrg
159fdf6a26fSmrg    -S) backupsuffix="$2"
160fdf6a26fSmrg        shift;;
161fdf6a26fSmrg
162339a7c43Smrg    -t)
163339a7c43Smrg        is_target_a_directory=always
164339a7c43Smrg        dst_arg=$2
165339a7c43Smrg        # Protect names problematic for 'test' and other utilities.
166339a7c43Smrg        case $dst_arg in
167339a7c43Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168339a7c43Smrg        esac
169339a7c43Smrg        shift;;
170444c061aSmrg
171339a7c43Smrg    -T) is_target_a_directory=never;;
172444c061aSmrg
173444c061aSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174444c061aSmrg
175339a7c43Smrg    --) shift
176339a7c43Smrg        break;;
1772265a131Smrg
178339a7c43Smrg    -*) echo "$0: invalid option: $1" >&2
179339a7c43Smrg        exit 1;;
1802265a131Smrg
1812265a131Smrg    *)  break;;
182444c061aSmrg  esac
1832265a131Smrg  shift
184444c061aSmrgdone
185444c061aSmrg
186339a7c43Smrg# We allow the use of options -d and -T together, by making -d
187339a7c43Smrg# take the precedence; this is for compatibility with GNU install.
188339a7c43Smrg
189339a7c43Smrgif test -n "$dir_arg"; then
190339a7c43Smrg  if test -n "$dst_arg"; then
191339a7c43Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192339a7c43Smrg    exit 1
193339a7c43Smrg  fi
194339a7c43Smrgfi
195339a7c43Smrg
1962265a131Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1972265a131Smrg  # When -d is used, all remaining arguments are directories to create.
1982265a131Smrg  # When -t is used, the destination is already specified.
1992265a131Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2002265a131Smrg  for arg
2012265a131Smrg  do
2022265a131Smrg    if test -n "$dst_arg"; then
2032265a131Smrg      # $@ is not empty: it contains at least $arg.
2042265a131Smrg      set fnord "$@" "$dst_arg"
2052265a131Smrg      shift # fnord
2062265a131Smrg    fi
2072265a131Smrg    shift # arg
2082265a131Smrg    dst_arg=$arg
2099e7bcd65Smrg    # Protect names problematic for 'test' and other utilities.
2109e7bcd65Smrg    case $dst_arg in
2119e7bcd65Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2129e7bcd65Smrg    esac
2132265a131Smrg  done
2142265a131Smrgfi
2152265a131Smrg
2162265a131Smrgif test $# -eq 0; then
217444c061aSmrg  if test -z "$dir_arg"; then
218444c061aSmrg    echo "$0: no input file specified." >&2
219444c061aSmrg    exit 1
220444c061aSmrg  fi
2219e7bcd65Smrg  # It's OK to call 'install-sh -d' without argument.
222444c061aSmrg  # This can happen when creating conditional directories.
223444c061aSmrg  exit 0
224444c061aSmrgfi
225444c061aSmrg
226339a7c43Smrgif test -z "$dir_arg"; then
227339a7c43Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228339a7c43Smrg    if test ! -d "$dst_arg"; then
229339a7c43Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230339a7c43Smrg      exit 1
231339a7c43Smrg    fi
232339a7c43Smrg  fi
233339a7c43Smrgfi
234339a7c43Smrg
2352265a131Smrgif test -z "$dir_arg"; then
2369e7bcd65Smrg  do_exit='(exit $ret); exit $ret'
2379e7bcd65Smrg  trap "ret=129; $do_exit" 1
2389e7bcd65Smrg  trap "ret=130; $do_exit" 2
2399e7bcd65Smrg  trap "ret=141; $do_exit" 13
2409e7bcd65Smrg  trap "ret=143; $do_exit" 15
2412265a131Smrg
2422265a131Smrg  # Set umask so as not to create temps with too-generous modes.
2432265a131Smrg  # However, 'strip' requires both read and write access to temps.
2442265a131Smrg  case $mode in
2452265a131Smrg    # Optimize common cases.
2462265a131Smrg    *644) cp_umask=133;;
2472265a131Smrg    *755) cp_umask=22;;
2482265a131Smrg
2492265a131Smrg    *[0-7])
2502265a131Smrg      if test -z "$stripcmd"; then
251339a7c43Smrg        u_plus_rw=
2522265a131Smrg      else
253339a7c43Smrg        u_plus_rw='% 200'
2542265a131Smrg      fi
2552265a131Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2562265a131Smrg    *)
2572265a131Smrg      if test -z "$stripcmd"; then
258339a7c43Smrg        u_plus_rw=
2592265a131Smrg      else
260339a7c43Smrg        u_plus_rw=,u+rw
2612265a131Smrg      fi
2622265a131Smrg      cp_umask=$mode$u_plus_rw;;
2632265a131Smrg  esac
2642265a131Smrgfi
2652265a131Smrg
266444c061aSmrgfor src
267444c061aSmrgdo
2689e7bcd65Smrg  # Protect names problematic for 'test' and other utilities.
269444c061aSmrg  case $src in
2709e7bcd65Smrg    -* | [=\(\)!]) src=./$src;;
271444c061aSmrg  esac
272444c061aSmrg
273444c061aSmrg  if test -n "$dir_arg"; then
274444c061aSmrg    dst=$src
2752265a131Smrg    dstdir=$dst
2762265a131Smrg    test -d "$dstdir"
2772265a131Smrg    dstdir_status=$?
278fdf6a26fSmrg    # Don't chown directories that already exist.
279fdf6a26fSmrg    if test $dstdir_status = 0; then
280fdf6a26fSmrg      chowncmd=""
281fdf6a26fSmrg    fi
282444c061aSmrg  else
2832265a131Smrg
284444c061aSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285444c061aSmrg    # might cause directories to be created, which would be especially bad
286444c061aSmrg    # if $src (and thus $dsttmp) contains '*'.
287444c061aSmrg    if test ! -f "$src" && test ! -d "$src"; then
288444c061aSmrg      echo "$0: $src does not exist." >&2
289444c061aSmrg      exit 1
290444c061aSmrg    fi
291444c061aSmrg
2922265a131Smrg    if test -z "$dst_arg"; then
293444c061aSmrg      echo "$0: no destination specified." >&2
294444c061aSmrg      exit 1
295444c061aSmrg    fi
2962265a131Smrg    dst=$dst_arg
297444c061aSmrg
2980568f49bSmrg    # If destination is a directory, append the input filename.
299444c061aSmrg    if test -d "$dst"; then
300339a7c43Smrg      if test "$is_target_a_directory" = never; then
301339a7c43Smrg        echo "$0: $dst_arg: Is a directory" >&2
302339a7c43Smrg        exit 1
303444c061aSmrg      fi
3042265a131Smrg      dstdir=$dst
3050568f49bSmrg      dstbase=`basename "$src"`
3060568f49bSmrg      case $dst in
3070568f49bSmrg	*/) dst=$dst$dstbase;;
3080568f49bSmrg	*)  dst=$dst/$dstbase;;
3090568f49bSmrg      esac
3102265a131Smrg      dstdir_status=0
3112265a131Smrg    else
312339a7c43Smrg      dstdir=`dirname "$dst"`
3132265a131Smrg      test -d "$dstdir"
3142265a131Smrg      dstdir_status=$?
315444c061aSmrg    fi
316444c061aSmrg  fi
317444c061aSmrg
3180568f49bSmrg  case $dstdir in
3190568f49bSmrg    */) dstdirslash=$dstdir;;
3200568f49bSmrg    *)  dstdirslash=$dstdir/;;
3210568f49bSmrg  esac
3220568f49bSmrg
3232265a131Smrg  obsolete_mkdir_used=false
3242265a131Smrg
3252265a131Smrg  if test $dstdir_status != 0; then
3262265a131Smrg    case $posix_mkdir in
3272265a131Smrg      '')
328339a7c43Smrg        # With -d, create the new directory with the user-specified mode.
329339a7c43Smrg        # Otherwise, rely on $mkdir_umask.
330339a7c43Smrg        if test -n "$dir_arg"; then
331339a7c43Smrg          mkdir_mode=-m$mode
332339a7c43Smrg        else
333339a7c43Smrg          mkdir_mode=
334339a7c43Smrg        fi
335339a7c43Smrg
336339a7c43Smrg        posix_mkdir=false
337fdf6a26fSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338fdf6a26fSmrg	# here however when possible just to lower collision chance.
339fdf6a26fSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340fdf6a26fSmrg
341fdf6a26fSmrg	trap '
342fdf6a26fSmrg	  ret=$?
343fdf6a26fSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344fdf6a26fSmrg	  exit $ret
345fdf6a26fSmrg	' 0
346fdf6a26fSmrg
347fdf6a26fSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
348fdf6a26fSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349fdf6a26fSmrg	# directory is successfully created first before we actually test
350fdf6a26fSmrg	# 'mkdir -p'.
351fdf6a26fSmrg	if (umask $mkdir_umask &&
352fdf6a26fSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353fdf6a26fSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354fdf6a26fSmrg	then
355fdf6a26fSmrg	  if test -z "$dir_arg" || {
356fdf6a26fSmrg	       # Check for POSIX incompatibilities with -m.
357fdf6a26fSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358fdf6a26fSmrg	       # other-writable bit of parent directory when it shouldn't.
359fdf6a26fSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360fdf6a26fSmrg	       test_tmpdir="$tmpdir/a"
361fdf6a26fSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362fdf6a26fSmrg	       case $ls_ld_tmpdir in
363fdf6a26fSmrg		 d????-?r-*) different_mode=700;;
364fdf6a26fSmrg		 d????-?--*) different_mode=755;;
365fdf6a26fSmrg		 *) false;;
366fdf6a26fSmrg	       esac &&
367fdf6a26fSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368fdf6a26fSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369fdf6a26fSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370fdf6a26fSmrg	       }
371fdf6a26fSmrg	     }
372fdf6a26fSmrg	  then posix_mkdir=:
373fdf6a26fSmrg	  fi
374fdf6a26fSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375fdf6a26fSmrg	else
376fdf6a26fSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377fdf6a26fSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378fdf6a26fSmrg	fi
379fdf6a26fSmrg	trap '' 0;;
3802265a131Smrg    esac
381444c061aSmrg
3822265a131Smrg    if
3832265a131Smrg      $posix_mkdir && (
384339a7c43Smrg        umask $mkdir_umask &&
385339a7c43Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3862265a131Smrg      )
3872265a131Smrg    then :
3882265a131Smrg    else
389444c061aSmrg
390fdf6a26fSmrg      # mkdir does not conform to POSIX,
3912265a131Smrg      # or it failed possibly due to a race condition.  Create the
3922265a131Smrg      # directory the slow way, step by step, checking for races as we go.
393444c061aSmrg
3942265a131Smrg      case $dstdir in
395339a7c43Smrg        /*) prefix='/';;
396339a7c43Smrg        [-=\(\)!]*) prefix='./';;
397339a7c43Smrg        *)  prefix='';;
3982265a131Smrg      esac
399444c061aSmrg
4002265a131Smrg      oIFS=$IFS
4012265a131Smrg      IFS=/
402339a7c43Smrg      set -f
4032265a131Smrg      set fnord $dstdir
404444c061aSmrg      shift
405339a7c43Smrg      set +f
4062265a131Smrg      IFS=$oIFS
4072265a131Smrg
4082265a131Smrg      prefixes=
4092265a131Smrg
4102265a131Smrg      for d
4112265a131Smrg      do
412339a7c43Smrg        test X"$d" = X && continue
413339a7c43Smrg
414339a7c43Smrg        prefix=$prefix$d
415339a7c43Smrg        if test -d "$prefix"; then
416339a7c43Smrg          prefixes=
417339a7c43Smrg        else
418339a7c43Smrg          if $posix_mkdir; then
419fdf6a26fSmrg            (umask $mkdir_umask &&
420339a7c43Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421339a7c43Smrg            # Don't fail if two instances are running concurrently.
422339a7c43Smrg            test -d "$prefix" || exit 1
423339a7c43Smrg          else
424339a7c43Smrg            case $prefix in
425339a7c43Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426339a7c43Smrg              *) qprefix=$prefix;;
427339a7c43Smrg            esac
428339a7c43Smrg            prefixes="$prefixes '$qprefix'"
429339a7c43Smrg          fi
430339a7c43Smrg        fi
431339a7c43Smrg        prefix=$prefix/
4322265a131Smrg      done
4332265a131Smrg
4342265a131Smrg      if test -n "$prefixes"; then
435339a7c43Smrg        # Don't fail if two instances are running concurrently.
436339a7c43Smrg        (umask $mkdir_umask &&
437339a7c43Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438339a7c43Smrg          test -d "$dstdir" || exit 1
439339a7c43Smrg        obsolete_mkdir_used=true
440444c061aSmrg      fi
4412265a131Smrg    fi
442444c061aSmrg  fi
443444c061aSmrg
444444c061aSmrg  if test -n "$dir_arg"; then
4452265a131Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4462265a131Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4472265a131Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4482265a131Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449444c061aSmrg  else
450444c061aSmrg
451444c061aSmrg    # Make a couple of temp file names in the proper directory.
4520568f49bSmrg    dsttmp=${dstdirslash}_inst.$$_
4530568f49bSmrg    rmtmp=${dstdirslash}_rm.$$_
454444c061aSmrg
455444c061aSmrg    # Trap to clean up those temp files at exit.
456444c061aSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457444c061aSmrg
458444c061aSmrg    # Copy the file name to the temp name.
459a3bd7f05Smrg    (umask $cp_umask &&
460a3bd7f05Smrg     { test -z "$stripcmd" || {
461a3bd7f05Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462a3bd7f05Smrg	 # which would cause strip to fail.
463a3bd7f05Smrg	 if test -z "$doit"; then
464a3bd7f05Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465a3bd7f05Smrg	 else
466a3bd7f05Smrg	   $doit touch "$dsttmp"
467a3bd7f05Smrg	 fi
468a3bd7f05Smrg       }
469a3bd7f05Smrg     } &&
470a3bd7f05Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471444c061aSmrg
472444c061aSmrg    # and set any options; do chmod last to preserve setuid bits.
473444c061aSmrg    #
474444c061aSmrg    # If any of these fail, we abort the whole thing.  If we want to
475444c061aSmrg    # ignore errors from any of these, just make sure not to ignore
476444c061aSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477444c061aSmrg    #
4782265a131Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4792265a131Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4802265a131Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4812265a131Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4822265a131Smrg
4832265a131Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4842265a131Smrg    if $copy_on_change &&
485339a7c43Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486339a7c43Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487339a7c43Smrg       set -f &&
4882265a131Smrg       set X $old && old=:$2:$4:$5:$6 &&
4892265a131Smrg       set X $new && new=:$2:$4:$5:$6 &&
490339a7c43Smrg       set +f &&
4912265a131Smrg       test "$old" = "$new" &&
4922265a131Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4932265a131Smrg    then
4942265a131Smrg      rm -f "$dsttmp"
4952265a131Smrg    else
496fdf6a26fSmrg      # If $backupsuffix is set, and the file being installed
497fdf6a26fSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498fdf6a26fSmrg      # e.g., if mv doesn't support -f.
499fdf6a26fSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500fdf6a26fSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501fdf6a26fSmrg      fi
502fdf6a26fSmrg
5032265a131Smrg      # Rename the file to the real destination.
5042265a131Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5052265a131Smrg
5062265a131Smrg      # The rename failed, perhaps because mv can't rename something else
5072265a131Smrg      # to itself, or perhaps because mv is so ancient that it does not
5082265a131Smrg      # support -f.
5092265a131Smrg      {
510339a7c43Smrg        # Now remove or move aside any old file at destination location.
511339a7c43Smrg        # We try this two ways since rm can't unlink itself on some
512339a7c43Smrg        # systems and the destination file might be busy for other
513339a7c43Smrg        # reasons.  In this case, the final cleanup might fail but the new
514339a7c43Smrg        # file should still install successfully.
515339a7c43Smrg        {
516339a7c43Smrg          test ! -f "$dst" ||
517fdf6a26fSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
518339a7c43Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519fdf6a26fSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520339a7c43Smrg          } ||
521339a7c43Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522339a7c43Smrg            (exit 1); exit 1
523339a7c43Smrg          }
524339a7c43Smrg        } &&
525339a7c43Smrg
526339a7c43Smrg        # Now rename the file to the real destination.
527339a7c43Smrg        $doit $mvcmd "$dsttmp" "$dst"
5282265a131Smrg      }
5292265a131Smrg    fi || exit 1
5302265a131Smrg
5312265a131Smrg    trap '' 0
5322265a131Smrg  fi
533444c061aSmrgdone
534444c061aSmrg
535444c061aSmrg# Local variables:
5360568f49bSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
537444c061aSmrg# time-stamp-start: "scriptversion="
538444c061aSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5390568f49bSmrg# time-stamp-time-zone: "UTC0"
5402ae83157Smrg# time-stamp-end: "; # UTC"
541444c061aSmrg# End:
542