1a99f82b8Smrg#!/bin/sh
2a99f82b8Smrg# install - install a program, script, or datafile
32d62a77cSmrg
40c1e1135Smrgscriptversion=2020-11-14.01; # UTC
52d62a77cSmrg
6a99f82b8Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7a99f82b8Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8a99f82b8Smrg# following copyright and license.
9a99f82b8Smrg#
10a99f82b8Smrg# Copyright (C) 1994 X Consortium
11a99f82b8Smrg#
12a99f82b8Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13a99f82b8Smrg# of this software and associated documentation files (the "Software"), to
14a99f82b8Smrg# deal in the Software without restriction, including without limitation the
15a99f82b8Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16a99f82b8Smrg# sell copies of the Software, and to permit persons to whom the Software is
17a99f82b8Smrg# furnished to do so, subject to the following conditions:
18a99f82b8Smrg#
19a99f82b8Smrg# The above copyright notice and this permission notice shall be included in
20a99f82b8Smrg# all copies or substantial portions of the Software.
21a99f82b8Smrg#
22a99f82b8Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a99f82b8Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a99f82b8Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25a99f82b8Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26a99f82b8Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27a99f82b8Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a99f82b8Smrg#
29a99f82b8Smrg# Except as contained in this notice, the name of the X Consortium shall not
30a99f82b8Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31a99f82b8Smrg# ings in this Software without prior written authorization from the X Consor-
32a99f82b8Smrg# tium.
33a99f82b8Smrg#
34a99f82b8Smrg#
35a99f82b8Smrg# FSF changes to this file are in the public domain.
36a99f82b8Smrg#
37a99f82b8Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3818b08247Smrg# 'make' implicit rules from creating a file called install from it
39a99f82b8Smrg# when there is no Makefile.
40a99f82b8Smrg#
41a99f82b8Smrg# This script is compatible with the BSD install script, but was written
422d62a77cSmrg# from scratch.
43a99f82b8Smrg
4418b08247Smrgtab='	'
452d62a77cSmrgnl='
462d62a77cSmrg'
4718b08247SmrgIFS=" $tab$nl"
48a99f82b8Smrg
4918b08247Smrg# Set DOITPROG to "echo" to test this script.
50a99f82b8Smrg
512d62a77cSmrgdoit=${DOITPROG-}
5218b08247Smrgdoit_exec=${doit:-exec}
53a99f82b8Smrg
542d62a77cSmrg# Put in absolute file names if you don't have them in your path;
552d62a77cSmrg# or use environment vars.
562d62a77cSmrg
572d62a77cSmrgchgrpprog=${CHGRPPROG-chgrp}
582d62a77cSmrgchmodprog=${CHMODPROG-chmod}
592d62a77cSmrgchownprog=${CHOWNPROG-chown}
602d62a77cSmrgcmpprog=${CMPPROG-cmp}
612d62a77cSmrgcpprog=${CPPROG-cp}
622d62a77cSmrgmkdirprog=${MKDIRPROG-mkdir}
632d62a77cSmrgmvprog=${MVPROG-mv}
642d62a77cSmrgrmprog=${RMPROG-rm}
652d62a77cSmrgstripprog=${STRIPPROG-strip}
662d62a77cSmrg
672d62a77cSmrgposix_mkdir=
682d62a77cSmrg
692d62a77cSmrg# Desired mode of installed file.
702d62a77cSmrgmode=0755
712d62a77cSmrg
720c1e1135Smrg# Create dirs (including intermediate dirs) using mode 755.
730c1e1135Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
740c1e1135Smrgmkdir_umask=22
750c1e1135Smrg
760c1e1135Smrgbackupsuffix=
772d62a77cSmrgchgrpcmd=
782d62a77cSmrgchmodcmd=$chmodprog
792d62a77cSmrgchowncmd=
802d62a77cSmrgmvcmd=$mvprog
812d62a77cSmrgrmcmd="$rmprog -f"
822d62a77cSmrgstripcmd=
83a99f82b8Smrg
842d62a77cSmrgsrc=
852d62a77cSmrgdst=
862d62a77cSmrgdir_arg=
872d62a77cSmrgdst_arg=
88a99f82b8Smrg
892d62a77cSmrgcopy_on_change=false
9018b08247Smrgis_target_a_directory=possibly
91a99f82b8Smrg
922d62a77cSmrgusage="\
932d62a77cSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
942d62a77cSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
952d62a77cSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
962d62a77cSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97a99f82b8Smrg
982d62a77cSmrgIn the 1st form, copy SRCFILE to DSTFILE.
992d62a77cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1002d62a77cSmrgIn the 4th, create DIRECTORIES.
101a99f82b8Smrg
1022d62a77cSmrgOptions:
1032d62a77cSmrg     --help     display this help and exit.
1042d62a77cSmrg     --version  display version info and exit.
105a99f82b8Smrg
1062d62a77cSmrg  -c            (ignored)
1070c1e1135Smrg  -C            install only if different (preserve data modification time)
1082d62a77cSmrg  -d            create directories instead of installing files.
1092d62a77cSmrg  -g GROUP      $chgrpprog installed files to GROUP.
1102d62a77cSmrg  -m MODE       $chmodprog installed files to MODE.
1112d62a77cSmrg  -o USER       $chownprog installed files to USER.
1120c1e1135Smrg  -p            pass -p to $cpprog.
1132d62a77cSmrg  -s            $stripprog installed files.
1140c1e1135Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1152d62a77cSmrg  -t DIRECTORY  install into DIRECTORY.
1162d62a77cSmrg  -T            report an error if DSTFILE is a directory.
117a99f82b8Smrg
1182d62a77cSmrgEnvironment variables override the default commands:
1192d62a77cSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1202d62a77cSmrg  RMPROG STRIPPROG
1210c1e1135Smrg
1220c1e1135SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1230c1e1135Smrgit's up to you to specify -f if you want it.
1240c1e1135Smrg
1250c1e1135SmrgIf -S is not specified, no backups are attempted.
1260c1e1135Smrg
1270c1e1135SmrgEmail bug reports to bug-automake@gnu.org.
1280c1e1135SmrgAutomake home page: https://www.gnu.org/software/automake/
1292d62a77cSmrg"
130a99f82b8Smrg
1312d62a77cSmrgwhile test $# -ne 0; do
1322d62a77cSmrg  case $1 in
1332d62a77cSmrg    -c) ;;
134a99f82b8Smrg
1352d62a77cSmrg    -C) copy_on_change=true;;
136a99f82b8Smrg
1372d62a77cSmrg    -d) dir_arg=true;;
138a99f82b8Smrg
1392d62a77cSmrg    -g) chgrpcmd="$chgrpprog $2"
14018b08247Smrg        shift;;
141a99f82b8Smrg
1422d62a77cSmrg    --help) echo "$usage"; exit $?;;
143a99f82b8Smrg
1442d62a77cSmrg    -m) mode=$2
14518b08247Smrg        case $mode in
14618b08247Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14718b08247Smrg            echo "$0: invalid mode: $mode" >&2
14818b08247Smrg            exit 1;;
14918b08247Smrg        esac
15018b08247Smrg        shift;;
151a99f82b8Smrg
1522d62a77cSmrg    -o) chowncmd="$chownprog $2"
15318b08247Smrg        shift;;
154a99f82b8Smrg
1550c1e1135Smrg    -p) cpprog="$cpprog -p";;
1560c1e1135Smrg
1572d62a77cSmrg    -s) stripcmd=$stripprog;;
158a99f82b8Smrg
1590c1e1135Smrg    -S) backupsuffix="$2"
1600c1e1135Smrg        shift;;
1610c1e1135Smrg
16218b08247Smrg    -t)
16318b08247Smrg        is_target_a_directory=always
16418b08247Smrg        dst_arg=$2
16518b08247Smrg        # Protect names problematic for 'test' and other utilities.
16618b08247Smrg        case $dst_arg in
16718b08247Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16818b08247Smrg        esac
16918b08247Smrg        shift;;
170a99f82b8Smrg
17118b08247Smrg    -T) is_target_a_directory=never;;
172a99f82b8Smrg
1732d62a77cSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174a99f82b8Smrg
17518b08247Smrg    --) shift
17618b08247Smrg        break;;
177a99f82b8Smrg
17818b08247Smrg    -*) echo "$0: invalid option: $1" >&2
17918b08247Smrg        exit 1;;
180a99f82b8Smrg
1812d62a77cSmrg    *)  break;;
1822d62a77cSmrg  esac
1832d62a77cSmrg  shift
1842d62a77cSmrgdone
185a99f82b8Smrg
18618b08247Smrg# We allow the use of options -d and -T together, by making -d
18718b08247Smrg# take the precedence; this is for compatibility with GNU install.
18818b08247Smrg
18918b08247Smrgif test -n "$dir_arg"; then
19018b08247Smrg  if test -n "$dst_arg"; then
19118b08247Smrg    echo "$0: target directory not allowed when installing a directory." >&2
19218b08247Smrg    exit 1
19318b08247Smrg  fi
19418b08247Smrgfi
19518b08247Smrg
1962d62a77cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1972d62a77cSmrg  # When -d is used, all remaining arguments are directories to create.
1982d62a77cSmrg  # When -t is used, the destination is already specified.
1992d62a77cSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2002d62a77cSmrg  for arg
2012d62a77cSmrg  do
2022d62a77cSmrg    if test -n "$dst_arg"; then
2032d62a77cSmrg      # $@ is not empty: it contains at least $arg.
2042d62a77cSmrg      set fnord "$@" "$dst_arg"
2052d62a77cSmrg      shift # fnord
2062d62a77cSmrg    fi
2072d62a77cSmrg    shift # arg
2082d62a77cSmrg    dst_arg=$arg
20918b08247Smrg    # Protect names problematic for 'test' and other utilities.
21018b08247Smrg    case $dst_arg in
21118b08247Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21218b08247Smrg    esac
2132d62a77cSmrg  done
2142d62a77cSmrgfi
215a99f82b8Smrg
2162d62a77cSmrgif test $# -eq 0; then
2172d62a77cSmrg  if test -z "$dir_arg"; then
2182d62a77cSmrg    echo "$0: no input file specified." >&2
2192d62a77cSmrg    exit 1
2202d62a77cSmrg  fi
22118b08247Smrg  # It's OK to call 'install-sh -d' without argument.
2222d62a77cSmrg  # This can happen when creating conditional directories.
2232d62a77cSmrg  exit 0
2242d62a77cSmrgfi
225a99f82b8Smrg
2262d62a77cSmrgif test -z "$dir_arg"; then
22718b08247Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22818b08247Smrg    if test ! -d "$dst_arg"; then
22918b08247Smrg      echo "$0: $dst_arg: Is not a directory." >&2
23018b08247Smrg      exit 1
23118b08247Smrg    fi
23218b08247Smrg  fi
23318b08247Smrgfi
23418b08247Smrg
23518b08247Smrgif test -z "$dir_arg"; then
23618b08247Smrg  do_exit='(exit $ret); exit $ret'
23718b08247Smrg  trap "ret=129; $do_exit" 1
23818b08247Smrg  trap "ret=130; $do_exit" 2
23918b08247Smrg  trap "ret=141; $do_exit" 13
24018b08247Smrg  trap "ret=143; $do_exit" 15
2412d62a77cSmrg
2422d62a77cSmrg  # Set umask so as not to create temps with too-generous modes.
2432d62a77cSmrg  # However, 'strip' requires both read and write access to temps.
2442d62a77cSmrg  case $mode in
2452d62a77cSmrg    # Optimize common cases.
2462d62a77cSmrg    *644) cp_umask=133;;
2472d62a77cSmrg    *755) cp_umask=22;;
2482d62a77cSmrg
2492d62a77cSmrg    *[0-7])
2502d62a77cSmrg      if test -z "$stripcmd"; then
25118b08247Smrg        u_plus_rw=
2522d62a77cSmrg      else
25318b08247Smrg        u_plus_rw='% 200'
2542d62a77cSmrg      fi
2552d62a77cSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2562d62a77cSmrg    *)
2572d62a77cSmrg      if test -z "$stripcmd"; then
25818b08247Smrg        u_plus_rw=
2592d62a77cSmrg      else
26018b08247Smrg        u_plus_rw=,u+rw
2612d62a77cSmrg      fi
2622d62a77cSmrg      cp_umask=$mode$u_plus_rw;;
2632d62a77cSmrg  esac
2642d62a77cSmrgfi
265a99f82b8Smrg
2662d62a77cSmrgfor src
2672d62a77cSmrgdo
26818b08247Smrg  # Protect names problematic for 'test' and other utilities.
2692d62a77cSmrg  case $src in
27018b08247Smrg    -* | [=\(\)!]) src=./$src;;
2712d62a77cSmrg  esac
2722d62a77cSmrg
2732d62a77cSmrg  if test -n "$dir_arg"; then
2742d62a77cSmrg    dst=$src
2752d62a77cSmrg    dstdir=$dst
2762d62a77cSmrg    test -d "$dstdir"
2772d62a77cSmrg    dstdir_status=$?
2780c1e1135Smrg    # Don't chown directories that already exist.
2790c1e1135Smrg    if test $dstdir_status = 0; then
2800c1e1135Smrg      chowncmd=""
2810c1e1135Smrg    fi
2822d62a77cSmrg  else
2832d62a77cSmrg
2842d62a77cSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2852d62a77cSmrg    # might cause directories to be created, which would be especially bad
2862d62a77cSmrg    # if $src (and thus $dsttmp) contains '*'.
2872d62a77cSmrg    if test ! -f "$src" && test ! -d "$src"; then
2882d62a77cSmrg      echo "$0: $src does not exist." >&2
2892d62a77cSmrg      exit 1
2902d62a77cSmrg    fi
2912d62a77cSmrg
2922d62a77cSmrg    if test -z "$dst_arg"; then
2932d62a77cSmrg      echo "$0: no destination specified." >&2
2942d62a77cSmrg      exit 1
2952d62a77cSmrg    fi
2962d62a77cSmrg    dst=$dst_arg
297a99f82b8Smrg
2980c1e1135Smrg    # If destination is a directory, append the input filename.
2992d62a77cSmrg    if test -d "$dst"; then
30018b08247Smrg      if test "$is_target_a_directory" = never; then
30118b08247Smrg        echo "$0: $dst_arg: Is a directory" >&2
30218b08247Smrg        exit 1
3032d62a77cSmrg      fi
3042d62a77cSmrg      dstdir=$dst
3050c1e1135Smrg      dstbase=`basename "$src"`
3060c1e1135Smrg      case $dst in
3070c1e1135Smrg	*/) dst=$dst$dstbase;;
3080c1e1135Smrg	*)  dst=$dst/$dstbase;;
3090c1e1135Smrg      esac
3102d62a77cSmrg      dstdir_status=0
3112d62a77cSmrg    else
31218b08247Smrg      dstdir=`dirname "$dst"`
3132d62a77cSmrg      test -d "$dstdir"
3142d62a77cSmrg      dstdir_status=$?
3152d62a77cSmrg    fi
3162d62a77cSmrg  fi
3172d62a77cSmrg
3180c1e1135Smrg  case $dstdir in
3190c1e1135Smrg    */) dstdirslash=$dstdir;;
3200c1e1135Smrg    *)  dstdirslash=$dstdir/;;
3210c1e1135Smrg  esac
3220c1e1135Smrg
3232d62a77cSmrg  obsolete_mkdir_used=false
3242d62a77cSmrg
3252d62a77cSmrg  if test $dstdir_status != 0; then
3262d62a77cSmrg    case $posix_mkdir in
3272d62a77cSmrg      '')
32818b08247Smrg        # With -d, create the new directory with the user-specified mode.
32918b08247Smrg        # Otherwise, rely on $mkdir_umask.
33018b08247Smrg        if test -n "$dir_arg"; then
33118b08247Smrg          mkdir_mode=-m$mode
33218b08247Smrg        else
33318b08247Smrg          mkdir_mode=
33418b08247Smrg        fi
33518b08247Smrg
33618b08247Smrg        posix_mkdir=false
3370c1e1135Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3380c1e1135Smrg	# here however when possible just to lower collision chance.
3390c1e1135Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3400c1e1135Smrg
3410c1e1135Smrg	trap '
3420c1e1135Smrg	  ret=$?
3430c1e1135Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3440c1e1135Smrg	  exit $ret
3450c1e1135Smrg	' 0
3460c1e1135Smrg
3470c1e1135Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3480c1e1135Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3490c1e1135Smrg	# directory is successfully created first before we actually test
3500c1e1135Smrg	# 'mkdir -p'.
3510c1e1135Smrg	if (umask $mkdir_umask &&
3520c1e1135Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3530c1e1135Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3540c1e1135Smrg	then
3550c1e1135Smrg	  if test -z "$dir_arg" || {
3560c1e1135Smrg	       # Check for POSIX incompatibilities with -m.
3570c1e1135Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3580c1e1135Smrg	       # other-writable bit of parent directory when it shouldn't.
3590c1e1135Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3600c1e1135Smrg	       test_tmpdir="$tmpdir/a"
3610c1e1135Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3620c1e1135Smrg	       case $ls_ld_tmpdir in
3630c1e1135Smrg		 d????-?r-*) different_mode=700;;
3640c1e1135Smrg		 d????-?--*) different_mode=755;;
3650c1e1135Smrg		 *) false;;
3660c1e1135Smrg	       esac &&
3670c1e1135Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3680c1e1135Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3690c1e1135Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3700c1e1135Smrg	       }
3710c1e1135Smrg	     }
3720c1e1135Smrg	  then posix_mkdir=:
3730c1e1135Smrg	  fi
3740c1e1135Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3750c1e1135Smrg	else
3760c1e1135Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
3770c1e1135Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3780c1e1135Smrg	fi
3790c1e1135Smrg	trap '' 0;;
3802d62a77cSmrg    esac
381a99f82b8Smrg
3822d62a77cSmrg    if
3832d62a77cSmrg      $posix_mkdir && (
38418b08247Smrg        umask $mkdir_umask &&
38518b08247Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3862d62a77cSmrg      )
3872d62a77cSmrg    then :
3882d62a77cSmrg    else
3892d62a77cSmrg
3900c1e1135Smrg      # mkdir does not conform to POSIX,
3912d62a77cSmrg      # or it failed possibly due to a race condition.  Create the
3922d62a77cSmrg      # directory the slow way, step by step, checking for races as we go.
3932d62a77cSmrg
3942d62a77cSmrg      case $dstdir in
39518b08247Smrg        /*) prefix='/';;
39618b08247Smrg        [-=\(\)!]*) prefix='./';;
39718b08247Smrg        *)  prefix='';;
3982d62a77cSmrg      esac
3992d62a77cSmrg
4002d62a77cSmrg      oIFS=$IFS
4012d62a77cSmrg      IFS=/
40218b08247Smrg      set -f
4032d62a77cSmrg      set fnord $dstdir
4042d62a77cSmrg      shift
40518b08247Smrg      set +f
4062d62a77cSmrg      IFS=$oIFS
4072d62a77cSmrg
4082d62a77cSmrg      prefixes=
4092d62a77cSmrg
4102d62a77cSmrg      for d
4112d62a77cSmrg      do
41218b08247Smrg        test X"$d" = X && continue
41318b08247Smrg
41418b08247Smrg        prefix=$prefix$d
41518b08247Smrg        if test -d "$prefix"; then
41618b08247Smrg          prefixes=
41718b08247Smrg        else
41818b08247Smrg          if $posix_mkdir; then
4190c1e1135Smrg            (umask $mkdir_umask &&
42018b08247Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42118b08247Smrg            # Don't fail if two instances are running concurrently.
42218b08247Smrg            test -d "$prefix" || exit 1
42318b08247Smrg          else
42418b08247Smrg            case $prefix in
42518b08247Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42618b08247Smrg              *) qprefix=$prefix;;
42718b08247Smrg            esac
42818b08247Smrg            prefixes="$prefixes '$qprefix'"
42918b08247Smrg          fi
43018b08247Smrg        fi
43118b08247Smrg        prefix=$prefix/
4322d62a77cSmrg      done
4332d62a77cSmrg
4342d62a77cSmrg      if test -n "$prefixes"; then
43518b08247Smrg        # Don't fail if two instances are running concurrently.
43618b08247Smrg        (umask $mkdir_umask &&
43718b08247Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43818b08247Smrg          test -d "$dstdir" || exit 1
43918b08247Smrg        obsolete_mkdir_used=true
4402d62a77cSmrg      fi
4412d62a77cSmrg    fi
4422d62a77cSmrg  fi
4432d62a77cSmrg
4442d62a77cSmrg  if test -n "$dir_arg"; then
4452d62a77cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4462d62a77cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4472d62a77cSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4482d62a77cSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4492d62a77cSmrg  else
4502d62a77cSmrg
4512d62a77cSmrg    # Make a couple of temp file names in the proper directory.
4520c1e1135Smrg    dsttmp=${dstdirslash}_inst.$$_
4530c1e1135Smrg    rmtmp=${dstdirslash}_rm.$$_
4542d62a77cSmrg
4552d62a77cSmrg    # Trap to clean up those temp files at exit.
4562d62a77cSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4572d62a77cSmrg
4582d62a77cSmrg    # Copy the file name to the temp name.
4590c1e1135Smrg    (umask $cp_umask &&
4600c1e1135Smrg     { test -z "$stripcmd" || {
4610c1e1135Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4620c1e1135Smrg	 # which would cause strip to fail.
4630c1e1135Smrg	 if test -z "$doit"; then
4640c1e1135Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4650c1e1135Smrg	 else
4660c1e1135Smrg	   $doit touch "$dsttmp"
4670c1e1135Smrg	 fi
4680c1e1135Smrg       }
4690c1e1135Smrg     } &&
4700c1e1135Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4712d62a77cSmrg
4722d62a77cSmrg    # and set any options; do chmod last to preserve setuid bits.
4732d62a77cSmrg    #
4742d62a77cSmrg    # If any of these fail, we abort the whole thing.  If we want to
4752d62a77cSmrg    # ignore errors from any of these, just make sure not to ignore
4762d62a77cSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4772d62a77cSmrg    #
4782d62a77cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4792d62a77cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4802d62a77cSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4812d62a77cSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4822d62a77cSmrg
4832d62a77cSmrg    # If -C, don't bother to copy if it wouldn't change the file.
4842d62a77cSmrg    if $copy_on_change &&
48518b08247Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48618b08247Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48718b08247Smrg       set -f &&
4882d62a77cSmrg       set X $old && old=:$2:$4:$5:$6 &&
4892d62a77cSmrg       set X $new && new=:$2:$4:$5:$6 &&
49018b08247Smrg       set +f &&
4912d62a77cSmrg       test "$old" = "$new" &&
4922d62a77cSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4932d62a77cSmrg    then
4942d62a77cSmrg      rm -f "$dsttmp"
4952d62a77cSmrg    else
4960c1e1135Smrg      # If $backupsuffix is set, and the file being installed
4970c1e1135Smrg      # already exists, attempt a backup.  Don't worry if it fails,
4980c1e1135Smrg      # e.g., if mv doesn't support -f.
4990c1e1135Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
5000c1e1135Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5010c1e1135Smrg      fi
5020c1e1135Smrg
5032d62a77cSmrg      # Rename the file to the real destination.
5042d62a77cSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5052d62a77cSmrg
5062d62a77cSmrg      # The rename failed, perhaps because mv can't rename something else
5072d62a77cSmrg      # to itself, or perhaps because mv is so ancient that it does not
5082d62a77cSmrg      # support -f.
5092d62a77cSmrg      {
51018b08247Smrg        # Now remove or move aside any old file at destination location.
51118b08247Smrg        # We try this two ways since rm can't unlink itself on some
51218b08247Smrg        # systems and the destination file might be busy for other
51318b08247Smrg        # reasons.  In this case, the final cleanup might fail but the new
51418b08247Smrg        # file should still install successfully.
51518b08247Smrg        {
51618b08247Smrg          test ! -f "$dst" ||
5170c1e1135Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
51818b08247Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5190c1e1135Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52018b08247Smrg          } ||
52118b08247Smrg          { echo "$0: cannot unlink or rename $dst" >&2
52218b08247Smrg            (exit 1); exit 1
52318b08247Smrg          }
52418b08247Smrg        } &&
52518b08247Smrg
52618b08247Smrg        # Now rename the file to the real destination.
52718b08247Smrg        $doit $mvcmd "$dsttmp" "$dst"
5282d62a77cSmrg      }
5292d62a77cSmrg    fi || exit 1
5302d62a77cSmrg
5312d62a77cSmrg    trap '' 0
5322d62a77cSmrg  fi
5332d62a77cSmrgdone
534a99f82b8Smrg
5352d62a77cSmrg# Local variables:
5360c1e1135Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
5372d62a77cSmrg# time-stamp-start: "scriptversion="
5382d62a77cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5390c1e1135Smrg# time-stamp-time-zone: "UTC0"
5402d62a77cSmrg# time-stamp-end: "; # UTC"
5412d62a77cSmrg# End:
542