140dd5a57Smrg#!/bin/sh
240dd5a57Smrg# install - install a program, script, or datafile
340dd5a57Smrg
440dd5a57Smrgscriptversion=2009-04-28.21; # UTC
540dd5a57Smrg
640dd5a57Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
740dd5a57Smrg# later released in X11R6 (xc/config/util/install.sh) with the
840dd5a57Smrg# following copyright and license.
940dd5a57Smrg#
1040dd5a57Smrg# Copyright (C) 1994 X Consortium
1140dd5a57Smrg#
1240dd5a57Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1340dd5a57Smrg# of this software and associated documentation files (the "Software"), to
1440dd5a57Smrg# deal in the Software without restriction, including without limitation the
1540dd5a57Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1640dd5a57Smrg# sell copies of the Software, and to permit persons to whom the Software is
1740dd5a57Smrg# furnished to do so, subject to the following conditions:
1840dd5a57Smrg#
1940dd5a57Smrg# The above copyright notice and this permission notice shall be included in
2040dd5a57Smrg# all copies or substantial portions of the Software.
2140dd5a57Smrg#
2240dd5a57Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2340dd5a57Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2440dd5a57Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2540dd5a57Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2640dd5a57Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2740dd5a57Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2840dd5a57Smrg#
2940dd5a57Smrg# Except as contained in this notice, the name of the X Consortium shall not
3040dd5a57Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3140dd5a57Smrg# ings in this Software without prior written authorization from the X Consor-
3240dd5a57Smrg# tium.
3340dd5a57Smrg#
3440dd5a57Smrg#
3540dd5a57Smrg# FSF changes to this file are in the public domain.
3640dd5a57Smrg#
3740dd5a57Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3840dd5a57Smrg# `make' implicit rules from creating a file called install from it
3940dd5a57Smrg# when there is no Makefile.
4040dd5a57Smrg#
4140dd5a57Smrg# This script is compatible with the BSD install script, but was written
4240dd5a57Smrg# from scratch.
4340dd5a57Smrg
4440dd5a57Smrgnl='
4540dd5a57Smrg'
4640dd5a57SmrgIFS=" ""	$nl"
4740dd5a57Smrg
4840dd5a57Smrg# set DOITPROG to echo to test this script
4940dd5a57Smrg
5040dd5a57Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5140dd5a57Smrgdoit=${DOITPROG-}
5240dd5a57Smrgif test -z "$doit"; then
5340dd5a57Smrg  doit_exec=exec
5440dd5a57Smrgelse
5540dd5a57Smrg  doit_exec=$doit
5640dd5a57Smrgfi
5740dd5a57Smrg
5840dd5a57Smrg# Put in absolute file names if you don't have them in your path;
5940dd5a57Smrg# or use environment vars.
6040dd5a57Smrg
6140dd5a57Smrgchgrpprog=${CHGRPPROG-chgrp}
6240dd5a57Smrgchmodprog=${CHMODPROG-chmod}
6340dd5a57Smrgchownprog=${CHOWNPROG-chown}
6440dd5a57Smrgcmpprog=${CMPPROG-cmp}
6540dd5a57Smrgcpprog=${CPPROG-cp}
6640dd5a57Smrgmkdirprog=${MKDIRPROG-mkdir}
6740dd5a57Smrgmvprog=${MVPROG-mv}
6840dd5a57Smrgrmprog=${RMPROG-rm}
6940dd5a57Smrgstripprog=${STRIPPROG-strip}
7040dd5a57Smrg
7140dd5a57Smrgposix_glob='?'
7240dd5a57Smrginitialize_posix_glob='
7340dd5a57Smrg  test "$posix_glob" != "?" || {
7440dd5a57Smrg    if (set -f) 2>/dev/null; then
7540dd5a57Smrg      posix_glob=
7640dd5a57Smrg    else
7740dd5a57Smrg      posix_glob=:
7840dd5a57Smrg    fi
7940dd5a57Smrg  }
8040dd5a57Smrg'
8140dd5a57Smrg
8240dd5a57Smrgposix_mkdir=
8340dd5a57Smrg
8440dd5a57Smrg# Desired mode of installed file.
8540dd5a57Smrgmode=0755
8640dd5a57Smrg
8740dd5a57Smrgchgrpcmd=
8840dd5a57Smrgchmodcmd=$chmodprog
8940dd5a57Smrgchowncmd=
9040dd5a57Smrgmvcmd=$mvprog
9140dd5a57Smrgrmcmd="$rmprog -f"
9240dd5a57Smrgstripcmd=
9340dd5a57Smrg
9440dd5a57Smrgsrc=
9540dd5a57Smrgdst=
9640dd5a57Smrgdir_arg=
9740dd5a57Smrgdst_arg=
9840dd5a57Smrg
9940dd5a57Smrgcopy_on_change=false
10040dd5a57Smrgno_target_directory=
10140dd5a57Smrg
10240dd5a57Smrgusage="\
10340dd5a57SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10440dd5a57Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10540dd5a57Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10640dd5a57Smrg   or: $0 [OPTION]... -d DIRECTORIES...
10740dd5a57Smrg
10840dd5a57SmrgIn the 1st form, copy SRCFILE to DSTFILE.
10940dd5a57SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11040dd5a57SmrgIn the 4th, create DIRECTORIES.
11140dd5a57Smrg
11240dd5a57SmrgOptions:
11340dd5a57Smrg     --help     display this help and exit.
11440dd5a57Smrg     --version  display version info and exit.
11540dd5a57Smrg
11640dd5a57Smrg  -c            (ignored)
11740dd5a57Smrg  -C            install only if different (preserve the last data modification time)
11840dd5a57Smrg  -d            create directories instead of installing files.
11940dd5a57Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12040dd5a57Smrg  -m MODE       $chmodprog installed files to MODE.
12140dd5a57Smrg  -o USER       $chownprog installed files to USER.
12240dd5a57Smrg  -s            $stripprog installed files.
12340dd5a57Smrg  -t DIRECTORY  install into DIRECTORY.
12440dd5a57Smrg  -T            report an error if DSTFILE is a directory.
12540dd5a57Smrg
12640dd5a57SmrgEnvironment variables override the default commands:
12740dd5a57Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12840dd5a57Smrg  RMPROG STRIPPROG
12940dd5a57Smrg"
13040dd5a57Smrg
13140dd5a57Smrgwhile test $# -ne 0; do
13240dd5a57Smrg  case $1 in
13340dd5a57Smrg    -c) ;;
13440dd5a57Smrg
13540dd5a57Smrg    -C) copy_on_change=true;;
13640dd5a57Smrg
13740dd5a57Smrg    -d) dir_arg=true;;
13840dd5a57Smrg
13940dd5a57Smrg    -g) chgrpcmd="$chgrpprog $2"
14040dd5a57Smrg	shift;;
14140dd5a57Smrg
14240dd5a57Smrg    --help) echo "$usage"; exit $?;;
14340dd5a57Smrg
14440dd5a57Smrg    -m) mode=$2
14540dd5a57Smrg	case $mode in
14640dd5a57Smrg	  *' '* | *'	'* | *'
14740dd5a57Smrg'*	  | *'*'* | *'?'* | *'['*)
14840dd5a57Smrg	    echo "$0: invalid mode: $mode" >&2
14940dd5a57Smrg	    exit 1;;
15040dd5a57Smrg	esac
15140dd5a57Smrg	shift;;
15240dd5a57Smrg
15340dd5a57Smrg    -o) chowncmd="$chownprog $2"
15440dd5a57Smrg	shift;;
15540dd5a57Smrg
15640dd5a57Smrg    -s) stripcmd=$stripprog;;
15740dd5a57Smrg
15840dd5a57Smrg    -t) dst_arg=$2
15940dd5a57Smrg	shift;;
16040dd5a57Smrg
16140dd5a57Smrg    -T) no_target_directory=true;;
16240dd5a57Smrg
16340dd5a57Smrg    --version) echo "$0 $scriptversion"; exit $?;;
16440dd5a57Smrg
16540dd5a57Smrg    --)	shift
16640dd5a57Smrg	break;;
16740dd5a57Smrg
16840dd5a57Smrg    -*)	echo "$0: invalid option: $1" >&2
16940dd5a57Smrg	exit 1;;
17040dd5a57Smrg
17140dd5a57Smrg    *)  break;;
17240dd5a57Smrg  esac
17340dd5a57Smrg  shift
17440dd5a57Smrgdone
17540dd5a57Smrg
17640dd5a57Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17740dd5a57Smrg  # When -d is used, all remaining arguments are directories to create.
17840dd5a57Smrg  # When -t is used, the destination is already specified.
17940dd5a57Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18040dd5a57Smrg  for arg
18140dd5a57Smrg  do
18240dd5a57Smrg    if test -n "$dst_arg"; then
18340dd5a57Smrg      # $@ is not empty: it contains at least $arg.
18440dd5a57Smrg      set fnord "$@" "$dst_arg"
18540dd5a57Smrg      shift # fnord
18640dd5a57Smrg    fi
18740dd5a57Smrg    shift # arg
18840dd5a57Smrg    dst_arg=$arg
18940dd5a57Smrg  done
19040dd5a57Smrgfi
19140dd5a57Smrg
19240dd5a57Smrgif test $# -eq 0; then
19340dd5a57Smrg  if test -z "$dir_arg"; then
19440dd5a57Smrg    echo "$0: no input file specified." >&2
19540dd5a57Smrg    exit 1
19640dd5a57Smrg  fi
19740dd5a57Smrg  # It's OK to call `install-sh -d' without argument.
19840dd5a57Smrg  # This can happen when creating conditional directories.
19940dd5a57Smrg  exit 0
20040dd5a57Smrgfi
20140dd5a57Smrg
20240dd5a57Smrgif test -z "$dir_arg"; then
20340dd5a57Smrg  trap '(exit $?); exit' 1 2 13 15
20440dd5a57Smrg
20540dd5a57Smrg  # Set umask so as not to create temps with too-generous modes.
20640dd5a57Smrg  # However, 'strip' requires both read and write access to temps.
20740dd5a57Smrg  case $mode in
20840dd5a57Smrg    # Optimize common cases.
20940dd5a57Smrg    *644) cp_umask=133;;
21040dd5a57Smrg    *755) cp_umask=22;;
21140dd5a57Smrg
21240dd5a57Smrg    *[0-7])
21340dd5a57Smrg      if test -z "$stripcmd"; then
21440dd5a57Smrg	u_plus_rw=
21540dd5a57Smrg      else
21640dd5a57Smrg	u_plus_rw='% 200'
21740dd5a57Smrg      fi
21840dd5a57Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21940dd5a57Smrg    *)
22040dd5a57Smrg      if test -z "$stripcmd"; then
22140dd5a57Smrg	u_plus_rw=
22240dd5a57Smrg      else
22340dd5a57Smrg	u_plus_rw=,u+rw
22440dd5a57Smrg      fi
22540dd5a57Smrg      cp_umask=$mode$u_plus_rw;;
22640dd5a57Smrg  esac
22740dd5a57Smrgfi
22840dd5a57Smrg
22940dd5a57Smrgfor src
23040dd5a57Smrgdo
23140dd5a57Smrg  # Protect names starting with `-'.
23240dd5a57Smrg  case $src in
23340dd5a57Smrg    -*) src=./$src;;
23440dd5a57Smrg  esac
23540dd5a57Smrg
23640dd5a57Smrg  if test -n "$dir_arg"; then
23740dd5a57Smrg    dst=$src
23840dd5a57Smrg    dstdir=$dst
23940dd5a57Smrg    test -d "$dstdir"
24040dd5a57Smrg    dstdir_status=$?
24140dd5a57Smrg  else
24240dd5a57Smrg
24340dd5a57Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24440dd5a57Smrg    # might cause directories to be created, which would be especially bad
24540dd5a57Smrg    # if $src (and thus $dsttmp) contains '*'.
24640dd5a57Smrg    if test ! -f "$src" && test ! -d "$src"; then
24740dd5a57Smrg      echo "$0: $src does not exist." >&2
24840dd5a57Smrg      exit 1
24940dd5a57Smrg    fi
25040dd5a57Smrg
25140dd5a57Smrg    if test -z "$dst_arg"; then
25240dd5a57Smrg      echo "$0: no destination specified." >&2
25340dd5a57Smrg      exit 1
25440dd5a57Smrg    fi
25540dd5a57Smrg
25640dd5a57Smrg    dst=$dst_arg
25740dd5a57Smrg    # Protect names starting with `-'.
25840dd5a57Smrg    case $dst in
25940dd5a57Smrg      -*) dst=./$dst;;
26040dd5a57Smrg    esac
26140dd5a57Smrg
26240dd5a57Smrg    # If destination is a directory, append the input filename; won't work
26340dd5a57Smrg    # if double slashes aren't ignored.
26440dd5a57Smrg    if test -d "$dst"; then
26540dd5a57Smrg      if test -n "$no_target_directory"; then
26640dd5a57Smrg	echo "$0: $dst_arg: Is a directory" >&2
26740dd5a57Smrg	exit 1
26840dd5a57Smrg      fi
26940dd5a57Smrg      dstdir=$dst
27040dd5a57Smrg      dst=$dstdir/`basename "$src"`
27140dd5a57Smrg      dstdir_status=0
27240dd5a57Smrg    else
27340dd5a57Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27440dd5a57Smrg      dstdir=`
27540dd5a57Smrg	(dirname "$dst") 2>/dev/null ||
27640dd5a57Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27740dd5a57Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27840dd5a57Smrg	     X"$dst" : 'X\(//\)$' \| \
27940dd5a57Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28040dd5a57Smrg	echo X"$dst" |
28140dd5a57Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28240dd5a57Smrg		   s//\1/
28340dd5a57Smrg		   q
28440dd5a57Smrg		 }
28540dd5a57Smrg		 /^X\(\/\/\)[^/].*/{
28640dd5a57Smrg		   s//\1/
28740dd5a57Smrg		   q
28840dd5a57Smrg		 }
28940dd5a57Smrg		 /^X\(\/\/\)$/{
29040dd5a57Smrg		   s//\1/
29140dd5a57Smrg		   q
29240dd5a57Smrg		 }
29340dd5a57Smrg		 /^X\(\/\).*/{
29440dd5a57Smrg		   s//\1/
29540dd5a57Smrg		   q
29640dd5a57Smrg		 }
29740dd5a57Smrg		 s/.*/./; q'
29840dd5a57Smrg      `
29940dd5a57Smrg
30040dd5a57Smrg      test -d "$dstdir"
30140dd5a57Smrg      dstdir_status=$?
30240dd5a57Smrg    fi
30340dd5a57Smrg  fi
30440dd5a57Smrg
30540dd5a57Smrg  obsolete_mkdir_used=false
30640dd5a57Smrg
30740dd5a57Smrg  if test $dstdir_status != 0; then
30840dd5a57Smrg    case $posix_mkdir in
30940dd5a57Smrg      '')
31040dd5a57Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31140dd5a57Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31240dd5a57Smrg	umask=`umask`
31340dd5a57Smrg	case $stripcmd.$umask in
31440dd5a57Smrg	  # Optimize common cases.
31540dd5a57Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31640dd5a57Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31740dd5a57Smrg
31840dd5a57Smrg	  *[0-7])
31940dd5a57Smrg	    mkdir_umask=`expr $umask + 22 \
32040dd5a57Smrg	      - $umask % 100 % 40 + $umask % 20 \
32140dd5a57Smrg	      - $umask % 10 % 4 + $umask % 2
32240dd5a57Smrg	    `;;
32340dd5a57Smrg	  *) mkdir_umask=$umask,go-w;;
32440dd5a57Smrg	esac
32540dd5a57Smrg
32640dd5a57Smrg	# With -d, create the new directory with the user-specified mode.
32740dd5a57Smrg	# Otherwise, rely on $mkdir_umask.
32840dd5a57Smrg	if test -n "$dir_arg"; then
32940dd5a57Smrg	  mkdir_mode=-m$mode
33040dd5a57Smrg	else
33140dd5a57Smrg	  mkdir_mode=
33240dd5a57Smrg	fi
33340dd5a57Smrg
33440dd5a57Smrg	posix_mkdir=false
33540dd5a57Smrg	case $umask in
33640dd5a57Smrg	  *[123567][0-7][0-7])
33740dd5a57Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33840dd5a57Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33940dd5a57Smrg	    ;;
34040dd5a57Smrg	  *)
34140dd5a57Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34240dd5a57Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34340dd5a57Smrg
34440dd5a57Smrg	    if (umask $mkdir_umask &&
34540dd5a57Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34640dd5a57Smrg	    then
34740dd5a57Smrg	      if test -z "$dir_arg" || {
34840dd5a57Smrg		   # Check for POSIX incompatibilities with -m.
34940dd5a57Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35040dd5a57Smrg		   # other-writeable bit of parent directory when it shouldn't.
35140dd5a57Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35240dd5a57Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35340dd5a57Smrg		   case $ls_ld_tmpdir in
35440dd5a57Smrg		     d????-?r-*) different_mode=700;;
35540dd5a57Smrg		     d????-?--*) different_mode=755;;
35640dd5a57Smrg		     *) false;;
35740dd5a57Smrg		   esac &&
35840dd5a57Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35940dd5a57Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36040dd5a57Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36140dd5a57Smrg		   }
36240dd5a57Smrg		 }
36340dd5a57Smrg	      then posix_mkdir=:
36440dd5a57Smrg	      fi
36540dd5a57Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36640dd5a57Smrg	    else
36740dd5a57Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36840dd5a57Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36940dd5a57Smrg	    fi
37040dd5a57Smrg	    trap '' 0;;
37140dd5a57Smrg	esac;;
37240dd5a57Smrg    esac
37340dd5a57Smrg
37440dd5a57Smrg    if
37540dd5a57Smrg      $posix_mkdir && (
37640dd5a57Smrg	umask $mkdir_umask &&
37740dd5a57Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37840dd5a57Smrg      )
37940dd5a57Smrg    then :
38040dd5a57Smrg    else
38140dd5a57Smrg
38240dd5a57Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38340dd5a57Smrg      # or it failed possibly due to a race condition.  Create the
38440dd5a57Smrg      # directory the slow way, step by step, checking for races as we go.
38540dd5a57Smrg
38640dd5a57Smrg      case $dstdir in
38740dd5a57Smrg	/*) prefix='/';;
38840dd5a57Smrg	-*) prefix='./';;
38940dd5a57Smrg	*)  prefix='';;
39040dd5a57Smrg      esac
39140dd5a57Smrg
39240dd5a57Smrg      eval "$initialize_posix_glob"
39340dd5a57Smrg
39440dd5a57Smrg      oIFS=$IFS
39540dd5a57Smrg      IFS=/
39640dd5a57Smrg      $posix_glob set -f
39740dd5a57Smrg      set fnord $dstdir
39840dd5a57Smrg      shift
39940dd5a57Smrg      $posix_glob set +f
40040dd5a57Smrg      IFS=$oIFS
40140dd5a57Smrg
40240dd5a57Smrg      prefixes=
40340dd5a57Smrg
40440dd5a57Smrg      for d
40540dd5a57Smrg      do
40640dd5a57Smrg	test -z "$d" && continue
40740dd5a57Smrg
40840dd5a57Smrg	prefix=$prefix$d
40940dd5a57Smrg	if test -d "$prefix"; then
41040dd5a57Smrg	  prefixes=
41140dd5a57Smrg	else
41240dd5a57Smrg	  if $posix_mkdir; then
41340dd5a57Smrg	    (umask=$mkdir_umask &&
41440dd5a57Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41540dd5a57Smrg	    # Don't fail if two instances are running concurrently.
41640dd5a57Smrg	    test -d "$prefix" || exit 1
41740dd5a57Smrg	  else
41840dd5a57Smrg	    case $prefix in
41940dd5a57Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42040dd5a57Smrg	      *) qprefix=$prefix;;
42140dd5a57Smrg	    esac
42240dd5a57Smrg	    prefixes="$prefixes '$qprefix'"
42340dd5a57Smrg	  fi
42440dd5a57Smrg	fi
42540dd5a57Smrg	prefix=$prefix/
42640dd5a57Smrg      done
42740dd5a57Smrg
42840dd5a57Smrg      if test -n "$prefixes"; then
42940dd5a57Smrg	# Don't fail if two instances are running concurrently.
43040dd5a57Smrg	(umask $mkdir_umask &&
43140dd5a57Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43240dd5a57Smrg	  test -d "$dstdir" || exit 1
43340dd5a57Smrg	obsolete_mkdir_used=true
43440dd5a57Smrg      fi
43540dd5a57Smrg    fi
43640dd5a57Smrg  fi
43740dd5a57Smrg
43840dd5a57Smrg  if test -n "$dir_arg"; then
43940dd5a57Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44040dd5a57Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44140dd5a57Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44240dd5a57Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44340dd5a57Smrg  else
44440dd5a57Smrg
44540dd5a57Smrg    # Make a couple of temp file names in the proper directory.
44640dd5a57Smrg    dsttmp=$dstdir/_inst.$$_
44740dd5a57Smrg    rmtmp=$dstdir/_rm.$$_
44840dd5a57Smrg
44940dd5a57Smrg    # Trap to clean up those temp files at exit.
45040dd5a57Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45140dd5a57Smrg
45240dd5a57Smrg    # Copy the file name to the temp name.
45340dd5a57Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45440dd5a57Smrg
45540dd5a57Smrg    # and set any options; do chmod last to preserve setuid bits.
45640dd5a57Smrg    #
45740dd5a57Smrg    # If any of these fail, we abort the whole thing.  If we want to
45840dd5a57Smrg    # ignore errors from any of these, just make sure not to ignore
45940dd5a57Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46040dd5a57Smrg    #
46140dd5a57Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46240dd5a57Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46340dd5a57Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46440dd5a57Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46540dd5a57Smrg
46640dd5a57Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46740dd5a57Smrg    if $copy_on_change &&
46840dd5a57Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46940dd5a57Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47040dd5a57Smrg
47140dd5a57Smrg       eval "$initialize_posix_glob" &&
47240dd5a57Smrg       $posix_glob set -f &&
47340dd5a57Smrg       set X $old && old=:$2:$4:$5:$6 &&
47440dd5a57Smrg       set X $new && new=:$2:$4:$5:$6 &&
47540dd5a57Smrg       $posix_glob set +f &&
47640dd5a57Smrg
47740dd5a57Smrg       test "$old" = "$new" &&
47840dd5a57Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47940dd5a57Smrg    then
48040dd5a57Smrg      rm -f "$dsttmp"
48140dd5a57Smrg    else
48240dd5a57Smrg      # Rename the file to the real destination.
48340dd5a57Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48440dd5a57Smrg
48540dd5a57Smrg      # The rename failed, perhaps because mv can't rename something else
48640dd5a57Smrg      # to itself, or perhaps because mv is so ancient that it does not
48740dd5a57Smrg      # support -f.
48840dd5a57Smrg      {
48940dd5a57Smrg	# Now remove or move aside any old file at destination location.
49040dd5a57Smrg	# We try this two ways since rm can't unlink itself on some
49140dd5a57Smrg	# systems and the destination file might be busy for other
49240dd5a57Smrg	# reasons.  In this case, the final cleanup might fail but the new
49340dd5a57Smrg	# file should still install successfully.
49440dd5a57Smrg	{
49540dd5a57Smrg	  test ! -f "$dst" ||
49640dd5a57Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49740dd5a57Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49840dd5a57Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49940dd5a57Smrg	  } ||
50040dd5a57Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50140dd5a57Smrg	    (exit 1); exit 1
50240dd5a57Smrg	  }
50340dd5a57Smrg	} &&
50440dd5a57Smrg
50540dd5a57Smrg	# Now rename the file to the real destination.
50640dd5a57Smrg	$doit $mvcmd "$dsttmp" "$dst"
50740dd5a57Smrg      }
50840dd5a57Smrg    fi || exit 1
50940dd5a57Smrg
51040dd5a57Smrg    trap '' 0
51140dd5a57Smrg  fi
51240dd5a57Smrgdone
51340dd5a57Smrg
51440dd5a57Smrg# Local variables:
51540dd5a57Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51640dd5a57Smrg# time-stamp-start: "scriptversion="
51740dd5a57Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51840dd5a57Smrg# time-stamp-time-zone: "UTC"
51940dd5a57Smrg# time-stamp-end: "; # UTC"
52040dd5a57Smrg# End:
521