install-sh revision 8ae04fa6
18ae04fa6Smrg#!/bin/sh
28ae04fa6Smrg# install - install a program, script, or datafile
38ae04fa6Smrg
48ae04fa6Smrgscriptversion=2006-10-14.15
58ae04fa6Smrg
68ae04fa6Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
78ae04fa6Smrg# later released in X11R6 (xc/config/util/install.sh) with the
88ae04fa6Smrg# following copyright and license.
98ae04fa6Smrg#
108ae04fa6Smrg# Copyright (C) 1994 X Consortium
118ae04fa6Smrg#
128ae04fa6Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
138ae04fa6Smrg# of this software and associated documentation files (the "Software"), to
148ae04fa6Smrg# deal in the Software without restriction, including without limitation the
158ae04fa6Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
168ae04fa6Smrg# sell copies of the Software, and to permit persons to whom the Software is
178ae04fa6Smrg# furnished to do so, subject to the following conditions:
188ae04fa6Smrg#
198ae04fa6Smrg# The above copyright notice and this permission notice shall be included in
208ae04fa6Smrg# all copies or substantial portions of the Software.
218ae04fa6Smrg#
228ae04fa6Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
238ae04fa6Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
248ae04fa6Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
258ae04fa6Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
268ae04fa6Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
278ae04fa6Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
288ae04fa6Smrg#
298ae04fa6Smrg# Except as contained in this notice, the name of the X Consortium shall not
308ae04fa6Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
318ae04fa6Smrg# ings in this Software without prior written authorization from the X Consor-
328ae04fa6Smrg# tium.
338ae04fa6Smrg#
348ae04fa6Smrg#
358ae04fa6Smrg# FSF changes to this file are in the public domain.
368ae04fa6Smrg#
378ae04fa6Smrg# Calling this script install-sh is preferred over install.sh, to prevent
388ae04fa6Smrg# `make' implicit rules from creating a file called install from it
398ae04fa6Smrg# when there is no Makefile.
408ae04fa6Smrg#
418ae04fa6Smrg# This script is compatible with the BSD install script, but was written
428ae04fa6Smrg# from scratch.
438ae04fa6Smrg
448ae04fa6Smrgnl='
458ae04fa6Smrg'
468ae04fa6SmrgIFS=" ""	$nl"
478ae04fa6Smrg
488ae04fa6Smrg# set DOITPROG to echo to test this script
498ae04fa6Smrg
508ae04fa6Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
518ae04fa6Smrgdoit="${DOITPROG-}"
528ae04fa6Smrgif test -z "$doit"; then
538ae04fa6Smrg  doit_exec=exec
548ae04fa6Smrgelse
558ae04fa6Smrg  doit_exec=$doit
568ae04fa6Smrgfi
578ae04fa6Smrg
588ae04fa6Smrg# Put in absolute file names if you don't have them in your path;
598ae04fa6Smrg# or use environment vars.
608ae04fa6Smrg
618ae04fa6Smrgmvprog="${MVPROG-mv}"
628ae04fa6Smrgcpprog="${CPPROG-cp}"
638ae04fa6Smrgchmodprog="${CHMODPROG-chmod}"
648ae04fa6Smrgchownprog="${CHOWNPROG-chown}"
658ae04fa6Smrgchgrpprog="${CHGRPPROG-chgrp}"
668ae04fa6Smrgstripprog="${STRIPPROG-strip}"
678ae04fa6Smrgrmprog="${RMPROG-rm}"
688ae04fa6Smrgmkdirprog="${MKDIRPROG-mkdir}"
698ae04fa6Smrg
708ae04fa6Smrgposix_glob=
718ae04fa6Smrgposix_mkdir=
728ae04fa6Smrg
738ae04fa6Smrg# Desired mode of installed file.
748ae04fa6Smrgmode=0755
758ae04fa6Smrg
768ae04fa6Smrgchmodcmd=$chmodprog
778ae04fa6Smrgchowncmd=
788ae04fa6Smrgchgrpcmd=
798ae04fa6Smrgstripcmd=
808ae04fa6Smrgrmcmd="$rmprog -f"
818ae04fa6Smrgmvcmd="$mvprog"
828ae04fa6Smrgsrc=
838ae04fa6Smrgdst=
848ae04fa6Smrgdir_arg=
858ae04fa6Smrgdstarg=
868ae04fa6Smrgno_target_directory=
878ae04fa6Smrg
888ae04fa6Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
898ae04fa6Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
908ae04fa6Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
918ae04fa6Smrg   or: $0 [OPTION]... -d DIRECTORIES...
928ae04fa6Smrg
938ae04fa6SmrgIn the 1st form, copy SRCFILE to DSTFILE.
948ae04fa6SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
958ae04fa6SmrgIn the 4th, create DIRECTORIES.
968ae04fa6Smrg
978ae04fa6SmrgOptions:
988ae04fa6Smrg-c         (ignored)
998ae04fa6Smrg-d         create directories instead of installing files.
1008ae04fa6Smrg-g GROUP   $chgrpprog installed files to GROUP.
1018ae04fa6Smrg-m MODE    $chmodprog installed files to MODE.
1028ae04fa6Smrg-o USER    $chownprog installed files to USER.
1038ae04fa6Smrg-s         $stripprog installed files.
1048ae04fa6Smrg-t DIRECTORY  install into DIRECTORY.
1058ae04fa6Smrg-T         report an error if DSTFILE is a directory.
1068ae04fa6Smrg--help     display this help and exit.
1078ae04fa6Smrg--version  display version info and exit.
1088ae04fa6Smrg
1098ae04fa6SmrgEnvironment variables override the default commands:
1108ae04fa6Smrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
1118ae04fa6Smrg"
1128ae04fa6Smrg
1138ae04fa6Smrgwhile test $# -ne 0; do
1148ae04fa6Smrg  case $1 in
1158ae04fa6Smrg    -c) shift
1168ae04fa6Smrg        continue;;
1178ae04fa6Smrg
1188ae04fa6Smrg    -d) dir_arg=true
1198ae04fa6Smrg        shift
1208ae04fa6Smrg        continue;;
1218ae04fa6Smrg
1228ae04fa6Smrg    -g) chgrpcmd="$chgrpprog $2"
1238ae04fa6Smrg        shift
1248ae04fa6Smrg        shift
1258ae04fa6Smrg        continue;;
1268ae04fa6Smrg
1278ae04fa6Smrg    --help) echo "$usage"; exit $?;;
1288ae04fa6Smrg
1298ae04fa6Smrg    -m) mode=$2
1308ae04fa6Smrg        shift
1318ae04fa6Smrg        shift
1328ae04fa6Smrg	case $mode in
1338ae04fa6Smrg	  *' '* | *'	'* | *'
1348ae04fa6Smrg'*	  | *'*'* | *'?'* | *'['*)
1358ae04fa6Smrg	    echo "$0: invalid mode: $mode" >&2
1368ae04fa6Smrg	    exit 1;;
1378ae04fa6Smrg	esac
1388ae04fa6Smrg        continue;;
1398ae04fa6Smrg
1408ae04fa6Smrg    -o) chowncmd="$chownprog $2"
1418ae04fa6Smrg        shift
1428ae04fa6Smrg        shift
1438ae04fa6Smrg        continue;;
1448ae04fa6Smrg
1458ae04fa6Smrg    -s) stripcmd=$stripprog
1468ae04fa6Smrg        shift
1478ae04fa6Smrg        continue;;
1488ae04fa6Smrg
1498ae04fa6Smrg    -t) dstarg=$2
1508ae04fa6Smrg	shift
1518ae04fa6Smrg	shift
1528ae04fa6Smrg	continue;;
1538ae04fa6Smrg
1548ae04fa6Smrg    -T) no_target_directory=true
1558ae04fa6Smrg	shift
1568ae04fa6Smrg	continue;;
1578ae04fa6Smrg
1588ae04fa6Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1598ae04fa6Smrg
1608ae04fa6Smrg    --)	shift
1618ae04fa6Smrg	break;;
1628ae04fa6Smrg
1638ae04fa6Smrg    -*)	echo "$0: invalid option: $1" >&2
1648ae04fa6Smrg	exit 1;;
1658ae04fa6Smrg
1668ae04fa6Smrg    *)  break;;
1678ae04fa6Smrg  esac
1688ae04fa6Smrgdone
1698ae04fa6Smrg
1708ae04fa6Smrgif test $# -ne 0 && test -z "$dir_arg$dstarg"; then
1718ae04fa6Smrg  # When -d is used, all remaining arguments are directories to create.
1728ae04fa6Smrg  # When -t is used, the destination is already specified.
1738ae04fa6Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1748ae04fa6Smrg  for arg
1758ae04fa6Smrg  do
1768ae04fa6Smrg    if test -n "$dstarg"; then
1778ae04fa6Smrg      # $@ is not empty: it contains at least $arg.
1788ae04fa6Smrg      set fnord "$@" "$dstarg"
1798ae04fa6Smrg      shift # fnord
1808ae04fa6Smrg    fi
1818ae04fa6Smrg    shift # arg
1828ae04fa6Smrg    dstarg=$arg
1838ae04fa6Smrg  done
1848ae04fa6Smrgfi
1858ae04fa6Smrg
1868ae04fa6Smrgif test $# -eq 0; then
1878ae04fa6Smrg  if test -z "$dir_arg"; then
1888ae04fa6Smrg    echo "$0: no input file specified." >&2
1898ae04fa6Smrg    exit 1
1908ae04fa6Smrg  fi
1918ae04fa6Smrg  # It's OK to call `install-sh -d' without argument.
1928ae04fa6Smrg  # This can happen when creating conditional directories.
1938ae04fa6Smrg  exit 0
1948ae04fa6Smrgfi
1958ae04fa6Smrg
1968ae04fa6Smrgif test -z "$dir_arg"; then
1978ae04fa6Smrg  trap '(exit $?); exit' 1 2 13 15
1988ae04fa6Smrg
1998ae04fa6Smrg  # Set umask so as not to create temps with too-generous modes.
2008ae04fa6Smrg  # However, 'strip' requires both read and write access to temps.
2018ae04fa6Smrg  case $mode in
2028ae04fa6Smrg    # Optimize common cases.
2038ae04fa6Smrg    *644) cp_umask=133;;
2048ae04fa6Smrg    *755) cp_umask=22;;
2058ae04fa6Smrg
2068ae04fa6Smrg    *[0-7])
2078ae04fa6Smrg      if test -z "$stripcmd"; then
2088ae04fa6Smrg	u_plus_rw=
2098ae04fa6Smrg      else
2108ae04fa6Smrg	u_plus_rw='% 200'
2118ae04fa6Smrg      fi
2128ae04fa6Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2138ae04fa6Smrg    *)
2148ae04fa6Smrg      if test -z "$stripcmd"; then
2158ae04fa6Smrg	u_plus_rw=
2168ae04fa6Smrg      else
2178ae04fa6Smrg	u_plus_rw=,u+rw
2188ae04fa6Smrg      fi
2198ae04fa6Smrg      cp_umask=$mode$u_plus_rw;;
2208ae04fa6Smrg  esac
2218ae04fa6Smrgfi
2228ae04fa6Smrg
2238ae04fa6Smrgfor src
2248ae04fa6Smrgdo
2258ae04fa6Smrg  # Protect names starting with `-'.
2268ae04fa6Smrg  case $src in
2278ae04fa6Smrg    -*) src=./$src ;;
2288ae04fa6Smrg  esac
2298ae04fa6Smrg
2308ae04fa6Smrg  if test -n "$dir_arg"; then
2318ae04fa6Smrg    dst=$src
2328ae04fa6Smrg    dstdir=$dst
2338ae04fa6Smrg    test -d "$dstdir"
2348ae04fa6Smrg    dstdir_status=$?
2358ae04fa6Smrg  else
2368ae04fa6Smrg
2378ae04fa6Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2388ae04fa6Smrg    # might cause directories to be created, which would be especially bad
2398ae04fa6Smrg    # if $src (and thus $dsttmp) contains '*'.
2408ae04fa6Smrg    if test ! -f "$src" && test ! -d "$src"; then
2418ae04fa6Smrg      echo "$0: $src does not exist." >&2
2428ae04fa6Smrg      exit 1
2438ae04fa6Smrg    fi
2448ae04fa6Smrg
2458ae04fa6Smrg    if test -z "$dstarg"; then
2468ae04fa6Smrg      echo "$0: no destination specified." >&2
2478ae04fa6Smrg      exit 1
2488ae04fa6Smrg    fi
2498ae04fa6Smrg
2508ae04fa6Smrg    dst=$dstarg
2518ae04fa6Smrg    # Protect names starting with `-'.
2528ae04fa6Smrg    case $dst in
2538ae04fa6Smrg      -*) dst=./$dst ;;
2548ae04fa6Smrg    esac
2558ae04fa6Smrg
2568ae04fa6Smrg    # If destination is a directory, append the input filename; won't work
2578ae04fa6Smrg    # if double slashes aren't ignored.
2588ae04fa6Smrg    if test -d "$dst"; then
2598ae04fa6Smrg      if test -n "$no_target_directory"; then
2608ae04fa6Smrg	echo "$0: $dstarg: Is a directory" >&2
2618ae04fa6Smrg	exit 1
2628ae04fa6Smrg      fi
2638ae04fa6Smrg      dstdir=$dst
2648ae04fa6Smrg      dst=$dstdir/`basename "$src"`
2658ae04fa6Smrg      dstdir_status=0
2668ae04fa6Smrg    else
2678ae04fa6Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2688ae04fa6Smrg      dstdir=`
2698ae04fa6Smrg	(dirname "$dst") 2>/dev/null ||
2708ae04fa6Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2718ae04fa6Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2728ae04fa6Smrg	     X"$dst" : 'X\(//\)$' \| \
2738ae04fa6Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2748ae04fa6Smrg	echo X"$dst" |
2758ae04fa6Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2768ae04fa6Smrg		   s//\1/
2778ae04fa6Smrg		   q
2788ae04fa6Smrg		 }
2798ae04fa6Smrg		 /^X\(\/\/\)[^/].*/{
2808ae04fa6Smrg		   s//\1/
2818ae04fa6Smrg		   q
2828ae04fa6Smrg		 }
2838ae04fa6Smrg		 /^X\(\/\/\)$/{
2848ae04fa6Smrg		   s//\1/
2858ae04fa6Smrg		   q
2868ae04fa6Smrg		 }
2878ae04fa6Smrg		 /^X\(\/\).*/{
2888ae04fa6Smrg		   s//\1/
2898ae04fa6Smrg		   q
2908ae04fa6Smrg		 }
2918ae04fa6Smrg		 s/.*/./; q'
2928ae04fa6Smrg      `
2938ae04fa6Smrg
2948ae04fa6Smrg      test -d "$dstdir"
2958ae04fa6Smrg      dstdir_status=$?
2968ae04fa6Smrg    fi
2978ae04fa6Smrg  fi
2988ae04fa6Smrg
2998ae04fa6Smrg  obsolete_mkdir_used=false
3008ae04fa6Smrg
3018ae04fa6Smrg  if test $dstdir_status != 0; then
3028ae04fa6Smrg    case $posix_mkdir in
3038ae04fa6Smrg      '')
3048ae04fa6Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3058ae04fa6Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3068ae04fa6Smrg	umask=`umask`
3078ae04fa6Smrg	case $stripcmd.$umask in
3088ae04fa6Smrg	  # Optimize common cases.
3098ae04fa6Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3108ae04fa6Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3118ae04fa6Smrg
3128ae04fa6Smrg	  *[0-7])
3138ae04fa6Smrg	    mkdir_umask=`expr $umask + 22 \
3148ae04fa6Smrg	      - $umask % 100 % 40 + $umask % 20 \
3158ae04fa6Smrg	      - $umask % 10 % 4 + $umask % 2
3168ae04fa6Smrg	    `;;
3178ae04fa6Smrg	  *) mkdir_umask=$umask,go-w;;
3188ae04fa6Smrg	esac
3198ae04fa6Smrg
3208ae04fa6Smrg	# With -d, create the new directory with the user-specified mode.
3218ae04fa6Smrg	# Otherwise, rely on $mkdir_umask.
3228ae04fa6Smrg	if test -n "$dir_arg"; then
3238ae04fa6Smrg	  mkdir_mode=-m$mode
3248ae04fa6Smrg	else
3258ae04fa6Smrg	  mkdir_mode=
3268ae04fa6Smrg	fi
3278ae04fa6Smrg
3288ae04fa6Smrg	posix_mkdir=false
3298ae04fa6Smrg	case $umask in
3308ae04fa6Smrg	  *[123567][0-7][0-7])
3318ae04fa6Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3328ae04fa6Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3338ae04fa6Smrg	    ;;
3348ae04fa6Smrg	  *)
3358ae04fa6Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3368ae04fa6Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3378ae04fa6Smrg
3388ae04fa6Smrg	    if (umask $mkdir_umask &&
3398ae04fa6Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3408ae04fa6Smrg	    then
3418ae04fa6Smrg	      if test -z "$dir_arg" || {
3428ae04fa6Smrg		   # Check for POSIX incompatibilities with -m.
3438ae04fa6Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3448ae04fa6Smrg		   # other-writeable bit of parent directory when it shouldn't.
3458ae04fa6Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3468ae04fa6Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3478ae04fa6Smrg		   case $ls_ld_tmpdir in
3488ae04fa6Smrg		     d????-?r-*) different_mode=700;;
3498ae04fa6Smrg		     d????-?--*) different_mode=755;;
3508ae04fa6Smrg		     *) false;;
3518ae04fa6Smrg		   esac &&
3528ae04fa6Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3538ae04fa6Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3548ae04fa6Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3558ae04fa6Smrg		   }
3568ae04fa6Smrg		 }
3578ae04fa6Smrg	      then posix_mkdir=:
3588ae04fa6Smrg	      fi
3598ae04fa6Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3608ae04fa6Smrg	    else
3618ae04fa6Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3628ae04fa6Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3638ae04fa6Smrg	    fi
3648ae04fa6Smrg	    trap '' 0;;
3658ae04fa6Smrg	esac;;
3668ae04fa6Smrg    esac
3678ae04fa6Smrg
3688ae04fa6Smrg    if
3698ae04fa6Smrg      $posix_mkdir && (
3708ae04fa6Smrg	umask $mkdir_umask &&
3718ae04fa6Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3728ae04fa6Smrg      )
3738ae04fa6Smrg    then :
3748ae04fa6Smrg    else
3758ae04fa6Smrg
3768ae04fa6Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3778ae04fa6Smrg      # or it failed possibly due to a race condition.  Create the
3788ae04fa6Smrg      # directory the slow way, step by step, checking for races as we go.
3798ae04fa6Smrg
3808ae04fa6Smrg      case $dstdir in
3818ae04fa6Smrg	/*) prefix=/ ;;
3828ae04fa6Smrg	-*) prefix=./ ;;
3838ae04fa6Smrg	*)  prefix= ;;
3848ae04fa6Smrg      esac
3858ae04fa6Smrg
3868ae04fa6Smrg      case $posix_glob in
3878ae04fa6Smrg        '')
3888ae04fa6Smrg	  if (set -f) 2>/dev/null; then
3898ae04fa6Smrg	    posix_glob=true
3908ae04fa6Smrg	  else
3918ae04fa6Smrg	    posix_glob=false
3928ae04fa6Smrg	  fi ;;
3938ae04fa6Smrg      esac
3948ae04fa6Smrg
3958ae04fa6Smrg      oIFS=$IFS
3968ae04fa6Smrg      IFS=/
3978ae04fa6Smrg      $posix_glob && set -f
3988ae04fa6Smrg      set fnord $dstdir
3998ae04fa6Smrg      shift
4008ae04fa6Smrg      $posix_glob && set +f
4018ae04fa6Smrg      IFS=$oIFS
4028ae04fa6Smrg
4038ae04fa6Smrg      prefixes=
4048ae04fa6Smrg
4058ae04fa6Smrg      for d
4068ae04fa6Smrg      do
4078ae04fa6Smrg	test -z "$d" && continue
4088ae04fa6Smrg
4098ae04fa6Smrg	prefix=$prefix$d
4108ae04fa6Smrg	if test -d "$prefix"; then
4118ae04fa6Smrg	  prefixes=
4128ae04fa6Smrg	else
4138ae04fa6Smrg	  if $posix_mkdir; then
4148ae04fa6Smrg	    (umask=$mkdir_umask &&
4158ae04fa6Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4168ae04fa6Smrg	    # Don't fail if two instances are running concurrently.
4178ae04fa6Smrg	    test -d "$prefix" || exit 1
4188ae04fa6Smrg	  else
4198ae04fa6Smrg	    case $prefix in
4208ae04fa6Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4218ae04fa6Smrg	      *) qprefix=$prefix;;
4228ae04fa6Smrg	    esac
4238ae04fa6Smrg	    prefixes="$prefixes '$qprefix'"
4248ae04fa6Smrg	  fi
4258ae04fa6Smrg	fi
4268ae04fa6Smrg	prefix=$prefix/
4278ae04fa6Smrg      done
4288ae04fa6Smrg
4298ae04fa6Smrg      if test -n "$prefixes"; then
4308ae04fa6Smrg	# Don't fail if two instances are running concurrently.
4318ae04fa6Smrg	(umask $mkdir_umask &&
4328ae04fa6Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4338ae04fa6Smrg	  test -d "$dstdir" || exit 1
4348ae04fa6Smrg	obsolete_mkdir_used=true
4358ae04fa6Smrg      fi
4368ae04fa6Smrg    fi
4378ae04fa6Smrg  fi
4388ae04fa6Smrg
4398ae04fa6Smrg  if test -n "$dir_arg"; then
4408ae04fa6Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4418ae04fa6Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4428ae04fa6Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4438ae04fa6Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4448ae04fa6Smrg  else
4458ae04fa6Smrg
4468ae04fa6Smrg    # Make a couple of temp file names in the proper directory.
4478ae04fa6Smrg    dsttmp=$dstdir/_inst.$$_
4488ae04fa6Smrg    rmtmp=$dstdir/_rm.$$_
4498ae04fa6Smrg
4508ae04fa6Smrg    # Trap to clean up those temp files at exit.
4518ae04fa6Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4528ae04fa6Smrg
4538ae04fa6Smrg    # Copy the file name to the temp name.
4548ae04fa6Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4558ae04fa6Smrg
4568ae04fa6Smrg    # and set any options; do chmod last to preserve setuid bits.
4578ae04fa6Smrg    #
4588ae04fa6Smrg    # If any of these fail, we abort the whole thing.  If we want to
4598ae04fa6Smrg    # ignore errors from any of these, just make sure not to ignore
4608ae04fa6Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4618ae04fa6Smrg    #
4628ae04fa6Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
4638ae04fa6Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
4648ae04fa6Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
4658ae04fa6Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4668ae04fa6Smrg
4678ae04fa6Smrg    # Now rename the file to the real destination.
4688ae04fa6Smrg    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
4698ae04fa6Smrg      || {
4708ae04fa6Smrg	   # The rename failed, perhaps because mv can't rename something else
4718ae04fa6Smrg	   # to itself, or perhaps because mv is so ancient that it does not
4728ae04fa6Smrg	   # support -f.
4738ae04fa6Smrg
4748ae04fa6Smrg	   # Now remove or move aside any old file at destination location.
4758ae04fa6Smrg	   # We try this two ways since rm can't unlink itself on some
4768ae04fa6Smrg	   # systems and the destination file might be busy for other
4778ae04fa6Smrg	   # reasons.  In this case, the final cleanup might fail but the new
4788ae04fa6Smrg	   # file should still install successfully.
4798ae04fa6Smrg	   {
4808ae04fa6Smrg	     if test -f "$dst"; then
4818ae04fa6Smrg	       $doit $rmcmd -f "$dst" 2>/dev/null \
4828ae04fa6Smrg	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
4838ae04fa6Smrg		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
4848ae04fa6Smrg	       || {
4858ae04fa6Smrg		 echo "$0: cannot unlink or rename $dst" >&2
4868ae04fa6Smrg		 (exit 1); exit 1
4878ae04fa6Smrg	       }
4888ae04fa6Smrg	     else
4898ae04fa6Smrg	       :
4908ae04fa6Smrg	     fi
4918ae04fa6Smrg	   } &&
4928ae04fa6Smrg
4938ae04fa6Smrg	   # Now rename the file to the real destination.
4948ae04fa6Smrg	   $doit $mvcmd "$dsttmp" "$dst"
4958ae04fa6Smrg	 }
4968ae04fa6Smrg    } || exit 1
4978ae04fa6Smrg
4988ae04fa6Smrg    trap '' 0
4998ae04fa6Smrg  fi
5008ae04fa6Smrgdone
5018ae04fa6Smrg
5028ae04fa6Smrg# Local variables:
5038ae04fa6Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5048ae04fa6Smrg# time-stamp-start: "scriptversion="
5058ae04fa6Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5068ae04fa6Smrg# time-stamp-end: "$"
5078ae04fa6Smrg# End:
508