1a0195d5fSmrg#!/bin/sh
2a0195d5fSmrg# install - install a program, script, or datafile
3a0195d5fSmrg
40f5b81e5Smrgscriptversion=2020-11-14.01; # UTC
5a0195d5fSmrg
6a0195d5fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7a0195d5fSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8a0195d5fSmrg# following copyright and license.
9a0195d5fSmrg#
10a0195d5fSmrg# Copyright (C) 1994 X Consortium
11a0195d5fSmrg#
12a0195d5fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13a0195d5fSmrg# of this software and associated documentation files (the "Software"), to
14a0195d5fSmrg# deal in the Software without restriction, including without limitation the
15a0195d5fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16a0195d5fSmrg# sell copies of the Software, and to permit persons to whom the Software is
17a0195d5fSmrg# furnished to do so, subject to the following conditions:
18a0195d5fSmrg#
19a0195d5fSmrg# The above copyright notice and this permission notice shall be included in
20a0195d5fSmrg# all copies or substantial portions of the Software.
21a0195d5fSmrg#
22a0195d5fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a0195d5fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a0195d5fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25a0195d5fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26a0195d5fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27a0195d5fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a0195d5fSmrg#
29a0195d5fSmrg# Except as contained in this notice, the name of the X Consortium shall not
30a0195d5fSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31a0195d5fSmrg# ings in this Software without prior written authorization from the X Consor-
32a0195d5fSmrg# tium.
33a0195d5fSmrg#
34a0195d5fSmrg#
35a0195d5fSmrg# FSF changes to this file are in the public domain.
36a0195d5fSmrg#
37a0195d5fSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3840a76396Smrg# 'make' implicit rules from creating a file called install from it
39a0195d5fSmrg# when there is no Makefile.
40a0195d5fSmrg#
41a0195d5fSmrg# This script is compatible with the BSD install script, but was written
42a0195d5fSmrg# from scratch.
43a0195d5fSmrg
44feee4651Smrgtab='	'
45a0195d5fSmrgnl='
46a0195d5fSmrg'
47feee4651SmrgIFS=" $tab$nl"
48a0195d5fSmrg
49feee4651Smrg# Set DOITPROG to "echo" to test this script.
50a0195d5fSmrg
51a0195d5fSmrgdoit=${DOITPROG-}
52feee4651Smrgdoit_exec=${doit:-exec}
53a0195d5fSmrg
54a0195d5fSmrg# Put in absolute file names if you don't have them in your path;
55a0195d5fSmrg# or use environment vars.
56a0195d5fSmrg
57a0195d5fSmrgchgrpprog=${CHGRPPROG-chgrp}
58a0195d5fSmrgchmodprog=${CHMODPROG-chmod}
59a0195d5fSmrgchownprog=${CHOWNPROG-chown}
60a0195d5fSmrgcmpprog=${CMPPROG-cmp}
61a0195d5fSmrgcpprog=${CPPROG-cp}
62a0195d5fSmrgmkdirprog=${MKDIRPROG-mkdir}
63a0195d5fSmrgmvprog=${MVPROG-mv}
64a0195d5fSmrgrmprog=${RMPROG-rm}
65a0195d5fSmrgstripprog=${STRIPPROG-strip}
66a0195d5fSmrg
67a0195d5fSmrgposix_mkdir=
68a0195d5fSmrg
69a0195d5fSmrg# Desired mode of installed file.
70a0195d5fSmrgmode=0755
71a0195d5fSmrg
720f5b81e5Smrg# Create dirs (including intermediate dirs) using mode 755.
730f5b81e5Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
740f5b81e5Smrgmkdir_umask=22
750f5b81e5Smrg
760f5b81e5Smrgbackupsuffix=
77a0195d5fSmrgchgrpcmd=
78a0195d5fSmrgchmodcmd=$chmodprog
79a0195d5fSmrgchowncmd=
80a0195d5fSmrgmvcmd=$mvprog
81a0195d5fSmrgrmcmd="$rmprog -f"
82a0195d5fSmrgstripcmd=
83a0195d5fSmrg
84a0195d5fSmrgsrc=
85a0195d5fSmrgdst=
86a0195d5fSmrgdir_arg=
87a0195d5fSmrgdst_arg=
88a0195d5fSmrg
89a0195d5fSmrgcopy_on_change=false
90feee4651Smrgis_target_a_directory=possibly
91a0195d5fSmrg
92a0195d5fSmrgusage="\
93a0195d5fSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94a0195d5fSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95a0195d5fSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96a0195d5fSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97a0195d5fSmrg
98a0195d5fSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99a0195d5fSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100a0195d5fSmrgIn the 4th, create DIRECTORIES.
101a0195d5fSmrg
102a0195d5fSmrgOptions:
103a0195d5fSmrg     --help     display this help and exit.
104a0195d5fSmrg     --version  display version info and exit.
105a0195d5fSmrg
106a0195d5fSmrg  -c            (ignored)
1070f5b81e5Smrg  -C            install only if different (preserve data modification time)
108a0195d5fSmrg  -d            create directories instead of installing files.
109a0195d5fSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110a0195d5fSmrg  -m MODE       $chmodprog installed files to MODE.
111a0195d5fSmrg  -o USER       $chownprog installed files to USER.
1120f5b81e5Smrg  -p            pass -p to $cpprog.
113a0195d5fSmrg  -s            $stripprog installed files.
1140f5b81e5Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115a0195d5fSmrg  -t DIRECTORY  install into DIRECTORY.
116a0195d5fSmrg  -T            report an error if DSTFILE is a directory.
117a0195d5fSmrg
118a0195d5fSmrgEnvironment variables override the default commands:
119a0195d5fSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120a0195d5fSmrg  RMPROG STRIPPROG
1210f5b81e5Smrg
1220f5b81e5SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1230f5b81e5Smrgit's up to you to specify -f if you want it.
1240f5b81e5Smrg
1250f5b81e5SmrgIf -S is not specified, no backups are attempted.
1260f5b81e5Smrg
1270f5b81e5SmrgEmail bug reports to bug-automake@gnu.org.
1280f5b81e5SmrgAutomake home page: https://www.gnu.org/software/automake/
129a0195d5fSmrg"
130a0195d5fSmrg
131a0195d5fSmrgwhile test $# -ne 0; do
132a0195d5fSmrg  case $1 in
133a0195d5fSmrg    -c) ;;
134a0195d5fSmrg
135a0195d5fSmrg    -C) copy_on_change=true;;
136a0195d5fSmrg
137a0195d5fSmrg    -d) dir_arg=true;;
138a0195d5fSmrg
139a0195d5fSmrg    -g) chgrpcmd="$chgrpprog $2"
140feee4651Smrg        shift;;
141a0195d5fSmrg
142a0195d5fSmrg    --help) echo "$usage"; exit $?;;
143a0195d5fSmrg
144a0195d5fSmrg    -m) mode=$2
145feee4651Smrg        case $mode in
146feee4651Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147feee4651Smrg            echo "$0: invalid mode: $mode" >&2
148feee4651Smrg            exit 1;;
149feee4651Smrg        esac
150feee4651Smrg        shift;;
151a0195d5fSmrg
152a0195d5fSmrg    -o) chowncmd="$chownprog $2"
153feee4651Smrg        shift;;
154a0195d5fSmrg
1550f5b81e5Smrg    -p) cpprog="$cpprog -p";;
1560f5b81e5Smrg
157a0195d5fSmrg    -s) stripcmd=$stripprog;;
158a0195d5fSmrg
1590f5b81e5Smrg    -S) backupsuffix="$2"
1600f5b81e5Smrg        shift;;
1610f5b81e5Smrg
162feee4651Smrg    -t)
163feee4651Smrg        is_target_a_directory=always
164feee4651Smrg        dst_arg=$2
165feee4651Smrg        # Protect names problematic for 'test' and other utilities.
166feee4651Smrg        case $dst_arg in
167feee4651Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168feee4651Smrg        esac
169feee4651Smrg        shift;;
170a0195d5fSmrg
171feee4651Smrg    -T) is_target_a_directory=never;;
172a0195d5fSmrg
173a0195d5fSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174a0195d5fSmrg
175feee4651Smrg    --) shift
176feee4651Smrg        break;;
177a0195d5fSmrg
178feee4651Smrg    -*) echo "$0: invalid option: $1" >&2
179feee4651Smrg        exit 1;;
180a0195d5fSmrg
181a0195d5fSmrg    *)  break;;
182a0195d5fSmrg  esac
183a0195d5fSmrg  shift
184a0195d5fSmrgdone
185a0195d5fSmrg
186feee4651Smrg# We allow the use of options -d and -T together, by making -d
187feee4651Smrg# take the precedence; this is for compatibility with GNU install.
188feee4651Smrg
189feee4651Smrgif test -n "$dir_arg"; then
190feee4651Smrg  if test -n "$dst_arg"; then
191feee4651Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192feee4651Smrg    exit 1
193feee4651Smrg  fi
194feee4651Smrgfi
195feee4651Smrg
196a0195d5fSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197a0195d5fSmrg  # When -d is used, all remaining arguments are directories to create.
198a0195d5fSmrg  # When -t is used, the destination is already specified.
199a0195d5fSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200a0195d5fSmrg  for arg
201a0195d5fSmrg  do
202a0195d5fSmrg    if test -n "$dst_arg"; then
203a0195d5fSmrg      # $@ is not empty: it contains at least $arg.
204a0195d5fSmrg      set fnord "$@" "$dst_arg"
205a0195d5fSmrg      shift # fnord
206a0195d5fSmrg    fi
207a0195d5fSmrg    shift # arg
208a0195d5fSmrg    dst_arg=$arg
20940a76396Smrg    # Protect names problematic for 'test' and other utilities.
21040a76396Smrg    case $dst_arg in
21140a76396Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21240a76396Smrg    esac
213a0195d5fSmrg  done
214a0195d5fSmrgfi
215a0195d5fSmrg
216a0195d5fSmrgif test $# -eq 0; then
217a0195d5fSmrg  if test -z "$dir_arg"; then
218a0195d5fSmrg    echo "$0: no input file specified." >&2
219a0195d5fSmrg    exit 1
220a0195d5fSmrg  fi
22140a76396Smrg  # It's OK to call 'install-sh -d' without argument.
222a0195d5fSmrg  # This can happen when creating conditional directories.
223a0195d5fSmrg  exit 0
224a0195d5fSmrgfi
225a0195d5fSmrg
226feee4651Smrgif test -z "$dir_arg"; then
227feee4651Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228feee4651Smrg    if test ! -d "$dst_arg"; then
229feee4651Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230feee4651Smrg      exit 1
231feee4651Smrg    fi
232feee4651Smrg  fi
233feee4651Smrgfi
234feee4651Smrg
235a0195d5fSmrgif test -z "$dir_arg"; then
23640a76396Smrg  do_exit='(exit $ret); exit $ret'
23740a76396Smrg  trap "ret=129; $do_exit" 1
23840a76396Smrg  trap "ret=130; $do_exit" 2
23940a76396Smrg  trap "ret=141; $do_exit" 13
24040a76396Smrg  trap "ret=143; $do_exit" 15
241a0195d5fSmrg
242a0195d5fSmrg  # Set umask so as not to create temps with too-generous modes.
243a0195d5fSmrg  # However, 'strip' requires both read and write access to temps.
244a0195d5fSmrg  case $mode in
245a0195d5fSmrg    # Optimize common cases.
246a0195d5fSmrg    *644) cp_umask=133;;
247a0195d5fSmrg    *755) cp_umask=22;;
248a0195d5fSmrg
249a0195d5fSmrg    *[0-7])
250a0195d5fSmrg      if test -z "$stripcmd"; then
251feee4651Smrg        u_plus_rw=
252a0195d5fSmrg      else
253feee4651Smrg        u_plus_rw='% 200'
254a0195d5fSmrg      fi
255a0195d5fSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256a0195d5fSmrg    *)
257a0195d5fSmrg      if test -z "$stripcmd"; then
258feee4651Smrg        u_plus_rw=
259a0195d5fSmrg      else
260feee4651Smrg        u_plus_rw=,u+rw
261a0195d5fSmrg      fi
262a0195d5fSmrg      cp_umask=$mode$u_plus_rw;;
263a0195d5fSmrg  esac
264a0195d5fSmrgfi
265a0195d5fSmrg
266a0195d5fSmrgfor src
267a0195d5fSmrgdo
26840a76396Smrg  # Protect names problematic for 'test' and other utilities.
269a0195d5fSmrg  case $src in
27040a76396Smrg    -* | [=\(\)!]) src=./$src;;
271a0195d5fSmrg  esac
272a0195d5fSmrg
273a0195d5fSmrg  if test -n "$dir_arg"; then
274a0195d5fSmrg    dst=$src
275a0195d5fSmrg    dstdir=$dst
276a0195d5fSmrg    test -d "$dstdir"
277a0195d5fSmrg    dstdir_status=$?
2780f5b81e5Smrg    # Don't chown directories that already exist.
2790f5b81e5Smrg    if test $dstdir_status = 0; then
2800f5b81e5Smrg      chowncmd=""
2810f5b81e5Smrg    fi
282a0195d5fSmrg  else
283a0195d5fSmrg
284a0195d5fSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285a0195d5fSmrg    # might cause directories to be created, which would be especially bad
286a0195d5fSmrg    # if $src (and thus $dsttmp) contains '*'.
287a0195d5fSmrg    if test ! -f "$src" && test ! -d "$src"; then
288a0195d5fSmrg      echo "$0: $src does not exist." >&2
289a0195d5fSmrg      exit 1
290a0195d5fSmrg    fi
291a0195d5fSmrg
292a0195d5fSmrg    if test -z "$dst_arg"; then
293a0195d5fSmrg      echo "$0: no destination specified." >&2
294a0195d5fSmrg      exit 1
295a0195d5fSmrg    fi
296a0195d5fSmrg    dst=$dst_arg
297a0195d5fSmrg
2980f5b81e5Smrg    # If destination is a directory, append the input filename.
299a0195d5fSmrg    if test -d "$dst"; then
300feee4651Smrg      if test "$is_target_a_directory" = never; then
301feee4651Smrg        echo "$0: $dst_arg: Is a directory" >&2
302feee4651Smrg        exit 1
303a0195d5fSmrg      fi
304a0195d5fSmrg      dstdir=$dst
3050f5b81e5Smrg      dstbase=`basename "$src"`
3060f5b81e5Smrg      case $dst in
3070f5b81e5Smrg	*/) dst=$dst$dstbase;;
3080f5b81e5Smrg	*)  dst=$dst/$dstbase;;
3090f5b81e5Smrg      esac
310a0195d5fSmrg      dstdir_status=0
311a0195d5fSmrg    else
312feee4651Smrg      dstdir=`dirname "$dst"`
313a0195d5fSmrg      test -d "$dstdir"
314a0195d5fSmrg      dstdir_status=$?
315a0195d5fSmrg    fi
316a0195d5fSmrg  fi
317a0195d5fSmrg
3180f5b81e5Smrg  case $dstdir in
3190f5b81e5Smrg    */) dstdirslash=$dstdir;;
3200f5b81e5Smrg    *)  dstdirslash=$dstdir/;;
3210f5b81e5Smrg  esac
3220f5b81e5Smrg
323a0195d5fSmrg  obsolete_mkdir_used=false
324a0195d5fSmrg
325a0195d5fSmrg  if test $dstdir_status != 0; then
326a0195d5fSmrg    case $posix_mkdir in
327a0195d5fSmrg      '')
328feee4651Smrg        # With -d, create the new directory with the user-specified mode.
329feee4651Smrg        # Otherwise, rely on $mkdir_umask.
330feee4651Smrg        if test -n "$dir_arg"; then
331feee4651Smrg          mkdir_mode=-m$mode
332feee4651Smrg        else
333feee4651Smrg          mkdir_mode=
334feee4651Smrg        fi
335feee4651Smrg
336feee4651Smrg        posix_mkdir=false
3370f5b81e5Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3380f5b81e5Smrg	# here however when possible just to lower collision chance.
3390f5b81e5Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3400f5b81e5Smrg
3410f5b81e5Smrg	trap '
3420f5b81e5Smrg	  ret=$?
3430f5b81e5Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3440f5b81e5Smrg	  exit $ret
3450f5b81e5Smrg	' 0
3460f5b81e5Smrg
3470f5b81e5Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3480f5b81e5Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3490f5b81e5Smrg	# directory is successfully created first before we actually test
3500f5b81e5Smrg	# 'mkdir -p'.
3510f5b81e5Smrg	if (umask $mkdir_umask &&
3520f5b81e5Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3530f5b81e5Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3540f5b81e5Smrg	then
3550f5b81e5Smrg	  if test -z "$dir_arg" || {
3560f5b81e5Smrg	       # Check for POSIX incompatibilities with -m.
3570f5b81e5Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3580f5b81e5Smrg	       # other-writable bit of parent directory when it shouldn't.
3590f5b81e5Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3600f5b81e5Smrg	       test_tmpdir="$tmpdir/a"
3610f5b81e5Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3620f5b81e5Smrg	       case $ls_ld_tmpdir in
3630f5b81e5Smrg		 d????-?r-*) different_mode=700;;
3640f5b81e5Smrg		 d????-?--*) different_mode=755;;
3650f5b81e5Smrg		 *) false;;
3660f5b81e5Smrg	       esac &&
3670f5b81e5Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3680f5b81e5Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3690f5b81e5Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3700f5b81e5Smrg	       }
3710f5b81e5Smrg	     }
3720f5b81e5Smrg	  then posix_mkdir=:
3730f5b81e5Smrg	  fi
3740f5b81e5Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3750f5b81e5Smrg	else
3760f5b81e5Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
3770f5b81e5Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3780f5b81e5Smrg	fi
3790f5b81e5Smrg	trap '' 0;;
380a0195d5fSmrg    esac
381a0195d5fSmrg
382a0195d5fSmrg    if
383a0195d5fSmrg      $posix_mkdir && (
384feee4651Smrg        umask $mkdir_umask &&
385feee4651Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386a0195d5fSmrg      )
387a0195d5fSmrg    then :
388a0195d5fSmrg    else
389a0195d5fSmrg
3900f5b81e5Smrg      # mkdir does not conform to POSIX,
391a0195d5fSmrg      # or it failed possibly due to a race condition.  Create the
392a0195d5fSmrg      # directory the slow way, step by step, checking for races as we go.
393a0195d5fSmrg
394a0195d5fSmrg      case $dstdir in
395feee4651Smrg        /*) prefix='/';;
396feee4651Smrg        [-=\(\)!]*) prefix='./';;
397feee4651Smrg        *)  prefix='';;
398a0195d5fSmrg      esac
399a0195d5fSmrg
400a0195d5fSmrg      oIFS=$IFS
401a0195d5fSmrg      IFS=/
402feee4651Smrg      set -f
403a0195d5fSmrg      set fnord $dstdir
404a0195d5fSmrg      shift
405feee4651Smrg      set +f
406a0195d5fSmrg      IFS=$oIFS
407a0195d5fSmrg
408a0195d5fSmrg      prefixes=
409a0195d5fSmrg
410a0195d5fSmrg      for d
411a0195d5fSmrg      do
412feee4651Smrg        test X"$d" = X && continue
413feee4651Smrg
414feee4651Smrg        prefix=$prefix$d
415feee4651Smrg        if test -d "$prefix"; then
416feee4651Smrg          prefixes=
417feee4651Smrg        else
418feee4651Smrg          if $posix_mkdir; then
4190f5b81e5Smrg            (umask $mkdir_umask &&
420feee4651Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421feee4651Smrg            # Don't fail if two instances are running concurrently.
422feee4651Smrg            test -d "$prefix" || exit 1
423feee4651Smrg          else
424feee4651Smrg            case $prefix in
425feee4651Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426feee4651Smrg              *) qprefix=$prefix;;
427feee4651Smrg            esac
428feee4651Smrg            prefixes="$prefixes '$qprefix'"
429feee4651Smrg          fi
430feee4651Smrg        fi
431feee4651Smrg        prefix=$prefix/
432a0195d5fSmrg      done
433a0195d5fSmrg
434a0195d5fSmrg      if test -n "$prefixes"; then
435feee4651Smrg        # Don't fail if two instances are running concurrently.
436feee4651Smrg        (umask $mkdir_umask &&
437feee4651Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438feee4651Smrg          test -d "$dstdir" || exit 1
439feee4651Smrg        obsolete_mkdir_used=true
440a0195d5fSmrg      fi
441a0195d5fSmrg    fi
442a0195d5fSmrg  fi
443a0195d5fSmrg
444a0195d5fSmrg  if test -n "$dir_arg"; then
445a0195d5fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446a0195d5fSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447a0195d5fSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448a0195d5fSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449a0195d5fSmrg  else
450a0195d5fSmrg
451a0195d5fSmrg    # Make a couple of temp file names in the proper directory.
4520f5b81e5Smrg    dsttmp=${dstdirslash}_inst.$$_
4530f5b81e5Smrg    rmtmp=${dstdirslash}_rm.$$_
454a0195d5fSmrg
455a0195d5fSmrg    # Trap to clean up those temp files at exit.
456a0195d5fSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457a0195d5fSmrg
458a0195d5fSmrg    # Copy the file name to the temp name.
4590f5b81e5Smrg    (umask $cp_umask &&
4600f5b81e5Smrg     { test -z "$stripcmd" || {
4610f5b81e5Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4620f5b81e5Smrg	 # which would cause strip to fail.
4630f5b81e5Smrg	 if test -z "$doit"; then
4640f5b81e5Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4650f5b81e5Smrg	 else
4660f5b81e5Smrg	   $doit touch "$dsttmp"
4670f5b81e5Smrg	 fi
4680f5b81e5Smrg       }
4690f5b81e5Smrg     } &&
4700f5b81e5Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471a0195d5fSmrg
472a0195d5fSmrg    # and set any options; do chmod last to preserve setuid bits.
473a0195d5fSmrg    #
474a0195d5fSmrg    # If any of these fail, we abort the whole thing.  If we want to
475a0195d5fSmrg    # ignore errors from any of these, just make sure not to ignore
476a0195d5fSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477a0195d5fSmrg    #
478a0195d5fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479a0195d5fSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480a0195d5fSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481a0195d5fSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482a0195d5fSmrg
483a0195d5fSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484a0195d5fSmrg    if $copy_on_change &&
485feee4651Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486feee4651Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487feee4651Smrg       set -f &&
488a0195d5fSmrg       set X $old && old=:$2:$4:$5:$6 &&
489a0195d5fSmrg       set X $new && new=:$2:$4:$5:$6 &&
490feee4651Smrg       set +f &&
491a0195d5fSmrg       test "$old" = "$new" &&
492a0195d5fSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493a0195d5fSmrg    then
494a0195d5fSmrg      rm -f "$dsttmp"
495a0195d5fSmrg    else
4960f5b81e5Smrg      # If $backupsuffix is set, and the file being installed
4970f5b81e5Smrg      # already exists, attempt a backup.  Don't worry if it fails,
4980f5b81e5Smrg      # e.g., if mv doesn't support -f.
4990f5b81e5Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
5000f5b81e5Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5010f5b81e5Smrg      fi
5020f5b81e5Smrg
503a0195d5fSmrg      # Rename the file to the real destination.
504a0195d5fSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505a0195d5fSmrg
506a0195d5fSmrg      # The rename failed, perhaps because mv can't rename something else
507a0195d5fSmrg      # to itself, or perhaps because mv is so ancient that it does not
508a0195d5fSmrg      # support -f.
509a0195d5fSmrg      {
510feee4651Smrg        # Now remove or move aside any old file at destination location.
511feee4651Smrg        # We try this two ways since rm can't unlink itself on some
512feee4651Smrg        # systems and the destination file might be busy for other
513feee4651Smrg        # reasons.  In this case, the final cleanup might fail but the new
514feee4651Smrg        # file should still install successfully.
515feee4651Smrg        {
516feee4651Smrg          test ! -f "$dst" ||
5170f5b81e5Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518feee4651Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5190f5b81e5Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520feee4651Smrg          } ||
521feee4651Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522feee4651Smrg            (exit 1); exit 1
523feee4651Smrg          }
524feee4651Smrg        } &&
525feee4651Smrg
526feee4651Smrg        # Now rename the file to the real destination.
527feee4651Smrg        $doit $mvcmd "$dsttmp" "$dst"
528a0195d5fSmrg      }
529a0195d5fSmrg    fi || exit 1
530a0195d5fSmrg
531a0195d5fSmrg    trap '' 0
532a0195d5fSmrg  fi
533a0195d5fSmrgdone
534a0195d5fSmrg
535a0195d5fSmrg# Local variables:
5360f5b81e5Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537a0195d5fSmrg# time-stamp-start: "scriptversion="
538a0195d5fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5390f5b81e5Smrg# time-stamp-time-zone: "UTC0"
5406600fe5bSmrg# time-stamp-end: "; # UTC"
541a0195d5fSmrg# End:
542