install-sh revision b71ad168
164ce7165Smrg#!/bin/sh
264ce7165Smrg# install - install a program, script, or datafile
364ce7165Smrg
4b71ad168Smrgscriptversion=2020-11-14.01; # UTC
564ce7165Smrg
664ce7165Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
764ce7165Smrg# later released in X11R6 (xc/config/util/install.sh) with the
864ce7165Smrg# following copyright and license.
964ce7165Smrg#
1064ce7165Smrg# Copyright (C) 1994 X Consortium
1164ce7165Smrg#
1264ce7165Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1364ce7165Smrg# of this software and associated documentation files (the "Software"), to
1464ce7165Smrg# deal in the Software without restriction, including without limitation the
1564ce7165Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1664ce7165Smrg# sell copies of the Software, and to permit persons to whom the Software is
1764ce7165Smrg# furnished to do so, subject to the following conditions:
1864ce7165Smrg#
1964ce7165Smrg# The above copyright notice and this permission notice shall be included in
2064ce7165Smrg# all copies or substantial portions of the Software.
2164ce7165Smrg#
2264ce7165Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2364ce7165Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2464ce7165Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2564ce7165Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2664ce7165Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2764ce7165Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2864ce7165Smrg#
2964ce7165Smrg# Except as contained in this notice, the name of the X Consortium shall not
3064ce7165Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3164ce7165Smrg# ings in this Software without prior written authorization from the X Consor-
3264ce7165Smrg# tium.
3364ce7165Smrg#
3464ce7165Smrg#
3564ce7165Smrg# FSF changes to this file are in the public domain.
3664ce7165Smrg#
3764ce7165Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3881440437Smrg# 'make' implicit rules from creating a file called install from it
3964ce7165Smrg# when there is no Makefile.
4064ce7165Smrg#
4164ce7165Smrg# This script is compatible with the BSD install script, but was written
4264ce7165Smrg# from scratch.
4364ce7165Smrg
4495fdd44cSmrgtab='	'
4564ce7165Smrgnl='
4664ce7165Smrg'
4795fdd44cSmrgIFS=" $tab$nl"
4864ce7165Smrg
4995fdd44cSmrg# Set DOITPROG to "echo" to test this script.
5064ce7165Smrg
51fb23d3a8Smrgdoit=${DOITPROG-}
5295fdd44cSmrgdoit_exec=${doit:-exec}
5364ce7165Smrg
5464ce7165Smrg# Put in absolute file names if you don't have them in your path;
5564ce7165Smrg# or use environment vars.
5664ce7165Smrg
57fb23d3a8Smrgchgrpprog=${CHGRPPROG-chgrp}
58fb23d3a8Smrgchmodprog=${CHMODPROG-chmod}
59fb23d3a8Smrgchownprog=${CHOWNPROG-chown}
60fb23d3a8Smrgcmpprog=${CMPPROG-cmp}
61fb23d3a8Smrgcpprog=${CPPROG-cp}
62fb23d3a8Smrgmkdirprog=${MKDIRPROG-mkdir}
63fb23d3a8Smrgmvprog=${MVPROG-mv}
64fb23d3a8Smrgrmprog=${RMPROG-rm}
65fb23d3a8Smrgstripprog=${STRIPPROG-strip}
66fb23d3a8Smrg
6764ce7165Smrgposix_mkdir=
6864ce7165Smrg
6964ce7165Smrg# Desired mode of installed file.
7064ce7165Smrgmode=0755
7164ce7165Smrg
72b71ad168Smrg# Create dirs (including intermediate dirs) using mode 755.
73b71ad168Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74b71ad168Smrgmkdir_umask=22
75b71ad168Smrg
76b71ad168Smrgbackupsuffix=
77fb23d3a8Smrgchgrpcmd=
7864ce7165Smrgchmodcmd=$chmodprog
7964ce7165Smrgchowncmd=
80fb23d3a8Smrgmvcmd=$mvprog
8164ce7165Smrgrmcmd="$rmprog -f"
82fb23d3a8Smrgstripcmd=
83fb23d3a8Smrg
8464ce7165Smrgsrc=
8564ce7165Smrgdst=
8664ce7165Smrgdir_arg=
87fb23d3a8Smrgdst_arg=
88fb23d3a8Smrg
89fb23d3a8Smrgcopy_on_change=false
9095fdd44cSmrgis_target_a_directory=possibly
9164ce7165Smrg
92fb23d3a8Smrgusage="\
93fb23d3a8SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9464ce7165Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9564ce7165Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9664ce7165Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9764ce7165Smrg
9864ce7165SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9964ce7165SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10064ce7165SmrgIn the 4th, create DIRECTORIES.
10164ce7165Smrg
10264ce7165SmrgOptions:
103fb23d3a8Smrg     --help     display this help and exit.
104fb23d3a8Smrg     --version  display version info and exit.
105fb23d3a8Smrg
106fb23d3a8Smrg  -c            (ignored)
107b71ad168Smrg  -C            install only if different (preserve data modification time)
108fb23d3a8Smrg  -d            create directories instead of installing files.
109fb23d3a8Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110fb23d3a8Smrg  -m MODE       $chmodprog installed files to MODE.
111fb23d3a8Smrg  -o USER       $chownprog installed files to USER.
112b71ad168Smrg  -p            pass -p to $cpprog.
113fb23d3a8Smrg  -s            $stripprog installed files.
114b71ad168Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115fb23d3a8Smrg  -t DIRECTORY  install into DIRECTORY.
116fb23d3a8Smrg  -T            report an error if DSTFILE is a directory.
11764ce7165Smrg
11864ce7165SmrgEnvironment variables override the default commands:
119fb23d3a8Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120fb23d3a8Smrg  RMPROG STRIPPROG
121b71ad168Smrg
122b71ad168SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123b71ad168Smrgit's up to you to specify -f if you want it.
124b71ad168Smrg
125b71ad168SmrgIf -S is not specified, no backups are attempted.
126b71ad168Smrg
127b71ad168SmrgEmail bug reports to bug-automake@gnu.org.
128b71ad168SmrgAutomake home page: https://www.gnu.org/software/automake/
12964ce7165Smrg"
13064ce7165Smrg
13164ce7165Smrgwhile test $# -ne 0; do
13264ce7165Smrg  case $1 in
133fb23d3a8Smrg    -c) ;;
134fb23d3a8Smrg
135fb23d3a8Smrg    -C) copy_on_change=true;;
13664ce7165Smrg
137fb23d3a8Smrg    -d) dir_arg=true;;
13864ce7165Smrg
13964ce7165Smrg    -g) chgrpcmd="$chgrpprog $2"
14095fdd44cSmrg        shift;;
14164ce7165Smrg
14264ce7165Smrg    --help) echo "$usage"; exit $?;;
14364ce7165Smrg
14464ce7165Smrg    -m) mode=$2
14595fdd44cSmrg        case $mode in
14695fdd44cSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14795fdd44cSmrg            echo "$0: invalid mode: $mode" >&2
14895fdd44cSmrg            exit 1;;
14995fdd44cSmrg        esac
15095fdd44cSmrg        shift;;
15164ce7165Smrg
15264ce7165Smrg    -o) chowncmd="$chownprog $2"
15395fdd44cSmrg        shift;;
15464ce7165Smrg
155b71ad168Smrg    -p) cpprog="$cpprog -p";;
156b71ad168Smrg
157fb23d3a8Smrg    -s) stripcmd=$stripprog;;
15864ce7165Smrg
159b71ad168Smrg    -S) backupsuffix="$2"
160b71ad168Smrg        shift;;
161b71ad168Smrg
16295fdd44cSmrg    -t)
16395fdd44cSmrg        is_target_a_directory=always
16495fdd44cSmrg        dst_arg=$2
16595fdd44cSmrg        # Protect names problematic for 'test' and other utilities.
16695fdd44cSmrg        case $dst_arg in
16795fdd44cSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16895fdd44cSmrg        esac
16995fdd44cSmrg        shift;;
17064ce7165Smrg
17195fdd44cSmrg    -T) is_target_a_directory=never;;
17264ce7165Smrg
17364ce7165Smrg    --version) echo "$0 $scriptversion"; exit $?;;
17464ce7165Smrg
17595fdd44cSmrg    --) shift
17695fdd44cSmrg        break;;
17764ce7165Smrg
17895fdd44cSmrg    -*) echo "$0: invalid option: $1" >&2
17995fdd44cSmrg        exit 1;;
18064ce7165Smrg
18164ce7165Smrg    *)  break;;
18264ce7165Smrg  esac
183fb23d3a8Smrg  shift
18464ce7165Smrgdone
18564ce7165Smrg
18695fdd44cSmrg# We allow the use of options -d and -T together, by making -d
18795fdd44cSmrg# take the precedence; this is for compatibility with GNU install.
18895fdd44cSmrg
18995fdd44cSmrgif test -n "$dir_arg"; then
19095fdd44cSmrg  if test -n "$dst_arg"; then
19195fdd44cSmrg    echo "$0: target directory not allowed when installing a directory." >&2
19295fdd44cSmrg    exit 1
19395fdd44cSmrg  fi
19495fdd44cSmrgfi
19595fdd44cSmrg
196fb23d3a8Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19764ce7165Smrg  # When -d is used, all remaining arguments are directories to create.
19864ce7165Smrg  # When -t is used, the destination is already specified.
19964ce7165Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20064ce7165Smrg  for arg
20164ce7165Smrg  do
202fb23d3a8Smrg    if test -n "$dst_arg"; then
20364ce7165Smrg      # $@ is not empty: it contains at least $arg.
204fb23d3a8Smrg      set fnord "$@" "$dst_arg"
20564ce7165Smrg      shift # fnord
20664ce7165Smrg    fi
20764ce7165Smrg    shift # arg
208fb23d3a8Smrg    dst_arg=$arg
20981440437Smrg    # Protect names problematic for 'test' and other utilities.
21081440437Smrg    case $dst_arg in
21181440437Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21281440437Smrg    esac
21364ce7165Smrg  done
21464ce7165Smrgfi
21564ce7165Smrg
21664ce7165Smrgif test $# -eq 0; then
21764ce7165Smrg  if test -z "$dir_arg"; then
21864ce7165Smrg    echo "$0: no input file specified." >&2
21964ce7165Smrg    exit 1
22064ce7165Smrg  fi
22181440437Smrg  # It's OK to call 'install-sh -d' without argument.
22264ce7165Smrg  # This can happen when creating conditional directories.
22364ce7165Smrg  exit 0
22464ce7165Smrgfi
22564ce7165Smrg
22695fdd44cSmrgif test -z "$dir_arg"; then
22795fdd44cSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22895fdd44cSmrg    if test ! -d "$dst_arg"; then
22995fdd44cSmrg      echo "$0: $dst_arg: Is not a directory." >&2
23095fdd44cSmrg      exit 1
23195fdd44cSmrg    fi
23295fdd44cSmrg  fi
23395fdd44cSmrgfi
23495fdd44cSmrg
23564ce7165Smrgif test -z "$dir_arg"; then
23681440437Smrg  do_exit='(exit $ret); exit $ret'
23781440437Smrg  trap "ret=129; $do_exit" 1
23881440437Smrg  trap "ret=130; $do_exit" 2
23981440437Smrg  trap "ret=141; $do_exit" 13
24081440437Smrg  trap "ret=143; $do_exit" 15
24164ce7165Smrg
24264ce7165Smrg  # Set umask so as not to create temps with too-generous modes.
24364ce7165Smrg  # However, 'strip' requires both read and write access to temps.
24464ce7165Smrg  case $mode in
24564ce7165Smrg    # Optimize common cases.
24664ce7165Smrg    *644) cp_umask=133;;
24764ce7165Smrg    *755) cp_umask=22;;
24864ce7165Smrg
24964ce7165Smrg    *[0-7])
25064ce7165Smrg      if test -z "$stripcmd"; then
25195fdd44cSmrg        u_plus_rw=
25264ce7165Smrg      else
25395fdd44cSmrg        u_plus_rw='% 200'
25464ce7165Smrg      fi
25564ce7165Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25664ce7165Smrg    *)
25764ce7165Smrg      if test -z "$stripcmd"; then
25895fdd44cSmrg        u_plus_rw=
25964ce7165Smrg      else
26095fdd44cSmrg        u_plus_rw=,u+rw
26164ce7165Smrg      fi
26264ce7165Smrg      cp_umask=$mode$u_plus_rw;;
26364ce7165Smrg  esac
26464ce7165Smrgfi
26564ce7165Smrg
26664ce7165Smrgfor src
26764ce7165Smrgdo
26881440437Smrg  # Protect names problematic for 'test' and other utilities.
26964ce7165Smrg  case $src in
27081440437Smrg    -* | [=\(\)!]) src=./$src;;
27164ce7165Smrg  esac
27264ce7165Smrg
27364ce7165Smrg  if test -n "$dir_arg"; then
27464ce7165Smrg    dst=$src
27564ce7165Smrg    dstdir=$dst
27664ce7165Smrg    test -d "$dstdir"
27764ce7165Smrg    dstdir_status=$?
278b71ad168Smrg    # Don't chown directories that already exist.
279b71ad168Smrg    if test $dstdir_status = 0; then
280b71ad168Smrg      chowncmd=""
281b71ad168Smrg    fi
28264ce7165Smrg  else
28364ce7165Smrg
28464ce7165Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28564ce7165Smrg    # might cause directories to be created, which would be especially bad
28664ce7165Smrg    # if $src (and thus $dsttmp) contains '*'.
28764ce7165Smrg    if test ! -f "$src" && test ! -d "$src"; then
28864ce7165Smrg      echo "$0: $src does not exist." >&2
28964ce7165Smrg      exit 1
29064ce7165Smrg    fi
29164ce7165Smrg
292fb23d3a8Smrg    if test -z "$dst_arg"; then
29364ce7165Smrg      echo "$0: no destination specified." >&2
29464ce7165Smrg      exit 1
29564ce7165Smrg    fi
296fb23d3a8Smrg    dst=$dst_arg
29764ce7165Smrg
298b71ad168Smrg    # If destination is a directory, append the input filename.
29964ce7165Smrg    if test -d "$dst"; then
30095fdd44cSmrg      if test "$is_target_a_directory" = never; then
30195fdd44cSmrg        echo "$0: $dst_arg: Is a directory" >&2
30295fdd44cSmrg        exit 1
30364ce7165Smrg      fi
30464ce7165Smrg      dstdir=$dst
305b71ad168Smrg      dstbase=`basename "$src"`
306b71ad168Smrg      case $dst in
307b71ad168Smrg	*/) dst=$dst$dstbase;;
308b71ad168Smrg	*)  dst=$dst/$dstbase;;
309b71ad168Smrg      esac
31064ce7165Smrg      dstdir_status=0
31164ce7165Smrg    else
31295fdd44cSmrg      dstdir=`dirname "$dst"`
31364ce7165Smrg      test -d "$dstdir"
31464ce7165Smrg      dstdir_status=$?
31564ce7165Smrg    fi
31664ce7165Smrg  fi
31764ce7165Smrg
318b71ad168Smrg  case $dstdir in
319b71ad168Smrg    */) dstdirslash=$dstdir;;
320b71ad168Smrg    *)  dstdirslash=$dstdir/;;
321b71ad168Smrg  esac
322b71ad168Smrg
32364ce7165Smrg  obsolete_mkdir_used=false
32464ce7165Smrg
32564ce7165Smrg  if test $dstdir_status != 0; then
32664ce7165Smrg    case $posix_mkdir in
32764ce7165Smrg      '')
32895fdd44cSmrg        # With -d, create the new directory with the user-specified mode.
32995fdd44cSmrg        # Otherwise, rely on $mkdir_umask.
33095fdd44cSmrg        if test -n "$dir_arg"; then
33195fdd44cSmrg          mkdir_mode=-m$mode
33295fdd44cSmrg        else
33395fdd44cSmrg          mkdir_mode=
33495fdd44cSmrg        fi
33595fdd44cSmrg
33695fdd44cSmrg        posix_mkdir=false
337b71ad168Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338b71ad168Smrg	# here however when possible just to lower collision chance.
339b71ad168Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340b71ad168Smrg
341b71ad168Smrg	trap '
342b71ad168Smrg	  ret=$?
343b71ad168Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344b71ad168Smrg	  exit $ret
345b71ad168Smrg	' 0
346b71ad168Smrg
347b71ad168Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348b71ad168Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349b71ad168Smrg	# directory is successfully created first before we actually test
350b71ad168Smrg	# 'mkdir -p'.
351b71ad168Smrg	if (umask $mkdir_umask &&
352b71ad168Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353b71ad168Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354b71ad168Smrg	then
355b71ad168Smrg	  if test -z "$dir_arg" || {
356b71ad168Smrg	       # Check for POSIX incompatibilities with -m.
357b71ad168Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358b71ad168Smrg	       # other-writable bit of parent directory when it shouldn't.
359b71ad168Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360b71ad168Smrg	       test_tmpdir="$tmpdir/a"
361b71ad168Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362b71ad168Smrg	       case $ls_ld_tmpdir in
363b71ad168Smrg		 d????-?r-*) different_mode=700;;
364b71ad168Smrg		 d????-?--*) different_mode=755;;
365b71ad168Smrg		 *) false;;
366b71ad168Smrg	       esac &&
367b71ad168Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368b71ad168Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369b71ad168Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370b71ad168Smrg	       }
371b71ad168Smrg	     }
372b71ad168Smrg	  then posix_mkdir=:
373b71ad168Smrg	  fi
374b71ad168Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375b71ad168Smrg	else
376b71ad168Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377b71ad168Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378b71ad168Smrg	fi
379b71ad168Smrg	trap '' 0;;
38064ce7165Smrg    esac
38164ce7165Smrg
38264ce7165Smrg    if
38364ce7165Smrg      $posix_mkdir && (
38495fdd44cSmrg        umask $mkdir_umask &&
38595fdd44cSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38664ce7165Smrg      )
38764ce7165Smrg    then :
38864ce7165Smrg    else
38964ce7165Smrg
390b71ad168Smrg      # mkdir does not conform to POSIX,
39164ce7165Smrg      # or it failed possibly due to a race condition.  Create the
39264ce7165Smrg      # directory the slow way, step by step, checking for races as we go.
39364ce7165Smrg
39464ce7165Smrg      case $dstdir in
39595fdd44cSmrg        /*) prefix='/';;
39695fdd44cSmrg        [-=\(\)!]*) prefix='./';;
39795fdd44cSmrg        *)  prefix='';;
39864ce7165Smrg      esac
39964ce7165Smrg
40064ce7165Smrg      oIFS=$IFS
40164ce7165Smrg      IFS=/
40295fdd44cSmrg      set -f
40364ce7165Smrg      set fnord $dstdir
40464ce7165Smrg      shift
40595fdd44cSmrg      set +f
40664ce7165Smrg      IFS=$oIFS
40764ce7165Smrg
40864ce7165Smrg      prefixes=
40964ce7165Smrg
41064ce7165Smrg      for d
41164ce7165Smrg      do
41295fdd44cSmrg        test X"$d" = X && continue
41395fdd44cSmrg
41495fdd44cSmrg        prefix=$prefix$d
41595fdd44cSmrg        if test -d "$prefix"; then
41695fdd44cSmrg          prefixes=
41795fdd44cSmrg        else
41895fdd44cSmrg          if $posix_mkdir; then
419b71ad168Smrg            (umask $mkdir_umask &&
42095fdd44cSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42195fdd44cSmrg            # Don't fail if two instances are running concurrently.
42295fdd44cSmrg            test -d "$prefix" || exit 1
42395fdd44cSmrg          else
42495fdd44cSmrg            case $prefix in
42595fdd44cSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42695fdd44cSmrg              *) qprefix=$prefix;;
42795fdd44cSmrg            esac
42895fdd44cSmrg            prefixes="$prefixes '$qprefix'"
42995fdd44cSmrg          fi
43095fdd44cSmrg        fi
43195fdd44cSmrg        prefix=$prefix/
43264ce7165Smrg      done
43364ce7165Smrg
43464ce7165Smrg      if test -n "$prefixes"; then
43595fdd44cSmrg        # Don't fail if two instances are running concurrently.
43695fdd44cSmrg        (umask $mkdir_umask &&
43795fdd44cSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43895fdd44cSmrg          test -d "$dstdir" || exit 1
43995fdd44cSmrg        obsolete_mkdir_used=true
44064ce7165Smrg      fi
44164ce7165Smrg    fi
44264ce7165Smrg  fi
44364ce7165Smrg
44464ce7165Smrg  if test -n "$dir_arg"; then
44564ce7165Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44664ce7165Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44764ce7165Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44864ce7165Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44964ce7165Smrg  else
45064ce7165Smrg
45164ce7165Smrg    # Make a couple of temp file names in the proper directory.
452b71ad168Smrg    dsttmp=${dstdirslash}_inst.$$_
453b71ad168Smrg    rmtmp=${dstdirslash}_rm.$$_
45464ce7165Smrg
45564ce7165Smrg    # Trap to clean up those temp files at exit.
45664ce7165Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45764ce7165Smrg
45864ce7165Smrg    # Copy the file name to the temp name.
459b71ad168Smrg    (umask $cp_umask &&
460b71ad168Smrg     { test -z "$stripcmd" || {
461b71ad168Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462b71ad168Smrg	 # which would cause strip to fail.
463b71ad168Smrg	 if test -z "$doit"; then
464b71ad168Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465b71ad168Smrg	 else
466b71ad168Smrg	   $doit touch "$dsttmp"
467b71ad168Smrg	 fi
468b71ad168Smrg       }
469b71ad168Smrg     } &&
470b71ad168Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47164ce7165Smrg
47264ce7165Smrg    # and set any options; do chmod last to preserve setuid bits.
47364ce7165Smrg    #
47464ce7165Smrg    # If any of these fail, we abort the whole thing.  If we want to
47564ce7165Smrg    # ignore errors from any of these, just make sure not to ignore
47664ce7165Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47764ce7165Smrg    #
478fb23d3a8Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479fb23d3a8Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480fb23d3a8Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481fb23d3a8Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482fb23d3a8Smrg
483fb23d3a8Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484fb23d3a8Smrg    if $copy_on_change &&
48595fdd44cSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48695fdd44cSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48795fdd44cSmrg       set -f &&
488fb23d3a8Smrg       set X $old && old=:$2:$4:$5:$6 &&
489fb23d3a8Smrg       set X $new && new=:$2:$4:$5:$6 &&
49095fdd44cSmrg       set +f &&
491fb23d3a8Smrg       test "$old" = "$new" &&
492fb23d3a8Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493fb23d3a8Smrg    then
494fb23d3a8Smrg      rm -f "$dsttmp"
495fb23d3a8Smrg    else
496b71ad168Smrg      # If $backupsuffix is set, and the file being installed
497b71ad168Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498b71ad168Smrg      # e.g., if mv doesn't support -f.
499b71ad168Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500b71ad168Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501b71ad168Smrg      fi
502b71ad168Smrg
503fb23d3a8Smrg      # Rename the file to the real destination.
504fb23d3a8Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505fb23d3a8Smrg
506fb23d3a8Smrg      # The rename failed, perhaps because mv can't rename something else
507fb23d3a8Smrg      # to itself, or perhaps because mv is so ancient that it does not
508fb23d3a8Smrg      # support -f.
509fb23d3a8Smrg      {
51095fdd44cSmrg        # Now remove or move aside any old file at destination location.
51195fdd44cSmrg        # We try this two ways since rm can't unlink itself on some
51295fdd44cSmrg        # systems and the destination file might be busy for other
51395fdd44cSmrg        # reasons.  In this case, the final cleanup might fail but the new
51495fdd44cSmrg        # file should still install successfully.
51595fdd44cSmrg        {
51695fdd44cSmrg          test ! -f "$dst" ||
517b71ad168Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
51895fdd44cSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519b71ad168Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52095fdd44cSmrg          } ||
52195fdd44cSmrg          { echo "$0: cannot unlink or rename $dst" >&2
52295fdd44cSmrg            (exit 1); exit 1
52395fdd44cSmrg          }
52495fdd44cSmrg        } &&
52595fdd44cSmrg
52695fdd44cSmrg        # Now rename the file to the real destination.
52795fdd44cSmrg        $doit $mvcmd "$dsttmp" "$dst"
528fb23d3a8Smrg      }
529fb23d3a8Smrg    fi || exit 1
53064ce7165Smrg
53164ce7165Smrg    trap '' 0
53264ce7165Smrg  fi
53364ce7165Smrgdone
53464ce7165Smrg
53564ce7165Smrg# Local variables:
536b71ad168Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
53764ce7165Smrg# time-stamp-start: "scriptversion="
53864ce7165Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539b71ad168Smrg# time-stamp-time-zone: "UTC0"
540fb23d3a8Smrg# time-stamp-end: "; # UTC"
54164ce7165Smrg# End:
542