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