132b578d3Smrg#!/bin/sh
232b578d3Smrg# install - install a program, script, or datafile
332b578d3Smrg
40b0ce0bfSmrgscriptversion=2020-11-14.01; # UTC
532b578d3Smrg
632b578d3Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
732b578d3Smrg# later released in X11R6 (xc/config/util/install.sh) with the
832b578d3Smrg# following copyright and license.
932b578d3Smrg#
1032b578d3Smrg# Copyright (C) 1994 X Consortium
1132b578d3Smrg#
1232b578d3Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1332b578d3Smrg# of this software and associated documentation files (the "Software"), to
1432b578d3Smrg# deal in the Software without restriction, including without limitation the
1532b578d3Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1632b578d3Smrg# sell copies of the Software, and to permit persons to whom the Software is
1732b578d3Smrg# furnished to do so, subject to the following conditions:
1832b578d3Smrg#
1932b578d3Smrg# The above copyright notice and this permission notice shall be included in
2032b578d3Smrg# all copies or substantial portions of the Software.
2132b578d3Smrg#
2232b578d3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2332b578d3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2432b578d3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2532b578d3Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2632b578d3Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2732b578d3Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2832b578d3Smrg#
2932b578d3Smrg# Except as contained in this notice, the name of the X Consortium shall not
3032b578d3Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3132b578d3Smrg# ings in this Software without prior written authorization from the X Consor-
3232b578d3Smrg# tium.
3332b578d3Smrg#
3432b578d3Smrg#
3532b578d3Smrg# FSF changes to this file are in the public domain.
3632b578d3Smrg#
3732b578d3Smrg# Calling this script install-sh is preferred over install.sh, to prevent
385a81ca49Smrg# 'make' implicit rules from creating a file called install from it
3932b578d3Smrg# when there is no Makefile.
4032b578d3Smrg#
4132b578d3Smrg# This script is compatible with the BSD install script, but was written
4232b578d3Smrg# from scratch.
4332b578d3Smrg
445a81ca49Smrgtab='	'
4532b578d3Smrgnl='
4632b578d3Smrg'
475a81ca49SmrgIFS=" $tab$nl"
4832b578d3Smrg
495a81ca49Smrg# Set DOITPROG to "echo" to test this script.
5032b578d3Smrg
512a51b5beSmrgdoit=${DOITPROG-}
525a81ca49Smrgdoit_exec=${doit:-exec}
5332b578d3Smrg
5432b578d3Smrg# Put in absolute file names if you don't have them in your path;
5532b578d3Smrg# or use environment vars.
5632b578d3Smrg
572a51b5beSmrgchgrpprog=${CHGRPPROG-chgrp}
582a51b5beSmrgchmodprog=${CHMODPROG-chmod}
592a51b5beSmrgchownprog=${CHOWNPROG-chown}
602a51b5beSmrgcmpprog=${CMPPROG-cmp}
612a51b5beSmrgcpprog=${CPPROG-cp}
622a51b5beSmrgmkdirprog=${MKDIRPROG-mkdir}
632a51b5beSmrgmvprog=${MVPROG-mv}
642a51b5beSmrgrmprog=${RMPROG-rm}
652a51b5beSmrgstripprog=${STRIPPROG-strip}
662a51b5beSmrg
6732b578d3Smrgposix_mkdir=
6832b578d3Smrg
6932b578d3Smrg# Desired mode of installed file.
7032b578d3Smrgmode=0755
7132b578d3Smrg
720b0ce0bfSmrg# Create dirs (including intermediate dirs) using mode 755.
730b0ce0bfSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
740b0ce0bfSmrgmkdir_umask=22
750b0ce0bfSmrg
760b0ce0bfSmrgbackupsuffix=
772a51b5beSmrgchgrpcmd=
7832b578d3Smrgchmodcmd=$chmodprog
7932b578d3Smrgchowncmd=
802a51b5beSmrgmvcmd=$mvprog
81d31dbc53Smrgrmcmd="$rmprog -f"
822a51b5beSmrgstripcmd=
832a51b5beSmrg
8432b578d3Smrgsrc=
8532b578d3Smrgdst=
8632b578d3Smrgdir_arg=
872a51b5beSmrgdst_arg=
882a51b5beSmrg
892a51b5beSmrgcopy_on_change=false
905a81ca49Smrgis_target_a_directory=possibly
9132b578d3Smrg
922a51b5beSmrgusage="\
932a51b5beSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9432b578d3Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9532b578d3Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9632b578d3Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9732b578d3Smrg
9832b578d3SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9932b578d3SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10032b578d3SmrgIn the 4th, create DIRECTORIES.
10132b578d3Smrg
10232b578d3SmrgOptions:
1032a51b5beSmrg     --help     display this help and exit.
1042a51b5beSmrg     --version  display version info and exit.
1052a51b5beSmrg
1062a51b5beSmrg  -c            (ignored)
1070b0ce0bfSmrg  -C            install only if different (preserve data modification time)
1082a51b5beSmrg  -d            create directories instead of installing files.
1092a51b5beSmrg  -g GROUP      $chgrpprog installed files to GROUP.
1102a51b5beSmrg  -m MODE       $chmodprog installed files to MODE.
1112a51b5beSmrg  -o USER       $chownprog installed files to USER.
1120b0ce0bfSmrg  -p            pass -p to $cpprog.
1132a51b5beSmrg  -s            $stripprog installed files.
1140b0ce0bfSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1152a51b5beSmrg  -t DIRECTORY  install into DIRECTORY.
1162a51b5beSmrg  -T            report an error if DSTFILE is a directory.
11732b578d3Smrg
11832b578d3SmrgEnvironment variables override the default commands:
1192a51b5beSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1202a51b5beSmrg  RMPROG STRIPPROG
1210b0ce0bfSmrg
1220b0ce0bfSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1230b0ce0bfSmrgit's up to you to specify -f if you want it.
1240b0ce0bfSmrg
1250b0ce0bfSmrgIf -S is not specified, no backups are attempted.
1260b0ce0bfSmrg
1270b0ce0bfSmrgEmail bug reports to bug-automake@gnu.org.
1280b0ce0bfSmrgAutomake home page: https://www.gnu.org/software/automake/
12932b578d3Smrg"
13032b578d3Smrg
13132b578d3Smrgwhile test $# -ne 0; do
13232b578d3Smrg  case $1 in
1332a51b5beSmrg    -c) ;;
1342a51b5beSmrg
1352a51b5beSmrg    -C) copy_on_change=true;;
13632b578d3Smrg
1372a51b5beSmrg    -d) dir_arg=true;;
13832b578d3Smrg
13932b578d3Smrg    -g) chgrpcmd="$chgrpprog $2"
1405a81ca49Smrg        shift;;
14132b578d3Smrg
14232b578d3Smrg    --help) echo "$usage"; exit $?;;
14332b578d3Smrg
14432b578d3Smrg    -m) mode=$2
1455a81ca49Smrg        case $mode in
1465a81ca49Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1475a81ca49Smrg            echo "$0: invalid mode: $mode" >&2
1485a81ca49Smrg            exit 1;;
1495a81ca49Smrg        esac
1505a81ca49Smrg        shift;;
15132b578d3Smrg
15232b578d3Smrg    -o) chowncmd="$chownprog $2"
1535a81ca49Smrg        shift;;
15432b578d3Smrg
1550b0ce0bfSmrg    -p) cpprog="$cpprog -p";;
1560b0ce0bfSmrg
1572a51b5beSmrg    -s) stripcmd=$stripprog;;
15832b578d3Smrg
1590b0ce0bfSmrg    -S) backupsuffix="$2"
1600b0ce0bfSmrg        shift;;
1610b0ce0bfSmrg
1625a81ca49Smrg    -t)
1635a81ca49Smrg        is_target_a_directory=always
1645a81ca49Smrg        dst_arg=$2
1655a81ca49Smrg        # Protect names problematic for 'test' and other utilities.
1665a81ca49Smrg        case $dst_arg in
1675a81ca49Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1685a81ca49Smrg        esac
1695a81ca49Smrg        shift;;
17032b578d3Smrg
1715a81ca49Smrg    -T) is_target_a_directory=never;;
17232b578d3Smrg
17332b578d3Smrg    --version) echo "$0 $scriptversion"; exit $?;;
17432b578d3Smrg
1755a81ca49Smrg    --) shift
1765a81ca49Smrg        break;;
17732b578d3Smrg
1785a81ca49Smrg    -*) echo "$0: invalid option: $1" >&2
1795a81ca49Smrg        exit 1;;
18032b578d3Smrg
18132b578d3Smrg    *)  break;;
18232b578d3Smrg  esac
1832a51b5beSmrg  shift
18432b578d3Smrgdone
18532b578d3Smrg
1865a81ca49Smrg# We allow the use of options -d and -T together, by making -d
1875a81ca49Smrg# take the precedence; this is for compatibility with GNU install.
1885a81ca49Smrg
1895a81ca49Smrgif test -n "$dir_arg"; then
1905a81ca49Smrg  if test -n "$dst_arg"; then
1915a81ca49Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1925a81ca49Smrg    exit 1
1935a81ca49Smrg  fi
1945a81ca49Smrgfi
1955a81ca49Smrg
1962a51b5beSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19732b578d3Smrg  # When -d is used, all remaining arguments are directories to create.
19832b578d3Smrg  # When -t is used, the destination is already specified.
19932b578d3Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20032b578d3Smrg  for arg
20132b578d3Smrg  do
2022a51b5beSmrg    if test -n "$dst_arg"; then
20332b578d3Smrg      # $@ is not empty: it contains at least $arg.
2042a51b5beSmrg      set fnord "$@" "$dst_arg"
20532b578d3Smrg      shift # fnord
20632b578d3Smrg    fi
20732b578d3Smrg    shift # arg
2082a51b5beSmrg    dst_arg=$arg
2095a81ca49Smrg    # Protect names problematic for 'test' and other utilities.
210e35d4d8eSmrg    case $dst_arg in
211e35d4d8eSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212e35d4d8eSmrg    esac
21332b578d3Smrg  done
21432b578d3Smrgfi
21532b578d3Smrg
21632b578d3Smrgif test $# -eq 0; then
21732b578d3Smrg  if test -z "$dir_arg"; then
21832b578d3Smrg    echo "$0: no input file specified." >&2
21932b578d3Smrg    exit 1
22032b578d3Smrg  fi
2215a81ca49Smrg  # It's OK to call 'install-sh -d' without argument.
22232b578d3Smrg  # This can happen when creating conditional directories.
22332b578d3Smrg  exit 0
22432b578d3Smrgfi
22532b578d3Smrg
2265a81ca49Smrgif test -z "$dir_arg"; then
2275a81ca49Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2285a81ca49Smrg    if test ! -d "$dst_arg"; then
2295a81ca49Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2305a81ca49Smrg      exit 1
2315a81ca49Smrg    fi
2325a81ca49Smrg  fi
2335a81ca49Smrgfi
2345a81ca49Smrg
23532b578d3Smrgif test -z "$dir_arg"; then
236e35d4d8eSmrg  do_exit='(exit $ret); exit $ret'
237e35d4d8eSmrg  trap "ret=129; $do_exit" 1
238e35d4d8eSmrg  trap "ret=130; $do_exit" 2
239e35d4d8eSmrg  trap "ret=141; $do_exit" 13
240e35d4d8eSmrg  trap "ret=143; $do_exit" 15
24132b578d3Smrg
24232b578d3Smrg  # Set umask so as not to create temps with too-generous modes.
24332b578d3Smrg  # However, 'strip' requires both read and write access to temps.
24432b578d3Smrg  case $mode in
24532b578d3Smrg    # Optimize common cases.
24632b578d3Smrg    *644) cp_umask=133;;
24732b578d3Smrg    *755) cp_umask=22;;
24832b578d3Smrg
24932b578d3Smrg    *[0-7])
25032b578d3Smrg      if test -z "$stripcmd"; then
2515a81ca49Smrg        u_plus_rw=
25232b578d3Smrg      else
2535a81ca49Smrg        u_plus_rw='% 200'
25432b578d3Smrg      fi
25532b578d3Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25632b578d3Smrg    *)
25732b578d3Smrg      if test -z "$stripcmd"; then
2585a81ca49Smrg        u_plus_rw=
25932b578d3Smrg      else
2605a81ca49Smrg        u_plus_rw=,u+rw
26132b578d3Smrg      fi
26232b578d3Smrg      cp_umask=$mode$u_plus_rw;;
26332b578d3Smrg  esac
26432b578d3Smrgfi
26532b578d3Smrg
26632b578d3Smrgfor src
26732b578d3Smrgdo
2685a81ca49Smrg  # Protect names problematic for 'test' and other utilities.
26932b578d3Smrg  case $src in
270e35d4d8eSmrg    -* | [=\(\)!]) src=./$src;;
27132b578d3Smrg  esac
27232b578d3Smrg
27332b578d3Smrg  if test -n "$dir_arg"; then
27432b578d3Smrg    dst=$src
27532b578d3Smrg    dstdir=$dst
27632b578d3Smrg    test -d "$dstdir"
27732b578d3Smrg    dstdir_status=$?
2780b0ce0bfSmrg    # Don't chown directories that already exist.
2790b0ce0bfSmrg    if test $dstdir_status = 0; then
2800b0ce0bfSmrg      chowncmd=""
2810b0ce0bfSmrg    fi
28232b578d3Smrg  else
28332b578d3Smrg
28432b578d3Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28532b578d3Smrg    # might cause directories to be created, which would be especially bad
28632b578d3Smrg    # if $src (and thus $dsttmp) contains '*'.
28732b578d3Smrg    if test ! -f "$src" && test ! -d "$src"; then
28832b578d3Smrg      echo "$0: $src does not exist." >&2
28932b578d3Smrg      exit 1
29032b578d3Smrg    fi
29132b578d3Smrg
2922a51b5beSmrg    if test -z "$dst_arg"; then
29332b578d3Smrg      echo "$0: no destination specified." >&2
29432b578d3Smrg      exit 1
29532b578d3Smrg    fi
2962a51b5beSmrg    dst=$dst_arg
29732b578d3Smrg
2980b0ce0bfSmrg    # If destination is a directory, append the input filename.
29932b578d3Smrg    if test -d "$dst"; then
3005a81ca49Smrg      if test "$is_target_a_directory" = never; then
3015a81ca49Smrg        echo "$0: $dst_arg: Is a directory" >&2
3025a81ca49Smrg        exit 1
30332b578d3Smrg      fi
30432b578d3Smrg      dstdir=$dst
3050b0ce0bfSmrg      dstbase=`basename "$src"`
3060b0ce0bfSmrg      case $dst in
3070b0ce0bfSmrg	*/) dst=$dst$dstbase;;
3080b0ce0bfSmrg	*)  dst=$dst/$dstbase;;
3090b0ce0bfSmrg      esac
31032b578d3Smrg      dstdir_status=0
31132b578d3Smrg    else
3125a81ca49Smrg      dstdir=`dirname "$dst"`
31332b578d3Smrg      test -d "$dstdir"
31432b578d3Smrg      dstdir_status=$?
31532b578d3Smrg    fi
31632b578d3Smrg  fi
31732b578d3Smrg
3180b0ce0bfSmrg  case $dstdir in
3190b0ce0bfSmrg    */) dstdirslash=$dstdir;;
3200b0ce0bfSmrg    *)  dstdirslash=$dstdir/;;
3210b0ce0bfSmrg  esac
3220b0ce0bfSmrg
32332b578d3Smrg  obsolete_mkdir_used=false
32432b578d3Smrg
32532b578d3Smrg  if test $dstdir_status != 0; then
32632b578d3Smrg    case $posix_mkdir in
32732b578d3Smrg      '')
3285a81ca49Smrg        # With -d, create the new directory with the user-specified mode.
3295a81ca49Smrg        # Otherwise, rely on $mkdir_umask.
3305a81ca49Smrg        if test -n "$dir_arg"; then
3315a81ca49Smrg          mkdir_mode=-m$mode
3325a81ca49Smrg        else
3335a81ca49Smrg          mkdir_mode=
3345a81ca49Smrg        fi
3355a81ca49Smrg
3365a81ca49Smrg        posix_mkdir=false
3370b0ce0bfSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3380b0ce0bfSmrg	# here however when possible just to lower collision chance.
3390b0ce0bfSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3400b0ce0bfSmrg
3410b0ce0bfSmrg	trap '
3420b0ce0bfSmrg	  ret=$?
3430b0ce0bfSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3440b0ce0bfSmrg	  exit $ret
3450b0ce0bfSmrg	' 0
3460b0ce0bfSmrg
3470b0ce0bfSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
3480b0ce0bfSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3490b0ce0bfSmrg	# directory is successfully created first before we actually test
3500b0ce0bfSmrg	# 'mkdir -p'.
3510b0ce0bfSmrg	if (umask $mkdir_umask &&
3520b0ce0bfSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3530b0ce0bfSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3540b0ce0bfSmrg	then
3550b0ce0bfSmrg	  if test -z "$dir_arg" || {
3560b0ce0bfSmrg	       # Check for POSIX incompatibilities with -m.
3570b0ce0bfSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3580b0ce0bfSmrg	       # other-writable bit of parent directory when it shouldn't.
3590b0ce0bfSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3600b0ce0bfSmrg	       test_tmpdir="$tmpdir/a"
3610b0ce0bfSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3620b0ce0bfSmrg	       case $ls_ld_tmpdir in
3630b0ce0bfSmrg		 d????-?r-*) different_mode=700;;
3640b0ce0bfSmrg		 d????-?--*) different_mode=755;;
3650b0ce0bfSmrg		 *) false;;
3660b0ce0bfSmrg	       esac &&
3670b0ce0bfSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3680b0ce0bfSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3690b0ce0bfSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3700b0ce0bfSmrg	       }
3710b0ce0bfSmrg	     }
3720b0ce0bfSmrg	  then posix_mkdir=:
3730b0ce0bfSmrg	  fi
3740b0ce0bfSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3750b0ce0bfSmrg	else
3760b0ce0bfSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
3770b0ce0bfSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3780b0ce0bfSmrg	fi
3790b0ce0bfSmrg	trap '' 0;;
38032b578d3Smrg    esac
38132b578d3Smrg
38232b578d3Smrg    if
38332b578d3Smrg      $posix_mkdir && (
3845a81ca49Smrg        umask $mkdir_umask &&
3855a81ca49Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38632b578d3Smrg      )
38732b578d3Smrg    then :
38832b578d3Smrg    else
38932b578d3Smrg
3900b0ce0bfSmrg      # mkdir does not conform to POSIX,
39132b578d3Smrg      # or it failed possibly due to a race condition.  Create the
39232b578d3Smrg      # directory the slow way, step by step, checking for races as we go.
39332b578d3Smrg
39432b578d3Smrg      case $dstdir in
3955a81ca49Smrg        /*) prefix='/';;
3965a81ca49Smrg        [-=\(\)!]*) prefix='./';;
3975a81ca49Smrg        *)  prefix='';;
39832b578d3Smrg      esac
39932b578d3Smrg
40032b578d3Smrg      oIFS=$IFS
40132b578d3Smrg      IFS=/
4025a81ca49Smrg      set -f
40332b578d3Smrg      set fnord $dstdir
40432b578d3Smrg      shift
4055a81ca49Smrg      set +f
40632b578d3Smrg      IFS=$oIFS
40732b578d3Smrg
40832b578d3Smrg      prefixes=
40932b578d3Smrg
41032b578d3Smrg      for d
41132b578d3Smrg      do
4125a81ca49Smrg        test X"$d" = X && continue
4135a81ca49Smrg
4145a81ca49Smrg        prefix=$prefix$d
4155a81ca49Smrg        if test -d "$prefix"; then
4165a81ca49Smrg          prefixes=
4175a81ca49Smrg        else
4185a81ca49Smrg          if $posix_mkdir; then
4190b0ce0bfSmrg            (umask $mkdir_umask &&
4205a81ca49Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4215a81ca49Smrg            # Don't fail if two instances are running concurrently.
4225a81ca49Smrg            test -d "$prefix" || exit 1
4235a81ca49Smrg          else
4245a81ca49Smrg            case $prefix in
4255a81ca49Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4265a81ca49Smrg              *) qprefix=$prefix;;
4275a81ca49Smrg            esac
4285a81ca49Smrg            prefixes="$prefixes '$qprefix'"
4295a81ca49Smrg          fi
4305a81ca49Smrg        fi
4315a81ca49Smrg        prefix=$prefix/
43232b578d3Smrg      done
43332b578d3Smrg
43432b578d3Smrg      if test -n "$prefixes"; then
4355a81ca49Smrg        # Don't fail if two instances are running concurrently.
4365a81ca49Smrg        (umask $mkdir_umask &&
4375a81ca49Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4385a81ca49Smrg          test -d "$dstdir" || exit 1
4395a81ca49Smrg        obsolete_mkdir_used=true
44032b578d3Smrg      fi
44132b578d3Smrg    fi
44232b578d3Smrg  fi
44332b578d3Smrg
44432b578d3Smrg  if test -n "$dir_arg"; then
44532b578d3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44632b578d3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44732b578d3Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44832b578d3Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44932b578d3Smrg  else
45032b578d3Smrg
45132b578d3Smrg    # Make a couple of temp file names in the proper directory.
4520b0ce0bfSmrg    dsttmp=${dstdirslash}_inst.$$_
4530b0ce0bfSmrg    rmtmp=${dstdirslash}_rm.$$_
45432b578d3Smrg
45532b578d3Smrg    # Trap to clean up those temp files at exit.
45632b578d3Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45732b578d3Smrg
45832b578d3Smrg    # Copy the file name to the temp name.
4590b0ce0bfSmrg    (umask $cp_umask &&
4600b0ce0bfSmrg     { test -z "$stripcmd" || {
4610b0ce0bfSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4620b0ce0bfSmrg	 # which would cause strip to fail.
4630b0ce0bfSmrg	 if test -z "$doit"; then
4640b0ce0bfSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4650b0ce0bfSmrg	 else
4660b0ce0bfSmrg	   $doit touch "$dsttmp"
4670b0ce0bfSmrg	 fi
4680b0ce0bfSmrg       }
4690b0ce0bfSmrg     } &&
4700b0ce0bfSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47132b578d3Smrg
47232b578d3Smrg    # and set any options; do chmod last to preserve setuid bits.
47332b578d3Smrg    #
47432b578d3Smrg    # If any of these fail, we abort the whole thing.  If we want to
47532b578d3Smrg    # ignore errors from any of these, just make sure not to ignore
47632b578d3Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47732b578d3Smrg    #
4782a51b5beSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4792a51b5beSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4802a51b5beSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4812a51b5beSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4822a51b5beSmrg
4832a51b5beSmrg    # If -C, don't bother to copy if it wouldn't change the file.
4842a51b5beSmrg    if $copy_on_change &&
4855a81ca49Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4865a81ca49Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4875a81ca49Smrg       set -f &&
4882a51b5beSmrg       set X $old && old=:$2:$4:$5:$6 &&
4892a51b5beSmrg       set X $new && new=:$2:$4:$5:$6 &&
4905a81ca49Smrg       set +f &&
4912a51b5beSmrg       test "$old" = "$new" &&
4922a51b5beSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4932a51b5beSmrg    then
4942a51b5beSmrg      rm -f "$dsttmp"
4952a51b5beSmrg    else
4960b0ce0bfSmrg      # If $backupsuffix is set, and the file being installed
4970b0ce0bfSmrg      # already exists, attempt a backup.  Don't worry if it fails,
4980b0ce0bfSmrg      # e.g., if mv doesn't support -f.
4990b0ce0bfSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
5000b0ce0bfSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5010b0ce0bfSmrg      fi
5020b0ce0bfSmrg
5032a51b5beSmrg      # Rename the file to the real destination.
5042a51b5beSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5052a51b5beSmrg
5062a51b5beSmrg      # The rename failed, perhaps because mv can't rename something else
5072a51b5beSmrg      # to itself, or perhaps because mv is so ancient that it does not
5082a51b5beSmrg      # support -f.
5092a51b5beSmrg      {
5105a81ca49Smrg        # Now remove or move aside any old file at destination location.
5115a81ca49Smrg        # We try this two ways since rm can't unlink itself on some
5125a81ca49Smrg        # systems and the destination file might be busy for other
5135a81ca49Smrg        # reasons.  In this case, the final cleanup might fail but the new
5145a81ca49Smrg        # file should still install successfully.
5155a81ca49Smrg        {
5165a81ca49Smrg          test ! -f "$dst" ||
5170b0ce0bfSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
5185a81ca49Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5190b0ce0bfSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5205a81ca49Smrg          } ||
5215a81ca49Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5225a81ca49Smrg            (exit 1); exit 1
5235a81ca49Smrg          }
5245a81ca49Smrg        } &&
5255a81ca49Smrg
5265a81ca49Smrg        # Now rename the file to the real destination.
5275a81ca49Smrg        $doit $mvcmd "$dsttmp" "$dst"
5282a51b5beSmrg      }
5292a51b5beSmrg    fi || exit 1
53032b578d3Smrg
53132b578d3Smrg    trap '' 0
53232b578d3Smrg  fi
53332b578d3Smrgdone
53432b578d3Smrg
53532b578d3Smrg# Local variables:
5360b0ce0bfSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
53732b578d3Smrg# time-stamp-start: "scriptversion="
53832b578d3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5390b0ce0bfSmrg# time-stamp-time-zone: "UTC0"
5402a51b5beSmrg# time-stamp-end: "; # UTC"
54132b578d3Smrg# End:
542