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