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