xref: /freebsd/contrib/libpcap/install-sh (revision afdbf109c6a661a729938f68211054a0a50d38ac)
18cf6c252SPaul Traina#!/bin/sh
28cf6c252SPaul Traina# install - install a program, script, or datafile
3*afdbf109SJoseph Mingrone
4*afdbf109SJoseph Mingronescriptversion=2020-11-14.01; # UTC
5*afdbf109SJoseph Mingrone
6*afdbf109SJoseph Mingrone# This originates from X11R5 (mit/util/scripts/install.sh), which was
7*afdbf109SJoseph Mingrone# later released in X11R6 (xc/config/util/install.sh) with the
8*afdbf109SJoseph Mingrone# following copyright and license.
93052b236SBill Fenner#
10*afdbf109SJoseph Mingrone# Copyright (C) 1994 X Consortium
113052b236SBill Fenner#
12*afdbf109SJoseph Mingrone# Permission is hereby granted, free of charge, to any person obtaining a copy
13*afdbf109SJoseph Mingrone# of this software and associated documentation files (the "Software"), to
14*afdbf109SJoseph Mingrone# deal in the Software without restriction, including without limitation the
15*afdbf109SJoseph Mingrone# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16*afdbf109SJoseph Mingrone# sell copies of the Software, and to permit persons to whom the Software is
17*afdbf109SJoseph Mingrone# furnished to do so, subject to the following conditions:
18*afdbf109SJoseph Mingrone#
19*afdbf109SJoseph Mingrone# The above copyright notice and this permission notice shall be included in
20*afdbf109SJoseph Mingrone# all copies or substantial portions of the Software.
21*afdbf109SJoseph Mingrone#
22*afdbf109SJoseph Mingrone# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23*afdbf109SJoseph Mingrone# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24*afdbf109SJoseph Mingrone# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25*afdbf109SJoseph Mingrone# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26*afdbf109SJoseph Mingrone# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27*afdbf109SJoseph Mingrone# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28*afdbf109SJoseph Mingrone#
29*afdbf109SJoseph Mingrone# Except as contained in this notice, the name of the X Consortium shall not
30*afdbf109SJoseph Mingrone# be used in advertising or otherwise to promote the sale, use or other deal-
31*afdbf109SJoseph Mingrone# ings in this Software without prior written authorization from the X Consor-
32*afdbf109SJoseph Mingrone# tium.
33*afdbf109SJoseph Mingrone#
34*afdbf109SJoseph Mingrone#
35*afdbf109SJoseph Mingrone# FSF changes to this file are in the public domain.
368cf6c252SPaul Traina#
378cf6c252SPaul Traina# Calling this script install-sh is preferred over install.sh, to prevent
38*afdbf109SJoseph Mingrone# 'make' implicit rules from creating a file called install from it
398cf6c252SPaul Traina# when there is no Makefile.
408cf6c252SPaul Traina#
418cf6c252SPaul Traina# This script is compatible with the BSD install script, but was written
42*afdbf109SJoseph Mingrone# from scratch.
438cf6c252SPaul Traina
44*afdbf109SJoseph Mingronetab='	'
45*afdbf109SJoseph Mingronenl='
46*afdbf109SJoseph Mingrone'
47*afdbf109SJoseph MingroneIFS=" $tab$nl"
488cf6c252SPaul Traina
49*afdbf109SJoseph Mingrone# Set DOITPROG to "echo" to test this script.
508cf6c252SPaul Traina
51*afdbf109SJoseph Mingronedoit=${DOITPROG-}
52*afdbf109SJoseph Mingronedoit_exec=${doit:-exec}
538cf6c252SPaul Traina
54*afdbf109SJoseph Mingrone# Put in absolute file names if you don't have them in your path;
55*afdbf109SJoseph Mingrone# or use environment vars.
568cf6c252SPaul Traina
57*afdbf109SJoseph Mingronechgrpprog=${CHGRPPROG-chgrp}
58*afdbf109SJoseph Mingronechmodprog=${CHMODPROG-chmod}
59*afdbf109SJoseph Mingronechownprog=${CHOWNPROG-chown}
60*afdbf109SJoseph Mingronecmpprog=${CMPPROG-cmp}
61*afdbf109SJoseph Mingronecpprog=${CPPROG-cp}
62*afdbf109SJoseph Mingronemkdirprog=${MKDIRPROG-mkdir}
63*afdbf109SJoseph Mingronemvprog=${MVPROG-mv}
64*afdbf109SJoseph Mingronermprog=${RMPROG-rm}
65*afdbf109SJoseph Mingronestripprog=${STRIPPROG-strip}
668cf6c252SPaul Traina
67*afdbf109SJoseph Mingroneposix_mkdir=
688cf6c252SPaul Traina
69*afdbf109SJoseph Mingrone# Desired mode of installed file.
70*afdbf109SJoseph Mingronemode=0755
71*afdbf109SJoseph Mingrone
72*afdbf109SJoseph Mingrone# Create dirs (including intermediate dirs) using mode 755.
73*afdbf109SJoseph Mingrone# This is like GNU 'install' as of coreutils 8.32 (2020).
74*afdbf109SJoseph Mingronemkdir_umask=22
75*afdbf109SJoseph Mingrone
76*afdbf109SJoseph Mingronebackupsuffix=
77*afdbf109SJoseph Mingronechgrpcmd=
78*afdbf109SJoseph Mingronechmodcmd=$chmodprog
79*afdbf109SJoseph Mingronechowncmd=
80*afdbf109SJoseph Mingronemvcmd=$mvprog
818cf6c252SPaul Trainarmcmd="$rmprog -f"
82*afdbf109SJoseph Mingronestripcmd=
838cf6c252SPaul Traina
84*afdbf109SJoseph Mingronesrc=
85*afdbf109SJoseph Mingronedst=
86*afdbf109SJoseph Mingronedir_arg=
87*afdbf109SJoseph Mingronedst_arg=
88*afdbf109SJoseph Mingrone
89*afdbf109SJoseph Mingronecopy_on_change=false
90*afdbf109SJoseph Mingroneis_target_a_directory=possibly
91*afdbf109SJoseph Mingrone
92*afdbf109SJoseph Mingroneusage="\
93*afdbf109SJoseph MingroneUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94*afdbf109SJoseph Mingrone   or: $0 [OPTION]... SRCFILES... DIRECTORY
95*afdbf109SJoseph Mingrone   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96*afdbf109SJoseph Mingrone   or: $0 [OPTION]... -d DIRECTORIES...
97*afdbf109SJoseph Mingrone
98*afdbf109SJoseph MingroneIn the 1st form, copy SRCFILE to DSTFILE.
99*afdbf109SJoseph MingroneIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100*afdbf109SJoseph MingroneIn the 4th, create DIRECTORIES.
101*afdbf109SJoseph Mingrone
102*afdbf109SJoseph MingroneOptions:
103*afdbf109SJoseph Mingrone     --help     display this help and exit.
104*afdbf109SJoseph Mingrone     --version  display version info and exit.
105*afdbf109SJoseph Mingrone
106*afdbf109SJoseph Mingrone  -c            (ignored)
107*afdbf109SJoseph Mingrone  -C            install only if different (preserve data modification time)
108*afdbf109SJoseph Mingrone  -d            create directories instead of installing files.
109*afdbf109SJoseph Mingrone  -g GROUP      $chgrpprog installed files to GROUP.
110*afdbf109SJoseph Mingrone  -m MODE       $chmodprog installed files to MODE.
111*afdbf109SJoseph Mingrone  -o USER       $chownprog installed files to USER.
112*afdbf109SJoseph Mingrone  -p            pass -p to $cpprog.
113*afdbf109SJoseph Mingrone  -s            $stripprog installed files.
114*afdbf109SJoseph Mingrone  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115*afdbf109SJoseph Mingrone  -t DIRECTORY  install into DIRECTORY.
116*afdbf109SJoseph Mingrone  -T            report an error if DSTFILE is a directory.
117*afdbf109SJoseph Mingrone
118*afdbf109SJoseph MingroneEnvironment variables override the default commands:
119*afdbf109SJoseph Mingrone  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120*afdbf109SJoseph Mingrone  RMPROG STRIPPROG
121*afdbf109SJoseph Mingrone
122*afdbf109SJoseph MingroneBy default, rm is invoked with -f; when overridden with RMPROG,
123*afdbf109SJoseph Mingroneit's up to you to specify -f if you want it.
124*afdbf109SJoseph Mingrone
125*afdbf109SJoseph MingroneIf -S is not specified, no backups are attempted.
126*afdbf109SJoseph Mingrone
127*afdbf109SJoseph MingroneEmail bug reports to bug-automake@gnu.org.
128*afdbf109SJoseph MingroneAutomake home page: https://www.gnu.org/software/automake/
129*afdbf109SJoseph Mingrone"
130*afdbf109SJoseph Mingrone
131*afdbf109SJoseph Mingronewhile test $# -ne 0; do
1328cf6c252SPaul Traina  case $1 in
133*afdbf109SJoseph Mingrone    -c) ;;
1348cf6c252SPaul Traina
135*afdbf109SJoseph Mingrone    -C) copy_on_change=true;;
1368cf6c252SPaul Traina
137*afdbf109SJoseph Mingrone    -d) dir_arg=true;;
1388cf6c252SPaul Traina
1398cf6c252SPaul Traina    -g) chgrpcmd="$chgrpprog $2"
140*afdbf109SJoseph Mingrone        shift;;
1418cf6c252SPaul Traina
142*afdbf109SJoseph Mingrone    --help) echo "$usage"; exit $?;;
1438cf6c252SPaul Traina
144*afdbf109SJoseph Mingrone    -m) mode=$2
145*afdbf109SJoseph Mingrone        case $mode in
146*afdbf109SJoseph Mingrone          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147*afdbf109SJoseph Mingrone            echo "$0: invalid mode: $mode" >&2
148*afdbf109SJoseph Mingrone            exit 1;;
149*afdbf109SJoseph Mingrone        esac
150*afdbf109SJoseph Mingrone        shift;;
1518cf6c252SPaul Traina
152*afdbf109SJoseph Mingrone    -o) chowncmd="$chownprog $2"
153*afdbf109SJoseph Mingrone        shift;;
1548cf6c252SPaul Traina
155*afdbf109SJoseph Mingrone    -p) cpprog="$cpprog -p";;
156*afdbf109SJoseph Mingrone
157*afdbf109SJoseph Mingrone    -s) stripcmd=$stripprog;;
158*afdbf109SJoseph Mingrone
159*afdbf109SJoseph Mingrone    -S) backupsuffix="$2"
160*afdbf109SJoseph Mingrone        shift;;
161*afdbf109SJoseph Mingrone
162*afdbf109SJoseph Mingrone    -t)
163*afdbf109SJoseph Mingrone        is_target_a_directory=always
164*afdbf109SJoseph Mingrone        dst_arg=$2
165*afdbf109SJoseph Mingrone        # Protect names problematic for 'test' and other utilities.
166*afdbf109SJoseph Mingrone        case $dst_arg in
167*afdbf109SJoseph Mingrone          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168*afdbf109SJoseph Mingrone        esac
169*afdbf109SJoseph Mingrone        shift;;
170*afdbf109SJoseph Mingrone
171*afdbf109SJoseph Mingrone    -T) is_target_a_directory=never;;
172*afdbf109SJoseph Mingrone
173*afdbf109SJoseph Mingrone    --version) echo "$0 $scriptversion"; exit $?;;
174*afdbf109SJoseph Mingrone
175*afdbf109SJoseph Mingrone    --) shift
176*afdbf109SJoseph Mingrone        break;;
177*afdbf109SJoseph Mingrone
178*afdbf109SJoseph Mingrone    -*) echo "$0: invalid option: $1" >&2
179*afdbf109SJoseph Mingrone        exit 1;;
180*afdbf109SJoseph Mingrone
181*afdbf109SJoseph Mingrone    *)  break;;
182*afdbf109SJoseph Mingrone  esac
183*afdbf109SJoseph Mingrone  shift
184*afdbf109SJoseph Mingronedone
185*afdbf109SJoseph Mingrone
186*afdbf109SJoseph Mingrone# We allow the use of options -d and -T together, by making -d
187*afdbf109SJoseph Mingrone# take the precedence; this is for compatibility with GNU install.
188*afdbf109SJoseph Mingrone
189*afdbf109SJoseph Mingroneif test -n "$dir_arg"; then
190*afdbf109SJoseph Mingrone  if test -n "$dst_arg"; then
191*afdbf109SJoseph Mingrone    echo "$0: target directory not allowed when installing a directory." >&2
192*afdbf109SJoseph Mingrone    exit 1
1938cf6c252SPaul Traina  fi
194*afdbf109SJoseph Mingronefi
195*afdbf109SJoseph Mingrone
196*afdbf109SJoseph Mingroneif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197*afdbf109SJoseph Mingrone  # When -d is used, all remaining arguments are directories to create.
198*afdbf109SJoseph Mingrone  # When -t is used, the destination is already specified.
199*afdbf109SJoseph Mingrone  # Otherwise, the last argument is the destination.  Remove it from $@.
200*afdbf109SJoseph Mingrone  for arg
201*afdbf109SJoseph Mingrone  do
202*afdbf109SJoseph Mingrone    if test -n "$dst_arg"; then
203*afdbf109SJoseph Mingrone      # $@ is not empty: it contains at least $arg.
204*afdbf109SJoseph Mingrone      set fnord "$@" "$dst_arg"
205*afdbf109SJoseph Mingrone      shift # fnord
206*afdbf109SJoseph Mingrone    fi
207*afdbf109SJoseph Mingrone    shift # arg
208*afdbf109SJoseph Mingrone    dst_arg=$arg
209*afdbf109SJoseph Mingrone    # Protect names problematic for 'test' and other utilities.
210*afdbf109SJoseph Mingrone    case $dst_arg in
211*afdbf109SJoseph Mingrone      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2128cf6c252SPaul Traina    esac
2138cf6c252SPaul Traina  done
214*afdbf109SJoseph Mingronefi
2158cf6c252SPaul Traina
216*afdbf109SJoseph Mingroneif test $# -eq 0; then
217*afdbf109SJoseph Mingrone  if test -z "$dir_arg"; then
218*afdbf109SJoseph Mingrone    echo "$0: no input file specified." >&2
2198cf6c252SPaul Traina    exit 1
220*afdbf109SJoseph Mingrone  fi
221*afdbf109SJoseph Mingrone  # It's OK to call 'install-sh -d' without argument.
222*afdbf109SJoseph Mingrone  # This can happen when creating conditional directories.
223*afdbf109SJoseph Mingrone  exit 0
2248cf6c252SPaul Trainafi
2258cf6c252SPaul Traina
226*afdbf109SJoseph Mingroneif test -z "$dir_arg"; then
227*afdbf109SJoseph Mingrone  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228*afdbf109SJoseph Mingrone    if test ! -d "$dst_arg"; then
229*afdbf109SJoseph Mingrone      echo "$0: $dst_arg: Is not a directory." >&2
230*afdbf109SJoseph Mingrone      exit 1
231*afdbf109SJoseph Mingrone    fi
232*afdbf109SJoseph Mingrone  fi
233*afdbf109SJoseph Mingronefi
234*afdbf109SJoseph Mingrone
235*afdbf109SJoseph Mingroneif test -z "$dir_arg"; then
236*afdbf109SJoseph Mingrone  do_exit='(exit $ret); exit $ret'
237*afdbf109SJoseph Mingrone  trap "ret=129; $do_exit" 1
238*afdbf109SJoseph Mingrone  trap "ret=130; $do_exit" 2
239*afdbf109SJoseph Mingrone  trap "ret=141; $do_exit" 13
240*afdbf109SJoseph Mingrone  trap "ret=143; $do_exit" 15
241*afdbf109SJoseph Mingrone
242*afdbf109SJoseph Mingrone  # Set umask so as not to create temps with too-generous modes.
243*afdbf109SJoseph Mingrone  # However, 'strip' requires both read and write access to temps.
244*afdbf109SJoseph Mingrone  case $mode in
245*afdbf109SJoseph Mingrone    # Optimize common cases.
246*afdbf109SJoseph Mingrone    *644) cp_umask=133;;
247*afdbf109SJoseph Mingrone    *755) cp_umask=22;;
248*afdbf109SJoseph Mingrone
249*afdbf109SJoseph Mingrone    *[0-7])
250*afdbf109SJoseph Mingrone      if test -z "$stripcmd"; then
251*afdbf109SJoseph Mingrone        u_plus_rw=
252*afdbf109SJoseph Mingrone      else
253*afdbf109SJoseph Mingrone        u_plus_rw='% 200'
254*afdbf109SJoseph Mingrone      fi
255*afdbf109SJoseph Mingrone      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256*afdbf109SJoseph Mingrone    *)
257*afdbf109SJoseph Mingrone      if test -z "$stripcmd"; then
258*afdbf109SJoseph Mingrone        u_plus_rw=
259*afdbf109SJoseph Mingrone      else
260*afdbf109SJoseph Mingrone        u_plus_rw=,u+rw
261*afdbf109SJoseph Mingrone      fi
262*afdbf109SJoseph Mingrone      cp_umask=$mode$u_plus_rw;;
263*afdbf109SJoseph Mingrone  esac
264*afdbf109SJoseph Mingronefi
265*afdbf109SJoseph Mingrone
266*afdbf109SJoseph Mingronefor src
267*afdbf109SJoseph Mingronedo
268*afdbf109SJoseph Mingrone  # Protect names problematic for 'test' and other utilities.
269*afdbf109SJoseph Mingrone  case $src in
270*afdbf109SJoseph Mingrone    -* | [=\(\)!]) src=./$src;;
271*afdbf109SJoseph Mingrone  esac
272*afdbf109SJoseph Mingrone
273*afdbf109SJoseph Mingrone  if test -n "$dir_arg"; then
2748cf6c252SPaul Traina    dst=$src
275*afdbf109SJoseph Mingrone    dstdir=$dst
276*afdbf109SJoseph Mingrone    test -d "$dstdir"
277*afdbf109SJoseph Mingrone    dstdir_status=$?
278*afdbf109SJoseph Mingrone    # Don't chown directories that already exist.
279*afdbf109SJoseph Mingrone    if test $dstdir_status = 0; then
280*afdbf109SJoseph Mingrone      chowncmd=""
2818cf6c252SPaul Traina    fi
2828cf6c252SPaul Traina  else
2838cf6c252SPaul Traina
284*afdbf109SJoseph Mingrone    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2858cf6c252SPaul Traina    # might cause directories to be created, which would be especially bad
2868cf6c252SPaul Traina    # if $src (and thus $dsttmp) contains '*'.
287*afdbf109SJoseph Mingrone    if test ! -f "$src" && test ! -d "$src"; then
288*afdbf109SJoseph Mingrone      echo "$0: $src does not exist." >&2
2898cf6c252SPaul Traina      exit 1
2908cf6c252SPaul Traina    fi
2918cf6c252SPaul Traina
292*afdbf109SJoseph Mingrone    if test -z "$dst_arg"; then
293*afdbf109SJoseph Mingrone      echo "$0: no destination specified." >&2
2948cf6c252SPaul Traina      exit 1
295*afdbf109SJoseph Mingrone    fi
296*afdbf109SJoseph Mingrone    dst=$dst_arg
297*afdbf109SJoseph Mingrone
298*afdbf109SJoseph Mingrone    # If destination is a directory, append the input filename.
299*afdbf109SJoseph Mingrone    if test -d "$dst"; then
300*afdbf109SJoseph Mingrone      if test "$is_target_a_directory" = never; then
301*afdbf109SJoseph Mingrone        echo "$0: $dst_arg: Is a directory" >&2
302*afdbf109SJoseph Mingrone        exit 1
303*afdbf109SJoseph Mingrone      fi
304*afdbf109SJoseph Mingrone      dstdir=$dst
305*afdbf109SJoseph Mingrone      dstbase=`basename "$src"`
306*afdbf109SJoseph Mingrone      case $dst in
307*afdbf109SJoseph Mingrone	*/) dst=$dst$dstbase;;
308*afdbf109SJoseph Mingrone	*)  dst=$dst/$dstbase;;
309*afdbf109SJoseph Mingrone      esac
310*afdbf109SJoseph Mingrone      dstdir_status=0
3118cf6c252SPaul Traina    else
312*afdbf109SJoseph Mingrone      dstdir=`dirname "$dst"`
313*afdbf109SJoseph Mingrone      test -d "$dstdir"
314*afdbf109SJoseph Mingrone      dstdir_status=$?
315*afdbf109SJoseph Mingrone    fi
3168cf6c252SPaul Traina  fi
3178cf6c252SPaul Traina
318*afdbf109SJoseph Mingrone  case $dstdir in
319*afdbf109SJoseph Mingrone    */) dstdirslash=$dstdir;;
320*afdbf109SJoseph Mingrone    *)  dstdirslash=$dstdir/;;
321*afdbf109SJoseph Mingrone  esac
3228cf6c252SPaul Traina
323*afdbf109SJoseph Mingrone  obsolete_mkdir_used=false
324*afdbf109SJoseph Mingrone
325*afdbf109SJoseph Mingrone  if test $dstdir_status != 0; then
326*afdbf109SJoseph Mingrone    case $posix_mkdir in
327*afdbf109SJoseph Mingrone      '')
328*afdbf109SJoseph Mingrone        # With -d, create the new directory with the user-specified mode.
329*afdbf109SJoseph Mingrone        # Otherwise, rely on $mkdir_umask.
330*afdbf109SJoseph Mingrone        if test -n "$dir_arg"; then
331*afdbf109SJoseph Mingrone          mkdir_mode=-m$mode
332*afdbf109SJoseph Mingrone        else
333*afdbf109SJoseph Mingrone          mkdir_mode=
334*afdbf109SJoseph Mingrone        fi
335*afdbf109SJoseph Mingrone
336*afdbf109SJoseph Mingrone        posix_mkdir=false
337*afdbf109SJoseph Mingrone	# The $RANDOM variable is not portable (e.g., dash).  Use it
338*afdbf109SJoseph Mingrone	# here however when possible just to lower collision chance.
339*afdbf109SJoseph Mingrone	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340*afdbf109SJoseph Mingrone
341*afdbf109SJoseph Mingrone	trap '
342*afdbf109SJoseph Mingrone	  ret=$?
343*afdbf109SJoseph Mingrone	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344*afdbf109SJoseph Mingrone	  exit $ret
345*afdbf109SJoseph Mingrone	' 0
346*afdbf109SJoseph Mingrone
347*afdbf109SJoseph Mingrone	# Because "mkdir -p" follows existing symlinks and we likely work
348*afdbf109SJoseph Mingrone	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349*afdbf109SJoseph Mingrone	# directory is successfully created first before we actually test
350*afdbf109SJoseph Mingrone	# 'mkdir -p'.
351*afdbf109SJoseph Mingrone	if (umask $mkdir_umask &&
352*afdbf109SJoseph Mingrone	    $mkdirprog $mkdir_mode "$tmpdir" &&
353*afdbf109SJoseph Mingrone	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3548cf6c252SPaul Traina	then
355*afdbf109SJoseph Mingrone	  if test -z "$dir_arg" || {
356*afdbf109SJoseph Mingrone	       # Check for POSIX incompatibilities with -m.
357*afdbf109SJoseph Mingrone	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358*afdbf109SJoseph Mingrone	       # other-writable bit of parent directory when it shouldn't.
359*afdbf109SJoseph Mingrone	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360*afdbf109SJoseph Mingrone	       test_tmpdir="$tmpdir/a"
361*afdbf109SJoseph Mingrone	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362*afdbf109SJoseph Mingrone	       case $ls_ld_tmpdir in
363*afdbf109SJoseph Mingrone		 d????-?r-*) different_mode=700;;
364*afdbf109SJoseph Mingrone		 d????-?--*) different_mode=755;;
365*afdbf109SJoseph Mingrone		 *) false;;
366*afdbf109SJoseph Mingrone	       esac &&
367*afdbf109SJoseph Mingrone	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368*afdbf109SJoseph Mingrone		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369*afdbf109SJoseph Mingrone		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370*afdbf109SJoseph Mingrone	       }
371*afdbf109SJoseph Mingrone	     }
372*afdbf109SJoseph Mingrone	  then posix_mkdir=:
373*afdbf109SJoseph Mingrone	  fi
374*afdbf109SJoseph Mingrone	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3758cf6c252SPaul Traina	else
376*afdbf109SJoseph Mingrone	  # Remove any dirs left behind by ancient mkdir implementations.
377*afdbf109SJoseph Mingrone	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3788cf6c252SPaul Traina	fi
379*afdbf109SJoseph Mingrone	trap '' 0;;
380*afdbf109SJoseph Mingrone    esac
3818cf6c252SPaul Traina
382*afdbf109SJoseph Mingrone    if
383*afdbf109SJoseph Mingrone      $posix_mkdir && (
384*afdbf109SJoseph Mingrone        umask $mkdir_umask &&
385*afdbf109SJoseph Mingrone        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386*afdbf109SJoseph Mingrone      )
387*afdbf109SJoseph Mingrone    then :
388*afdbf109SJoseph Mingrone    else
3898cf6c252SPaul Traina
390*afdbf109SJoseph Mingrone      # mkdir does not conform to POSIX,
391*afdbf109SJoseph Mingrone      # or it failed possibly due to a race condition.  Create the
392*afdbf109SJoseph Mingrone      # directory the slow way, step by step, checking for races as we go.
3938cf6c252SPaul Traina
394*afdbf109SJoseph Mingrone      case $dstdir in
395*afdbf109SJoseph Mingrone        /*) prefix='/';;
396*afdbf109SJoseph Mingrone        [-=\(\)!]*) prefix='./';;
397*afdbf109SJoseph Mingrone        *)  prefix='';;
398*afdbf109SJoseph Mingrone      esac
3998cf6c252SPaul Traina
400*afdbf109SJoseph Mingrone      oIFS=$IFS
401*afdbf109SJoseph Mingrone      IFS=/
402*afdbf109SJoseph Mingrone      set -f
403*afdbf109SJoseph Mingrone      set fnord $dstdir
4048cf6c252SPaul Traina      shift
405*afdbf109SJoseph Mingrone      set +f
406*afdbf109SJoseph Mingrone      IFS=$oIFS
4078cf6c252SPaul Traina
408*afdbf109SJoseph Mingrone      prefixes=
409*afdbf109SJoseph Mingrone
410*afdbf109SJoseph Mingrone      for d
411*afdbf109SJoseph Mingrone      do
412*afdbf109SJoseph Mingrone        test X"$d" = X && continue
413*afdbf109SJoseph Mingrone
414*afdbf109SJoseph Mingrone        prefix=$prefix$d
415*afdbf109SJoseph Mingrone        if test -d "$prefix"; then
416*afdbf109SJoseph Mingrone          prefixes=
4178cf6c252SPaul Traina        else
418*afdbf109SJoseph Mingrone          if $posix_mkdir; then
419*afdbf109SJoseph Mingrone            (umask $mkdir_umask &&
420*afdbf109SJoseph Mingrone             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421*afdbf109SJoseph Mingrone            # Don't fail if two instances are running concurrently.
422*afdbf109SJoseph Mingrone            test -d "$prefix" || exit 1
423*afdbf109SJoseph Mingrone          else
424*afdbf109SJoseph Mingrone            case $prefix in
425*afdbf109SJoseph Mingrone              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426*afdbf109SJoseph Mingrone              *) qprefix=$prefix;;
427*afdbf109SJoseph Mingrone            esac
428*afdbf109SJoseph Mingrone            prefixes="$prefixes '$qprefix'"
4298cf6c252SPaul Traina          fi
430*afdbf109SJoseph Mingrone        fi
431*afdbf109SJoseph Mingrone        prefix=$prefix/
4328cf6c252SPaul Traina      done
433*afdbf109SJoseph Mingrone
434*afdbf109SJoseph Mingrone      if test -n "$prefixes"; then
435*afdbf109SJoseph Mingrone        # Don't fail if two instances are running concurrently.
436*afdbf109SJoseph Mingrone        (umask $mkdir_umask &&
437*afdbf109SJoseph Mingrone         eval "\$doit_exec \$mkdirprog $prefixes") ||
438*afdbf109SJoseph Mingrone          test -d "$dstdir" || exit 1
439*afdbf109SJoseph Mingrone        obsolete_mkdir_used=true
440*afdbf109SJoseph Mingrone      fi
441*afdbf109SJoseph Mingrone    fi
4428cf6c252SPaul Traina  fi
4438cf6c252SPaul Traina
444*afdbf109SJoseph Mingrone  if test -n "$dir_arg"; then
445*afdbf109SJoseph Mingrone    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446*afdbf109SJoseph Mingrone    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447*afdbf109SJoseph Mingrone    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448*afdbf109SJoseph Mingrone      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4498cf6c252SPaul Traina  else
4508cf6c252SPaul Traina
451*afdbf109SJoseph Mingrone    # Make a couple of temp file names in the proper directory.
452*afdbf109SJoseph Mingrone    dsttmp=${dstdirslash}_inst.$$_
453*afdbf109SJoseph Mingrone    rmtmp=${dstdirslash}_rm.$$_
4548cf6c252SPaul Traina
455*afdbf109SJoseph Mingrone    # Trap to clean up those temp files at exit.
456*afdbf109SJoseph Mingrone    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457*afdbf109SJoseph Mingrone
458*afdbf109SJoseph Mingrone    # Copy the file name to the temp name.
459*afdbf109SJoseph Mingrone    (umask $cp_umask &&
460*afdbf109SJoseph Mingrone     { test -z "$stripcmd" || {
461*afdbf109SJoseph Mingrone	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462*afdbf109SJoseph Mingrone	 # which would cause strip to fail.
463*afdbf109SJoseph Mingrone	 if test -z "$doit"; then
464*afdbf109SJoseph Mingrone	   : >"$dsttmp" # No need to fork-exec 'touch'.
4658cf6c252SPaul Traina	 else
466*afdbf109SJoseph Mingrone	   $doit touch "$dsttmp"
4678cf6c252SPaul Traina	 fi
468*afdbf109SJoseph Mingrone       }
469*afdbf109SJoseph Mingrone     } &&
470*afdbf109SJoseph Mingrone     $doit_exec $cpprog "$src" "$dsttmp") &&
4718cf6c252SPaul Traina
472*afdbf109SJoseph Mingrone    # and set any options; do chmod last to preserve setuid bits.
473*afdbf109SJoseph Mingrone    #
4748cf6c252SPaul Traina    # If any of these fail, we abort the whole thing.  If we want to
4758cf6c252SPaul Traina    # ignore errors from any of these, just make sure not to ignore
476*afdbf109SJoseph Mingrone    # errors from the above "$doit $cpprog $src $dsttmp" command.
477*afdbf109SJoseph Mingrone    #
478*afdbf109SJoseph Mingrone    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479*afdbf109SJoseph Mingrone    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480*afdbf109SJoseph Mingrone    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481*afdbf109SJoseph Mingrone    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4828cf6c252SPaul Traina
483*afdbf109SJoseph Mingrone    # If -C, don't bother to copy if it wouldn't change the file.
484*afdbf109SJoseph Mingrone    if $copy_on_change &&
485*afdbf109SJoseph Mingrone       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486*afdbf109SJoseph Mingrone       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487*afdbf109SJoseph Mingrone       set -f &&
488*afdbf109SJoseph Mingrone       set X $old && old=:$2:$4:$5:$6 &&
489*afdbf109SJoseph Mingrone       set X $new && new=:$2:$4:$5:$6 &&
490*afdbf109SJoseph Mingrone       set +f &&
491*afdbf109SJoseph Mingrone       test "$old" = "$new" &&
492*afdbf109SJoseph Mingrone       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493*afdbf109SJoseph Mingrone    then
494*afdbf109SJoseph Mingrone      rm -f "$dsttmp"
495*afdbf109SJoseph Mingrone    else
496*afdbf109SJoseph Mingrone      # If $backupsuffix is set, and the file being installed
497*afdbf109SJoseph Mingrone      # already exists, attempt a backup.  Don't worry if it fails,
498*afdbf109SJoseph Mingrone      # e.g., if mv doesn't support -f.
499*afdbf109SJoseph Mingrone      if test -n "$backupsuffix" && test -f "$dst"; then
500*afdbf109SJoseph Mingrone        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501*afdbf109SJoseph Mingrone      fi
502*afdbf109SJoseph Mingrone
503*afdbf109SJoseph Mingrone      # Rename the file to the real destination.
504*afdbf109SJoseph Mingrone      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505*afdbf109SJoseph Mingrone
506*afdbf109SJoseph Mingrone      # The rename failed, perhaps because mv can't rename something else
507*afdbf109SJoseph Mingrone      # to itself, or perhaps because mv is so ancient that it does not
508*afdbf109SJoseph Mingrone      # support -f.
509*afdbf109SJoseph Mingrone      {
510*afdbf109SJoseph Mingrone        # Now remove or move aside any old file at destination location.
511*afdbf109SJoseph Mingrone        # We try this two ways since rm can't unlink itself on some
512*afdbf109SJoseph Mingrone        # systems and the destination file might be busy for other
513*afdbf109SJoseph Mingrone        # reasons.  In this case, the final cleanup might fail but the new
514*afdbf109SJoseph Mingrone        # file should still install successfully.
515*afdbf109SJoseph Mingrone        {
516*afdbf109SJoseph Mingrone          test ! -f "$dst" ||
517*afdbf109SJoseph Mingrone          $doit $rmcmd "$dst" 2>/dev/null ||
518*afdbf109SJoseph Mingrone          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519*afdbf109SJoseph Mingrone            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520*afdbf109SJoseph Mingrone          } ||
521*afdbf109SJoseph Mingrone          { echo "$0: cannot unlink or rename $dst" >&2
522*afdbf109SJoseph Mingrone            (exit 1); exit 1
523*afdbf109SJoseph Mingrone          }
524*afdbf109SJoseph Mingrone        } &&
5258cf6c252SPaul Traina
5268cf6c252SPaul Traina        # Now rename the file to the real destination.
527*afdbf109SJoseph Mingrone        $doit $mvcmd "$dsttmp" "$dst"
528*afdbf109SJoseph Mingrone      }
529*afdbf109SJoseph Mingrone    fi || exit 1
5308cf6c252SPaul Traina
531*afdbf109SJoseph Mingrone    trap '' 0
532*afdbf109SJoseph Mingrone  fi
533*afdbf109SJoseph Mingronedone
5348cf6c252SPaul Traina
535*afdbf109SJoseph Mingrone# Local variables:
536*afdbf109SJoseph Mingrone# eval: (add-hook 'before-save-hook 'time-stamp)
537*afdbf109SJoseph Mingrone# time-stamp-start: "scriptversion="
538*afdbf109SJoseph Mingrone# time-stamp-format: "%:y-%02m-%02d.%02H"
539*afdbf109SJoseph Mingrone# time-stamp-time-zone: "UTC0"
540*afdbf109SJoseph Mingrone# time-stamp-end: "; # UTC"
541*afdbf109SJoseph Mingrone# End:
542