143f32c10Smrg#!/bin/sh
243f32c10Smrg# install - install a program, script, or datafile
343f32c10Smrg
4d3173433Smrgscriptversion=2024-06-19.01; # UTC
543f32c10Smrg
643f32c10Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
743f32c10Smrg# later released in X11R6 (xc/config/util/install.sh) with the
843f32c10Smrg# following copyright and license.
943f32c10Smrg#
1043f32c10Smrg# Copyright (C) 1994 X Consortium
1143f32c10Smrg#
1243f32c10Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1343f32c10Smrg# of this software and associated documentation files (the "Software"), to
1443f32c10Smrg# deal in the Software without restriction, including without limitation the
1543f32c10Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1643f32c10Smrg# sell copies of the Software, and to permit persons to whom the Software is
1743f32c10Smrg# furnished to do so, subject to the following conditions:
1843f32c10Smrg#
1943f32c10Smrg# The above copyright notice and this permission notice shall be included in
2043f32c10Smrg# all copies or substantial portions of the Software.
2143f32c10Smrg#
2243f32c10Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2343f32c10Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2443f32c10Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2543f32c10Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2643f32c10Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2743f32c10Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2843f32c10Smrg#
2943f32c10Smrg# Except as contained in this notice, the name of the X Consortium shall not
3043f32c10Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3143f32c10Smrg# ings in this Software without prior written authorization from the X Consor-
3243f32c10Smrg# tium.
3343f32c10Smrg#
3443f32c10Smrg#
3543f32c10Smrg# FSF changes to this file are in the public domain.
3643f32c10Smrg#
3743f32c10Smrg# Calling this script install-sh is preferred over install.sh, to prevent
386ef05171Smrg# 'make' implicit rules from creating a file called install from it
3943f32c10Smrg# when there is no Makefile.
4043f32c10Smrg#
4143f32c10Smrg# This script is compatible with the BSD install script, but was written
4243f32c10Smrg# from scratch.
4343f32c10Smrg
446ef05171Smrgtab='	'
4543f32c10Smrgnl='
4643f32c10Smrg'
476ef05171SmrgIFS=" $tab$nl"
4843f32c10Smrg
496ef05171Smrg# Set DOITPROG to "echo" to test this script.
5043f32c10Smrg
5143f32c10Smrgdoit=${DOITPROG-}
526ef05171Smrgdoit_exec=${doit:-exec}
5343f32c10Smrg
5443f32c10Smrg# Put in absolute file names if you don't have them in your path;
5543f32c10Smrg# or use environment vars.
5643f32c10Smrg
5743f32c10Smrgchgrpprog=${CHGRPPROG-chgrp}
5843f32c10Smrgchmodprog=${CHMODPROG-chmod}
5943f32c10Smrgchownprog=${CHOWNPROG-chown}
6043f32c10Smrgcmpprog=${CMPPROG-cmp}
6143f32c10Smrgcpprog=${CPPROG-cp}
6243f32c10Smrgmkdirprog=${MKDIRPROG-mkdir}
6343f32c10Smrgmvprog=${MVPROG-mv}
6443f32c10Smrgrmprog=${RMPROG-rm}
6543f32c10Smrgstripprog=${STRIPPROG-strip}
6643f32c10Smrg
6743f32c10Smrgposix_mkdir=
6843f32c10Smrg
6943f32c10Smrg# Desired mode of installed file.
7043f32c10Smrgmode=0755
7143f32c10Smrg
72fbfaf8f3Smrg# Create dirs (including intermediate dirs) using mode 755.
73fbfaf8f3Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74fbfaf8f3Smrgmkdir_umask=22
75fbfaf8f3Smrg
76fbfaf8f3Smrgbackupsuffix=
7743f32c10Smrgchgrpcmd=
7843f32c10Smrgchmodcmd=$chmodprog
7943f32c10Smrgchowncmd=
8043f32c10Smrgmvcmd=$mvprog
8143f32c10Smrgrmcmd="$rmprog -f"
8243f32c10Smrgstripcmd=
8343f32c10Smrg
8443f32c10Smrgsrc=
8543f32c10Smrgdst=
8643f32c10Smrgdir_arg=
8743f32c10Smrgdst_arg=
8843f32c10Smrg
8943f32c10Smrgcopy_on_change=false
906ef05171Smrgis_target_a_directory=possibly
9143f32c10Smrg
9243f32c10Smrgusage="\
9343f32c10SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9443f32c10Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9543f32c10Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9643f32c10Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9743f32c10Smrg
9843f32c10SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9943f32c10SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10043f32c10SmrgIn the 4th, create DIRECTORIES.
10143f32c10Smrg
10243f32c10SmrgOptions:
10343f32c10Smrg     --help     display this help and exit.
10443f32c10Smrg     --version  display version info and exit.
10543f32c10Smrg
10643f32c10Smrg  -c            (ignored)
107fbfaf8f3Smrg  -C            install only if different (preserve data modification time)
10843f32c10Smrg  -d            create directories instead of installing files.
10943f32c10Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11043f32c10Smrg  -m MODE       $chmodprog installed files to MODE.
11143f32c10Smrg  -o USER       $chownprog installed files to USER.
112fbfaf8f3Smrg  -p            pass -p to $cpprog.
11343f32c10Smrg  -s            $stripprog installed files.
114fbfaf8f3Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11543f32c10Smrg  -t DIRECTORY  install into DIRECTORY.
11643f32c10Smrg  -T            report an error if DSTFILE is a directory.
11743f32c10Smrg
11843f32c10SmrgEnvironment variables override the default commands:
11943f32c10Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12043f32c10Smrg  RMPROG STRIPPROG
121fbfaf8f3Smrg
122fbfaf8f3SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123fbfaf8f3Smrgit's up to you to specify -f if you want it.
124fbfaf8f3Smrg
125fbfaf8f3SmrgIf -S is not specified, no backups are attempted.
126fbfaf8f3Smrg
127d3173433SmrgReport bugs to <bug-automake@gnu.org>.
128d3173433SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
129d3173433SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>."
13043f32c10Smrg
13143f32c10Smrgwhile test $# -ne 0; do
13243f32c10Smrg  case $1 in
13343f32c10Smrg    -c) ;;
13443f32c10Smrg
13543f32c10Smrg    -C) copy_on_change=true;;
13643f32c10Smrg
13743f32c10Smrg    -d) dir_arg=true;;
13843f32c10Smrg
13943f32c10Smrg    -g) chgrpcmd="$chgrpprog $2"
1406ef05171Smrg        shift;;
14143f32c10Smrg
14243f32c10Smrg    --help) echo "$usage"; exit $?;;
14343f32c10Smrg
14443f32c10Smrg    -m) mode=$2
1456ef05171Smrg        case $mode in
1466ef05171Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1476ef05171Smrg            echo "$0: invalid mode: $mode" >&2
1486ef05171Smrg            exit 1;;
1496ef05171Smrg        esac
1506ef05171Smrg        shift;;
15143f32c10Smrg
15243f32c10Smrg    -o) chowncmd="$chownprog $2"
1536ef05171Smrg        shift;;
15443f32c10Smrg
155fbfaf8f3Smrg    -p) cpprog="$cpprog -p";;
156fbfaf8f3Smrg
15743f32c10Smrg    -s) stripcmd=$stripprog;;
15843f32c10Smrg
159fbfaf8f3Smrg    -S) backupsuffix="$2"
160fbfaf8f3Smrg        shift;;
161fbfaf8f3Smrg
1626ef05171Smrg    -t)
1636ef05171Smrg        is_target_a_directory=always
1646ef05171Smrg        dst_arg=$2
1656ef05171Smrg        # Protect names problematic for 'test' and other utilities.
1666ef05171Smrg        case $dst_arg in
1676ef05171Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1686ef05171Smrg        esac
1696ef05171Smrg        shift;;
17043f32c10Smrg
1716ef05171Smrg    -T) is_target_a_directory=never;;
17243f32c10Smrg
173d3173433Smrg    --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;;
17443f32c10Smrg
1756ef05171Smrg    --) shift
1766ef05171Smrg        break;;
17743f32c10Smrg
1786ef05171Smrg    -*) echo "$0: invalid option: $1" >&2
1796ef05171Smrg        exit 1;;
18043f32c10Smrg
18143f32c10Smrg    *)  break;;
18243f32c10Smrg  esac
18343f32c10Smrg  shift
18443f32c10Smrgdone
18543f32c10Smrg
1866ef05171Smrg# We allow the use of options -d and -T together, by making -d
1876ef05171Smrg# take the precedence; this is for compatibility with GNU install.
1886ef05171Smrg
1896ef05171Smrgif test -n "$dir_arg"; then
1906ef05171Smrg  if test -n "$dst_arg"; then
1916ef05171Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1926ef05171Smrg    exit 1
1936ef05171Smrg  fi
1946ef05171Smrgfi
1956ef05171Smrg
19643f32c10Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19743f32c10Smrg  # When -d is used, all remaining arguments are directories to create.
19843f32c10Smrg  # When -t is used, the destination is already specified.
19943f32c10Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20043f32c10Smrg  for arg
20143f32c10Smrg  do
20243f32c10Smrg    if test -n "$dst_arg"; then
20343f32c10Smrg      # $@ is not empty: it contains at least $arg.
20443f32c10Smrg      set fnord "$@" "$dst_arg"
20543f32c10Smrg      shift # fnord
20643f32c10Smrg    fi
20743f32c10Smrg    shift # arg
20843f32c10Smrg    dst_arg=$arg
2096ef05171Smrg    # Protect names problematic for 'test' and other utilities.
2106ef05171Smrg    case $dst_arg in
2116ef05171Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2126ef05171Smrg    esac
21343f32c10Smrg  done
21443f32c10Smrgfi
21543f32c10Smrg
21643f32c10Smrgif test $# -eq 0; then
21743f32c10Smrg  if test -z "$dir_arg"; then
21843f32c10Smrg    echo "$0: no input file specified." >&2
21943f32c10Smrg    exit 1
22043f32c10Smrg  fi
2216ef05171Smrg  # It's OK to call 'install-sh -d' without argument.
22243f32c10Smrg  # This can happen when creating conditional directories.
22343f32c10Smrg  exit 0
22443f32c10Smrgfi
22543f32c10Smrg
22643f32c10Smrgif test -z "$dir_arg"; then
2276ef05171Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2286ef05171Smrg    if test ! -d "$dst_arg"; then
2296ef05171Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2306ef05171Smrg      exit 1
2316ef05171Smrg    fi
2326ef05171Smrg  fi
2336ef05171Smrgfi
2346ef05171Smrg
2356ef05171Smrgif test -z "$dir_arg"; then
2366ef05171Smrg  do_exit='(exit $ret); exit $ret'
2376ef05171Smrg  trap "ret=129; $do_exit" 1
2386ef05171Smrg  trap "ret=130; $do_exit" 2
2396ef05171Smrg  trap "ret=141; $do_exit" 13
2406ef05171Smrg  trap "ret=143; $do_exit" 15
24143f32c10Smrg
24243f32c10Smrg  # Set umask so as not to create temps with too-generous modes.
24343f32c10Smrg  # However, 'strip' requires both read and write access to temps.
24443f32c10Smrg  case $mode in
24543f32c10Smrg    # Optimize common cases.
24643f32c10Smrg    *644) cp_umask=133;;
24743f32c10Smrg    *755) cp_umask=22;;
24843f32c10Smrg
24943f32c10Smrg    *[0-7])
25043f32c10Smrg      if test -z "$stripcmd"; then
2516ef05171Smrg        u_plus_rw=
25243f32c10Smrg      else
2536ef05171Smrg        u_plus_rw='% 200'
25443f32c10Smrg      fi
25543f32c10Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25643f32c10Smrg    *)
25743f32c10Smrg      if test -z "$stripcmd"; then
2586ef05171Smrg        u_plus_rw=
25943f32c10Smrg      else
2606ef05171Smrg        u_plus_rw=,u+rw
26143f32c10Smrg      fi
26243f32c10Smrg      cp_umask=$mode$u_plus_rw;;
26343f32c10Smrg  esac
26443f32c10Smrgfi
26543f32c10Smrg
26643f32c10Smrgfor src
26743f32c10Smrgdo
2686ef05171Smrg  # Protect names problematic for 'test' and other utilities.
26943f32c10Smrg  case $src in
2706ef05171Smrg    -* | [=\(\)!]) src=./$src;;
27143f32c10Smrg  esac
27243f32c10Smrg
27343f32c10Smrg  if test -n "$dir_arg"; then
27443f32c10Smrg    dst=$src
27543f32c10Smrg    dstdir=$dst
27643f32c10Smrg    test -d "$dstdir"
27743f32c10Smrg    dstdir_status=$?
278fbfaf8f3Smrg    # Don't chown directories that already exist.
279fbfaf8f3Smrg    if test $dstdir_status = 0; then
280fbfaf8f3Smrg      chowncmd=""
281fbfaf8f3Smrg    fi
28243f32c10Smrg  else
28343f32c10Smrg
28443f32c10Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28543f32c10Smrg    # might cause directories to be created, which would be especially bad
28643f32c10Smrg    # if $src (and thus $dsttmp) contains '*'.
28743f32c10Smrg    if test ! -f "$src" && test ! -d "$src"; then
28843f32c10Smrg      echo "$0: $src does not exist." >&2
28943f32c10Smrg      exit 1
29043f32c10Smrg    fi
29143f32c10Smrg
29243f32c10Smrg    if test -z "$dst_arg"; then
29343f32c10Smrg      echo "$0: no destination specified." >&2
29443f32c10Smrg      exit 1
29543f32c10Smrg    fi
29643f32c10Smrg    dst=$dst_arg
29743f32c10Smrg
298ea148d1dSmrg    # If destination is a directory, append the input filename.
29943f32c10Smrg    if test -d "$dst"; then
3006ef05171Smrg      if test "$is_target_a_directory" = never; then
3016ef05171Smrg        echo "$0: $dst_arg: Is a directory" >&2
3026ef05171Smrg        exit 1
30343f32c10Smrg      fi
30443f32c10Smrg      dstdir=$dst
305ea148d1dSmrg      dstbase=`basename "$src"`
306ea148d1dSmrg      case $dst in
307ea148d1dSmrg	*/) dst=$dst$dstbase;;
308ea148d1dSmrg	*)  dst=$dst/$dstbase;;
309ea148d1dSmrg      esac
31043f32c10Smrg      dstdir_status=0
31143f32c10Smrg    else
3126ef05171Smrg      dstdir=`dirname "$dst"`
31343f32c10Smrg      test -d "$dstdir"
31443f32c10Smrg      dstdir_status=$?
31543f32c10Smrg    fi
31643f32c10Smrg  fi
31743f32c10Smrg
318ea148d1dSmrg  case $dstdir in
319ea148d1dSmrg    */) dstdirslash=$dstdir;;
320ea148d1dSmrg    *)  dstdirslash=$dstdir/;;
321ea148d1dSmrg  esac
322ea148d1dSmrg
32343f32c10Smrg  obsolete_mkdir_used=false
32443f32c10Smrg
32543f32c10Smrg  if test $dstdir_status != 0; then
32643f32c10Smrg    case $posix_mkdir in
32743f32c10Smrg      '')
3286ef05171Smrg        # With -d, create the new directory with the user-specified mode.
3296ef05171Smrg        # Otherwise, rely on $mkdir_umask.
3306ef05171Smrg        if test -n "$dir_arg"; then
3316ef05171Smrg          mkdir_mode=-m$mode
3326ef05171Smrg        else
3336ef05171Smrg          mkdir_mode=
3346ef05171Smrg        fi
3356ef05171Smrg
3366ef05171Smrg        posix_mkdir=false
337fbfaf8f3Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338fbfaf8f3Smrg	# here however when possible just to lower collision chance.
339fbfaf8f3Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340fbfaf8f3Smrg
341fbfaf8f3Smrg	trap '
342fbfaf8f3Smrg	  ret=$?
343fbfaf8f3Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344fbfaf8f3Smrg	  exit $ret
345fbfaf8f3Smrg	' 0
346fbfaf8f3Smrg
347fbfaf8f3Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348d3173433Smrg	# directly in world-writable /tmp, make sure that the '$tmpdir'
349fbfaf8f3Smrg	# directory is successfully created first before we actually test
350fbfaf8f3Smrg	# 'mkdir -p'.
351fbfaf8f3Smrg	if (umask $mkdir_umask &&
352fbfaf8f3Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353fbfaf8f3Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354fbfaf8f3Smrg	then
355fbfaf8f3Smrg	  if test -z "$dir_arg" || {
356d3173433Smrg	       # Check for POSIX incompatibility with -m.
357fbfaf8f3Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358fbfaf8f3Smrg	       # other-writable bit of parent directory when it shouldn't.
359fbfaf8f3Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360fbfaf8f3Smrg	       test_tmpdir="$tmpdir/a"
361fbfaf8f3Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362fbfaf8f3Smrg	       case $ls_ld_tmpdir in
363fbfaf8f3Smrg		 d????-?r-*) different_mode=700;;
364fbfaf8f3Smrg		 d????-?--*) different_mode=755;;
365fbfaf8f3Smrg		 *) false;;
366fbfaf8f3Smrg	       esac &&
367fbfaf8f3Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368fbfaf8f3Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369fbfaf8f3Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370fbfaf8f3Smrg	       }
371fbfaf8f3Smrg	     }
372fbfaf8f3Smrg	  then posix_mkdir=:
373fbfaf8f3Smrg	  fi
374fbfaf8f3Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375fbfaf8f3Smrg	else
376fbfaf8f3Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377fbfaf8f3Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378fbfaf8f3Smrg	fi
379fbfaf8f3Smrg	trap '' 0;;
38043f32c10Smrg    esac
38143f32c10Smrg
38243f32c10Smrg    if
38343f32c10Smrg      $posix_mkdir && (
3846ef05171Smrg        umask $mkdir_umask &&
3856ef05171Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38643f32c10Smrg      )
38743f32c10Smrg    then :
38843f32c10Smrg    else
38943f32c10Smrg
390fbfaf8f3Smrg      # mkdir does not conform to POSIX,
39143f32c10Smrg      # or it failed possibly due to a race condition.  Create the
39243f32c10Smrg      # directory the slow way, step by step, checking for races as we go.
39343f32c10Smrg
39443f32c10Smrg      case $dstdir in
3956ef05171Smrg        /*) prefix='/';;
3966ef05171Smrg        [-=\(\)!]*) prefix='./';;
3976ef05171Smrg        *)  prefix='';;
39843f32c10Smrg      esac
39943f32c10Smrg
40043f32c10Smrg      oIFS=$IFS
40143f32c10Smrg      IFS=/
4026ef05171Smrg      set -f
40343f32c10Smrg      set fnord $dstdir
40443f32c10Smrg      shift
4056ef05171Smrg      set +f
40643f32c10Smrg      IFS=$oIFS
40743f32c10Smrg
40843f32c10Smrg      prefixes=
40943f32c10Smrg
41043f32c10Smrg      for d
41143f32c10Smrg      do
4126ef05171Smrg        test X"$d" = X && continue
4136ef05171Smrg
4146ef05171Smrg        prefix=$prefix$d
4156ef05171Smrg        if test -d "$prefix"; then
4166ef05171Smrg          prefixes=
4176ef05171Smrg        else
4186ef05171Smrg          if $posix_mkdir; then
419fbfaf8f3Smrg            (umask $mkdir_umask &&
4206ef05171Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4216ef05171Smrg            # Don't fail if two instances are running concurrently.
4226ef05171Smrg            test -d "$prefix" || exit 1
4236ef05171Smrg          else
4246ef05171Smrg            case $prefix in
4256ef05171Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4266ef05171Smrg              *) qprefix=$prefix;;
4276ef05171Smrg            esac
4286ef05171Smrg            prefixes="$prefixes '$qprefix'"
4296ef05171Smrg          fi
4306ef05171Smrg        fi
4316ef05171Smrg        prefix=$prefix/
43243f32c10Smrg      done
43343f32c10Smrg
43443f32c10Smrg      if test -n "$prefixes"; then
4356ef05171Smrg        # Don't fail if two instances are running concurrently.
4366ef05171Smrg        (umask $mkdir_umask &&
4376ef05171Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4386ef05171Smrg          test -d "$dstdir" || exit 1
4396ef05171Smrg        obsolete_mkdir_used=true
44043f32c10Smrg      fi
44143f32c10Smrg    fi
44243f32c10Smrg  fi
44343f32c10Smrg
44443f32c10Smrg  if test -n "$dir_arg"; then
44543f32c10Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44643f32c10Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44743f32c10Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44843f32c10Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44943f32c10Smrg  else
45043f32c10Smrg
45143f32c10Smrg    # Make a couple of temp file names in the proper directory.
452ea148d1dSmrg    dsttmp=${dstdirslash}_inst.$$_
453ea148d1dSmrg    rmtmp=${dstdirslash}_rm.$$_
45443f32c10Smrg
45543f32c10Smrg    # Trap to clean up those temp files at exit.
45643f32c10Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45743f32c10Smrg
45843f32c10Smrg    # Copy the file name to the temp name.
459c813b494Smrg    (umask $cp_umask &&
460c813b494Smrg     { test -z "$stripcmd" || {
461c813b494Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462c813b494Smrg	 # which would cause strip to fail.
463c813b494Smrg	 if test -z "$doit"; then
464c813b494Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465c813b494Smrg	 else
466c813b494Smrg	   $doit touch "$dsttmp"
467c813b494Smrg	 fi
468c813b494Smrg       }
469c813b494Smrg     } &&
470c813b494Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47143f32c10Smrg
47243f32c10Smrg    # and set any options; do chmod last to preserve setuid bits.
47343f32c10Smrg    #
47443f32c10Smrg    # If any of these fail, we abort the whole thing.  If we want to
47543f32c10Smrg    # ignore errors from any of these, just make sure not to ignore
47643f32c10Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47743f32c10Smrg    #
47843f32c10Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47943f32c10Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48043f32c10Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48143f32c10Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48243f32c10Smrg
48343f32c10Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48443f32c10Smrg    if $copy_on_change &&
4856ef05171Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4866ef05171Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4876ef05171Smrg       set -f &&
48843f32c10Smrg       set X $old && old=:$2:$4:$5:$6 &&
48943f32c10Smrg       set X $new && new=:$2:$4:$5:$6 &&
4906ef05171Smrg       set +f &&
49143f32c10Smrg       test "$old" = "$new" &&
49243f32c10Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49343f32c10Smrg    then
49443f32c10Smrg      rm -f "$dsttmp"
49543f32c10Smrg    else
496fbfaf8f3Smrg      # If $backupsuffix is set, and the file being installed
497fbfaf8f3Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498fbfaf8f3Smrg      # e.g., if mv doesn't support -f.
499fbfaf8f3Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500fbfaf8f3Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501fbfaf8f3Smrg      fi
502fbfaf8f3Smrg
50343f32c10Smrg      # Rename the file to the real destination.
50443f32c10Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50543f32c10Smrg
50643f32c10Smrg      # The rename failed, perhaps because mv can't rename something else
50743f32c10Smrg      # to itself, or perhaps because mv is so ancient that it does not
50843f32c10Smrg      # support -f.
50943f32c10Smrg      {
5106ef05171Smrg        # Now remove or move aside any old file at destination location.
5116ef05171Smrg        # We try this two ways since rm can't unlink itself on some
5126ef05171Smrg        # systems and the destination file might be busy for other
5136ef05171Smrg        # reasons.  In this case, the final cleanup might fail but the new
5146ef05171Smrg        # file should still install successfully.
5156ef05171Smrg        {
5166ef05171Smrg          test ! -f "$dst" ||
517fbfaf8f3Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
5186ef05171Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519fbfaf8f3Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5206ef05171Smrg          } ||
5216ef05171Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5226ef05171Smrg            (exit 1); exit 1
5236ef05171Smrg          }
5246ef05171Smrg        } &&
5256ef05171Smrg
5266ef05171Smrg        # Now rename the file to the real destination.
5276ef05171Smrg        $doit $mvcmd "$dsttmp" "$dst"
52843f32c10Smrg      }
52943f32c10Smrg    fi || exit 1
53043f32c10Smrg
53143f32c10Smrg    trap '' 0
53243f32c10Smrg  fi
53343f32c10Smrgdone
53443f32c10Smrg
53543f32c10Smrg# Local variables:
536ea148d1dSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
53743f32c10Smrg# time-stamp-start: "scriptversion="
53843f32c10Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5396ef05171Smrg# time-stamp-time-zone: "UTC0"
5406ef05171Smrg# time-stamp-end: "; # UTC"
54143f32c10Smrg# End:
542