154b5899cSmrg#!/bin/sh
205b261ecSmrg# install - install a program, script, or datafile
305b261ecSmrg
454b5899cSmrgscriptversion=2024-06-19.01; # UTC
505b261ecSmrg
605b261ecSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
705b261ecSmrg# later released in X11R6 (xc/config/util/install.sh) with the
805b261ecSmrg# following copyright and license.
905b261ecSmrg#
1005b261ecSmrg# Copyright (C) 1994 X Consortium
1105b261ecSmrg#
1205b261ecSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1305b261ecSmrg# of this software and associated documentation files (the "Software"), to
1405b261ecSmrg# deal in the Software without restriction, including without limitation the
1505b261ecSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1605b261ecSmrg# sell copies of the Software, and to permit persons to whom the Software is
1705b261ecSmrg# furnished to do so, subject to the following conditions:
1805b261ecSmrg#
1905b261ecSmrg# The above copyright notice and this permission notice shall be included in
2005b261ecSmrg# all copies or substantial portions of the Software.
2105b261ecSmrg#
2205b261ecSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2305b261ecSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2405b261ecSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2505b261ecSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2605b261ecSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2705b261ecSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2805b261ecSmrg#
2905b261ecSmrg# Except as contained in this notice, the name of the X Consortium shall not
3005b261ecSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3105b261ecSmrg# ings in this Software without prior written authorization from the X Consor-
3205b261ecSmrg# tium.
3305b261ecSmrg#
3405b261ecSmrg#
3505b261ecSmrg# FSF changes to this file are in the public domain.
3605b261ecSmrg#
3705b261ecSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3835c4bbdfSmrg# 'make' implicit rules from creating a file called install from it
3905b261ecSmrg# when there is no Makefile.
4005b261ecSmrg#
4105b261ecSmrg# This script is compatible with the BSD install script, but was written
4205b261ecSmrg# from scratch.
4305b261ecSmrg
4435c4bbdfSmrgtab='	'
4505b261ecSmrgnl='
4605b261ecSmrg'
4735c4bbdfSmrgIFS=" $tab$nl"
4805b261ecSmrg
4935c4bbdfSmrg# Set DOITPROG to "echo" to test this script.
5005b261ecSmrg
5135c4bbdfSmrgdoit=${DOITPROG-}
5235c4bbdfSmrgdoit_exec=${doit:-exec}
5305b261ecSmrg
5405b261ecSmrg# Put in absolute file names if you don't have them in your path;
5505b261ecSmrg# or use environment vars.
5605b261ecSmrg
5735c4bbdfSmrgchgrpprog=${CHGRPPROG-chgrp}
5835c4bbdfSmrgchmodprog=${CHMODPROG-chmod}
5935c4bbdfSmrgchownprog=${CHOWNPROG-chown}
6035c4bbdfSmrgcmpprog=${CMPPROG-cmp}
6135c4bbdfSmrgcpprog=${CPPROG-cp}
6235c4bbdfSmrgmkdirprog=${MKDIRPROG-mkdir}
6335c4bbdfSmrgmvprog=${MVPROG-mv}
6435c4bbdfSmrgrmprog=${RMPROG-rm}
6535c4bbdfSmrgstripprog=${STRIPPROG-strip}
6605b261ecSmrg
6705b261ecSmrgposix_mkdir=
6805b261ecSmrg
6905b261ecSmrg# Desired mode of installed file.
7005b261ecSmrgmode=0755
7105b261ecSmrg
72dc61d50dSmrg# Create dirs (including intermediate dirs) using mode 755.
73dc61d50dSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74dc61d50dSmrgmkdir_umask=22
75dc61d50dSmrg
76dc61d50dSmrgbackupsuffix=
7735c4bbdfSmrgchgrpcmd=
7805b261ecSmrgchmodcmd=$chmodprog
7905b261ecSmrgchowncmd=
8035c4bbdfSmrgmvcmd=$mvprog
81475c125cSmrgrmcmd="$rmprog -f"
8235c4bbdfSmrgstripcmd=
8335c4bbdfSmrg
8405b261ecSmrgsrc=
8505b261ecSmrgdst=
8605b261ecSmrgdir_arg=
8735c4bbdfSmrgdst_arg=
8835c4bbdfSmrg
8935c4bbdfSmrgcopy_on_change=false
9035c4bbdfSmrgis_target_a_directory=possibly
9105b261ecSmrg
9235c4bbdfSmrgusage="\
9335c4bbdfSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9405b261ecSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9505b261ecSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9605b261ecSmrg   or: $0 [OPTION]... -d DIRECTORIES...
9705b261ecSmrg
9805b261ecSmrgIn the 1st form, copy SRCFILE to DSTFILE.
9905b261ecSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10005b261ecSmrgIn the 4th, create DIRECTORIES.
10105b261ecSmrg
10205b261ecSmrgOptions:
10335c4bbdfSmrg     --help     display this help and exit.
10435c4bbdfSmrg     --version  display version info and exit.
10535c4bbdfSmrg
10635c4bbdfSmrg  -c            (ignored)
107dc61d50dSmrg  -C            install only if different (preserve data modification time)
10835c4bbdfSmrg  -d            create directories instead of installing files.
10935c4bbdfSmrg  -g GROUP      $chgrpprog installed files to GROUP.
11035c4bbdfSmrg  -m MODE       $chmodprog installed files to MODE.
11135c4bbdfSmrg  -o USER       $chownprog installed files to USER.
112dc61d50dSmrg  -p            pass -p to $cpprog.
11335c4bbdfSmrg  -s            $stripprog installed files.
114dc61d50dSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11535c4bbdfSmrg  -t DIRECTORY  install into DIRECTORY.
11635c4bbdfSmrg  -T            report an error if DSTFILE is a directory.
11705b261ecSmrg
11805b261ecSmrgEnvironment variables override the default commands:
11935c4bbdfSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12035c4bbdfSmrg  RMPROG STRIPPROG
121dc61d50dSmrg
122dc61d50dSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123dc61d50dSmrgit's up to you to specify -f if you want it.
124dc61d50dSmrg
125dc61d50dSmrgIf -S is not specified, no backups are attempted.
126dc61d50dSmrg
12758cf2af7SmrgReport bugs to <bug-automake@gnu.org>.
12858cf2af7SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
12958cf2af7SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>."
13005b261ecSmrg
13105b261ecSmrgwhile test $# -ne 0; do
13205b261ecSmrg  case $1 in
13335c4bbdfSmrg    -c) ;;
13405b261ecSmrg
13535c4bbdfSmrg    -C) copy_on_change=true;;
13635c4bbdfSmrg
13735c4bbdfSmrg    -d) dir_arg=true;;
13805b261ecSmrg
13905b261ecSmrg    -g) chgrpcmd="$chgrpprog $2"
14035c4bbdfSmrg        shift;;
14105b261ecSmrg
14205b261ecSmrg    --help) echo "$usage"; exit $?;;
14305b261ecSmrg
14405b261ecSmrg    -m) mode=$2
14535c4bbdfSmrg        case $mode in
14635c4bbdfSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14735c4bbdfSmrg            echo "$0: invalid mode: $mode" >&2
14835c4bbdfSmrg            exit 1;;
14935c4bbdfSmrg        esac
15035c4bbdfSmrg        shift;;
15105b261ecSmrg
15205b261ecSmrg    -o) chowncmd="$chownprog $2"
15335c4bbdfSmrg        shift;;
15405b261ecSmrg
155dc61d50dSmrg    -p) cpprog="$cpprog -p";;
156dc61d50dSmrg
15735c4bbdfSmrg    -s) stripcmd=$stripprog;;
15805b261ecSmrg
159dc61d50dSmrg    -S) backupsuffix="$2"
160dc61d50dSmrg        shift;;
161dc61d50dSmrg
16235c4bbdfSmrg    -t)
16335c4bbdfSmrg        is_target_a_directory=always
16435c4bbdfSmrg        dst_arg=$2
16535c4bbdfSmrg        # Protect names problematic for 'test' and other utilities.
16635c4bbdfSmrg        case $dst_arg in
16735c4bbdfSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16835c4bbdfSmrg        esac
16935c4bbdfSmrg        shift;;
17005b261ecSmrg
17135c4bbdfSmrg    -T) is_target_a_directory=never;;
17205b261ecSmrg
17354b5899cSmrg    --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;;
17405b261ecSmrg
17535c4bbdfSmrg    --) shift
17635c4bbdfSmrg        break;;
17705b261ecSmrg
17835c4bbdfSmrg    -*) echo "$0: invalid option: $1" >&2
17935c4bbdfSmrg        exit 1;;
18005b261ecSmrg
18105b261ecSmrg    *)  break;;
18205b261ecSmrg  esac
18335c4bbdfSmrg  shift
18405b261ecSmrgdone
18505b261ecSmrg
18635c4bbdfSmrg# We allow the use of options -d and -T together, by making -d
18735c4bbdfSmrg# take the precedence; this is for compatibility with GNU install.
18835c4bbdfSmrg
18935c4bbdfSmrgif test -n "$dir_arg"; then
19035c4bbdfSmrg  if test -n "$dst_arg"; then
19135c4bbdfSmrg    echo "$0: target directory not allowed when installing a directory." >&2
19235c4bbdfSmrg    exit 1
19335c4bbdfSmrg  fi
19435c4bbdfSmrgfi
19535c4bbdfSmrg
19635c4bbdfSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19705b261ecSmrg  # When -d is used, all remaining arguments are directories to create.
19805b261ecSmrg  # When -t is used, the destination is already specified.
19905b261ecSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20005b261ecSmrg  for arg
20105b261ecSmrg  do
20235c4bbdfSmrg    if test -n "$dst_arg"; then
20305b261ecSmrg      # $@ is not empty: it contains at least $arg.
20435c4bbdfSmrg      set fnord "$@" "$dst_arg"
20505b261ecSmrg      shift # fnord
20605b261ecSmrg    fi
20705b261ecSmrg    shift # arg
20835c4bbdfSmrg    dst_arg=$arg
20935c4bbdfSmrg    # Protect names problematic for 'test' and other utilities.
21035c4bbdfSmrg    case $dst_arg in
21135c4bbdfSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21235c4bbdfSmrg    esac
21305b261ecSmrg  done
21405b261ecSmrgfi
21505b261ecSmrg
21605b261ecSmrgif test $# -eq 0; then
21705b261ecSmrg  if test -z "$dir_arg"; then
21805b261ecSmrg    echo "$0: no input file specified." >&2
21905b261ecSmrg    exit 1
22005b261ecSmrg  fi
22135c4bbdfSmrg  # It's OK to call 'install-sh -d' without argument.
22205b261ecSmrg  # This can happen when creating conditional directories.
22305b261ecSmrg  exit 0
22405b261ecSmrgfi
22505b261ecSmrg
22605b261ecSmrgif test -z "$dir_arg"; then
22735c4bbdfSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22835c4bbdfSmrg    if test ! -d "$dst_arg"; then
22935c4bbdfSmrg      echo "$0: $dst_arg: Is not a directory." >&2
23035c4bbdfSmrg      exit 1
23135c4bbdfSmrg    fi
23235c4bbdfSmrg  fi
23335c4bbdfSmrgfi
23435c4bbdfSmrg
23535c4bbdfSmrgif test -z "$dir_arg"; then
23635c4bbdfSmrg  do_exit='(exit $ret); exit $ret'
23735c4bbdfSmrg  trap "ret=129; $do_exit" 1
23835c4bbdfSmrg  trap "ret=130; $do_exit" 2
23935c4bbdfSmrg  trap "ret=141; $do_exit" 13
24035c4bbdfSmrg  trap "ret=143; $do_exit" 15
24105b261ecSmrg
24205b261ecSmrg  # Set umask so as not to create temps with too-generous modes.
24305b261ecSmrg  # However, 'strip' requires both read and write access to temps.
24405b261ecSmrg  case $mode in
24505b261ecSmrg    # Optimize common cases.
24605b261ecSmrg    *644) cp_umask=133;;
24705b261ecSmrg    *755) cp_umask=22;;
24805b261ecSmrg
24905b261ecSmrg    *[0-7])
25005b261ecSmrg      if test -z "$stripcmd"; then
25135c4bbdfSmrg        u_plus_rw=
25205b261ecSmrg      else
25335c4bbdfSmrg        u_plus_rw='% 200'
25405b261ecSmrg      fi
25505b261ecSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25605b261ecSmrg    *)
25705b261ecSmrg      if test -z "$stripcmd"; then
25835c4bbdfSmrg        u_plus_rw=
25905b261ecSmrg      else
26035c4bbdfSmrg        u_plus_rw=,u+rw
26105b261ecSmrg      fi
26205b261ecSmrg      cp_umask=$mode$u_plus_rw;;
26305b261ecSmrg  esac
26405b261ecSmrgfi
26505b261ecSmrg
26605b261ecSmrgfor src
26705b261ecSmrgdo
26835c4bbdfSmrg  # Protect names problematic for 'test' and other utilities.
26905b261ecSmrg  case $src in
27035c4bbdfSmrg    -* | [=\(\)!]) src=./$src;;
27105b261ecSmrg  esac
27205b261ecSmrg
27305b261ecSmrg  if test -n "$dir_arg"; then
27405b261ecSmrg    dst=$src
27505b261ecSmrg    dstdir=$dst
27605b261ecSmrg    test -d "$dstdir"
27705b261ecSmrg    dstdir_status=$?
278dc61d50dSmrg    # Don't chown directories that already exist.
279dc61d50dSmrg    if test $dstdir_status = 0; then
280dc61d50dSmrg      chowncmd=""
281dc61d50dSmrg    fi
28205b261ecSmrg  else
28305b261ecSmrg
28405b261ecSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28505b261ecSmrg    # might cause directories to be created, which would be especially bad
28605b261ecSmrg    # if $src (and thus $dsttmp) contains '*'.
28705b261ecSmrg    if test ! -f "$src" && test ! -d "$src"; then
28805b261ecSmrg      echo "$0: $src does not exist." >&2
28905b261ecSmrg      exit 1
29005b261ecSmrg    fi
29105b261ecSmrg
29235c4bbdfSmrg    if test -z "$dst_arg"; then
29305b261ecSmrg      echo "$0: no destination specified." >&2
29405b261ecSmrg      exit 1
29505b261ecSmrg    fi
29635c4bbdfSmrg    dst=$dst_arg
29705b261ecSmrg
298e23ec014Smrg    # If destination is a directory, append the input filename.
29905b261ecSmrg    if test -d "$dst"; then
30035c4bbdfSmrg      if test "$is_target_a_directory" = never; then
30135c4bbdfSmrg        echo "$0: $dst_arg: Is a directory" >&2
30235c4bbdfSmrg        exit 1
30305b261ecSmrg      fi
30405b261ecSmrg      dstdir=$dst
305e23ec014Smrg      dstbase=`basename "$src"`
306e23ec014Smrg      case $dst in
307e23ec014Smrg	*/) dst=$dst$dstbase;;
308e23ec014Smrg	*)  dst=$dst/$dstbase;;
309e23ec014Smrg      esac
31005b261ecSmrg      dstdir_status=0
31105b261ecSmrg    else
31235c4bbdfSmrg      dstdir=`dirname "$dst"`
31305b261ecSmrg      test -d "$dstdir"
31405b261ecSmrg      dstdir_status=$?
31505b261ecSmrg    fi
31605b261ecSmrg  fi
31705b261ecSmrg
318e23ec014Smrg  case $dstdir in
319e23ec014Smrg    */) dstdirslash=$dstdir;;
320e23ec014Smrg    *)  dstdirslash=$dstdir/;;
321e23ec014Smrg  esac
322e23ec014Smrg
32305b261ecSmrg  obsolete_mkdir_used=false
32405b261ecSmrg
32505b261ecSmrg  if test $dstdir_status != 0; then
32605b261ecSmrg    case $posix_mkdir in
32705b261ecSmrg      '')
32835c4bbdfSmrg        # With -d, create the new directory with the user-specified mode.
32935c4bbdfSmrg        # Otherwise, rely on $mkdir_umask.
33035c4bbdfSmrg        if test -n "$dir_arg"; then
33135c4bbdfSmrg          mkdir_mode=-m$mode
33235c4bbdfSmrg        else
33335c4bbdfSmrg          mkdir_mode=
33435c4bbdfSmrg        fi
33535c4bbdfSmrg
33635c4bbdfSmrg        posix_mkdir=false
337dc61d50dSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338dc61d50dSmrg	# here however when possible just to lower collision chance.
339dc61d50dSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340dc61d50dSmrg
341dc61d50dSmrg	trap '
342dc61d50dSmrg	  ret=$?
343dc61d50dSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344dc61d50dSmrg	  exit $ret
345dc61d50dSmrg	' 0
346dc61d50dSmrg
347dc61d50dSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
34854b5899cSmrg	# directly in world-writable /tmp, make sure that the '$tmpdir'
349dc61d50dSmrg	# directory is successfully created first before we actually test
350dc61d50dSmrg	# 'mkdir -p'.
351dc61d50dSmrg	if (umask $mkdir_umask &&
352dc61d50dSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353dc61d50dSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354dc61d50dSmrg	then
355dc61d50dSmrg	  if test -z "$dir_arg" || {
35654b5899cSmrg	       # Check for POSIX incompatibility with -m.
357dc61d50dSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358dc61d50dSmrg	       # other-writable bit of parent directory when it shouldn't.
359dc61d50dSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360dc61d50dSmrg	       test_tmpdir="$tmpdir/a"
361dc61d50dSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362dc61d50dSmrg	       case $ls_ld_tmpdir in
363dc61d50dSmrg		 d????-?r-*) different_mode=700;;
364dc61d50dSmrg		 d????-?--*) different_mode=755;;
365dc61d50dSmrg		 *) false;;
366dc61d50dSmrg	       esac &&
367dc61d50dSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368dc61d50dSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369dc61d50dSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370dc61d50dSmrg	       }
371dc61d50dSmrg	     }
372dc61d50dSmrg	  then posix_mkdir=:
373dc61d50dSmrg	  fi
374dc61d50dSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375dc61d50dSmrg	else
376dc61d50dSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377dc61d50dSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378dc61d50dSmrg	fi
379dc61d50dSmrg	trap '' 0;;
38005b261ecSmrg    esac
38105b261ecSmrg
38205b261ecSmrg    if
38305b261ecSmrg      $posix_mkdir && (
38435c4bbdfSmrg        umask $mkdir_umask &&
38535c4bbdfSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38605b261ecSmrg      )
38705b261ecSmrg    then :
38805b261ecSmrg    else
38905b261ecSmrg
390dc61d50dSmrg      # mkdir does not conform to POSIX,
39105b261ecSmrg      # or it failed possibly due to a race condition.  Create the
39205b261ecSmrg      # directory the slow way, step by step, checking for races as we go.
39305b261ecSmrg
39405b261ecSmrg      case $dstdir in
39535c4bbdfSmrg        /*) prefix='/';;
39635c4bbdfSmrg        [-=\(\)!]*) prefix='./';;
39735c4bbdfSmrg        *)  prefix='';;
398475c125cSmrg      esac
39905b261ecSmrg
40005b261ecSmrg      oIFS=$IFS
40105b261ecSmrg      IFS=/
40235c4bbdfSmrg      set -f
40305b261ecSmrg      set fnord $dstdir
40405b261ecSmrg      shift
40535c4bbdfSmrg      set +f
40605b261ecSmrg      IFS=$oIFS
40705b261ecSmrg
40805b261ecSmrg      prefixes=
40905b261ecSmrg
41005b261ecSmrg      for d
41105b261ecSmrg      do
41235c4bbdfSmrg        test X"$d" = X && continue
41335c4bbdfSmrg
41435c4bbdfSmrg        prefix=$prefix$d
41535c4bbdfSmrg        if test -d "$prefix"; then
41635c4bbdfSmrg          prefixes=
41735c4bbdfSmrg        else
41835c4bbdfSmrg          if $posix_mkdir; then
419dc61d50dSmrg            (umask $mkdir_umask &&
42035c4bbdfSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42135c4bbdfSmrg            # Don't fail if two instances are running concurrently.
42235c4bbdfSmrg            test -d "$prefix" || exit 1
42335c4bbdfSmrg          else
42435c4bbdfSmrg            case $prefix in
42535c4bbdfSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42635c4bbdfSmrg              *) qprefix=$prefix;;
42735c4bbdfSmrg            esac
42835c4bbdfSmrg            prefixes="$prefixes '$qprefix'"
42935c4bbdfSmrg          fi
43035c4bbdfSmrg        fi
43135c4bbdfSmrg        prefix=$prefix/
43205b261ecSmrg      done
43305b261ecSmrg
43405b261ecSmrg      if test -n "$prefixes"; then
43535c4bbdfSmrg        # Don't fail if two instances are running concurrently.
43635c4bbdfSmrg        (umask $mkdir_umask &&
43735c4bbdfSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43835c4bbdfSmrg          test -d "$dstdir" || exit 1
43935c4bbdfSmrg        obsolete_mkdir_used=true
44005b261ecSmrg      fi
44105b261ecSmrg    fi
44205b261ecSmrg  fi
44305b261ecSmrg
44405b261ecSmrg  if test -n "$dir_arg"; then
44505b261ecSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44605b261ecSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44705b261ecSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44805b261ecSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44905b261ecSmrg  else
45005b261ecSmrg
45105b261ecSmrg    # Make a couple of temp file names in the proper directory.
452e23ec014Smrg    dsttmp=${dstdirslash}_inst.$$_
453e23ec014Smrg    rmtmp=${dstdirslash}_rm.$$_
45405b261ecSmrg
45505b261ecSmrg    # Trap to clean up those temp files at exit.
45605b261ecSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45705b261ecSmrg
45805b261ecSmrg    # Copy the file name to the temp name.
459dc61d50dSmrg    (umask $cp_umask &&
460dc61d50dSmrg     { test -z "$stripcmd" || {
461dc61d50dSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462dc61d50dSmrg	 # which would cause strip to fail.
463dc61d50dSmrg	 if test -z "$doit"; then
464dc61d50dSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465dc61d50dSmrg	 else
466dc61d50dSmrg	   $doit touch "$dsttmp"
467dc61d50dSmrg	 fi
468dc61d50dSmrg       }
469dc61d50dSmrg     } &&
470dc61d50dSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47105b261ecSmrg
47205b261ecSmrg    # and set any options; do chmod last to preserve setuid bits.
47305b261ecSmrg    #
47405b261ecSmrg    # If any of these fail, we abort the whole thing.  If we want to
47505b261ecSmrg    # ignore errors from any of these, just make sure not to ignore
47605b261ecSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47705b261ecSmrg    #
47835c4bbdfSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47935c4bbdfSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48035c4bbdfSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48135c4bbdfSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48235c4bbdfSmrg
48335c4bbdfSmrg    # If -C, don't bother to copy if it wouldn't change the file.
48435c4bbdfSmrg    if $copy_on_change &&
48535c4bbdfSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48635c4bbdfSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48735c4bbdfSmrg       set -f &&
48835c4bbdfSmrg       set X $old && old=:$2:$4:$5:$6 &&
48935c4bbdfSmrg       set X $new && new=:$2:$4:$5:$6 &&
49035c4bbdfSmrg       set +f &&
49135c4bbdfSmrg       test "$old" = "$new" &&
49235c4bbdfSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49335c4bbdfSmrg    then
49435c4bbdfSmrg      rm -f "$dsttmp"
49535c4bbdfSmrg    else
496dc61d50dSmrg      # If $backupsuffix is set, and the file being installed
497dc61d50dSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498dc61d50dSmrg      # e.g., if mv doesn't support -f.
499dc61d50dSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500dc61d50dSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501dc61d50dSmrg      fi
502dc61d50dSmrg
50335c4bbdfSmrg      # Rename the file to the real destination.
50435c4bbdfSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50535c4bbdfSmrg
50635c4bbdfSmrg      # The rename failed, perhaps because mv can't rename something else
50735c4bbdfSmrg      # to itself, or perhaps because mv is so ancient that it does not
50835c4bbdfSmrg      # support -f.
50935c4bbdfSmrg      {
51035c4bbdfSmrg        # Now remove or move aside any old file at destination location.
51135c4bbdfSmrg        # We try this two ways since rm can't unlink itself on some
51235c4bbdfSmrg        # systems and the destination file might be busy for other
51335c4bbdfSmrg        # reasons.  In this case, the final cleanup might fail but the new
51435c4bbdfSmrg        # file should still install successfully.
51535c4bbdfSmrg        {
51635c4bbdfSmrg          test ! -f "$dst" ||
517dc61d50dSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
51835c4bbdfSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519dc61d50dSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52035c4bbdfSmrg          } ||
52135c4bbdfSmrg          { echo "$0: cannot unlink or rename $dst" >&2
52235c4bbdfSmrg            (exit 1); exit 1
52335c4bbdfSmrg          }
52435c4bbdfSmrg        } &&
52535c4bbdfSmrg
52635c4bbdfSmrg        # Now rename the file to the real destination.
52735c4bbdfSmrg        $doit $mvcmd "$dsttmp" "$dst"
52835c4bbdfSmrg      }
52935c4bbdfSmrg    fi || exit 1
53005b261ecSmrg
53105b261ecSmrg    trap '' 0
53205b261ecSmrg  fi
53305b261ecSmrgdone
53405b261ecSmrg
53505b261ecSmrg# Local variables:
536e23ec014Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
53705b261ecSmrg# time-stamp-start: "scriptversion="
53805b261ecSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5391b5d61b8Smrg# time-stamp-time-zone: "UTC0"
54035c4bbdfSmrg# time-stamp-end: "; # UTC"
54105b261ecSmrg# End:
542