install-sh revision 8650bb69
12c7c4e3dSmrg#!/bin/sh
22c7c4e3dSmrg# install - install a program, script, or datafile
38650bb69Smrg
48650bb69Smrgscriptversion=2009-04-28.21; # UTC
58650bb69Smrg
62c7c4e3dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
72c7c4e3dSmrg# later released in X11R6 (xc/config/util/install.sh) with the
82c7c4e3dSmrg# following copyright and license.
92c7c4e3dSmrg#
102c7c4e3dSmrg# Copyright (C) 1994 X Consortium
112c7c4e3dSmrg#
122c7c4e3dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
132c7c4e3dSmrg# of this software and associated documentation files (the "Software"), to
142c7c4e3dSmrg# deal in the Software without restriction, including without limitation the
152c7c4e3dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
162c7c4e3dSmrg# sell copies of the Software, and to permit persons to whom the Software is
172c7c4e3dSmrg# furnished to do so, subject to the following conditions:
182c7c4e3dSmrg#
192c7c4e3dSmrg# The above copyright notice and this permission notice shall be included in
202c7c4e3dSmrg# all copies or substantial portions of the Software.
212c7c4e3dSmrg#
222c7c4e3dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
232c7c4e3dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
242c7c4e3dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
252c7c4e3dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
262c7c4e3dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
272c7c4e3dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
282c7c4e3dSmrg#
292c7c4e3dSmrg# Except as contained in this notice, the name of the X Consortium shall not
302c7c4e3dSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
312c7c4e3dSmrg# ings in this Software without prior written authorization from the X Consor-
322c7c4e3dSmrg# tium.
332c7c4e3dSmrg#
342c7c4e3dSmrg#
352c7c4e3dSmrg# FSF changes to this file are in the public domain.
362c7c4e3dSmrg#
372c7c4e3dSmrg# Calling this script install-sh is preferred over install.sh, to prevent
382c7c4e3dSmrg# `make' implicit rules from creating a file called install from it
392c7c4e3dSmrg# when there is no Makefile.
402c7c4e3dSmrg#
412c7c4e3dSmrg# This script is compatible with the BSD install script, but was written
428650bb69Smrg# from scratch.
432c7c4e3dSmrg
448650bb69Smrgnl='
458650bb69Smrg'
468650bb69SmrgIFS=" ""	$nl"
472c7c4e3dSmrg
482c7c4e3dSmrg# set DOITPROG to echo to test this script
492c7c4e3dSmrg
502c7c4e3dSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
518650bb69Smrgdoit=${DOITPROG-}
528650bb69Smrgif test -z "$doit"; then
538650bb69Smrg  doit_exec=exec
542c7c4e3dSmrgelse
558650bb69Smrg  doit_exec=$doit
562c7c4e3dSmrgfi
572c7c4e3dSmrg
588650bb69Smrg# Put in absolute file names if you don't have them in your path;
598650bb69Smrg# or use environment vars.
608650bb69Smrg
618650bb69Smrgchgrpprog=${CHGRPPROG-chgrp}
628650bb69Smrgchmodprog=${CHMODPROG-chmod}
638650bb69Smrgchownprog=${CHOWNPROG-chown}
648650bb69Smrgcmpprog=${CMPPROG-cmp}
658650bb69Smrgcpprog=${CPPROG-cp}
668650bb69Smrgmkdirprog=${MKDIRPROG-mkdir}
678650bb69Smrgmvprog=${MVPROG-mv}
688650bb69Smrgrmprog=${RMPROG-rm}
698650bb69Smrgstripprog=${STRIPPROG-strip}
708650bb69Smrg
718650bb69Smrgposix_glob='?'
728650bb69Smrginitialize_posix_glob='
738650bb69Smrg  test "$posix_glob" != "?" || {
748650bb69Smrg    if (set -f) 2>/dev/null; then
758650bb69Smrg      posix_glob=
768650bb69Smrg    else
778650bb69Smrg      posix_glob=:
788650bb69Smrg    fi
798650bb69Smrg  }
808650bb69Smrg'
818650bb69Smrg
828650bb69Smrgposix_mkdir=
838650bb69Smrg
848650bb69Smrg# Desired mode of installed file.
858650bb69Smrgmode=0755
868650bb69Smrg
878650bb69Smrgchgrpcmd=
888650bb69Smrgchmodcmd=$chmodprog
898650bb69Smrgchowncmd=
908650bb69Smrgmvcmd=$mvprog
918650bb69Smrgrmcmd="$rmprog -f"
928650bb69Smrgstripcmd=
932c7c4e3dSmrg
948650bb69Smrgsrc=
958650bb69Smrgdst=
968650bb69Smrgdir_arg=
978650bb69Smrgdst_arg=
982c7c4e3dSmrg
998650bb69Smrgcopy_on_change=false
1008650bb69Smrgno_target_directory=
1012c7c4e3dSmrg
1028650bb69Smrgusage="\
1038650bb69SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1048650bb69Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1058650bb69Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1068650bb69Smrg   or: $0 [OPTION]... -d DIRECTORIES...
1072c7c4e3dSmrg
1088650bb69SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1098650bb69SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1108650bb69SmrgIn the 4th, create DIRECTORIES.
1112c7c4e3dSmrg
1128650bb69SmrgOptions:
1138650bb69Smrg     --help     display this help and exit.
1148650bb69Smrg     --version  display version info and exit.
1152c7c4e3dSmrg
1168650bb69Smrg  -c            (ignored)
1178650bb69Smrg  -C            install only if different (preserve the last data modification time)
1188650bb69Smrg  -d            create directories instead of installing files.
1198650bb69Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1208650bb69Smrg  -m MODE       $chmodprog installed files to MODE.
1218650bb69Smrg  -o USER       $chownprog installed files to USER.
1228650bb69Smrg  -s            $stripprog installed files.
1238650bb69Smrg  -t DIRECTORY  install into DIRECTORY.
1248650bb69Smrg  -T            report an error if DSTFILE is a directory.
1252c7c4e3dSmrg
1268650bb69SmrgEnvironment variables override the default commands:
1278650bb69Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1288650bb69Smrg  RMPROG STRIPPROG
1298650bb69Smrg"
1302c7c4e3dSmrg
1318650bb69Smrgwhile test $# -ne 0; do
1328650bb69Smrg  case $1 in
1338650bb69Smrg    -c) ;;
1342c7c4e3dSmrg
1358650bb69Smrg    -C) copy_on_change=true;;
1362c7c4e3dSmrg
1378650bb69Smrg    -d) dir_arg=true;;
1382c7c4e3dSmrg
1398650bb69Smrg    -g) chgrpcmd="$chgrpprog $2"
1408650bb69Smrg	shift;;
1412c7c4e3dSmrg
1428650bb69Smrg    --help) echo "$usage"; exit $?;;
1432c7c4e3dSmrg
1448650bb69Smrg    -m) mode=$2
1458650bb69Smrg	case $mode in
1468650bb69Smrg	  *' '* | *'	'* | *'
1478650bb69Smrg'*	  | *'*'* | *'?'* | *'['*)
1488650bb69Smrg	    echo "$0: invalid mode: $mode" >&2
1498650bb69Smrg	    exit 1;;
1508650bb69Smrg	esac
1518650bb69Smrg	shift;;
1522c7c4e3dSmrg
1538650bb69Smrg    -o) chowncmd="$chownprog $2"
1548650bb69Smrg	shift;;
1552c7c4e3dSmrg
1568650bb69Smrg    -s) stripcmd=$stripprog;;
1572c7c4e3dSmrg
1588650bb69Smrg    -t) dst_arg=$2
1598650bb69Smrg	shift;;
1602c7c4e3dSmrg
1618650bb69Smrg    -T) no_target_directory=true;;
1622c7c4e3dSmrg
1638650bb69Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1642c7c4e3dSmrg
1658650bb69Smrg    --)	shift
1668650bb69Smrg	break;;
1672c7c4e3dSmrg
1688650bb69Smrg    -*)	echo "$0: invalid option: $1" >&2
1698650bb69Smrg	exit 1;;
1702c7c4e3dSmrg
1718650bb69Smrg    *)  break;;
1728650bb69Smrg  esac
1738650bb69Smrg  shift
1748650bb69Smrgdone
1752c7c4e3dSmrg
1768650bb69Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1778650bb69Smrg  # When -d is used, all remaining arguments are directories to create.
1788650bb69Smrg  # When -t is used, the destination is already specified.
1798650bb69Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1808650bb69Smrg  for arg
1818650bb69Smrg  do
1828650bb69Smrg    if test -n "$dst_arg"; then
1838650bb69Smrg      # $@ is not empty: it contains at least $arg.
1848650bb69Smrg      set fnord "$@" "$dst_arg"
1858650bb69Smrg      shift # fnord
1868650bb69Smrg    fi
1878650bb69Smrg    shift # arg
1888650bb69Smrg    dst_arg=$arg
1898650bb69Smrg  done
1908650bb69Smrgfi
1912c7c4e3dSmrg
1928650bb69Smrgif test $# -eq 0; then
1938650bb69Smrg  if test -z "$dir_arg"; then
1948650bb69Smrg    echo "$0: no input file specified." >&2
1958650bb69Smrg    exit 1
1968650bb69Smrg  fi
1978650bb69Smrg  # It's OK to call `install-sh -d' without argument.
1988650bb69Smrg  # This can happen when creating conditional directories.
1998650bb69Smrg  exit 0
2008650bb69Smrgfi
2012c7c4e3dSmrg
2028650bb69Smrgif test -z "$dir_arg"; then
2038650bb69Smrg  trap '(exit $?); exit' 1 2 13 15
2048650bb69Smrg
2058650bb69Smrg  # Set umask so as not to create temps with too-generous modes.
2068650bb69Smrg  # However, 'strip' requires both read and write access to temps.
2078650bb69Smrg  case $mode in
2088650bb69Smrg    # Optimize common cases.
2098650bb69Smrg    *644) cp_umask=133;;
2108650bb69Smrg    *755) cp_umask=22;;
2118650bb69Smrg
2128650bb69Smrg    *[0-7])
2138650bb69Smrg      if test -z "$stripcmd"; then
2148650bb69Smrg	u_plus_rw=
2158650bb69Smrg      else
2168650bb69Smrg	u_plus_rw='% 200'
2178650bb69Smrg      fi
2188650bb69Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2198650bb69Smrg    *)
2208650bb69Smrg      if test -z "$stripcmd"; then
2218650bb69Smrg	u_plus_rw=
2228650bb69Smrg      else
2238650bb69Smrg	u_plus_rw=,u+rw
2248650bb69Smrg      fi
2258650bb69Smrg      cp_umask=$mode$u_plus_rw;;
2268650bb69Smrg  esac
2278650bb69Smrgfi
2282c7c4e3dSmrg
2298650bb69Smrgfor src
2308650bb69Smrgdo
2318650bb69Smrg  # Protect names starting with `-'.
2328650bb69Smrg  case $src in
2338650bb69Smrg    -*) src=./$src;;
2348650bb69Smrg  esac
2358650bb69Smrg
2368650bb69Smrg  if test -n "$dir_arg"; then
2378650bb69Smrg    dst=$src
2388650bb69Smrg    dstdir=$dst
2398650bb69Smrg    test -d "$dstdir"
2408650bb69Smrg    dstdir_status=$?
2418650bb69Smrg  else
2428650bb69Smrg
2438650bb69Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2448650bb69Smrg    # might cause directories to be created, which would be especially bad
2458650bb69Smrg    # if $src (and thus $dsttmp) contains '*'.
2468650bb69Smrg    if test ! -f "$src" && test ! -d "$src"; then
2478650bb69Smrg      echo "$0: $src does not exist." >&2
2488650bb69Smrg      exit 1
2498650bb69Smrg    fi
2508650bb69Smrg
2518650bb69Smrg    if test -z "$dst_arg"; then
2528650bb69Smrg      echo "$0: no destination specified." >&2
2538650bb69Smrg      exit 1
2548650bb69Smrg    fi
2558650bb69Smrg
2568650bb69Smrg    dst=$dst_arg
2578650bb69Smrg    # Protect names starting with `-'.
2588650bb69Smrg    case $dst in
2598650bb69Smrg      -*) dst=./$dst;;
2608650bb69Smrg    esac
2612c7c4e3dSmrg
2628650bb69Smrg    # If destination is a directory, append the input filename; won't work
2638650bb69Smrg    # if double slashes aren't ignored.
2648650bb69Smrg    if test -d "$dst"; then
2658650bb69Smrg      if test -n "$no_target_directory"; then
2668650bb69Smrg	echo "$0: $dst_arg: Is a directory" >&2
2678650bb69Smrg	exit 1
2688650bb69Smrg      fi
2698650bb69Smrg      dstdir=$dst
2708650bb69Smrg      dst=$dstdir/`basename "$src"`
2718650bb69Smrg      dstdir_status=0
2728650bb69Smrg    else
2738650bb69Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2748650bb69Smrg      dstdir=`
2758650bb69Smrg	(dirname "$dst") 2>/dev/null ||
2768650bb69Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2778650bb69Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2788650bb69Smrg	     X"$dst" : 'X\(//\)$' \| \
2798650bb69Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2808650bb69Smrg	echo X"$dst" |
2818650bb69Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2828650bb69Smrg		   s//\1/
2838650bb69Smrg		   q
2848650bb69Smrg		 }
2858650bb69Smrg		 /^X\(\/\/\)[^/].*/{
2868650bb69Smrg		   s//\1/
2878650bb69Smrg		   q
2888650bb69Smrg		 }
2898650bb69Smrg		 /^X\(\/\/\)$/{
2908650bb69Smrg		   s//\1/
2918650bb69Smrg		   q
2928650bb69Smrg		 }
2938650bb69Smrg		 /^X\(\/\).*/{
2948650bb69Smrg		   s//\1/
2958650bb69Smrg		   q
2968650bb69Smrg		 }
2978650bb69Smrg		 s/.*/./; q'
2988650bb69Smrg      `
2998650bb69Smrg
3008650bb69Smrg      test -d "$dstdir"
3018650bb69Smrg      dstdir_status=$?
3028650bb69Smrg    fi
3038650bb69Smrg  fi
3048650bb69Smrg
3058650bb69Smrg  obsolete_mkdir_used=false
3068650bb69Smrg
3078650bb69Smrg  if test $dstdir_status != 0; then
3088650bb69Smrg    case $posix_mkdir in
3098650bb69Smrg      '')
3108650bb69Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3118650bb69Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3128650bb69Smrg	umask=`umask`
3138650bb69Smrg	case $stripcmd.$umask in
3148650bb69Smrg	  # Optimize common cases.
3158650bb69Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3168650bb69Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3178650bb69Smrg
3188650bb69Smrg	  *[0-7])
3198650bb69Smrg	    mkdir_umask=`expr $umask + 22 \
3208650bb69Smrg	      - $umask % 100 % 40 + $umask % 20 \
3218650bb69Smrg	      - $umask % 10 % 4 + $umask % 2
3228650bb69Smrg	    `;;
3238650bb69Smrg	  *) mkdir_umask=$umask,go-w;;
3248650bb69Smrg	esac
3258650bb69Smrg
3268650bb69Smrg	# With -d, create the new directory with the user-specified mode.
3278650bb69Smrg	# Otherwise, rely on $mkdir_umask.
3288650bb69Smrg	if test -n "$dir_arg"; then
3298650bb69Smrg	  mkdir_mode=-m$mode
3302c7c4e3dSmrg	else
3318650bb69Smrg	  mkdir_mode=
3322c7c4e3dSmrg	fi
3332c7c4e3dSmrg
3348650bb69Smrg	posix_mkdir=false
3358650bb69Smrg	case $umask in
3368650bb69Smrg	  *[123567][0-7][0-7])
3378650bb69Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3388650bb69Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3398650bb69Smrg	    ;;
3408650bb69Smrg	  *)
3418650bb69Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3428650bb69Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3438650bb69Smrg
3448650bb69Smrg	    if (umask $mkdir_umask &&
3458650bb69Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3468650bb69Smrg	    then
3478650bb69Smrg	      if test -z "$dir_arg" || {
3488650bb69Smrg		   # Check for POSIX incompatibilities with -m.
3498650bb69Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3508650bb69Smrg		   # other-writeable bit of parent directory when it shouldn't.
3518650bb69Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3528650bb69Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3538650bb69Smrg		   case $ls_ld_tmpdir in
3548650bb69Smrg		     d????-?r-*) different_mode=700;;
3558650bb69Smrg		     d????-?--*) different_mode=755;;
3568650bb69Smrg		     *) false;;
3578650bb69Smrg		   esac &&
3588650bb69Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3598650bb69Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3608650bb69Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3618650bb69Smrg		   }
3628650bb69Smrg		 }
3638650bb69Smrg	      then posix_mkdir=:
3648650bb69Smrg	      fi
3658650bb69Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3668650bb69Smrg	    else
3678650bb69Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3688650bb69Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3698650bb69Smrg	    fi
3708650bb69Smrg	    trap '' 0;;
3718650bb69Smrg	esac;;
3728650bb69Smrg    esac
3732c7c4e3dSmrg
3748650bb69Smrg    if
3758650bb69Smrg      $posix_mkdir && (
3768650bb69Smrg	umask $mkdir_umask &&
3778650bb69Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3788650bb69Smrg      )
3798650bb69Smrg    then :
3808650bb69Smrg    else
3818650bb69Smrg
3828650bb69Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3838650bb69Smrg      # or it failed possibly due to a race condition.  Create the
3848650bb69Smrg      # directory the slow way, step by step, checking for races as we go.
3858650bb69Smrg
3868650bb69Smrg      case $dstdir in
3878650bb69Smrg	/*) prefix='/';;
3888650bb69Smrg	-*) prefix='./';;
3898650bb69Smrg	*)  prefix='';;
3908650bb69Smrg      esac
3918650bb69Smrg
3928650bb69Smrg      eval "$initialize_posix_glob"
3938650bb69Smrg
3948650bb69Smrg      oIFS=$IFS
3958650bb69Smrg      IFS=/
3968650bb69Smrg      $posix_glob set -f
3978650bb69Smrg      set fnord $dstdir
3988650bb69Smrg      shift
3998650bb69Smrg      $posix_glob set +f
4008650bb69Smrg      IFS=$oIFS
4018650bb69Smrg
4028650bb69Smrg      prefixes=
4038650bb69Smrg
4048650bb69Smrg      for d
4058650bb69Smrg      do
4068650bb69Smrg	test -z "$d" && continue
4078650bb69Smrg
4088650bb69Smrg	prefix=$prefix$d
4098650bb69Smrg	if test -d "$prefix"; then
4108650bb69Smrg	  prefixes=
4118650bb69Smrg	else
4128650bb69Smrg	  if $posix_mkdir; then
4138650bb69Smrg	    (umask=$mkdir_umask &&
4148650bb69Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4158650bb69Smrg	    # Don't fail if two instances are running concurrently.
4168650bb69Smrg	    test -d "$prefix" || exit 1
4178650bb69Smrg	  else
4188650bb69Smrg	    case $prefix in
4198650bb69Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4208650bb69Smrg	      *) qprefix=$prefix;;
4218650bb69Smrg	    esac
4228650bb69Smrg	    prefixes="$prefixes '$qprefix'"
4238650bb69Smrg	  fi
4248650bb69Smrg	fi
4258650bb69Smrg	prefix=$prefix/
4268650bb69Smrg      done
4278650bb69Smrg
4288650bb69Smrg      if test -n "$prefixes"; then
4298650bb69Smrg	# Don't fail if two instances are running concurrently.
4308650bb69Smrg	(umask $mkdir_umask &&
4318650bb69Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4328650bb69Smrg	  test -d "$dstdir" || exit 1
4338650bb69Smrg	obsolete_mkdir_used=true
4348650bb69Smrg      fi
4358650bb69Smrg    fi
4368650bb69Smrg  fi
4378650bb69Smrg
4388650bb69Smrg  if test -n "$dir_arg"; then
4398650bb69Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4408650bb69Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4418650bb69Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4428650bb69Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4438650bb69Smrg  else
4448650bb69Smrg
4458650bb69Smrg    # Make a couple of temp file names in the proper directory.
4468650bb69Smrg    dsttmp=$dstdir/_inst.$$_
4478650bb69Smrg    rmtmp=$dstdir/_rm.$$_
4488650bb69Smrg
4498650bb69Smrg    # Trap to clean up those temp files at exit.
4508650bb69Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4518650bb69Smrg
4528650bb69Smrg    # Copy the file name to the temp name.
4538650bb69Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4548650bb69Smrg
4558650bb69Smrg    # and set any options; do chmod last to preserve setuid bits.
4568650bb69Smrg    #
4578650bb69Smrg    # If any of these fail, we abort the whole thing.  If we want to
4588650bb69Smrg    # ignore errors from any of these, just make sure not to ignore
4598650bb69Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4608650bb69Smrg    #
4618650bb69Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4628650bb69Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4638650bb69Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4648650bb69Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4658650bb69Smrg
4668650bb69Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4678650bb69Smrg    if $copy_on_change &&
4688650bb69Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4698650bb69Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4708650bb69Smrg
4718650bb69Smrg       eval "$initialize_posix_glob" &&
4728650bb69Smrg       $posix_glob set -f &&
4738650bb69Smrg       set X $old && old=:$2:$4:$5:$6 &&
4748650bb69Smrg       set X $new && new=:$2:$4:$5:$6 &&
4758650bb69Smrg       $posix_glob set +f &&
4768650bb69Smrg
4778650bb69Smrg       test "$old" = "$new" &&
4788650bb69Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4798650bb69Smrg    then
4808650bb69Smrg      rm -f "$dsttmp"
4818650bb69Smrg    else
4828650bb69Smrg      # Rename the file to the real destination.
4838650bb69Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4848650bb69Smrg
4858650bb69Smrg      # The rename failed, perhaps because mv can't rename something else
4868650bb69Smrg      # to itself, or perhaps because mv is so ancient that it does not
4878650bb69Smrg      # support -f.
4888650bb69Smrg      {
4898650bb69Smrg	# Now remove or move aside any old file at destination location.
4908650bb69Smrg	# We try this two ways since rm can't unlink itself on some
4918650bb69Smrg	# systems and the destination file might be busy for other
4928650bb69Smrg	# reasons.  In this case, the final cleanup might fail but the new
4938650bb69Smrg	# file should still install successfully.
4948650bb69Smrg	{
4958650bb69Smrg	  test ! -f "$dst" ||
4968650bb69Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4978650bb69Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4988650bb69Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4998650bb69Smrg	  } ||
5008650bb69Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5018650bb69Smrg	    (exit 1); exit 1
5028650bb69Smrg	  }
5038650bb69Smrg	} &&
5048650bb69Smrg
5058650bb69Smrg	# Now rename the file to the real destination.
5068650bb69Smrg	$doit $mvcmd "$dsttmp" "$dst"
5078650bb69Smrg      }
5088650bb69Smrg    fi || exit 1
5098650bb69Smrg
5108650bb69Smrg    trap '' 0
5118650bb69Smrg  fi
5128650bb69Smrgdone
5132c7c4e3dSmrg
5148650bb69Smrg# Local variables:
5158650bb69Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5168650bb69Smrg# time-stamp-start: "scriptversion="
5178650bb69Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5188650bb69Smrg# time-stamp-time-zone: "UTC"
5198650bb69Smrg# time-stamp-end: "; # UTC"
5208650bb69Smrg# End:
521