11a30de1fSmrg#!/bin/sh
21a30de1fSmrg# install - install a program, script, or datafile
31a30de1fSmrg
46eaa481cSmrgscriptversion=2020-11-14.01; # UTC
51a30de1fSmrg
61a30de1fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
71a30de1fSmrg# later released in X11R6 (xc/config/util/install.sh) with the
81a30de1fSmrg# following copyright and license.
91a30de1fSmrg#
101a30de1fSmrg# Copyright (C) 1994 X Consortium
111a30de1fSmrg#
121a30de1fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
131a30de1fSmrg# of this software and associated documentation files (the "Software"), to
141a30de1fSmrg# deal in the Software without restriction, including without limitation the
151a30de1fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
161a30de1fSmrg# sell copies of the Software, and to permit persons to whom the Software is
171a30de1fSmrg# furnished to do so, subject to the following conditions:
181a30de1fSmrg#
191a30de1fSmrg# The above copyright notice and this permission notice shall be included in
201a30de1fSmrg# all copies or substantial portions of the Software.
211a30de1fSmrg#
221a30de1fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
231a30de1fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241a30de1fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
251a30de1fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
261a30de1fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
271a30de1fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
281a30de1fSmrg#
291a30de1fSmrg# Except as contained in this notice, the name of the X Consortium shall not
301a30de1fSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
311a30de1fSmrg# ings in this Software without prior written authorization from the X Consor-
321a30de1fSmrg# tium.
331a30de1fSmrg#
341a30de1fSmrg#
351a30de1fSmrg# FSF changes to this file are in the public domain.
361a30de1fSmrg#
371a30de1fSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38a733a5bfSmrg# 'make' implicit rules from creating a file called install from it
391a30de1fSmrg# when there is no Makefile.
401a30de1fSmrg#
411a30de1fSmrg# This script is compatible with the BSD install script, but was written
421a30de1fSmrg# from scratch.
431a30de1fSmrg
441b983734Smrgtab='	'
451a30de1fSmrgnl='
461a30de1fSmrg'
471b983734SmrgIFS=" $tab$nl"
481a30de1fSmrg
491b983734Smrg# Set DOITPROG to "echo" to test this script.
501a30de1fSmrg
51b7fb5eacSmrgdoit=${DOITPROG-}
521b983734Smrgdoit_exec=${doit:-exec}
531a30de1fSmrg
541a30de1fSmrg# Put in absolute file names if you don't have them in your path;
551a30de1fSmrg# or use environment vars.
561a30de1fSmrg
57b7fb5eacSmrgchgrpprog=${CHGRPPROG-chgrp}
58b7fb5eacSmrgchmodprog=${CHMODPROG-chmod}
59b7fb5eacSmrgchownprog=${CHOWNPROG-chown}
60b7fb5eacSmrgcmpprog=${CMPPROG-cmp}
61b7fb5eacSmrgcpprog=${CPPROG-cp}
62b7fb5eacSmrgmkdirprog=${MKDIRPROG-mkdir}
63b7fb5eacSmrgmvprog=${MVPROG-mv}
64b7fb5eacSmrgrmprog=${RMPROG-rm}
65b7fb5eacSmrgstripprog=${STRIPPROG-strip}
66b7fb5eacSmrg
671a30de1fSmrgposix_mkdir=
681a30de1fSmrg
691a30de1fSmrg# Desired mode of installed file.
701a30de1fSmrgmode=0755
711a30de1fSmrg
726eaa481cSmrg# Create dirs (including intermediate dirs) using mode 755.
736eaa481cSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
746eaa481cSmrgmkdir_umask=22
756eaa481cSmrg
766eaa481cSmrgbackupsuffix=
77b7fb5eacSmrgchgrpcmd=
781a30de1fSmrgchmodcmd=$chmodprog
791a30de1fSmrgchowncmd=
80b7fb5eacSmrgmvcmd=$mvprog
811a30de1fSmrgrmcmd="$rmprog -f"
82b7fb5eacSmrgstripcmd=
83b7fb5eacSmrg
841a30de1fSmrgsrc=
851a30de1fSmrgdst=
861a30de1fSmrgdir_arg=
87b7fb5eacSmrgdst_arg=
88b7fb5eacSmrg
89b7fb5eacSmrgcopy_on_change=false
901b983734Smrgis_target_a_directory=possibly
911a30de1fSmrg
92b7fb5eacSmrgusage="\
93b7fb5eacSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
941a30de1fSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
951a30de1fSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
961a30de1fSmrg   or: $0 [OPTION]... -d DIRECTORIES...
971a30de1fSmrg
981a30de1fSmrgIn the 1st form, copy SRCFILE to DSTFILE.
991a30de1fSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1001a30de1fSmrgIn the 4th, create DIRECTORIES.
1011a30de1fSmrg
1021a30de1fSmrgOptions:
103b7fb5eacSmrg     --help     display this help and exit.
104b7fb5eacSmrg     --version  display version info and exit.
105b7fb5eacSmrg
106b7fb5eacSmrg  -c            (ignored)
1076eaa481cSmrg  -C            install only if different (preserve data modification time)
108b7fb5eacSmrg  -d            create directories instead of installing files.
109b7fb5eacSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110b7fb5eacSmrg  -m MODE       $chmodprog installed files to MODE.
111b7fb5eacSmrg  -o USER       $chownprog installed files to USER.
1126eaa481cSmrg  -p            pass -p to $cpprog.
113b7fb5eacSmrg  -s            $stripprog installed files.
1146eaa481cSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115b7fb5eacSmrg  -t DIRECTORY  install into DIRECTORY.
116b7fb5eacSmrg  -T            report an error if DSTFILE is a directory.
1171a30de1fSmrg
1181a30de1fSmrgEnvironment variables override the default commands:
119b7fb5eacSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120b7fb5eacSmrg  RMPROG STRIPPROG
1216eaa481cSmrg
1226eaa481cSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1236eaa481cSmrgit's up to you to specify -f if you want it.
1246eaa481cSmrg
1256eaa481cSmrgIf -S is not specified, no backups are attempted.
1266eaa481cSmrg
1276eaa481cSmrgEmail bug reports to bug-automake@gnu.org.
1286eaa481cSmrgAutomake home page: https://www.gnu.org/software/automake/
1291a30de1fSmrg"
1301a30de1fSmrg
1311a30de1fSmrgwhile test $# -ne 0; do
1321a30de1fSmrg  case $1 in
133b7fb5eacSmrg    -c) ;;
134b7fb5eacSmrg
135b7fb5eacSmrg    -C) copy_on_change=true;;
1361a30de1fSmrg
137b7fb5eacSmrg    -d) dir_arg=true;;
1381a30de1fSmrg
1391a30de1fSmrg    -g) chgrpcmd="$chgrpprog $2"
1401b983734Smrg        shift;;
1411a30de1fSmrg
1421a30de1fSmrg    --help) echo "$usage"; exit $?;;
1431a30de1fSmrg
1441a30de1fSmrg    -m) mode=$2
1451b983734Smrg        case $mode in
1461b983734Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1471b983734Smrg            echo "$0: invalid mode: $mode" >&2
1481b983734Smrg            exit 1;;
1491b983734Smrg        esac
1501b983734Smrg        shift;;
1511a30de1fSmrg
1521a30de1fSmrg    -o) chowncmd="$chownprog $2"
1531b983734Smrg        shift;;
1541a30de1fSmrg
1556eaa481cSmrg    -p) cpprog="$cpprog -p";;
1566eaa481cSmrg
157b7fb5eacSmrg    -s) stripcmd=$stripprog;;
1581a30de1fSmrg
1596eaa481cSmrg    -S) backupsuffix="$2"
1606eaa481cSmrg        shift;;
1616eaa481cSmrg
1621b983734Smrg    -t)
1631b983734Smrg        is_target_a_directory=always
1641b983734Smrg        dst_arg=$2
1651b983734Smrg        # Protect names problematic for 'test' and other utilities.
1661b983734Smrg        case $dst_arg in
1671b983734Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1681b983734Smrg        esac
1691b983734Smrg        shift;;
1701a30de1fSmrg
1711b983734Smrg    -T) is_target_a_directory=never;;
1721a30de1fSmrg
1731a30de1fSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1741a30de1fSmrg
1751b983734Smrg    --) shift
1761b983734Smrg        break;;
1771a30de1fSmrg
1781b983734Smrg    -*) echo "$0: invalid option: $1" >&2
1791b983734Smrg        exit 1;;
1801a30de1fSmrg
1811a30de1fSmrg    *)  break;;
1821a30de1fSmrg  esac
183b7fb5eacSmrg  shift
1841a30de1fSmrgdone
1851a30de1fSmrg
1861b983734Smrg# We allow the use of options -d and -T together, by making -d
1871b983734Smrg# take the precedence; this is for compatibility with GNU install.
1881b983734Smrg
1891b983734Smrgif test -n "$dir_arg"; then
1901b983734Smrg  if test -n "$dst_arg"; then
1911b983734Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1921b983734Smrg    exit 1
1931b983734Smrg  fi
1941b983734Smrgfi
1951b983734Smrg
196b7fb5eacSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1971a30de1fSmrg  # When -d is used, all remaining arguments are directories to create.
1981a30de1fSmrg  # When -t is used, the destination is already specified.
1991a30de1fSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2001a30de1fSmrg  for arg
2011a30de1fSmrg  do
202b7fb5eacSmrg    if test -n "$dst_arg"; then
2031a30de1fSmrg      # $@ is not empty: it contains at least $arg.
204b7fb5eacSmrg      set fnord "$@" "$dst_arg"
2051a30de1fSmrg      shift # fnord
2061a30de1fSmrg    fi
2071a30de1fSmrg    shift # arg
208b7fb5eacSmrg    dst_arg=$arg
209a733a5bfSmrg    # Protect names problematic for 'test' and other utilities.
210a733a5bfSmrg    case $dst_arg in
211a733a5bfSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212a733a5bfSmrg    esac
2131a30de1fSmrg  done
2141a30de1fSmrgfi
2151a30de1fSmrg
2161a30de1fSmrgif test $# -eq 0; then
2171a30de1fSmrg  if test -z "$dir_arg"; then
2181a30de1fSmrg    echo "$0: no input file specified." >&2
2191a30de1fSmrg    exit 1
2201a30de1fSmrg  fi
221a733a5bfSmrg  # It's OK to call 'install-sh -d' without argument.
2221a30de1fSmrg  # This can happen when creating conditional directories.
2231a30de1fSmrg  exit 0
2241a30de1fSmrgfi
2251a30de1fSmrg
2261b983734Smrgif test -z "$dir_arg"; then
2271b983734Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2281b983734Smrg    if test ! -d "$dst_arg"; then
2291b983734Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2301b983734Smrg      exit 1
2311b983734Smrg    fi
2321b983734Smrg  fi
2331b983734Smrgfi
2341b983734Smrg
2351a30de1fSmrgif test -z "$dir_arg"; then
236a733a5bfSmrg  do_exit='(exit $ret); exit $ret'
237a733a5bfSmrg  trap "ret=129; $do_exit" 1
238a733a5bfSmrg  trap "ret=130; $do_exit" 2
239a733a5bfSmrg  trap "ret=141; $do_exit" 13
240a733a5bfSmrg  trap "ret=143; $do_exit" 15
2411a30de1fSmrg
2421a30de1fSmrg  # Set umask so as not to create temps with too-generous modes.
2431a30de1fSmrg  # However, 'strip' requires both read and write access to temps.
2441a30de1fSmrg  case $mode in
2451a30de1fSmrg    # Optimize common cases.
2461a30de1fSmrg    *644) cp_umask=133;;
2471a30de1fSmrg    *755) cp_umask=22;;
2481a30de1fSmrg
2491a30de1fSmrg    *[0-7])
2501a30de1fSmrg      if test -z "$stripcmd"; then
2511b983734Smrg        u_plus_rw=
2521a30de1fSmrg      else
2531b983734Smrg        u_plus_rw='% 200'
2541a30de1fSmrg      fi
2551a30de1fSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2561a30de1fSmrg    *)
2571a30de1fSmrg      if test -z "$stripcmd"; then
2581b983734Smrg        u_plus_rw=
2591a30de1fSmrg      else
2601b983734Smrg        u_plus_rw=,u+rw
2611a30de1fSmrg      fi
2621a30de1fSmrg      cp_umask=$mode$u_plus_rw;;
2631a30de1fSmrg  esac
2641a30de1fSmrgfi
2651a30de1fSmrg
2661a30de1fSmrgfor src
2671a30de1fSmrgdo
268a733a5bfSmrg  # Protect names problematic for 'test' and other utilities.
2691a30de1fSmrg  case $src in
270a733a5bfSmrg    -* | [=\(\)!]) src=./$src;;
2711a30de1fSmrg  esac
2721a30de1fSmrg
2731a30de1fSmrg  if test -n "$dir_arg"; then
2741a30de1fSmrg    dst=$src
2751a30de1fSmrg    dstdir=$dst
2761a30de1fSmrg    test -d "$dstdir"
2771a30de1fSmrg    dstdir_status=$?
2786eaa481cSmrg    # Don't chown directories that already exist.
2796eaa481cSmrg    if test $dstdir_status = 0; then
2806eaa481cSmrg      chowncmd=""
2816eaa481cSmrg    fi
2821a30de1fSmrg  else
2831a30de1fSmrg
2841a30de1fSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2851a30de1fSmrg    # might cause directories to be created, which would be especially bad
2861a30de1fSmrg    # if $src (and thus $dsttmp) contains '*'.
2871a30de1fSmrg    if test ! -f "$src" && test ! -d "$src"; then
2881a30de1fSmrg      echo "$0: $src does not exist." >&2
2891a30de1fSmrg      exit 1
2901a30de1fSmrg    fi
2911a30de1fSmrg
292b7fb5eacSmrg    if test -z "$dst_arg"; then
2931a30de1fSmrg      echo "$0: no destination specified." >&2
2941a30de1fSmrg      exit 1
2951a30de1fSmrg    fi
296b7fb5eacSmrg    dst=$dst_arg
2971a30de1fSmrg
2986eaa481cSmrg    # If destination is a directory, append the input filename.
2991a30de1fSmrg    if test -d "$dst"; then
3001b983734Smrg      if test "$is_target_a_directory" = never; then
3011b983734Smrg        echo "$0: $dst_arg: Is a directory" >&2
3021b983734Smrg        exit 1
3031a30de1fSmrg      fi
3041a30de1fSmrg      dstdir=$dst
3056eaa481cSmrg      dstbase=`basename "$src"`
3066eaa481cSmrg      case $dst in
3076eaa481cSmrg	*/) dst=$dst$dstbase;;
3086eaa481cSmrg	*)  dst=$dst/$dstbase;;
3096eaa481cSmrg      esac
3101a30de1fSmrg      dstdir_status=0
3111a30de1fSmrg    else
3121b983734Smrg      dstdir=`dirname "$dst"`
3131a30de1fSmrg      test -d "$dstdir"
3141a30de1fSmrg      dstdir_status=$?
3151a30de1fSmrg    fi
3161a30de1fSmrg  fi
3171a30de1fSmrg
3186eaa481cSmrg  case $dstdir in
3196eaa481cSmrg    */) dstdirslash=$dstdir;;
3206eaa481cSmrg    *)  dstdirslash=$dstdir/;;
3216eaa481cSmrg  esac
3226eaa481cSmrg
3231a30de1fSmrg  obsolete_mkdir_used=false
3241a30de1fSmrg
3251a30de1fSmrg  if test $dstdir_status != 0; then
3261a30de1fSmrg    case $posix_mkdir in
3271a30de1fSmrg      '')
3281b983734Smrg        # With -d, create the new directory with the user-specified mode.
3291b983734Smrg        # Otherwise, rely on $mkdir_umask.
3301b983734Smrg        if test -n "$dir_arg"; then
3311b983734Smrg          mkdir_mode=-m$mode
3321b983734Smrg        else
3331b983734Smrg          mkdir_mode=
3341b983734Smrg        fi
3351b983734Smrg
3361b983734Smrg        posix_mkdir=false
3376eaa481cSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3386eaa481cSmrg	# here however when possible just to lower collision chance.
3396eaa481cSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3406eaa481cSmrg
3416eaa481cSmrg	trap '
3426eaa481cSmrg	  ret=$?
3436eaa481cSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3446eaa481cSmrg	  exit $ret
3456eaa481cSmrg	' 0
3466eaa481cSmrg
3476eaa481cSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
3486eaa481cSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3496eaa481cSmrg	# directory is successfully created first before we actually test
3506eaa481cSmrg	# 'mkdir -p'.
3516eaa481cSmrg	if (umask $mkdir_umask &&
3526eaa481cSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3536eaa481cSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3546eaa481cSmrg	then
3556eaa481cSmrg	  if test -z "$dir_arg" || {
3566eaa481cSmrg	       # Check for POSIX incompatibilities with -m.
3576eaa481cSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3586eaa481cSmrg	       # other-writable bit of parent directory when it shouldn't.
3596eaa481cSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3606eaa481cSmrg	       test_tmpdir="$tmpdir/a"
3616eaa481cSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3626eaa481cSmrg	       case $ls_ld_tmpdir in
3636eaa481cSmrg		 d????-?r-*) different_mode=700;;
3646eaa481cSmrg		 d????-?--*) different_mode=755;;
3656eaa481cSmrg		 *) false;;
3666eaa481cSmrg	       esac &&
3676eaa481cSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3686eaa481cSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3696eaa481cSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3706eaa481cSmrg	       }
3716eaa481cSmrg	     }
3726eaa481cSmrg	  then posix_mkdir=:
3736eaa481cSmrg	  fi
3746eaa481cSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3756eaa481cSmrg	else
3766eaa481cSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
3776eaa481cSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3786eaa481cSmrg	fi
3796eaa481cSmrg	trap '' 0;;
3801a30de1fSmrg    esac
3811a30de1fSmrg
3821a30de1fSmrg    if
3831a30de1fSmrg      $posix_mkdir && (
3841b983734Smrg        umask $mkdir_umask &&
3851b983734Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3861a30de1fSmrg      )
3871a30de1fSmrg    then :
3881a30de1fSmrg    else
3891a30de1fSmrg
3906eaa481cSmrg      # mkdir does not conform to POSIX,
3911a30de1fSmrg      # or it failed possibly due to a race condition.  Create the
3921a30de1fSmrg      # directory the slow way, step by step, checking for races as we go.
3931a30de1fSmrg
3941a30de1fSmrg      case $dstdir in
3951b983734Smrg        /*) prefix='/';;
3961b983734Smrg        [-=\(\)!]*) prefix='./';;
3971b983734Smrg        *)  prefix='';;
3981a30de1fSmrg      esac
3991a30de1fSmrg
4001a30de1fSmrg      oIFS=$IFS
4011a30de1fSmrg      IFS=/
4021b983734Smrg      set -f
4031a30de1fSmrg      set fnord $dstdir
4041a30de1fSmrg      shift
4051b983734Smrg      set +f
4061a30de1fSmrg      IFS=$oIFS
4071a30de1fSmrg
4081a30de1fSmrg      prefixes=
4091a30de1fSmrg
4101a30de1fSmrg      for d
4111a30de1fSmrg      do
4121b983734Smrg        test X"$d" = X && continue
4131b983734Smrg
4141b983734Smrg        prefix=$prefix$d
4151b983734Smrg        if test -d "$prefix"; then
4161b983734Smrg          prefixes=
4171b983734Smrg        else
4181b983734Smrg          if $posix_mkdir; then
4196eaa481cSmrg            (umask $mkdir_umask &&
4201b983734Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4211b983734Smrg            # Don't fail if two instances are running concurrently.
4221b983734Smrg            test -d "$prefix" || exit 1
4231b983734Smrg          else
4241b983734Smrg            case $prefix in
4251b983734Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4261b983734Smrg              *) qprefix=$prefix;;
4271b983734Smrg            esac
4281b983734Smrg            prefixes="$prefixes '$qprefix'"
4291b983734Smrg          fi
4301b983734Smrg        fi
4311b983734Smrg        prefix=$prefix/
4321a30de1fSmrg      done
4331a30de1fSmrg
4341a30de1fSmrg      if test -n "$prefixes"; then
4351b983734Smrg        # Don't fail if two instances are running concurrently.
4361b983734Smrg        (umask $mkdir_umask &&
4371b983734Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4381b983734Smrg          test -d "$dstdir" || exit 1
4391b983734Smrg        obsolete_mkdir_used=true
4401a30de1fSmrg      fi
4411a30de1fSmrg    fi
4421a30de1fSmrg  fi
4431a30de1fSmrg
4441a30de1fSmrg  if test -n "$dir_arg"; then
4451a30de1fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4461a30de1fSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4471a30de1fSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4481a30de1fSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4491a30de1fSmrg  else
4501a30de1fSmrg
4511a30de1fSmrg    # Make a couple of temp file names in the proper directory.
4526eaa481cSmrg    dsttmp=${dstdirslash}_inst.$$_
4536eaa481cSmrg    rmtmp=${dstdirslash}_rm.$$_
4541a30de1fSmrg
4551a30de1fSmrg    # Trap to clean up those temp files at exit.
4561a30de1fSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4571a30de1fSmrg
4581a30de1fSmrg    # Copy the file name to the temp name.
4596eaa481cSmrg    (umask $cp_umask &&
4606eaa481cSmrg     { test -z "$stripcmd" || {
4616eaa481cSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4626eaa481cSmrg	 # which would cause strip to fail.
4636eaa481cSmrg	 if test -z "$doit"; then
4646eaa481cSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4656eaa481cSmrg	 else
4666eaa481cSmrg	   $doit touch "$dsttmp"
4676eaa481cSmrg	 fi
4686eaa481cSmrg       }
4696eaa481cSmrg     } &&
4706eaa481cSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4711a30de1fSmrg
4721a30de1fSmrg    # and set any options; do chmod last to preserve setuid bits.
4731a30de1fSmrg    #
4741a30de1fSmrg    # If any of these fail, we abort the whole thing.  If we want to
4751a30de1fSmrg    # ignore errors from any of these, just make sure not to ignore
4761a30de1fSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4771a30de1fSmrg    #
478b7fb5eacSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479b7fb5eacSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480b7fb5eacSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481b7fb5eacSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482b7fb5eacSmrg
483b7fb5eacSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484b7fb5eacSmrg    if $copy_on_change &&
4851b983734Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4861b983734Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4871b983734Smrg       set -f &&
488b7fb5eacSmrg       set X $old && old=:$2:$4:$5:$6 &&
489b7fb5eacSmrg       set X $new && new=:$2:$4:$5:$6 &&
4901b983734Smrg       set +f &&
491b7fb5eacSmrg       test "$old" = "$new" &&
492b7fb5eacSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493b7fb5eacSmrg    then
494b7fb5eacSmrg      rm -f "$dsttmp"
495b7fb5eacSmrg    else
4966eaa481cSmrg      # If $backupsuffix is set, and the file being installed
4976eaa481cSmrg      # already exists, attempt a backup.  Don't worry if it fails,
4986eaa481cSmrg      # e.g., if mv doesn't support -f.
4996eaa481cSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
5006eaa481cSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5016eaa481cSmrg      fi
5026eaa481cSmrg
503b7fb5eacSmrg      # Rename the file to the real destination.
504b7fb5eacSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505b7fb5eacSmrg
506b7fb5eacSmrg      # The rename failed, perhaps because mv can't rename something else
507b7fb5eacSmrg      # to itself, or perhaps because mv is so ancient that it does not
508b7fb5eacSmrg      # support -f.
509b7fb5eacSmrg      {
5101b983734Smrg        # Now remove or move aside any old file at destination location.
5111b983734Smrg        # We try this two ways since rm can't unlink itself on some
5121b983734Smrg        # systems and the destination file might be busy for other
5131b983734Smrg        # reasons.  In this case, the final cleanup might fail but the new
5141b983734Smrg        # file should still install successfully.
5151b983734Smrg        {
5161b983734Smrg          test ! -f "$dst" ||
5176eaa481cSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
5181b983734Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5196eaa481cSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5201b983734Smrg          } ||
5211b983734Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5221b983734Smrg            (exit 1); exit 1
5231b983734Smrg          }
5241b983734Smrg        } &&
5251b983734Smrg
5261b983734Smrg        # Now rename the file to the real destination.
5271b983734Smrg        $doit $mvcmd "$dsttmp" "$dst"
528b7fb5eacSmrg      }
529b7fb5eacSmrg    fi || exit 1
5301a30de1fSmrg
5311a30de1fSmrg    trap '' 0
5321a30de1fSmrg  fi
5331a30de1fSmrgdone
5341a30de1fSmrg
5351a30de1fSmrg# Local variables:
5366eaa481cSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
5371a30de1fSmrg# time-stamp-start: "scriptversion="
5381a30de1fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5396eaa481cSmrg# time-stamp-time-zone: "UTC0"
540b7fb5eacSmrg# time-stamp-end: "; # UTC"
5411a30de1fSmrg# End:
542