1ff7e0accSmrg#!/bin/sh
2ff7e0accSmrg# install - install a program, script, or datafile
3ff7e0accSmrg
42163f7f8Smrgscriptversion=2020-11-14.01; # UTC
5ff7e0accSmrg
6ff7e0accSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7ff7e0accSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8ff7e0accSmrg# following copyright and license.
9ff7e0accSmrg#
10ff7e0accSmrg# Copyright (C) 1994 X Consortium
11ff7e0accSmrg#
12ff7e0accSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13ff7e0accSmrg# of this software and associated documentation files (the "Software"), to
14ff7e0accSmrg# deal in the Software without restriction, including without limitation the
15ff7e0accSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16ff7e0accSmrg# sell copies of the Software, and to permit persons to whom the Software is
17ff7e0accSmrg# furnished to do so, subject to the following conditions:
18ff7e0accSmrg#
19ff7e0accSmrg# The above copyright notice and this permission notice shall be included in
20ff7e0accSmrg# all copies or substantial portions of the Software.
21ff7e0accSmrg#
22ff7e0accSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23ff7e0accSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24ff7e0accSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25ff7e0accSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26ff7e0accSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27ff7e0accSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28ff7e0accSmrg#
29ff7e0accSmrg# Except as contained in this notice, the name of the X Consortium shall not
30ff7e0accSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31ff7e0accSmrg# ings in this Software without prior written authorization from the X Consor-
32ff7e0accSmrg# tium.
33ff7e0accSmrg#
34ff7e0accSmrg#
35ff7e0accSmrg# FSF changes to this file are in the public domain.
36ff7e0accSmrg#
37ff7e0accSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3805bee9bcSmrg# 'make' implicit rules from creating a file called install from it
39ff7e0accSmrg# when there is no Makefile.
40ff7e0accSmrg#
41ff7e0accSmrg# This script is compatible with the BSD install script, but was written
42ff7e0accSmrg# from scratch.
43ff7e0accSmrg
440c91c449Smrgtab='	'
45ff7e0accSmrgnl='
46ff7e0accSmrg'
470c91c449SmrgIFS=" $tab$nl"
48ff7e0accSmrg
490c91c449Smrg# Set DOITPROG to "echo" to test this script.
50ff7e0accSmrg
51a005d216Smrgdoit=${DOITPROG-}
520c91c449Smrgdoit_exec=${doit:-exec}
53ff7e0accSmrg
54ff7e0accSmrg# Put in absolute file names if you don't have them in your path;
55ff7e0accSmrg# or use environment vars.
56ff7e0accSmrg
57a005d216Smrgchgrpprog=${CHGRPPROG-chgrp}
58a005d216Smrgchmodprog=${CHMODPROG-chmod}
59a005d216Smrgchownprog=${CHOWNPROG-chown}
60a005d216Smrgcmpprog=${CMPPROG-cmp}
61a005d216Smrgcpprog=${CPPROG-cp}
62a005d216Smrgmkdirprog=${MKDIRPROG-mkdir}
63a005d216Smrgmvprog=${MVPROG-mv}
64a005d216Smrgrmprog=${RMPROG-rm}
65a005d216Smrgstripprog=${STRIPPROG-strip}
66a005d216Smrg
67ff7e0accSmrgposix_mkdir=
68ff7e0accSmrg
69ff7e0accSmrg# Desired mode of installed file.
70ff7e0accSmrgmode=0755
71ff7e0accSmrg
722163f7f8Smrg# Create dirs (including intermediate dirs) using mode 755.
732163f7f8Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
742163f7f8Smrgmkdir_umask=22
752163f7f8Smrg
762163f7f8Smrgbackupsuffix=
77a005d216Smrgchgrpcmd=
78ff7e0accSmrgchmodcmd=$chmodprog
79ff7e0accSmrgchowncmd=
80a005d216Smrgmvcmd=$mvprog
81ff7e0accSmrgrmcmd="$rmprog -f"
82a005d216Smrgstripcmd=
83a005d216Smrg
84ff7e0accSmrgsrc=
85ff7e0accSmrgdst=
86ff7e0accSmrgdir_arg=
87a005d216Smrgdst_arg=
88a005d216Smrg
89a005d216Smrgcopy_on_change=false
900c91c449Smrgis_target_a_directory=possibly
91ff7e0accSmrg
92a005d216Smrgusage="\
93a005d216SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94ff7e0accSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95ff7e0accSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96ff7e0accSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97ff7e0accSmrg
98ff7e0accSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99ff7e0accSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100ff7e0accSmrgIn the 4th, create DIRECTORIES.
101ff7e0accSmrg
102ff7e0accSmrgOptions:
103a005d216Smrg     --help     display this help and exit.
104a005d216Smrg     --version  display version info and exit.
105a005d216Smrg
106a005d216Smrg  -c            (ignored)
1072163f7f8Smrg  -C            install only if different (preserve data modification time)
108a005d216Smrg  -d            create directories instead of installing files.
109a005d216Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110a005d216Smrg  -m MODE       $chmodprog installed files to MODE.
111a005d216Smrg  -o USER       $chownprog installed files to USER.
1122163f7f8Smrg  -p            pass -p to $cpprog.
113a005d216Smrg  -s            $stripprog installed files.
1142163f7f8Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115a005d216Smrg  -t DIRECTORY  install into DIRECTORY.
116a005d216Smrg  -T            report an error if DSTFILE is a directory.
117ff7e0accSmrg
118ff7e0accSmrgEnvironment variables override the default commands:
119a005d216Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120a005d216Smrg  RMPROG STRIPPROG
1212163f7f8Smrg
1222163f7f8SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1232163f7f8Smrgit's up to you to specify -f if you want it.
1242163f7f8Smrg
1252163f7f8SmrgIf -S is not specified, no backups are attempted.
1262163f7f8Smrg
1272163f7f8SmrgEmail bug reports to bug-automake@gnu.org.
1282163f7f8SmrgAutomake home page: https://www.gnu.org/software/automake/
129ff7e0accSmrg"
130ff7e0accSmrg
131ff7e0accSmrgwhile test $# -ne 0; do
132ff7e0accSmrg  case $1 in
133a005d216Smrg    -c) ;;
134a005d216Smrg
135a005d216Smrg    -C) copy_on_change=true;;
136ff7e0accSmrg
137a005d216Smrg    -d) dir_arg=true;;
138ff7e0accSmrg
139ff7e0accSmrg    -g) chgrpcmd="$chgrpprog $2"
1400c91c449Smrg        shift;;
141ff7e0accSmrg
142ff7e0accSmrg    --help) echo "$usage"; exit $?;;
143ff7e0accSmrg
144ff7e0accSmrg    -m) mode=$2
1450c91c449Smrg        case $mode in
1460c91c449Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1470c91c449Smrg            echo "$0: invalid mode: $mode" >&2
1480c91c449Smrg            exit 1;;
1490c91c449Smrg        esac
1500c91c449Smrg        shift;;
151ff7e0accSmrg
152ff7e0accSmrg    -o) chowncmd="$chownprog $2"
1530c91c449Smrg        shift;;
154ff7e0accSmrg
1552163f7f8Smrg    -p) cpprog="$cpprog -p";;
1562163f7f8Smrg
157a005d216Smrg    -s) stripcmd=$stripprog;;
158ff7e0accSmrg
1592163f7f8Smrg    -S) backupsuffix="$2"
1602163f7f8Smrg        shift;;
1612163f7f8Smrg
1620c91c449Smrg    -t)
1630c91c449Smrg        is_target_a_directory=always
1640c91c449Smrg        dst_arg=$2
1650c91c449Smrg        # Protect names problematic for 'test' and other utilities.
1660c91c449Smrg        case $dst_arg in
1670c91c449Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1680c91c449Smrg        esac
1690c91c449Smrg        shift;;
170ff7e0accSmrg
1710c91c449Smrg    -T) is_target_a_directory=never;;
172ff7e0accSmrg
173ff7e0accSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174ff7e0accSmrg
1750c91c449Smrg    --) shift
1760c91c449Smrg        break;;
177ff7e0accSmrg
1780c91c449Smrg    -*) echo "$0: invalid option: $1" >&2
1790c91c449Smrg        exit 1;;
180ff7e0accSmrg
181ff7e0accSmrg    *)  break;;
182ff7e0accSmrg  esac
183a005d216Smrg  shift
184ff7e0accSmrgdone
185ff7e0accSmrg
1860c91c449Smrg# We allow the use of options -d and -T together, by making -d
1870c91c449Smrg# take the precedence; this is for compatibility with GNU install.
1880c91c449Smrg
1890c91c449Smrgif test -n "$dir_arg"; then
1900c91c449Smrg  if test -n "$dst_arg"; then
1910c91c449Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1920c91c449Smrg    exit 1
1930c91c449Smrg  fi
1940c91c449Smrgfi
1950c91c449Smrg
196a005d216Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197ff7e0accSmrg  # When -d is used, all remaining arguments are directories to create.
198ff7e0accSmrg  # When -t is used, the destination is already specified.
199ff7e0accSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200ff7e0accSmrg  for arg
201ff7e0accSmrg  do
202a005d216Smrg    if test -n "$dst_arg"; then
203ff7e0accSmrg      # $@ is not empty: it contains at least $arg.
204a005d216Smrg      set fnord "$@" "$dst_arg"
205ff7e0accSmrg      shift # fnord
206ff7e0accSmrg    fi
207ff7e0accSmrg    shift # arg
208a005d216Smrg    dst_arg=$arg
20905bee9bcSmrg    # Protect names problematic for 'test' and other utilities.
21005bee9bcSmrg    case $dst_arg in
21105bee9bcSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21205bee9bcSmrg    esac
213ff7e0accSmrg  done
214ff7e0accSmrgfi
215ff7e0accSmrg
216ff7e0accSmrgif test $# -eq 0; then
217ff7e0accSmrg  if test -z "$dir_arg"; then
218ff7e0accSmrg    echo "$0: no input file specified." >&2
219ff7e0accSmrg    exit 1
220ff7e0accSmrg  fi
22105bee9bcSmrg  # It's OK to call 'install-sh -d' without argument.
222ff7e0accSmrg  # This can happen when creating conditional directories.
223ff7e0accSmrg  exit 0
224ff7e0accSmrgfi
225ff7e0accSmrg
2260c91c449Smrgif test -z "$dir_arg"; then
2270c91c449Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2280c91c449Smrg    if test ! -d "$dst_arg"; then
2290c91c449Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2300c91c449Smrg      exit 1
2310c91c449Smrg    fi
2320c91c449Smrg  fi
2330c91c449Smrgfi
2340c91c449Smrg
235ff7e0accSmrgif test -z "$dir_arg"; then
23605bee9bcSmrg  do_exit='(exit $ret); exit $ret'
23705bee9bcSmrg  trap "ret=129; $do_exit" 1
23805bee9bcSmrg  trap "ret=130; $do_exit" 2
23905bee9bcSmrg  trap "ret=141; $do_exit" 13
24005bee9bcSmrg  trap "ret=143; $do_exit" 15
241ff7e0accSmrg
242ff7e0accSmrg  # Set umask so as not to create temps with too-generous modes.
243ff7e0accSmrg  # However, 'strip' requires both read and write access to temps.
244ff7e0accSmrg  case $mode in
245ff7e0accSmrg    # Optimize common cases.
246ff7e0accSmrg    *644) cp_umask=133;;
247ff7e0accSmrg    *755) cp_umask=22;;
248ff7e0accSmrg
249ff7e0accSmrg    *[0-7])
250ff7e0accSmrg      if test -z "$stripcmd"; then
2510c91c449Smrg        u_plus_rw=
252ff7e0accSmrg      else
2530c91c449Smrg        u_plus_rw='% 200'
254ff7e0accSmrg      fi
255ff7e0accSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256ff7e0accSmrg    *)
257ff7e0accSmrg      if test -z "$stripcmd"; then
2580c91c449Smrg        u_plus_rw=
259ff7e0accSmrg      else
2600c91c449Smrg        u_plus_rw=,u+rw
261ff7e0accSmrg      fi
262ff7e0accSmrg      cp_umask=$mode$u_plus_rw;;
263ff7e0accSmrg  esac
264ff7e0accSmrgfi
265ff7e0accSmrg
266ff7e0accSmrgfor src
267ff7e0accSmrgdo
26805bee9bcSmrg  # Protect names problematic for 'test' and other utilities.
269ff7e0accSmrg  case $src in
27005bee9bcSmrg    -* | [=\(\)!]) src=./$src;;
271ff7e0accSmrg  esac
272ff7e0accSmrg
273ff7e0accSmrg  if test -n "$dir_arg"; then
274ff7e0accSmrg    dst=$src
275ff7e0accSmrg    dstdir=$dst
276ff7e0accSmrg    test -d "$dstdir"
277ff7e0accSmrg    dstdir_status=$?
2782163f7f8Smrg    # Don't chown directories that already exist.
2792163f7f8Smrg    if test $dstdir_status = 0; then
2802163f7f8Smrg      chowncmd=""
2812163f7f8Smrg    fi
282ff7e0accSmrg  else
283ff7e0accSmrg
284ff7e0accSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285ff7e0accSmrg    # might cause directories to be created, which would be especially bad
286ff7e0accSmrg    # if $src (and thus $dsttmp) contains '*'.
287ff7e0accSmrg    if test ! -f "$src" && test ! -d "$src"; then
288ff7e0accSmrg      echo "$0: $src does not exist." >&2
289ff7e0accSmrg      exit 1
290ff7e0accSmrg    fi
291ff7e0accSmrg
292a005d216Smrg    if test -z "$dst_arg"; then
293ff7e0accSmrg      echo "$0: no destination specified." >&2
294ff7e0accSmrg      exit 1
295ff7e0accSmrg    fi
296a005d216Smrg    dst=$dst_arg
297ff7e0accSmrg
2982163f7f8Smrg    # If destination is a directory, append the input filename.
299ff7e0accSmrg    if test -d "$dst"; then
3000c91c449Smrg      if test "$is_target_a_directory" = never; then
3010c91c449Smrg        echo "$0: $dst_arg: Is a directory" >&2
3020c91c449Smrg        exit 1
303ff7e0accSmrg      fi
304ff7e0accSmrg      dstdir=$dst
3052163f7f8Smrg      dstbase=`basename "$src"`
3062163f7f8Smrg      case $dst in
3072163f7f8Smrg	*/) dst=$dst$dstbase;;
3082163f7f8Smrg	*)  dst=$dst/$dstbase;;
3092163f7f8Smrg      esac
310ff7e0accSmrg      dstdir_status=0
311ff7e0accSmrg    else
3120c91c449Smrg      dstdir=`dirname "$dst"`
313ff7e0accSmrg      test -d "$dstdir"
314ff7e0accSmrg      dstdir_status=$?
315ff7e0accSmrg    fi
316ff7e0accSmrg  fi
317ff7e0accSmrg
3182163f7f8Smrg  case $dstdir in
3192163f7f8Smrg    */) dstdirslash=$dstdir;;
3202163f7f8Smrg    *)  dstdirslash=$dstdir/;;
3212163f7f8Smrg  esac
3222163f7f8Smrg
323ff7e0accSmrg  obsolete_mkdir_used=false
324ff7e0accSmrg
325ff7e0accSmrg  if test $dstdir_status != 0; then
326ff7e0accSmrg    case $posix_mkdir in
327ff7e0accSmrg      '')
3280c91c449Smrg        # With -d, create the new directory with the user-specified mode.
3290c91c449Smrg        # Otherwise, rely on $mkdir_umask.
3300c91c449Smrg        if test -n "$dir_arg"; then
3310c91c449Smrg          mkdir_mode=-m$mode
3320c91c449Smrg        else
3330c91c449Smrg          mkdir_mode=
3340c91c449Smrg        fi
3350c91c449Smrg
3360c91c449Smrg        posix_mkdir=false
3372163f7f8Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3382163f7f8Smrg	# here however when possible just to lower collision chance.
3392163f7f8Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3402163f7f8Smrg
3412163f7f8Smrg	trap '
3422163f7f8Smrg	  ret=$?
3432163f7f8Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3442163f7f8Smrg	  exit $ret
3452163f7f8Smrg	' 0
3462163f7f8Smrg
3472163f7f8Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3482163f7f8Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3492163f7f8Smrg	# directory is successfully created first before we actually test
3502163f7f8Smrg	# 'mkdir -p'.
3512163f7f8Smrg	if (umask $mkdir_umask &&
3522163f7f8Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3532163f7f8Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3542163f7f8Smrg	then
3552163f7f8Smrg	  if test -z "$dir_arg" || {
3562163f7f8Smrg	       # Check for POSIX incompatibilities with -m.
3572163f7f8Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3582163f7f8Smrg	       # other-writable bit of parent directory when it shouldn't.
3592163f7f8Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3602163f7f8Smrg	       test_tmpdir="$tmpdir/a"
3612163f7f8Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3622163f7f8Smrg	       case $ls_ld_tmpdir in
3632163f7f8Smrg		 d????-?r-*) different_mode=700;;
3642163f7f8Smrg		 d????-?--*) different_mode=755;;
3652163f7f8Smrg		 *) false;;
3662163f7f8Smrg	       esac &&
3672163f7f8Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3682163f7f8Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3692163f7f8Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3702163f7f8Smrg	       }
3712163f7f8Smrg	     }
3722163f7f8Smrg	  then posix_mkdir=:
3732163f7f8Smrg	  fi
3742163f7f8Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3752163f7f8Smrg	else
3762163f7f8Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
3772163f7f8Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3782163f7f8Smrg	fi
3792163f7f8Smrg	trap '' 0;;
380ff7e0accSmrg    esac
381ff7e0accSmrg
382ff7e0accSmrg    if
383ff7e0accSmrg      $posix_mkdir && (
3840c91c449Smrg        umask $mkdir_umask &&
3850c91c449Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386ff7e0accSmrg      )
387ff7e0accSmrg    then :
388ff7e0accSmrg    else
389ff7e0accSmrg
3902163f7f8Smrg      # mkdir does not conform to POSIX,
391ff7e0accSmrg      # or it failed possibly due to a race condition.  Create the
392ff7e0accSmrg      # directory the slow way, step by step, checking for races as we go.
393ff7e0accSmrg
394ff7e0accSmrg      case $dstdir in
3950c91c449Smrg        /*) prefix='/';;
3960c91c449Smrg        [-=\(\)!]*) prefix='./';;
3970c91c449Smrg        *)  prefix='';;
398ff7e0accSmrg      esac
399ff7e0accSmrg
400ff7e0accSmrg      oIFS=$IFS
401ff7e0accSmrg      IFS=/
4020c91c449Smrg      set -f
403ff7e0accSmrg      set fnord $dstdir
404ff7e0accSmrg      shift
4050c91c449Smrg      set +f
406ff7e0accSmrg      IFS=$oIFS
407ff7e0accSmrg
408ff7e0accSmrg      prefixes=
409ff7e0accSmrg
410ff7e0accSmrg      for d
411ff7e0accSmrg      do
4120c91c449Smrg        test X"$d" = X && continue
4130c91c449Smrg
4140c91c449Smrg        prefix=$prefix$d
4150c91c449Smrg        if test -d "$prefix"; then
4160c91c449Smrg          prefixes=
4170c91c449Smrg        else
4180c91c449Smrg          if $posix_mkdir; then
4192163f7f8Smrg            (umask $mkdir_umask &&
4200c91c449Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4210c91c449Smrg            # Don't fail if two instances are running concurrently.
4220c91c449Smrg            test -d "$prefix" || exit 1
4230c91c449Smrg          else
4240c91c449Smrg            case $prefix in
4250c91c449Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4260c91c449Smrg              *) qprefix=$prefix;;
4270c91c449Smrg            esac
4280c91c449Smrg            prefixes="$prefixes '$qprefix'"
4290c91c449Smrg          fi
4300c91c449Smrg        fi
4310c91c449Smrg        prefix=$prefix/
432ff7e0accSmrg      done
433ff7e0accSmrg
434ff7e0accSmrg      if test -n "$prefixes"; then
4350c91c449Smrg        # Don't fail if two instances are running concurrently.
4360c91c449Smrg        (umask $mkdir_umask &&
4370c91c449Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4380c91c449Smrg          test -d "$dstdir" || exit 1
4390c91c449Smrg        obsolete_mkdir_used=true
440ff7e0accSmrg      fi
441ff7e0accSmrg    fi
442ff7e0accSmrg  fi
443ff7e0accSmrg
444ff7e0accSmrg  if test -n "$dir_arg"; then
445ff7e0accSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446ff7e0accSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447ff7e0accSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448ff7e0accSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449ff7e0accSmrg  else
450ff7e0accSmrg
451ff7e0accSmrg    # Make a couple of temp file names in the proper directory.
4522163f7f8Smrg    dsttmp=${dstdirslash}_inst.$$_
4532163f7f8Smrg    rmtmp=${dstdirslash}_rm.$$_
454ff7e0accSmrg
455ff7e0accSmrg    # Trap to clean up those temp files at exit.
456ff7e0accSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457ff7e0accSmrg
458ff7e0accSmrg    # Copy the file name to the temp name.
4592163f7f8Smrg    (umask $cp_umask &&
4602163f7f8Smrg     { test -z "$stripcmd" || {
4612163f7f8Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4622163f7f8Smrg	 # which would cause strip to fail.
4632163f7f8Smrg	 if test -z "$doit"; then
4642163f7f8Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4652163f7f8Smrg	 else
4662163f7f8Smrg	   $doit touch "$dsttmp"
4672163f7f8Smrg	 fi
4682163f7f8Smrg       }
4692163f7f8Smrg     } &&
4702163f7f8Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471ff7e0accSmrg
472ff7e0accSmrg    # and set any options; do chmod last to preserve setuid bits.
473ff7e0accSmrg    #
474ff7e0accSmrg    # If any of these fail, we abort the whole thing.  If we want to
475ff7e0accSmrg    # ignore errors from any of these, just make sure not to ignore
476ff7e0accSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477ff7e0accSmrg    #
478a005d216Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479a005d216Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480a005d216Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481a005d216Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482a005d216Smrg
483a005d216Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484a005d216Smrg    if $copy_on_change &&
4850c91c449Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4860c91c449Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4870c91c449Smrg       set -f &&
488a005d216Smrg       set X $old && old=:$2:$4:$5:$6 &&
489a005d216Smrg       set X $new && new=:$2:$4:$5:$6 &&
4900c91c449Smrg       set +f &&
491a005d216Smrg       test "$old" = "$new" &&
492a005d216Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493a005d216Smrg    then
494a005d216Smrg      rm -f "$dsttmp"
495a005d216Smrg    else
4962163f7f8Smrg      # If $backupsuffix is set, and the file being installed
4972163f7f8Smrg      # already exists, attempt a backup.  Don't worry if it fails,
4982163f7f8Smrg      # e.g., if mv doesn't support -f.
4992163f7f8Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
5002163f7f8Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5012163f7f8Smrg      fi
5022163f7f8Smrg
503a005d216Smrg      # Rename the file to the real destination.
504a005d216Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505a005d216Smrg
506a005d216Smrg      # The rename failed, perhaps because mv can't rename something else
507a005d216Smrg      # to itself, or perhaps because mv is so ancient that it does not
508a005d216Smrg      # support -f.
509a005d216Smrg      {
5100c91c449Smrg        # Now remove or move aside any old file at destination location.
5110c91c449Smrg        # We try this two ways since rm can't unlink itself on some
5120c91c449Smrg        # systems and the destination file might be busy for other
5130c91c449Smrg        # reasons.  In this case, the final cleanup might fail but the new
5140c91c449Smrg        # file should still install successfully.
5150c91c449Smrg        {
5160c91c449Smrg          test ! -f "$dst" ||
5172163f7f8Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
5180c91c449Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5192163f7f8Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5200c91c449Smrg          } ||
5210c91c449Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5220c91c449Smrg            (exit 1); exit 1
5230c91c449Smrg          }
5240c91c449Smrg        } &&
5250c91c449Smrg
5260c91c449Smrg        # Now rename the file to the real destination.
5270c91c449Smrg        $doit $mvcmd "$dsttmp" "$dst"
528a005d216Smrg      }
529a005d216Smrg    fi || exit 1
530ff7e0accSmrg
531ff7e0accSmrg    trap '' 0
532ff7e0accSmrg  fi
533ff7e0accSmrgdone
534ff7e0accSmrg
535ff7e0accSmrg# Local variables:
5362163f7f8Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537ff7e0accSmrg# time-stamp-start: "scriptversion="
538ff7e0accSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5392163f7f8Smrg# time-stamp-time-zone: "UTC0"
54010998002Smrg# time-stamp-end: "; # UTC"
541ff7e0accSmrg# End:
542