install-sh revision bc77032b
166fe65f6Smrg#!/bin/sh
266fe65f6Smrg# install - install a program, script, or datafile
366fe65f6Smrg
4bc77032bSmrgscriptversion=2020-11-14.01; # UTC
566fe65f6Smrg
666fe65f6Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
766fe65f6Smrg# later released in X11R6 (xc/config/util/install.sh) with the
866fe65f6Smrg# following copyright and license.
966fe65f6Smrg#
1066fe65f6Smrg# Copyright (C) 1994 X Consortium
1166fe65f6Smrg#
1266fe65f6Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1366fe65f6Smrg# of this software and associated documentation files (the "Software"), to
1466fe65f6Smrg# deal in the Software without restriction, including without limitation the
1566fe65f6Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1666fe65f6Smrg# sell copies of the Software, and to permit persons to whom the Software is
1766fe65f6Smrg# furnished to do so, subject to the following conditions:
1866fe65f6Smrg#
1966fe65f6Smrg# The above copyright notice and this permission notice shall be included in
2066fe65f6Smrg# all copies or substantial portions of the Software.
2166fe65f6Smrg#
2266fe65f6Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2366fe65f6Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2466fe65f6Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2566fe65f6Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2666fe65f6Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2766fe65f6Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2866fe65f6Smrg#
2966fe65f6Smrg# Except as contained in this notice, the name of the X Consortium shall not
3066fe65f6Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3166fe65f6Smrg# ings in this Software without prior written authorization from the X Consor-
3266fe65f6Smrg# tium.
3366fe65f6Smrg#
3466fe65f6Smrg#
3566fe65f6Smrg# FSF changes to this file are in the public domain.
3666fe65f6Smrg#
3766fe65f6Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38c6a6acfbSmrg# 'make' implicit rules from creating a file called install from it
3966fe65f6Smrg# when there is no Makefile.
4066fe65f6Smrg#
4166fe65f6Smrg# This script is compatible with the BSD install script, but was written
4289d09728Smrg# from scratch.
4389d09728Smrg
44c6a6acfbSmrgtab='	'
4589d09728Smrgnl='
4689d09728Smrg'
47c6a6acfbSmrgIFS=" $tab$nl"
4866fe65f6Smrg
49c6a6acfbSmrg# Set DOITPROG to "echo" to test this script.
5066fe65f6Smrg
5189d09728Smrgdoit=${DOITPROG-}
52c6a6acfbSmrgdoit_exec=${doit:-exec}
5366fe65f6Smrg
5489d09728Smrg# Put in absolute file names if you don't have them in your path;
5589d09728Smrg# or use environment vars.
5689d09728Smrg
5789d09728Smrgchgrpprog=${CHGRPPROG-chgrp}
5889d09728Smrgchmodprog=${CHMODPROG-chmod}
5989d09728Smrgchownprog=${CHOWNPROG-chown}
6089d09728Smrgcmpprog=${CMPPROG-cmp}
6189d09728Smrgcpprog=${CPPROG-cp}
6289d09728Smrgmkdirprog=${MKDIRPROG-mkdir}
6389d09728Smrgmvprog=${MVPROG-mv}
6489d09728Smrgrmprog=${RMPROG-rm}
6589d09728Smrgstripprog=${STRIPPROG-strip}
6689d09728Smrg
6789d09728Smrgposix_mkdir=
6889d09728Smrg
6989d09728Smrg# Desired mode of installed file.
7089d09728Smrgmode=0755
7166fe65f6Smrg
72bc77032bSmrg# Create dirs (including intermediate dirs) using mode 755.
73bc77032bSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74bc77032bSmrgmkdir_umask=22
75bc77032bSmrg
76bc77032bSmrgbackupsuffix=
7766fe65f6Smrgchgrpcmd=
7889d09728Smrgchmodcmd=$chmodprog
7989d09728Smrgchowncmd=
8089d09728Smrgmvcmd=$mvprog
8166fe65f6Smrgrmcmd="$rmprog -f"
8289d09728Smrgstripcmd=
8389d09728Smrg
8466fe65f6Smrgsrc=
8566fe65f6Smrgdst=
8666fe65f6Smrgdir_arg=
8789d09728Smrgdst_arg=
8889d09728Smrg
8989d09728Smrgcopy_on_change=false
90c6a6acfbSmrgis_target_a_directory=possibly
9166fe65f6Smrg
9289d09728Smrgusage="\
9389d09728SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9466fe65f6Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9566fe65f6Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9666fe65f6Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9766fe65f6Smrg
9866fe65f6SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9966fe65f6SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10066fe65f6SmrgIn the 4th, create DIRECTORIES.
10166fe65f6Smrg
10266fe65f6SmrgOptions:
10389d09728Smrg     --help     display this help and exit.
10489d09728Smrg     --version  display version info and exit.
10589d09728Smrg
10689d09728Smrg  -c            (ignored)
107bc77032bSmrg  -C            install only if different (preserve data modification time)
10889d09728Smrg  -d            create directories instead of installing files.
10989d09728Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11089d09728Smrg  -m MODE       $chmodprog installed files to MODE.
11189d09728Smrg  -o USER       $chownprog installed files to USER.
112bc77032bSmrg  -p            pass -p to $cpprog.
11389d09728Smrg  -s            $stripprog installed files.
114bc77032bSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11589d09728Smrg  -t DIRECTORY  install into DIRECTORY.
11689d09728Smrg  -T            report an error if DSTFILE is a directory.
11766fe65f6Smrg
11866fe65f6SmrgEnvironment variables override the default commands:
11989d09728Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12089d09728Smrg  RMPROG STRIPPROG
121bc77032bSmrg
122bc77032bSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123bc77032bSmrgit's up to you to specify -f if you want it.
124bc77032bSmrg
125bc77032bSmrgIf -S is not specified, no backups are attempted.
126bc77032bSmrg
127bc77032bSmrgEmail bug reports to bug-automake@gnu.org.
128bc77032bSmrgAutomake home page: https://www.gnu.org/software/automake/
12966fe65f6Smrg"
13066fe65f6Smrg
13189d09728Smrgwhile test $# -ne 0; do
13266fe65f6Smrg  case $1 in
13389d09728Smrg    -c) ;;
13489d09728Smrg
13589d09728Smrg    -C) copy_on_change=true;;
13666fe65f6Smrg
13789d09728Smrg    -d) dir_arg=true;;
13866fe65f6Smrg
13966fe65f6Smrg    -g) chgrpcmd="$chgrpprog $2"
140c6a6acfbSmrg        shift;;
14166fe65f6Smrg
14266fe65f6Smrg    --help) echo "$usage"; exit $?;;
14366fe65f6Smrg
14489d09728Smrg    -m) mode=$2
145c6a6acfbSmrg        case $mode in
146c6a6acfbSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147c6a6acfbSmrg            echo "$0: invalid mode: $mode" >&2
148c6a6acfbSmrg            exit 1;;
149c6a6acfbSmrg        esac
150c6a6acfbSmrg        shift;;
15166fe65f6Smrg
15266fe65f6Smrg    -o) chowncmd="$chownprog $2"
153c6a6acfbSmrg        shift;;
15466fe65f6Smrg
155bc77032bSmrg    -p) cpprog="$cpprog -p";;
156bc77032bSmrg
15789d09728Smrg    -s) stripcmd=$stripprog;;
15866fe65f6Smrg
159bc77032bSmrg    -S) backupsuffix="$2"
160bc77032bSmrg        shift;;
161bc77032bSmrg
162c6a6acfbSmrg    -t)
163c6a6acfbSmrg        is_target_a_directory=always
164c6a6acfbSmrg        dst_arg=$2
165c6a6acfbSmrg        # Protect names problematic for 'test' and other utilities.
166c6a6acfbSmrg        case $dst_arg in
167c6a6acfbSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168c6a6acfbSmrg        esac
169c6a6acfbSmrg        shift;;
17066fe65f6Smrg
171c6a6acfbSmrg    -T) is_target_a_directory=never;;
17266fe65f6Smrg
17366fe65f6Smrg    --version) echo "$0 $scriptversion"; exit $?;;
17466fe65f6Smrg
175c6a6acfbSmrg    --) shift
176c6a6acfbSmrg        break;;
17789d09728Smrg
178c6a6acfbSmrg    -*) echo "$0: invalid option: $1" >&2
179c6a6acfbSmrg        exit 1;;
18089d09728Smrg
18189d09728Smrg    *)  break;;
18266fe65f6Smrg  esac
18389d09728Smrg  shift
18466fe65f6Smrgdone
18566fe65f6Smrg
186c6a6acfbSmrg# We allow the use of options -d and -T together, by making -d
187c6a6acfbSmrg# take the precedence; this is for compatibility with GNU install.
188c6a6acfbSmrg
189c6a6acfbSmrgif test -n "$dir_arg"; then
190c6a6acfbSmrg  if test -n "$dst_arg"; then
191c6a6acfbSmrg    echo "$0: target directory not allowed when installing a directory." >&2
192c6a6acfbSmrg    exit 1
193c6a6acfbSmrg  fi
194c6a6acfbSmrgfi
195c6a6acfbSmrg
19689d09728Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19789d09728Smrg  # When -d is used, all remaining arguments are directories to create.
19889d09728Smrg  # When -t is used, the destination is already specified.
19989d09728Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20089d09728Smrg  for arg
20189d09728Smrg  do
20289d09728Smrg    if test -n "$dst_arg"; then
20389d09728Smrg      # $@ is not empty: it contains at least $arg.
20489d09728Smrg      set fnord "$@" "$dst_arg"
20589d09728Smrg      shift # fnord
20689d09728Smrg    fi
20789d09728Smrg    shift # arg
20889d09728Smrg    dst_arg=$arg
209c6a6acfbSmrg    # Protect names problematic for 'test' and other utilities.
210c6a6acfbSmrg    case $dst_arg in
211c6a6acfbSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212c6a6acfbSmrg    esac
21389d09728Smrg  done
21489d09728Smrgfi
21589d09728Smrg
21689d09728Smrgif test $# -eq 0; then
21766fe65f6Smrg  if test -z "$dir_arg"; then
21866fe65f6Smrg    echo "$0: no input file specified." >&2
21966fe65f6Smrg    exit 1
22066fe65f6Smrg  fi
221c6a6acfbSmrg  # It's OK to call 'install-sh -d' without argument.
22266fe65f6Smrg  # This can happen when creating conditional directories.
22366fe65f6Smrg  exit 0
22466fe65f6Smrgfi
22566fe65f6Smrg
22689d09728Smrgif test -z "$dir_arg"; then
227c6a6acfbSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228c6a6acfbSmrg    if test ! -d "$dst_arg"; then
229c6a6acfbSmrg      echo "$0: $dst_arg: Is not a directory." >&2
230c6a6acfbSmrg      exit 1
231c6a6acfbSmrg    fi
232c6a6acfbSmrg  fi
233c6a6acfbSmrgfi
234c6a6acfbSmrg
235c6a6acfbSmrgif test -z "$dir_arg"; then
236c6a6acfbSmrg  do_exit='(exit $ret); exit $ret'
237c6a6acfbSmrg  trap "ret=129; $do_exit" 1
238c6a6acfbSmrg  trap "ret=130; $do_exit" 2
239c6a6acfbSmrg  trap "ret=141; $do_exit" 13
240c6a6acfbSmrg  trap "ret=143; $do_exit" 15
24189d09728Smrg
24289d09728Smrg  # Set umask so as not to create temps with too-generous modes.
24389d09728Smrg  # However, 'strip' requires both read and write access to temps.
24489d09728Smrg  case $mode in
24589d09728Smrg    # Optimize common cases.
24689d09728Smrg    *644) cp_umask=133;;
24789d09728Smrg    *755) cp_umask=22;;
24889d09728Smrg
24989d09728Smrg    *[0-7])
25089d09728Smrg      if test -z "$stripcmd"; then
251c6a6acfbSmrg        u_plus_rw=
25289d09728Smrg      else
253c6a6acfbSmrg        u_plus_rw='% 200'
25489d09728Smrg      fi
25589d09728Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25689d09728Smrg    *)
25789d09728Smrg      if test -z "$stripcmd"; then
258c6a6acfbSmrg        u_plus_rw=
25989d09728Smrg      else
260c6a6acfbSmrg        u_plus_rw=,u+rw
26189d09728Smrg      fi
26289d09728Smrg      cp_umask=$mode$u_plus_rw;;
26389d09728Smrg  esac
26489d09728Smrgfi
26589d09728Smrg
26666fe65f6Smrgfor src
26766fe65f6Smrgdo
268c6a6acfbSmrg  # Protect names problematic for 'test' and other utilities.
26966fe65f6Smrg  case $src in
270c6a6acfbSmrg    -* | [=\(\)!]) src=./$src;;
27166fe65f6Smrg  esac
27266fe65f6Smrg
27366fe65f6Smrg  if test -n "$dir_arg"; then
27466fe65f6Smrg    dst=$src
27589d09728Smrg    dstdir=$dst
27689d09728Smrg    test -d "$dstdir"
27789d09728Smrg    dstdir_status=$?
278bc77032bSmrg    # Don't chown directories that already exist.
279bc77032bSmrg    if test $dstdir_status = 0; then
280bc77032bSmrg      chowncmd=""
281bc77032bSmrg    fi
28266fe65f6Smrg  else
28389d09728Smrg
28466fe65f6Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28566fe65f6Smrg    # might cause directories to be created, which would be especially bad
28666fe65f6Smrg    # if $src (and thus $dsttmp) contains '*'.
28766fe65f6Smrg    if test ! -f "$src" && test ! -d "$src"; then
28866fe65f6Smrg      echo "$0: $src does not exist." >&2
28966fe65f6Smrg      exit 1
29066fe65f6Smrg    fi
29166fe65f6Smrg
29289d09728Smrg    if test -z "$dst_arg"; then
29366fe65f6Smrg      echo "$0: no destination specified." >&2
29466fe65f6Smrg      exit 1
29566fe65f6Smrg    fi
29689d09728Smrg    dst=$dst_arg
29766fe65f6Smrg
298bc77032bSmrg    # If destination is a directory, append the input filename.
29966fe65f6Smrg    if test -d "$dst"; then
300c6a6acfbSmrg      if test "$is_target_a_directory" = never; then
301c6a6acfbSmrg        echo "$0: $dst_arg: Is a directory" >&2
302c6a6acfbSmrg        exit 1
30366fe65f6Smrg      fi
30489d09728Smrg      dstdir=$dst
305bc77032bSmrg      dstbase=`basename "$src"`
306bc77032bSmrg      case $dst in
307bc77032bSmrg	*/) dst=$dst$dstbase;;
308bc77032bSmrg	*)  dst=$dst/$dstbase;;
309bc77032bSmrg      esac
31089d09728Smrg      dstdir_status=0
31189d09728Smrg    else
312c6a6acfbSmrg      dstdir=`dirname "$dst"`
31389d09728Smrg      test -d "$dstdir"
31489d09728Smrg      dstdir_status=$?
31566fe65f6Smrg    fi
31666fe65f6Smrg  fi
31766fe65f6Smrg
318bc77032bSmrg  case $dstdir in
319bc77032bSmrg    */) dstdirslash=$dstdir;;
320bc77032bSmrg    *)  dstdirslash=$dstdir/;;
321bc77032bSmrg  esac
322bc77032bSmrg
32389d09728Smrg  obsolete_mkdir_used=false
32489d09728Smrg
32589d09728Smrg  if test $dstdir_status != 0; then
32689d09728Smrg    case $posix_mkdir in
32789d09728Smrg      '')
328c6a6acfbSmrg        # With -d, create the new directory with the user-specified mode.
329c6a6acfbSmrg        # Otherwise, rely on $mkdir_umask.
330c6a6acfbSmrg        if test -n "$dir_arg"; then
331c6a6acfbSmrg          mkdir_mode=-m$mode
332c6a6acfbSmrg        else
333c6a6acfbSmrg          mkdir_mode=
334c6a6acfbSmrg        fi
335c6a6acfbSmrg
336c6a6acfbSmrg        posix_mkdir=false
337bc77032bSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338bc77032bSmrg	# here however when possible just to lower collision chance.
339bc77032bSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340bc77032bSmrg
341bc77032bSmrg	trap '
342bc77032bSmrg	  ret=$?
343bc77032bSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344bc77032bSmrg	  exit $ret
345bc77032bSmrg	' 0
346bc77032bSmrg
347bc77032bSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
348bc77032bSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349bc77032bSmrg	# directory is successfully created first before we actually test
350bc77032bSmrg	# 'mkdir -p'.
351bc77032bSmrg	if (umask $mkdir_umask &&
352bc77032bSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353bc77032bSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354bc77032bSmrg	then
355bc77032bSmrg	  if test -z "$dir_arg" || {
356bc77032bSmrg	       # Check for POSIX incompatibilities with -m.
357bc77032bSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358bc77032bSmrg	       # other-writable bit of parent directory when it shouldn't.
359bc77032bSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360bc77032bSmrg	       test_tmpdir="$tmpdir/a"
361bc77032bSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362bc77032bSmrg	       case $ls_ld_tmpdir in
363bc77032bSmrg		 d????-?r-*) different_mode=700;;
364bc77032bSmrg		 d????-?--*) different_mode=755;;
365bc77032bSmrg		 *) false;;
366bc77032bSmrg	       esac &&
367bc77032bSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368bc77032bSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369bc77032bSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370bc77032bSmrg	       }
371bc77032bSmrg	     }
372bc77032bSmrg	  then posix_mkdir=:
373bc77032bSmrg	  fi
374bc77032bSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375bc77032bSmrg	else
376bc77032bSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377bc77032bSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378bc77032bSmrg	fi
379bc77032bSmrg	trap '' 0;;
38089d09728Smrg    esac
38166fe65f6Smrg
38289d09728Smrg    if
38389d09728Smrg      $posix_mkdir && (
384c6a6acfbSmrg        umask $mkdir_umask &&
385c6a6acfbSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38689d09728Smrg      )
38789d09728Smrg    then :
38889d09728Smrg    else
38966fe65f6Smrg
390bc77032bSmrg      # mkdir does not conform to POSIX,
39189d09728Smrg      # or it failed possibly due to a race condition.  Create the
39289d09728Smrg      # directory the slow way, step by step, checking for races as we go.
39366fe65f6Smrg
39489d09728Smrg      case $dstdir in
395c6a6acfbSmrg        /*) prefix='/';;
396c6a6acfbSmrg        [-=\(\)!]*) prefix='./';;
397c6a6acfbSmrg        *)  prefix='';;
39889d09728Smrg      esac
39966fe65f6Smrg
40089d09728Smrg      oIFS=$IFS
40189d09728Smrg      IFS=/
402c6a6acfbSmrg      set -f
40389d09728Smrg      set fnord $dstdir
40466fe65f6Smrg      shift
405c6a6acfbSmrg      set +f
40689d09728Smrg      IFS=$oIFS
40789d09728Smrg
40889d09728Smrg      prefixes=
40989d09728Smrg
41089d09728Smrg      for d
41189d09728Smrg      do
412c6a6acfbSmrg        test X"$d" = X && continue
413c6a6acfbSmrg
414c6a6acfbSmrg        prefix=$prefix$d
415c6a6acfbSmrg        if test -d "$prefix"; then
416c6a6acfbSmrg          prefixes=
417c6a6acfbSmrg        else
418c6a6acfbSmrg          if $posix_mkdir; then
419bc77032bSmrg            (umask $mkdir_umask &&
420c6a6acfbSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421c6a6acfbSmrg            # Don't fail if two instances are running concurrently.
422c6a6acfbSmrg            test -d "$prefix" || exit 1
423c6a6acfbSmrg          else
424c6a6acfbSmrg            case $prefix in
425c6a6acfbSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426c6a6acfbSmrg              *) qprefix=$prefix;;
427c6a6acfbSmrg            esac
428c6a6acfbSmrg            prefixes="$prefixes '$qprefix'"
429c6a6acfbSmrg          fi
430c6a6acfbSmrg        fi
431c6a6acfbSmrg        prefix=$prefix/
43289d09728Smrg      done
43389d09728Smrg
43489d09728Smrg      if test -n "$prefixes"; then
435c6a6acfbSmrg        # Don't fail if two instances are running concurrently.
436c6a6acfbSmrg        (umask $mkdir_umask &&
437c6a6acfbSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438c6a6acfbSmrg          test -d "$dstdir" || exit 1
439c6a6acfbSmrg        obsolete_mkdir_used=true
44066fe65f6Smrg      fi
44189d09728Smrg    fi
44266fe65f6Smrg  fi
44366fe65f6Smrg
44466fe65f6Smrg  if test -n "$dir_arg"; then
44589d09728Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44689d09728Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44789d09728Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44889d09728Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44966fe65f6Smrg  else
45066fe65f6Smrg
45166fe65f6Smrg    # Make a couple of temp file names in the proper directory.
452bc77032bSmrg    dsttmp=${dstdirslash}_inst.$$_
453bc77032bSmrg    rmtmp=${dstdirslash}_rm.$$_
45466fe65f6Smrg
45566fe65f6Smrg    # Trap to clean up those temp files at exit.
45666fe65f6Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45766fe65f6Smrg
45866fe65f6Smrg    # Copy the file name to the temp name.
459bc77032bSmrg    (umask $cp_umask &&
460bc77032bSmrg     { test -z "$stripcmd" || {
461bc77032bSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462bc77032bSmrg	 # which would cause strip to fail.
463bc77032bSmrg	 if test -z "$doit"; then
464bc77032bSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465bc77032bSmrg	 else
466bc77032bSmrg	   $doit touch "$dsttmp"
467bc77032bSmrg	 fi
468bc77032bSmrg       }
469bc77032bSmrg     } &&
470bc77032bSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47166fe65f6Smrg
47266fe65f6Smrg    # and set any options; do chmod last to preserve setuid bits.
47366fe65f6Smrg    #
47466fe65f6Smrg    # If any of these fail, we abort the whole thing.  If we want to
47566fe65f6Smrg    # ignore errors from any of these, just make sure not to ignore
47666fe65f6Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47766fe65f6Smrg    #
47889d09728Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47989d09728Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48089d09728Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48189d09728Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48289d09728Smrg
48389d09728Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48489d09728Smrg    if $copy_on_change &&
485c6a6acfbSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486c6a6acfbSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487c6a6acfbSmrg       set -f &&
48889d09728Smrg       set X $old && old=:$2:$4:$5:$6 &&
48989d09728Smrg       set X $new && new=:$2:$4:$5:$6 &&
490c6a6acfbSmrg       set +f &&
49189d09728Smrg       test "$old" = "$new" &&
49289d09728Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49389d09728Smrg    then
49489d09728Smrg      rm -f "$dsttmp"
49589d09728Smrg    else
496bc77032bSmrg      # If $backupsuffix is set, and the file being installed
497bc77032bSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498bc77032bSmrg      # e.g., if mv doesn't support -f.
499bc77032bSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500bc77032bSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501bc77032bSmrg      fi
502bc77032bSmrg
50389d09728Smrg      # Rename the file to the real destination.
50489d09728Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50589d09728Smrg
50689d09728Smrg      # The rename failed, perhaps because mv can't rename something else
50789d09728Smrg      # to itself, or perhaps because mv is so ancient that it does not
50889d09728Smrg      # support -f.
50989d09728Smrg      {
510c6a6acfbSmrg        # Now remove or move aside any old file at destination location.
511c6a6acfbSmrg        # We try this two ways since rm can't unlink itself on some
512c6a6acfbSmrg        # systems and the destination file might be busy for other
513c6a6acfbSmrg        # reasons.  In this case, the final cleanup might fail but the new
514c6a6acfbSmrg        # file should still install successfully.
515c6a6acfbSmrg        {
516c6a6acfbSmrg          test ! -f "$dst" ||
517bc77032bSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
518c6a6acfbSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519bc77032bSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520c6a6acfbSmrg          } ||
521c6a6acfbSmrg          { echo "$0: cannot unlink or rename $dst" >&2
522c6a6acfbSmrg            (exit 1); exit 1
523c6a6acfbSmrg          }
524c6a6acfbSmrg        } &&
525c6a6acfbSmrg
526c6a6acfbSmrg        # Now rename the file to the real destination.
527c6a6acfbSmrg        $doit $mvcmd "$dsttmp" "$dst"
52889d09728Smrg      }
52989d09728Smrg    fi || exit 1
53089d09728Smrg
53189d09728Smrg    trap '' 0
53289d09728Smrg  fi
53366fe65f6Smrgdone
53466fe65f6Smrg
53566fe65f6Smrg# Local variables:
536bc77032bSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
53766fe65f6Smrg# time-stamp-start: "scriptversion="
53866fe65f6Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539bc77032bSmrg# time-stamp-time-zone: "UTC0"
540a9274bcdSmrg# time-stamp-end: "; # UTC"
54166fe65f6Smrg# End:
542