install-sh revision 7a84e134
17a84e134Smrg#!/bin/sh
27a84e134Smrg# install - install a program, script, or datafile
37a84e134Smrg
47a84e134Smrgscriptversion=2006-10-14.15
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
387a84e134Smrg# `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.
517a84e134Smrgdoit="${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
617a84e134Smrgmvprog="${MVPROG-mv}"
627a84e134Smrgcpprog="${CPPROG-cp}"
637a84e134Smrgchmodprog="${CHMODPROG-chmod}"
647a84e134Smrgchownprog="${CHOWNPROG-chown}"
657a84e134Smrgchgrpprog="${CHGRPPROG-chgrp}"
667a84e134Smrgstripprog="${STRIPPROG-strip}"
677a84e134Smrgrmprog="${RMPROG-rm}"
687a84e134Smrgmkdirprog="${MKDIRPROG-mkdir}"
697a84e134Smrg
707a84e134Smrgposix_glob=
717a84e134Smrgposix_mkdir=
727a84e134Smrg
737a84e134Smrg# Desired mode of installed file.
747a84e134Smrgmode=0755
757a84e134Smrg
767a84e134Smrgchmodcmd=$chmodprog
777a84e134Smrgchowncmd=
787a84e134Smrgchgrpcmd=
797a84e134Smrgstripcmd=
807a84e134Smrgrmcmd="$rmprog -f"
817a84e134Smrgmvcmd="$mvprog"
827a84e134Smrgsrc=
837a84e134Smrgdst=
847a84e134Smrgdir_arg=
857a84e134Smrgdstarg=
867a84e134Smrgno_target_directory=
877a84e134Smrg
887a84e134Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
897a84e134Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
907a84e134Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
917a84e134Smrg   or: $0 [OPTION]... -d DIRECTORIES...
927a84e134Smrg
937a84e134SmrgIn the 1st form, copy SRCFILE to DSTFILE.
947a84e134SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
957a84e134SmrgIn the 4th, create DIRECTORIES.
967a84e134Smrg
977a84e134SmrgOptions:
987a84e134Smrg-c         (ignored)
997a84e134Smrg-d         create directories instead of installing files.
1007a84e134Smrg-g GROUP   $chgrpprog installed files to GROUP.
1017a84e134Smrg-m MODE    $chmodprog installed files to MODE.
1027a84e134Smrg-o USER    $chownprog installed files to USER.
1037a84e134Smrg-s         $stripprog installed files.
1047a84e134Smrg-t DIRECTORY  install into DIRECTORY.
1057a84e134Smrg-T         report an error if DSTFILE is a directory.
1067a84e134Smrg--help     display this help and exit.
1077a84e134Smrg--version  display version info and exit.
1087a84e134Smrg
1097a84e134SmrgEnvironment variables override the default commands:
1107a84e134Smrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
1117a84e134Smrg"
1127a84e134Smrg
1137a84e134Smrgwhile test $# -ne 0; do
1147a84e134Smrg  case $1 in
1157a84e134Smrg    -c) shift
1167a84e134Smrg        continue;;
1177a84e134Smrg
1187a84e134Smrg    -d) dir_arg=true
1197a84e134Smrg        shift
1207a84e134Smrg        continue;;
1217a84e134Smrg
1227a84e134Smrg    -g) chgrpcmd="$chgrpprog $2"
1237a84e134Smrg        shift
1247a84e134Smrg        shift
1257a84e134Smrg        continue;;
1267a84e134Smrg
1277a84e134Smrg    --help) echo "$usage"; exit $?;;
1287a84e134Smrg
1297a84e134Smrg    -m) mode=$2
1307a84e134Smrg        shift
1317a84e134Smrg        shift
1327a84e134Smrg	case $mode in
1337a84e134Smrg	  *' '* | *'	'* | *'
1347a84e134Smrg'*	  | *'*'* | *'?'* | *'['*)
1357a84e134Smrg	    echo "$0: invalid mode: $mode" >&2
1367a84e134Smrg	    exit 1;;
1377a84e134Smrg	esac
1387a84e134Smrg        continue;;
1397a84e134Smrg
1407a84e134Smrg    -o) chowncmd="$chownprog $2"
1417a84e134Smrg        shift
1427a84e134Smrg        shift
1437a84e134Smrg        continue;;
1447a84e134Smrg
1457a84e134Smrg    -s) stripcmd=$stripprog
1467a84e134Smrg        shift
1477a84e134Smrg        continue;;
1487a84e134Smrg
1497a84e134Smrg    -t) dstarg=$2
1507a84e134Smrg	shift
1517a84e134Smrg	shift
1527a84e134Smrg	continue;;
1537a84e134Smrg
1547a84e134Smrg    -T) no_target_directory=true
1557a84e134Smrg	shift
1567a84e134Smrg	continue;;
1577a84e134Smrg
1587a84e134Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1597a84e134Smrg
1607a84e134Smrg    --)	shift
1617a84e134Smrg	break;;
1627a84e134Smrg
1637a84e134Smrg    -*)	echo "$0: invalid option: $1" >&2
1647a84e134Smrg	exit 1;;
1657a84e134Smrg
1667a84e134Smrg    *)  break;;
1677a84e134Smrg  esac
1687a84e134Smrgdone
1697a84e134Smrg
1707a84e134Smrgif test $# -ne 0 && test -z "$dir_arg$dstarg"; then
1717a84e134Smrg  # When -d is used, all remaining arguments are directories to create.
1727a84e134Smrg  # When -t is used, the destination is already specified.
1737a84e134Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1747a84e134Smrg  for arg
1757a84e134Smrg  do
1767a84e134Smrg    if test -n "$dstarg"; then
1777a84e134Smrg      # $@ is not empty: it contains at least $arg.
1787a84e134Smrg      set fnord "$@" "$dstarg"
1797a84e134Smrg      shift # fnord
1807a84e134Smrg    fi
1817a84e134Smrg    shift # arg
1827a84e134Smrg    dstarg=$arg
1837a84e134Smrg  done
1847a84e134Smrgfi
1857a84e134Smrg
1867a84e134Smrgif test $# -eq 0; then
1877a84e134Smrg  if test -z "$dir_arg"; then
1887a84e134Smrg    echo "$0: no input file specified." >&2
1897a84e134Smrg    exit 1
1907a84e134Smrg  fi
1917a84e134Smrg  # It's OK to call `install-sh -d' without argument.
1927a84e134Smrg  # This can happen when creating conditional directories.
1937a84e134Smrg  exit 0
1947a84e134Smrgfi
1957a84e134Smrg
1967a84e134Smrgif test -z "$dir_arg"; then
1977a84e134Smrg  trap '(exit $?); exit' 1 2 13 15
1987a84e134Smrg
1997a84e134Smrg  # Set umask so as not to create temps with too-generous modes.
2007a84e134Smrg  # However, 'strip' requires both read and write access to temps.
2017a84e134Smrg  case $mode in
2027a84e134Smrg    # Optimize common cases.
2037a84e134Smrg    *644) cp_umask=133;;
2047a84e134Smrg    *755) cp_umask=22;;
2057a84e134Smrg
2067a84e134Smrg    *[0-7])
2077a84e134Smrg      if test -z "$stripcmd"; then
2087a84e134Smrg	u_plus_rw=
2097a84e134Smrg      else
2107a84e134Smrg	u_plus_rw='% 200'
2117a84e134Smrg      fi
2127a84e134Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2137a84e134Smrg    *)
2147a84e134Smrg      if test -z "$stripcmd"; then
2157a84e134Smrg	u_plus_rw=
2167a84e134Smrg      else
2177a84e134Smrg	u_plus_rw=,u+rw
2187a84e134Smrg      fi
2197a84e134Smrg      cp_umask=$mode$u_plus_rw;;
2207a84e134Smrg  esac
2217a84e134Smrgfi
2227a84e134Smrg
2237a84e134Smrgfor src
2247a84e134Smrgdo
2257a84e134Smrg  # Protect names starting with `-'.
2267a84e134Smrg  case $src in
2277a84e134Smrg    -*) src=./$src ;;
2287a84e134Smrg  esac
2297a84e134Smrg
2307a84e134Smrg  if test -n "$dir_arg"; then
2317a84e134Smrg    dst=$src
2327a84e134Smrg    dstdir=$dst
2337a84e134Smrg    test -d "$dstdir"
2347a84e134Smrg    dstdir_status=$?
2357a84e134Smrg  else
2367a84e134Smrg
2377a84e134Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2387a84e134Smrg    # might cause directories to be created, which would be especially bad
2397a84e134Smrg    # if $src (and thus $dsttmp) contains '*'.
2407a84e134Smrg    if test ! -f "$src" && test ! -d "$src"; then
2417a84e134Smrg      echo "$0: $src does not exist." >&2
2427a84e134Smrg      exit 1
2437a84e134Smrg    fi
2447a84e134Smrg
2457a84e134Smrg    if test -z "$dstarg"; then
2467a84e134Smrg      echo "$0: no destination specified." >&2
2477a84e134Smrg      exit 1
2487a84e134Smrg    fi
2497a84e134Smrg
2507a84e134Smrg    dst=$dstarg
2517a84e134Smrg    # Protect names starting with `-'.
2527a84e134Smrg    case $dst in
2537a84e134Smrg      -*) dst=./$dst ;;
2547a84e134Smrg    esac
2557a84e134Smrg
2567a84e134Smrg    # If destination is a directory, append the input filename; won't work
2577a84e134Smrg    # if double slashes aren't ignored.
2587a84e134Smrg    if test -d "$dst"; then
2597a84e134Smrg      if test -n "$no_target_directory"; then
2607a84e134Smrg	echo "$0: $dstarg: Is a directory" >&2
2617a84e134Smrg	exit 1
2627a84e134Smrg      fi
2637a84e134Smrg      dstdir=$dst
2647a84e134Smrg      dst=$dstdir/`basename "$src"`
2657a84e134Smrg      dstdir_status=0
2667a84e134Smrg    else
2677a84e134Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2687a84e134Smrg      dstdir=`
2697a84e134Smrg	(dirname "$dst") 2>/dev/null ||
2707a84e134Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2717a84e134Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2727a84e134Smrg	     X"$dst" : 'X\(//\)$' \| \
2737a84e134Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2747a84e134Smrg	echo X"$dst" |
2757a84e134Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2767a84e134Smrg		   s//\1/
2777a84e134Smrg		   q
2787a84e134Smrg		 }
2797a84e134Smrg		 /^X\(\/\/\)[^/].*/{
2807a84e134Smrg		   s//\1/
2817a84e134Smrg		   q
2827a84e134Smrg		 }
2837a84e134Smrg		 /^X\(\/\/\)$/{
2847a84e134Smrg		   s//\1/
2857a84e134Smrg		   q
2867a84e134Smrg		 }
2877a84e134Smrg		 /^X\(\/\).*/{
2887a84e134Smrg		   s//\1/
2897a84e134Smrg		   q
2907a84e134Smrg		 }
2917a84e134Smrg		 s/.*/./; q'
2927a84e134Smrg      `
2937a84e134Smrg
2947a84e134Smrg      test -d "$dstdir"
2957a84e134Smrg      dstdir_status=$?
2967a84e134Smrg    fi
2977a84e134Smrg  fi
2987a84e134Smrg
2997a84e134Smrg  obsolete_mkdir_used=false
3007a84e134Smrg
3017a84e134Smrg  if test $dstdir_status != 0; then
3027a84e134Smrg    case $posix_mkdir in
3037a84e134Smrg      '')
3047a84e134Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3057a84e134Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3067a84e134Smrg	umask=`umask`
3077a84e134Smrg	case $stripcmd.$umask in
3087a84e134Smrg	  # Optimize common cases.
3097a84e134Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3107a84e134Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3117a84e134Smrg
3127a84e134Smrg	  *[0-7])
3137a84e134Smrg	    mkdir_umask=`expr $umask + 22 \
3147a84e134Smrg	      - $umask % 100 % 40 + $umask % 20 \
3157a84e134Smrg	      - $umask % 10 % 4 + $umask % 2
3167a84e134Smrg	    `;;
3177a84e134Smrg	  *) mkdir_umask=$umask,go-w;;
3187a84e134Smrg	esac
3197a84e134Smrg
3207a84e134Smrg	# With -d, create the new directory with the user-specified mode.
3217a84e134Smrg	# Otherwise, rely on $mkdir_umask.
3227a84e134Smrg	if test -n "$dir_arg"; then
3237a84e134Smrg	  mkdir_mode=-m$mode
3247a84e134Smrg	else
3257a84e134Smrg	  mkdir_mode=
3267a84e134Smrg	fi
3277a84e134Smrg
3287a84e134Smrg	posix_mkdir=false
3297a84e134Smrg	case $umask in
3307a84e134Smrg	  *[123567][0-7][0-7])
3317a84e134Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3327a84e134Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3337a84e134Smrg	    ;;
3347a84e134Smrg	  *)
3357a84e134Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3367a84e134Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3377a84e134Smrg
3387a84e134Smrg	    if (umask $mkdir_umask &&
3397a84e134Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3407a84e134Smrg	    then
3417a84e134Smrg	      if test -z "$dir_arg" || {
3427a84e134Smrg		   # Check for POSIX incompatibilities with -m.
3437a84e134Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3447a84e134Smrg		   # other-writeable bit of parent directory when it shouldn't.
3457a84e134Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3467a84e134Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3477a84e134Smrg		   case $ls_ld_tmpdir in
3487a84e134Smrg		     d????-?r-*) different_mode=700;;
3497a84e134Smrg		     d????-?--*) different_mode=755;;
3507a84e134Smrg		     *) false;;
3517a84e134Smrg		   esac &&
3527a84e134Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3537a84e134Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3547a84e134Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3557a84e134Smrg		   }
3567a84e134Smrg		 }
3577a84e134Smrg	      then posix_mkdir=:
3587a84e134Smrg	      fi
3597a84e134Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3607a84e134Smrg	    else
3617a84e134Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3627a84e134Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3637a84e134Smrg	    fi
3647a84e134Smrg	    trap '' 0;;
3657a84e134Smrg	esac;;
3667a84e134Smrg    esac
3677a84e134Smrg
3687a84e134Smrg    if
3697a84e134Smrg      $posix_mkdir && (
3707a84e134Smrg	umask $mkdir_umask &&
3717a84e134Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3727a84e134Smrg      )
3737a84e134Smrg    then :
3747a84e134Smrg    else
3757a84e134Smrg
3767a84e134Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3777a84e134Smrg      # or it failed possibly due to a race condition.  Create the
3787a84e134Smrg      # directory the slow way, step by step, checking for races as we go.
3797a84e134Smrg
3807a84e134Smrg      case $dstdir in
3817a84e134Smrg	/*) prefix=/ ;;
3827a84e134Smrg	-*) prefix=./ ;;
3837a84e134Smrg	*)  prefix= ;;
3847a84e134Smrg      esac
3857a84e134Smrg
3867a84e134Smrg      case $posix_glob in
3877a84e134Smrg        '')
3887a84e134Smrg	  if (set -f) 2>/dev/null; then
3897a84e134Smrg	    posix_glob=true
3907a84e134Smrg	  else
3917a84e134Smrg	    posix_glob=false
3927a84e134Smrg	  fi ;;
3937a84e134Smrg      esac
3947a84e134Smrg
3957a84e134Smrg      oIFS=$IFS
3967a84e134Smrg      IFS=/
3977a84e134Smrg      $posix_glob && set -f
3987a84e134Smrg      set fnord $dstdir
3997a84e134Smrg      shift
4007a84e134Smrg      $posix_glob && set +f
4017a84e134Smrg      IFS=$oIFS
4027a84e134Smrg
4037a84e134Smrg      prefixes=
4047a84e134Smrg
4057a84e134Smrg      for d
4067a84e134Smrg      do
4077a84e134Smrg	test -z "$d" && continue
4087a84e134Smrg
4097a84e134Smrg	prefix=$prefix$d
4107a84e134Smrg	if test -d "$prefix"; then
4117a84e134Smrg	  prefixes=
4127a84e134Smrg	else
4137a84e134Smrg	  if $posix_mkdir; then
4147a84e134Smrg	    (umask=$mkdir_umask &&
4157a84e134Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4167a84e134Smrg	    # Don't fail if two instances are running concurrently.
4177a84e134Smrg	    test -d "$prefix" || exit 1
4187a84e134Smrg	  else
4197a84e134Smrg	    case $prefix in
4207a84e134Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4217a84e134Smrg	      *) qprefix=$prefix;;
4227a84e134Smrg	    esac
4237a84e134Smrg	    prefixes="$prefixes '$qprefix'"
4247a84e134Smrg	  fi
4257a84e134Smrg	fi
4267a84e134Smrg	prefix=$prefix/
4277a84e134Smrg      done
4287a84e134Smrg
4297a84e134Smrg      if test -n "$prefixes"; then
4307a84e134Smrg	# Don't fail if two instances are running concurrently.
4317a84e134Smrg	(umask $mkdir_umask &&
4327a84e134Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4337a84e134Smrg	  test -d "$dstdir" || exit 1
4347a84e134Smrg	obsolete_mkdir_used=true
4357a84e134Smrg      fi
4367a84e134Smrg    fi
4377a84e134Smrg  fi
4387a84e134Smrg
4397a84e134Smrg  if test -n "$dir_arg"; then
4407a84e134Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4417a84e134Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4427a84e134Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4437a84e134Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4447a84e134Smrg  else
4457a84e134Smrg
4467a84e134Smrg    # Make a couple of temp file names in the proper directory.
4477a84e134Smrg    dsttmp=$dstdir/_inst.$$_
4487a84e134Smrg    rmtmp=$dstdir/_rm.$$_
4497a84e134Smrg
4507a84e134Smrg    # Trap to clean up those temp files at exit.
4517a84e134Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4527a84e134Smrg
4537a84e134Smrg    # Copy the file name to the temp name.
4547a84e134Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4557a84e134Smrg
4567a84e134Smrg    # and set any options; do chmod last to preserve setuid bits.
4577a84e134Smrg    #
4587a84e134Smrg    # If any of these fail, we abort the whole thing.  If we want to
4597a84e134Smrg    # ignore errors from any of these, just make sure not to ignore
4607a84e134Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4617a84e134Smrg    #
4627a84e134Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
4637a84e134Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
4647a84e134Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
4657a84e134Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4667a84e134Smrg
4677a84e134Smrg    # Now rename the file to the real destination.
4687a84e134Smrg    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
4697a84e134Smrg      || {
4707a84e134Smrg	   # The rename failed, perhaps because mv can't rename something else
4717a84e134Smrg	   # to itself, or perhaps because mv is so ancient that it does not
4727a84e134Smrg	   # support -f.
4737a84e134Smrg
4747a84e134Smrg	   # Now remove or move aside any old file at destination location.
4757a84e134Smrg	   # We try this two ways since rm can't unlink itself on some
4767a84e134Smrg	   # systems and the destination file might be busy for other
4777a84e134Smrg	   # reasons.  In this case, the final cleanup might fail but the new
4787a84e134Smrg	   # file should still install successfully.
4797a84e134Smrg	   {
4807a84e134Smrg	     if test -f "$dst"; then
4817a84e134Smrg	       $doit $rmcmd -f "$dst" 2>/dev/null \
4827a84e134Smrg	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
4837a84e134Smrg		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
4847a84e134Smrg	       || {
4857a84e134Smrg		 echo "$0: cannot unlink or rename $dst" >&2
4867a84e134Smrg		 (exit 1); exit 1
4877a84e134Smrg	       }
4887a84e134Smrg	     else
4897a84e134Smrg	       :
4907a84e134Smrg	     fi
4917a84e134Smrg	   } &&
4927a84e134Smrg
4937a84e134Smrg	   # Now rename the file to the real destination.
4947a84e134Smrg	   $doit $mvcmd "$dsttmp" "$dst"
4957a84e134Smrg	 }
4967a84e134Smrg    } || exit 1
4977a84e134Smrg
4987a84e134Smrg    trap '' 0
4997a84e134Smrg  fi
5007a84e134Smrgdone
5017a84e134Smrg
5027a84e134Smrg# Local variables:
5037a84e134Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5047a84e134Smrg# time-stamp-start: "scriptversion="
5057a84e134Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5067a84e134Smrg# time-stamp-end: "$"
5077a84e134Smrg# End:
508