1f33d31a3Smrg#!/bin/sh
2f33d31a3Smrg# install - install a program, script, or datafile
3f33d31a3Smrg
48b5ec993Smrgscriptversion=2024-06-19.01; # UTC
5f33d31a3Smrg
6f33d31a3Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7f33d31a3Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8f33d31a3Smrg# following copyright and license.
9f33d31a3Smrg#
10f33d31a3Smrg# Copyright (C) 1994 X Consortium
11f33d31a3Smrg#
12f33d31a3Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13f33d31a3Smrg# of this software and associated documentation files (the "Software"), to
14f33d31a3Smrg# deal in the Software without restriction, including without limitation the
15f33d31a3Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16f33d31a3Smrg# sell copies of the Software, and to permit persons to whom the Software is
17f33d31a3Smrg# furnished to do so, subject to the following conditions:
18f33d31a3Smrg#
19f33d31a3Smrg# The above copyright notice and this permission notice shall be included in
20f33d31a3Smrg# all copies or substantial portions of the Software.
21f33d31a3Smrg#
22f33d31a3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23f33d31a3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24f33d31a3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25f33d31a3Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26f33d31a3Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27f33d31a3Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28f33d31a3Smrg#
29f33d31a3Smrg# Except as contained in this notice, the name of the X Consortium shall not
30f33d31a3Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31f33d31a3Smrg# ings in this Software without prior written authorization from the X Consor-
32f33d31a3Smrg# tium.
33f33d31a3Smrg#
34f33d31a3Smrg#
35f33d31a3Smrg# FSF changes to this file are in the public domain.
36f33d31a3Smrg#
37f33d31a3Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38f33d31a3Smrg# 'make' implicit rules from creating a file called install from it
39f33d31a3Smrg# when there is no Makefile.
40f33d31a3Smrg#
41f33d31a3Smrg# This script is compatible with the BSD install script, but was written
42f33d31a3Smrg# from scratch.
43f33d31a3Smrg
44f33d31a3Smrgtab='	'
45f33d31a3Smrgnl='
46f33d31a3Smrg'
47f33d31a3SmrgIFS=" $tab$nl"
48f33d31a3Smrg
49f33d31a3Smrg# Set DOITPROG to "echo" to test this script.
50f33d31a3Smrg
51f33d31a3Smrgdoit=${DOITPROG-}
52f33d31a3Smrgdoit_exec=${doit:-exec}
53f33d31a3Smrg
54f33d31a3Smrg# Put in absolute file names if you don't have them in your path;
55f33d31a3Smrg# or use environment vars.
56f33d31a3Smrg
57f33d31a3Smrgchgrpprog=${CHGRPPROG-chgrp}
58f33d31a3Smrgchmodprog=${CHMODPROG-chmod}
59f33d31a3Smrgchownprog=${CHOWNPROG-chown}
60f33d31a3Smrgcmpprog=${CMPPROG-cmp}
61f33d31a3Smrgcpprog=${CPPROG-cp}
62f33d31a3Smrgmkdirprog=${MKDIRPROG-mkdir}
63f33d31a3Smrgmvprog=${MVPROG-mv}
64f33d31a3Smrgrmprog=${RMPROG-rm}
65f33d31a3Smrgstripprog=${STRIPPROG-strip}
66f33d31a3Smrg
67f33d31a3Smrgposix_mkdir=
68f33d31a3Smrg
69f33d31a3Smrg# Desired mode of installed file.
70f33d31a3Smrgmode=0755
71f33d31a3Smrg
729f1d9476Smrg# Create dirs (including intermediate dirs) using mode 755.
739f1d9476Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
749f1d9476Smrgmkdir_umask=22
759f1d9476Smrg
769f1d9476Smrgbackupsuffix=
77f33d31a3Smrgchgrpcmd=
78f33d31a3Smrgchmodcmd=$chmodprog
79f33d31a3Smrgchowncmd=
80f33d31a3Smrgmvcmd=$mvprog
81f33d31a3Smrgrmcmd="$rmprog -f"
82f33d31a3Smrgstripcmd=
83f33d31a3Smrg
84f33d31a3Smrgsrc=
85f33d31a3Smrgdst=
86f33d31a3Smrgdir_arg=
87f33d31a3Smrgdst_arg=
88f33d31a3Smrg
89f33d31a3Smrgcopy_on_change=false
90f33d31a3Smrgis_target_a_directory=possibly
91f33d31a3Smrg
92f33d31a3Smrgusage="\
93f33d31a3SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94f33d31a3Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95f33d31a3Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96f33d31a3Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97f33d31a3Smrg
98f33d31a3SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99f33d31a3SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100f33d31a3SmrgIn the 4th, create DIRECTORIES.
101f33d31a3Smrg
102f33d31a3SmrgOptions:
103f33d31a3Smrg     --help     display this help and exit.
104f33d31a3Smrg     --version  display version info and exit.
105f33d31a3Smrg
106f33d31a3Smrg  -c            (ignored)
1079f1d9476Smrg  -C            install only if different (preserve data modification time)
108f33d31a3Smrg  -d            create directories instead of installing files.
109f33d31a3Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110f33d31a3Smrg  -m MODE       $chmodprog installed files to MODE.
111f33d31a3Smrg  -o USER       $chownprog installed files to USER.
1129f1d9476Smrg  -p            pass -p to $cpprog.
113f33d31a3Smrg  -s            $stripprog installed files.
1149f1d9476Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115f33d31a3Smrg  -t DIRECTORY  install into DIRECTORY.
116f33d31a3Smrg  -T            report an error if DSTFILE is a directory.
117f33d31a3Smrg
118f33d31a3SmrgEnvironment variables override the default commands:
119f33d31a3Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120f33d31a3Smrg  RMPROG STRIPPROG
1219f1d9476Smrg
1229f1d9476SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1239f1d9476Smrgit's up to you to specify -f if you want it.
1249f1d9476Smrg
1259f1d9476SmrgIf -S is not specified, no backups are attempted.
1269f1d9476Smrg
1278b5ec993SmrgReport bugs to <bug-automake@gnu.org>.
1288b5ec993SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
1298b5ec993SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>."
130f33d31a3Smrg
131f33d31a3Smrgwhile test $# -ne 0; do
132f33d31a3Smrg  case $1 in
133f33d31a3Smrg    -c) ;;
134f33d31a3Smrg
135f33d31a3Smrg    -C) copy_on_change=true;;
136f33d31a3Smrg
137f33d31a3Smrg    -d) dir_arg=true;;
138f33d31a3Smrg
139f33d31a3Smrg    -g) chgrpcmd="$chgrpprog $2"
140f33d31a3Smrg        shift;;
141f33d31a3Smrg
142f33d31a3Smrg    --help) echo "$usage"; exit $?;;
143f33d31a3Smrg
144f33d31a3Smrg    -m) mode=$2
145f33d31a3Smrg        case $mode in
146f33d31a3Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147f33d31a3Smrg            echo "$0: invalid mode: $mode" >&2
148f33d31a3Smrg            exit 1;;
149f33d31a3Smrg        esac
150f33d31a3Smrg        shift;;
151f33d31a3Smrg
152f33d31a3Smrg    -o) chowncmd="$chownprog $2"
153f33d31a3Smrg        shift;;
154f33d31a3Smrg
1559f1d9476Smrg    -p) cpprog="$cpprog -p";;
1569f1d9476Smrg
157f33d31a3Smrg    -s) stripcmd=$stripprog;;
158f33d31a3Smrg
1599f1d9476Smrg    -S) backupsuffix="$2"
1609f1d9476Smrg        shift;;
1619f1d9476Smrg
162f33d31a3Smrg    -t)
163f33d31a3Smrg        is_target_a_directory=always
164f33d31a3Smrg        dst_arg=$2
165f33d31a3Smrg        # Protect names problematic for 'test' and other utilities.
166f33d31a3Smrg        case $dst_arg in
167f33d31a3Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168f33d31a3Smrg        esac
169f33d31a3Smrg        shift;;
170f33d31a3Smrg
171f33d31a3Smrg    -T) is_target_a_directory=never;;
172f33d31a3Smrg
1738b5ec993Smrg    --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;;
174f33d31a3Smrg
175f33d31a3Smrg    --) shift
176f33d31a3Smrg        break;;
177f33d31a3Smrg
178f33d31a3Smrg    -*) echo "$0: invalid option: $1" >&2
179f33d31a3Smrg        exit 1;;
180f33d31a3Smrg
181f33d31a3Smrg    *)  break;;
182f33d31a3Smrg  esac
183f33d31a3Smrg  shift
184f33d31a3Smrgdone
185f33d31a3Smrg
186f33d31a3Smrg# We allow the use of options -d and -T together, by making -d
187f33d31a3Smrg# take the precedence; this is for compatibility with GNU install.
188f33d31a3Smrg
189f33d31a3Smrgif test -n "$dir_arg"; then
190f33d31a3Smrg  if test -n "$dst_arg"; then
191f33d31a3Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192f33d31a3Smrg    exit 1
193f33d31a3Smrg  fi
194f33d31a3Smrgfi
195f33d31a3Smrg
196f33d31a3Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197f33d31a3Smrg  # When -d is used, all remaining arguments are directories to create.
198f33d31a3Smrg  # When -t is used, the destination is already specified.
199f33d31a3Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200f33d31a3Smrg  for arg
201f33d31a3Smrg  do
202f33d31a3Smrg    if test -n "$dst_arg"; then
203f33d31a3Smrg      # $@ is not empty: it contains at least $arg.
204f33d31a3Smrg      set fnord "$@" "$dst_arg"
205f33d31a3Smrg      shift # fnord
206f33d31a3Smrg    fi
207f33d31a3Smrg    shift # arg
208f33d31a3Smrg    dst_arg=$arg
209f33d31a3Smrg    # Protect names problematic for 'test' and other utilities.
210f33d31a3Smrg    case $dst_arg in
211f33d31a3Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212f33d31a3Smrg    esac
213f33d31a3Smrg  done
214f33d31a3Smrgfi
215f33d31a3Smrg
216f33d31a3Smrgif test $# -eq 0; then
217f33d31a3Smrg  if test -z "$dir_arg"; then
218f33d31a3Smrg    echo "$0: no input file specified." >&2
219f33d31a3Smrg    exit 1
220f33d31a3Smrg  fi
221f33d31a3Smrg  # It's OK to call 'install-sh -d' without argument.
222f33d31a3Smrg  # This can happen when creating conditional directories.
223f33d31a3Smrg  exit 0
224f33d31a3Smrgfi
225f33d31a3Smrg
226f33d31a3Smrgif test -z "$dir_arg"; then
227f33d31a3Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228f33d31a3Smrg    if test ! -d "$dst_arg"; then
229f33d31a3Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230f33d31a3Smrg      exit 1
231f33d31a3Smrg    fi
232f33d31a3Smrg  fi
233f33d31a3Smrgfi
234f33d31a3Smrg
235f33d31a3Smrgif test -z "$dir_arg"; then
236f33d31a3Smrg  do_exit='(exit $ret); exit $ret'
237f33d31a3Smrg  trap "ret=129; $do_exit" 1
238f33d31a3Smrg  trap "ret=130; $do_exit" 2
239f33d31a3Smrg  trap "ret=141; $do_exit" 13
240f33d31a3Smrg  trap "ret=143; $do_exit" 15
241f33d31a3Smrg
242f33d31a3Smrg  # Set umask so as not to create temps with too-generous modes.
243f33d31a3Smrg  # However, 'strip' requires both read and write access to temps.
244f33d31a3Smrg  case $mode in
245f33d31a3Smrg    # Optimize common cases.
246f33d31a3Smrg    *644) cp_umask=133;;
247f33d31a3Smrg    *755) cp_umask=22;;
248f33d31a3Smrg
249f33d31a3Smrg    *[0-7])
250f33d31a3Smrg      if test -z "$stripcmd"; then
251f33d31a3Smrg        u_plus_rw=
252f33d31a3Smrg      else
253f33d31a3Smrg        u_plus_rw='% 200'
254f33d31a3Smrg      fi
255f33d31a3Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256f33d31a3Smrg    *)
257f33d31a3Smrg      if test -z "$stripcmd"; then
258f33d31a3Smrg        u_plus_rw=
259f33d31a3Smrg      else
260f33d31a3Smrg        u_plus_rw=,u+rw
261f33d31a3Smrg      fi
262f33d31a3Smrg      cp_umask=$mode$u_plus_rw;;
263f33d31a3Smrg  esac
264f33d31a3Smrgfi
265f33d31a3Smrg
266f33d31a3Smrgfor src
267f33d31a3Smrgdo
268f33d31a3Smrg  # Protect names problematic for 'test' and other utilities.
269f33d31a3Smrg  case $src in
270f33d31a3Smrg    -* | [=\(\)!]) src=./$src;;
271f33d31a3Smrg  esac
272f33d31a3Smrg
273f33d31a3Smrg  if test -n "$dir_arg"; then
274f33d31a3Smrg    dst=$src
275f33d31a3Smrg    dstdir=$dst
276f33d31a3Smrg    test -d "$dstdir"
277f33d31a3Smrg    dstdir_status=$?
2789f1d9476Smrg    # Don't chown directories that already exist.
2799f1d9476Smrg    if test $dstdir_status = 0; then
2809f1d9476Smrg      chowncmd=""
2819f1d9476Smrg    fi
282f33d31a3Smrg  else
283f33d31a3Smrg
284f33d31a3Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285f33d31a3Smrg    # might cause directories to be created, which would be especially bad
286f33d31a3Smrg    # if $src (and thus $dsttmp) contains '*'.
287f33d31a3Smrg    if test ! -f "$src" && test ! -d "$src"; then
288f33d31a3Smrg      echo "$0: $src does not exist." >&2
289f33d31a3Smrg      exit 1
290f33d31a3Smrg    fi
291f33d31a3Smrg
292f33d31a3Smrg    if test -z "$dst_arg"; then
293f33d31a3Smrg      echo "$0: no destination specified." >&2
294f33d31a3Smrg      exit 1
295f33d31a3Smrg    fi
296f33d31a3Smrg    dst=$dst_arg
297f33d31a3Smrg
2989f1d9476Smrg    # If destination is a directory, append the input filename.
299f33d31a3Smrg    if test -d "$dst"; then
300f33d31a3Smrg      if test "$is_target_a_directory" = never; then
301f33d31a3Smrg        echo "$0: $dst_arg: Is a directory" >&2
302f33d31a3Smrg        exit 1
303f33d31a3Smrg      fi
304f33d31a3Smrg      dstdir=$dst
3059f1d9476Smrg      dstbase=`basename "$src"`
3069f1d9476Smrg      case $dst in
3079f1d9476Smrg	*/) dst=$dst$dstbase;;
3089f1d9476Smrg	*)  dst=$dst/$dstbase;;
3099f1d9476Smrg      esac
310f33d31a3Smrg      dstdir_status=0
311f33d31a3Smrg    else
312f33d31a3Smrg      dstdir=`dirname "$dst"`
313f33d31a3Smrg      test -d "$dstdir"
314f33d31a3Smrg      dstdir_status=$?
315f33d31a3Smrg    fi
316f33d31a3Smrg  fi
317f33d31a3Smrg
3189f1d9476Smrg  case $dstdir in
3199f1d9476Smrg    */) dstdirslash=$dstdir;;
3209f1d9476Smrg    *)  dstdirslash=$dstdir/;;
3219f1d9476Smrg  esac
3229f1d9476Smrg
323f33d31a3Smrg  obsolete_mkdir_used=false
324f33d31a3Smrg
325f33d31a3Smrg  if test $dstdir_status != 0; then
326f33d31a3Smrg    case $posix_mkdir in
327f33d31a3Smrg      '')
328f33d31a3Smrg        # With -d, create the new directory with the user-specified mode.
329f33d31a3Smrg        # Otherwise, rely on $mkdir_umask.
330f33d31a3Smrg        if test -n "$dir_arg"; then
331f33d31a3Smrg          mkdir_mode=-m$mode
332f33d31a3Smrg        else
333f33d31a3Smrg          mkdir_mode=
334f33d31a3Smrg        fi
335f33d31a3Smrg
336f33d31a3Smrg        posix_mkdir=false
3379f1d9476Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3389f1d9476Smrg	# here however when possible just to lower collision chance.
3399f1d9476Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3409f1d9476Smrg
3419f1d9476Smrg	trap '
3429f1d9476Smrg	  ret=$?
3439f1d9476Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3449f1d9476Smrg	  exit $ret
3459f1d9476Smrg	' 0
3469f1d9476Smrg
3479f1d9476Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3488b5ec993Smrg	# directly in world-writable /tmp, make sure that the '$tmpdir'
3499f1d9476Smrg	# directory is successfully created first before we actually test
3509f1d9476Smrg	# 'mkdir -p'.
3519f1d9476Smrg	if (umask $mkdir_umask &&
3529f1d9476Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3539f1d9476Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3549f1d9476Smrg	then
3559f1d9476Smrg	  if test -z "$dir_arg" || {
3568b5ec993Smrg	       # Check for POSIX incompatibility with -m.
3579f1d9476Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3589f1d9476Smrg	       # other-writable bit of parent directory when it shouldn't.
3599f1d9476Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3609f1d9476Smrg	       test_tmpdir="$tmpdir/a"
3619f1d9476Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3629f1d9476Smrg	       case $ls_ld_tmpdir in
3639f1d9476Smrg		 d????-?r-*) different_mode=700;;
3649f1d9476Smrg		 d????-?--*) different_mode=755;;
3659f1d9476Smrg		 *) false;;
3669f1d9476Smrg	       esac &&
3679f1d9476Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3689f1d9476Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3699f1d9476Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3709f1d9476Smrg	       }
3719f1d9476Smrg	     }
3729f1d9476Smrg	  then posix_mkdir=:
3739f1d9476Smrg	  fi
3749f1d9476Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3759f1d9476Smrg	else
3769f1d9476Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
3779f1d9476Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3789f1d9476Smrg	fi
3799f1d9476Smrg	trap '' 0;;
380f33d31a3Smrg    esac
381f33d31a3Smrg
382f33d31a3Smrg    if
383f33d31a3Smrg      $posix_mkdir && (
384f33d31a3Smrg        umask $mkdir_umask &&
385f33d31a3Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386f33d31a3Smrg      )
387f33d31a3Smrg    then :
388f33d31a3Smrg    else
389f33d31a3Smrg
3909f1d9476Smrg      # mkdir does not conform to POSIX,
391f33d31a3Smrg      # or it failed possibly due to a race condition.  Create the
392f33d31a3Smrg      # directory the slow way, step by step, checking for races as we go.
393f33d31a3Smrg
394f33d31a3Smrg      case $dstdir in
395f33d31a3Smrg        /*) prefix='/';;
396f33d31a3Smrg        [-=\(\)!]*) prefix='./';;
397f33d31a3Smrg        *)  prefix='';;
398f33d31a3Smrg      esac
399f33d31a3Smrg
400f33d31a3Smrg      oIFS=$IFS
401f33d31a3Smrg      IFS=/
402f33d31a3Smrg      set -f
403f33d31a3Smrg      set fnord $dstdir
404f33d31a3Smrg      shift
405f33d31a3Smrg      set +f
406f33d31a3Smrg      IFS=$oIFS
407f33d31a3Smrg
408f33d31a3Smrg      prefixes=
409f33d31a3Smrg
410f33d31a3Smrg      for d
411f33d31a3Smrg      do
412f33d31a3Smrg        test X"$d" = X && continue
413f33d31a3Smrg
414f33d31a3Smrg        prefix=$prefix$d
415f33d31a3Smrg        if test -d "$prefix"; then
416f33d31a3Smrg          prefixes=
417f33d31a3Smrg        else
418f33d31a3Smrg          if $posix_mkdir; then
4199f1d9476Smrg            (umask $mkdir_umask &&
420f33d31a3Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421f33d31a3Smrg            # Don't fail if two instances are running concurrently.
422f33d31a3Smrg            test -d "$prefix" || exit 1
423f33d31a3Smrg          else
424f33d31a3Smrg            case $prefix in
425f33d31a3Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426f33d31a3Smrg              *) qprefix=$prefix;;
427f33d31a3Smrg            esac
428f33d31a3Smrg            prefixes="$prefixes '$qprefix'"
429f33d31a3Smrg          fi
430f33d31a3Smrg        fi
431f33d31a3Smrg        prefix=$prefix/
432f33d31a3Smrg      done
433f33d31a3Smrg
434f33d31a3Smrg      if test -n "$prefixes"; then
435f33d31a3Smrg        # Don't fail if two instances are running concurrently.
436f33d31a3Smrg        (umask $mkdir_umask &&
437f33d31a3Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438f33d31a3Smrg          test -d "$dstdir" || exit 1
439f33d31a3Smrg        obsolete_mkdir_used=true
440f33d31a3Smrg      fi
441f33d31a3Smrg    fi
442f33d31a3Smrg  fi
443f33d31a3Smrg
444f33d31a3Smrg  if test -n "$dir_arg"; then
445f33d31a3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446f33d31a3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447f33d31a3Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448f33d31a3Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449f33d31a3Smrg  else
450f33d31a3Smrg
451f33d31a3Smrg    # Make a couple of temp file names in the proper directory.
4529f1d9476Smrg    dsttmp=${dstdirslash}_inst.$$_
4539f1d9476Smrg    rmtmp=${dstdirslash}_rm.$$_
454f33d31a3Smrg
455f33d31a3Smrg    # Trap to clean up those temp files at exit.
456f33d31a3Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457f33d31a3Smrg
458f33d31a3Smrg    # Copy the file name to the temp name.
4599f1d9476Smrg    (umask $cp_umask &&
4609f1d9476Smrg     { test -z "$stripcmd" || {
4619f1d9476Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4629f1d9476Smrg	 # which would cause strip to fail.
4639f1d9476Smrg	 if test -z "$doit"; then
4649f1d9476Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4659f1d9476Smrg	 else
4669f1d9476Smrg	   $doit touch "$dsttmp"
4679f1d9476Smrg	 fi
4689f1d9476Smrg       }
4699f1d9476Smrg     } &&
4709f1d9476Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471f33d31a3Smrg
472f33d31a3Smrg    # and set any options; do chmod last to preserve setuid bits.
473f33d31a3Smrg    #
474f33d31a3Smrg    # If any of these fail, we abort the whole thing.  If we want to
475f33d31a3Smrg    # ignore errors from any of these, just make sure not to ignore
476f33d31a3Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477f33d31a3Smrg    #
478f33d31a3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479f33d31a3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480f33d31a3Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481f33d31a3Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482f33d31a3Smrg
483f33d31a3Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484f33d31a3Smrg    if $copy_on_change &&
485f33d31a3Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486f33d31a3Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487f33d31a3Smrg       set -f &&
488f33d31a3Smrg       set X $old && old=:$2:$4:$5:$6 &&
489f33d31a3Smrg       set X $new && new=:$2:$4:$5:$6 &&
490f33d31a3Smrg       set +f &&
491f33d31a3Smrg       test "$old" = "$new" &&
492f33d31a3Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493f33d31a3Smrg    then
494f33d31a3Smrg      rm -f "$dsttmp"
495f33d31a3Smrg    else
4969f1d9476Smrg      # If $backupsuffix is set, and the file being installed
4979f1d9476Smrg      # already exists, attempt a backup.  Don't worry if it fails,
4989f1d9476Smrg      # e.g., if mv doesn't support -f.
4999f1d9476Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
5009f1d9476Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5019f1d9476Smrg      fi
5029f1d9476Smrg
503f33d31a3Smrg      # Rename the file to the real destination.
504f33d31a3Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505f33d31a3Smrg
506f33d31a3Smrg      # The rename failed, perhaps because mv can't rename something else
507f33d31a3Smrg      # to itself, or perhaps because mv is so ancient that it does not
508f33d31a3Smrg      # support -f.
509f33d31a3Smrg      {
510f33d31a3Smrg        # Now remove or move aside any old file at destination location.
511f33d31a3Smrg        # We try this two ways since rm can't unlink itself on some
512f33d31a3Smrg        # systems and the destination file might be busy for other
513f33d31a3Smrg        # reasons.  In this case, the final cleanup might fail but the new
514f33d31a3Smrg        # file should still install successfully.
515f33d31a3Smrg        {
516f33d31a3Smrg          test ! -f "$dst" ||
5179f1d9476Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518f33d31a3Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5199f1d9476Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520f33d31a3Smrg          } ||
521f33d31a3Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522f33d31a3Smrg            (exit 1); exit 1
523f33d31a3Smrg          }
524f33d31a3Smrg        } &&
525f33d31a3Smrg
526f33d31a3Smrg        # Now rename the file to the real destination.
527f33d31a3Smrg        $doit $mvcmd "$dsttmp" "$dst"
528f33d31a3Smrg      }
529f33d31a3Smrg    fi || exit 1
530f33d31a3Smrg
531f33d31a3Smrg    trap '' 0
532f33d31a3Smrg  fi
533f33d31a3Smrgdone
534f33d31a3Smrg
535f33d31a3Smrg# Local variables:
5369f1d9476Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537f33d31a3Smrg# time-stamp-start: "scriptversion="
538f33d31a3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5399f1d9476Smrg# time-stamp-time-zone: "UTC0"
540f33d31a3Smrg# time-stamp-end: "; # UTC"
541f33d31a3Smrg# End:
542