1c582b7e3Smrg#!/bin/sh
2c582b7e3Smrg# install - install a program, script, or datafile
3c582b7e3Smrg
48ce07328Smrgscriptversion=2020-11-14.01; # UTC
5c582b7e3Smrg
6c582b7e3Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7c582b7e3Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8c582b7e3Smrg# following copyright and license.
9c582b7e3Smrg#
10c582b7e3Smrg# Copyright (C) 1994 X Consortium
11c582b7e3Smrg#
12c582b7e3Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13c582b7e3Smrg# of this software and associated documentation files (the "Software"), to
14c582b7e3Smrg# deal in the Software without restriction, including without limitation the
15c582b7e3Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16c582b7e3Smrg# sell copies of the Software, and to permit persons to whom the Software is
17c582b7e3Smrg# furnished to do so, subject to the following conditions:
18c582b7e3Smrg#
19c582b7e3Smrg# The above copyright notice and this permission notice shall be included in
20c582b7e3Smrg# all copies or substantial portions of the Software.
21c582b7e3Smrg#
22c582b7e3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23c582b7e3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24c582b7e3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25c582b7e3Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26c582b7e3Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27c582b7e3Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28c582b7e3Smrg#
29c582b7e3Smrg# Except as contained in this notice, the name of the X Consortium shall not
30c582b7e3Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31c582b7e3Smrg# ings in this Software without prior written authorization from the X Consor-
32c582b7e3Smrg# tium.
33c582b7e3Smrg#
34c582b7e3Smrg#
35c582b7e3Smrg# FSF changes to this file are in the public domain.
36c582b7e3Smrg#
37c582b7e3Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38e3d74329Smrg# 'make' implicit rules from creating a file called install from it
39c582b7e3Smrg# when there is no Makefile.
40c582b7e3Smrg#
41c582b7e3Smrg# This script is compatible with the BSD install script, but was written
42c582b7e3Smrg# from scratch.
43c582b7e3Smrg
44a56d54acSmrgtab='	'
45c582b7e3Smrgnl='
46c582b7e3Smrg'
47a56d54acSmrgIFS=" $tab$nl"
48c582b7e3Smrg
49a56d54acSmrg# Set DOITPROG to "echo" to test this script.
50c582b7e3Smrg
51c582b7e3Smrgdoit=${DOITPROG-}
52a56d54acSmrgdoit_exec=${doit:-exec}
53c582b7e3Smrg
54c582b7e3Smrg# Put in absolute file names if you don't have them in your path;
55c582b7e3Smrg# or use environment vars.
56c582b7e3Smrg
57c582b7e3Smrgchgrpprog=${CHGRPPROG-chgrp}
58c582b7e3Smrgchmodprog=${CHMODPROG-chmod}
59c582b7e3Smrgchownprog=${CHOWNPROG-chown}
60c582b7e3Smrgcmpprog=${CMPPROG-cmp}
61c582b7e3Smrgcpprog=${CPPROG-cp}
62c582b7e3Smrgmkdirprog=${MKDIRPROG-mkdir}
63c582b7e3Smrgmvprog=${MVPROG-mv}
64c582b7e3Smrgrmprog=${RMPROG-rm}
65c582b7e3Smrgstripprog=${STRIPPROG-strip}
66c582b7e3Smrg
67c582b7e3Smrgposix_mkdir=
68c582b7e3Smrg
69c582b7e3Smrg# Desired mode of installed file.
70c582b7e3Smrgmode=0755
71c582b7e3Smrg
728ce07328Smrg# Create dirs (including intermediate dirs) using mode 755.
738ce07328Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
748ce07328Smrgmkdir_umask=22
758ce07328Smrg
768ce07328Smrgbackupsuffix=
77c582b7e3Smrgchgrpcmd=
78c582b7e3Smrgchmodcmd=$chmodprog
79c582b7e3Smrgchowncmd=
80c582b7e3Smrgmvcmd=$mvprog
81c582b7e3Smrgrmcmd="$rmprog -f"
82c582b7e3Smrgstripcmd=
83c582b7e3Smrg
84c582b7e3Smrgsrc=
85c582b7e3Smrgdst=
86c582b7e3Smrgdir_arg=
87c582b7e3Smrgdst_arg=
88c582b7e3Smrg
89c582b7e3Smrgcopy_on_change=false
90a56d54acSmrgis_target_a_directory=possibly
91c582b7e3Smrg
92c582b7e3Smrgusage="\
93c582b7e3SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94c582b7e3Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95c582b7e3Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96c582b7e3Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97c582b7e3Smrg
98c582b7e3SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99c582b7e3SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100c582b7e3SmrgIn the 4th, create DIRECTORIES.
101c582b7e3Smrg
102c582b7e3SmrgOptions:
103c582b7e3Smrg     --help     display this help and exit.
104c582b7e3Smrg     --version  display version info and exit.
105c582b7e3Smrg
106c582b7e3Smrg  -c            (ignored)
1078ce07328Smrg  -C            install only if different (preserve data modification time)
108c582b7e3Smrg  -d            create directories instead of installing files.
109c582b7e3Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110c582b7e3Smrg  -m MODE       $chmodprog installed files to MODE.
111c582b7e3Smrg  -o USER       $chownprog installed files to USER.
1128ce07328Smrg  -p            pass -p to $cpprog.
113c582b7e3Smrg  -s            $stripprog installed files.
1148ce07328Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115c582b7e3Smrg  -t DIRECTORY  install into DIRECTORY.
116c582b7e3Smrg  -T            report an error if DSTFILE is a directory.
117c582b7e3Smrg
118c582b7e3SmrgEnvironment variables override the default commands:
119c582b7e3Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120c582b7e3Smrg  RMPROG STRIPPROG
1218ce07328Smrg
1228ce07328SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1238ce07328Smrgit's up to you to specify -f if you want it.
1248ce07328Smrg
1258ce07328SmrgIf -S is not specified, no backups are attempted.
1268ce07328Smrg
1278ce07328SmrgEmail bug reports to bug-automake@gnu.org.
1288ce07328SmrgAutomake home page: https://www.gnu.org/software/automake/
129c582b7e3Smrg"
130c582b7e3Smrg
131c582b7e3Smrgwhile test $# -ne 0; do
132c582b7e3Smrg  case $1 in
133c582b7e3Smrg    -c) ;;
134c582b7e3Smrg
135c582b7e3Smrg    -C) copy_on_change=true;;
136c582b7e3Smrg
137c582b7e3Smrg    -d) dir_arg=true;;
138c582b7e3Smrg
139c582b7e3Smrg    -g) chgrpcmd="$chgrpprog $2"
140a56d54acSmrg        shift;;
141c582b7e3Smrg
142c582b7e3Smrg    --help) echo "$usage"; exit $?;;
143c582b7e3Smrg
144c582b7e3Smrg    -m) mode=$2
145a56d54acSmrg        case $mode in
146a56d54acSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147a56d54acSmrg            echo "$0: invalid mode: $mode" >&2
148a56d54acSmrg            exit 1;;
149a56d54acSmrg        esac
150a56d54acSmrg        shift;;
151c582b7e3Smrg
152c582b7e3Smrg    -o) chowncmd="$chownprog $2"
153a56d54acSmrg        shift;;
154c582b7e3Smrg
1558ce07328Smrg    -p) cpprog="$cpprog -p";;
1568ce07328Smrg
157c582b7e3Smrg    -s) stripcmd=$stripprog;;
158c582b7e3Smrg
1598ce07328Smrg    -S) backupsuffix="$2"
1608ce07328Smrg        shift;;
1618ce07328Smrg
162a56d54acSmrg    -t)
163a56d54acSmrg        is_target_a_directory=always
164a56d54acSmrg        dst_arg=$2
165a56d54acSmrg        # Protect names problematic for 'test' and other utilities.
166a56d54acSmrg        case $dst_arg in
167a56d54acSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168a56d54acSmrg        esac
169a56d54acSmrg        shift;;
170c582b7e3Smrg
171a56d54acSmrg    -T) is_target_a_directory=never;;
172c582b7e3Smrg
173c582b7e3Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174c582b7e3Smrg
175a56d54acSmrg    --) shift
176a56d54acSmrg        break;;
177c582b7e3Smrg
178a56d54acSmrg    -*) echo "$0: invalid option: $1" >&2
179a56d54acSmrg        exit 1;;
180c582b7e3Smrg
181c582b7e3Smrg    *)  break;;
182c582b7e3Smrg  esac
183c582b7e3Smrg  shift
184c582b7e3Smrgdone
185c582b7e3Smrg
186a56d54acSmrg# We allow the use of options -d and -T together, by making -d
187a56d54acSmrg# take the precedence; this is for compatibility with GNU install.
188a56d54acSmrg
189a56d54acSmrgif test -n "$dir_arg"; then
190a56d54acSmrg  if test -n "$dst_arg"; then
191a56d54acSmrg    echo "$0: target directory not allowed when installing a directory." >&2
192a56d54acSmrg    exit 1
193a56d54acSmrg  fi
194a56d54acSmrgfi
195a56d54acSmrg
196c582b7e3Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197c582b7e3Smrg  # When -d is used, all remaining arguments are directories to create.
198c582b7e3Smrg  # When -t is used, the destination is already specified.
199c582b7e3Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200c582b7e3Smrg  for arg
201c582b7e3Smrg  do
202c582b7e3Smrg    if test -n "$dst_arg"; then
203c582b7e3Smrg      # $@ is not empty: it contains at least $arg.
204c582b7e3Smrg      set fnord "$@" "$dst_arg"
205c582b7e3Smrg      shift # fnord
206c582b7e3Smrg    fi
207c582b7e3Smrg    shift # arg
208c582b7e3Smrg    dst_arg=$arg
209e3d74329Smrg    # Protect names problematic for 'test' and other utilities.
21042a55b46Smrg    case $dst_arg in
21142a55b46Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21242a55b46Smrg    esac
213c582b7e3Smrg  done
214c582b7e3Smrgfi
215c582b7e3Smrg
216c582b7e3Smrgif test $# -eq 0; then
217c582b7e3Smrg  if test -z "$dir_arg"; then
218c582b7e3Smrg    echo "$0: no input file specified." >&2
219c582b7e3Smrg    exit 1
220c582b7e3Smrg  fi
221e3d74329Smrg  # It's OK to call 'install-sh -d' without argument.
222c582b7e3Smrg  # This can happen when creating conditional directories.
223c582b7e3Smrg  exit 0
224c582b7e3Smrgfi
225c582b7e3Smrg
226a56d54acSmrgif test -z "$dir_arg"; then
227a56d54acSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228a56d54acSmrg    if test ! -d "$dst_arg"; then
229a56d54acSmrg      echo "$0: $dst_arg: Is not a directory." >&2
230a56d54acSmrg      exit 1
231a56d54acSmrg    fi
232a56d54acSmrg  fi
233a56d54acSmrgfi
234a56d54acSmrg
235c582b7e3Smrgif test -z "$dir_arg"; then
23642a55b46Smrg  do_exit='(exit $ret); exit $ret'
23742a55b46Smrg  trap "ret=129; $do_exit" 1
23842a55b46Smrg  trap "ret=130; $do_exit" 2
23942a55b46Smrg  trap "ret=141; $do_exit" 13
24042a55b46Smrg  trap "ret=143; $do_exit" 15
241c582b7e3Smrg
242c582b7e3Smrg  # Set umask so as not to create temps with too-generous modes.
243c582b7e3Smrg  # However, 'strip' requires both read and write access to temps.
244c582b7e3Smrg  case $mode in
245c582b7e3Smrg    # Optimize common cases.
246c582b7e3Smrg    *644) cp_umask=133;;
247c582b7e3Smrg    *755) cp_umask=22;;
248c582b7e3Smrg
249c582b7e3Smrg    *[0-7])
250c582b7e3Smrg      if test -z "$stripcmd"; then
251a56d54acSmrg        u_plus_rw=
252c582b7e3Smrg      else
253a56d54acSmrg        u_plus_rw='% 200'
254c582b7e3Smrg      fi
255c582b7e3Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256c582b7e3Smrg    *)
257c582b7e3Smrg      if test -z "$stripcmd"; then
258a56d54acSmrg        u_plus_rw=
259c582b7e3Smrg      else
260a56d54acSmrg        u_plus_rw=,u+rw
261c582b7e3Smrg      fi
262c582b7e3Smrg      cp_umask=$mode$u_plus_rw;;
263c582b7e3Smrg  esac
264c582b7e3Smrgfi
265c582b7e3Smrg
266c582b7e3Smrgfor src
267c582b7e3Smrgdo
268e3d74329Smrg  # Protect names problematic for 'test' and other utilities.
269c582b7e3Smrg  case $src in
27042a55b46Smrg    -* | [=\(\)!]) src=./$src;;
271c582b7e3Smrg  esac
272c582b7e3Smrg
273c582b7e3Smrg  if test -n "$dir_arg"; then
274c582b7e3Smrg    dst=$src
275c582b7e3Smrg    dstdir=$dst
276c582b7e3Smrg    test -d "$dstdir"
277c582b7e3Smrg    dstdir_status=$?
2788ce07328Smrg    # Don't chown directories that already exist.
2798ce07328Smrg    if test $dstdir_status = 0; then
2808ce07328Smrg      chowncmd=""
2818ce07328Smrg    fi
282c582b7e3Smrg  else
283c582b7e3Smrg
284c582b7e3Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285c582b7e3Smrg    # might cause directories to be created, which would be especially bad
286c582b7e3Smrg    # if $src (and thus $dsttmp) contains '*'.
287c582b7e3Smrg    if test ! -f "$src" && test ! -d "$src"; then
288c582b7e3Smrg      echo "$0: $src does not exist." >&2
289c582b7e3Smrg      exit 1
290c582b7e3Smrg    fi
291c582b7e3Smrg
292c582b7e3Smrg    if test -z "$dst_arg"; then
293c582b7e3Smrg      echo "$0: no destination specified." >&2
294c582b7e3Smrg      exit 1
295c582b7e3Smrg    fi
296c582b7e3Smrg    dst=$dst_arg
297c582b7e3Smrg
2988ce07328Smrg    # If destination is a directory, append the input filename.
299c582b7e3Smrg    if test -d "$dst"; then
300a56d54acSmrg      if test "$is_target_a_directory" = never; then
301a56d54acSmrg        echo "$0: $dst_arg: Is a directory" >&2
302a56d54acSmrg        exit 1
303c582b7e3Smrg      fi
304c582b7e3Smrg      dstdir=$dst
3058ce07328Smrg      dstbase=`basename "$src"`
3068ce07328Smrg      case $dst in
3078ce07328Smrg	*/) dst=$dst$dstbase;;
3088ce07328Smrg	*)  dst=$dst/$dstbase;;
3098ce07328Smrg      esac
310c582b7e3Smrg      dstdir_status=0
311c582b7e3Smrg    else
312a56d54acSmrg      dstdir=`dirname "$dst"`
313c582b7e3Smrg      test -d "$dstdir"
314c582b7e3Smrg      dstdir_status=$?
315c582b7e3Smrg    fi
316c582b7e3Smrg  fi
317c582b7e3Smrg
3188ce07328Smrg  case $dstdir in
3198ce07328Smrg    */) dstdirslash=$dstdir;;
3208ce07328Smrg    *)  dstdirslash=$dstdir/;;
3218ce07328Smrg  esac
3228ce07328Smrg
323c582b7e3Smrg  obsolete_mkdir_used=false
324c582b7e3Smrg
325c582b7e3Smrg  if test $dstdir_status != 0; then
326c582b7e3Smrg    case $posix_mkdir in
327c582b7e3Smrg      '')
328a56d54acSmrg        # With -d, create the new directory with the user-specified mode.
329a56d54acSmrg        # Otherwise, rely on $mkdir_umask.
330a56d54acSmrg        if test -n "$dir_arg"; then
331a56d54acSmrg          mkdir_mode=-m$mode
332a56d54acSmrg        else
333a56d54acSmrg          mkdir_mode=
334a56d54acSmrg        fi
335a56d54acSmrg
336a56d54acSmrg        posix_mkdir=false
3378ce07328Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3388ce07328Smrg	# here however when possible just to lower collision chance.
3398ce07328Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3408ce07328Smrg
3418ce07328Smrg	trap '
3428ce07328Smrg	  ret=$?
3438ce07328Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3448ce07328Smrg	  exit $ret
3458ce07328Smrg	' 0
3468ce07328Smrg
3478ce07328Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3488ce07328Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3498ce07328Smrg	# directory is successfully created first before we actually test
3508ce07328Smrg	# 'mkdir -p'.
3518ce07328Smrg	if (umask $mkdir_umask &&
3528ce07328Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3538ce07328Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3548ce07328Smrg	then
3558ce07328Smrg	  if test -z "$dir_arg" || {
3568ce07328Smrg	       # Check for POSIX incompatibilities with -m.
3578ce07328Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3588ce07328Smrg	       # other-writable bit of parent directory when it shouldn't.
3598ce07328Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3608ce07328Smrg	       test_tmpdir="$tmpdir/a"
3618ce07328Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3628ce07328Smrg	       case $ls_ld_tmpdir in
3638ce07328Smrg		 d????-?r-*) different_mode=700;;
3648ce07328Smrg		 d????-?--*) different_mode=755;;
3658ce07328Smrg		 *) false;;
3668ce07328Smrg	       esac &&
3678ce07328Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3688ce07328Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3698ce07328Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3708ce07328Smrg	       }
3718ce07328Smrg	     }
3728ce07328Smrg	  then posix_mkdir=:
3738ce07328Smrg	  fi
3748ce07328Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3758ce07328Smrg	else
3768ce07328Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
3778ce07328Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3788ce07328Smrg	fi
3798ce07328Smrg	trap '' 0;;
380c582b7e3Smrg    esac
381c582b7e3Smrg
382c582b7e3Smrg    if
383c582b7e3Smrg      $posix_mkdir && (
384a56d54acSmrg        umask $mkdir_umask &&
385a56d54acSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386c582b7e3Smrg      )
387c582b7e3Smrg    then :
388c582b7e3Smrg    else
389c582b7e3Smrg
3908ce07328Smrg      # mkdir does not conform to POSIX,
391c582b7e3Smrg      # or it failed possibly due to a race condition.  Create the
392c582b7e3Smrg      # directory the slow way, step by step, checking for races as we go.
393c582b7e3Smrg
394c582b7e3Smrg      case $dstdir in
395a56d54acSmrg        /*) prefix='/';;
396a56d54acSmrg        [-=\(\)!]*) prefix='./';;
397a56d54acSmrg        *)  prefix='';;
398c582b7e3Smrg      esac
399c582b7e3Smrg
400c582b7e3Smrg      oIFS=$IFS
401c582b7e3Smrg      IFS=/
402a56d54acSmrg      set -f
403c582b7e3Smrg      set fnord $dstdir
404c582b7e3Smrg      shift
405a56d54acSmrg      set +f
406c582b7e3Smrg      IFS=$oIFS
407c582b7e3Smrg
408c582b7e3Smrg      prefixes=
409c582b7e3Smrg
410c582b7e3Smrg      for d
411c582b7e3Smrg      do
412a56d54acSmrg        test X"$d" = X && continue
413a56d54acSmrg
414a56d54acSmrg        prefix=$prefix$d
415a56d54acSmrg        if test -d "$prefix"; then
416a56d54acSmrg          prefixes=
417a56d54acSmrg        else
418a56d54acSmrg          if $posix_mkdir; then
4198ce07328Smrg            (umask $mkdir_umask &&
420a56d54acSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421a56d54acSmrg            # Don't fail if two instances are running concurrently.
422a56d54acSmrg            test -d "$prefix" || exit 1
423a56d54acSmrg          else
424a56d54acSmrg            case $prefix in
425a56d54acSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426a56d54acSmrg              *) qprefix=$prefix;;
427a56d54acSmrg            esac
428a56d54acSmrg            prefixes="$prefixes '$qprefix'"
429a56d54acSmrg          fi
430a56d54acSmrg        fi
431a56d54acSmrg        prefix=$prefix/
432c582b7e3Smrg      done
433c582b7e3Smrg
434c582b7e3Smrg      if test -n "$prefixes"; then
435a56d54acSmrg        # Don't fail if two instances are running concurrently.
436a56d54acSmrg        (umask $mkdir_umask &&
437a56d54acSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438a56d54acSmrg          test -d "$dstdir" || exit 1
439a56d54acSmrg        obsolete_mkdir_used=true
440c582b7e3Smrg      fi
441c582b7e3Smrg    fi
442c582b7e3Smrg  fi
443c582b7e3Smrg
444c582b7e3Smrg  if test -n "$dir_arg"; then
445c582b7e3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446c582b7e3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447c582b7e3Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448c582b7e3Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449c582b7e3Smrg  else
450c582b7e3Smrg
451c582b7e3Smrg    # Make a couple of temp file names in the proper directory.
4528ce07328Smrg    dsttmp=${dstdirslash}_inst.$$_
4538ce07328Smrg    rmtmp=${dstdirslash}_rm.$$_
454c582b7e3Smrg
455c582b7e3Smrg    # Trap to clean up those temp files at exit.
456c582b7e3Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457c582b7e3Smrg
458c582b7e3Smrg    # Copy the file name to the temp name.
4598ce07328Smrg    (umask $cp_umask &&
4608ce07328Smrg     { test -z "$stripcmd" || {
4618ce07328Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4628ce07328Smrg	 # which would cause strip to fail.
4638ce07328Smrg	 if test -z "$doit"; then
4648ce07328Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4658ce07328Smrg	 else
4668ce07328Smrg	   $doit touch "$dsttmp"
4678ce07328Smrg	 fi
4688ce07328Smrg       }
4698ce07328Smrg     } &&
4708ce07328Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471c582b7e3Smrg
472c582b7e3Smrg    # and set any options; do chmod last to preserve setuid bits.
473c582b7e3Smrg    #
474c582b7e3Smrg    # If any of these fail, we abort the whole thing.  If we want to
475c582b7e3Smrg    # ignore errors from any of these, just make sure not to ignore
476c582b7e3Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477c582b7e3Smrg    #
478c582b7e3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479c582b7e3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480c582b7e3Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481c582b7e3Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482c582b7e3Smrg
483c582b7e3Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484c582b7e3Smrg    if $copy_on_change &&
485a56d54acSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486a56d54acSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487a56d54acSmrg       set -f &&
488c582b7e3Smrg       set X $old && old=:$2:$4:$5:$6 &&
489c582b7e3Smrg       set X $new && new=:$2:$4:$5:$6 &&
490a56d54acSmrg       set +f &&
491c582b7e3Smrg       test "$old" = "$new" &&
492c582b7e3Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493c582b7e3Smrg    then
494c582b7e3Smrg      rm -f "$dsttmp"
495c582b7e3Smrg    else
4968ce07328Smrg      # If $backupsuffix is set, and the file being installed
4978ce07328Smrg      # already exists, attempt a backup.  Don't worry if it fails,
4988ce07328Smrg      # e.g., if mv doesn't support -f.
4998ce07328Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
5008ce07328Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5018ce07328Smrg      fi
5028ce07328Smrg
503c582b7e3Smrg      # Rename the file to the real destination.
504c582b7e3Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505c582b7e3Smrg
506c582b7e3Smrg      # The rename failed, perhaps because mv can't rename something else
507c582b7e3Smrg      # to itself, or perhaps because mv is so ancient that it does not
508c582b7e3Smrg      # support -f.
509c582b7e3Smrg      {
510a56d54acSmrg        # Now remove or move aside any old file at destination location.
511a56d54acSmrg        # We try this two ways since rm can't unlink itself on some
512a56d54acSmrg        # systems and the destination file might be busy for other
513a56d54acSmrg        # reasons.  In this case, the final cleanup might fail but the new
514a56d54acSmrg        # file should still install successfully.
515a56d54acSmrg        {
516a56d54acSmrg          test ! -f "$dst" ||
5178ce07328Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518a56d54acSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5198ce07328Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520a56d54acSmrg          } ||
521a56d54acSmrg          { echo "$0: cannot unlink or rename $dst" >&2
522a56d54acSmrg            (exit 1); exit 1
523a56d54acSmrg          }
524a56d54acSmrg        } &&
525a56d54acSmrg
526a56d54acSmrg        # Now rename the file to the real destination.
527a56d54acSmrg        $doit $mvcmd "$dsttmp" "$dst"
528c582b7e3Smrg      }
529c582b7e3Smrg    fi || exit 1
530c582b7e3Smrg
531c582b7e3Smrg    trap '' 0
532c582b7e3Smrg  fi
533c582b7e3Smrgdone
534c582b7e3Smrg
535c582b7e3Smrg# Local variables:
5368ce07328Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537c582b7e3Smrg# time-stamp-start: "scriptversion="
538c582b7e3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5398ce07328Smrg# time-stamp-time-zone: "UTC0"
54019019ffeSmrg# time-stamp-end: "; # UTC"
541c582b7e3Smrg# End:
542