install-sh revision df9ffc95
109885543Smrg#!/bin/sh
209885543Smrg# install - install a program, script, or datafile
309885543Smrg
4df9ffc95Smrgscriptversion=2011-11-20.07; # UTC
509885543Smrg
609885543Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
709885543Smrg# later released in X11R6 (xc/config/util/install.sh) with the
809885543Smrg# following copyright and license.
909885543Smrg#
1009885543Smrg# Copyright (C) 1994 X Consortium
1109885543Smrg#
1209885543Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1309885543Smrg# of this software and associated documentation files (the "Software"), to
1409885543Smrg# deal in the Software without restriction, including without limitation the
1509885543Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1609885543Smrg# sell copies of the Software, and to permit persons to whom the Software is
1709885543Smrg# furnished to do so, subject to the following conditions:
1809885543Smrg#
1909885543Smrg# The above copyright notice and this permission notice shall be included in
2009885543Smrg# all copies or substantial portions of the Software.
2109885543Smrg#
2209885543Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2309885543Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2409885543Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2509885543Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2609885543Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2709885543Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2809885543Smrg#
2909885543Smrg# Except as contained in this notice, the name of the X Consortium shall not
3009885543Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3109885543Smrg# ings in this Software without prior written authorization from the X Consor-
3209885543Smrg# tium.
3309885543Smrg#
3409885543Smrg#
3509885543Smrg# FSF changes to this file are in the public domain.
3609885543Smrg#
3709885543Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38df9ffc95Smrg# 'make' implicit rules from creating a file called install from it
3909885543Smrg# when there is no Makefile.
4009885543Smrg#
4109885543Smrg# This script is compatible with the BSD install script, but was written
427104f784Smrg# from scratch.
437104f784Smrg
447104f784Smrgnl='
457104f784Smrg'
467104f784SmrgIFS=" ""	$nl"
4709885543Smrg
4809885543Smrg# set DOITPROG to echo to test this script
4909885543Smrg
5009885543Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
517104f784Smrgdoit=${DOITPROG-}
527104f784Smrgif test -z "$doit"; then
537104f784Smrg  doit_exec=exec
547104f784Smrgelse
557104f784Smrg  doit_exec=$doit
567104f784Smrgfi
5709885543Smrg
587104f784Smrg# Put in absolute file names if you don't have them in your path;
597104f784Smrg# or use environment vars.
607104f784Smrg
617104f784Smrgchgrpprog=${CHGRPPROG-chgrp}
627104f784Smrgchmodprog=${CHMODPROG-chmod}
637104f784Smrgchownprog=${CHOWNPROG-chown}
647104f784Smrgcmpprog=${CMPPROG-cmp}
657104f784Smrgcpprog=${CPPROG-cp}
667104f784Smrgmkdirprog=${MKDIRPROG-mkdir}
677104f784Smrgmvprog=${MVPROG-mv}
687104f784Smrgrmprog=${RMPROG-rm}
697104f784Smrgstripprog=${STRIPPROG-strip}
707104f784Smrg
717104f784Smrgposix_glob='?'
727104f784Smrginitialize_posix_glob='
737104f784Smrg  test "$posix_glob" != "?" || {
747104f784Smrg    if (set -f) 2>/dev/null; then
757104f784Smrg      posix_glob=
767104f784Smrg    else
777104f784Smrg      posix_glob=:
787104f784Smrg    fi
797104f784Smrg  }
807104f784Smrg'
8109885543Smrg
827104f784Smrgposix_mkdir=
837104f784Smrg
847104f784Smrg# Desired mode of installed file.
857104f784Smrgmode=0755
8609885543Smrg
8709885543Smrgchgrpcmd=
887104f784Smrgchmodcmd=$chmodprog
897104f784Smrgchowncmd=
907104f784Smrgmvcmd=$mvprog
9109885543Smrgrmcmd="$rmprog -f"
927104f784Smrgstripcmd=
937104f784Smrg
9409885543Smrgsrc=
9509885543Smrgdst=
9609885543Smrgdir_arg=
977104f784Smrgdst_arg=
987104f784Smrg
997104f784Smrgcopy_on_change=false
10009885543Smrgno_target_directory=
10109885543Smrg
1027104f784Smrgusage="\
1037104f784SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10409885543Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10509885543Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10609885543Smrg   or: $0 [OPTION]... -d DIRECTORIES...
10709885543Smrg
10809885543SmrgIn the 1st form, copy SRCFILE to DSTFILE.
10909885543SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11009885543SmrgIn the 4th, create DIRECTORIES.
11109885543Smrg
11209885543SmrgOptions:
1137104f784Smrg     --help     display this help and exit.
1147104f784Smrg     --version  display version info and exit.
1157104f784Smrg
1167104f784Smrg  -c            (ignored)
1177104f784Smrg  -C            install only if different (preserve the last data modification time)
1187104f784Smrg  -d            create directories instead of installing files.
1197104f784Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1207104f784Smrg  -m MODE       $chmodprog installed files to MODE.
1217104f784Smrg  -o USER       $chownprog installed files to USER.
1227104f784Smrg  -s            $stripprog installed files.
1237104f784Smrg  -t DIRECTORY  install into DIRECTORY.
1247104f784Smrg  -T            report an error if DSTFILE is a directory.
12509885543Smrg
12609885543SmrgEnvironment variables override the default commands:
1277104f784Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1287104f784Smrg  RMPROG STRIPPROG
12909885543Smrg"
13009885543Smrg
1317104f784Smrgwhile test $# -ne 0; do
13209885543Smrg  case $1 in
1337104f784Smrg    -c) ;;
1347104f784Smrg
1357104f784Smrg    -C) copy_on_change=true;;
13609885543Smrg
1377104f784Smrg    -d) dir_arg=true;;
13809885543Smrg
13909885543Smrg    -g) chgrpcmd="$chgrpprog $2"
1407104f784Smrg	shift;;
14109885543Smrg
14209885543Smrg    --help) echo "$usage"; exit $?;;
14309885543Smrg
1447104f784Smrg    -m) mode=$2
1457104f784Smrg	case $mode in
1467104f784Smrg	  *' '* | *'	'* | *'
1477104f784Smrg'*	  | *'*'* | *'?'* | *'['*)
1487104f784Smrg	    echo "$0: invalid mode: $mode" >&2
1497104f784Smrg	    exit 1;;
1507104f784Smrg	esac
1517104f784Smrg	shift;;
15209885543Smrg
15309885543Smrg    -o) chowncmd="$chownprog $2"
1547104f784Smrg	shift;;
15509885543Smrg
1567104f784Smrg    -s) stripcmd=$stripprog;;
15709885543Smrg
1587104f784Smrg    -t) dst_arg=$2
159df9ffc95Smrg	# Protect names problematic for 'test' and other utilities.
160b12e5c03Smrg	case $dst_arg in
161b12e5c03Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162b12e5c03Smrg	esac
1637104f784Smrg	shift;;
16409885543Smrg
1657104f784Smrg    -T) no_target_directory=true;;
16609885543Smrg
16709885543Smrg    --version) echo "$0 $scriptversion"; exit $?;;
16809885543Smrg
1697104f784Smrg    --)	shift
17009885543Smrg	break;;
1717104f784Smrg
1727104f784Smrg    -*)	echo "$0: invalid option: $1" >&2
1737104f784Smrg	exit 1;;
1747104f784Smrg
1757104f784Smrg    *)  break;;
17609885543Smrg  esac
1777104f784Smrg  shift
17809885543Smrgdone
17909885543Smrg
1807104f784Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1817104f784Smrg  # When -d is used, all remaining arguments are directories to create.
1827104f784Smrg  # When -t is used, the destination is already specified.
1837104f784Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1847104f784Smrg  for arg
1857104f784Smrg  do
1867104f784Smrg    if test -n "$dst_arg"; then
1877104f784Smrg      # $@ is not empty: it contains at least $arg.
1887104f784Smrg      set fnord "$@" "$dst_arg"
1897104f784Smrg      shift # fnord
1907104f784Smrg    fi
1917104f784Smrg    shift # arg
1927104f784Smrg    dst_arg=$arg
193df9ffc95Smrg    # Protect names problematic for 'test' and other utilities.
194b12e5c03Smrg    case $dst_arg in
195b12e5c03Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196b12e5c03Smrg    esac
1977104f784Smrg  done
1987104f784Smrgfi
1997104f784Smrg
2007104f784Smrgif test $# -eq 0; then
20109885543Smrg  if test -z "$dir_arg"; then
20209885543Smrg    echo "$0: no input file specified." >&2
20309885543Smrg    exit 1
20409885543Smrg  fi
205df9ffc95Smrg  # It's OK to call 'install-sh -d' without argument.
20609885543Smrg  # This can happen when creating conditional directories.
20709885543Smrg  exit 0
20809885543Smrgfi
20909885543Smrg
2107104f784Smrgif test -z "$dir_arg"; then
211b12e5c03Smrg  do_exit='(exit $ret); exit $ret'
212b12e5c03Smrg  trap "ret=129; $do_exit" 1
213b12e5c03Smrg  trap "ret=130; $do_exit" 2
214b12e5c03Smrg  trap "ret=141; $do_exit" 13
215b12e5c03Smrg  trap "ret=143; $do_exit" 15
2167104f784Smrg
2177104f784Smrg  # Set umask so as not to create temps with too-generous modes.
2187104f784Smrg  # However, 'strip' requires both read and write access to temps.
2197104f784Smrg  case $mode in
2207104f784Smrg    # Optimize common cases.
2217104f784Smrg    *644) cp_umask=133;;
2227104f784Smrg    *755) cp_umask=22;;
2237104f784Smrg
2247104f784Smrg    *[0-7])
2257104f784Smrg      if test -z "$stripcmd"; then
2267104f784Smrg	u_plus_rw=
2277104f784Smrg      else
2287104f784Smrg	u_plus_rw='% 200'
2297104f784Smrg      fi
2307104f784Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2317104f784Smrg    *)
2327104f784Smrg      if test -z "$stripcmd"; then
2337104f784Smrg	u_plus_rw=
2347104f784Smrg      else
2357104f784Smrg	u_plus_rw=,u+rw
2367104f784Smrg      fi
2377104f784Smrg      cp_umask=$mode$u_plus_rw;;
2387104f784Smrg  esac
2397104f784Smrgfi
2407104f784Smrg
24109885543Smrgfor src
24209885543Smrgdo
243df9ffc95Smrg  # Protect names problematic for 'test' and other utilities.
24409885543Smrg  case $src in
245b12e5c03Smrg    -* | [=\(\)!]) src=./$src;;
24609885543Smrg  esac
24709885543Smrg
24809885543Smrg  if test -n "$dir_arg"; then
24909885543Smrg    dst=$src
2507104f784Smrg    dstdir=$dst
2517104f784Smrg    test -d "$dstdir"
2527104f784Smrg    dstdir_status=$?
25309885543Smrg  else
2547104f784Smrg
25509885543Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
25609885543Smrg    # might cause directories to be created, which would be especially bad
25709885543Smrg    # if $src (and thus $dsttmp) contains '*'.
25809885543Smrg    if test ! -f "$src" && test ! -d "$src"; then
25909885543Smrg      echo "$0: $src does not exist." >&2
26009885543Smrg      exit 1
26109885543Smrg    fi
26209885543Smrg
2637104f784Smrg    if test -z "$dst_arg"; then
26409885543Smrg      echo "$0: no destination specified." >&2
26509885543Smrg      exit 1
26609885543Smrg    fi
2677104f784Smrg    dst=$dst_arg
26809885543Smrg
26909885543Smrg    # If destination is a directory, append the input filename; won't work
27009885543Smrg    # if double slashes aren't ignored.
27109885543Smrg    if test -d "$dst"; then
27209885543Smrg      if test -n "$no_target_directory"; then
2737104f784Smrg	echo "$0: $dst_arg: Is a directory" >&2
27409885543Smrg	exit 1
27509885543Smrg      fi
2767104f784Smrg      dstdir=$dst
2777104f784Smrg      dst=$dstdir/`basename "$src"`
2787104f784Smrg      dstdir_status=0
2797104f784Smrg    else
2807104f784Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2817104f784Smrg      dstdir=`
2827104f784Smrg	(dirname "$dst") 2>/dev/null ||
2837104f784Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2847104f784Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2857104f784Smrg	     X"$dst" : 'X\(//\)$' \| \
2867104f784Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2877104f784Smrg	echo X"$dst" |
2887104f784Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2897104f784Smrg		   s//\1/
2907104f784Smrg		   q
2917104f784Smrg		 }
2927104f784Smrg		 /^X\(\/\/\)[^/].*/{
2937104f784Smrg		   s//\1/
2947104f784Smrg		   q
2957104f784Smrg		 }
2967104f784Smrg		 /^X\(\/\/\)$/{
2977104f784Smrg		   s//\1/
2987104f784Smrg		   q
2997104f784Smrg		 }
3007104f784Smrg		 /^X\(\/\).*/{
3017104f784Smrg		   s//\1/
3027104f784Smrg		   q
3037104f784Smrg		 }
3047104f784Smrg		 s/.*/./; q'
3057104f784Smrg      `
3067104f784Smrg
3077104f784Smrg      test -d "$dstdir"
3087104f784Smrg      dstdir_status=$?
30909885543Smrg    fi
31009885543Smrg  fi
31109885543Smrg
3127104f784Smrg  obsolete_mkdir_used=false
3137104f784Smrg
3147104f784Smrg  if test $dstdir_status != 0; then
3157104f784Smrg    case $posix_mkdir in
3167104f784Smrg      '')
3177104f784Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3187104f784Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3197104f784Smrg	umask=`umask`
3207104f784Smrg	case $stripcmd.$umask in
3217104f784Smrg	  # Optimize common cases.
3227104f784Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3237104f784Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3247104f784Smrg
3257104f784Smrg	  *[0-7])
3267104f784Smrg	    mkdir_umask=`expr $umask + 22 \
3277104f784Smrg	      - $umask % 100 % 40 + $umask % 20 \
3287104f784Smrg	      - $umask % 10 % 4 + $umask % 2
3297104f784Smrg	    `;;
3307104f784Smrg	  *) mkdir_umask=$umask,go-w;;
3317104f784Smrg	esac
3327104f784Smrg
3337104f784Smrg	# With -d, create the new directory with the user-specified mode.
3347104f784Smrg	# Otherwise, rely on $mkdir_umask.
3357104f784Smrg	if test -n "$dir_arg"; then
3367104f784Smrg	  mkdir_mode=-m$mode
3377104f784Smrg	else
3387104f784Smrg	  mkdir_mode=
3397104f784Smrg	fi
3407104f784Smrg
3417104f784Smrg	posix_mkdir=false
3427104f784Smrg	case $umask in
3437104f784Smrg	  *[123567][0-7][0-7])
3447104f784Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3457104f784Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3467104f784Smrg	    ;;
3477104f784Smrg	  *)
3487104f784Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3497104f784Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3507104f784Smrg
3517104f784Smrg	    if (umask $mkdir_umask &&
3527104f784Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3537104f784Smrg	    then
3547104f784Smrg	      if test -z "$dir_arg" || {
3557104f784Smrg		   # Check for POSIX incompatibilities with -m.
3567104f784Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357df9ffc95Smrg		   # other-writable bit of parent directory when it shouldn't.
3587104f784Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3597104f784Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3607104f784Smrg		   case $ls_ld_tmpdir in
3617104f784Smrg		     d????-?r-*) different_mode=700;;
3627104f784Smrg		     d????-?--*) different_mode=755;;
3637104f784Smrg		     *) false;;
3647104f784Smrg		   esac &&
3657104f784Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3667104f784Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3677104f784Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3687104f784Smrg		   }
3697104f784Smrg		 }
3707104f784Smrg	      then posix_mkdir=:
3717104f784Smrg	      fi
3727104f784Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3737104f784Smrg	    else
3747104f784Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3757104f784Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3767104f784Smrg	    fi
3777104f784Smrg	    trap '' 0;;
3787104f784Smrg	esac;;
3797104f784Smrg    esac
38009885543Smrg
3817104f784Smrg    if
3827104f784Smrg      $posix_mkdir && (
3837104f784Smrg	umask $mkdir_umask &&
3847104f784Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3857104f784Smrg      )
3867104f784Smrg    then :
3877104f784Smrg    else
38809885543Smrg
3897104f784Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3907104f784Smrg      # or it failed possibly due to a race condition.  Create the
3917104f784Smrg      # directory the slow way, step by step, checking for races as we go.
39209885543Smrg
3937104f784Smrg      case $dstdir in
3947104f784Smrg	/*) prefix='/';;
395b12e5c03Smrg	[-=\(\)!]*) prefix='./';;
3967104f784Smrg	*)  prefix='';;
3977104f784Smrg      esac
39809885543Smrg
3997104f784Smrg      eval "$initialize_posix_glob"
40009885543Smrg
4017104f784Smrg      oIFS=$IFS
4027104f784Smrg      IFS=/
4037104f784Smrg      $posix_glob set -f
4047104f784Smrg      set fnord $dstdir
40509885543Smrg      shift
4067104f784Smrg      $posix_glob set +f
4077104f784Smrg      IFS=$oIFS
4087104f784Smrg
4097104f784Smrg      prefixes=
4107104f784Smrg
4117104f784Smrg      for d
4127104f784Smrg      do
413b12e5c03Smrg	test X"$d" = X && continue
4147104f784Smrg
4157104f784Smrg	prefix=$prefix$d
4167104f784Smrg	if test -d "$prefix"; then
4177104f784Smrg	  prefixes=
4187104f784Smrg	else
4197104f784Smrg	  if $posix_mkdir; then
4207104f784Smrg	    (umask=$mkdir_umask &&
4217104f784Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4227104f784Smrg	    # Don't fail if two instances are running concurrently.
4237104f784Smrg	    test -d "$prefix" || exit 1
4247104f784Smrg	  else
4257104f784Smrg	    case $prefix in
4267104f784Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4277104f784Smrg	      *) qprefix=$prefix;;
4287104f784Smrg	    esac
4297104f784Smrg	    prefixes="$prefixes '$qprefix'"
4307104f784Smrg	  fi
4317104f784Smrg	fi
4327104f784Smrg	prefix=$prefix/
4337104f784Smrg      done
4347104f784Smrg
4357104f784Smrg      if test -n "$prefixes"; then
4367104f784Smrg	# Don't fail if two instances are running concurrently.
4377104f784Smrg	(umask $mkdir_umask &&
4387104f784Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4397104f784Smrg	  test -d "$dstdir" || exit 1
4407104f784Smrg	obsolete_mkdir_used=true
44109885543Smrg      fi
4427104f784Smrg    fi
44309885543Smrg  fi
44409885543Smrg
44509885543Smrg  if test -n "$dir_arg"; then
4467104f784Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4477104f784Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4487104f784Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4497104f784Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
45009885543Smrg  else
45109885543Smrg
45209885543Smrg    # Make a couple of temp file names in the proper directory.
45309885543Smrg    dsttmp=$dstdir/_inst.$$_
45409885543Smrg    rmtmp=$dstdir/_rm.$$_
45509885543Smrg
45609885543Smrg    # Trap to clean up those temp files at exit.
45709885543Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45809885543Smrg
45909885543Smrg    # Copy the file name to the temp name.
4607104f784Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
46109885543Smrg
46209885543Smrg    # and set any options; do chmod last to preserve setuid bits.
46309885543Smrg    #
46409885543Smrg    # If any of these fail, we abort the whole thing.  If we want to
46509885543Smrg    # ignore errors from any of these, just make sure not to ignore
46609885543Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46709885543Smrg    #
4687104f784Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4697104f784Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4707104f784Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4717104f784Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4727104f784Smrg
4737104f784Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4747104f784Smrg    if $copy_on_change &&
4757104f784Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4767104f784Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4777104f784Smrg
4787104f784Smrg       eval "$initialize_posix_glob" &&
4797104f784Smrg       $posix_glob set -f &&
4807104f784Smrg       set X $old && old=:$2:$4:$5:$6 &&
4817104f784Smrg       set X $new && new=:$2:$4:$5:$6 &&
4827104f784Smrg       $posix_glob set +f &&
4837104f784Smrg
4847104f784Smrg       test "$old" = "$new" &&
4857104f784Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4867104f784Smrg    then
4877104f784Smrg      rm -f "$dsttmp"
4887104f784Smrg    else
4897104f784Smrg      # Rename the file to the real destination.
4907104f784Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4917104f784Smrg
4927104f784Smrg      # The rename failed, perhaps because mv can't rename something else
4937104f784Smrg      # to itself, or perhaps because mv is so ancient that it does not
4947104f784Smrg      # support -f.
4957104f784Smrg      {
4967104f784Smrg	# Now remove or move aside any old file at destination location.
4977104f784Smrg	# We try this two ways since rm can't unlink itself on some
4987104f784Smrg	# systems and the destination file might be busy for other
4997104f784Smrg	# reasons.  In this case, the final cleanup might fail but the new
5007104f784Smrg	# file should still install successfully.
5017104f784Smrg	{
5027104f784Smrg	  test ! -f "$dst" ||
5037104f784Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
5047104f784Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5057104f784Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
5067104f784Smrg	  } ||
5077104f784Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5087104f784Smrg	    (exit 1); exit 1
5097104f784Smrg	  }
5107104f784Smrg	} &&
5117104f784Smrg
5127104f784Smrg	# Now rename the file to the real destination.
5137104f784Smrg	$doit $mvcmd "$dsttmp" "$dst"
5147104f784Smrg      }
5157104f784Smrg    fi || exit 1
5167104f784Smrg
5177104f784Smrg    trap '' 0
5187104f784Smrg  fi
51909885543Smrgdone
52009885543Smrg
52109885543Smrg# Local variables:
52209885543Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
52309885543Smrg# time-stamp-start: "scriptversion="
52409885543Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525b698ba48Smrg# time-stamp-time-zone: "UTC"
526b698ba48Smrg# time-stamp-end: "; # UTC"
52709885543Smrg# End:
528