173143b9aSmrg#!/bin/sh
273143b9aSmrg# install - install a program, script, or datafile
373143b9aSmrg
447a4502cSmrgscriptversion=2024-06-19.01; # UTC
573143b9aSmrg
673143b9aSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
773143b9aSmrg# later released in X11R6 (xc/config/util/install.sh) with the
873143b9aSmrg# following copyright and license.
973143b9aSmrg#
1073143b9aSmrg# Copyright (C) 1994 X Consortium
1173143b9aSmrg#
1273143b9aSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1373143b9aSmrg# of this software and associated documentation files (the "Software"), to
1473143b9aSmrg# deal in the Software without restriction, including without limitation the
1573143b9aSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1673143b9aSmrg# sell copies of the Software, and to permit persons to whom the Software is
1773143b9aSmrg# furnished to do so, subject to the following conditions:
1873143b9aSmrg#
1973143b9aSmrg# The above copyright notice and this permission notice shall be included in
2073143b9aSmrg# all copies or substantial portions of the Software.
2173143b9aSmrg#
2273143b9aSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2373143b9aSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2473143b9aSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2573143b9aSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2673143b9aSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2773143b9aSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2873143b9aSmrg#
2973143b9aSmrg# Except as contained in this notice, the name of the X Consortium shall not
3073143b9aSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3173143b9aSmrg# ings in this Software without prior written authorization from the X Consor-
3273143b9aSmrg# tium.
3373143b9aSmrg#
3473143b9aSmrg#
3573143b9aSmrg# FSF changes to this file are in the public domain.
3673143b9aSmrg#
3773143b9aSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3875ebec6dSmrg# 'make' implicit rules from creating a file called install from it
3973143b9aSmrg# when there is no Makefile.
4073143b9aSmrg#
4173143b9aSmrg# This script is compatible with the BSD install script, but was written
4273143b9aSmrg# from scratch.
4373143b9aSmrg
443d2ed3e3Smrgtab='	'
4573143b9aSmrgnl='
4673143b9aSmrg'
473d2ed3e3SmrgIFS=" $tab$nl"
4873143b9aSmrg
493d2ed3e3Smrg# Set DOITPROG to "echo" to test this script.
5073143b9aSmrg
51b53e5eeaSmrgdoit=${DOITPROG-}
523d2ed3e3Smrgdoit_exec=${doit:-exec}
5373143b9aSmrg
5473143b9aSmrg# Put in absolute file names if you don't have them in your path;
5573143b9aSmrg# or use environment vars.
5673143b9aSmrg
57b53e5eeaSmrgchgrpprog=${CHGRPPROG-chgrp}
58b53e5eeaSmrgchmodprog=${CHMODPROG-chmod}
59b53e5eeaSmrgchownprog=${CHOWNPROG-chown}
60b53e5eeaSmrgcmpprog=${CMPPROG-cmp}
61b53e5eeaSmrgcpprog=${CPPROG-cp}
62b53e5eeaSmrgmkdirprog=${MKDIRPROG-mkdir}
63b53e5eeaSmrgmvprog=${MVPROG-mv}
64b53e5eeaSmrgrmprog=${RMPROG-rm}
65b53e5eeaSmrgstripprog=${STRIPPROG-strip}
66b53e5eeaSmrg
6773143b9aSmrgposix_mkdir=
6873143b9aSmrg
6973143b9aSmrg# Desired mode of installed file.
7073143b9aSmrgmode=0755
7173143b9aSmrg
72e45ace2bSmrg# Create dirs (including intermediate dirs) using mode 755.
73e45ace2bSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74e45ace2bSmrgmkdir_umask=22
75e45ace2bSmrg
76e45ace2bSmrgbackupsuffix=
77b53e5eeaSmrgchgrpcmd=
7873143b9aSmrgchmodcmd=$chmodprog
7973143b9aSmrgchowncmd=
80b53e5eeaSmrgmvcmd=$mvprog
818d4c0f7bSmrgrmcmd="$rmprog -f"
82b53e5eeaSmrgstripcmd=
83b53e5eeaSmrg
8473143b9aSmrgsrc=
8573143b9aSmrgdst=
8673143b9aSmrgdir_arg=
87b53e5eeaSmrgdst_arg=
88b53e5eeaSmrg
89b53e5eeaSmrgcopy_on_change=false
903d2ed3e3Smrgis_target_a_directory=possibly
9173143b9aSmrg
92b53e5eeaSmrgusage="\
93b53e5eeaSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9473143b9aSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9573143b9aSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9673143b9aSmrg   or: $0 [OPTION]... -d DIRECTORIES...
9773143b9aSmrg
9873143b9aSmrgIn the 1st form, copy SRCFILE to DSTFILE.
9973143b9aSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10073143b9aSmrgIn the 4th, create DIRECTORIES.
10173143b9aSmrg
10273143b9aSmrgOptions:
103b53e5eeaSmrg     --help     display this help and exit.
104b53e5eeaSmrg     --version  display version info and exit.
105b53e5eeaSmrg
106b53e5eeaSmrg  -c            (ignored)
107e45ace2bSmrg  -C            install only if different (preserve data modification time)
108b53e5eeaSmrg  -d            create directories instead of installing files.
109b53e5eeaSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110b53e5eeaSmrg  -m MODE       $chmodprog installed files to MODE.
111b53e5eeaSmrg  -o USER       $chownprog installed files to USER.
112e45ace2bSmrg  -p            pass -p to $cpprog.
113b53e5eeaSmrg  -s            $stripprog installed files.
114e45ace2bSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115b53e5eeaSmrg  -t DIRECTORY  install into DIRECTORY.
116b53e5eeaSmrg  -T            report an error if DSTFILE is a directory.
11773143b9aSmrg
11873143b9aSmrgEnvironment variables override the default commands:
119b53e5eeaSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120b53e5eeaSmrg  RMPROG STRIPPROG
121e45ace2bSmrg
122e45ace2bSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123e45ace2bSmrgit's up to you to specify -f if you want it.
124e45ace2bSmrg
125e45ace2bSmrgIf -S is not specified, no backups are attempted.
126e45ace2bSmrg
12747a4502cSmrgReport bugs to <bug-automake@gnu.org>.
12847a4502cSmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
12947a4502cSmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>."
13073143b9aSmrg
13173143b9aSmrgwhile test $# -ne 0; do
13273143b9aSmrg  case $1 in
133b53e5eeaSmrg    -c) ;;
134b53e5eeaSmrg
135b53e5eeaSmrg    -C) copy_on_change=true;;
13673143b9aSmrg
137b53e5eeaSmrg    -d) dir_arg=true;;
13873143b9aSmrg
13973143b9aSmrg    -g) chgrpcmd="$chgrpprog $2"
1403d2ed3e3Smrg        shift;;
14173143b9aSmrg
14273143b9aSmrg    --help) echo "$usage"; exit $?;;
14373143b9aSmrg
14473143b9aSmrg    -m) mode=$2
1453d2ed3e3Smrg        case $mode in
1463d2ed3e3Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1473d2ed3e3Smrg            echo "$0: invalid mode: $mode" >&2
1483d2ed3e3Smrg            exit 1;;
1493d2ed3e3Smrg        esac
1503d2ed3e3Smrg        shift;;
15173143b9aSmrg
15273143b9aSmrg    -o) chowncmd="$chownprog $2"
1533d2ed3e3Smrg        shift;;
15473143b9aSmrg
155e45ace2bSmrg    -p) cpprog="$cpprog -p";;
156e45ace2bSmrg
157b53e5eeaSmrg    -s) stripcmd=$stripprog;;
15873143b9aSmrg
159e45ace2bSmrg    -S) backupsuffix="$2"
160e45ace2bSmrg        shift;;
161e45ace2bSmrg
1623d2ed3e3Smrg    -t)
1633d2ed3e3Smrg        is_target_a_directory=always
1643d2ed3e3Smrg        dst_arg=$2
1653d2ed3e3Smrg        # Protect names problematic for 'test' and other utilities.
1663d2ed3e3Smrg        case $dst_arg in
1673d2ed3e3Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1683d2ed3e3Smrg        esac
1693d2ed3e3Smrg        shift;;
17073143b9aSmrg
1713d2ed3e3Smrg    -T) is_target_a_directory=never;;
17273143b9aSmrg
17347a4502cSmrg    --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;;
17473143b9aSmrg
1753d2ed3e3Smrg    --) shift
1763d2ed3e3Smrg        break;;
17773143b9aSmrg
1783d2ed3e3Smrg    -*) echo "$0: invalid option: $1" >&2
1793d2ed3e3Smrg        exit 1;;
18073143b9aSmrg
18173143b9aSmrg    *)  break;;
18273143b9aSmrg  esac
183b53e5eeaSmrg  shift
18473143b9aSmrgdone
18573143b9aSmrg
1863d2ed3e3Smrg# We allow the use of options -d and -T together, by making -d
1873d2ed3e3Smrg# take the precedence; this is for compatibility with GNU install.
1883d2ed3e3Smrg
1893d2ed3e3Smrgif test -n "$dir_arg"; then
1903d2ed3e3Smrg  if test -n "$dst_arg"; then
1913d2ed3e3Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1923d2ed3e3Smrg    exit 1
1933d2ed3e3Smrg  fi
1943d2ed3e3Smrgfi
1953d2ed3e3Smrg
196b53e5eeaSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19773143b9aSmrg  # When -d is used, all remaining arguments are directories to create.
19873143b9aSmrg  # When -t is used, the destination is already specified.
19973143b9aSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20073143b9aSmrg  for arg
20173143b9aSmrg  do
202b53e5eeaSmrg    if test -n "$dst_arg"; then
20373143b9aSmrg      # $@ is not empty: it contains at least $arg.
204b53e5eeaSmrg      set fnord "$@" "$dst_arg"
20573143b9aSmrg      shift # fnord
20673143b9aSmrg    fi
20773143b9aSmrg    shift # arg
208b53e5eeaSmrg    dst_arg=$arg
20975ebec6dSmrg    # Protect names problematic for 'test' and other utilities.
21075ebec6dSmrg    case $dst_arg in
21175ebec6dSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21275ebec6dSmrg    esac
21373143b9aSmrg  done
21473143b9aSmrgfi
21573143b9aSmrg
21673143b9aSmrgif test $# -eq 0; then
21773143b9aSmrg  if test -z "$dir_arg"; then
21873143b9aSmrg    echo "$0: no input file specified." >&2
21973143b9aSmrg    exit 1
22073143b9aSmrg  fi
22175ebec6dSmrg  # It's OK to call 'install-sh -d' without argument.
22273143b9aSmrg  # This can happen when creating conditional directories.
22373143b9aSmrg  exit 0
22473143b9aSmrgfi
22573143b9aSmrg
2263d2ed3e3Smrgif test -z "$dir_arg"; then
2273d2ed3e3Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2283d2ed3e3Smrg    if test ! -d "$dst_arg"; then
2293d2ed3e3Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2303d2ed3e3Smrg      exit 1
2313d2ed3e3Smrg    fi
2323d2ed3e3Smrg  fi
2333d2ed3e3Smrgfi
2343d2ed3e3Smrg
23573143b9aSmrgif test -z "$dir_arg"; then
23675ebec6dSmrg  do_exit='(exit $ret); exit $ret'
23775ebec6dSmrg  trap "ret=129; $do_exit" 1
23875ebec6dSmrg  trap "ret=130; $do_exit" 2
23975ebec6dSmrg  trap "ret=141; $do_exit" 13
24075ebec6dSmrg  trap "ret=143; $do_exit" 15
24173143b9aSmrg
24273143b9aSmrg  # Set umask so as not to create temps with too-generous modes.
24373143b9aSmrg  # However, 'strip' requires both read and write access to temps.
24473143b9aSmrg  case $mode in
24573143b9aSmrg    # Optimize common cases.
24673143b9aSmrg    *644) cp_umask=133;;
24773143b9aSmrg    *755) cp_umask=22;;
24873143b9aSmrg
24973143b9aSmrg    *[0-7])
25073143b9aSmrg      if test -z "$stripcmd"; then
2513d2ed3e3Smrg        u_plus_rw=
25273143b9aSmrg      else
2533d2ed3e3Smrg        u_plus_rw='% 200'
25473143b9aSmrg      fi
25573143b9aSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25673143b9aSmrg    *)
25773143b9aSmrg      if test -z "$stripcmd"; then
2583d2ed3e3Smrg        u_plus_rw=
25973143b9aSmrg      else
2603d2ed3e3Smrg        u_plus_rw=,u+rw
26173143b9aSmrg      fi
26273143b9aSmrg      cp_umask=$mode$u_plus_rw;;
26373143b9aSmrg  esac
26473143b9aSmrgfi
26573143b9aSmrg
26673143b9aSmrgfor src
26773143b9aSmrgdo
26875ebec6dSmrg  # Protect names problematic for 'test' and other utilities.
26973143b9aSmrg  case $src in
27075ebec6dSmrg    -* | [=\(\)!]) src=./$src;;
27173143b9aSmrg  esac
27273143b9aSmrg
27373143b9aSmrg  if test -n "$dir_arg"; then
27473143b9aSmrg    dst=$src
27573143b9aSmrg    dstdir=$dst
27673143b9aSmrg    test -d "$dstdir"
27773143b9aSmrg    dstdir_status=$?
278e45ace2bSmrg    # Don't chown directories that already exist.
279e45ace2bSmrg    if test $dstdir_status = 0; then
280e45ace2bSmrg      chowncmd=""
281e45ace2bSmrg    fi
28273143b9aSmrg  else
28373143b9aSmrg
28473143b9aSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28573143b9aSmrg    # might cause directories to be created, which would be especially bad
28673143b9aSmrg    # if $src (and thus $dsttmp) contains '*'.
28773143b9aSmrg    if test ! -f "$src" && test ! -d "$src"; then
28873143b9aSmrg      echo "$0: $src does not exist." >&2
28973143b9aSmrg      exit 1
29073143b9aSmrg    fi
29173143b9aSmrg
292b53e5eeaSmrg    if test -z "$dst_arg"; then
29373143b9aSmrg      echo "$0: no destination specified." >&2
29473143b9aSmrg      exit 1
29573143b9aSmrg    fi
296b53e5eeaSmrg    dst=$dst_arg
29773143b9aSmrg
298e45ace2bSmrg    # If destination is a directory, append the input filename.
29973143b9aSmrg    if test -d "$dst"; then
3003d2ed3e3Smrg      if test "$is_target_a_directory" = never; then
3013d2ed3e3Smrg        echo "$0: $dst_arg: Is a directory" >&2
3023d2ed3e3Smrg        exit 1
30373143b9aSmrg      fi
30473143b9aSmrg      dstdir=$dst
305e45ace2bSmrg      dstbase=`basename "$src"`
306e45ace2bSmrg      case $dst in
307e45ace2bSmrg	*/) dst=$dst$dstbase;;
308e45ace2bSmrg	*)  dst=$dst/$dstbase;;
309e45ace2bSmrg      esac
31073143b9aSmrg      dstdir_status=0
31173143b9aSmrg    else
3123d2ed3e3Smrg      dstdir=`dirname "$dst"`
31373143b9aSmrg      test -d "$dstdir"
31473143b9aSmrg      dstdir_status=$?
31573143b9aSmrg    fi
31673143b9aSmrg  fi
31773143b9aSmrg
318e45ace2bSmrg  case $dstdir in
319e45ace2bSmrg    */) dstdirslash=$dstdir;;
320e45ace2bSmrg    *)  dstdirslash=$dstdir/;;
321e45ace2bSmrg  esac
322e45ace2bSmrg
32373143b9aSmrg  obsolete_mkdir_used=false
32473143b9aSmrg
32573143b9aSmrg  if test $dstdir_status != 0; then
32673143b9aSmrg    case $posix_mkdir in
32773143b9aSmrg      '')
3283d2ed3e3Smrg        # With -d, create the new directory with the user-specified mode.
3293d2ed3e3Smrg        # Otherwise, rely on $mkdir_umask.
3303d2ed3e3Smrg        if test -n "$dir_arg"; then
3313d2ed3e3Smrg          mkdir_mode=-m$mode
3323d2ed3e3Smrg        else
3333d2ed3e3Smrg          mkdir_mode=
3343d2ed3e3Smrg        fi
3353d2ed3e3Smrg
3363d2ed3e3Smrg        posix_mkdir=false
337e45ace2bSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338e45ace2bSmrg	# here however when possible just to lower collision chance.
339e45ace2bSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340e45ace2bSmrg
341e45ace2bSmrg	trap '
342e45ace2bSmrg	  ret=$?
343e45ace2bSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344e45ace2bSmrg	  exit $ret
345e45ace2bSmrg	' 0
346e45ace2bSmrg
347e45ace2bSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
34847a4502cSmrg	# directly in world-writable /tmp, make sure that the '$tmpdir'
349e45ace2bSmrg	# directory is successfully created first before we actually test
350e45ace2bSmrg	# 'mkdir -p'.
351e45ace2bSmrg	if (umask $mkdir_umask &&
352e45ace2bSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353e45ace2bSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354e45ace2bSmrg	then
355e45ace2bSmrg	  if test -z "$dir_arg" || {
35647a4502cSmrg	       # Check for POSIX incompatibility with -m.
357e45ace2bSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358e45ace2bSmrg	       # other-writable bit of parent directory when it shouldn't.
359e45ace2bSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360e45ace2bSmrg	       test_tmpdir="$tmpdir/a"
361e45ace2bSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362e45ace2bSmrg	       case $ls_ld_tmpdir in
363e45ace2bSmrg		 d????-?r-*) different_mode=700;;
364e45ace2bSmrg		 d????-?--*) different_mode=755;;
365e45ace2bSmrg		 *) false;;
366e45ace2bSmrg	       esac &&
367e45ace2bSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368e45ace2bSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369e45ace2bSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370e45ace2bSmrg	       }
371e45ace2bSmrg	     }
372e45ace2bSmrg	  then posix_mkdir=:
373e45ace2bSmrg	  fi
374e45ace2bSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375e45ace2bSmrg	else
376e45ace2bSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377e45ace2bSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378e45ace2bSmrg	fi
379e45ace2bSmrg	trap '' 0;;
38073143b9aSmrg    esac
38173143b9aSmrg
38273143b9aSmrg    if
38373143b9aSmrg      $posix_mkdir && (
3843d2ed3e3Smrg        umask $mkdir_umask &&
3853d2ed3e3Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38673143b9aSmrg      )
38773143b9aSmrg    then :
38873143b9aSmrg    else
38973143b9aSmrg
390e45ace2bSmrg      # mkdir does not conform to POSIX,
39173143b9aSmrg      # or it failed possibly due to a race condition.  Create the
39273143b9aSmrg      # directory the slow way, step by step, checking for races as we go.
39373143b9aSmrg
39473143b9aSmrg      case $dstdir in
3953d2ed3e3Smrg        /*) prefix='/';;
3963d2ed3e3Smrg        [-=\(\)!]*) prefix='./';;
3973d2ed3e3Smrg        *)  prefix='';;
39873143b9aSmrg      esac
39973143b9aSmrg
40073143b9aSmrg      oIFS=$IFS
40173143b9aSmrg      IFS=/
4023d2ed3e3Smrg      set -f
40373143b9aSmrg      set fnord $dstdir
40473143b9aSmrg      shift
4053d2ed3e3Smrg      set +f
40673143b9aSmrg      IFS=$oIFS
40773143b9aSmrg
40873143b9aSmrg      prefixes=
40973143b9aSmrg
41073143b9aSmrg      for d
41173143b9aSmrg      do
4123d2ed3e3Smrg        test X"$d" = X && continue
4133d2ed3e3Smrg
4143d2ed3e3Smrg        prefix=$prefix$d
4153d2ed3e3Smrg        if test -d "$prefix"; then
4163d2ed3e3Smrg          prefixes=
4173d2ed3e3Smrg        else
4183d2ed3e3Smrg          if $posix_mkdir; then
419e45ace2bSmrg            (umask $mkdir_umask &&
4203d2ed3e3Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4213d2ed3e3Smrg            # Don't fail if two instances are running concurrently.
4223d2ed3e3Smrg            test -d "$prefix" || exit 1
4233d2ed3e3Smrg          else
4243d2ed3e3Smrg            case $prefix in
4253d2ed3e3Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4263d2ed3e3Smrg              *) qprefix=$prefix;;
4273d2ed3e3Smrg            esac
4283d2ed3e3Smrg            prefixes="$prefixes '$qprefix'"
4293d2ed3e3Smrg          fi
4303d2ed3e3Smrg        fi
4313d2ed3e3Smrg        prefix=$prefix/
43273143b9aSmrg      done
43373143b9aSmrg
43473143b9aSmrg      if test -n "$prefixes"; then
4353d2ed3e3Smrg        # Don't fail if two instances are running concurrently.
4363d2ed3e3Smrg        (umask $mkdir_umask &&
4373d2ed3e3Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4383d2ed3e3Smrg          test -d "$dstdir" || exit 1
4393d2ed3e3Smrg        obsolete_mkdir_used=true
44073143b9aSmrg      fi
44173143b9aSmrg    fi
44273143b9aSmrg  fi
44373143b9aSmrg
44473143b9aSmrg  if test -n "$dir_arg"; then
44573143b9aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44673143b9aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44773143b9aSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44873143b9aSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44973143b9aSmrg  else
45073143b9aSmrg
45173143b9aSmrg    # Make a couple of temp file names in the proper directory.
452e45ace2bSmrg    dsttmp=${dstdirslash}_inst.$$_
453e45ace2bSmrg    rmtmp=${dstdirslash}_rm.$$_
45473143b9aSmrg
45573143b9aSmrg    # Trap to clean up those temp files at exit.
45673143b9aSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45773143b9aSmrg
45873143b9aSmrg    # Copy the file name to the temp name.
459e45ace2bSmrg    (umask $cp_umask &&
460e45ace2bSmrg     { test -z "$stripcmd" || {
461e45ace2bSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462e45ace2bSmrg	 # which would cause strip to fail.
463e45ace2bSmrg	 if test -z "$doit"; then
464e45ace2bSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465e45ace2bSmrg	 else
466e45ace2bSmrg	   $doit touch "$dsttmp"
467e45ace2bSmrg	 fi
468e45ace2bSmrg       }
469e45ace2bSmrg     } &&
470e45ace2bSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47173143b9aSmrg
47273143b9aSmrg    # and set any options; do chmod last to preserve setuid bits.
47373143b9aSmrg    #
47473143b9aSmrg    # If any of these fail, we abort the whole thing.  If we want to
47573143b9aSmrg    # ignore errors from any of these, just make sure not to ignore
47673143b9aSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47773143b9aSmrg    #
478b53e5eeaSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479b53e5eeaSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480b53e5eeaSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481b53e5eeaSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482b53e5eeaSmrg
483b53e5eeaSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484b53e5eeaSmrg    if $copy_on_change &&
4853d2ed3e3Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4863d2ed3e3Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4873d2ed3e3Smrg       set -f &&
488b53e5eeaSmrg       set X $old && old=:$2:$4:$5:$6 &&
489b53e5eeaSmrg       set X $new && new=:$2:$4:$5:$6 &&
4903d2ed3e3Smrg       set +f &&
491b53e5eeaSmrg       test "$old" = "$new" &&
492b53e5eeaSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493b53e5eeaSmrg    then
494b53e5eeaSmrg      rm -f "$dsttmp"
495b53e5eeaSmrg    else
496e45ace2bSmrg      # If $backupsuffix is set, and the file being installed
497e45ace2bSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498e45ace2bSmrg      # e.g., if mv doesn't support -f.
499e45ace2bSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500e45ace2bSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501e45ace2bSmrg      fi
502e45ace2bSmrg
503b53e5eeaSmrg      # Rename the file to the real destination.
504b53e5eeaSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505b53e5eeaSmrg
506b53e5eeaSmrg      # The rename failed, perhaps because mv can't rename something else
507b53e5eeaSmrg      # to itself, or perhaps because mv is so ancient that it does not
508b53e5eeaSmrg      # support -f.
509b53e5eeaSmrg      {
5103d2ed3e3Smrg        # Now remove or move aside any old file at destination location.
5113d2ed3e3Smrg        # We try this two ways since rm can't unlink itself on some
5123d2ed3e3Smrg        # systems and the destination file might be busy for other
5133d2ed3e3Smrg        # reasons.  In this case, the final cleanup might fail but the new
5143d2ed3e3Smrg        # file should still install successfully.
5153d2ed3e3Smrg        {
5163d2ed3e3Smrg          test ! -f "$dst" ||
517e45ace2bSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
5183d2ed3e3Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519e45ace2bSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5203d2ed3e3Smrg          } ||
5213d2ed3e3Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5223d2ed3e3Smrg            (exit 1); exit 1
5233d2ed3e3Smrg          }
5243d2ed3e3Smrg        } &&
5253d2ed3e3Smrg
5263d2ed3e3Smrg        # Now rename the file to the real destination.
5273d2ed3e3Smrg        $doit $mvcmd "$dsttmp" "$dst"
528b53e5eeaSmrg      }
529b53e5eeaSmrg    fi || exit 1
53073143b9aSmrg
53173143b9aSmrg    trap '' 0
53273143b9aSmrg  fi
53373143b9aSmrgdone
53473143b9aSmrg
53573143b9aSmrg# Local variables:
536e45ace2bSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
53773143b9aSmrg# time-stamp-start: "scriptversion="
53873143b9aSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539e45ace2bSmrg# time-stamp-time-zone: "UTC0"
540b53e5eeaSmrg# time-stamp-end: "; # UTC"
54173143b9aSmrg# End:
542