xref: /openbsd-src/usr.sbin/unbound/install-sh (revision bdcafcd9f69778e6b55155acb0eaf7ccd2048781)
1eaf2578eSsthen#!/usr/bin/sh
2933707f3Ssthen# install - install a program, script, or datafile
3933707f3Ssthen
4*bdcafcd9Ssthenscriptversion=2020-11-14.01; # UTC
5933707f3Ssthen
6933707f3Ssthen# This originates from X11R5 (mit/util/scripts/install.sh), which was
7933707f3Ssthen# later released in X11R6 (xc/config/util/install.sh) with the
8933707f3Ssthen# following copyright and license.
9933707f3Ssthen#
10933707f3Ssthen# Copyright (C) 1994 X Consortium
11933707f3Ssthen#
12933707f3Ssthen# Permission is hereby granted, free of charge, to any person obtaining a copy
13933707f3Ssthen# of this software and associated documentation files (the "Software"), to
14933707f3Ssthen# deal in the Software without restriction, including without limitation the
15933707f3Ssthen# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16933707f3Ssthen# sell copies of the Software, and to permit persons to whom the Software is
17933707f3Ssthen# furnished to do so, subject to the following conditions:
18933707f3Ssthen#
19933707f3Ssthen# The above copyright notice and this permission notice shall be included in
20933707f3Ssthen# all copies or substantial portions of the Software.
21933707f3Ssthen#
22933707f3Ssthen# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23933707f3Ssthen# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24933707f3Ssthen# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25933707f3Ssthen# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26933707f3Ssthen# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27933707f3Ssthen# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28933707f3Ssthen#
29933707f3Ssthen# Except as contained in this notice, the name of the X Consortium shall not
30933707f3Ssthen# be used in advertising or otherwise to promote the sale, use or other deal-
31933707f3Ssthen# ings in this Software without prior written authorization from the X Consor-
32933707f3Ssthen# tium.
33933707f3Ssthen#
34933707f3Ssthen#
35933707f3Ssthen# FSF changes to this file are in the public domain.
36933707f3Ssthen#
37933707f3Ssthen# Calling this script install-sh is preferred over install.sh, to prevent
38229e174cSsthen# 'make' implicit rules from creating a file called install from it
39933707f3Ssthen# when there is no Makefile.
40933707f3Ssthen#
41933707f3Ssthen# This script is compatible with the BSD install script, but was written
42933707f3Ssthen# from scratch.
43933707f3Ssthen
4432e31f52Ssthentab='	'
45933707f3Ssthennl='
46933707f3Ssthen'
4732e31f52SsthenIFS=" $tab$nl"
48933707f3Ssthen
4932e31f52Ssthen# Set DOITPROG to "echo" to test this script.
50933707f3Ssthen
51933707f3Ssthendoit=${DOITPROG-}
5232e31f52Ssthendoit_exec=${doit:-exec}
53933707f3Ssthen
54933707f3Ssthen# Put in absolute file names if you don't have them in your path;
55933707f3Ssthen# or use environment vars.
56933707f3Ssthen
57933707f3Ssthenchgrpprog=${CHGRPPROG-chgrp}
58933707f3Ssthenchmodprog=${CHMODPROG-chmod}
59933707f3Ssthenchownprog=${CHOWNPROG-chown}
60933707f3Ssthencmpprog=${CMPPROG-cmp}
61933707f3Ssthencpprog=${CPPROG-cp}
62933707f3Ssthenmkdirprog=${MKDIRPROG-mkdir}
63933707f3Ssthenmvprog=${MVPROG-mv}
64933707f3Ssthenrmprog=${RMPROG-rm}
65933707f3Ssthenstripprog=${STRIPPROG-strip}
66933707f3Ssthen
67933707f3Ssthenposix_mkdir=
68933707f3Ssthen
69933707f3Ssthen# Desired mode of installed file.
70933707f3Ssthenmode=0755
71933707f3Ssthen
72*bdcafcd9Ssthen# Create dirs (including intermediate dirs) using mode 755.
73*bdcafcd9Ssthen# This is like GNU 'install' as of coreutils 8.32 (2020).
74*bdcafcd9Ssthenmkdir_umask=22
75*bdcafcd9Ssthen
76*bdcafcd9Ssthenbackupsuffix=
77933707f3Ssthenchgrpcmd=
78933707f3Ssthenchmodcmd=$chmodprog
79933707f3Ssthenchowncmd=
80933707f3Ssthenmvcmd=$mvprog
81933707f3Ssthenrmcmd="$rmprog -f"
82933707f3Ssthenstripcmd=
83933707f3Ssthen
84933707f3Ssthensrc=
85933707f3Ssthendst=
86933707f3Ssthendir_arg=
87933707f3Ssthendst_arg=
88933707f3Ssthen
89933707f3Ssthencopy_on_change=false
9032e31f52Ssthenis_target_a_directory=possibly
91933707f3Ssthen
92933707f3Ssthenusage="\
93933707f3SsthenUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94933707f3Ssthen   or: $0 [OPTION]... SRCFILES... DIRECTORY
95933707f3Ssthen   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96933707f3Ssthen   or: $0 [OPTION]... -d DIRECTORIES...
97933707f3Ssthen
98933707f3SsthenIn the 1st form, copy SRCFILE to DSTFILE.
99933707f3SsthenIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100933707f3SsthenIn the 4th, create DIRECTORIES.
101933707f3Ssthen
102933707f3SsthenOptions:
103933707f3Ssthen     --help     display this help and exit.
104933707f3Ssthen     --version  display version info and exit.
105933707f3Ssthen
106933707f3Ssthen  -c            (ignored)
107*bdcafcd9Ssthen  -C            install only if different (preserve data modification time)
108933707f3Ssthen  -d            create directories instead of installing files.
109933707f3Ssthen  -g GROUP      $chgrpprog installed files to GROUP.
110933707f3Ssthen  -m MODE       $chmodprog installed files to MODE.
111933707f3Ssthen  -o USER       $chownprog installed files to USER.
112*bdcafcd9Ssthen  -p            pass -p to $cpprog.
113933707f3Ssthen  -s            $stripprog installed files.
114*bdcafcd9Ssthen  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115933707f3Ssthen  -t DIRECTORY  install into DIRECTORY.
116933707f3Ssthen  -T            report an error if DSTFILE is a directory.
117933707f3Ssthen
118933707f3SsthenEnvironment variables override the default commands:
119933707f3Ssthen  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120933707f3Ssthen  RMPROG STRIPPROG
121*bdcafcd9Ssthen
122*bdcafcd9SsthenBy default, rm is invoked with -f; when overridden with RMPROG,
123*bdcafcd9Ssthenit's up to you to specify -f if you want it.
124*bdcafcd9Ssthen
125*bdcafcd9SsthenIf -S is not specified, no backups are attempted.
126*bdcafcd9Ssthen
127*bdcafcd9SsthenEmail bug reports to bug-automake@gnu.org.
128*bdcafcd9SsthenAutomake home page: https://www.gnu.org/software/automake/
129933707f3Ssthen"
130933707f3Ssthen
131933707f3Ssthenwhile test $# -ne 0; do
132933707f3Ssthen  case $1 in
133933707f3Ssthen    -c) ;;
134933707f3Ssthen
135933707f3Ssthen    -C) copy_on_change=true;;
136933707f3Ssthen
137933707f3Ssthen    -d) dir_arg=true;;
138933707f3Ssthen
139933707f3Ssthen    -g) chgrpcmd="$chgrpprog $2"
140933707f3Ssthen        shift;;
141933707f3Ssthen
142933707f3Ssthen    --help) echo "$usage"; exit $?;;
143933707f3Ssthen
144933707f3Ssthen    -m) mode=$2
145933707f3Ssthen        case $mode in
14632e31f52Ssthen          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147933707f3Ssthen            echo "$0: invalid mode: $mode" >&2
148933707f3Ssthen            exit 1;;
149933707f3Ssthen        esac
150933707f3Ssthen        shift;;
151933707f3Ssthen
152933707f3Ssthen    -o) chowncmd="$chownprog $2"
153933707f3Ssthen        shift;;
154933707f3Ssthen
155*bdcafcd9Ssthen    -p) cpprog="$cpprog -p";;
156*bdcafcd9Ssthen
157933707f3Ssthen    -s) stripcmd=$stripprog;;
158933707f3Ssthen
159*bdcafcd9Ssthen    -S) backupsuffix="$2"
160*bdcafcd9Ssthen        shift;;
161*bdcafcd9Ssthen
16232e31f52Ssthen    -t)
16332e31f52Ssthen        is_target_a_directory=always
16432e31f52Ssthen        dst_arg=$2
165229e174cSsthen        # Protect names problematic for 'test' and other utilities.
166229e174cSsthen        case $dst_arg in
167229e174cSsthen          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168229e174cSsthen        esac
169933707f3Ssthen        shift;;
170933707f3Ssthen
17132e31f52Ssthen    -T) is_target_a_directory=never;;
172933707f3Ssthen
173933707f3Ssthen    --version) echo "$0 $scriptversion"; exit $?;;
174933707f3Ssthen
175933707f3Ssthen    --) shift
176933707f3Ssthen        break;;
177933707f3Ssthen
178933707f3Ssthen    -*) echo "$0: invalid option: $1" >&2
179933707f3Ssthen        exit 1;;
180933707f3Ssthen
181933707f3Ssthen    *)  break;;
182933707f3Ssthen  esac
183933707f3Ssthen  shift
184933707f3Ssthendone
185933707f3Ssthen
18632e31f52Ssthen# We allow the use of options -d and -T together, by making -d
18732e31f52Ssthen# take the precedence; this is for compatibility with GNU install.
18832e31f52Ssthen
18932e31f52Ssthenif test -n "$dir_arg"; then
19032e31f52Ssthen  if test -n "$dst_arg"; then
19132e31f52Ssthen    echo "$0: target directory not allowed when installing a directory." >&2
19232e31f52Ssthen    exit 1
19332e31f52Ssthen  fi
19432e31f52Ssthenfi
19532e31f52Ssthen
196933707f3Ssthenif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197933707f3Ssthen  # When -d is used, all remaining arguments are directories to create.
198933707f3Ssthen  # When -t is used, the destination is already specified.
199933707f3Ssthen  # Otherwise, the last argument is the destination.  Remove it from $@.
200933707f3Ssthen  for arg
201933707f3Ssthen  do
202933707f3Ssthen    if test -n "$dst_arg"; then
203933707f3Ssthen      # $@ is not empty: it contains at least $arg.
204933707f3Ssthen      set fnord "$@" "$dst_arg"
205933707f3Ssthen      shift # fnord
206933707f3Ssthen    fi
207933707f3Ssthen    shift # arg
208933707f3Ssthen    dst_arg=$arg
209229e174cSsthen    # Protect names problematic for 'test' and other utilities.
210229e174cSsthen    case $dst_arg in
211229e174cSsthen      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212229e174cSsthen    esac
213933707f3Ssthen  done
214933707f3Ssthenfi
215933707f3Ssthen
216933707f3Ssthenif test $# -eq 0; then
217933707f3Ssthen  if test -z "$dir_arg"; then
218933707f3Ssthen    echo "$0: no input file specified." >&2
219933707f3Ssthen    exit 1
220933707f3Ssthen  fi
221229e174cSsthen  # It's OK to call 'install-sh -d' without argument.
222933707f3Ssthen  # This can happen when creating conditional directories.
223933707f3Ssthen  exit 0
224933707f3Ssthenfi
225933707f3Ssthen
226933707f3Ssthenif test -z "$dir_arg"; then
22732e31f52Ssthen  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22832e31f52Ssthen    if test ! -d "$dst_arg"; then
22932e31f52Ssthen      echo "$0: $dst_arg: Is not a directory." >&2
23032e31f52Ssthen      exit 1
23132e31f52Ssthen    fi
23232e31f52Ssthen  fi
23332e31f52Ssthenfi
23432e31f52Ssthen
23532e31f52Ssthenif test -z "$dir_arg"; then
236229e174cSsthen  do_exit='(exit $ret); exit $ret'
237229e174cSsthen  trap "ret=129; $do_exit" 1
238229e174cSsthen  trap "ret=130; $do_exit" 2
239229e174cSsthen  trap "ret=141; $do_exit" 13
240229e174cSsthen  trap "ret=143; $do_exit" 15
241933707f3Ssthen
242933707f3Ssthen  # Set umask so as not to create temps with too-generous modes.
243933707f3Ssthen  # However, 'strip' requires both read and write access to temps.
244933707f3Ssthen  case $mode in
245933707f3Ssthen    # Optimize common cases.
246933707f3Ssthen    *644) cp_umask=133;;
247933707f3Ssthen    *755) cp_umask=22;;
248933707f3Ssthen
249933707f3Ssthen    *[0-7])
250933707f3Ssthen      if test -z "$stripcmd"; then
251933707f3Ssthen        u_plus_rw=
252933707f3Ssthen      else
253933707f3Ssthen        u_plus_rw='% 200'
254933707f3Ssthen      fi
255933707f3Ssthen      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256933707f3Ssthen    *)
257933707f3Ssthen      if test -z "$stripcmd"; then
258933707f3Ssthen        u_plus_rw=
259933707f3Ssthen      else
260933707f3Ssthen        u_plus_rw=,u+rw
261933707f3Ssthen      fi
262933707f3Ssthen      cp_umask=$mode$u_plus_rw;;
263933707f3Ssthen  esac
264933707f3Ssthenfi
265933707f3Ssthen
266933707f3Ssthenfor src
267933707f3Ssthendo
268229e174cSsthen  # Protect names problematic for 'test' and other utilities.
269933707f3Ssthen  case $src in
270229e174cSsthen    -* | [=\(\)!]) src=./$src;;
271933707f3Ssthen  esac
272933707f3Ssthen
273933707f3Ssthen  if test -n "$dir_arg"; then
274933707f3Ssthen    dst=$src
275933707f3Ssthen    dstdir=$dst
276933707f3Ssthen    test -d "$dstdir"
277933707f3Ssthen    dstdir_status=$?
278*bdcafcd9Ssthen    # Don't chown directories that already exist.
279*bdcafcd9Ssthen    if test $dstdir_status = 0; then
280*bdcafcd9Ssthen      chowncmd=""
281*bdcafcd9Ssthen    fi
282933707f3Ssthen  else
283933707f3Ssthen
284933707f3Ssthen    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285933707f3Ssthen    # might cause directories to be created, which would be especially bad
286933707f3Ssthen    # if $src (and thus $dsttmp) contains '*'.
287933707f3Ssthen    if test ! -f "$src" && test ! -d "$src"; then
288933707f3Ssthen      echo "$0: $src does not exist." >&2
289933707f3Ssthen      exit 1
290933707f3Ssthen    fi
291933707f3Ssthen
292933707f3Ssthen    if test -z "$dst_arg"; then
293933707f3Ssthen      echo "$0: no destination specified." >&2
294933707f3Ssthen      exit 1
295933707f3Ssthen    fi
296933707f3Ssthen    dst=$dst_arg
297933707f3Ssthen
298*bdcafcd9Ssthen    # If destination is a directory, append the input filename.
299933707f3Ssthen    if test -d "$dst"; then
30032e31f52Ssthen      if test "$is_target_a_directory" = never; then
301933707f3Ssthen        echo "$0: $dst_arg: Is a directory" >&2
302933707f3Ssthen        exit 1
303933707f3Ssthen      fi
304933707f3Ssthen      dstdir=$dst
305*bdcafcd9Ssthen      dstbase=`basename "$src"`
306*bdcafcd9Ssthen      case $dst in
307*bdcafcd9Ssthen	*/) dst=$dst$dstbase;;
308*bdcafcd9Ssthen	*)  dst=$dst/$dstbase;;
309*bdcafcd9Ssthen      esac
310933707f3Ssthen      dstdir_status=0
311933707f3Ssthen    else
31232e31f52Ssthen      dstdir=`dirname "$dst"`
313933707f3Ssthen      test -d "$dstdir"
314933707f3Ssthen      dstdir_status=$?
315933707f3Ssthen    fi
316933707f3Ssthen  fi
317933707f3Ssthen
318*bdcafcd9Ssthen  case $dstdir in
319*bdcafcd9Ssthen    */) dstdirslash=$dstdir;;
320*bdcafcd9Ssthen    *)  dstdirslash=$dstdir/;;
321*bdcafcd9Ssthen  esac
322*bdcafcd9Ssthen
323933707f3Ssthen  obsolete_mkdir_used=false
324933707f3Ssthen
325933707f3Ssthen  if test $dstdir_status != 0; then
326933707f3Ssthen    case $posix_mkdir in
327933707f3Ssthen      '')
328933707f3Ssthen        # With -d, create the new directory with the user-specified mode.
329933707f3Ssthen        # Otherwise, rely on $mkdir_umask.
330933707f3Ssthen        if test -n "$dir_arg"; then
331933707f3Ssthen          mkdir_mode=-m$mode
332933707f3Ssthen        else
333933707f3Ssthen          mkdir_mode=
334933707f3Ssthen        fi
335933707f3Ssthen
336933707f3Ssthen        posix_mkdir=false
337*bdcafcd9Ssthen	# The $RANDOM variable is not portable (e.g., dash).  Use it
338*bdcafcd9Ssthen	# here however when possible just to lower collision chance.
339933707f3Ssthen	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340933707f3Ssthen
341*bdcafcd9Ssthen	trap '
342*bdcafcd9Ssthen	  ret=$?
343*bdcafcd9Ssthen	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344*bdcafcd9Ssthen	  exit $ret
345*bdcafcd9Ssthen	' 0
346*bdcafcd9Ssthen
347*bdcafcd9Ssthen	# Because "mkdir -p" follows existing symlinks and we likely work
348*bdcafcd9Ssthen	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349*bdcafcd9Ssthen	# directory is successfully created first before we actually test
350*bdcafcd9Ssthen	# 'mkdir -p'.
351933707f3Ssthen	if (umask $mkdir_umask &&
352*bdcafcd9Ssthen	    $mkdirprog $mkdir_mode "$tmpdir" &&
353*bdcafcd9Ssthen	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354933707f3Ssthen	then
355933707f3Ssthen	  if test -z "$dir_arg" || {
356933707f3Ssthen	       # Check for POSIX incompatibilities with -m.
357933707f3Ssthen	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358229e174cSsthen	       # other-writable bit of parent directory when it shouldn't.
359933707f3Ssthen	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360*bdcafcd9Ssthen	       test_tmpdir="$tmpdir/a"
361*bdcafcd9Ssthen	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362933707f3Ssthen	       case $ls_ld_tmpdir in
363933707f3Ssthen		 d????-?r-*) different_mode=700;;
364933707f3Ssthen		 d????-?--*) different_mode=755;;
365933707f3Ssthen		 *) false;;
366933707f3Ssthen	       esac &&
367*bdcafcd9Ssthen	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368*bdcafcd9Ssthen		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369933707f3Ssthen		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370933707f3Ssthen	       }
371933707f3Ssthen	     }
372933707f3Ssthen	  then posix_mkdir=:
373933707f3Ssthen	  fi
374*bdcafcd9Ssthen	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375933707f3Ssthen	else
376933707f3Ssthen	  # Remove any dirs left behind by ancient mkdir implementations.
377*bdcafcd9Ssthen	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378933707f3Ssthen	fi
379933707f3Ssthen	trap '' 0;;
380933707f3Ssthen    esac
381933707f3Ssthen
382933707f3Ssthen    if
383933707f3Ssthen      $posix_mkdir && (
384933707f3Ssthen        umask $mkdir_umask &&
385933707f3Ssthen        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386933707f3Ssthen      )
387933707f3Ssthen    then :
388933707f3Ssthen    else
389933707f3Ssthen
390*bdcafcd9Ssthen      # mkdir does not conform to POSIX,
391933707f3Ssthen      # or it failed possibly due to a race condition.  Create the
392933707f3Ssthen      # directory the slow way, step by step, checking for races as we go.
393933707f3Ssthen
394933707f3Ssthen      case $dstdir in
395933707f3Ssthen        /*) prefix='/';;
396229e174cSsthen        [-=\(\)!]*) prefix='./';;
397933707f3Ssthen        *)  prefix='';;
398933707f3Ssthen      esac
399933707f3Ssthen
400933707f3Ssthen      oIFS=$IFS
401933707f3Ssthen      IFS=/
40232e31f52Ssthen      set -f
403933707f3Ssthen      set fnord $dstdir
404933707f3Ssthen      shift
40532e31f52Ssthen      set +f
406933707f3Ssthen      IFS=$oIFS
407933707f3Ssthen
408933707f3Ssthen      prefixes=
409933707f3Ssthen
410933707f3Ssthen      for d
411933707f3Ssthen      do
412229e174cSsthen        test X"$d" = X && continue
413933707f3Ssthen
414933707f3Ssthen        prefix=$prefix$d
415933707f3Ssthen        if test -d "$prefix"; then
416933707f3Ssthen          prefixes=
417933707f3Ssthen        else
418933707f3Ssthen          if $posix_mkdir; then
419*bdcafcd9Ssthen            (umask $mkdir_umask &&
420933707f3Ssthen             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421933707f3Ssthen            # Don't fail if two instances are running concurrently.
422933707f3Ssthen            test -d "$prefix" || exit 1
423933707f3Ssthen          else
424933707f3Ssthen            case $prefix in
425933707f3Ssthen              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426933707f3Ssthen              *) qprefix=$prefix;;
427933707f3Ssthen            esac
428933707f3Ssthen            prefixes="$prefixes '$qprefix'"
429933707f3Ssthen          fi
430933707f3Ssthen        fi
431933707f3Ssthen        prefix=$prefix/
432933707f3Ssthen      done
433933707f3Ssthen
434933707f3Ssthen      if test -n "$prefixes"; then
435933707f3Ssthen        # Don't fail if two instances are running concurrently.
436933707f3Ssthen        (umask $mkdir_umask &&
437933707f3Ssthen         eval "\$doit_exec \$mkdirprog $prefixes") ||
438933707f3Ssthen          test -d "$dstdir" || exit 1
439933707f3Ssthen        obsolete_mkdir_used=true
440933707f3Ssthen      fi
441933707f3Ssthen    fi
442933707f3Ssthen  fi
443933707f3Ssthen
444933707f3Ssthen  if test -n "$dir_arg"; then
445933707f3Ssthen    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446933707f3Ssthen    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447933707f3Ssthen    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448933707f3Ssthen      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449933707f3Ssthen  else
450933707f3Ssthen
451933707f3Ssthen    # Make a couple of temp file names in the proper directory.
452*bdcafcd9Ssthen    dsttmp=${dstdirslash}_inst.$$_
453*bdcafcd9Ssthen    rmtmp=${dstdirslash}_rm.$$_
454933707f3Ssthen
455933707f3Ssthen    # Trap to clean up those temp files at exit.
456933707f3Ssthen    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457933707f3Ssthen
458933707f3Ssthen    # Copy the file name to the temp name.
459*bdcafcd9Ssthen    (umask $cp_umask &&
460*bdcafcd9Ssthen     { test -z "$stripcmd" || {
461*bdcafcd9Ssthen	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462*bdcafcd9Ssthen	 # which would cause strip to fail.
463*bdcafcd9Ssthen	 if test -z "$doit"; then
464*bdcafcd9Ssthen	   : >"$dsttmp" # No need to fork-exec 'touch'.
465*bdcafcd9Ssthen	 else
466*bdcafcd9Ssthen	   $doit touch "$dsttmp"
467*bdcafcd9Ssthen	 fi
468*bdcafcd9Ssthen       }
469*bdcafcd9Ssthen     } &&
470*bdcafcd9Ssthen     $doit_exec $cpprog "$src" "$dsttmp") &&
471933707f3Ssthen
472933707f3Ssthen    # and set any options; do chmod last to preserve setuid bits.
473933707f3Ssthen    #
474933707f3Ssthen    # If any of these fail, we abort the whole thing.  If we want to
475933707f3Ssthen    # ignore errors from any of these, just make sure not to ignore
476933707f3Ssthen    # errors from the above "$doit $cpprog $src $dsttmp" command.
477933707f3Ssthen    #
478933707f3Ssthen    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479933707f3Ssthen    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480933707f3Ssthen    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481933707f3Ssthen    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482933707f3Ssthen
483933707f3Ssthen    # If -C, don't bother to copy if it wouldn't change the file.
484933707f3Ssthen    if $copy_on_change &&
485933707f3Ssthen       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486933707f3Ssthen       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48732e31f52Ssthen       set -f &&
488933707f3Ssthen       set X $old && old=:$2:$4:$5:$6 &&
489933707f3Ssthen       set X $new && new=:$2:$4:$5:$6 &&
49032e31f52Ssthen       set +f &&
491933707f3Ssthen       test "$old" = "$new" &&
492933707f3Ssthen       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493933707f3Ssthen    then
494933707f3Ssthen      rm -f "$dsttmp"
495933707f3Ssthen    else
496*bdcafcd9Ssthen      # If $backupsuffix is set, and the file being installed
497*bdcafcd9Ssthen      # already exists, attempt a backup.  Don't worry if it fails,
498*bdcafcd9Ssthen      # e.g., if mv doesn't support -f.
499*bdcafcd9Ssthen      if test -n "$backupsuffix" && test -f "$dst"; then
500*bdcafcd9Ssthen        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501*bdcafcd9Ssthen      fi
502*bdcafcd9Ssthen
503933707f3Ssthen      # Rename the file to the real destination.
504933707f3Ssthen      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505933707f3Ssthen
506933707f3Ssthen      # The rename failed, perhaps because mv can't rename something else
507933707f3Ssthen      # to itself, or perhaps because mv is so ancient that it does not
508933707f3Ssthen      # support -f.
509933707f3Ssthen      {
510933707f3Ssthen        # Now remove or move aside any old file at destination location.
511933707f3Ssthen        # We try this two ways since rm can't unlink itself on some
512933707f3Ssthen        # systems and the destination file might be busy for other
513933707f3Ssthen        # reasons.  In this case, the final cleanup might fail but the new
514933707f3Ssthen        # file should still install successfully.
515933707f3Ssthen        {
516933707f3Ssthen          test ! -f "$dst" ||
517*bdcafcd9Ssthen          $doit $rmcmd "$dst" 2>/dev/null ||
518933707f3Ssthen          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519*bdcafcd9Ssthen            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520933707f3Ssthen          } ||
521933707f3Ssthen          { echo "$0: cannot unlink or rename $dst" >&2
522933707f3Ssthen            (exit 1); exit 1
523933707f3Ssthen          }
524933707f3Ssthen        } &&
525933707f3Ssthen
526933707f3Ssthen        # Now rename the file to the real destination.
527933707f3Ssthen        $doit $mvcmd "$dsttmp" "$dst"
528933707f3Ssthen      }
529933707f3Ssthen    fi || exit 1
530933707f3Ssthen
531933707f3Ssthen    trap '' 0
532933707f3Ssthen  fi
533933707f3Ssthendone
534933707f3Ssthen
535933707f3Ssthen# Local variables:
536*bdcafcd9Ssthen# eval: (add-hook 'before-save-hook 'time-stamp)
537933707f3Ssthen# time-stamp-start: "scriptversion="
538933707f3Ssthen# time-stamp-format: "%:y-%02m-%02d.%02H"
539*bdcafcd9Ssthen# time-stamp-time-zone: "UTC0"
540933707f3Ssthen# time-stamp-end: "; # UTC"
541933707f3Ssthen# End:
542