install-sh revision f6d57fde
17a0395d0Smrg#!/bin/sh
27a0395d0Smrg# install - install a program, script, or datafile
37a0395d0Smrg
4f6d57fdeSmrgscriptversion=2024-06-19.01; # UTC
57a0395d0Smrg
67a0395d0Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
77a0395d0Smrg# later released in X11R6 (xc/config/util/install.sh) with the
87a0395d0Smrg# following copyright and license.
97a0395d0Smrg#
107a0395d0Smrg# Copyright (C) 1994 X Consortium
117a0395d0Smrg#
127a0395d0Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
137a0395d0Smrg# of this software and associated documentation files (the "Software"), to
147a0395d0Smrg# deal in the Software without restriction, including without limitation the
157a0395d0Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
167a0395d0Smrg# sell copies of the Software, and to permit persons to whom the Software is
177a0395d0Smrg# furnished to do so, subject to the following conditions:
187a0395d0Smrg#
197a0395d0Smrg# The above copyright notice and this permission notice shall be included in
207a0395d0Smrg# all copies or substantial portions of the Software.
217a0395d0Smrg#
227a0395d0Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237a0395d0Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247a0395d0Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
257a0395d0Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
267a0395d0Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
277a0395d0Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
287a0395d0Smrg#
297a0395d0Smrg# Except as contained in this notice, the name of the X Consortium shall not
307a0395d0Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
317a0395d0Smrg# ings in this Software without prior written authorization from the X Consor-
327a0395d0Smrg# tium.
337a0395d0Smrg#
347a0395d0Smrg#
357a0395d0Smrg# FSF changes to this file are in the public domain.
367a0395d0Smrg#
377a0395d0Smrg# Calling this script install-sh is preferred over install.sh, to prevent
388abc0ccfSmrg# 'make' implicit rules from creating a file called install from it
397a0395d0Smrg# when there is no Makefile.
407a0395d0Smrg#
417a0395d0Smrg# This script is compatible with the BSD install script, but was written
427a0395d0Smrg# from scratch.
437a0395d0Smrg
4440c5344fSmrgtab='	'
457a0395d0Smrgnl='
467a0395d0Smrg'
4740c5344fSmrgIFS=" $tab$nl"
487a0395d0Smrg
4940c5344fSmrg# Set DOITPROG to "echo" to test this script.
507a0395d0Smrg
517a0395d0Smrgdoit=${DOITPROG-}
5240c5344fSmrgdoit_exec=${doit:-exec}
537a0395d0Smrg
547a0395d0Smrg# Put in absolute file names if you don't have them in your path;
557a0395d0Smrg# or use environment vars.
567a0395d0Smrg
577a0395d0Smrgchgrpprog=${CHGRPPROG-chgrp}
587a0395d0Smrgchmodprog=${CHMODPROG-chmod}
597a0395d0Smrgchownprog=${CHOWNPROG-chown}
607a0395d0Smrgcmpprog=${CMPPROG-cmp}
617a0395d0Smrgcpprog=${CPPROG-cp}
627a0395d0Smrgmkdirprog=${MKDIRPROG-mkdir}
637a0395d0Smrgmvprog=${MVPROG-mv}
647a0395d0Smrgrmprog=${RMPROG-rm}
657a0395d0Smrgstripprog=${STRIPPROG-strip}
667a0395d0Smrg
677a0395d0Smrgposix_mkdir=
687a0395d0Smrg
697a0395d0Smrg# Desired mode of installed file.
707a0395d0Smrgmode=0755
717a0395d0Smrg
720d22642bSmrg# Create dirs (including intermediate dirs) using mode 755.
730d22642bSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
740d22642bSmrgmkdir_umask=22
750d22642bSmrg
760d22642bSmrgbackupsuffix=
777a0395d0Smrgchgrpcmd=
787a0395d0Smrgchmodcmd=$chmodprog
797a0395d0Smrgchowncmd=
807a0395d0Smrgmvcmd=$mvprog
817a0395d0Smrgrmcmd="$rmprog -f"
827a0395d0Smrgstripcmd=
837a0395d0Smrg
847a0395d0Smrgsrc=
857a0395d0Smrgdst=
867a0395d0Smrgdir_arg=
877a0395d0Smrgdst_arg=
887a0395d0Smrg
897a0395d0Smrgcopy_on_change=false
9040c5344fSmrgis_target_a_directory=possibly
917a0395d0Smrg
927a0395d0Smrgusage="\
937a0395d0SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
947a0395d0Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
957a0395d0Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
967a0395d0Smrg   or: $0 [OPTION]... -d DIRECTORIES...
977a0395d0Smrg
987a0395d0SmrgIn the 1st form, copy SRCFILE to DSTFILE.
997a0395d0SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1007a0395d0SmrgIn the 4th, create DIRECTORIES.
1017a0395d0Smrg
1027a0395d0SmrgOptions:
1037a0395d0Smrg     --help     display this help and exit.
1047a0395d0Smrg     --version  display version info and exit.
1057a0395d0Smrg
1067a0395d0Smrg  -c            (ignored)
1070d22642bSmrg  -C            install only if different (preserve data modification time)
1087a0395d0Smrg  -d            create directories instead of installing files.
1097a0395d0Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1107a0395d0Smrg  -m MODE       $chmodprog installed files to MODE.
1117a0395d0Smrg  -o USER       $chownprog installed files to USER.
1120d22642bSmrg  -p            pass -p to $cpprog.
1137a0395d0Smrg  -s            $stripprog installed files.
1140d22642bSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1157a0395d0Smrg  -t DIRECTORY  install into DIRECTORY.
1167a0395d0Smrg  -T            report an error if DSTFILE is a directory.
1177a0395d0Smrg
1187a0395d0SmrgEnvironment variables override the default commands:
1197a0395d0Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1207a0395d0Smrg  RMPROG STRIPPROG
1210d22642bSmrg
1220d22642bSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1230d22642bSmrgit's up to you to specify -f if you want it.
1240d22642bSmrg
1250d22642bSmrgIf -S is not specified, no backups are attempted.
1260d22642bSmrg
127f6d57fdeSmrgReport bugs to <bug-automake@gnu.org>.
128f6d57fdeSmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
129f6d57fdeSmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>."
1307a0395d0Smrg
1317a0395d0Smrgwhile test $# -ne 0; do
1327a0395d0Smrg  case $1 in
1337a0395d0Smrg    -c) ;;
1347a0395d0Smrg
1357a0395d0Smrg    -C) copy_on_change=true;;
1367a0395d0Smrg
1377a0395d0Smrg    -d) dir_arg=true;;
1387a0395d0Smrg
1397a0395d0Smrg    -g) chgrpcmd="$chgrpprog $2"
14040c5344fSmrg        shift;;
1417a0395d0Smrg
1427a0395d0Smrg    --help) echo "$usage"; exit $?;;
1437a0395d0Smrg
1447a0395d0Smrg    -m) mode=$2
14540c5344fSmrg        case $mode in
14640c5344fSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14740c5344fSmrg            echo "$0: invalid mode: $mode" >&2
14840c5344fSmrg            exit 1;;
14940c5344fSmrg        esac
15040c5344fSmrg        shift;;
1517a0395d0Smrg
1527a0395d0Smrg    -o) chowncmd="$chownprog $2"
15340c5344fSmrg        shift;;
1547a0395d0Smrg
1550d22642bSmrg    -p) cpprog="$cpprog -p";;
1560d22642bSmrg
1577a0395d0Smrg    -s) stripcmd=$stripprog;;
1587a0395d0Smrg
1590d22642bSmrg    -S) backupsuffix="$2"
1600d22642bSmrg        shift;;
1610d22642bSmrg
16240c5344fSmrg    -t)
16340c5344fSmrg        is_target_a_directory=always
16440c5344fSmrg        dst_arg=$2
16540c5344fSmrg        # Protect names problematic for 'test' and other utilities.
16640c5344fSmrg        case $dst_arg in
16740c5344fSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16840c5344fSmrg        esac
16940c5344fSmrg        shift;;
1707a0395d0Smrg
17140c5344fSmrg    -T) is_target_a_directory=never;;
1727a0395d0Smrg
173f6d57fdeSmrg    --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;;
1747a0395d0Smrg
17540c5344fSmrg    --) shift
17640c5344fSmrg        break;;
1777a0395d0Smrg
17840c5344fSmrg    -*) echo "$0: invalid option: $1" >&2
17940c5344fSmrg        exit 1;;
1807a0395d0Smrg
1817a0395d0Smrg    *)  break;;
1827a0395d0Smrg  esac
1837a0395d0Smrg  shift
1847a0395d0Smrgdone
1857a0395d0Smrg
18640c5344fSmrg# We allow the use of options -d and -T together, by making -d
18740c5344fSmrg# take the precedence; this is for compatibility with GNU install.
18840c5344fSmrg
18940c5344fSmrgif test -n "$dir_arg"; then
19040c5344fSmrg  if test -n "$dst_arg"; then
19140c5344fSmrg    echo "$0: target directory not allowed when installing a directory." >&2
19240c5344fSmrg    exit 1
19340c5344fSmrg  fi
19440c5344fSmrgfi
19540c5344fSmrg
1967a0395d0Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1977a0395d0Smrg  # When -d is used, all remaining arguments are directories to create.
1987a0395d0Smrg  # When -t is used, the destination is already specified.
1997a0395d0Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2007a0395d0Smrg  for arg
2017a0395d0Smrg  do
2027a0395d0Smrg    if test -n "$dst_arg"; then
2037a0395d0Smrg      # $@ is not empty: it contains at least $arg.
2047a0395d0Smrg      set fnord "$@" "$dst_arg"
2057a0395d0Smrg      shift # fnord
2067a0395d0Smrg    fi
2077a0395d0Smrg    shift # arg
2087a0395d0Smrg    dst_arg=$arg
2098abc0ccfSmrg    # Protect names problematic for 'test' and other utilities.
2108abc0ccfSmrg    case $dst_arg in
2118abc0ccfSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2128abc0ccfSmrg    esac
2137a0395d0Smrg  done
2147a0395d0Smrgfi
2157a0395d0Smrg
2167a0395d0Smrgif test $# -eq 0; then
2177a0395d0Smrg  if test -z "$dir_arg"; then
2187a0395d0Smrg    echo "$0: no input file specified." >&2
2197a0395d0Smrg    exit 1
2207a0395d0Smrg  fi
2218abc0ccfSmrg  # It's OK to call 'install-sh -d' without argument.
2227a0395d0Smrg  # This can happen when creating conditional directories.
2237a0395d0Smrg  exit 0
2247a0395d0Smrgfi
2257a0395d0Smrg
22640c5344fSmrgif test -z "$dir_arg"; then
22740c5344fSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22840c5344fSmrg    if test ! -d "$dst_arg"; then
22940c5344fSmrg      echo "$0: $dst_arg: Is not a directory." >&2
23040c5344fSmrg      exit 1
23140c5344fSmrg    fi
23240c5344fSmrg  fi
23340c5344fSmrgfi
23440c5344fSmrg
2357a0395d0Smrgif test -z "$dir_arg"; then
2368abc0ccfSmrg  do_exit='(exit $ret); exit $ret'
2378abc0ccfSmrg  trap "ret=129; $do_exit" 1
2388abc0ccfSmrg  trap "ret=130; $do_exit" 2
2398abc0ccfSmrg  trap "ret=141; $do_exit" 13
2408abc0ccfSmrg  trap "ret=143; $do_exit" 15
2417a0395d0Smrg
2427a0395d0Smrg  # Set umask so as not to create temps with too-generous modes.
2437a0395d0Smrg  # However, 'strip' requires both read and write access to temps.
2447a0395d0Smrg  case $mode in
2457a0395d0Smrg    # Optimize common cases.
2467a0395d0Smrg    *644) cp_umask=133;;
2477a0395d0Smrg    *755) cp_umask=22;;
2487a0395d0Smrg
2497a0395d0Smrg    *[0-7])
2507a0395d0Smrg      if test -z "$stripcmd"; then
25140c5344fSmrg        u_plus_rw=
2527a0395d0Smrg      else
25340c5344fSmrg        u_plus_rw='% 200'
2547a0395d0Smrg      fi
2557a0395d0Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2567a0395d0Smrg    *)
2577a0395d0Smrg      if test -z "$stripcmd"; then
25840c5344fSmrg        u_plus_rw=
2597a0395d0Smrg      else
26040c5344fSmrg        u_plus_rw=,u+rw
2617a0395d0Smrg      fi
2627a0395d0Smrg      cp_umask=$mode$u_plus_rw;;
2637a0395d0Smrg  esac
2647a0395d0Smrgfi
2657a0395d0Smrg
2667a0395d0Smrgfor src
2677a0395d0Smrgdo
2688abc0ccfSmrg  # Protect names problematic for 'test' and other utilities.
2697a0395d0Smrg  case $src in
2708abc0ccfSmrg    -* | [=\(\)!]) src=./$src;;
2717a0395d0Smrg  esac
2727a0395d0Smrg
2737a0395d0Smrg  if test -n "$dir_arg"; then
2747a0395d0Smrg    dst=$src
2757a0395d0Smrg    dstdir=$dst
2767a0395d0Smrg    test -d "$dstdir"
2777a0395d0Smrg    dstdir_status=$?
2780d22642bSmrg    # Don't chown directories that already exist.
2790d22642bSmrg    if test $dstdir_status = 0; then
2800d22642bSmrg      chowncmd=""
2810d22642bSmrg    fi
2827a0395d0Smrg  else
2837a0395d0Smrg
2847a0395d0Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2857a0395d0Smrg    # might cause directories to be created, which would be especially bad
2867a0395d0Smrg    # if $src (and thus $dsttmp) contains '*'.
2877a0395d0Smrg    if test ! -f "$src" && test ! -d "$src"; then
2887a0395d0Smrg      echo "$0: $src does not exist." >&2
2897a0395d0Smrg      exit 1
2907a0395d0Smrg    fi
2917a0395d0Smrg
2927a0395d0Smrg    if test -z "$dst_arg"; then
2937a0395d0Smrg      echo "$0: no destination specified." >&2
2947a0395d0Smrg      exit 1
2957a0395d0Smrg    fi
2967a0395d0Smrg    dst=$dst_arg
2977a0395d0Smrg
2986c3c2bceSmrg    # If destination is a directory, append the input filename.
2997a0395d0Smrg    if test -d "$dst"; then
30040c5344fSmrg      if test "$is_target_a_directory" = never; then
30140c5344fSmrg        echo "$0: $dst_arg: Is a directory" >&2
30240c5344fSmrg        exit 1
3037a0395d0Smrg      fi
3047a0395d0Smrg      dstdir=$dst
3056c3c2bceSmrg      dstbase=`basename "$src"`
3066c3c2bceSmrg      case $dst in
3076c3c2bceSmrg	*/) dst=$dst$dstbase;;
3086c3c2bceSmrg	*)  dst=$dst/$dstbase;;
3096c3c2bceSmrg      esac
3107a0395d0Smrg      dstdir_status=0
3117a0395d0Smrg    else
31240c5344fSmrg      dstdir=`dirname "$dst"`
3137a0395d0Smrg      test -d "$dstdir"
3147a0395d0Smrg      dstdir_status=$?
3157a0395d0Smrg    fi
3167a0395d0Smrg  fi
3177a0395d0Smrg
3186c3c2bceSmrg  case $dstdir in
3196c3c2bceSmrg    */) dstdirslash=$dstdir;;
3206c3c2bceSmrg    *)  dstdirslash=$dstdir/;;
3216c3c2bceSmrg  esac
3226c3c2bceSmrg
3237a0395d0Smrg  obsolete_mkdir_used=false
3247a0395d0Smrg
3257a0395d0Smrg  if test $dstdir_status != 0; then
3267a0395d0Smrg    case $posix_mkdir in
3277a0395d0Smrg      '')
32840c5344fSmrg        # With -d, create the new directory with the user-specified mode.
32940c5344fSmrg        # Otherwise, rely on $mkdir_umask.
33040c5344fSmrg        if test -n "$dir_arg"; then
33140c5344fSmrg          mkdir_mode=-m$mode
33240c5344fSmrg        else
33340c5344fSmrg          mkdir_mode=
33440c5344fSmrg        fi
33540c5344fSmrg
33640c5344fSmrg        posix_mkdir=false
3370d22642bSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3380d22642bSmrg	# here however when possible just to lower collision chance.
3390d22642bSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3400d22642bSmrg
3410d22642bSmrg	trap '
3420d22642bSmrg	  ret=$?
3430d22642bSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3440d22642bSmrg	  exit $ret
3450d22642bSmrg	' 0
3460d22642bSmrg
3470d22642bSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
348f6d57fdeSmrg	# directly in world-writable /tmp, make sure that the '$tmpdir'
3490d22642bSmrg	# directory is successfully created first before we actually test
3500d22642bSmrg	# 'mkdir -p'.
3510d22642bSmrg	if (umask $mkdir_umask &&
3520d22642bSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3530d22642bSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3540d22642bSmrg	then
3550d22642bSmrg	  if test -z "$dir_arg" || {
356f6d57fdeSmrg	       # Check for POSIX incompatibility with -m.
3570d22642bSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3580d22642bSmrg	       # other-writable bit of parent directory when it shouldn't.
3590d22642bSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3600d22642bSmrg	       test_tmpdir="$tmpdir/a"
3610d22642bSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3620d22642bSmrg	       case $ls_ld_tmpdir in
3630d22642bSmrg		 d????-?r-*) different_mode=700;;
3640d22642bSmrg		 d????-?--*) different_mode=755;;
3650d22642bSmrg		 *) false;;
3660d22642bSmrg	       esac &&
3670d22642bSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3680d22642bSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3690d22642bSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3700d22642bSmrg	       }
3710d22642bSmrg	     }
3720d22642bSmrg	  then posix_mkdir=:
3730d22642bSmrg	  fi
3740d22642bSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3750d22642bSmrg	else
3760d22642bSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
3770d22642bSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3780d22642bSmrg	fi
3790d22642bSmrg	trap '' 0;;
3807a0395d0Smrg    esac
3817a0395d0Smrg
3827a0395d0Smrg    if
3837a0395d0Smrg      $posix_mkdir && (
38440c5344fSmrg        umask $mkdir_umask &&
38540c5344fSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3867a0395d0Smrg      )
3877a0395d0Smrg    then :
3887a0395d0Smrg    else
3897a0395d0Smrg
3900d22642bSmrg      # mkdir does not conform to POSIX,
3917a0395d0Smrg      # or it failed possibly due to a race condition.  Create the
3927a0395d0Smrg      # directory the slow way, step by step, checking for races as we go.
3937a0395d0Smrg
3947a0395d0Smrg      case $dstdir in
39540c5344fSmrg        /*) prefix='/';;
39640c5344fSmrg        [-=\(\)!]*) prefix='./';;
39740c5344fSmrg        *)  prefix='';;
3987a0395d0Smrg      esac
3997a0395d0Smrg
4007a0395d0Smrg      oIFS=$IFS
4017a0395d0Smrg      IFS=/
40240c5344fSmrg      set -f
4037a0395d0Smrg      set fnord $dstdir
4047a0395d0Smrg      shift
40540c5344fSmrg      set +f
4067a0395d0Smrg      IFS=$oIFS
4077a0395d0Smrg
4087a0395d0Smrg      prefixes=
4097a0395d0Smrg
4107a0395d0Smrg      for d
4117a0395d0Smrg      do
41240c5344fSmrg        test X"$d" = X && continue
41340c5344fSmrg
41440c5344fSmrg        prefix=$prefix$d
41540c5344fSmrg        if test -d "$prefix"; then
41640c5344fSmrg          prefixes=
41740c5344fSmrg        else
41840c5344fSmrg          if $posix_mkdir; then
4190d22642bSmrg            (umask $mkdir_umask &&
42040c5344fSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42140c5344fSmrg            # Don't fail if two instances are running concurrently.
42240c5344fSmrg            test -d "$prefix" || exit 1
42340c5344fSmrg          else
42440c5344fSmrg            case $prefix in
42540c5344fSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42640c5344fSmrg              *) qprefix=$prefix;;
42740c5344fSmrg            esac
42840c5344fSmrg            prefixes="$prefixes '$qprefix'"
42940c5344fSmrg          fi
43040c5344fSmrg        fi
43140c5344fSmrg        prefix=$prefix/
4327a0395d0Smrg      done
4337a0395d0Smrg
4347a0395d0Smrg      if test -n "$prefixes"; then
43540c5344fSmrg        # Don't fail if two instances are running concurrently.
43640c5344fSmrg        (umask $mkdir_umask &&
43740c5344fSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43840c5344fSmrg          test -d "$dstdir" || exit 1
43940c5344fSmrg        obsolete_mkdir_used=true
4407a0395d0Smrg      fi
4417a0395d0Smrg    fi
4427a0395d0Smrg  fi
4437a0395d0Smrg
4447a0395d0Smrg  if test -n "$dir_arg"; then
4457a0395d0Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4467a0395d0Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4477a0395d0Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4487a0395d0Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4497a0395d0Smrg  else
4507a0395d0Smrg
4517a0395d0Smrg    # Make a couple of temp file names in the proper directory.
4526c3c2bceSmrg    dsttmp=${dstdirslash}_inst.$$_
4536c3c2bceSmrg    rmtmp=${dstdirslash}_rm.$$_
4547a0395d0Smrg
4557a0395d0Smrg    # Trap to clean up those temp files at exit.
4567a0395d0Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4577a0395d0Smrg
4587a0395d0Smrg    # Copy the file name to the temp name.
4590d22642bSmrg    (umask $cp_umask &&
4600d22642bSmrg     { test -z "$stripcmd" || {
4610d22642bSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4620d22642bSmrg	 # which would cause strip to fail.
4630d22642bSmrg	 if test -z "$doit"; then
4640d22642bSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4650d22642bSmrg	 else
4660d22642bSmrg	   $doit touch "$dsttmp"
4670d22642bSmrg	 fi
4680d22642bSmrg       }
4690d22642bSmrg     } &&
4700d22642bSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4717a0395d0Smrg
4727a0395d0Smrg    # and set any options; do chmod last to preserve setuid bits.
4737a0395d0Smrg    #
4747a0395d0Smrg    # If any of these fail, we abort the whole thing.  If we want to
4757a0395d0Smrg    # ignore errors from any of these, just make sure not to ignore
4767a0395d0Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4777a0395d0Smrg    #
4787a0395d0Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4797a0395d0Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4807a0395d0Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4817a0395d0Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4827a0395d0Smrg
4837a0395d0Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4847a0395d0Smrg    if $copy_on_change &&
48540c5344fSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48640c5344fSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48740c5344fSmrg       set -f &&
4887a0395d0Smrg       set X $old && old=:$2:$4:$5:$6 &&
4897a0395d0Smrg       set X $new && new=:$2:$4:$5:$6 &&
49040c5344fSmrg       set +f &&
4917a0395d0Smrg       test "$old" = "$new" &&
4927a0395d0Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4937a0395d0Smrg    then
4947a0395d0Smrg      rm -f "$dsttmp"
4957a0395d0Smrg    else
4960d22642bSmrg      # If $backupsuffix is set, and the file being installed
4970d22642bSmrg      # already exists, attempt a backup.  Don't worry if it fails,
4980d22642bSmrg      # e.g., if mv doesn't support -f.
4990d22642bSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
5000d22642bSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5010d22642bSmrg      fi
5020d22642bSmrg
5037a0395d0Smrg      # Rename the file to the real destination.
5047a0395d0Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5057a0395d0Smrg
5067a0395d0Smrg      # The rename failed, perhaps because mv can't rename something else
5077a0395d0Smrg      # to itself, or perhaps because mv is so ancient that it does not
5087a0395d0Smrg      # support -f.
5097a0395d0Smrg      {
51040c5344fSmrg        # Now remove or move aside any old file at destination location.
51140c5344fSmrg        # We try this two ways since rm can't unlink itself on some
51240c5344fSmrg        # systems and the destination file might be busy for other
51340c5344fSmrg        # reasons.  In this case, the final cleanup might fail but the new
51440c5344fSmrg        # file should still install successfully.
51540c5344fSmrg        {
51640c5344fSmrg          test ! -f "$dst" ||
5170d22642bSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
51840c5344fSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5190d22642bSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52040c5344fSmrg          } ||
52140c5344fSmrg          { echo "$0: cannot unlink or rename $dst" >&2
52240c5344fSmrg            (exit 1); exit 1
52340c5344fSmrg          }
52440c5344fSmrg        } &&
52540c5344fSmrg
52640c5344fSmrg        # Now rename the file to the real destination.
52740c5344fSmrg        $doit $mvcmd "$dsttmp" "$dst"
5287a0395d0Smrg      }
5297a0395d0Smrg    fi || exit 1
5307a0395d0Smrg
5317a0395d0Smrg    trap '' 0
5327a0395d0Smrg  fi
5337a0395d0Smrgdone
5347a0395d0Smrg
5357a0395d0Smrg# Local variables:
5366c3c2bceSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
5377a0395d0Smrg# time-stamp-start: "scriptversion="
5387a0395d0Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5396c3c2bceSmrg# time-stamp-time-zone: "UTC0"
5407366012aSmrg# time-stamp-end: "; # UTC"
5417a0395d0Smrg# End:
542