1c7484f1fSmrg#!/bin/sh
2c7484f1fSmrg# install - install a program, script, or datafile
3c7484f1fSmrg
463323ee5Smrgscriptversion=2020-11-14.01; # UTC
5c7484f1fSmrg
6c7484f1fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7c7484f1fSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8c7484f1fSmrg# following copyright and license.
9c7484f1fSmrg#
10c7484f1fSmrg# Copyright (C) 1994 X Consortium
11c7484f1fSmrg#
12c7484f1fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13c7484f1fSmrg# of this software and associated documentation files (the "Software"), to
14c7484f1fSmrg# deal in the Software without restriction, including without limitation the
15c7484f1fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16c7484f1fSmrg# sell copies of the Software, and to permit persons to whom the Software is
17c7484f1fSmrg# furnished to do so, subject to the following conditions:
18c7484f1fSmrg#
19c7484f1fSmrg# The above copyright notice and this permission notice shall be included in
20c7484f1fSmrg# all copies or substantial portions of the Software.
21c7484f1fSmrg#
22c7484f1fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23c7484f1fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24c7484f1fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25c7484f1fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26c7484f1fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27c7484f1fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28c7484f1fSmrg#
29c7484f1fSmrg# Except as contained in this notice, the name of the X Consortium shall not
30c7484f1fSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31c7484f1fSmrg# ings in this Software without prior written authorization from the X Consor-
32c7484f1fSmrg# tium.
33c7484f1fSmrg#
34c7484f1fSmrg#
35c7484f1fSmrg# FSF changes to this file are in the public domain.
36c7484f1fSmrg#
37c7484f1fSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38e6ecbdaeSmrg# 'make' implicit rules from creating a file called install from it
39c7484f1fSmrg# when there is no Makefile.
40c7484f1fSmrg#
41c7484f1fSmrg# This script is compatible with the BSD install script, but was written
42c7484f1fSmrg# from scratch.
43c7484f1fSmrg
44ebb1c56dSmrgtab='	'
45c7484f1fSmrgnl='
46c7484f1fSmrg'
47ebb1c56dSmrgIFS=" $tab$nl"
48c7484f1fSmrg
49ebb1c56dSmrg# Set DOITPROG to "echo" to test this script.
50c7484f1fSmrg
51baed689fSmrgdoit=${DOITPROG-}
52ebb1c56dSmrgdoit_exec=${doit:-exec}
53c7484f1fSmrg
54c7484f1fSmrg# Put in absolute file names if you don't have them in your path;
55c7484f1fSmrg# or use environment vars.
56c7484f1fSmrg
57baed689fSmrgchgrpprog=${CHGRPPROG-chgrp}
58baed689fSmrgchmodprog=${CHMODPROG-chmod}
59baed689fSmrgchownprog=${CHOWNPROG-chown}
60baed689fSmrgcmpprog=${CMPPROG-cmp}
61baed689fSmrgcpprog=${CPPROG-cp}
62baed689fSmrgmkdirprog=${MKDIRPROG-mkdir}
63baed689fSmrgmvprog=${MVPROG-mv}
64baed689fSmrgrmprog=${RMPROG-rm}
65baed689fSmrgstripprog=${STRIPPROG-strip}
66baed689fSmrg
67c7484f1fSmrgposix_mkdir=
68c7484f1fSmrg
69c7484f1fSmrg# Desired mode of installed file.
70c7484f1fSmrgmode=0755
71c7484f1fSmrg
7263323ee5Smrg# Create dirs (including intermediate dirs) using mode 755.
7363323ee5Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7463323ee5Smrgmkdir_umask=22
7563323ee5Smrg
7663323ee5Smrgbackupsuffix=
77baed689fSmrgchgrpcmd=
78c7484f1fSmrgchmodcmd=$chmodprog
79c7484f1fSmrgchowncmd=
80baed689fSmrgmvcmd=$mvprog
81c7484f1fSmrgrmcmd="$rmprog -f"
82baed689fSmrgstripcmd=
83baed689fSmrg
84c7484f1fSmrgsrc=
85c7484f1fSmrgdst=
86c7484f1fSmrgdir_arg=
87baed689fSmrgdst_arg=
88baed689fSmrg
89baed689fSmrgcopy_on_change=false
90ebb1c56dSmrgis_target_a_directory=possibly
91c7484f1fSmrg
92baed689fSmrgusage="\
93baed689fSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94c7484f1fSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95c7484f1fSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96c7484f1fSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97c7484f1fSmrg
98c7484f1fSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99c7484f1fSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100c7484f1fSmrgIn the 4th, create DIRECTORIES.
101c7484f1fSmrg
102c7484f1fSmrgOptions:
103baed689fSmrg     --help     display this help and exit.
104baed689fSmrg     --version  display version info and exit.
105baed689fSmrg
106baed689fSmrg  -c            (ignored)
10763323ee5Smrg  -C            install only if different (preserve data modification time)
108baed689fSmrg  -d            create directories instead of installing files.
109baed689fSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110baed689fSmrg  -m MODE       $chmodprog installed files to MODE.
111baed689fSmrg  -o USER       $chownprog installed files to USER.
11263323ee5Smrg  -p            pass -p to $cpprog.
113baed689fSmrg  -s            $stripprog installed files.
11463323ee5Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115baed689fSmrg  -t DIRECTORY  install into DIRECTORY.
116baed689fSmrg  -T            report an error if DSTFILE is a directory.
117c7484f1fSmrg
118c7484f1fSmrgEnvironment variables override the default commands:
119baed689fSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120baed689fSmrg  RMPROG STRIPPROG
12163323ee5Smrg
12263323ee5SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12363323ee5Smrgit's up to you to specify -f if you want it.
12463323ee5Smrg
12563323ee5SmrgIf -S is not specified, no backups are attempted.
12663323ee5Smrg
12763323ee5SmrgEmail bug reports to bug-automake@gnu.org.
12863323ee5SmrgAutomake home page: https://www.gnu.org/software/automake/
129c7484f1fSmrg"
130c7484f1fSmrg
131c7484f1fSmrgwhile test $# -ne 0; do
132c7484f1fSmrg  case $1 in
133baed689fSmrg    -c) ;;
134baed689fSmrg
135baed689fSmrg    -C) copy_on_change=true;;
136c7484f1fSmrg
137baed689fSmrg    -d) dir_arg=true;;
138c7484f1fSmrg
139c7484f1fSmrg    -g) chgrpcmd="$chgrpprog $2"
140ebb1c56dSmrg        shift;;
141c7484f1fSmrg
142c7484f1fSmrg    --help) echo "$usage"; exit $?;;
143c7484f1fSmrg
144c7484f1fSmrg    -m) mode=$2
145ebb1c56dSmrg        case $mode in
146ebb1c56dSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147ebb1c56dSmrg            echo "$0: invalid mode: $mode" >&2
148ebb1c56dSmrg            exit 1;;
149ebb1c56dSmrg        esac
150ebb1c56dSmrg        shift;;
151c7484f1fSmrg
152c7484f1fSmrg    -o) chowncmd="$chownprog $2"
153ebb1c56dSmrg        shift;;
154c7484f1fSmrg
15563323ee5Smrg    -p) cpprog="$cpprog -p";;
15663323ee5Smrg
157baed689fSmrg    -s) stripcmd=$stripprog;;
158c7484f1fSmrg
15963323ee5Smrg    -S) backupsuffix="$2"
16063323ee5Smrg        shift;;
16163323ee5Smrg
162ebb1c56dSmrg    -t)
163ebb1c56dSmrg        is_target_a_directory=always
164ebb1c56dSmrg        dst_arg=$2
165ebb1c56dSmrg        # Protect names problematic for 'test' and other utilities.
166ebb1c56dSmrg        case $dst_arg in
167ebb1c56dSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168ebb1c56dSmrg        esac
169ebb1c56dSmrg        shift;;
170c7484f1fSmrg
171ebb1c56dSmrg    -T) is_target_a_directory=never;;
172c7484f1fSmrg
173c7484f1fSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174c7484f1fSmrg
175ebb1c56dSmrg    --) shift
176ebb1c56dSmrg        break;;
177c7484f1fSmrg
178ebb1c56dSmrg    -*) echo "$0: invalid option: $1" >&2
179ebb1c56dSmrg        exit 1;;
180c7484f1fSmrg
181c7484f1fSmrg    *)  break;;
182c7484f1fSmrg  esac
183baed689fSmrg  shift
184c7484f1fSmrgdone
185c7484f1fSmrg
186ebb1c56dSmrg# We allow the use of options -d and -T together, by making -d
187ebb1c56dSmrg# take the precedence; this is for compatibility with GNU install.
188ebb1c56dSmrg
189ebb1c56dSmrgif test -n "$dir_arg"; then
190ebb1c56dSmrg  if test -n "$dst_arg"; then
191ebb1c56dSmrg    echo "$0: target directory not allowed when installing a directory." >&2
192ebb1c56dSmrg    exit 1
193ebb1c56dSmrg  fi
194ebb1c56dSmrgfi
195ebb1c56dSmrg
196baed689fSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197c7484f1fSmrg  # When -d is used, all remaining arguments are directories to create.
198c7484f1fSmrg  # When -t is used, the destination is already specified.
199c7484f1fSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200c7484f1fSmrg  for arg
201c7484f1fSmrg  do
202baed689fSmrg    if test -n "$dst_arg"; then
203c7484f1fSmrg      # $@ is not empty: it contains at least $arg.
204baed689fSmrg      set fnord "$@" "$dst_arg"
205c7484f1fSmrg      shift # fnord
206c7484f1fSmrg    fi
207c7484f1fSmrg    shift # arg
208baed689fSmrg    dst_arg=$arg
209e6ecbdaeSmrg    # Protect names problematic for 'test' and other utilities.
210e6ecbdaeSmrg    case $dst_arg in
211e6ecbdaeSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212e6ecbdaeSmrg    esac
213c7484f1fSmrg  done
214c7484f1fSmrgfi
215c7484f1fSmrg
216c7484f1fSmrgif test $# -eq 0; then
217c7484f1fSmrg  if test -z "$dir_arg"; then
218c7484f1fSmrg    echo "$0: no input file specified." >&2
219c7484f1fSmrg    exit 1
220c7484f1fSmrg  fi
221e6ecbdaeSmrg  # It's OK to call 'install-sh -d' without argument.
222c7484f1fSmrg  # This can happen when creating conditional directories.
223c7484f1fSmrg  exit 0
224c7484f1fSmrgfi
225c7484f1fSmrg
226ebb1c56dSmrgif test -z "$dir_arg"; then
227ebb1c56dSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228ebb1c56dSmrg    if test ! -d "$dst_arg"; then
229ebb1c56dSmrg      echo "$0: $dst_arg: Is not a directory." >&2
230ebb1c56dSmrg      exit 1
231ebb1c56dSmrg    fi
232ebb1c56dSmrg  fi
233ebb1c56dSmrgfi
234ebb1c56dSmrg
235c7484f1fSmrgif test -z "$dir_arg"; then
236e6ecbdaeSmrg  do_exit='(exit $ret); exit $ret'
237e6ecbdaeSmrg  trap "ret=129; $do_exit" 1
238e6ecbdaeSmrg  trap "ret=130; $do_exit" 2
239e6ecbdaeSmrg  trap "ret=141; $do_exit" 13
240e6ecbdaeSmrg  trap "ret=143; $do_exit" 15
241c7484f1fSmrg
242c7484f1fSmrg  # Set umask so as not to create temps with too-generous modes.
243c7484f1fSmrg  # However, 'strip' requires both read and write access to temps.
244c7484f1fSmrg  case $mode in
245c7484f1fSmrg    # Optimize common cases.
246c7484f1fSmrg    *644) cp_umask=133;;
247c7484f1fSmrg    *755) cp_umask=22;;
248c7484f1fSmrg
249c7484f1fSmrg    *[0-7])
250c7484f1fSmrg      if test -z "$stripcmd"; then
251ebb1c56dSmrg        u_plus_rw=
252c7484f1fSmrg      else
253ebb1c56dSmrg        u_plus_rw='% 200'
254c7484f1fSmrg      fi
255c7484f1fSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256c7484f1fSmrg    *)
257c7484f1fSmrg      if test -z "$stripcmd"; then
258ebb1c56dSmrg        u_plus_rw=
259c7484f1fSmrg      else
260ebb1c56dSmrg        u_plus_rw=,u+rw
261c7484f1fSmrg      fi
262c7484f1fSmrg      cp_umask=$mode$u_plus_rw;;
263c7484f1fSmrg  esac
264c7484f1fSmrgfi
265c7484f1fSmrg
266c7484f1fSmrgfor src
267c7484f1fSmrgdo
268e6ecbdaeSmrg  # Protect names problematic for 'test' and other utilities.
269c7484f1fSmrg  case $src in
270e6ecbdaeSmrg    -* | [=\(\)!]) src=./$src;;
271c7484f1fSmrg  esac
272c7484f1fSmrg
273c7484f1fSmrg  if test -n "$dir_arg"; then
274c7484f1fSmrg    dst=$src
275c7484f1fSmrg    dstdir=$dst
276c7484f1fSmrg    test -d "$dstdir"
277c7484f1fSmrg    dstdir_status=$?
27863323ee5Smrg    # Don't chown directories that already exist.
27963323ee5Smrg    if test $dstdir_status = 0; then
28063323ee5Smrg      chowncmd=""
28163323ee5Smrg    fi
282c7484f1fSmrg  else
283c7484f1fSmrg
284c7484f1fSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285c7484f1fSmrg    # might cause directories to be created, which would be especially bad
286c7484f1fSmrg    # if $src (and thus $dsttmp) contains '*'.
287c7484f1fSmrg    if test ! -f "$src" && test ! -d "$src"; then
288c7484f1fSmrg      echo "$0: $src does not exist." >&2
289c7484f1fSmrg      exit 1
290c7484f1fSmrg    fi
291c7484f1fSmrg
292baed689fSmrg    if test -z "$dst_arg"; then
293c7484f1fSmrg      echo "$0: no destination specified." >&2
294c7484f1fSmrg      exit 1
295c7484f1fSmrg    fi
296baed689fSmrg    dst=$dst_arg
297c7484f1fSmrg
29863323ee5Smrg    # If destination is a directory, append the input filename.
299c7484f1fSmrg    if test -d "$dst"; then
300ebb1c56dSmrg      if test "$is_target_a_directory" = never; then
301ebb1c56dSmrg        echo "$0: $dst_arg: Is a directory" >&2
302ebb1c56dSmrg        exit 1
303c7484f1fSmrg      fi
304c7484f1fSmrg      dstdir=$dst
30563323ee5Smrg      dstbase=`basename "$src"`
30663323ee5Smrg      case $dst in
30763323ee5Smrg	*/) dst=$dst$dstbase;;
30863323ee5Smrg	*)  dst=$dst/$dstbase;;
30963323ee5Smrg      esac
310c7484f1fSmrg      dstdir_status=0
311c7484f1fSmrg    else
312ebb1c56dSmrg      dstdir=`dirname "$dst"`
313c7484f1fSmrg      test -d "$dstdir"
314c7484f1fSmrg      dstdir_status=$?
315c7484f1fSmrg    fi
316c7484f1fSmrg  fi
317c7484f1fSmrg
31863323ee5Smrg  case $dstdir in
31963323ee5Smrg    */) dstdirslash=$dstdir;;
32063323ee5Smrg    *)  dstdirslash=$dstdir/;;
32163323ee5Smrg  esac
32263323ee5Smrg
323c7484f1fSmrg  obsolete_mkdir_used=false
324c7484f1fSmrg
325c7484f1fSmrg  if test $dstdir_status != 0; then
326c7484f1fSmrg    case $posix_mkdir in
327c7484f1fSmrg      '')
328ebb1c56dSmrg        # With -d, create the new directory with the user-specified mode.
329ebb1c56dSmrg        # Otherwise, rely on $mkdir_umask.
330ebb1c56dSmrg        if test -n "$dir_arg"; then
331ebb1c56dSmrg          mkdir_mode=-m$mode
332ebb1c56dSmrg        else
333ebb1c56dSmrg          mkdir_mode=
334ebb1c56dSmrg        fi
335ebb1c56dSmrg
336ebb1c56dSmrg        posix_mkdir=false
33763323ee5Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33863323ee5Smrg	# here however when possible just to lower collision chance.
33963323ee5Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34063323ee5Smrg
34163323ee5Smrg	trap '
34263323ee5Smrg	  ret=$?
34363323ee5Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34463323ee5Smrg	  exit $ret
34563323ee5Smrg	' 0
34663323ee5Smrg
34763323ee5Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
34863323ee5Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34963323ee5Smrg	# directory is successfully created first before we actually test
35063323ee5Smrg	# 'mkdir -p'.
35163323ee5Smrg	if (umask $mkdir_umask &&
35263323ee5Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35363323ee5Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35463323ee5Smrg	then
35563323ee5Smrg	  if test -z "$dir_arg" || {
35663323ee5Smrg	       # Check for POSIX incompatibilities with -m.
35763323ee5Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35863323ee5Smrg	       # other-writable bit of parent directory when it shouldn't.
35963323ee5Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36063323ee5Smrg	       test_tmpdir="$tmpdir/a"
36163323ee5Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36263323ee5Smrg	       case $ls_ld_tmpdir in
36363323ee5Smrg		 d????-?r-*) different_mode=700;;
36463323ee5Smrg		 d????-?--*) different_mode=755;;
36563323ee5Smrg		 *) false;;
36663323ee5Smrg	       esac &&
36763323ee5Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36863323ee5Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36963323ee5Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37063323ee5Smrg	       }
37163323ee5Smrg	     }
37263323ee5Smrg	  then posix_mkdir=:
37363323ee5Smrg	  fi
37463323ee5Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37563323ee5Smrg	else
37663323ee5Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
37763323ee5Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37863323ee5Smrg	fi
37963323ee5Smrg	trap '' 0;;
380c7484f1fSmrg    esac
381c7484f1fSmrg
382c7484f1fSmrg    if
383c7484f1fSmrg      $posix_mkdir && (
384ebb1c56dSmrg        umask $mkdir_umask &&
385ebb1c56dSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386c7484f1fSmrg      )
387c7484f1fSmrg    then :
388c7484f1fSmrg    else
389c7484f1fSmrg
39063323ee5Smrg      # mkdir does not conform to POSIX,
391c7484f1fSmrg      # or it failed possibly due to a race condition.  Create the
392c7484f1fSmrg      # directory the slow way, step by step, checking for races as we go.
393c7484f1fSmrg
394c7484f1fSmrg      case $dstdir in
395ebb1c56dSmrg        /*) prefix='/';;
396ebb1c56dSmrg        [-=\(\)!]*) prefix='./';;
397ebb1c56dSmrg        *)  prefix='';;
398c7484f1fSmrg      esac
399c7484f1fSmrg
400c7484f1fSmrg      oIFS=$IFS
401c7484f1fSmrg      IFS=/
402ebb1c56dSmrg      set -f
403c7484f1fSmrg      set fnord $dstdir
404c7484f1fSmrg      shift
405ebb1c56dSmrg      set +f
406c7484f1fSmrg      IFS=$oIFS
407c7484f1fSmrg
408c7484f1fSmrg      prefixes=
409c7484f1fSmrg
410c7484f1fSmrg      for d
411c7484f1fSmrg      do
412ebb1c56dSmrg        test X"$d" = X && continue
413ebb1c56dSmrg
414ebb1c56dSmrg        prefix=$prefix$d
415ebb1c56dSmrg        if test -d "$prefix"; then
416ebb1c56dSmrg          prefixes=
417ebb1c56dSmrg        else
418ebb1c56dSmrg          if $posix_mkdir; then
41963323ee5Smrg            (umask $mkdir_umask &&
420ebb1c56dSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421ebb1c56dSmrg            # Don't fail if two instances are running concurrently.
422ebb1c56dSmrg            test -d "$prefix" || exit 1
423ebb1c56dSmrg          else
424ebb1c56dSmrg            case $prefix in
425ebb1c56dSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426ebb1c56dSmrg              *) qprefix=$prefix;;
427ebb1c56dSmrg            esac
428ebb1c56dSmrg            prefixes="$prefixes '$qprefix'"
429ebb1c56dSmrg          fi
430ebb1c56dSmrg        fi
431ebb1c56dSmrg        prefix=$prefix/
432c7484f1fSmrg      done
433c7484f1fSmrg
434c7484f1fSmrg      if test -n "$prefixes"; then
435ebb1c56dSmrg        # Don't fail if two instances are running concurrently.
436ebb1c56dSmrg        (umask $mkdir_umask &&
437ebb1c56dSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438ebb1c56dSmrg          test -d "$dstdir" || exit 1
439ebb1c56dSmrg        obsolete_mkdir_used=true
440c7484f1fSmrg      fi
441c7484f1fSmrg    fi
442c7484f1fSmrg  fi
443c7484f1fSmrg
444c7484f1fSmrg  if test -n "$dir_arg"; then
445c7484f1fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446c7484f1fSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447c7484f1fSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448c7484f1fSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449c7484f1fSmrg  else
450c7484f1fSmrg
451c7484f1fSmrg    # Make a couple of temp file names in the proper directory.
45263323ee5Smrg    dsttmp=${dstdirslash}_inst.$$_
45363323ee5Smrg    rmtmp=${dstdirslash}_rm.$$_
454c7484f1fSmrg
455c7484f1fSmrg    # Trap to clean up those temp files at exit.
456c7484f1fSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457c7484f1fSmrg
458c7484f1fSmrg    # Copy the file name to the temp name.
45963323ee5Smrg    (umask $cp_umask &&
46063323ee5Smrg     { test -z "$stripcmd" || {
46163323ee5Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46263323ee5Smrg	 # which would cause strip to fail.
46363323ee5Smrg	 if test -z "$doit"; then
46463323ee5Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46563323ee5Smrg	 else
46663323ee5Smrg	   $doit touch "$dsttmp"
46763323ee5Smrg	 fi
46863323ee5Smrg       }
46963323ee5Smrg     } &&
47063323ee5Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471c7484f1fSmrg
472c7484f1fSmrg    # and set any options; do chmod last to preserve setuid bits.
473c7484f1fSmrg    #
474c7484f1fSmrg    # If any of these fail, we abort the whole thing.  If we want to
475c7484f1fSmrg    # ignore errors from any of these, just make sure not to ignore
476c7484f1fSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477c7484f1fSmrg    #
478baed689fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479baed689fSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480baed689fSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481baed689fSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482baed689fSmrg
483baed689fSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484baed689fSmrg    if $copy_on_change &&
485ebb1c56dSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486ebb1c56dSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487ebb1c56dSmrg       set -f &&
488baed689fSmrg       set X $old && old=:$2:$4:$5:$6 &&
489baed689fSmrg       set X $new && new=:$2:$4:$5:$6 &&
490ebb1c56dSmrg       set +f &&
491baed689fSmrg       test "$old" = "$new" &&
492baed689fSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493baed689fSmrg    then
494baed689fSmrg      rm -f "$dsttmp"
495baed689fSmrg    else
49663323ee5Smrg      # If $backupsuffix is set, and the file being installed
49763323ee5Smrg      # already exists, attempt a backup.  Don't worry if it fails,
49863323ee5Smrg      # e.g., if mv doesn't support -f.
49963323ee5Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
50063323ee5Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50163323ee5Smrg      fi
50263323ee5Smrg
503baed689fSmrg      # Rename the file to the real destination.
504baed689fSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505baed689fSmrg
506baed689fSmrg      # The rename failed, perhaps because mv can't rename something else
507baed689fSmrg      # to itself, or perhaps because mv is so ancient that it does not
508baed689fSmrg      # support -f.
509baed689fSmrg      {
510ebb1c56dSmrg        # Now remove or move aside any old file at destination location.
511ebb1c56dSmrg        # We try this two ways since rm can't unlink itself on some
512ebb1c56dSmrg        # systems and the destination file might be busy for other
513ebb1c56dSmrg        # reasons.  In this case, the final cleanup might fail but the new
514ebb1c56dSmrg        # file should still install successfully.
515ebb1c56dSmrg        {
516ebb1c56dSmrg          test ! -f "$dst" ||
51763323ee5Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518ebb1c56dSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51963323ee5Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520ebb1c56dSmrg          } ||
521ebb1c56dSmrg          { echo "$0: cannot unlink or rename $dst" >&2
522ebb1c56dSmrg            (exit 1); exit 1
523ebb1c56dSmrg          }
524ebb1c56dSmrg        } &&
525ebb1c56dSmrg
526ebb1c56dSmrg        # Now rename the file to the real destination.
527ebb1c56dSmrg        $doit $mvcmd "$dsttmp" "$dst"
528baed689fSmrg      }
529baed689fSmrg    fi || exit 1
530c7484f1fSmrg
531c7484f1fSmrg    trap '' 0
532c7484f1fSmrg  fi
533c7484f1fSmrgdone
534c7484f1fSmrg
535c7484f1fSmrg# Local variables:
53663323ee5Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537c7484f1fSmrg# time-stamp-start: "scriptversion="
538c7484f1fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53963323ee5Smrg# time-stamp-time-zone: "UTC0"
540baed689fSmrg# time-stamp-end: "; # UTC"
541c7484f1fSmrg# End:
542