10e20ee16Smrg#!/bin/sh
20e20ee16Smrg# install - install a program, script, or datafile
33e6697d4Smrg
48b648e79Smrgscriptversion=2024-06-19.01; # UTC
53e6697d4Smrg
60e20ee16Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
70e20ee16Smrg# later released in X11R6 (xc/config/util/install.sh) with the
80e20ee16Smrg# following copyright and license.
90e20ee16Smrg#
100e20ee16Smrg# Copyright (C) 1994 X Consortium
110e20ee16Smrg#
120e20ee16Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
130e20ee16Smrg# of this software and associated documentation files (the "Software"), to
140e20ee16Smrg# deal in the Software without restriction, including without limitation the
150e20ee16Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
160e20ee16Smrg# sell copies of the Software, and to permit persons to whom the Software is
170e20ee16Smrg# furnished to do so, subject to the following conditions:
180e20ee16Smrg#
190e20ee16Smrg# The above copyright notice and this permission notice shall be included in
200e20ee16Smrg# all copies or substantial portions of the Software.
210e20ee16Smrg#
220e20ee16Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
230e20ee16Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
240e20ee16Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
250e20ee16Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
260e20ee16Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
270e20ee16Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
280e20ee16Smrg#
290e20ee16Smrg# Except as contained in this notice, the name of the X Consortium shall not
300e20ee16Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
310e20ee16Smrg# ings in this Software without prior written authorization from the X Consor-
320e20ee16Smrg# tium.
330e20ee16Smrg#
340e20ee16Smrg#
350e20ee16Smrg# FSF changes to this file are in the public domain.
360e20ee16Smrg#
370e20ee16Smrg# Calling this script install-sh is preferred over install.sh, to prevent
382f167a42Smrg# 'make' implicit rules from creating a file called install from it
390e20ee16Smrg# when there is no Makefile.
400e20ee16Smrg#
410e20ee16Smrg# This script is compatible with the BSD install script, but was written
423e6697d4Smrg# from scratch.
430e20ee16Smrg
442f167a42Smrgtab='	'
453e6697d4Smrgnl='
463e6697d4Smrg'
472f167a42SmrgIFS=" $tab$nl"
480e20ee16Smrg
492f167a42Smrg# Set DOITPROG to "echo" to test this script.
500e20ee16Smrg
513e6697d4Smrgdoit=${DOITPROG-}
522f167a42Smrgdoit_exec=${doit:-exec}
530e20ee16Smrg
543e6697d4Smrg# Put in absolute file names if you don't have them in your path;
553e6697d4Smrg# or use environment vars.
563e6697d4Smrg
573e6697d4Smrgchgrpprog=${CHGRPPROG-chgrp}
583e6697d4Smrgchmodprog=${CHMODPROG-chmod}
593e6697d4Smrgchownprog=${CHOWNPROG-chown}
603e6697d4Smrgcmpprog=${CMPPROG-cmp}
613e6697d4Smrgcpprog=${CPPROG-cp}
623e6697d4Smrgmkdirprog=${MKDIRPROG-mkdir}
633e6697d4Smrgmvprog=${MVPROG-mv}
643e6697d4Smrgrmprog=${RMPROG-rm}
653e6697d4Smrgstripprog=${STRIPPROG-strip}
663e6697d4Smrg
673e6697d4Smrgposix_mkdir=
683e6697d4Smrg
693e6697d4Smrg# Desired mode of installed file.
703e6697d4Smrgmode=0755
713e6697d4Smrg
72e6bced22Smrg# Create dirs (including intermediate dirs) using mode 755.
73e6bced22Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74e6bced22Smrgmkdir_umask=22
75e6bced22Smrg
76e6bced22Smrgbackupsuffix=
773e6697d4Smrgchgrpcmd=
783e6697d4Smrgchmodcmd=$chmodprog
793e6697d4Smrgchowncmd=
803e6697d4Smrgmvcmd=$mvprog
813e6697d4Smrgrmcmd="$rmprog -f"
823e6697d4Smrgstripcmd=
830e20ee16Smrg
843e6697d4Smrgsrc=
853e6697d4Smrgdst=
863e6697d4Smrgdir_arg=
873e6697d4Smrgdst_arg=
880e20ee16Smrg
893e6697d4Smrgcopy_on_change=false
902f167a42Smrgis_target_a_directory=possibly
910e20ee16Smrg
923e6697d4Smrgusage="\
933e6697d4SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
943e6697d4Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
953e6697d4Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
963e6697d4Smrg   or: $0 [OPTION]... -d DIRECTORIES...
970e20ee16Smrg
983e6697d4SmrgIn the 1st form, copy SRCFILE to DSTFILE.
993e6697d4SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1003e6697d4SmrgIn the 4th, create DIRECTORIES.
1010e20ee16Smrg
1023e6697d4SmrgOptions:
1033e6697d4Smrg     --help     display this help and exit.
1043e6697d4Smrg     --version  display version info and exit.
1050e20ee16Smrg
1063e6697d4Smrg  -c            (ignored)
107e6bced22Smrg  -C            install only if different (preserve data modification time)
1083e6697d4Smrg  -d            create directories instead of installing files.
1093e6697d4Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1103e6697d4Smrg  -m MODE       $chmodprog installed files to MODE.
1113e6697d4Smrg  -o USER       $chownprog installed files to USER.
112e6bced22Smrg  -p            pass -p to $cpprog.
1133e6697d4Smrg  -s            $stripprog installed files.
114e6bced22Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1153e6697d4Smrg  -t DIRECTORY  install into DIRECTORY.
1163e6697d4Smrg  -T            report an error if DSTFILE is a directory.
1170e20ee16Smrg
1183e6697d4SmrgEnvironment variables override the default commands:
1193e6697d4Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1203e6697d4Smrg  RMPROG STRIPPROG
121e6bced22Smrg
122e6bced22SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123e6bced22Smrgit's up to you to specify -f if you want it.
124e6bced22Smrg
125e6bced22SmrgIf -S is not specified, no backups are attempted.
126e6bced22Smrg
1278b648e79SmrgReport bugs to <bug-automake@gnu.org>.
1288b648e79SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
1298b648e79SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>."
1300e20ee16Smrg
1313e6697d4Smrgwhile test $# -ne 0; do
1323e6697d4Smrg  case $1 in
1333e6697d4Smrg    -c) ;;
1340e20ee16Smrg
1353e6697d4Smrg    -C) copy_on_change=true;;
1360e20ee16Smrg
1373e6697d4Smrg    -d) dir_arg=true;;
1380e20ee16Smrg
1393e6697d4Smrg    -g) chgrpcmd="$chgrpprog $2"
1402f167a42Smrg        shift;;
1410e20ee16Smrg
1423e6697d4Smrg    --help) echo "$usage"; exit $?;;
1430e20ee16Smrg
1443e6697d4Smrg    -m) mode=$2
1452f167a42Smrg        case $mode in
1462f167a42Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1472f167a42Smrg            echo "$0: invalid mode: $mode" >&2
1482f167a42Smrg            exit 1;;
1492f167a42Smrg        esac
1502f167a42Smrg        shift;;
1510e20ee16Smrg
1523e6697d4Smrg    -o) chowncmd="$chownprog $2"
1532f167a42Smrg        shift;;
1540e20ee16Smrg
155e6bced22Smrg    -p) cpprog="$cpprog -p";;
156e6bced22Smrg
1573e6697d4Smrg    -s) stripcmd=$stripprog;;
1580e20ee16Smrg
159e6bced22Smrg    -S) backupsuffix="$2"
160e6bced22Smrg        shift;;
161e6bced22Smrg
1622f167a42Smrg    -t)
1632f167a42Smrg        is_target_a_directory=always
1642f167a42Smrg        dst_arg=$2
1652f167a42Smrg        # Protect names problematic for 'test' and other utilities.
1662f167a42Smrg        case $dst_arg in
1672f167a42Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1682f167a42Smrg        esac
1692f167a42Smrg        shift;;
1700e20ee16Smrg
1712f167a42Smrg    -T) is_target_a_directory=never;;
1720e20ee16Smrg
1738b648e79Smrg    --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;;
1740e20ee16Smrg
1752f167a42Smrg    --) shift
1762f167a42Smrg        break;;
1770e20ee16Smrg
1782f167a42Smrg    -*) echo "$0: invalid option: $1" >&2
1792f167a42Smrg        exit 1;;
1800e20ee16Smrg
1813e6697d4Smrg    *)  break;;
1823e6697d4Smrg  esac
1833e6697d4Smrg  shift
1843e6697d4Smrgdone
1850e20ee16Smrg
1862f167a42Smrg# We allow the use of options -d and -T together, by making -d
1872f167a42Smrg# take the precedence; this is for compatibility with GNU install.
1882f167a42Smrg
1892f167a42Smrgif test -n "$dir_arg"; then
1902f167a42Smrg  if test -n "$dst_arg"; then
1912f167a42Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1922f167a42Smrg    exit 1
1932f167a42Smrg  fi
1942f167a42Smrgfi
1952f167a42Smrg
1963e6697d4Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1973e6697d4Smrg  # When -d is used, all remaining arguments are directories to create.
1983e6697d4Smrg  # When -t is used, the destination is already specified.
1993e6697d4Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2003e6697d4Smrg  for arg
2013e6697d4Smrg  do
2023e6697d4Smrg    if test -n "$dst_arg"; then
2033e6697d4Smrg      # $@ is not empty: it contains at least $arg.
2043e6697d4Smrg      set fnord "$@" "$dst_arg"
2053e6697d4Smrg      shift # fnord
2063e6697d4Smrg    fi
2073e6697d4Smrg    shift # arg
2083e6697d4Smrg    dst_arg=$arg
2092f167a42Smrg    # Protect names problematic for 'test' and other utilities.
2102f167a42Smrg    case $dst_arg in
2112f167a42Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2122f167a42Smrg    esac
2133e6697d4Smrg  done
2143e6697d4Smrgfi
2150e20ee16Smrg
2163e6697d4Smrgif test $# -eq 0; then
2173e6697d4Smrg  if test -z "$dir_arg"; then
2183e6697d4Smrg    echo "$0: no input file specified." >&2
2193e6697d4Smrg    exit 1
2203e6697d4Smrg  fi
2212f167a42Smrg  # It's OK to call 'install-sh -d' without argument.
2223e6697d4Smrg  # This can happen when creating conditional directories.
2233e6697d4Smrg  exit 0
2243e6697d4Smrgfi
2250e20ee16Smrg
2263e6697d4Smrgif test -z "$dir_arg"; then
2272f167a42Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2282f167a42Smrg    if test ! -d "$dst_arg"; then
2292f167a42Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2302f167a42Smrg      exit 1
2312f167a42Smrg    fi
2322f167a42Smrg  fi
2332f167a42Smrgfi
2342f167a42Smrg
2352f167a42Smrgif test -z "$dir_arg"; then
2362f167a42Smrg  do_exit='(exit $ret); exit $ret'
2372f167a42Smrg  trap "ret=129; $do_exit" 1
2382f167a42Smrg  trap "ret=130; $do_exit" 2
2392f167a42Smrg  trap "ret=141; $do_exit" 13
2402f167a42Smrg  trap "ret=143; $do_exit" 15
2413e6697d4Smrg
2423e6697d4Smrg  # Set umask so as not to create temps with too-generous modes.
2433e6697d4Smrg  # However, 'strip' requires both read and write access to temps.
2443e6697d4Smrg  case $mode in
2453e6697d4Smrg    # Optimize common cases.
2463e6697d4Smrg    *644) cp_umask=133;;
2473e6697d4Smrg    *755) cp_umask=22;;
2483e6697d4Smrg
2493e6697d4Smrg    *[0-7])
2503e6697d4Smrg      if test -z "$stripcmd"; then
2512f167a42Smrg        u_plus_rw=
2523e6697d4Smrg      else
2532f167a42Smrg        u_plus_rw='% 200'
2543e6697d4Smrg      fi
2553e6697d4Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2563e6697d4Smrg    *)
2573e6697d4Smrg      if test -z "$stripcmd"; then
2582f167a42Smrg        u_plus_rw=
2593e6697d4Smrg      else
2602f167a42Smrg        u_plus_rw=,u+rw
2613e6697d4Smrg      fi
2623e6697d4Smrg      cp_umask=$mode$u_plus_rw;;
2633e6697d4Smrg  esac
2643e6697d4Smrgfi
2650e20ee16Smrg
2663e6697d4Smrgfor src
2673e6697d4Smrgdo
2682f167a42Smrg  # Protect names problematic for 'test' and other utilities.
2693e6697d4Smrg  case $src in
2702f167a42Smrg    -* | [=\(\)!]) src=./$src;;
2713e6697d4Smrg  esac
2723e6697d4Smrg
2733e6697d4Smrg  if test -n "$dir_arg"; then
2743e6697d4Smrg    dst=$src
2753e6697d4Smrg    dstdir=$dst
2763e6697d4Smrg    test -d "$dstdir"
2773e6697d4Smrg    dstdir_status=$?
278e6bced22Smrg    # Don't chown directories that already exist.
279e6bced22Smrg    if test $dstdir_status = 0; then
280e6bced22Smrg      chowncmd=""
281e6bced22Smrg    fi
2823e6697d4Smrg  else
2833e6697d4Smrg
2843e6697d4Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2853e6697d4Smrg    # might cause directories to be created, which would be especially bad
2863e6697d4Smrg    # if $src (and thus $dsttmp) contains '*'.
2873e6697d4Smrg    if test ! -f "$src" && test ! -d "$src"; then
2883e6697d4Smrg      echo "$0: $src does not exist." >&2
2893e6697d4Smrg      exit 1
2903e6697d4Smrg    fi
2913e6697d4Smrg
2923e6697d4Smrg    if test -z "$dst_arg"; then
2933e6697d4Smrg      echo "$0: no destination specified." >&2
2943e6697d4Smrg      exit 1
2953e6697d4Smrg    fi
2963e6697d4Smrg    dst=$dst_arg
2970e20ee16Smrg
298fc34a306Smrg    # If destination is a directory, append the input filename.
2993e6697d4Smrg    if test -d "$dst"; then
3002f167a42Smrg      if test "$is_target_a_directory" = never; then
3012f167a42Smrg        echo "$0: $dst_arg: Is a directory" >&2
3022f167a42Smrg        exit 1
3033e6697d4Smrg      fi
3043e6697d4Smrg      dstdir=$dst
305fc34a306Smrg      dstbase=`basename "$src"`
306fc34a306Smrg      case $dst in
307fc34a306Smrg	*/) dst=$dst$dstbase;;
308fc34a306Smrg	*)  dst=$dst/$dstbase;;
309fc34a306Smrg      esac
3103e6697d4Smrg      dstdir_status=0
3113e6697d4Smrg    else
3122f167a42Smrg      dstdir=`dirname "$dst"`
3133e6697d4Smrg      test -d "$dstdir"
3143e6697d4Smrg      dstdir_status=$?
3153e6697d4Smrg    fi
3163e6697d4Smrg  fi
3173e6697d4Smrg
318fc34a306Smrg  case $dstdir in
319fc34a306Smrg    */) dstdirslash=$dstdir;;
320fc34a306Smrg    *)  dstdirslash=$dstdir/;;
321fc34a306Smrg  esac
322fc34a306Smrg
3233e6697d4Smrg  obsolete_mkdir_used=false
3243e6697d4Smrg
3253e6697d4Smrg  if test $dstdir_status != 0; then
3263e6697d4Smrg    case $posix_mkdir in
3273e6697d4Smrg      '')
3282f167a42Smrg        # With -d, create the new directory with the user-specified mode.
3292f167a42Smrg        # Otherwise, rely on $mkdir_umask.
3302f167a42Smrg        if test -n "$dir_arg"; then
3312f167a42Smrg          mkdir_mode=-m$mode
3322f167a42Smrg        else
3332f167a42Smrg          mkdir_mode=
3342f167a42Smrg        fi
3352f167a42Smrg
3362f167a42Smrg        posix_mkdir=false
337e6bced22Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338e6bced22Smrg	# here however when possible just to lower collision chance.
339e6bced22Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340e6bced22Smrg
341e6bced22Smrg	trap '
342e6bced22Smrg	  ret=$?
343e6bced22Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344e6bced22Smrg	  exit $ret
345e6bced22Smrg	' 0
346e6bced22Smrg
347e6bced22Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3488b648e79Smrg	# directly in world-writable /tmp, make sure that the '$tmpdir'
349e6bced22Smrg	# directory is successfully created first before we actually test
350e6bced22Smrg	# 'mkdir -p'.
351e6bced22Smrg	if (umask $mkdir_umask &&
352e6bced22Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353e6bced22Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354e6bced22Smrg	then
355e6bced22Smrg	  if test -z "$dir_arg" || {
3568b648e79Smrg	       # Check for POSIX incompatibility with -m.
357e6bced22Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358e6bced22Smrg	       # other-writable bit of parent directory when it shouldn't.
359e6bced22Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360e6bced22Smrg	       test_tmpdir="$tmpdir/a"
361e6bced22Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362e6bced22Smrg	       case $ls_ld_tmpdir in
363e6bced22Smrg		 d????-?r-*) different_mode=700;;
364e6bced22Smrg		 d????-?--*) different_mode=755;;
365e6bced22Smrg		 *) false;;
366e6bced22Smrg	       esac &&
367e6bced22Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368e6bced22Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369e6bced22Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370e6bced22Smrg	       }
371e6bced22Smrg	     }
372e6bced22Smrg	  then posix_mkdir=:
373e6bced22Smrg	  fi
374e6bced22Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375e6bced22Smrg	else
376e6bced22Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377e6bced22Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378e6bced22Smrg	fi
379e6bced22Smrg	trap '' 0;;
3803e6697d4Smrg    esac
3810e20ee16Smrg
3823e6697d4Smrg    if
3833e6697d4Smrg      $posix_mkdir && (
3842f167a42Smrg        umask $mkdir_umask &&
3852f167a42Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3863e6697d4Smrg      )
3873e6697d4Smrg    then :
3883e6697d4Smrg    else
3893e6697d4Smrg
390e6bced22Smrg      # mkdir does not conform to POSIX,
3913e6697d4Smrg      # or it failed possibly due to a race condition.  Create the
3923e6697d4Smrg      # directory the slow way, step by step, checking for races as we go.
3933e6697d4Smrg
3943e6697d4Smrg      case $dstdir in
3952f167a42Smrg        /*) prefix='/';;
3962f167a42Smrg        [-=\(\)!]*) prefix='./';;
3972f167a42Smrg        *)  prefix='';;
3983e6697d4Smrg      esac
3993e6697d4Smrg
4003e6697d4Smrg      oIFS=$IFS
4013e6697d4Smrg      IFS=/
4022f167a42Smrg      set -f
4033e6697d4Smrg      set fnord $dstdir
4043e6697d4Smrg      shift
4052f167a42Smrg      set +f
4063e6697d4Smrg      IFS=$oIFS
4073e6697d4Smrg
4083e6697d4Smrg      prefixes=
4093e6697d4Smrg
4103e6697d4Smrg      for d
4113e6697d4Smrg      do
4122f167a42Smrg        test X"$d" = X && continue
4132f167a42Smrg
4142f167a42Smrg        prefix=$prefix$d
4152f167a42Smrg        if test -d "$prefix"; then
4162f167a42Smrg          prefixes=
4172f167a42Smrg        else
4182f167a42Smrg          if $posix_mkdir; then
419e6bced22Smrg            (umask $mkdir_umask &&
4202f167a42Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4212f167a42Smrg            # Don't fail if two instances are running concurrently.
4222f167a42Smrg            test -d "$prefix" || exit 1
4232f167a42Smrg          else
4242f167a42Smrg            case $prefix in
4252f167a42Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4262f167a42Smrg              *) qprefix=$prefix;;
4272f167a42Smrg            esac
4282f167a42Smrg            prefixes="$prefixes '$qprefix'"
4292f167a42Smrg          fi
4302f167a42Smrg        fi
4312f167a42Smrg        prefix=$prefix/
4323e6697d4Smrg      done
4333e6697d4Smrg
4343e6697d4Smrg      if test -n "$prefixes"; then
4352f167a42Smrg        # Don't fail if two instances are running concurrently.
4362f167a42Smrg        (umask $mkdir_umask &&
4372f167a42Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4382f167a42Smrg          test -d "$dstdir" || exit 1
4392f167a42Smrg        obsolete_mkdir_used=true
4403e6697d4Smrg      fi
4413e6697d4Smrg    fi
4423e6697d4Smrg  fi
4433e6697d4Smrg
4443e6697d4Smrg  if test -n "$dir_arg"; then
4453e6697d4Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4463e6697d4Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4473e6697d4Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4483e6697d4Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4493e6697d4Smrg  else
4503e6697d4Smrg
4513e6697d4Smrg    # Make a couple of temp file names in the proper directory.
452fc34a306Smrg    dsttmp=${dstdirslash}_inst.$$_
453fc34a306Smrg    rmtmp=${dstdirslash}_rm.$$_
4543e6697d4Smrg
4553e6697d4Smrg    # Trap to clean up those temp files at exit.
4563e6697d4Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4573e6697d4Smrg
4583e6697d4Smrg    # Copy the file name to the temp name.
459e6bced22Smrg    (umask $cp_umask &&
460e6bced22Smrg     { test -z "$stripcmd" || {
461e6bced22Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462e6bced22Smrg	 # which would cause strip to fail.
463e6bced22Smrg	 if test -z "$doit"; then
464e6bced22Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465e6bced22Smrg	 else
466e6bced22Smrg	   $doit touch "$dsttmp"
467e6bced22Smrg	 fi
468e6bced22Smrg       }
469e6bced22Smrg     } &&
470e6bced22Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4713e6697d4Smrg
4723e6697d4Smrg    # and set any options; do chmod last to preserve setuid bits.
4733e6697d4Smrg    #
4743e6697d4Smrg    # If any of these fail, we abort the whole thing.  If we want to
4753e6697d4Smrg    # ignore errors from any of these, just make sure not to ignore
4763e6697d4Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4773e6697d4Smrg    #
4783e6697d4Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4793e6697d4Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4803e6697d4Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4813e6697d4Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4823e6697d4Smrg
4833e6697d4Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4843e6697d4Smrg    if $copy_on_change &&
4852f167a42Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4862f167a42Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4872f167a42Smrg       set -f &&
4883e6697d4Smrg       set X $old && old=:$2:$4:$5:$6 &&
4893e6697d4Smrg       set X $new && new=:$2:$4:$5:$6 &&
4902f167a42Smrg       set +f &&
4913e6697d4Smrg       test "$old" = "$new" &&
4923e6697d4Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4933e6697d4Smrg    then
4943e6697d4Smrg      rm -f "$dsttmp"
4953e6697d4Smrg    else
496e6bced22Smrg      # If $backupsuffix is set, and the file being installed
497e6bced22Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498e6bced22Smrg      # e.g., if mv doesn't support -f.
499e6bced22Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500e6bced22Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501e6bced22Smrg      fi
502e6bced22Smrg
5033e6697d4Smrg      # Rename the file to the real destination.
5043e6697d4Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5053e6697d4Smrg
5063e6697d4Smrg      # The rename failed, perhaps because mv can't rename something else
5073e6697d4Smrg      # to itself, or perhaps because mv is so ancient that it does not
5083e6697d4Smrg      # support -f.
5093e6697d4Smrg      {
5102f167a42Smrg        # Now remove or move aside any old file at destination location.
5112f167a42Smrg        # We try this two ways since rm can't unlink itself on some
5122f167a42Smrg        # systems and the destination file might be busy for other
5132f167a42Smrg        # reasons.  In this case, the final cleanup might fail but the new
5142f167a42Smrg        # file should still install successfully.
5152f167a42Smrg        {
5162f167a42Smrg          test ! -f "$dst" ||
517e6bced22Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
5182f167a42Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519e6bced22Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5202f167a42Smrg          } ||
5212f167a42Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5222f167a42Smrg            (exit 1); exit 1
5232f167a42Smrg          }
5242f167a42Smrg        } &&
5252f167a42Smrg
5262f167a42Smrg        # Now rename the file to the real destination.
5272f167a42Smrg        $doit $mvcmd "$dsttmp" "$dst"
5283e6697d4Smrg      }
5293e6697d4Smrg    fi || exit 1
5303e6697d4Smrg
5313e6697d4Smrg    trap '' 0
5323e6697d4Smrg  fi
5333e6697d4Smrgdone
5340e20ee16Smrg
5353e6697d4Smrg# Local variables:
536fc34a306Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
5373e6697d4Smrg# time-stamp-start: "scriptversion="
5383e6697d4Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539fc34a306Smrg# time-stamp-time-zone: "UTC0"
5403e6697d4Smrg# time-stamp-end: "; # UTC"
5413e6697d4Smrg# End:
542