1d514b0f3Smrg#!/bin/sh
2d514b0f3Smrg# install - install a program, script, or datafile
3d514b0f3Smrg
4d514b0f3Smrgscriptversion=2020-11-14.01; # UTC
5d514b0f3Smrg
6d514b0f3Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7d514b0f3Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8d514b0f3Smrg# following copyright and license.
9d514b0f3Smrg#
10d514b0f3Smrg# Copyright (C) 1994 X Consortium
11d514b0f3Smrg#
12d514b0f3Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13d514b0f3Smrg# of this software and associated documentation files (the "Software"), to
14d514b0f3Smrg# deal in the Software without restriction, including without limitation the
15d514b0f3Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16d514b0f3Smrg# sell copies of the Software, and to permit persons to whom the Software is
17d514b0f3Smrg# furnished to do so, subject to the following conditions:
18d514b0f3Smrg#
19d514b0f3Smrg# The above copyright notice and this permission notice shall be included in
20d514b0f3Smrg# all copies or substantial portions of the Software.
21d514b0f3Smrg#
22d514b0f3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23d514b0f3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24d514b0f3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25d514b0f3Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26d514b0f3Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27d514b0f3Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28d514b0f3Smrg#
29d514b0f3Smrg# Except as contained in this notice, the name of the X Consortium shall not
30d514b0f3Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31d514b0f3Smrg# ings in this Software without prior written authorization from the X Consor-
32d514b0f3Smrg# tium.
33d514b0f3Smrg#
34d514b0f3Smrg#
35d514b0f3Smrg# FSF changes to this file are in the public domain.
36d514b0f3Smrg#
37d514b0f3Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38d514b0f3Smrg# 'make' implicit rules from creating a file called install from it
39d514b0f3Smrg# when there is no Makefile.
40d514b0f3Smrg#
41d514b0f3Smrg# This script is compatible with the BSD install script, but was written
42d514b0f3Smrg# from scratch.
43d514b0f3Smrg
44d514b0f3Smrgtab='	'
45d514b0f3Smrgnl='
46d514b0f3Smrg'
47d514b0f3SmrgIFS=" $tab$nl"
48d514b0f3Smrg
49d514b0f3Smrg# Set DOITPROG to "echo" to test this script.
50d514b0f3Smrg
51d514b0f3Smrgdoit=${DOITPROG-}
52d514b0f3Smrgdoit_exec=${doit:-exec}
53d514b0f3Smrg
54d514b0f3Smrg# Put in absolute file names if you don't have them in your path;
55d514b0f3Smrg# or use environment vars.
56d514b0f3Smrg
57d514b0f3Smrgchgrpprog=${CHGRPPROG-chgrp}
58d514b0f3Smrgchmodprog=${CHMODPROG-chmod}
59d514b0f3Smrgchownprog=${CHOWNPROG-chown}
60d514b0f3Smrgcmpprog=${CMPPROG-cmp}
61d514b0f3Smrgcpprog=${CPPROG-cp}
62d514b0f3Smrgmkdirprog=${MKDIRPROG-mkdir}
63d514b0f3Smrgmvprog=${MVPROG-mv}
64d514b0f3Smrgrmprog=${RMPROG-rm}
65d514b0f3Smrgstripprog=${STRIPPROG-strip}
66d514b0f3Smrg
67d514b0f3Smrgposix_mkdir=
68d514b0f3Smrg
69d514b0f3Smrg# Desired mode of installed file.
70d514b0f3Smrgmode=0755
71d514b0f3Smrg
72d514b0f3Smrg# Create dirs (including intermediate dirs) using mode 755.
73d514b0f3Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74d514b0f3Smrgmkdir_umask=22
75d514b0f3Smrg
76d514b0f3Smrgbackupsuffix=
77d514b0f3Smrgchgrpcmd=
78d514b0f3Smrgchmodcmd=$chmodprog
79d514b0f3Smrgchowncmd=
80d514b0f3Smrgmvcmd=$mvprog
81d514b0f3Smrgrmcmd="$rmprog -f"
82d514b0f3Smrgstripcmd=
83d514b0f3Smrg
84d514b0f3Smrgsrc=
85d514b0f3Smrgdst=
86d514b0f3Smrgdir_arg=
87d514b0f3Smrgdst_arg=
88d514b0f3Smrg
89d514b0f3Smrgcopy_on_change=false
90d514b0f3Smrgis_target_a_directory=possibly
91d514b0f3Smrg
92d514b0f3Smrgusage="\
93d514b0f3SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94d514b0f3Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95d514b0f3Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96d514b0f3Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97d514b0f3Smrg
98d514b0f3SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99d514b0f3SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100d514b0f3SmrgIn the 4th, create DIRECTORIES.
101d514b0f3Smrg
102d514b0f3SmrgOptions:
103d514b0f3Smrg     --help     display this help and exit.
104d514b0f3Smrg     --version  display version info and exit.
105d514b0f3Smrg
106d514b0f3Smrg  -c            (ignored)
107d514b0f3Smrg  -C            install only if different (preserve data modification time)
108d514b0f3Smrg  -d            create directories instead of installing files.
109d514b0f3Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110d514b0f3Smrg  -m MODE       $chmodprog installed files to MODE.
111d514b0f3Smrg  -o USER       $chownprog installed files to USER.
112d514b0f3Smrg  -p            pass -p to $cpprog.
113d514b0f3Smrg  -s            $stripprog installed files.
114d514b0f3Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115d514b0f3Smrg  -t DIRECTORY  install into DIRECTORY.
116d514b0f3Smrg  -T            report an error if DSTFILE is a directory.
117d514b0f3Smrg
118d514b0f3SmrgEnvironment variables override the default commands:
119d514b0f3Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120d514b0f3Smrg  RMPROG STRIPPROG
121d514b0f3Smrg
122d514b0f3SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123d514b0f3Smrgit's up to you to specify -f if you want it.
124d514b0f3Smrg
125d514b0f3SmrgIf -S is not specified, no backups are attempted.
126d514b0f3Smrg
127d514b0f3SmrgEmail bug reports to bug-automake@gnu.org.
128d514b0f3SmrgAutomake home page: https://www.gnu.org/software/automake/
129d514b0f3Smrg"
130d514b0f3Smrg
131d514b0f3Smrgwhile test $# -ne 0; do
132d514b0f3Smrg  case $1 in
133d514b0f3Smrg    -c) ;;
134d514b0f3Smrg
135d514b0f3Smrg    -C) copy_on_change=true;;
136d514b0f3Smrg
137d514b0f3Smrg    -d) dir_arg=true;;
138d514b0f3Smrg
139d514b0f3Smrg    -g) chgrpcmd="$chgrpprog $2"
140d514b0f3Smrg        shift;;
141d514b0f3Smrg
142d514b0f3Smrg    --help) echo "$usage"; exit $?;;
143d514b0f3Smrg
144d514b0f3Smrg    -m) mode=$2
145d514b0f3Smrg        case $mode in
146d514b0f3Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147d514b0f3Smrg            echo "$0: invalid mode: $mode" >&2
148d514b0f3Smrg            exit 1;;
149d514b0f3Smrg        esac
150d514b0f3Smrg        shift;;
151d514b0f3Smrg
152d514b0f3Smrg    -o) chowncmd="$chownprog $2"
153d514b0f3Smrg        shift;;
154d514b0f3Smrg
155d514b0f3Smrg    -p) cpprog="$cpprog -p";;
156d514b0f3Smrg
157d514b0f3Smrg    -s) stripcmd=$stripprog;;
158d514b0f3Smrg
159d514b0f3Smrg    -S) backupsuffix="$2"
160d514b0f3Smrg        shift;;
161d514b0f3Smrg
162d514b0f3Smrg    -t)
163d514b0f3Smrg        is_target_a_directory=always
164d514b0f3Smrg        dst_arg=$2
165d514b0f3Smrg        # Protect names problematic for 'test' and other utilities.
166d514b0f3Smrg        case $dst_arg in
167d514b0f3Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168d514b0f3Smrg        esac
169d514b0f3Smrg        shift;;
170d514b0f3Smrg
171d514b0f3Smrg    -T) is_target_a_directory=never;;
172d514b0f3Smrg
173d514b0f3Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174d514b0f3Smrg
175d514b0f3Smrg    --) shift
176d514b0f3Smrg        break;;
177d514b0f3Smrg
178d514b0f3Smrg    -*) echo "$0: invalid option: $1" >&2
179d514b0f3Smrg        exit 1;;
180d514b0f3Smrg
181d514b0f3Smrg    *)  break;;
182d514b0f3Smrg  esac
183d514b0f3Smrg  shift
184d514b0f3Smrgdone
185d514b0f3Smrg
186d514b0f3Smrg# We allow the use of options -d and -T together, by making -d
187d514b0f3Smrg# take the precedence; this is for compatibility with GNU install.
188d514b0f3Smrg
189d514b0f3Smrgif test -n "$dir_arg"; then
190d514b0f3Smrg  if test -n "$dst_arg"; then
191d514b0f3Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192d514b0f3Smrg    exit 1
193d514b0f3Smrg  fi
194d514b0f3Smrgfi
195d514b0f3Smrg
196d514b0f3Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197d514b0f3Smrg  # When -d is used, all remaining arguments are directories to create.
198d514b0f3Smrg  # When -t is used, the destination is already specified.
199d514b0f3Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200d514b0f3Smrg  for arg
201d514b0f3Smrg  do
202d514b0f3Smrg    if test -n "$dst_arg"; then
203d514b0f3Smrg      # $@ is not empty: it contains at least $arg.
204d514b0f3Smrg      set fnord "$@" "$dst_arg"
205d514b0f3Smrg      shift # fnord
206d514b0f3Smrg    fi
207d514b0f3Smrg    shift # arg
208d514b0f3Smrg    dst_arg=$arg
209d514b0f3Smrg    # Protect names problematic for 'test' and other utilities.
210d514b0f3Smrg    case $dst_arg in
211d514b0f3Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212d514b0f3Smrg    esac
213d514b0f3Smrg  done
214d514b0f3Smrgfi
215d514b0f3Smrg
216d514b0f3Smrgif test $# -eq 0; then
217d514b0f3Smrg  if test -z "$dir_arg"; then
218d514b0f3Smrg    echo "$0: no input file specified." >&2
219d514b0f3Smrg    exit 1
220d514b0f3Smrg  fi
221d514b0f3Smrg  # It's OK to call 'install-sh -d' without argument.
222d514b0f3Smrg  # This can happen when creating conditional directories.
223d514b0f3Smrg  exit 0
224d514b0f3Smrgfi
225d514b0f3Smrg
226d514b0f3Smrgif test -z "$dir_arg"; then
227d514b0f3Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228d514b0f3Smrg    if test ! -d "$dst_arg"; then
229d514b0f3Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230d514b0f3Smrg      exit 1
231d514b0f3Smrg    fi
232d514b0f3Smrg  fi
233d514b0f3Smrgfi
234d514b0f3Smrg
235d514b0f3Smrgif test -z "$dir_arg"; then
236d514b0f3Smrg  do_exit='(exit $ret); exit $ret'
237d514b0f3Smrg  trap "ret=129; $do_exit" 1
238d514b0f3Smrg  trap "ret=130; $do_exit" 2
239d514b0f3Smrg  trap "ret=141; $do_exit" 13
240d514b0f3Smrg  trap "ret=143; $do_exit" 15
241d514b0f3Smrg
242d514b0f3Smrg  # Set umask so as not to create temps with too-generous modes.
243d514b0f3Smrg  # However, 'strip' requires both read and write access to temps.
244d514b0f3Smrg  case $mode in
245d514b0f3Smrg    # Optimize common cases.
246d514b0f3Smrg    *644) cp_umask=133;;
247d514b0f3Smrg    *755) cp_umask=22;;
248d514b0f3Smrg
249d514b0f3Smrg    *[0-7])
250d514b0f3Smrg      if test -z "$stripcmd"; then
251d514b0f3Smrg        u_plus_rw=
252d514b0f3Smrg      else
253d514b0f3Smrg        u_plus_rw='% 200'
254d514b0f3Smrg      fi
255d514b0f3Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256d514b0f3Smrg    *)
257d514b0f3Smrg      if test -z "$stripcmd"; then
258d514b0f3Smrg        u_plus_rw=
259d514b0f3Smrg      else
260d514b0f3Smrg        u_plus_rw=,u+rw
261d514b0f3Smrg      fi
262d514b0f3Smrg      cp_umask=$mode$u_plus_rw;;
263d514b0f3Smrg  esac
264d514b0f3Smrgfi
265d514b0f3Smrg
266d514b0f3Smrgfor src
267d514b0f3Smrgdo
268d514b0f3Smrg  # Protect names problematic for 'test' and other utilities.
269d514b0f3Smrg  case $src in
270d514b0f3Smrg    -* | [=\(\)!]) src=./$src;;
271d514b0f3Smrg  esac
272d514b0f3Smrg
273d514b0f3Smrg  if test -n "$dir_arg"; then
274d514b0f3Smrg    dst=$src
275d514b0f3Smrg    dstdir=$dst
276d514b0f3Smrg    test -d "$dstdir"
277d514b0f3Smrg    dstdir_status=$?
278d514b0f3Smrg    # Don't chown directories that already exist.
279d514b0f3Smrg    if test $dstdir_status = 0; then
280d514b0f3Smrg      chowncmd=""
281d514b0f3Smrg    fi
282d514b0f3Smrg  else
283d514b0f3Smrg
284d514b0f3Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285d514b0f3Smrg    # might cause directories to be created, which would be especially bad
286d514b0f3Smrg    # if $src (and thus $dsttmp) contains '*'.
287d514b0f3Smrg    if test ! -f "$src" && test ! -d "$src"; then
288d514b0f3Smrg      echo "$0: $src does not exist." >&2
289d514b0f3Smrg      exit 1
290d514b0f3Smrg    fi
291d514b0f3Smrg
292d514b0f3Smrg    if test -z "$dst_arg"; then
293d514b0f3Smrg      echo "$0: no destination specified." >&2
294d514b0f3Smrg      exit 1
295d514b0f3Smrg    fi
296d514b0f3Smrg    dst=$dst_arg
297d514b0f3Smrg
298d514b0f3Smrg    # If destination is a directory, append the input filename.
299d514b0f3Smrg    if test -d "$dst"; then
300d514b0f3Smrg      if test "$is_target_a_directory" = never; then
301d514b0f3Smrg        echo "$0: $dst_arg: Is a directory" >&2
302d514b0f3Smrg        exit 1
303d514b0f3Smrg      fi
304d514b0f3Smrg      dstdir=$dst
305d514b0f3Smrg      dstbase=`basename "$src"`
306d514b0f3Smrg      case $dst in
307d514b0f3Smrg	*/) dst=$dst$dstbase;;
308d514b0f3Smrg	*)  dst=$dst/$dstbase;;
309d514b0f3Smrg      esac
310d514b0f3Smrg      dstdir_status=0
311d514b0f3Smrg    else
312d514b0f3Smrg      dstdir=`dirname "$dst"`
313d514b0f3Smrg      test -d "$dstdir"
314d514b0f3Smrg      dstdir_status=$?
315d514b0f3Smrg    fi
316d514b0f3Smrg  fi
317d514b0f3Smrg
318d514b0f3Smrg  case $dstdir in
319d514b0f3Smrg    */) dstdirslash=$dstdir;;
320d514b0f3Smrg    *)  dstdirslash=$dstdir/;;
321d514b0f3Smrg  esac
322d514b0f3Smrg
323d514b0f3Smrg  obsolete_mkdir_used=false
324d514b0f3Smrg
325d514b0f3Smrg  if test $dstdir_status != 0; then
326d514b0f3Smrg    case $posix_mkdir in
327d514b0f3Smrg      '')
328d514b0f3Smrg        # With -d, create the new directory with the user-specified mode.
329d514b0f3Smrg        # Otherwise, rely on $mkdir_umask.
330d514b0f3Smrg        if test -n "$dir_arg"; then
331d514b0f3Smrg          mkdir_mode=-m$mode
332d514b0f3Smrg        else
333d514b0f3Smrg          mkdir_mode=
334d514b0f3Smrg        fi
335d514b0f3Smrg
336d514b0f3Smrg        posix_mkdir=false
337d514b0f3Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338d514b0f3Smrg	# here however when possible just to lower collision chance.
339d514b0f3Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340d514b0f3Smrg
341d514b0f3Smrg	trap '
342d514b0f3Smrg	  ret=$?
343d514b0f3Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344d514b0f3Smrg	  exit $ret
345d514b0f3Smrg	' 0
346d514b0f3Smrg
347d514b0f3Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348d514b0f3Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349d514b0f3Smrg	# directory is successfully created first before we actually test
350d514b0f3Smrg	# 'mkdir -p'.
351d514b0f3Smrg	if (umask $mkdir_umask &&
352d514b0f3Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353d514b0f3Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354d514b0f3Smrg	then
355d514b0f3Smrg	  if test -z "$dir_arg" || {
356d514b0f3Smrg	       # Check for POSIX incompatibilities with -m.
357d514b0f3Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358d514b0f3Smrg	       # other-writable bit of parent directory when it shouldn't.
359d514b0f3Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360d514b0f3Smrg	       test_tmpdir="$tmpdir/a"
361d514b0f3Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362d514b0f3Smrg	       case $ls_ld_tmpdir in
363d514b0f3Smrg		 d????-?r-*) different_mode=700;;
364d514b0f3Smrg		 d????-?--*) different_mode=755;;
365d514b0f3Smrg		 *) false;;
366d514b0f3Smrg	       esac &&
367d514b0f3Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368d514b0f3Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369d514b0f3Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370d514b0f3Smrg	       }
371d514b0f3Smrg	     }
372d514b0f3Smrg	  then posix_mkdir=:
373d514b0f3Smrg	  fi
374d514b0f3Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375d514b0f3Smrg	else
376d514b0f3Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377d514b0f3Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378d514b0f3Smrg	fi
379d514b0f3Smrg	trap '' 0;;
380d514b0f3Smrg    esac
381d514b0f3Smrg
382d514b0f3Smrg    if
383d514b0f3Smrg      $posix_mkdir && (
384d514b0f3Smrg        umask $mkdir_umask &&
385d514b0f3Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386d514b0f3Smrg      )
387d514b0f3Smrg    then :
388d514b0f3Smrg    else
389d514b0f3Smrg
390d514b0f3Smrg      # mkdir does not conform to POSIX,
391d514b0f3Smrg      # or it failed possibly due to a race condition.  Create the
392d514b0f3Smrg      # directory the slow way, step by step, checking for races as we go.
393d514b0f3Smrg
394d514b0f3Smrg      case $dstdir in
395d514b0f3Smrg        /*) prefix='/';;
396d514b0f3Smrg        [-=\(\)!]*) prefix='./';;
397d514b0f3Smrg        *)  prefix='';;
398d514b0f3Smrg      esac
399d514b0f3Smrg
400d514b0f3Smrg      oIFS=$IFS
401d514b0f3Smrg      IFS=/
402d514b0f3Smrg      set -f
403d514b0f3Smrg      set fnord $dstdir
404d514b0f3Smrg      shift
405d514b0f3Smrg      set +f
406d514b0f3Smrg      IFS=$oIFS
407d514b0f3Smrg
408d514b0f3Smrg      prefixes=
409d514b0f3Smrg
410d514b0f3Smrg      for d
411d514b0f3Smrg      do
412d514b0f3Smrg        test X"$d" = X && continue
413d514b0f3Smrg
414d514b0f3Smrg        prefix=$prefix$d
415d514b0f3Smrg        if test -d "$prefix"; then
416d514b0f3Smrg          prefixes=
417d514b0f3Smrg        else
418d514b0f3Smrg          if $posix_mkdir; then
419d514b0f3Smrg            (umask $mkdir_umask &&
420d514b0f3Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421d514b0f3Smrg            # Don't fail if two instances are running concurrently.
422d514b0f3Smrg            test -d "$prefix" || exit 1
423d514b0f3Smrg          else
424d514b0f3Smrg            case $prefix in
425d514b0f3Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426d514b0f3Smrg              *) qprefix=$prefix;;
427d514b0f3Smrg            esac
428d514b0f3Smrg            prefixes="$prefixes '$qprefix'"
429d514b0f3Smrg          fi
430d514b0f3Smrg        fi
431d514b0f3Smrg        prefix=$prefix/
432d514b0f3Smrg      done
433d514b0f3Smrg
434d514b0f3Smrg      if test -n "$prefixes"; then
435d514b0f3Smrg        # Don't fail if two instances are running concurrently.
436d514b0f3Smrg        (umask $mkdir_umask &&
437d514b0f3Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438d514b0f3Smrg          test -d "$dstdir" || exit 1
439d514b0f3Smrg        obsolete_mkdir_used=true
440d514b0f3Smrg      fi
441d514b0f3Smrg    fi
442d514b0f3Smrg  fi
443d514b0f3Smrg
444d514b0f3Smrg  if test -n "$dir_arg"; then
445d514b0f3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446d514b0f3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447d514b0f3Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448d514b0f3Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449d514b0f3Smrg  else
450d514b0f3Smrg
451d514b0f3Smrg    # Make a couple of temp file names in the proper directory.
452d514b0f3Smrg    dsttmp=${dstdirslash}_inst.$$_
453d514b0f3Smrg    rmtmp=${dstdirslash}_rm.$$_
454d514b0f3Smrg
455d514b0f3Smrg    # Trap to clean up those temp files at exit.
456d514b0f3Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457d514b0f3Smrg
458d514b0f3Smrg    # Copy the file name to the temp name.
459d514b0f3Smrg    (umask $cp_umask &&
460d514b0f3Smrg     { test -z "$stripcmd" || {
461d514b0f3Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462d514b0f3Smrg	 # which would cause strip to fail.
463d514b0f3Smrg	 if test -z "$doit"; then
464d514b0f3Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465d514b0f3Smrg	 else
466d514b0f3Smrg	   $doit touch "$dsttmp"
467d514b0f3Smrg	 fi
468d514b0f3Smrg       }
469d514b0f3Smrg     } &&
470d514b0f3Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471d514b0f3Smrg
472d514b0f3Smrg    # and set any options; do chmod last to preserve setuid bits.
473d514b0f3Smrg    #
474d514b0f3Smrg    # If any of these fail, we abort the whole thing.  If we want to
475d514b0f3Smrg    # ignore errors from any of these, just make sure not to ignore
476d514b0f3Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477d514b0f3Smrg    #
478d514b0f3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479d514b0f3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480d514b0f3Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481d514b0f3Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482d514b0f3Smrg
483d514b0f3Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484d514b0f3Smrg    if $copy_on_change &&
485d514b0f3Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486d514b0f3Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487d514b0f3Smrg       set -f &&
488d514b0f3Smrg       set X $old && old=:$2:$4:$5:$6 &&
489d514b0f3Smrg       set X $new && new=:$2:$4:$5:$6 &&
490d514b0f3Smrg       set +f &&
491d514b0f3Smrg       test "$old" = "$new" &&
492d514b0f3Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493d514b0f3Smrg    then
494d514b0f3Smrg      rm -f "$dsttmp"
495d514b0f3Smrg    else
496d514b0f3Smrg      # If $backupsuffix is set, and the file being installed
497d514b0f3Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498d514b0f3Smrg      # e.g., if mv doesn't support -f.
499d514b0f3Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500d514b0f3Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501d514b0f3Smrg      fi
502d514b0f3Smrg
503d514b0f3Smrg      # Rename the file to the real destination.
504d514b0f3Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505d514b0f3Smrg
506d514b0f3Smrg      # The rename failed, perhaps because mv can't rename something else
507d514b0f3Smrg      # to itself, or perhaps because mv is so ancient that it does not
508d514b0f3Smrg      # support -f.
509d514b0f3Smrg      {
510d514b0f3Smrg        # Now remove or move aside any old file at destination location.
511d514b0f3Smrg        # We try this two ways since rm can't unlink itself on some
512d514b0f3Smrg        # systems and the destination file might be busy for other
513d514b0f3Smrg        # reasons.  In this case, the final cleanup might fail but the new
514d514b0f3Smrg        # file should still install successfully.
515d514b0f3Smrg        {
516d514b0f3Smrg          test ! -f "$dst" ||
517d514b0f3Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518d514b0f3Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519d514b0f3Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520d514b0f3Smrg          } ||
521d514b0f3Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522d514b0f3Smrg            (exit 1); exit 1
523d514b0f3Smrg          }
524d514b0f3Smrg        } &&
525d514b0f3Smrg
526d514b0f3Smrg        # Now rename the file to the real destination.
527d514b0f3Smrg        $doit $mvcmd "$dsttmp" "$dst"
528d514b0f3Smrg      }
529d514b0f3Smrg    fi || exit 1
530d514b0f3Smrg
531d514b0f3Smrg    trap '' 0
532d514b0f3Smrg  fi
533d514b0f3Smrgdone
534d514b0f3Smrg
535d514b0f3Smrg# Local variables:
536d514b0f3Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537d514b0f3Smrg# time-stamp-start: "scriptversion="
538d514b0f3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539d514b0f3Smrg# time-stamp-time-zone: "UTC0"
540d514b0f3Smrg# time-stamp-end: "; # UTC"
541d514b0f3Smrg# End:
542