install-sh revision ffe57424
117ca54c3Smrg#!/bin/sh
217ca54c3Smrg# install - install a program, script, or datafile
317ca54c3Smrg
4ffe57424Smrgscriptversion=2020-11-14.01; # UTC
517ca54c3Smrg
617ca54c3Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
717ca54c3Smrg# later released in X11R6 (xc/config/util/install.sh) with the
817ca54c3Smrg# following copyright and license.
917ca54c3Smrg#
1017ca54c3Smrg# Copyright (C) 1994 X Consortium
1117ca54c3Smrg#
1217ca54c3Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1317ca54c3Smrg# of this software and associated documentation files (the "Software"), to
1417ca54c3Smrg# deal in the Software without restriction, including without limitation the
1517ca54c3Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1617ca54c3Smrg# sell copies of the Software, and to permit persons to whom the Software is
1717ca54c3Smrg# furnished to do so, subject to the following conditions:
1817ca54c3Smrg#
1917ca54c3Smrg# The above copyright notice and this permission notice shall be included in
2017ca54c3Smrg# all copies or substantial portions of the Software.
2117ca54c3Smrg#
2217ca54c3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2317ca54c3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2417ca54c3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2517ca54c3Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2617ca54c3Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2717ca54c3Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2817ca54c3Smrg#
2917ca54c3Smrg# Except as contained in this notice, the name of the X Consortium shall not
3017ca54c3Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3117ca54c3Smrg# ings in this Software without prior written authorization from the X Consor-
3217ca54c3Smrg# tium.
3317ca54c3Smrg#
3417ca54c3Smrg#
3517ca54c3Smrg# FSF changes to this file are in the public domain.
3617ca54c3Smrg#
3717ca54c3Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38ea1d6981Smrg# 'make' implicit rules from creating a file called install from it
3917ca54c3Smrg# when there is no Makefile.
4017ca54c3Smrg#
4117ca54c3Smrg# This script is compatible with the BSD install script, but was written
42ea1d6981Smrg# from scratch.
4317ca54c3Smrg
44ea1d6981Smrgtab='	'
45ea1d6981Smrgnl='
46ea1d6981Smrg'
47ea1d6981SmrgIFS=" $tab$nl"
4817ca54c3Smrg
49ea1d6981Smrg# Set DOITPROG to "echo" to test this script.
5017ca54c3Smrg
51ea1d6981Smrgdoit=${DOITPROG-}
52ea1d6981Smrgdoit_exec=${doit:-exec}
5317ca54c3Smrg
54ea1d6981Smrg# Put in absolute file names if you don't have them in your path;
55ea1d6981Smrg# or use environment vars.
56ea1d6981Smrg
57ea1d6981Smrgchgrpprog=${CHGRPPROG-chgrp}
58ea1d6981Smrgchmodprog=${CHMODPROG-chmod}
59ea1d6981Smrgchownprog=${CHOWNPROG-chown}
60ea1d6981Smrgcmpprog=${CMPPROG-cmp}
61ea1d6981Smrgcpprog=${CPPROG-cp}
62ea1d6981Smrgmkdirprog=${MKDIRPROG-mkdir}
63ea1d6981Smrgmvprog=${MVPROG-mv}
64ea1d6981Smrgrmprog=${RMPROG-rm}
65ea1d6981Smrgstripprog=${STRIPPROG-strip}
66ea1d6981Smrg
67ea1d6981Smrgposix_mkdir=
68ea1d6981Smrg
69ea1d6981Smrg# Desired mode of installed file.
70ea1d6981Smrgmode=0755
7117ca54c3Smrg
72ffe57424Smrg# Create dirs (including intermediate dirs) using mode 755.
73ffe57424Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74ffe57424Smrgmkdir_umask=22
75ffe57424Smrg
76ffe57424Smrgbackupsuffix=
7717ca54c3Smrgchgrpcmd=
78ea1d6981Smrgchmodcmd=$chmodprog
79ea1d6981Smrgchowncmd=
80ea1d6981Smrgmvcmd=$mvprog
8117ca54c3Smrgrmcmd="$rmprog -f"
82ea1d6981Smrgstripcmd=
83ea1d6981Smrg
8417ca54c3Smrgsrc=
8517ca54c3Smrgdst=
8617ca54c3Smrgdir_arg=
87ea1d6981Smrgdst_arg=
8817ca54c3Smrg
89ea1d6981Smrgcopy_on_change=false
90ea1d6981Smrgis_target_a_directory=possibly
91ea1d6981Smrg
92ea1d6981Smrgusage="\
93ea1d6981SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9417ca54c3Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95ea1d6981Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96ea1d6981Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9717ca54c3Smrg
98ea1d6981SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99ea1d6981SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100ea1d6981SmrgIn the 4th, create DIRECTORIES.
10117ca54c3Smrg
10217ca54c3SmrgOptions:
103ea1d6981Smrg     --help     display this help and exit.
104ea1d6981Smrg     --version  display version info and exit.
105ea1d6981Smrg
106ea1d6981Smrg  -c            (ignored)
107ffe57424Smrg  -C            install only if different (preserve data modification time)
108ea1d6981Smrg  -d            create directories instead of installing files.
109ea1d6981Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110ea1d6981Smrg  -m MODE       $chmodprog installed files to MODE.
111ea1d6981Smrg  -o USER       $chownprog installed files to USER.
112ffe57424Smrg  -p            pass -p to $cpprog.
113ea1d6981Smrg  -s            $stripprog installed files.
114ffe57424Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115ea1d6981Smrg  -t DIRECTORY  install into DIRECTORY.
116ea1d6981Smrg  -T            report an error if DSTFILE is a directory.
11717ca54c3Smrg
11817ca54c3SmrgEnvironment variables override the default commands:
119ea1d6981Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120ea1d6981Smrg  RMPROG STRIPPROG
121ffe57424Smrg
122ffe57424SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123ffe57424Smrgit's up to you to specify -f if you want it.
124ffe57424Smrg
125ffe57424SmrgIf -S is not specified, no backups are attempted.
126ffe57424Smrg
127ffe57424SmrgEmail bug reports to bug-automake@gnu.org.
128ffe57424SmrgAutomake home page: https://www.gnu.org/software/automake/
12917ca54c3Smrg"
13017ca54c3Smrg
131ea1d6981Smrgwhile test $# -ne 0; do
13217ca54c3Smrg  case $1 in
133ea1d6981Smrg    -c) ;;
13417ca54c3Smrg
135ea1d6981Smrg    -C) copy_on_change=true;;
13617ca54c3Smrg
137ea1d6981Smrg    -d) dir_arg=true;;
13817ca54c3Smrg
13917ca54c3Smrg    -g) chgrpcmd="$chgrpprog $2"
140ea1d6981Smrg        shift;;
14117ca54c3Smrg
142ea1d6981Smrg    --help) echo "$usage"; exit $?;;
14317ca54c3Smrg
144ea1d6981Smrg    -m) mode=$2
145ea1d6981Smrg        case $mode in
146ea1d6981Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147ea1d6981Smrg            echo "$0: invalid mode: $mode" >&2
148ea1d6981Smrg            exit 1;;
149ea1d6981Smrg        esac
150ea1d6981Smrg        shift;;
15117ca54c3Smrg
15217ca54c3Smrg    -o) chowncmd="$chownprog $2"
153ea1d6981Smrg        shift;;
154ea1d6981Smrg
155ffe57424Smrg    -p) cpprog="$cpprog -p";;
156ffe57424Smrg
157ea1d6981Smrg    -s) stripcmd=$stripprog;;
158ea1d6981Smrg
159ffe57424Smrg    -S) backupsuffix="$2"
160ffe57424Smrg        shift;;
161ffe57424Smrg
162ea1d6981Smrg    -t)
163ea1d6981Smrg        is_target_a_directory=always
164ea1d6981Smrg        dst_arg=$2
165ea1d6981Smrg        # Protect names problematic for 'test' and other utilities.
166ea1d6981Smrg        case $dst_arg in
167ea1d6981Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168ea1d6981Smrg        esac
169ea1d6981Smrg        shift;;
170ea1d6981Smrg
171ea1d6981Smrg    -T) is_target_a_directory=never;;
172ea1d6981Smrg
173ea1d6981Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174ea1d6981Smrg
175ea1d6981Smrg    --) shift
176ea1d6981Smrg        break;;
177ea1d6981Smrg
178ea1d6981Smrg    -*) echo "$0: invalid option: $1" >&2
179ea1d6981Smrg        exit 1;;
180ea1d6981Smrg
181ea1d6981Smrg    *)  break;;
18217ca54c3Smrg  esac
183ea1d6981Smrg  shift
18417ca54c3Smrgdone
18517ca54c3Smrg
186ea1d6981Smrg# We allow the use of options -d and -T together, by making -d
187ea1d6981Smrg# take the precedence; this is for compatibility with GNU install.
188ea1d6981Smrg
189ea1d6981Smrgif test -n "$dir_arg"; then
190ea1d6981Smrg  if test -n "$dst_arg"; then
191ea1d6981Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192ea1d6981Smrg    exit 1
193ea1d6981Smrg  fi
194ea1d6981Smrgfi
195ea1d6981Smrg
196ea1d6981Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197ea1d6981Smrg  # When -d is used, all remaining arguments are directories to create.
198ea1d6981Smrg  # When -t is used, the destination is already specified.
199ea1d6981Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200ea1d6981Smrg  for arg
201ea1d6981Smrg  do
202ea1d6981Smrg    if test -n "$dst_arg"; then
203ea1d6981Smrg      # $@ is not empty: it contains at least $arg.
204ea1d6981Smrg      set fnord "$@" "$dst_arg"
205ea1d6981Smrg      shift # fnord
206ea1d6981Smrg    fi
207ea1d6981Smrg    shift # arg
208ea1d6981Smrg    dst_arg=$arg
209ea1d6981Smrg    # Protect names problematic for 'test' and other utilities.
210ea1d6981Smrg    case $dst_arg in
211ea1d6981Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212ea1d6981Smrg    esac
213ea1d6981Smrg  done
214ea1d6981Smrgfi
215ea1d6981Smrg
216ea1d6981Smrgif test $# -eq 0; then
21717ca54c3Smrg  if test -z "$dir_arg"; then
21817ca54c3Smrg    echo "$0: no input file specified." >&2
21917ca54c3Smrg    exit 1
22017ca54c3Smrg  fi
221ea1d6981Smrg  # It's OK to call 'install-sh -d' without argument.
22217ca54c3Smrg  # This can happen when creating conditional directories.
22317ca54c3Smrg  exit 0
22417ca54c3Smrgfi
22517ca54c3Smrg
226ea1d6981Smrgif test -z "$dir_arg"; then
227ea1d6981Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228ea1d6981Smrg    if test ! -d "$dst_arg"; then
229ea1d6981Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230ea1d6981Smrg      exit 1
231ea1d6981Smrg    fi
232ea1d6981Smrg  fi
233ea1d6981Smrgfi
234ea1d6981Smrg
235ea1d6981Smrgif test -z "$dir_arg"; then
236ea1d6981Smrg  do_exit='(exit $ret); exit $ret'
237ea1d6981Smrg  trap "ret=129; $do_exit" 1
238ea1d6981Smrg  trap "ret=130; $do_exit" 2
239ea1d6981Smrg  trap "ret=141; $do_exit" 13
240ea1d6981Smrg  trap "ret=143; $do_exit" 15
241ea1d6981Smrg
242ea1d6981Smrg  # Set umask so as not to create temps with too-generous modes.
243ea1d6981Smrg  # However, 'strip' requires both read and write access to temps.
244ea1d6981Smrg  case $mode in
245ea1d6981Smrg    # Optimize common cases.
246ea1d6981Smrg    *644) cp_umask=133;;
247ea1d6981Smrg    *755) cp_umask=22;;
248ea1d6981Smrg
249ea1d6981Smrg    *[0-7])
250ea1d6981Smrg      if test -z "$stripcmd"; then
251ea1d6981Smrg        u_plus_rw=
252ea1d6981Smrg      else
253ea1d6981Smrg        u_plus_rw='% 200'
254ea1d6981Smrg      fi
255ea1d6981Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256ea1d6981Smrg    *)
257ea1d6981Smrg      if test -z "$stripcmd"; then
258ea1d6981Smrg        u_plus_rw=
259ea1d6981Smrg      else
260ea1d6981Smrg        u_plus_rw=,u+rw
261ea1d6981Smrg      fi
262ea1d6981Smrg      cp_umask=$mode$u_plus_rw;;
263ea1d6981Smrg  esac
264ea1d6981Smrgfi
265ea1d6981Smrg
26617ca54c3Smrgfor src
26717ca54c3Smrgdo
268ea1d6981Smrg  # Protect names problematic for 'test' and other utilities.
26917ca54c3Smrg  case $src in
270ea1d6981Smrg    -* | [=\(\)!]) src=./$src;;
27117ca54c3Smrg  esac
27217ca54c3Smrg
27317ca54c3Smrg  if test -n "$dir_arg"; then
27417ca54c3Smrg    dst=$src
275ea1d6981Smrg    dstdir=$dst
276ea1d6981Smrg    test -d "$dstdir"
277ea1d6981Smrg    dstdir_status=$?
278ffe57424Smrg    # Don't chown directories that already exist.
279ffe57424Smrg    if test $dstdir_status = 0; then
280ffe57424Smrg      chowncmd=""
281ffe57424Smrg    fi
28217ca54c3Smrg  else
283ea1d6981Smrg
284ea1d6981Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28517ca54c3Smrg    # might cause directories to be created, which would be especially bad
28617ca54c3Smrg    # if $src (and thus $dsttmp) contains '*'.
28717ca54c3Smrg    if test ! -f "$src" && test ! -d "$src"; then
28817ca54c3Smrg      echo "$0: $src does not exist." >&2
28917ca54c3Smrg      exit 1
29017ca54c3Smrg    fi
29117ca54c3Smrg
292ea1d6981Smrg    if test -z "$dst_arg"; then
29317ca54c3Smrg      echo "$0: no destination specified." >&2
29417ca54c3Smrg      exit 1
29517ca54c3Smrg    fi
296ea1d6981Smrg    dst=$dst_arg
29717ca54c3Smrg
298ea1d6981Smrg    # If destination is a directory, append the input filename.
29917ca54c3Smrg    if test -d "$dst"; then
300ea1d6981Smrg      if test "$is_target_a_directory" = never; then
301ea1d6981Smrg        echo "$0: $dst_arg: Is a directory" >&2
302ea1d6981Smrg        exit 1
303ea1d6981Smrg      fi
304ea1d6981Smrg      dstdir=$dst
305ea1d6981Smrg      dstbase=`basename "$src"`
306ea1d6981Smrg      case $dst in
307ea1d6981Smrg	*/) dst=$dst$dstbase;;
308ea1d6981Smrg	*)  dst=$dst/$dstbase;;
309ea1d6981Smrg      esac
310ea1d6981Smrg      dstdir_status=0
311ea1d6981Smrg    else
312ea1d6981Smrg      dstdir=`dirname "$dst"`
313ea1d6981Smrg      test -d "$dstdir"
314ea1d6981Smrg      dstdir_status=$?
31517ca54c3Smrg    fi
31617ca54c3Smrg  fi
31717ca54c3Smrg
318ea1d6981Smrg  case $dstdir in
319ea1d6981Smrg    */) dstdirslash=$dstdir;;
320ea1d6981Smrg    *)  dstdirslash=$dstdir/;;
321ea1d6981Smrg  esac
32217ca54c3Smrg
323ea1d6981Smrg  obsolete_mkdir_used=false
324ea1d6981Smrg
325ea1d6981Smrg  if test $dstdir_status != 0; then
326ea1d6981Smrg    case $posix_mkdir in
327ea1d6981Smrg      '')
328ea1d6981Smrg        # With -d, create the new directory with the user-specified mode.
329ea1d6981Smrg        # Otherwise, rely on $mkdir_umask.
330ea1d6981Smrg        if test -n "$dir_arg"; then
331ea1d6981Smrg          mkdir_mode=-m$mode
332ea1d6981Smrg        else
333ea1d6981Smrg          mkdir_mode=
334ea1d6981Smrg        fi
335ea1d6981Smrg
336ea1d6981Smrg        posix_mkdir=false
337ffe57424Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338ffe57424Smrg	# here however when possible just to lower collision chance.
339ffe57424Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340ffe57424Smrg
341ffe57424Smrg	trap '
342ffe57424Smrg	  ret=$?
343ffe57424Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344ffe57424Smrg	  exit $ret
345ffe57424Smrg	' 0
346ffe57424Smrg
347ffe57424Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348ffe57424Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349ffe57424Smrg	# directory is successfully created first before we actually test
350ffe57424Smrg	# 'mkdir -p'.
351ffe57424Smrg	if (umask $mkdir_umask &&
352ffe57424Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353ffe57424Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354ffe57424Smrg	then
355ffe57424Smrg	  if test -z "$dir_arg" || {
356ffe57424Smrg	       # Check for POSIX incompatibilities with -m.
357ffe57424Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358ffe57424Smrg	       # other-writable bit of parent directory when it shouldn't.
359ffe57424Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360ffe57424Smrg	       test_tmpdir="$tmpdir/a"
361ffe57424Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362ffe57424Smrg	       case $ls_ld_tmpdir in
363ffe57424Smrg		 d????-?r-*) different_mode=700;;
364ffe57424Smrg		 d????-?--*) different_mode=755;;
365ffe57424Smrg		 *) false;;
366ffe57424Smrg	       esac &&
367ffe57424Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368ffe57424Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369ffe57424Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370ffe57424Smrg	       }
371ffe57424Smrg	     }
372ffe57424Smrg	  then posix_mkdir=:
373ffe57424Smrg	  fi
374ffe57424Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375ffe57424Smrg	else
376ffe57424Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377ffe57424Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378ffe57424Smrg	fi
379ffe57424Smrg	trap '' 0;;
380ea1d6981Smrg    esac
38117ca54c3Smrg
382ea1d6981Smrg    if
383ea1d6981Smrg      $posix_mkdir && (
384ea1d6981Smrg        umask $mkdir_umask &&
385ea1d6981Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386ea1d6981Smrg      )
387ea1d6981Smrg    then :
388ea1d6981Smrg    else
38917ca54c3Smrg
390ffe57424Smrg      # mkdir does not conform to POSIX,
391ea1d6981Smrg      # or it failed possibly due to a race condition.  Create the
392ea1d6981Smrg      # directory the slow way, step by step, checking for races as we go.
39317ca54c3Smrg
394ea1d6981Smrg      case $dstdir in
395ea1d6981Smrg        /*) prefix='/';;
396ea1d6981Smrg        [-=\(\)!]*) prefix='./';;
397ea1d6981Smrg        *)  prefix='';;
398ea1d6981Smrg      esac
39917ca54c3Smrg
400ea1d6981Smrg      oIFS=$IFS
401ea1d6981Smrg      IFS=/
402ea1d6981Smrg      set -f
403ea1d6981Smrg      set fnord $dstdir
40417ca54c3Smrg      shift
405ea1d6981Smrg      set +f
406ea1d6981Smrg      IFS=$oIFS
407ea1d6981Smrg
408ea1d6981Smrg      prefixes=
409ea1d6981Smrg
410ea1d6981Smrg      for d
411ea1d6981Smrg      do
412ea1d6981Smrg        test X"$d" = X && continue
413ea1d6981Smrg
414ea1d6981Smrg        prefix=$prefix$d
415ea1d6981Smrg        if test -d "$prefix"; then
416ea1d6981Smrg          prefixes=
417ea1d6981Smrg        else
418ea1d6981Smrg          if $posix_mkdir; then
419ffe57424Smrg            (umask $mkdir_umask &&
420ea1d6981Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421ea1d6981Smrg            # Don't fail if two instances are running concurrently.
422ea1d6981Smrg            test -d "$prefix" || exit 1
423ea1d6981Smrg          else
424ea1d6981Smrg            case $prefix in
425ea1d6981Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426ea1d6981Smrg              *) qprefix=$prefix;;
427ea1d6981Smrg            esac
428ea1d6981Smrg            prefixes="$prefixes '$qprefix'"
429ea1d6981Smrg          fi
430ea1d6981Smrg        fi
431ea1d6981Smrg        prefix=$prefix/
432ea1d6981Smrg      done
433ea1d6981Smrg
434ea1d6981Smrg      if test -n "$prefixes"; then
435ea1d6981Smrg        # Don't fail if two instances are running concurrently.
436ea1d6981Smrg        (umask $mkdir_umask &&
437ea1d6981Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438ea1d6981Smrg          test -d "$dstdir" || exit 1
439ea1d6981Smrg        obsolete_mkdir_used=true
44017ca54c3Smrg      fi
441ea1d6981Smrg    fi
44217ca54c3Smrg  fi
44317ca54c3Smrg
44417ca54c3Smrg  if test -n "$dir_arg"; then
445ea1d6981Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446ea1d6981Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447ea1d6981Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448ea1d6981Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44917ca54c3Smrg  else
45017ca54c3Smrg
45117ca54c3Smrg    # Make a couple of temp file names in the proper directory.
452ea1d6981Smrg    dsttmp=${dstdirslash}_inst.$$_
453ea1d6981Smrg    rmtmp=${dstdirslash}_rm.$$_
45417ca54c3Smrg
45517ca54c3Smrg    # Trap to clean up those temp files at exit.
456ea1d6981Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45717ca54c3Smrg
458ea1d6981Smrg    # Copy the file name to the temp name.
459ffe57424Smrg    (umask $cp_umask &&
460ffe57424Smrg     { test -z "$stripcmd" || {
461ffe57424Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462ffe57424Smrg	 # which would cause strip to fail.
463ffe57424Smrg	 if test -z "$doit"; then
464ffe57424Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465ffe57424Smrg	 else
466ffe57424Smrg	   $doit touch "$dsttmp"
467ffe57424Smrg	 fi
468ffe57424Smrg       }
469ffe57424Smrg     } &&
470ffe57424Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47117ca54c3Smrg
47217ca54c3Smrg    # and set any options; do chmod last to preserve setuid bits.
47317ca54c3Smrg    #
47417ca54c3Smrg    # If any of these fail, we abort the whole thing.  If we want to
47517ca54c3Smrg    # ignore errors from any of these, just make sure not to ignore
476ea1d6981Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47717ca54c3Smrg    #
478ea1d6981Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479ea1d6981Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480ea1d6981Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481ea1d6981Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482ea1d6981Smrg
483ea1d6981Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484ea1d6981Smrg    if $copy_on_change &&
485ea1d6981Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486ea1d6981Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487ea1d6981Smrg       set -f &&
488ea1d6981Smrg       set X $old && old=:$2:$4:$5:$6 &&
489ea1d6981Smrg       set X $new && new=:$2:$4:$5:$6 &&
490ea1d6981Smrg       set +f &&
491ea1d6981Smrg       test "$old" = "$new" &&
492ea1d6981Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493ea1d6981Smrg    then
494ea1d6981Smrg      rm -f "$dsttmp"
495ea1d6981Smrg    else
496ffe57424Smrg      # If $backupsuffix is set, and the file being installed
497ffe57424Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498ffe57424Smrg      # e.g., if mv doesn't support -f.
499ffe57424Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500ffe57424Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501ffe57424Smrg      fi
502ffe57424Smrg
503ea1d6981Smrg      # Rename the file to the real destination.
504ea1d6981Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505ea1d6981Smrg
506ea1d6981Smrg      # The rename failed, perhaps because mv can't rename something else
507ea1d6981Smrg      # to itself, or perhaps because mv is so ancient that it does not
508ea1d6981Smrg      # support -f.
509ea1d6981Smrg      {
510ea1d6981Smrg        # Now remove or move aside any old file at destination location.
511ea1d6981Smrg        # We try this two ways since rm can't unlink itself on some
512ea1d6981Smrg        # systems and the destination file might be busy for other
513ea1d6981Smrg        # reasons.  In this case, the final cleanup might fail but the new
514ea1d6981Smrg        # file should still install successfully.
515ea1d6981Smrg        {
516ea1d6981Smrg          test ! -f "$dst" ||
517ffe57424Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518ea1d6981Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519ffe57424Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520ea1d6981Smrg          } ||
521ea1d6981Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522ea1d6981Smrg            (exit 1); exit 1
523ea1d6981Smrg          }
524ea1d6981Smrg        } &&
525ea1d6981Smrg
526ea1d6981Smrg        # Now rename the file to the real destination.
527ea1d6981Smrg        $doit $mvcmd "$dsttmp" "$dst"
528ea1d6981Smrg      }
529ea1d6981Smrg    fi || exit 1
530ea1d6981Smrg
531ea1d6981Smrg    trap '' 0
532ea1d6981Smrg  fi
53317ca54c3Smrgdone
53417ca54c3Smrg
53517ca54c3Smrg# Local variables:
536ea1d6981Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
53717ca54c3Smrg# time-stamp-start: "scriptversion="
53817ca54c3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539ea1d6981Smrg# time-stamp-time-zone: "UTC0"
540ea1d6981Smrg# time-stamp-end: "; # UTC"
54117ca54c3Smrg# End:
542