install-sh revision 7c5f6000
1bccedf53Smrg#!/bin/sh
2bccedf53Smrg# install - install a program, script, or datafile
37c5f6000Smrg
47c5f6000Smrgscriptversion=2009-04-28.21; # UTC
57c5f6000Smrg
6bccedf53Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7bccedf53Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8bccedf53Smrg# following copyright and license.
9bccedf53Smrg#
10bccedf53Smrg# Copyright (C) 1994 X Consortium
11bccedf53Smrg#
12bccedf53Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13bccedf53Smrg# of this software and associated documentation files (the "Software"), to
14bccedf53Smrg# deal in the Software without restriction, including without limitation the
15bccedf53Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16bccedf53Smrg# sell copies of the Software, and to permit persons to whom the Software is
17bccedf53Smrg# furnished to do so, subject to the following conditions:
18bccedf53Smrg#
19bccedf53Smrg# The above copyright notice and this permission notice shall be included in
20bccedf53Smrg# all copies or substantial portions of the Software.
21bccedf53Smrg#
22bccedf53Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23bccedf53Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24bccedf53Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25bccedf53Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26bccedf53Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27bccedf53Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28bccedf53Smrg#
29bccedf53Smrg# Except as contained in this notice, the name of the X Consortium shall not
30bccedf53Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31bccedf53Smrg# ings in this Software without prior written authorization from the X Consor-
32bccedf53Smrg# tium.
33bccedf53Smrg#
34bccedf53Smrg#
35bccedf53Smrg# FSF changes to this file are in the public domain.
36bccedf53Smrg#
37bccedf53Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38bccedf53Smrg# `make' implicit rules from creating a file called install from it
39bccedf53Smrg# when there is no Makefile.
40bccedf53Smrg#
41bccedf53Smrg# This script is compatible with the BSD install script, but was written
427c5f6000Smrg# from scratch.
43bccedf53Smrg
447c5f6000Smrgnl='
457c5f6000Smrg'
467c5f6000SmrgIFS=" ""	$nl"
47bccedf53Smrg
48bccedf53Smrg# set DOITPROG to echo to test this script
49bccedf53Smrg
50bccedf53Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
517c5f6000Smrgdoit=${DOITPROG-}
527c5f6000Smrgif test -z "$doit"; then
537c5f6000Smrg  doit_exec=exec
54bccedf53Smrgelse
557c5f6000Smrg  doit_exec=$doit
56bccedf53Smrgfi
57bccedf53Smrg
587c5f6000Smrg# Put in absolute file names if you don't have them in your path;
597c5f6000Smrg# or use environment vars.
607c5f6000Smrg
617c5f6000Smrgchgrpprog=${CHGRPPROG-chgrp}
627c5f6000Smrgchmodprog=${CHMODPROG-chmod}
637c5f6000Smrgchownprog=${CHOWNPROG-chown}
647c5f6000Smrgcmpprog=${CMPPROG-cmp}
657c5f6000Smrgcpprog=${CPPROG-cp}
667c5f6000Smrgmkdirprog=${MKDIRPROG-mkdir}
677c5f6000Smrgmvprog=${MVPROG-mv}
687c5f6000Smrgrmprog=${RMPROG-rm}
697c5f6000Smrgstripprog=${STRIPPROG-strip}
707c5f6000Smrg
717c5f6000Smrgposix_glob='?'
727c5f6000Smrginitialize_posix_glob='
737c5f6000Smrg  test "$posix_glob" != "?" || {
747c5f6000Smrg    if (set -f) 2>/dev/null; then
757c5f6000Smrg      posix_glob=
767c5f6000Smrg    else
777c5f6000Smrg      posix_glob=:
787c5f6000Smrg    fi
797c5f6000Smrg  }
807c5f6000Smrg'
817c5f6000Smrg
827c5f6000Smrgposix_mkdir=
837c5f6000Smrg
847c5f6000Smrg# Desired mode of installed file.
857c5f6000Smrgmode=0755
867c5f6000Smrg
877c5f6000Smrgchgrpcmd=
887c5f6000Smrgchmodcmd=$chmodprog
897c5f6000Smrgchowncmd=
907c5f6000Smrgmvcmd=$mvprog
917c5f6000Smrgrmcmd="$rmprog -f"
927c5f6000Smrgstripcmd=
93bccedf53Smrg
947c5f6000Smrgsrc=
957c5f6000Smrgdst=
967c5f6000Smrgdir_arg=
977c5f6000Smrgdst_arg=
98bccedf53Smrg
997c5f6000Smrgcopy_on_change=false
1007c5f6000Smrgno_target_directory=
101bccedf53Smrg
1027c5f6000Smrgusage="\
1037c5f6000SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1047c5f6000Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1057c5f6000Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1067c5f6000Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107bccedf53Smrg
1087c5f6000SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1097c5f6000SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1107c5f6000SmrgIn the 4th, create DIRECTORIES.
111bccedf53Smrg
1127c5f6000SmrgOptions:
1137c5f6000Smrg     --help     display this help and exit.
1147c5f6000Smrg     --version  display version info and exit.
115bccedf53Smrg
1167c5f6000Smrg  -c            (ignored)
1177c5f6000Smrg  -C            install only if different (preserve the last data modification time)
1187c5f6000Smrg  -d            create directories instead of installing files.
1197c5f6000Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1207c5f6000Smrg  -m MODE       $chmodprog installed files to MODE.
1217c5f6000Smrg  -o USER       $chownprog installed files to USER.
1227c5f6000Smrg  -s            $stripprog installed files.
1237c5f6000Smrg  -t DIRECTORY  install into DIRECTORY.
1247c5f6000Smrg  -T            report an error if DSTFILE is a directory.
125bccedf53Smrg
1267c5f6000SmrgEnvironment variables override the default commands:
1277c5f6000Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1287c5f6000Smrg  RMPROG STRIPPROG
1297c5f6000Smrg"
130bccedf53Smrg
1317c5f6000Smrgwhile test $# -ne 0; do
1327c5f6000Smrg  case $1 in
1337c5f6000Smrg    -c) ;;
134bccedf53Smrg
1357c5f6000Smrg    -C) copy_on_change=true;;
136bccedf53Smrg
1377c5f6000Smrg    -d) dir_arg=true;;
138bccedf53Smrg
1397c5f6000Smrg    -g) chgrpcmd="$chgrpprog $2"
1407c5f6000Smrg	shift;;
141bccedf53Smrg
1427c5f6000Smrg    --help) echo "$usage"; exit $?;;
143bccedf53Smrg
1447c5f6000Smrg    -m) mode=$2
1457c5f6000Smrg	case $mode in
1467c5f6000Smrg	  *' '* | *'	'* | *'
1477c5f6000Smrg'*	  | *'*'* | *'?'* | *'['*)
1487c5f6000Smrg	    echo "$0: invalid mode: $mode" >&2
1497c5f6000Smrg	    exit 1;;
1507c5f6000Smrg	esac
1517c5f6000Smrg	shift;;
152bccedf53Smrg
1537c5f6000Smrg    -o) chowncmd="$chownprog $2"
1547c5f6000Smrg	shift;;
155bccedf53Smrg
1567c5f6000Smrg    -s) stripcmd=$stripprog;;
157bccedf53Smrg
1587c5f6000Smrg    -t) dst_arg=$2
1597c5f6000Smrg	shift;;
160bccedf53Smrg
1617c5f6000Smrg    -T) no_target_directory=true;;
162bccedf53Smrg
1637c5f6000Smrg    --version) echo "$0 $scriptversion"; exit $?;;
164bccedf53Smrg
1657c5f6000Smrg    --)	shift
1667c5f6000Smrg	break;;
167bccedf53Smrg
1687c5f6000Smrg    -*)	echo "$0: invalid option: $1" >&2
1697c5f6000Smrg	exit 1;;
170bccedf53Smrg
1717c5f6000Smrg    *)  break;;
1727c5f6000Smrg  esac
1737c5f6000Smrg  shift
1747c5f6000Smrgdone
175bccedf53Smrg
1767c5f6000Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1777c5f6000Smrg  # When -d is used, all remaining arguments are directories to create.
1787c5f6000Smrg  # When -t is used, the destination is already specified.
1797c5f6000Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1807c5f6000Smrg  for arg
1817c5f6000Smrg  do
1827c5f6000Smrg    if test -n "$dst_arg"; then
1837c5f6000Smrg      # $@ is not empty: it contains at least $arg.
1847c5f6000Smrg      set fnord "$@" "$dst_arg"
1857c5f6000Smrg      shift # fnord
1867c5f6000Smrg    fi
1877c5f6000Smrg    shift # arg
1887c5f6000Smrg    dst_arg=$arg
1897c5f6000Smrg  done
1907c5f6000Smrgfi
191bccedf53Smrg
1927c5f6000Smrgif test $# -eq 0; then
1937c5f6000Smrg  if test -z "$dir_arg"; then
1947c5f6000Smrg    echo "$0: no input file specified." >&2
1957c5f6000Smrg    exit 1
1967c5f6000Smrg  fi
1977c5f6000Smrg  # It's OK to call `install-sh -d' without argument.
1987c5f6000Smrg  # This can happen when creating conditional directories.
1997c5f6000Smrg  exit 0
2007c5f6000Smrgfi
201bccedf53Smrg
2027c5f6000Smrgif test -z "$dir_arg"; then
2037c5f6000Smrg  trap '(exit $?); exit' 1 2 13 15
2047c5f6000Smrg
2057c5f6000Smrg  # Set umask so as not to create temps with too-generous modes.
2067c5f6000Smrg  # However, 'strip' requires both read and write access to temps.
2077c5f6000Smrg  case $mode in
2087c5f6000Smrg    # Optimize common cases.
2097c5f6000Smrg    *644) cp_umask=133;;
2107c5f6000Smrg    *755) cp_umask=22;;
2117c5f6000Smrg
2127c5f6000Smrg    *[0-7])
2137c5f6000Smrg      if test -z "$stripcmd"; then
2147c5f6000Smrg	u_plus_rw=
2157c5f6000Smrg      else
2167c5f6000Smrg	u_plus_rw='% 200'
2177c5f6000Smrg      fi
2187c5f6000Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2197c5f6000Smrg    *)
2207c5f6000Smrg      if test -z "$stripcmd"; then
2217c5f6000Smrg	u_plus_rw=
2227c5f6000Smrg      else
2237c5f6000Smrg	u_plus_rw=,u+rw
2247c5f6000Smrg      fi
2257c5f6000Smrg      cp_umask=$mode$u_plus_rw;;
2267c5f6000Smrg  esac
2277c5f6000Smrgfi
228bccedf53Smrg
2297c5f6000Smrgfor src
2307c5f6000Smrgdo
2317c5f6000Smrg  # Protect names starting with `-'.
2327c5f6000Smrg  case $src in
2337c5f6000Smrg    -*) src=./$src;;
2347c5f6000Smrg  esac
2357c5f6000Smrg
2367c5f6000Smrg  if test -n "$dir_arg"; then
2377c5f6000Smrg    dst=$src
2387c5f6000Smrg    dstdir=$dst
2397c5f6000Smrg    test -d "$dstdir"
2407c5f6000Smrg    dstdir_status=$?
2417c5f6000Smrg  else
2427c5f6000Smrg
2437c5f6000Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2447c5f6000Smrg    # might cause directories to be created, which would be especially bad
2457c5f6000Smrg    # if $src (and thus $dsttmp) contains '*'.
2467c5f6000Smrg    if test ! -f "$src" && test ! -d "$src"; then
2477c5f6000Smrg      echo "$0: $src does not exist." >&2
2487c5f6000Smrg      exit 1
2497c5f6000Smrg    fi
2507c5f6000Smrg
2517c5f6000Smrg    if test -z "$dst_arg"; then
2527c5f6000Smrg      echo "$0: no destination specified." >&2
2537c5f6000Smrg      exit 1
2547c5f6000Smrg    fi
2557c5f6000Smrg
2567c5f6000Smrg    dst=$dst_arg
2577c5f6000Smrg    # Protect names starting with `-'.
2587c5f6000Smrg    case $dst in
2597c5f6000Smrg      -*) dst=./$dst;;
2607c5f6000Smrg    esac
261bccedf53Smrg
2627c5f6000Smrg    # If destination is a directory, append the input filename; won't work
2637c5f6000Smrg    # if double slashes aren't ignored.
2647c5f6000Smrg    if test -d "$dst"; then
2657c5f6000Smrg      if test -n "$no_target_directory"; then
2667c5f6000Smrg	echo "$0: $dst_arg: Is a directory" >&2
2677c5f6000Smrg	exit 1
2687c5f6000Smrg      fi
2697c5f6000Smrg      dstdir=$dst
2707c5f6000Smrg      dst=$dstdir/`basename "$src"`
2717c5f6000Smrg      dstdir_status=0
2727c5f6000Smrg    else
2737c5f6000Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2747c5f6000Smrg      dstdir=`
2757c5f6000Smrg	(dirname "$dst") 2>/dev/null ||
2767c5f6000Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2777c5f6000Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2787c5f6000Smrg	     X"$dst" : 'X\(//\)$' \| \
2797c5f6000Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2807c5f6000Smrg	echo X"$dst" |
2817c5f6000Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2827c5f6000Smrg		   s//\1/
2837c5f6000Smrg		   q
2847c5f6000Smrg		 }
2857c5f6000Smrg		 /^X\(\/\/\)[^/].*/{
2867c5f6000Smrg		   s//\1/
2877c5f6000Smrg		   q
2887c5f6000Smrg		 }
2897c5f6000Smrg		 /^X\(\/\/\)$/{
2907c5f6000Smrg		   s//\1/
2917c5f6000Smrg		   q
2927c5f6000Smrg		 }
2937c5f6000Smrg		 /^X\(\/\).*/{
2947c5f6000Smrg		   s//\1/
2957c5f6000Smrg		   q
2967c5f6000Smrg		 }
2977c5f6000Smrg		 s/.*/./; q'
2987c5f6000Smrg      `
2997c5f6000Smrg
3007c5f6000Smrg      test -d "$dstdir"
3017c5f6000Smrg      dstdir_status=$?
3027c5f6000Smrg    fi
3037c5f6000Smrg  fi
3047c5f6000Smrg
3057c5f6000Smrg  obsolete_mkdir_used=false
3067c5f6000Smrg
3077c5f6000Smrg  if test $dstdir_status != 0; then
3087c5f6000Smrg    case $posix_mkdir in
3097c5f6000Smrg      '')
3107c5f6000Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3117c5f6000Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3127c5f6000Smrg	umask=`umask`
3137c5f6000Smrg	case $stripcmd.$umask in
3147c5f6000Smrg	  # Optimize common cases.
3157c5f6000Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3167c5f6000Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3177c5f6000Smrg
3187c5f6000Smrg	  *[0-7])
3197c5f6000Smrg	    mkdir_umask=`expr $umask + 22 \
3207c5f6000Smrg	      - $umask % 100 % 40 + $umask % 20 \
3217c5f6000Smrg	      - $umask % 10 % 4 + $umask % 2
3227c5f6000Smrg	    `;;
3237c5f6000Smrg	  *) mkdir_umask=$umask,go-w;;
3247c5f6000Smrg	esac
3257c5f6000Smrg
3267c5f6000Smrg	# With -d, create the new directory with the user-specified mode.
3277c5f6000Smrg	# Otherwise, rely on $mkdir_umask.
3287c5f6000Smrg	if test -n "$dir_arg"; then
3297c5f6000Smrg	  mkdir_mode=-m$mode
330bccedf53Smrg	else
3317c5f6000Smrg	  mkdir_mode=
332bccedf53Smrg	fi
333bccedf53Smrg
3347c5f6000Smrg	posix_mkdir=false
3357c5f6000Smrg	case $umask in
3367c5f6000Smrg	  *[123567][0-7][0-7])
3377c5f6000Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3387c5f6000Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3397c5f6000Smrg	    ;;
3407c5f6000Smrg	  *)
3417c5f6000Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3427c5f6000Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3437c5f6000Smrg
3447c5f6000Smrg	    if (umask $mkdir_umask &&
3457c5f6000Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3467c5f6000Smrg	    then
3477c5f6000Smrg	      if test -z "$dir_arg" || {
3487c5f6000Smrg		   # Check for POSIX incompatibilities with -m.
3497c5f6000Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3507c5f6000Smrg		   # other-writeable bit of parent directory when it shouldn't.
3517c5f6000Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3527c5f6000Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3537c5f6000Smrg		   case $ls_ld_tmpdir in
3547c5f6000Smrg		     d????-?r-*) different_mode=700;;
3557c5f6000Smrg		     d????-?--*) different_mode=755;;
3567c5f6000Smrg		     *) false;;
3577c5f6000Smrg		   esac &&
3587c5f6000Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3597c5f6000Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3607c5f6000Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3617c5f6000Smrg		   }
3627c5f6000Smrg		 }
3637c5f6000Smrg	      then posix_mkdir=:
3647c5f6000Smrg	      fi
3657c5f6000Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3667c5f6000Smrg	    else
3677c5f6000Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3687c5f6000Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3697c5f6000Smrg	    fi
3707c5f6000Smrg	    trap '' 0;;
3717c5f6000Smrg	esac;;
3727c5f6000Smrg    esac
373bccedf53Smrg
3747c5f6000Smrg    if
3757c5f6000Smrg      $posix_mkdir && (
3767c5f6000Smrg	umask $mkdir_umask &&
3777c5f6000Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3787c5f6000Smrg      )
3797c5f6000Smrg    then :
3807c5f6000Smrg    else
3817c5f6000Smrg
3827c5f6000Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3837c5f6000Smrg      # or it failed possibly due to a race condition.  Create the
3847c5f6000Smrg      # directory the slow way, step by step, checking for races as we go.
3857c5f6000Smrg
3867c5f6000Smrg      case $dstdir in
3877c5f6000Smrg	/*) prefix='/';;
3887c5f6000Smrg	-*) prefix='./';;
3897c5f6000Smrg	*)  prefix='';;
3907c5f6000Smrg      esac
3917c5f6000Smrg
3927c5f6000Smrg      eval "$initialize_posix_glob"
3937c5f6000Smrg
3947c5f6000Smrg      oIFS=$IFS
3957c5f6000Smrg      IFS=/
3967c5f6000Smrg      $posix_glob set -f
3977c5f6000Smrg      set fnord $dstdir
3987c5f6000Smrg      shift
3997c5f6000Smrg      $posix_glob set +f
4007c5f6000Smrg      IFS=$oIFS
4017c5f6000Smrg
4027c5f6000Smrg      prefixes=
4037c5f6000Smrg
4047c5f6000Smrg      for d
4057c5f6000Smrg      do
4067c5f6000Smrg	test -z "$d" && continue
4077c5f6000Smrg
4087c5f6000Smrg	prefix=$prefix$d
4097c5f6000Smrg	if test -d "$prefix"; then
4107c5f6000Smrg	  prefixes=
4117c5f6000Smrg	else
4127c5f6000Smrg	  if $posix_mkdir; then
4137c5f6000Smrg	    (umask=$mkdir_umask &&
4147c5f6000Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4157c5f6000Smrg	    # Don't fail if two instances are running concurrently.
4167c5f6000Smrg	    test -d "$prefix" || exit 1
4177c5f6000Smrg	  else
4187c5f6000Smrg	    case $prefix in
4197c5f6000Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4207c5f6000Smrg	      *) qprefix=$prefix;;
4217c5f6000Smrg	    esac
4227c5f6000Smrg	    prefixes="$prefixes '$qprefix'"
4237c5f6000Smrg	  fi
4247c5f6000Smrg	fi
4257c5f6000Smrg	prefix=$prefix/
4267c5f6000Smrg      done
4277c5f6000Smrg
4287c5f6000Smrg      if test -n "$prefixes"; then
4297c5f6000Smrg	# Don't fail if two instances are running concurrently.
4307c5f6000Smrg	(umask $mkdir_umask &&
4317c5f6000Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4327c5f6000Smrg	  test -d "$dstdir" || exit 1
4337c5f6000Smrg	obsolete_mkdir_used=true
4347c5f6000Smrg      fi
4357c5f6000Smrg    fi
4367c5f6000Smrg  fi
4377c5f6000Smrg
4387c5f6000Smrg  if test -n "$dir_arg"; then
4397c5f6000Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4407c5f6000Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4417c5f6000Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4427c5f6000Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4437c5f6000Smrg  else
4447c5f6000Smrg
4457c5f6000Smrg    # Make a couple of temp file names in the proper directory.
4467c5f6000Smrg    dsttmp=$dstdir/_inst.$$_
4477c5f6000Smrg    rmtmp=$dstdir/_rm.$$_
4487c5f6000Smrg
4497c5f6000Smrg    # Trap to clean up those temp files at exit.
4507c5f6000Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4517c5f6000Smrg
4527c5f6000Smrg    # Copy the file name to the temp name.
4537c5f6000Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4547c5f6000Smrg
4557c5f6000Smrg    # and set any options; do chmod last to preserve setuid bits.
4567c5f6000Smrg    #
4577c5f6000Smrg    # If any of these fail, we abort the whole thing.  If we want to
4587c5f6000Smrg    # ignore errors from any of these, just make sure not to ignore
4597c5f6000Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4607c5f6000Smrg    #
4617c5f6000Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4627c5f6000Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4637c5f6000Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4647c5f6000Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4657c5f6000Smrg
4667c5f6000Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4677c5f6000Smrg    if $copy_on_change &&
4687c5f6000Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4697c5f6000Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4707c5f6000Smrg
4717c5f6000Smrg       eval "$initialize_posix_glob" &&
4727c5f6000Smrg       $posix_glob set -f &&
4737c5f6000Smrg       set X $old && old=:$2:$4:$5:$6 &&
4747c5f6000Smrg       set X $new && new=:$2:$4:$5:$6 &&
4757c5f6000Smrg       $posix_glob set +f &&
4767c5f6000Smrg
4777c5f6000Smrg       test "$old" = "$new" &&
4787c5f6000Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4797c5f6000Smrg    then
4807c5f6000Smrg      rm -f "$dsttmp"
4817c5f6000Smrg    else
4827c5f6000Smrg      # Rename the file to the real destination.
4837c5f6000Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4847c5f6000Smrg
4857c5f6000Smrg      # The rename failed, perhaps because mv can't rename something else
4867c5f6000Smrg      # to itself, or perhaps because mv is so ancient that it does not
4877c5f6000Smrg      # support -f.
4887c5f6000Smrg      {
4897c5f6000Smrg	# Now remove or move aside any old file at destination location.
4907c5f6000Smrg	# We try this two ways since rm can't unlink itself on some
4917c5f6000Smrg	# systems and the destination file might be busy for other
4927c5f6000Smrg	# reasons.  In this case, the final cleanup might fail but the new
4937c5f6000Smrg	# file should still install successfully.
4947c5f6000Smrg	{
4957c5f6000Smrg	  test ! -f "$dst" ||
4967c5f6000Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4977c5f6000Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4987c5f6000Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4997c5f6000Smrg	  } ||
5007c5f6000Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5017c5f6000Smrg	    (exit 1); exit 1
5027c5f6000Smrg	  }
5037c5f6000Smrg	} &&
5047c5f6000Smrg
5057c5f6000Smrg	# Now rename the file to the real destination.
5067c5f6000Smrg	$doit $mvcmd "$dsttmp" "$dst"
5077c5f6000Smrg      }
5087c5f6000Smrg    fi || exit 1
5097c5f6000Smrg
5107c5f6000Smrg    trap '' 0
5117c5f6000Smrg  fi
5127c5f6000Smrgdone
513bccedf53Smrg
5147c5f6000Smrg# Local variables:
5157c5f6000Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5167c5f6000Smrg# time-stamp-start: "scriptversion="
5177c5f6000Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5187c5f6000Smrg# time-stamp-time-zone: "UTC"
5197c5f6000Smrg# time-stamp-end: "; # UTC"
5207c5f6000Smrg# End:
521