install-sh revision c889a3bf
17a84e134Smrg#!/bin/sh
27a84e134Smrg# install - install a program, script, or datafile
37a84e134Smrg
4c889a3bfSmrgscriptversion=2011-11-20.07; # UTC
57a84e134Smrg
67a84e134Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
77a84e134Smrg# later released in X11R6 (xc/config/util/install.sh) with the
87a84e134Smrg# following copyright and license.
97a84e134Smrg#
107a84e134Smrg# Copyright (C) 1994 X Consortium
117a84e134Smrg#
127a84e134Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
137a84e134Smrg# of this software and associated documentation files (the "Software"), to
147a84e134Smrg# deal in the Software without restriction, including without limitation the
157a84e134Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
167a84e134Smrg# sell copies of the Software, and to permit persons to whom the Software is
177a84e134Smrg# furnished to do so, subject to the following conditions:
187a84e134Smrg#
197a84e134Smrg# The above copyright notice and this permission notice shall be included in
207a84e134Smrg# all copies or substantial portions of the Software.
217a84e134Smrg#
227a84e134Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237a84e134Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247a84e134Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
257a84e134Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
267a84e134Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
277a84e134Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
287a84e134Smrg#
297a84e134Smrg# Except as contained in this notice, the name of the X Consortium shall not
307a84e134Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
317a84e134Smrg# ings in this Software without prior written authorization from the X Consor-
327a84e134Smrg# tium.
337a84e134Smrg#
347a84e134Smrg#
357a84e134Smrg# FSF changes to this file are in the public domain.
367a84e134Smrg#
377a84e134Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38c889a3bfSmrg# 'make' implicit rules from creating a file called install from it
397a84e134Smrg# when there is no Makefile.
407a84e134Smrg#
417a84e134Smrg# This script is compatible with the BSD install script, but was written
427a84e134Smrg# from scratch.
437a84e134Smrg
447a84e134Smrgnl='
457a84e134Smrg'
467a84e134SmrgIFS=" ""	$nl"
477a84e134Smrg
487a84e134Smrg# set DOITPROG to echo to test this script
497a84e134Smrg
507a84e134Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51ab902922Smrgdoit=${DOITPROG-}
527a84e134Smrgif test -z "$doit"; then
537a84e134Smrg  doit_exec=exec
547a84e134Smrgelse
557a84e134Smrg  doit_exec=$doit
567a84e134Smrgfi
577a84e134Smrg
587a84e134Smrg# Put in absolute file names if you don't have them in your path;
597a84e134Smrg# or use environment vars.
607a84e134Smrg
61ab902922Smrgchgrpprog=${CHGRPPROG-chgrp}
62ab902922Smrgchmodprog=${CHMODPROG-chmod}
63ab902922Smrgchownprog=${CHOWNPROG-chown}
64ab902922Smrgcmpprog=${CMPPROG-cmp}
65ab902922Smrgcpprog=${CPPROG-cp}
66ab902922Smrgmkdirprog=${MKDIRPROG-mkdir}
67ab902922Smrgmvprog=${MVPROG-mv}
68ab902922Smrgrmprog=${RMPROG-rm}
69ab902922Smrgstripprog=${STRIPPROG-strip}
70ab902922Smrg
71ab902922Smrgposix_glob='?'
72ab902922Smrginitialize_posix_glob='
73ab902922Smrg  test "$posix_glob" != "?" || {
74ab902922Smrg    if (set -f) 2>/dev/null; then
75ab902922Smrg      posix_glob=
76ab902922Smrg    else
77ab902922Smrg      posix_glob=:
78ab902922Smrg    fi
79ab902922Smrg  }
80ab902922Smrg'
817a84e134Smrg
827a84e134Smrgposix_mkdir=
837a84e134Smrg
847a84e134Smrg# Desired mode of installed file.
857a84e134Smrgmode=0755
867a84e134Smrg
87ab902922Smrgchgrpcmd=
887a84e134Smrgchmodcmd=$chmodprog
897a84e134Smrgchowncmd=
90ab902922Smrgmvcmd=$mvprog
917a84e134Smrgrmcmd="$rmprog -f"
92ab902922Smrgstripcmd=
93ab902922Smrg
947a84e134Smrgsrc=
957a84e134Smrgdst=
967a84e134Smrgdir_arg=
97ab902922Smrgdst_arg=
98ab902922Smrg
99ab902922Smrgcopy_on_change=false
1007a84e134Smrgno_target_directory=
1017a84e134Smrg
102ab902922Smrgusage="\
103ab902922SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1047a84e134Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1057a84e134Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1067a84e134Smrg   or: $0 [OPTION]... -d DIRECTORIES...
1077a84e134Smrg
1087a84e134SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1097a84e134SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1107a84e134SmrgIn the 4th, create DIRECTORIES.
1117a84e134Smrg
1127a84e134SmrgOptions:
113ab902922Smrg     --help     display this help and exit.
114ab902922Smrg     --version  display version info and exit.
115ab902922Smrg
116ab902922Smrg  -c            (ignored)
117ab902922Smrg  -C            install only if different (preserve the last data modification time)
118ab902922Smrg  -d            create directories instead of installing files.
119ab902922Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120ab902922Smrg  -m MODE       $chmodprog installed files to MODE.
121ab902922Smrg  -o USER       $chownprog installed files to USER.
122ab902922Smrg  -s            $stripprog installed files.
123ab902922Smrg  -t DIRECTORY  install into DIRECTORY.
124ab902922Smrg  -T            report an error if DSTFILE is a directory.
1257a84e134Smrg
1267a84e134SmrgEnvironment variables override the default commands:
127ab902922Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128ab902922Smrg  RMPROG STRIPPROG
1297a84e134Smrg"
1307a84e134Smrg
1317a84e134Smrgwhile test $# -ne 0; do
1327a84e134Smrg  case $1 in
133ab902922Smrg    -c) ;;
134ab902922Smrg
135ab902922Smrg    -C) copy_on_change=true;;
1367a84e134Smrg
137ab902922Smrg    -d) dir_arg=true;;
1387a84e134Smrg
1397a84e134Smrg    -g) chgrpcmd="$chgrpprog $2"
140ab902922Smrg	shift;;
1417a84e134Smrg
1427a84e134Smrg    --help) echo "$usage"; exit $?;;
1437a84e134Smrg
1447a84e134Smrg    -m) mode=$2
1457a84e134Smrg	case $mode in
1467a84e134Smrg	  *' '* | *'	'* | *'
1477a84e134Smrg'*	  | *'*'* | *'?'* | *'['*)
1487a84e134Smrg	    echo "$0: invalid mode: $mode" >&2
1497a84e134Smrg	    exit 1;;
1507a84e134Smrg	esac
151ab902922Smrg	shift;;
1527a84e134Smrg
1537a84e134Smrg    -o) chowncmd="$chownprog $2"
154ab902922Smrg	shift;;
1557a84e134Smrg
156ab902922Smrg    -s) stripcmd=$stripprog;;
1577a84e134Smrg
158ab902922Smrg    -t) dst_arg=$2
159c889a3bfSmrg	# Protect names problematic for 'test' and other utilities.
160421c997bSmrg	case $dst_arg in
161421c997bSmrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162421c997bSmrg	esac
163ab902922Smrg	shift;;
1647a84e134Smrg
165ab902922Smrg    -T) no_target_directory=true;;
1667a84e134Smrg
1677a84e134Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1687a84e134Smrg
1697a84e134Smrg    --)	shift
1707a84e134Smrg	break;;
1717a84e134Smrg
1727a84e134Smrg    -*)	echo "$0: invalid option: $1" >&2
1737a84e134Smrg	exit 1;;
1747a84e134Smrg
1757a84e134Smrg    *)  break;;
1767a84e134Smrg  esac
177ab902922Smrg  shift
1787a84e134Smrgdone
1797a84e134Smrg
180ab902922Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1817a84e134Smrg  # When -d is used, all remaining arguments are directories to create.
1827a84e134Smrg  # When -t is used, the destination is already specified.
1837a84e134Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1847a84e134Smrg  for arg
1857a84e134Smrg  do
186ab902922Smrg    if test -n "$dst_arg"; then
1877a84e134Smrg      # $@ is not empty: it contains at least $arg.
188ab902922Smrg      set fnord "$@" "$dst_arg"
1897a84e134Smrg      shift # fnord
1907a84e134Smrg    fi
1917a84e134Smrg    shift # arg
192ab902922Smrg    dst_arg=$arg
193c889a3bfSmrg    # Protect names problematic for 'test' and other utilities.
194421c997bSmrg    case $dst_arg in
195421c997bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196421c997bSmrg    esac
1977a84e134Smrg  done
1987a84e134Smrgfi
1997a84e134Smrg
2007a84e134Smrgif test $# -eq 0; then
2017a84e134Smrg  if test -z "$dir_arg"; then
2027a84e134Smrg    echo "$0: no input file specified." >&2
2037a84e134Smrg    exit 1
2047a84e134Smrg  fi
205c889a3bfSmrg  # It's OK to call 'install-sh -d' without argument.
2067a84e134Smrg  # This can happen when creating conditional directories.
2077a84e134Smrg  exit 0
2087a84e134Smrgfi
2097a84e134Smrg
2107a84e134Smrgif test -z "$dir_arg"; then
211421c997bSmrg  do_exit='(exit $ret); exit $ret'
212421c997bSmrg  trap "ret=129; $do_exit" 1
213421c997bSmrg  trap "ret=130; $do_exit" 2
214421c997bSmrg  trap "ret=141; $do_exit" 13
215421c997bSmrg  trap "ret=143; $do_exit" 15
2167a84e134Smrg
2177a84e134Smrg  # Set umask so as not to create temps with too-generous modes.
2187a84e134Smrg  # However, 'strip' requires both read and write access to temps.
2197a84e134Smrg  case $mode in
2207a84e134Smrg    # Optimize common cases.
2217a84e134Smrg    *644) cp_umask=133;;
2227a84e134Smrg    *755) cp_umask=22;;
2237a84e134Smrg
2247a84e134Smrg    *[0-7])
2257a84e134Smrg      if test -z "$stripcmd"; then
2267a84e134Smrg	u_plus_rw=
2277a84e134Smrg      else
2287a84e134Smrg	u_plus_rw='% 200'
2297a84e134Smrg      fi
2307a84e134Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2317a84e134Smrg    *)
2327a84e134Smrg      if test -z "$stripcmd"; then
2337a84e134Smrg	u_plus_rw=
2347a84e134Smrg      else
2357a84e134Smrg	u_plus_rw=,u+rw
2367a84e134Smrg      fi
2377a84e134Smrg      cp_umask=$mode$u_plus_rw;;
2387a84e134Smrg  esac
2397a84e134Smrgfi
2407a84e134Smrg
2417a84e134Smrgfor src
2427a84e134Smrgdo
243c889a3bfSmrg  # Protect names problematic for 'test' and other utilities.
2447a84e134Smrg  case $src in
245421c997bSmrg    -* | [=\(\)!]) src=./$src;;
2467a84e134Smrg  esac
2477a84e134Smrg
2487a84e134Smrg  if test -n "$dir_arg"; then
2497a84e134Smrg    dst=$src
2507a84e134Smrg    dstdir=$dst
2517a84e134Smrg    test -d "$dstdir"
2527a84e134Smrg    dstdir_status=$?
2537a84e134Smrg  else
2547a84e134Smrg
2557a84e134Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2567a84e134Smrg    # might cause directories to be created, which would be especially bad
2577a84e134Smrg    # if $src (and thus $dsttmp) contains '*'.
2587a84e134Smrg    if test ! -f "$src" && test ! -d "$src"; then
2597a84e134Smrg      echo "$0: $src does not exist." >&2
2607a84e134Smrg      exit 1
2617a84e134Smrg    fi
2627a84e134Smrg
263ab902922Smrg    if test -z "$dst_arg"; then
2647a84e134Smrg      echo "$0: no destination specified." >&2
2657a84e134Smrg      exit 1
2667a84e134Smrg    fi
267ab902922Smrg    dst=$dst_arg
2687a84e134Smrg
2697a84e134Smrg    # If destination is a directory, append the input filename; won't work
2707a84e134Smrg    # if double slashes aren't ignored.
2717a84e134Smrg    if test -d "$dst"; then
2727a84e134Smrg      if test -n "$no_target_directory"; then
273ab902922Smrg	echo "$0: $dst_arg: Is a directory" >&2
2747a84e134Smrg	exit 1
2757a84e134Smrg      fi
2767a84e134Smrg      dstdir=$dst
2777a84e134Smrg      dst=$dstdir/`basename "$src"`
2787a84e134Smrg      dstdir_status=0
2797a84e134Smrg    else
2807a84e134Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2817a84e134Smrg      dstdir=`
2827a84e134Smrg	(dirname "$dst") 2>/dev/null ||
2837a84e134Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2847a84e134Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2857a84e134Smrg	     X"$dst" : 'X\(//\)$' \| \
2867a84e134Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2877a84e134Smrg	echo X"$dst" |
2887a84e134Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2897a84e134Smrg		   s//\1/
2907a84e134Smrg		   q
2917a84e134Smrg		 }
2927a84e134Smrg		 /^X\(\/\/\)[^/].*/{
2937a84e134Smrg		   s//\1/
2947a84e134Smrg		   q
2957a84e134Smrg		 }
2967a84e134Smrg		 /^X\(\/\/\)$/{
2977a84e134Smrg		   s//\1/
2987a84e134Smrg		   q
2997a84e134Smrg		 }
3007a84e134Smrg		 /^X\(\/\).*/{
3017a84e134Smrg		   s//\1/
3027a84e134Smrg		   q
3037a84e134Smrg		 }
3047a84e134Smrg		 s/.*/./; q'
3057a84e134Smrg      `
3067a84e134Smrg
3077a84e134Smrg      test -d "$dstdir"
3087a84e134Smrg      dstdir_status=$?
3097a84e134Smrg    fi
3107a84e134Smrg  fi
3117a84e134Smrg
3127a84e134Smrg  obsolete_mkdir_used=false
3137a84e134Smrg
3147a84e134Smrg  if test $dstdir_status != 0; then
3157a84e134Smrg    case $posix_mkdir in
3167a84e134Smrg      '')
3177a84e134Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3187a84e134Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3197a84e134Smrg	umask=`umask`
3207a84e134Smrg	case $stripcmd.$umask in
3217a84e134Smrg	  # Optimize common cases.
3227a84e134Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3237a84e134Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3247a84e134Smrg
3257a84e134Smrg	  *[0-7])
3267a84e134Smrg	    mkdir_umask=`expr $umask + 22 \
3277a84e134Smrg	      - $umask % 100 % 40 + $umask % 20 \
3287a84e134Smrg	      - $umask % 10 % 4 + $umask % 2
3297a84e134Smrg	    `;;
3307a84e134Smrg	  *) mkdir_umask=$umask,go-w;;
3317a84e134Smrg	esac
3327a84e134Smrg
3337a84e134Smrg	# With -d, create the new directory with the user-specified mode.
3347a84e134Smrg	# Otherwise, rely on $mkdir_umask.
3357a84e134Smrg	if test -n "$dir_arg"; then
3367a84e134Smrg	  mkdir_mode=-m$mode
3377a84e134Smrg	else
3387a84e134Smrg	  mkdir_mode=
3397a84e134Smrg	fi
3407a84e134Smrg
3417a84e134Smrg	posix_mkdir=false
3427a84e134Smrg	case $umask in
3437a84e134Smrg	  *[123567][0-7][0-7])
3447a84e134Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3457a84e134Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3467a84e134Smrg	    ;;
3477a84e134Smrg	  *)
3487a84e134Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3497a84e134Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3507a84e134Smrg
3517a84e134Smrg	    if (umask $mkdir_umask &&
3527a84e134Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3537a84e134Smrg	    then
3547a84e134Smrg	      if test -z "$dir_arg" || {
3557a84e134Smrg		   # Check for POSIX incompatibilities with -m.
3567a84e134Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357c889a3bfSmrg		   # other-writable bit of parent directory when it shouldn't.
3587a84e134Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3597a84e134Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3607a84e134Smrg		   case $ls_ld_tmpdir in
3617a84e134Smrg		     d????-?r-*) different_mode=700;;
3627a84e134Smrg		     d????-?--*) different_mode=755;;
3637a84e134Smrg		     *) false;;
3647a84e134Smrg		   esac &&
3657a84e134Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3667a84e134Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3677a84e134Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3687a84e134Smrg		   }
3697a84e134Smrg		 }
3707a84e134Smrg	      then posix_mkdir=:
3717a84e134Smrg	      fi
3727a84e134Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3737a84e134Smrg	    else
3747a84e134Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3757a84e134Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3767a84e134Smrg	    fi
3777a84e134Smrg	    trap '' 0;;
3787a84e134Smrg	esac;;
3797a84e134Smrg    esac
3807a84e134Smrg
3817a84e134Smrg    if
3827a84e134Smrg      $posix_mkdir && (
3837a84e134Smrg	umask $mkdir_umask &&
3847a84e134Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3857a84e134Smrg      )
3867a84e134Smrg    then :
3877a84e134Smrg    else
3887a84e134Smrg
3897a84e134Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3907a84e134Smrg      # or it failed possibly due to a race condition.  Create the
3917a84e134Smrg      # directory the slow way, step by step, checking for races as we go.
3927a84e134Smrg
3937a84e134Smrg      case $dstdir in
394ab902922Smrg	/*) prefix='/';;
395421c997bSmrg	[-=\(\)!]*) prefix='./';;
396ab902922Smrg	*)  prefix='';;
3977a84e134Smrg      esac
3987a84e134Smrg
399ab902922Smrg      eval "$initialize_posix_glob"
4007a84e134Smrg
4017a84e134Smrg      oIFS=$IFS
4027a84e134Smrg      IFS=/
403ab902922Smrg      $posix_glob set -f
4047a84e134Smrg      set fnord $dstdir
4057a84e134Smrg      shift
406ab902922Smrg      $posix_glob set +f
4077a84e134Smrg      IFS=$oIFS
4087a84e134Smrg
4097a84e134Smrg      prefixes=
4107a84e134Smrg
4117a84e134Smrg      for d
4127a84e134Smrg      do
413421c997bSmrg	test X"$d" = X && continue
4147a84e134Smrg
4157a84e134Smrg	prefix=$prefix$d
4167a84e134Smrg	if test -d "$prefix"; then
4177a84e134Smrg	  prefixes=
4187a84e134Smrg	else
4197a84e134Smrg	  if $posix_mkdir; then
4207a84e134Smrg	    (umask=$mkdir_umask &&
4217a84e134Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4227a84e134Smrg	    # Don't fail if two instances are running concurrently.
4237a84e134Smrg	    test -d "$prefix" || exit 1
4247a84e134Smrg	  else
4257a84e134Smrg	    case $prefix in
4267a84e134Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4277a84e134Smrg	      *) qprefix=$prefix;;
4287a84e134Smrg	    esac
4297a84e134Smrg	    prefixes="$prefixes '$qprefix'"
4307a84e134Smrg	  fi
4317a84e134Smrg	fi
4327a84e134Smrg	prefix=$prefix/
4337a84e134Smrg      done
4347a84e134Smrg
4357a84e134Smrg      if test -n "$prefixes"; then
4367a84e134Smrg	# Don't fail if two instances are running concurrently.
4377a84e134Smrg	(umask $mkdir_umask &&
4387a84e134Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4397a84e134Smrg	  test -d "$dstdir" || exit 1
4407a84e134Smrg	obsolete_mkdir_used=true
4417a84e134Smrg      fi
4427a84e134Smrg    fi
4437a84e134Smrg  fi
4447a84e134Smrg
4457a84e134Smrg  if test -n "$dir_arg"; then
4467a84e134Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4477a84e134Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4487a84e134Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4497a84e134Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4507a84e134Smrg  else
4517a84e134Smrg
4527a84e134Smrg    # Make a couple of temp file names in the proper directory.
4537a84e134Smrg    dsttmp=$dstdir/_inst.$$_
4547a84e134Smrg    rmtmp=$dstdir/_rm.$$_
4557a84e134Smrg
4567a84e134Smrg    # Trap to clean up those temp files at exit.
4577a84e134Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4587a84e134Smrg
4597a84e134Smrg    # Copy the file name to the temp name.
4607a84e134Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4617a84e134Smrg
4627a84e134Smrg    # and set any options; do chmod last to preserve setuid bits.
4637a84e134Smrg    #
4647a84e134Smrg    # If any of these fail, we abort the whole thing.  If we want to
4657a84e134Smrg    # ignore errors from any of these, just make sure not to ignore
4667a84e134Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4677a84e134Smrg    #
468ab902922Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469ab902922Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470ab902922Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471ab902922Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472ab902922Smrg
473ab902922Smrg    # If -C, don't bother to copy if it wouldn't change the file.
474ab902922Smrg    if $copy_on_change &&
475ab902922Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476ab902922Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477ab902922Smrg
478ab902922Smrg       eval "$initialize_posix_glob" &&
479ab902922Smrg       $posix_glob set -f &&
480ab902922Smrg       set X $old && old=:$2:$4:$5:$6 &&
481ab902922Smrg       set X $new && new=:$2:$4:$5:$6 &&
482ab902922Smrg       $posix_glob set +f &&
483ab902922Smrg
484ab902922Smrg       test "$old" = "$new" &&
485ab902922Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486ab902922Smrg    then
487ab902922Smrg      rm -f "$dsttmp"
488ab902922Smrg    else
489ab902922Smrg      # Rename the file to the real destination.
490ab902922Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491ab902922Smrg
492ab902922Smrg      # The rename failed, perhaps because mv can't rename something else
493ab902922Smrg      # to itself, or perhaps because mv is so ancient that it does not
494ab902922Smrg      # support -f.
495ab902922Smrg      {
496ab902922Smrg	# Now remove or move aside any old file at destination location.
497ab902922Smrg	# We try this two ways since rm can't unlink itself on some
498ab902922Smrg	# systems and the destination file might be busy for other
499ab902922Smrg	# reasons.  In this case, the final cleanup might fail but the new
500ab902922Smrg	# file should still install successfully.
501ab902922Smrg	{
502ab902922Smrg	  test ! -f "$dst" ||
503ab902922Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504ab902922Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505ab902922Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506ab902922Smrg	  } ||
507ab902922Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
508ab902922Smrg	    (exit 1); exit 1
509ab902922Smrg	  }
510ab902922Smrg	} &&
511ab902922Smrg
512ab902922Smrg	# Now rename the file to the real destination.
513ab902922Smrg	$doit $mvcmd "$dsttmp" "$dst"
514ab902922Smrg      }
515ab902922Smrg    fi || exit 1
5167a84e134Smrg
5177a84e134Smrg    trap '' 0
5187a84e134Smrg  fi
5197a84e134Smrgdone
5207a84e134Smrg
5217a84e134Smrg# Local variables:
5227a84e134Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5237a84e134Smrg# time-stamp-start: "scriptversion="
5247a84e134Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525e1e1713cSmrg# time-stamp-time-zone: "UTC"
526e1e1713cSmrg# time-stamp-end: "; # UTC"
5277a84e134Smrg# End:
528