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