145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#!/bin/sh
245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# install - install a program, script, or datafile
345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.orgscriptversion=2009-04-28.21; # UTC
545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# This originates from X11R5 (mit/util/scripts/install.sh), which was
745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# later released in X11R6 (xc/config/util/install.sh) with the
845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# following copyright and license.
945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#
1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Copyright (C) 1994 X Consortium
1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#
1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Permission is hereby granted, free of charge, to any person obtaining a copy
1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# of this software and associated documentation files (the "Software"), to
1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# deal in the Software without restriction, including without limitation the
1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# sell copies of the Software, and to permit persons to whom the Software is
1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# furnished to do so, subject to the following conditions:
1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#
1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# The above copyright notice and this permission notice shall be included in
2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# all copies or substantial portions of the Software.
2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#
2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Except as contained in this notice, the name of the X Consortium shall not
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# be used in advertising or otherwise to promote the sale, use or other deal-
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# ings in this Software without prior written authorization from the X Consor-
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# tium.
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#
3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# FSF changes to this file are in the public domain.
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Calling this script install-sh is preferred over install.sh, to prevent
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# `make' implicit rules from creating a file called install from it
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# when there is no Makefile.
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# This script is compatible with the BSD install script, but was written
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# from scratch.
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgnl='
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org'
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgIFS=" ""	$nl"
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# set DOITPROG to echo to test this script
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Don't use :- since 4.3BSD and earlier shells don't like it.
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgdoit=${DOITPROG-}
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgif test -z "$doit"; then
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  doit_exec=exec
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgelse
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  doit_exec=$doit
5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgfi
5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Put in absolute file names if you don't have them in your path;
5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# or use environment vars.
6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgchgrpprog=${CHGRPPROG-chgrp}
6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgchmodprog=${CHMODPROG-chmod}
6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgchownprog=${CHOWNPROG-chown}
6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcmpprog=${CMPPROG-cmp}
6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcpprog=${CPPROG-cp}
6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgmkdirprog=${MKDIRPROG-mkdir}
6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgmvprog=${MVPROG-mv}
6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgrmprog=${RMPROG-rm}
6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstripprog=${STRIPPROG-strip}
7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgposix_glob='?'
7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orginitialize_posix_glob='
7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  test "$posix_glob" != "?" || {
7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if (set -f) 2>/dev/null; then
7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      posix_glob=
7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    else
7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      posix_glob=:
7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    fi
7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  }
8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org'
8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgposix_mkdir=
8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Desired mode of installed file.
8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgmode=0755
8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgchgrpcmd=
8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgchmodcmd=$chmodprog
8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgchowncmd=
9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgmvcmd=$mvprog
9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgrmcmd="$rmprog -f"
9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstripcmd=
9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgsrc=
9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgdst=
9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgdir_arg=
9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgdst_arg=
9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgcopy_on_change=false
10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgno_target_directory=
10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgusage="\
10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org   or: $0 [OPTION]... SRCFILES... DIRECTORY
10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org   or: $0 [OPTION]... -d DIRECTORIES...
10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgIn the 1st form, copy SRCFILE to DSTFILE.
10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgIn the 4th, create DIRECTORIES.
11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgOptions:
11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     --help     display this help and exit.
11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org     --version  display version info and exit.
11545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  -c            (ignored)
11745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  -C            install only if different (preserve the last data modification time)
11845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  -d            create directories instead of installing files.
11945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  -g GROUP      $chgrpprog installed files to GROUP.
12045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  -m MODE       $chmodprog installed files to MODE.
12145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  -o USER       $chownprog installed files to USER.
12245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  -s            $stripprog installed files.
12345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  -t DIRECTORY  install into DIRECTORY.
12445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  -T            report an error if DSTFILE is a directory.
12545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
12645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgEnvironment variables override the default commands:
12745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  RMPROG STRIPPROG
12945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org"
13045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgwhile test $# -ne 0; do
13245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  case $1 in
13345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    -c) ;;
13445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    -C) copy_on_change=true;;
13645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    -d) dir_arg=true;;
13845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    -g) chgrpcmd="$chgrpprog $2"
14045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	shift;;
14145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
14245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    --help) echo "$usage"; exit $?;;
14345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
14445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    -m) mode=$2
14545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	case $mode in
14645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  *' '* | *'	'* | *'
14745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org'*	  | *'*'* | *'?'* | *'['*)
14845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    echo "$0: invalid mode: $mode" >&2
14945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    exit 1;;
15045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	esac
15145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	shift;;
15245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
15345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    -o) chowncmd="$chownprog $2"
15445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	shift;;
15545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
15645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    -s) stripcmd=$stripprog;;
15745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
15845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    -t) dst_arg=$2
15945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	shift;;
16045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    -T) no_target_directory=true;;
16245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    --version) echo "$0 $scriptversion"; exit $?;;
16445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    --)	shift
16645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	break;;
16745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    -*)	echo "$0: invalid option: $1" >&2
16945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	exit 1;;
17045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
17145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    *)  break;;
17245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  esac
17345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  shift
17445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgdone
17545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
17645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  # When -d is used, all remaining arguments are directories to create.
17845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  # When -t is used, the destination is already specified.
17945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  # Otherwise, the last argument is the destination.  Remove it from $@.
18045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  for arg
18145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  do
18245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if test -n "$dst_arg"; then
18345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      # $@ is not empty: it contains at least $arg.
18445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      set fnord "$@" "$dst_arg"
18545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      shift # fnord
18645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    fi
18745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    shift # arg
18845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    dst_arg=$arg
18945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  done
19045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgfi
19145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
19245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgif test $# -eq 0; then
19345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  if test -z "$dir_arg"; then
19445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    echo "$0: no input file specified." >&2
19545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    exit 1
19645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  fi
19745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  # It's OK to call `install-sh -d' without argument.
19845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  # This can happen when creating conditional directories.
19945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  exit 0
20045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgfi
20145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
20245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgif test -z "$dir_arg"; then
20345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  trap '(exit $?); exit' 1 2 13 15
20445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
20545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  # Set umask so as not to create temps with too-generous modes.
20645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  # However, 'strip' requires both read and write access to temps.
20745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  case $mode in
20845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # Optimize common cases.
20945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    *644) cp_umask=133;;
21045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    *755) cp_umask=22;;
21145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
21245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    *[0-7])
21345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      if test -z "$stripcmd"; then
21445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	u_plus_rw=
21545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      else
21645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	u_plus_rw='% 200'
21745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      fi
21845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    *)
22045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      if test -z "$stripcmd"; then
22145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	u_plus_rw=
22245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      else
22345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	u_plus_rw=,u+rw
22445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      fi
22545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      cp_umask=$mode$u_plus_rw;;
22645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  esac
22745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgfi
22845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
22945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgfor src
23045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgdo
23145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  # Protect names starting with `-'.
23245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  case $src in
23345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    -*) src=./$src;;
23445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  esac
23545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
23645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  if test -n "$dir_arg"; then
23745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    dst=$src
23845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    dstdir=$dst
23945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    test -d "$dstdir"
24045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    dstdir_status=$?
24145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  else
24245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
24345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # might cause directories to be created, which would be especially bad
24545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # if $src (and thus $dsttmp) contains '*'.
24645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if test ! -f "$src" && test ! -d "$src"; then
24745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      echo "$0: $src does not exist." >&2
24845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      exit 1
24945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    fi
25045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
25145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if test -z "$dst_arg"; then
25245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      echo "$0: no destination specified." >&2
25345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      exit 1
25445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    fi
25545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
25645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    dst=$dst_arg
25745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # Protect names starting with `-'.
25845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    case $dst in
25945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      -*) dst=./$dst;;
26045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    esac
26145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
26245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # If destination is a directory, append the input filename; won't work
26345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # if double slashes aren't ignored.
26445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if test -d "$dst"; then
26545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      if test -n "$no_target_directory"; then
26645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	echo "$0: $dst_arg: Is a directory" >&2
26745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	exit 1
26845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      fi
26945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      dstdir=$dst
27045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      dst=$dstdir/`basename "$src"`
27145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      dstdir_status=0
27245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    else
27345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      # Prefer dirname, but fall back on a substitute if dirname fails.
27445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      dstdir=`
27545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	(dirname "$dst") 2>/dev/null ||
27645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	     X"$dst" : 'X\(//\)[^/]' \| \
27845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	     X"$dst" : 'X\(//\)$' \| \
27945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	echo X"$dst" |
28145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   s//\1/
28345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   q
28445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		 }
28545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		 /^X\(\/\/\)[^/].*/{
28645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   s//\1/
28745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   q
28845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		 }
28945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		 /^X\(\/\/\)$/{
29045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   s//\1/
29145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   q
29245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		 }
29345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		 /^X\(\/\).*/{
29445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   s//\1/
29545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   q
29645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		 }
29745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		 s/.*/./; q'
29845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      `
29945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
30045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      test -d "$dstdir"
30145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      dstdir_status=$?
30245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    fi
30345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  fi
30445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
30545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  obsolete_mkdir_used=false
30645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
30745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  if test $dstdir_status != 0; then
30845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    case $posix_mkdir in
30945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      '')
31045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	# Create intermediate dirs using mode 755 as modified by the umask.
31145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	# This is like FreeBSD 'install' as of 1997-10-28.
31245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	umask=`umask`
31345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	case $stripcmd.$umask in
31445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  # Optimize common cases.
31545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  *[2367][2367]) mkdir_umask=$umask;;
31645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
31845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  *[0-7])
31945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    mkdir_umask=`expr $umask + 22 \
32045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	      - $umask % 100 % 40 + $umask % 20 \
32145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	      - $umask % 10 % 4 + $umask % 2
32245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    `;;
32345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  *) mkdir_umask=$umask,go-w;;
32445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	esac
32545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
32645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	# With -d, create the new directory with the user-specified mode.
32745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	# Otherwise, rely on $mkdir_umask.
32845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	if test -n "$dir_arg"; then
32945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  mkdir_mode=-m$mode
33045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	else
33145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  mkdir_mode=
33245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	fi
33345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
33445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	posix_mkdir=false
33545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	case $umask in
33645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  *[123567][0-7][0-7])
33745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    ;;
34045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  *)
34145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
34445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    if (umask $mkdir_umask &&
34545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    then
34745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	      if test -z "$dir_arg" || {
34845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   # Check for POSIX incompatibilities with -m.
34945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   # other-writeable bit of parent directory when it shouldn't.
35145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   case $ls_ld_tmpdir in
35445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		     d????-?r-*) different_mode=700;;
35545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		     d????-?--*) different_mode=755;;
35645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		     *) false;;
35745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   esac &&
35845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		   }
36245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org		 }
36345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	      then posix_mkdir=:
36445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	      fi
36545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	      rmdir "$tmpdir/d" "$tmpdir"
36645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    else
36745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	      # Remove any dirs left behind by ancient mkdir implementations.
36845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    fi
37045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    trap '' 0;;
37145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	esac;;
37245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    esac
37345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
37445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if
37545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      $posix_mkdir && (
37645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	umask $mkdir_umask &&
37745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      )
37945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    then :
38045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    else
38145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
38245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      # The umask is ridiculous, or mkdir does not conform to POSIX,
38345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      # or it failed possibly due to a race condition.  Create the
38445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      # directory the slow way, step by step, checking for races as we go.
38545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
38645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      case $dstdir in
38745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	/*) prefix='/';;
38845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	-*) prefix='./';;
38945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	*)  prefix='';;
39045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      esac
39145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
39245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      eval "$initialize_posix_glob"
39345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
39445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      oIFS=$IFS
39545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      IFS=/
39645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      $posix_glob set -f
39745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      set fnord $dstdir
39845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      shift
39945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      $posix_glob set +f
40045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      IFS=$oIFS
40145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
40245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      prefixes=
40345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
40445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      for d
40545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      do
40645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	test -z "$d" && continue
40745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
40845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	prefix=$prefix$d
40945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	if test -d "$prefix"; then
41045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  prefixes=
41145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	else
41245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  if $posix_mkdir; then
41345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    (umask=$mkdir_umask &&
41445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    # Don't fail if two instances are running concurrently.
41645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    test -d "$prefix" || exit 1
41745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  else
41845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    case $prefix in
41945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	      *) qprefix=$prefix;;
42145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    esac
42245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    prefixes="$prefixes '$qprefix'"
42345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  fi
42445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	fi
42545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	prefix=$prefix/
42645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      done
42745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
42845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      if test -n "$prefixes"; then
42945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	# Don't fail if two instances are running concurrently.
43045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	(umask $mkdir_umask &&
43145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  test -d "$dstdir" || exit 1
43345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	obsolete_mkdir_used=true
43445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      fi
43545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    fi
43645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  fi
43745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
43845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  if test -n "$dir_arg"; then
43945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  else
44445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
44545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # Make a couple of temp file names in the proper directory.
44645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    dsttmp=$dstdir/_inst.$$_
44745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    rmtmp=$dstdir/_rm.$$_
44845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
44945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # Trap to clean up those temp files at exit.
45045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
45245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # Copy the file name to the temp name.
45345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
45545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # and set any options; do chmod last to preserve setuid bits.
45645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    #
45745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # If any of these fail, we abort the whole thing.  If we want to
45845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # ignore errors from any of these, just make sure not to ignore
45945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # errors from the above "$doit $cpprog $src $dsttmp" command.
46045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    #
46145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
46645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    # If -C, don't bother to copy if it wouldn't change the file.
46745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    if $copy_on_change &&
46845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
47145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org       eval "$initialize_posix_glob" &&
47245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org       $posix_glob set -f &&
47345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org       set X $old && old=:$2:$4:$5:$6 &&
47445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org       set X $new && new=:$2:$4:$5:$6 &&
47545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org       $posix_glob set +f &&
47645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
47745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org       test "$old" = "$new" &&
47845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    then
48045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      rm -f "$dsttmp"
48145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    else
48245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      # Rename the file to the real destination.
48345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
48545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      # The rename failed, perhaps because mv can't rename something else
48645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      # to itself, or perhaps because mv is so ancient that it does not
48745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      # support -f.
48845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      {
48945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	# Now remove or move aside any old file at destination location.
49045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	# We try this two ways since rm can't unlink itself on some
49145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	# systems and the destination file might be busy for other
49245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	# reasons.  In this case, the final cleanup might fail but the new
49345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	# file should still install successfully.
49445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	{
49545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  test ! -f "$dst" ||
49645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  } ||
50045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  { echo "$0: cannot unlink or rename $dst" >&2
50145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    (exit 1); exit 1
50245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	  }
50345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	} &&
50445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
50545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	# Now rename the file to the real destination.
50645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	$doit $mvcmd "$dsttmp" "$dst"
50745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org      }
50845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    fi || exit 1
50945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
51045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    trap '' 0
51145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  fi
51245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgdone
51345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
51445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# Local variables:
51545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# eval: (add-hook 'write-file-hooks 'time-stamp)
51645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# time-stamp-start: "scriptversion="
51745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# time-stamp-format: "%:y-%02m-%02d.%02H"
518a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org# time-stamp-time-zone: "UTC"
519a1b5233e6d340f45f4846131fec9d0b92e203ce4hbono@chromium.org# time-stamp-end: "; # UTC"
52045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org# End:
521