install-sh revision 1.1
11.1Spooka#!/bin/sh
21.1Spooka# install - install a program, script, or datafile
31.1Spooka
41.1Spookascriptversion=2011-11-20.07; # UTC
51.1Spooka
61.1Spooka# This originates from X11R5 (mit/util/scripts/install.sh), which was
71.1Spooka# later released in X11R6 (xc/config/util/install.sh) with the
81.1Spooka# following copyright and license.
91.1Spooka#
101.1Spooka# Copyright (C) 1994 X Consortium
111.1Spooka#
121.1Spooka# Permission is hereby granted, free of charge, to any person obtaining a copy
131.1Spooka# of this software and associated documentation files (the "Software"), to
141.1Spooka# deal in the Software without restriction, including without limitation the
151.1Spooka# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
161.1Spooka# sell copies of the Software, and to permit persons to whom the Software is
171.1Spooka# furnished to do so, subject to the following conditions:
181.1Spooka#
191.1Spooka# The above copyright notice and this permission notice shall be included in
201.1Spooka# all copies or substantial portions of the Software.
211.1Spooka#
221.1Spooka# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
231.1Spooka# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241.1Spooka# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
251.1Spooka# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
261.1Spooka# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
271.1Spooka# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
281.1Spooka#
291.1Spooka# Except as contained in this notice, the name of the X Consortium shall not
301.1Spooka# be used in advertising or otherwise to promote the sale, use or other deal-
311.1Spooka# ings in this Software without prior written authorization from the X Consor-
321.1Spooka# tium.
331.1Spooka#
341.1Spooka#
351.1Spooka# FSF changes to this file are in the public domain.
361.1Spooka#
371.1Spooka# Calling this script install-sh is preferred over install.sh, to prevent
381.1Spooka# 'make' implicit rules from creating a file called install from it
391.1Spooka# when there is no Makefile.
401.1Spooka#
411.1Spooka# This script is compatible with the BSD install script, but was written
421.1Spooka# from scratch.
431.1Spooka
441.1Spookanl='
451.1Spooka'
461.1SpookaIFS=" ""	$nl"
471.1Spooka
481.1Spooka# set DOITPROG to echo to test this script
491.1Spooka
501.1Spooka# Don't use :- since 4.3BSD and earlier shells don't like it.
511.1Spookadoit=${DOITPROG-}
521.1Spookaif test -z "$doit"; then
531.1Spooka  doit_exec=exec
541.1Spookaelse
551.1Spooka  doit_exec=$doit
561.1Spookafi
571.1Spooka
581.1Spooka# Put in absolute file names if you don't have them in your path;
591.1Spooka# or use environment vars.
601.1Spooka
611.1Spookachgrpprog=${CHGRPPROG-chgrp}
621.1Spookachmodprog=${CHMODPROG-chmod}
631.1Spookachownprog=${CHOWNPROG-chown}
641.1Spookacmpprog=${CMPPROG-cmp}
651.1Spookacpprog=${CPPROG-cp}
661.1Spookamkdirprog=${MKDIRPROG-mkdir}
671.1Spookamvprog=${MVPROG-mv}
681.1Spookarmprog=${RMPROG-rm}
691.1Spookastripprog=${STRIPPROG-strip}
701.1Spooka
711.1Spookaposix_glob='?'
721.1Spookainitialize_posix_glob='
731.1Spooka  test "$posix_glob" != "?" || {
741.1Spooka    if (set -f) 2>/dev/null; then
751.1Spooka      posix_glob=
761.1Spooka    else
771.1Spooka      posix_glob=:
781.1Spooka    fi
791.1Spooka  }
801.1Spooka'
811.1Spooka
821.1Spookaposix_mkdir=
831.1Spooka
841.1Spooka# Desired mode of installed file.
851.1Spookamode=0755
861.1Spooka
871.1Spookachgrpcmd=
881.1Spookachmodcmd=$chmodprog
891.1Spookachowncmd=
901.1Spookamvcmd=$mvprog
911.1Spookarmcmd="$rmprog -f"
921.1Spookastripcmd=
931.1Spooka
941.1Spookasrc=
951.1Spookadst=
961.1Spookadir_arg=
971.1Spookadst_arg=
981.1Spooka
991.1Spookacopy_on_change=false
1001.1Spookano_target_directory=
1011.1Spooka
1021.1Spookausage="\
1031.1SpookaUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1041.1Spooka   or: $0 [OPTION]... SRCFILES... DIRECTORY
1051.1Spooka   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1061.1Spooka   or: $0 [OPTION]... -d DIRECTORIES...
1071.1Spooka
1081.1SpookaIn the 1st form, copy SRCFILE to DSTFILE.
1091.1SpookaIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1101.1SpookaIn the 4th, create DIRECTORIES.
1111.1Spooka
1121.1SpookaOptions:
1131.1Spooka     --help     display this help and exit.
1141.1Spooka     --version  display version info and exit.
1151.1Spooka
1161.1Spooka  -c            (ignored)
1171.1Spooka  -C            install only if different (preserve the last data modification time)
1181.1Spooka  -d            create directories instead of installing files.
1191.1Spooka  -g GROUP      $chgrpprog installed files to GROUP.
1201.1Spooka  -m MODE       $chmodprog installed files to MODE.
1211.1Spooka  -o USER       $chownprog installed files to USER.
1221.1Spooka  -s            $stripprog installed files.
1231.1Spooka  -t DIRECTORY  install into DIRECTORY.
1241.1Spooka  -T            report an error if DSTFILE is a directory.
1251.1Spooka
1261.1SpookaEnvironment variables override the default commands:
1271.1Spooka  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1281.1Spooka  RMPROG STRIPPROG
1291.1Spooka"
1301.1Spooka
1311.1Spookawhile test $# -ne 0; do
1321.1Spooka  case $1 in
1331.1Spooka    -c) ;;
1341.1Spooka
1351.1Spooka    -C) copy_on_change=true;;
1361.1Spooka
1371.1Spooka    -d) dir_arg=true;;
1381.1Spooka
1391.1Spooka    -g) chgrpcmd="$chgrpprog $2"
1401.1Spooka	shift;;
1411.1Spooka
1421.1Spooka    --help) echo "$usage"; exit $?;;
1431.1Spooka
1441.1Spooka    -m) mode=$2
1451.1Spooka	case $mode in
1461.1Spooka	  *' '* | *'	'* | *'
1471.1Spooka'*	  | *'*'* | *'?'* | *'['*)
1481.1Spooka	    echo "$0: invalid mode: $mode" >&2
1491.1Spooka	    exit 1;;
1501.1Spooka	esac
1511.1Spooka	shift;;
1521.1Spooka
1531.1Spooka    -o) chowncmd="$chownprog $2"
1541.1Spooka	shift;;
1551.1Spooka
1561.1Spooka    -s) stripcmd=$stripprog;;
1571.1Spooka
1581.1Spooka    -t) dst_arg=$2
1591.1Spooka	# Protect names problematic for 'test' and other utilities.
1601.1Spooka	case $dst_arg in
1611.1Spooka	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
1621.1Spooka	esac
1631.1Spooka	shift;;
1641.1Spooka
1651.1Spooka    -T) no_target_directory=true;;
1661.1Spooka
1671.1Spooka    --version) echo "$0 $scriptversion"; exit $?;;
1681.1Spooka
1691.1Spooka    --)	shift
1701.1Spooka	break;;
1711.1Spooka
1721.1Spooka    -*)	echo "$0: invalid option: $1" >&2
1731.1Spooka	exit 1;;
1741.1Spooka
1751.1Spooka    *)  break;;
1761.1Spooka  esac
1771.1Spooka  shift
1781.1Spookadone
1791.1Spooka
1801.1Spookaif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1811.1Spooka  # When -d is used, all remaining arguments are directories to create.
1821.1Spooka  # When -t is used, the destination is already specified.
1831.1Spooka  # Otherwise, the last argument is the destination.  Remove it from $@.
1841.1Spooka  for arg
1851.1Spooka  do
1861.1Spooka    if test -n "$dst_arg"; then
1871.1Spooka      # $@ is not empty: it contains at least $arg.
1881.1Spooka      set fnord "$@" "$dst_arg"
1891.1Spooka      shift # fnord
1901.1Spooka    fi
1911.1Spooka    shift # arg
1921.1Spooka    dst_arg=$arg
1931.1Spooka    # Protect names problematic for 'test' and other utilities.
1941.1Spooka    case $dst_arg in
1951.1Spooka      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1961.1Spooka    esac
1971.1Spooka  done
1981.1Spookafi
1991.1Spooka
2001.1Spookaif test $# -eq 0; then
2011.1Spooka  if test -z "$dir_arg"; then
2021.1Spooka    echo "$0: no input file specified." >&2
2031.1Spooka    exit 1
2041.1Spooka  fi
2051.1Spooka  # It's OK to call 'install-sh -d' without argument.
2061.1Spooka  # This can happen when creating conditional directories.
2071.1Spooka  exit 0
2081.1Spookafi
2091.1Spooka
2101.1Spookaif test -z "$dir_arg"; then
2111.1Spooka  do_exit='(exit $ret); exit $ret'
2121.1Spooka  trap "ret=129; $do_exit" 1
2131.1Spooka  trap "ret=130; $do_exit" 2
2141.1Spooka  trap "ret=141; $do_exit" 13
2151.1Spooka  trap "ret=143; $do_exit" 15
2161.1Spooka
2171.1Spooka  # Set umask so as not to create temps with too-generous modes.
2181.1Spooka  # However, 'strip' requires both read and write access to temps.
2191.1Spooka  case $mode in
2201.1Spooka    # Optimize common cases.
2211.1Spooka    *644) cp_umask=133;;
2221.1Spooka    *755) cp_umask=22;;
2231.1Spooka
2241.1Spooka    *[0-7])
2251.1Spooka      if test -z "$stripcmd"; then
2261.1Spooka	u_plus_rw=
2271.1Spooka      else
2281.1Spooka	u_plus_rw='% 200'
2291.1Spooka      fi
2301.1Spooka      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2311.1Spooka    *)
2321.1Spooka      if test -z "$stripcmd"; then
2331.1Spooka	u_plus_rw=
2341.1Spooka      else
2351.1Spooka	u_plus_rw=,u+rw
2361.1Spooka      fi
2371.1Spooka      cp_umask=$mode$u_plus_rw;;
2381.1Spooka  esac
2391.1Spookafi
2401.1Spooka
2411.1Spookafor src
2421.1Spookado
2431.1Spooka  # Protect names problematic for 'test' and other utilities.
2441.1Spooka  case $src in
2451.1Spooka    -* | [=\(\)!]) src=./$src;;
2461.1Spooka  esac
2471.1Spooka
2481.1Spooka  if test -n "$dir_arg"; then
2491.1Spooka    dst=$src
2501.1Spooka    dstdir=$dst
2511.1Spooka    test -d "$dstdir"
2521.1Spooka    dstdir_status=$?
2531.1Spooka  else
2541.1Spooka
2551.1Spooka    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2561.1Spooka    # might cause directories to be created, which would be especially bad
2571.1Spooka    # if $src (and thus $dsttmp) contains '*'.
2581.1Spooka    if test ! -f "$src" && test ! -d "$src"; then
2591.1Spooka      echo "$0: $src does not exist." >&2
2601.1Spooka      exit 1
2611.1Spooka    fi
2621.1Spooka
2631.1Spooka    if test -z "$dst_arg"; then
2641.1Spooka      echo "$0: no destination specified." >&2
2651.1Spooka      exit 1
2661.1Spooka    fi
2671.1Spooka    dst=$dst_arg
2681.1Spooka
2691.1Spooka    # If destination is a directory, append the input filename; won't work
2701.1Spooka    # if double slashes aren't ignored.
2711.1Spooka    if test -d "$dst"; then
2721.1Spooka      if test -n "$no_target_directory"; then
2731.1Spooka	echo "$0: $dst_arg: Is a directory" >&2
2741.1Spooka	exit 1
2751.1Spooka      fi
2761.1Spooka      dstdir=$dst
2771.1Spooka      dst=$dstdir/`basename "$src"`
2781.1Spooka      dstdir_status=0
2791.1Spooka    else
2801.1Spooka      # Prefer dirname, but fall back on a substitute if dirname fails.
2811.1Spooka      dstdir=`
2821.1Spooka	(dirname "$dst") 2>/dev/null ||
2831.1Spooka	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2841.1Spooka	     X"$dst" : 'X\(//\)[^/]' \| \
2851.1Spooka	     X"$dst" : 'X\(//\)$' \| \
2861.1Spooka	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2871.1Spooka	echo X"$dst" |
2881.1Spooka	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2891.1Spooka		   s//\1/
2901.1Spooka		   q
2911.1Spooka		 }
2921.1Spooka		 /^X\(\/\/\)[^/].*/{
2931.1Spooka		   s//\1/
2941.1Spooka		   q
2951.1Spooka		 }
2961.1Spooka		 /^X\(\/\/\)$/{
2971.1Spooka		   s//\1/
2981.1Spooka		   q
2991.1Spooka		 }
3001.1Spooka		 /^X\(\/\).*/{
3011.1Spooka		   s//\1/
3021.1Spooka		   q
3031.1Spooka		 }
3041.1Spooka		 s/.*/./; q'
3051.1Spooka      `
3061.1Spooka
3071.1Spooka      test -d "$dstdir"
3081.1Spooka      dstdir_status=$?
3091.1Spooka    fi
3101.1Spooka  fi
3111.1Spooka
3121.1Spooka  obsolete_mkdir_used=false
3131.1Spooka
3141.1Spooka  if test $dstdir_status != 0; then
3151.1Spooka    case $posix_mkdir in
3161.1Spooka      '')
3171.1Spooka	# Create intermediate dirs using mode 755 as modified by the umask.
3181.1Spooka	# This is like FreeBSD 'install' as of 1997-10-28.
3191.1Spooka	umask=`umask`
3201.1Spooka	case $stripcmd.$umask in
3211.1Spooka	  # Optimize common cases.
3221.1Spooka	  *[2367][2367]) mkdir_umask=$umask;;
3231.1Spooka	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3241.1Spooka
3251.1Spooka	  *[0-7])
3261.1Spooka	    mkdir_umask=`expr $umask + 22 \
3271.1Spooka	      - $umask % 100 % 40 + $umask % 20 \
3281.1Spooka	      - $umask % 10 % 4 + $umask % 2
3291.1Spooka	    `;;
3301.1Spooka	  *) mkdir_umask=$umask,go-w;;
3311.1Spooka	esac
3321.1Spooka
3331.1Spooka	# With -d, create the new directory with the user-specified mode.
3341.1Spooka	# Otherwise, rely on $mkdir_umask.
3351.1Spooka	if test -n "$dir_arg"; then
3361.1Spooka	  mkdir_mode=-m$mode
3371.1Spooka	else
3381.1Spooka	  mkdir_mode=
3391.1Spooka	fi
3401.1Spooka
3411.1Spooka	posix_mkdir=false
3421.1Spooka	case $umask in
3431.1Spooka	  *[123567][0-7][0-7])
3441.1Spooka	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3451.1Spooka	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3461.1Spooka	    ;;
3471.1Spooka	  *)
3481.1Spooka	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3491.1Spooka	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3501.1Spooka
3511.1Spooka	    if (umask $mkdir_umask &&
3521.1Spooka		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3531.1Spooka	    then
3541.1Spooka	      if test -z "$dir_arg" || {
3551.1Spooka		   # Check for POSIX incompatibilities with -m.
3561.1Spooka		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3571.1Spooka		   # other-writable bit of parent directory when it shouldn't.
3581.1Spooka		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3591.1Spooka		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3601.1Spooka		   case $ls_ld_tmpdir in
3611.1Spooka		     d????-?r-*) different_mode=700;;
3621.1Spooka		     d????-?--*) different_mode=755;;
3631.1Spooka		     *) false;;
3641.1Spooka		   esac &&
3651.1Spooka		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3661.1Spooka		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3671.1Spooka		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3681.1Spooka		   }
3691.1Spooka		 }
3701.1Spooka	      then posix_mkdir=:
3711.1Spooka	      fi
3721.1Spooka	      rmdir "$tmpdir/d" "$tmpdir"
3731.1Spooka	    else
3741.1Spooka	      # Remove any dirs left behind by ancient mkdir implementations.
3751.1Spooka	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3761.1Spooka	    fi
3771.1Spooka	    trap '' 0;;
3781.1Spooka	esac;;
3791.1Spooka    esac
3801.1Spooka
3811.1Spooka    if
3821.1Spooka      $posix_mkdir && (
3831.1Spooka	umask $mkdir_umask &&
3841.1Spooka	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3851.1Spooka      )
3861.1Spooka    then :
3871.1Spooka    else
3881.1Spooka
3891.1Spooka      # The umask is ridiculous, or mkdir does not conform to POSIX,
3901.1Spooka      # or it failed possibly due to a race condition.  Create the
3911.1Spooka      # directory the slow way, step by step, checking for races as we go.
3921.1Spooka
3931.1Spooka      case $dstdir in
3941.1Spooka	/*) prefix='/';;
3951.1Spooka	[-=\(\)!]*) prefix='./';;
3961.1Spooka	*)  prefix='';;
3971.1Spooka      esac
3981.1Spooka
3991.1Spooka      eval "$initialize_posix_glob"
4001.1Spooka
4011.1Spooka      oIFS=$IFS
4021.1Spooka      IFS=/
4031.1Spooka      $posix_glob set -f
4041.1Spooka      set fnord $dstdir
4051.1Spooka      shift
4061.1Spooka      $posix_glob set +f
4071.1Spooka      IFS=$oIFS
4081.1Spooka
4091.1Spooka      prefixes=
4101.1Spooka
4111.1Spooka      for d
4121.1Spooka      do
4131.1Spooka	test X"$d" = X && continue
4141.1Spooka
4151.1Spooka	prefix=$prefix$d
4161.1Spooka	if test -d "$prefix"; then
4171.1Spooka	  prefixes=
4181.1Spooka	else
4191.1Spooka	  if $posix_mkdir; then
4201.1Spooka	    (umask=$mkdir_umask &&
4211.1Spooka	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4221.1Spooka	    # Don't fail if two instances are running concurrently.
4231.1Spooka	    test -d "$prefix" || exit 1
4241.1Spooka	  else
4251.1Spooka	    case $prefix in
4261.1Spooka	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4271.1Spooka	      *) qprefix=$prefix;;
4281.1Spooka	    esac
4291.1Spooka	    prefixes="$prefixes '$qprefix'"
4301.1Spooka	  fi
4311.1Spooka	fi
4321.1Spooka	prefix=$prefix/
4331.1Spooka      done
4341.1Spooka
4351.1Spooka      if test -n "$prefixes"; then
4361.1Spooka	# Don't fail if two instances are running concurrently.
4371.1Spooka	(umask $mkdir_umask &&
4381.1Spooka	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4391.1Spooka	  test -d "$dstdir" || exit 1
4401.1Spooka	obsolete_mkdir_used=true
4411.1Spooka      fi
4421.1Spooka    fi
4431.1Spooka  fi
4441.1Spooka
4451.1Spooka  if test -n "$dir_arg"; then
4461.1Spooka    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4471.1Spooka    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4481.1Spooka    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4491.1Spooka      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4501.1Spooka  else
4511.1Spooka
4521.1Spooka    # Make a couple of temp file names in the proper directory.
4531.1Spooka    dsttmp=$dstdir/_inst.$$_
4541.1Spooka    rmtmp=$dstdir/_rm.$$_
4551.1Spooka
4561.1Spooka    # Trap to clean up those temp files at exit.
4571.1Spooka    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4581.1Spooka
4591.1Spooka    # Copy the file name to the temp name.
4601.1Spooka    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4611.1Spooka
4621.1Spooka    # and set any options; do chmod last to preserve setuid bits.
4631.1Spooka    #
4641.1Spooka    # If any of these fail, we abort the whole thing.  If we want to
4651.1Spooka    # ignore errors from any of these, just make sure not to ignore
4661.1Spooka    # errors from the above "$doit $cpprog $src $dsttmp" command.
4671.1Spooka    #
4681.1Spooka    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4691.1Spooka    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4701.1Spooka    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4711.1Spooka    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4721.1Spooka
4731.1Spooka    # If -C, don't bother to copy if it wouldn't change the file.
4741.1Spooka    if $copy_on_change &&
4751.1Spooka       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4761.1Spooka       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4771.1Spooka
4781.1Spooka       eval "$initialize_posix_glob" &&
4791.1Spooka       $posix_glob set -f &&
4801.1Spooka       set X $old && old=:$2:$4:$5:$6 &&
4811.1Spooka       set X $new && new=:$2:$4:$5:$6 &&
4821.1Spooka       $posix_glob set +f &&
4831.1Spooka
4841.1Spooka       test "$old" = "$new" &&
4851.1Spooka       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4861.1Spooka    then
4871.1Spooka      rm -f "$dsttmp"
4881.1Spooka    else
4891.1Spooka      # Rename the file to the real destination.
4901.1Spooka      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4911.1Spooka
4921.1Spooka      # The rename failed, perhaps because mv can't rename something else
4931.1Spooka      # to itself, or perhaps because mv is so ancient that it does not
4941.1Spooka      # support -f.
4951.1Spooka      {
4961.1Spooka	# Now remove or move aside any old file at destination location.
4971.1Spooka	# We try this two ways since rm can't unlink itself on some
4981.1Spooka	# systems and the destination file might be busy for other
4991.1Spooka	# reasons.  In this case, the final cleanup might fail but the new
5001.1Spooka	# file should still install successfully.
5011.1Spooka	{
5021.1Spooka	  test ! -f "$dst" ||
5031.1Spooka	  $doit $rmcmd -f "$dst" 2>/dev/null ||
5041.1Spooka	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5051.1Spooka	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
5061.1Spooka	  } ||
5071.1Spooka	  { echo "$0: cannot unlink or rename $dst" >&2
5081.1Spooka	    (exit 1); exit 1
5091.1Spooka	  }
5101.1Spooka	} &&
5111.1Spooka
5121.1Spooka	# Now rename the file to the real destination.
5131.1Spooka	$doit $mvcmd "$dsttmp" "$dst"
5141.1Spooka      }
5151.1Spooka    fi || exit 1
5161.1Spooka
5171.1Spooka    trap '' 0
5181.1Spooka  fi
5191.1Spookadone
5201.1Spooka
5211.1Spooka# Local variables:
5221.1Spooka# eval: (add-hook 'write-file-hooks 'time-stamp)
5231.1Spooka# time-stamp-start: "scriptversion="
5241.1Spooka# time-stamp-format: "%:y-%02m-%02d.%02H"
5251.1Spooka# time-stamp-time-zone: "UTC"
5261.1Spooka# time-stamp-end: "; # UTC"
5271.1Spooka# End:
528