install-sh revision 8abc0ccf
17a0395d0Smrg#!/bin/sh
27a0395d0Smrg# install - install a program, script, or datafile
37a0395d0Smrg
48abc0ccfSmrgscriptversion=2011-11-20.07; # UTC
57a0395d0Smrg
67a0395d0Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
77a0395d0Smrg# later released in X11R6 (xc/config/util/install.sh) with the
87a0395d0Smrg# following copyright and license.
97a0395d0Smrg#
107a0395d0Smrg# Copyright (C) 1994 X Consortium
117a0395d0Smrg#
127a0395d0Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
137a0395d0Smrg# of this software and associated documentation files (the "Software"), to
147a0395d0Smrg# deal in the Software without restriction, including without limitation the
157a0395d0Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
167a0395d0Smrg# sell copies of the Software, and to permit persons to whom the Software is
177a0395d0Smrg# furnished to do so, subject to the following conditions:
187a0395d0Smrg#
197a0395d0Smrg# The above copyright notice and this permission notice shall be included in
207a0395d0Smrg# all copies or substantial portions of the Software.
217a0395d0Smrg#
227a0395d0Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237a0395d0Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247a0395d0Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
257a0395d0Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
267a0395d0Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
277a0395d0Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
287a0395d0Smrg#
297a0395d0Smrg# Except as contained in this notice, the name of the X Consortium shall not
307a0395d0Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
317a0395d0Smrg# ings in this Software without prior written authorization from the X Consor-
327a0395d0Smrg# tium.
337a0395d0Smrg#
347a0395d0Smrg#
357a0395d0Smrg# FSF changes to this file are in the public domain.
367a0395d0Smrg#
377a0395d0Smrg# Calling this script install-sh is preferred over install.sh, to prevent
388abc0ccfSmrg# 'make' implicit rules from creating a file called install from it
397a0395d0Smrg# when there is no Makefile.
407a0395d0Smrg#
417a0395d0Smrg# This script is compatible with the BSD install script, but was written
427a0395d0Smrg# from scratch.
437a0395d0Smrg
447a0395d0Smrgnl='
457a0395d0Smrg'
467a0395d0SmrgIFS=" ""	$nl"
477a0395d0Smrg
487a0395d0Smrg# set DOITPROG to echo to test this script
497a0395d0Smrg
507a0395d0Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
517a0395d0Smrgdoit=${DOITPROG-}
527a0395d0Smrgif test -z "$doit"; then
537a0395d0Smrg  doit_exec=exec
547a0395d0Smrgelse
557a0395d0Smrg  doit_exec=$doit
567a0395d0Smrgfi
577a0395d0Smrg
587a0395d0Smrg# Put in absolute file names if you don't have them in your path;
597a0395d0Smrg# or use environment vars.
607a0395d0Smrg
617a0395d0Smrgchgrpprog=${CHGRPPROG-chgrp}
627a0395d0Smrgchmodprog=${CHMODPROG-chmod}
637a0395d0Smrgchownprog=${CHOWNPROG-chown}
647a0395d0Smrgcmpprog=${CMPPROG-cmp}
657a0395d0Smrgcpprog=${CPPROG-cp}
667a0395d0Smrgmkdirprog=${MKDIRPROG-mkdir}
677a0395d0Smrgmvprog=${MVPROG-mv}
687a0395d0Smrgrmprog=${RMPROG-rm}
697a0395d0Smrgstripprog=${STRIPPROG-strip}
707a0395d0Smrg
717a0395d0Smrgposix_glob='?'
727a0395d0Smrginitialize_posix_glob='
737a0395d0Smrg  test "$posix_glob" != "?" || {
747a0395d0Smrg    if (set -f) 2>/dev/null; then
757a0395d0Smrg      posix_glob=
767a0395d0Smrg    else
777a0395d0Smrg      posix_glob=:
787a0395d0Smrg    fi
797a0395d0Smrg  }
807a0395d0Smrg'
817a0395d0Smrg
827a0395d0Smrgposix_mkdir=
837a0395d0Smrg
847a0395d0Smrg# Desired mode of installed file.
857a0395d0Smrgmode=0755
867a0395d0Smrg
877a0395d0Smrgchgrpcmd=
887a0395d0Smrgchmodcmd=$chmodprog
897a0395d0Smrgchowncmd=
907a0395d0Smrgmvcmd=$mvprog
917a0395d0Smrgrmcmd="$rmprog -f"
927a0395d0Smrgstripcmd=
937a0395d0Smrg
947a0395d0Smrgsrc=
957a0395d0Smrgdst=
967a0395d0Smrgdir_arg=
977a0395d0Smrgdst_arg=
987a0395d0Smrg
997a0395d0Smrgcopy_on_change=false
1007a0395d0Smrgno_target_directory=
1017a0395d0Smrg
1027a0395d0Smrgusage="\
1037a0395d0SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1047a0395d0Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1057a0395d0Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1067a0395d0Smrg   or: $0 [OPTION]... -d DIRECTORIES...
1077a0395d0Smrg
1087a0395d0SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1097a0395d0SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1107a0395d0SmrgIn the 4th, create DIRECTORIES.
1117a0395d0Smrg
1127a0395d0SmrgOptions:
1137a0395d0Smrg     --help     display this help and exit.
1147a0395d0Smrg     --version  display version info and exit.
1157a0395d0Smrg
1167a0395d0Smrg  -c            (ignored)
1177a0395d0Smrg  -C            install only if different (preserve the last data modification time)
1187a0395d0Smrg  -d            create directories instead of installing files.
1197a0395d0Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1207a0395d0Smrg  -m MODE       $chmodprog installed files to MODE.
1217a0395d0Smrg  -o USER       $chownprog installed files to USER.
1227a0395d0Smrg  -s            $stripprog installed files.
1237a0395d0Smrg  -t DIRECTORY  install into DIRECTORY.
1247a0395d0Smrg  -T            report an error if DSTFILE is a directory.
1257a0395d0Smrg
1267a0395d0SmrgEnvironment variables override the default commands:
1277a0395d0Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1287a0395d0Smrg  RMPROG STRIPPROG
1297a0395d0Smrg"
1307a0395d0Smrg
1317a0395d0Smrgwhile test $# -ne 0; do
1327a0395d0Smrg  case $1 in
1337a0395d0Smrg    -c) ;;
1347a0395d0Smrg
1357a0395d0Smrg    -C) copy_on_change=true;;
1367a0395d0Smrg
1377a0395d0Smrg    -d) dir_arg=true;;
1387a0395d0Smrg
1397a0395d0Smrg    -g) chgrpcmd="$chgrpprog $2"
1407a0395d0Smrg	shift;;
1417a0395d0Smrg
1427a0395d0Smrg    --help) echo "$usage"; exit $?;;
1437a0395d0Smrg
1447a0395d0Smrg    -m) mode=$2
1457a0395d0Smrg	case $mode in
1467a0395d0Smrg	  *' '* | *'	'* | *'
1477a0395d0Smrg'*	  | *'*'* | *'?'* | *'['*)
1487a0395d0Smrg	    echo "$0: invalid mode: $mode" >&2
1497a0395d0Smrg	    exit 1;;
1507a0395d0Smrg	esac
1517a0395d0Smrg	shift;;
1527a0395d0Smrg
1537a0395d0Smrg    -o) chowncmd="$chownprog $2"
1547a0395d0Smrg	shift;;
1557a0395d0Smrg
1567a0395d0Smrg    -s) stripcmd=$stripprog;;
1577a0395d0Smrg
1587a0395d0Smrg    -t) dst_arg=$2
1598abc0ccfSmrg	# Protect names problematic for 'test' and other utilities.
1608abc0ccfSmrg	case $dst_arg in
1618abc0ccfSmrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
1628abc0ccfSmrg	esac
1637a0395d0Smrg	shift;;
1647a0395d0Smrg
1657a0395d0Smrg    -T) no_target_directory=true;;
1667a0395d0Smrg
1677a0395d0Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1687a0395d0Smrg
1697a0395d0Smrg    --)	shift
1707a0395d0Smrg	break;;
1717a0395d0Smrg
1727a0395d0Smrg    -*)	echo "$0: invalid option: $1" >&2
1737a0395d0Smrg	exit 1;;
1747a0395d0Smrg
1757a0395d0Smrg    *)  break;;
1767a0395d0Smrg  esac
1777a0395d0Smrg  shift
1787a0395d0Smrgdone
1797a0395d0Smrg
1807a0395d0Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1817a0395d0Smrg  # When -d is used, all remaining arguments are directories to create.
1827a0395d0Smrg  # When -t is used, the destination is already specified.
1837a0395d0Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1847a0395d0Smrg  for arg
1857a0395d0Smrg  do
1867a0395d0Smrg    if test -n "$dst_arg"; then
1877a0395d0Smrg      # $@ is not empty: it contains at least $arg.
1887a0395d0Smrg      set fnord "$@" "$dst_arg"
1897a0395d0Smrg      shift # fnord
1907a0395d0Smrg    fi
1917a0395d0Smrg    shift # arg
1927a0395d0Smrg    dst_arg=$arg
1938abc0ccfSmrg    # Protect names problematic for 'test' and other utilities.
1948abc0ccfSmrg    case $dst_arg in
1958abc0ccfSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1968abc0ccfSmrg    esac
1977a0395d0Smrg  done
1987a0395d0Smrgfi
1997a0395d0Smrg
2007a0395d0Smrgif test $# -eq 0; then
2017a0395d0Smrg  if test -z "$dir_arg"; then
2027a0395d0Smrg    echo "$0: no input file specified." >&2
2037a0395d0Smrg    exit 1
2047a0395d0Smrg  fi
2058abc0ccfSmrg  # It's OK to call 'install-sh -d' without argument.
2067a0395d0Smrg  # This can happen when creating conditional directories.
2077a0395d0Smrg  exit 0
2087a0395d0Smrgfi
2097a0395d0Smrg
2107a0395d0Smrgif test -z "$dir_arg"; then
2118abc0ccfSmrg  do_exit='(exit $ret); exit $ret'
2128abc0ccfSmrg  trap "ret=129; $do_exit" 1
2138abc0ccfSmrg  trap "ret=130; $do_exit" 2
2148abc0ccfSmrg  trap "ret=141; $do_exit" 13
2158abc0ccfSmrg  trap "ret=143; $do_exit" 15
2167a0395d0Smrg
2177a0395d0Smrg  # Set umask so as not to create temps with too-generous modes.
2187a0395d0Smrg  # However, 'strip' requires both read and write access to temps.
2197a0395d0Smrg  case $mode in
2207a0395d0Smrg    # Optimize common cases.
2217a0395d0Smrg    *644) cp_umask=133;;
2227a0395d0Smrg    *755) cp_umask=22;;
2237a0395d0Smrg
2247a0395d0Smrg    *[0-7])
2257a0395d0Smrg      if test -z "$stripcmd"; then
2267a0395d0Smrg	u_plus_rw=
2277a0395d0Smrg      else
2287a0395d0Smrg	u_plus_rw='% 200'
2297a0395d0Smrg      fi
2307a0395d0Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2317a0395d0Smrg    *)
2327a0395d0Smrg      if test -z "$stripcmd"; then
2337a0395d0Smrg	u_plus_rw=
2347a0395d0Smrg      else
2357a0395d0Smrg	u_plus_rw=,u+rw
2367a0395d0Smrg      fi
2377a0395d0Smrg      cp_umask=$mode$u_plus_rw;;
2387a0395d0Smrg  esac
2397a0395d0Smrgfi
2407a0395d0Smrg
2417a0395d0Smrgfor src
2427a0395d0Smrgdo
2438abc0ccfSmrg  # Protect names problematic for 'test' and other utilities.
2447a0395d0Smrg  case $src in
2458abc0ccfSmrg    -* | [=\(\)!]) src=./$src;;
2467a0395d0Smrg  esac
2477a0395d0Smrg
2487a0395d0Smrg  if test -n "$dir_arg"; then
2497a0395d0Smrg    dst=$src
2507a0395d0Smrg    dstdir=$dst
2517a0395d0Smrg    test -d "$dstdir"
2527a0395d0Smrg    dstdir_status=$?
2537a0395d0Smrg  else
2547a0395d0Smrg
2557a0395d0Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2567a0395d0Smrg    # might cause directories to be created, which would be especially bad
2577a0395d0Smrg    # if $src (and thus $dsttmp) contains '*'.
2587a0395d0Smrg    if test ! -f "$src" && test ! -d "$src"; then
2597a0395d0Smrg      echo "$0: $src does not exist." >&2
2607a0395d0Smrg      exit 1
2617a0395d0Smrg    fi
2627a0395d0Smrg
2637a0395d0Smrg    if test -z "$dst_arg"; then
2647a0395d0Smrg      echo "$0: no destination specified." >&2
2657a0395d0Smrg      exit 1
2667a0395d0Smrg    fi
2677a0395d0Smrg    dst=$dst_arg
2687a0395d0Smrg
2697a0395d0Smrg    # If destination is a directory, append the input filename; won't work
2707a0395d0Smrg    # if double slashes aren't ignored.
2717a0395d0Smrg    if test -d "$dst"; then
2727a0395d0Smrg      if test -n "$no_target_directory"; then
2737a0395d0Smrg	echo "$0: $dst_arg: Is a directory" >&2
2747a0395d0Smrg	exit 1
2757a0395d0Smrg      fi
2767a0395d0Smrg      dstdir=$dst
2777a0395d0Smrg      dst=$dstdir/`basename "$src"`
2787a0395d0Smrg      dstdir_status=0
2797a0395d0Smrg    else
2807a0395d0Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2817a0395d0Smrg      dstdir=`
2827a0395d0Smrg	(dirname "$dst") 2>/dev/null ||
2837a0395d0Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2847a0395d0Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2857a0395d0Smrg	     X"$dst" : 'X\(//\)$' \| \
2867a0395d0Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2877a0395d0Smrg	echo X"$dst" |
2887a0395d0Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2897a0395d0Smrg		   s//\1/
2907a0395d0Smrg		   q
2917a0395d0Smrg		 }
2927a0395d0Smrg		 /^X\(\/\/\)[^/].*/{
2937a0395d0Smrg		   s//\1/
2947a0395d0Smrg		   q
2957a0395d0Smrg		 }
2967a0395d0Smrg		 /^X\(\/\/\)$/{
2977a0395d0Smrg		   s//\1/
2987a0395d0Smrg		   q
2997a0395d0Smrg		 }
3007a0395d0Smrg		 /^X\(\/\).*/{
3017a0395d0Smrg		   s//\1/
3027a0395d0Smrg		   q
3037a0395d0Smrg		 }
3047a0395d0Smrg		 s/.*/./; q'
3057a0395d0Smrg      `
3067a0395d0Smrg
3077a0395d0Smrg      test -d "$dstdir"
3087a0395d0Smrg      dstdir_status=$?
3097a0395d0Smrg    fi
3107a0395d0Smrg  fi
3117a0395d0Smrg
3127a0395d0Smrg  obsolete_mkdir_used=false
3137a0395d0Smrg
3147a0395d0Smrg  if test $dstdir_status != 0; then
3157a0395d0Smrg    case $posix_mkdir in
3167a0395d0Smrg      '')
3177a0395d0Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3187a0395d0Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3197a0395d0Smrg	umask=`umask`
3207a0395d0Smrg	case $stripcmd.$umask in
3217a0395d0Smrg	  # Optimize common cases.
3227a0395d0Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3237a0395d0Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3247a0395d0Smrg
3257a0395d0Smrg	  *[0-7])
3267a0395d0Smrg	    mkdir_umask=`expr $umask + 22 \
3277a0395d0Smrg	      - $umask % 100 % 40 + $umask % 20 \
3287a0395d0Smrg	      - $umask % 10 % 4 + $umask % 2
3297a0395d0Smrg	    `;;
3307a0395d0Smrg	  *) mkdir_umask=$umask,go-w;;
3317a0395d0Smrg	esac
3327a0395d0Smrg
3337a0395d0Smrg	# With -d, create the new directory with the user-specified mode.
3347a0395d0Smrg	# Otherwise, rely on $mkdir_umask.
3357a0395d0Smrg	if test -n "$dir_arg"; then
3367a0395d0Smrg	  mkdir_mode=-m$mode
3377a0395d0Smrg	else
3387a0395d0Smrg	  mkdir_mode=
3397a0395d0Smrg	fi
3407a0395d0Smrg
3417a0395d0Smrg	posix_mkdir=false
3427a0395d0Smrg	case $umask in
3437a0395d0Smrg	  *[123567][0-7][0-7])
3447a0395d0Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3457a0395d0Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3467a0395d0Smrg	    ;;
3477a0395d0Smrg	  *)
3487a0395d0Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3497a0395d0Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3507a0395d0Smrg
3517a0395d0Smrg	    if (umask $mkdir_umask &&
3527a0395d0Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3537a0395d0Smrg	    then
3547a0395d0Smrg	      if test -z "$dir_arg" || {
3557a0395d0Smrg		   # Check for POSIX incompatibilities with -m.
3567a0395d0Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3578abc0ccfSmrg		   # other-writable bit of parent directory when it shouldn't.
3587a0395d0Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3597a0395d0Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3607a0395d0Smrg		   case $ls_ld_tmpdir in
3617a0395d0Smrg		     d????-?r-*) different_mode=700;;
3627a0395d0Smrg		     d????-?--*) different_mode=755;;
3637a0395d0Smrg		     *) false;;
3647a0395d0Smrg		   esac &&
3657a0395d0Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3667a0395d0Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3677a0395d0Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3687a0395d0Smrg		   }
3697a0395d0Smrg		 }
3707a0395d0Smrg	      then posix_mkdir=:
3717a0395d0Smrg	      fi
3727a0395d0Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3737a0395d0Smrg	    else
3747a0395d0Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3757a0395d0Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3767a0395d0Smrg	    fi
3777a0395d0Smrg	    trap '' 0;;
3787a0395d0Smrg	esac;;
3797a0395d0Smrg    esac
3807a0395d0Smrg
3817a0395d0Smrg    if
3827a0395d0Smrg      $posix_mkdir && (
3837a0395d0Smrg	umask $mkdir_umask &&
3847a0395d0Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3857a0395d0Smrg      )
3867a0395d0Smrg    then :
3877a0395d0Smrg    else
3887a0395d0Smrg
3897a0395d0Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3907a0395d0Smrg      # or it failed possibly due to a race condition.  Create the
3917a0395d0Smrg      # directory the slow way, step by step, checking for races as we go.
3927a0395d0Smrg
3937a0395d0Smrg      case $dstdir in
3947a0395d0Smrg	/*) prefix='/';;
3958abc0ccfSmrg	[-=\(\)!]*) prefix='./';;
3967a0395d0Smrg	*)  prefix='';;
3977a0395d0Smrg      esac
3987a0395d0Smrg
3997a0395d0Smrg      eval "$initialize_posix_glob"
4007a0395d0Smrg
4017a0395d0Smrg      oIFS=$IFS
4027a0395d0Smrg      IFS=/
4037a0395d0Smrg      $posix_glob set -f
4047a0395d0Smrg      set fnord $dstdir
4057a0395d0Smrg      shift
4067a0395d0Smrg      $posix_glob set +f
4077a0395d0Smrg      IFS=$oIFS
4087a0395d0Smrg
4097a0395d0Smrg      prefixes=
4107a0395d0Smrg
4117a0395d0Smrg      for d
4127a0395d0Smrg      do
4138abc0ccfSmrg	test X"$d" = X && continue
4147a0395d0Smrg
4157a0395d0Smrg	prefix=$prefix$d
4167a0395d0Smrg	if test -d "$prefix"; then
4177a0395d0Smrg	  prefixes=
4187a0395d0Smrg	else
4197a0395d0Smrg	  if $posix_mkdir; then
4207a0395d0Smrg	    (umask=$mkdir_umask &&
4217a0395d0Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4227a0395d0Smrg	    # Don't fail if two instances are running concurrently.
4237a0395d0Smrg	    test -d "$prefix" || exit 1
4247a0395d0Smrg	  else
4257a0395d0Smrg	    case $prefix in
4267a0395d0Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4277a0395d0Smrg	      *) qprefix=$prefix;;
4287a0395d0Smrg	    esac
4297a0395d0Smrg	    prefixes="$prefixes '$qprefix'"
4307a0395d0Smrg	  fi
4317a0395d0Smrg	fi
4327a0395d0Smrg	prefix=$prefix/
4337a0395d0Smrg      done
4347a0395d0Smrg
4357a0395d0Smrg      if test -n "$prefixes"; then
4367a0395d0Smrg	# Don't fail if two instances are running concurrently.
4377a0395d0Smrg	(umask $mkdir_umask &&
4387a0395d0Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4397a0395d0Smrg	  test -d "$dstdir" || exit 1
4407a0395d0Smrg	obsolete_mkdir_used=true
4417a0395d0Smrg      fi
4427a0395d0Smrg    fi
4437a0395d0Smrg  fi
4447a0395d0Smrg
4457a0395d0Smrg  if test -n "$dir_arg"; then
4467a0395d0Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4477a0395d0Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4487a0395d0Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4497a0395d0Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4507a0395d0Smrg  else
4517a0395d0Smrg
4527a0395d0Smrg    # Make a couple of temp file names in the proper directory.
4537a0395d0Smrg    dsttmp=$dstdir/_inst.$$_
4547a0395d0Smrg    rmtmp=$dstdir/_rm.$$_
4557a0395d0Smrg
4567a0395d0Smrg    # Trap to clean up those temp files at exit.
4577a0395d0Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4587a0395d0Smrg
4597a0395d0Smrg    # Copy the file name to the temp name.
4607a0395d0Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4617a0395d0Smrg
4627a0395d0Smrg    # and set any options; do chmod last to preserve setuid bits.
4637a0395d0Smrg    #
4647a0395d0Smrg    # If any of these fail, we abort the whole thing.  If we want to
4657a0395d0Smrg    # ignore errors from any of these, just make sure not to ignore
4667a0395d0Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4677a0395d0Smrg    #
4687a0395d0Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4697a0395d0Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4707a0395d0Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4717a0395d0Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4727a0395d0Smrg
4737a0395d0Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4747a0395d0Smrg    if $copy_on_change &&
4757a0395d0Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4767a0395d0Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4777a0395d0Smrg
4787a0395d0Smrg       eval "$initialize_posix_glob" &&
4797a0395d0Smrg       $posix_glob set -f &&
4807a0395d0Smrg       set X $old && old=:$2:$4:$5:$6 &&
4817a0395d0Smrg       set X $new && new=:$2:$4:$5:$6 &&
4827a0395d0Smrg       $posix_glob set +f &&
4837a0395d0Smrg
4847a0395d0Smrg       test "$old" = "$new" &&
4857a0395d0Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4867a0395d0Smrg    then
4877a0395d0Smrg      rm -f "$dsttmp"
4887a0395d0Smrg    else
4897a0395d0Smrg      # Rename the file to the real destination.
4907a0395d0Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4917a0395d0Smrg
4927a0395d0Smrg      # The rename failed, perhaps because mv can't rename something else
4937a0395d0Smrg      # to itself, or perhaps because mv is so ancient that it does not
4947a0395d0Smrg      # support -f.
4957a0395d0Smrg      {
4967a0395d0Smrg	# Now remove or move aside any old file at destination location.
4977a0395d0Smrg	# We try this two ways since rm can't unlink itself on some
4987a0395d0Smrg	# systems and the destination file might be busy for other
4997a0395d0Smrg	# reasons.  In this case, the final cleanup might fail but the new
5007a0395d0Smrg	# file should still install successfully.
5017a0395d0Smrg	{
5027a0395d0Smrg	  test ! -f "$dst" ||
5037a0395d0Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
5047a0395d0Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5057a0395d0Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
5067a0395d0Smrg	  } ||
5077a0395d0Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5087a0395d0Smrg	    (exit 1); exit 1
5097a0395d0Smrg	  }
5107a0395d0Smrg	} &&
5117a0395d0Smrg
5127a0395d0Smrg	# Now rename the file to the real destination.
5137a0395d0Smrg	$doit $mvcmd "$dsttmp" "$dst"
5147a0395d0Smrg      }
5157a0395d0Smrg    fi || exit 1
5167a0395d0Smrg
5177a0395d0Smrg    trap '' 0
5187a0395d0Smrg  fi
5197a0395d0Smrgdone
5207a0395d0Smrg
5217a0395d0Smrg# Local variables:
5227a0395d0Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5237a0395d0Smrg# time-stamp-start: "scriptversion="
5247a0395d0Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5257366012aSmrg# time-stamp-time-zone: "UTC"
5267366012aSmrg# time-stamp-end: "; # UTC"
5277a0395d0Smrg# End:
528