install-sh revision 899129b3
1bd1da9d7Smrg#!/bin/sh
2bd1da9d7Smrg# install - install a program, script, or datafile
3bd1da9d7Smrg
4899129b3Smrgscriptversion=2011-11-20.07; # UTC
5bd1da9d7Smrg
6bd1da9d7Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7bd1da9d7Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8bd1da9d7Smrg# following copyright and license.
9bd1da9d7Smrg#
10bd1da9d7Smrg# Copyright (C) 1994 X Consortium
11bd1da9d7Smrg#
12bd1da9d7Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13bd1da9d7Smrg# of this software and associated documentation files (the "Software"), to
14bd1da9d7Smrg# deal in the Software without restriction, including without limitation the
15bd1da9d7Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16bd1da9d7Smrg# sell copies of the Software, and to permit persons to whom the Software is
17bd1da9d7Smrg# furnished to do so, subject to the following conditions:
18bd1da9d7Smrg#
19bd1da9d7Smrg# The above copyright notice and this permission notice shall be included in
20bd1da9d7Smrg# all copies or substantial portions of the Software.
21bd1da9d7Smrg#
22bd1da9d7Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23bd1da9d7Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24bd1da9d7Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25bd1da9d7Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26bd1da9d7Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27bd1da9d7Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28bd1da9d7Smrg#
29bd1da9d7Smrg# Except as contained in this notice, the name of the X Consortium shall not
30bd1da9d7Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31bd1da9d7Smrg# ings in this Software without prior written authorization from the X Consor-
32bd1da9d7Smrg# tium.
33bd1da9d7Smrg#
34bd1da9d7Smrg#
35bd1da9d7Smrg# FSF changes to this file are in the public domain.
36bd1da9d7Smrg#
37bd1da9d7Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38899129b3Smrg# 'make' implicit rules from creating a file called install from it
39bd1da9d7Smrg# when there is no Makefile.
40bd1da9d7Smrg#
41bd1da9d7Smrg# This script is compatible with the BSD install script, but was written
425bcb6992Smrg# from scratch.
435bcb6992Smrg
445bcb6992Smrgnl='
455bcb6992Smrg'
465bcb6992SmrgIFS=" ""	$nl"
47bd1da9d7Smrg
48bd1da9d7Smrg# set DOITPROG to echo to test this script
49bd1da9d7Smrg
50bd1da9d7Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
515bcb6992Smrgdoit=${DOITPROG-}
525bcb6992Smrgif test -z "$doit"; then
535bcb6992Smrg  doit_exec=exec
545bcb6992Smrgelse
555bcb6992Smrg  doit_exec=$doit
565bcb6992Smrgfi
57bd1da9d7Smrg
585bcb6992Smrg# Put in absolute file names if you don't have them in your path;
595bcb6992Smrg# or use environment vars.
605bcb6992Smrg
615bcb6992Smrgchgrpprog=${CHGRPPROG-chgrp}
625bcb6992Smrgchmodprog=${CHMODPROG-chmod}
635bcb6992Smrgchownprog=${CHOWNPROG-chown}
645bcb6992Smrgcmpprog=${CMPPROG-cmp}
655bcb6992Smrgcpprog=${CPPROG-cp}
665bcb6992Smrgmkdirprog=${MKDIRPROG-mkdir}
675bcb6992Smrgmvprog=${MVPROG-mv}
685bcb6992Smrgrmprog=${RMPROG-rm}
695bcb6992Smrgstripprog=${STRIPPROG-strip}
705bcb6992Smrg
715bcb6992Smrgposix_glob='?'
725bcb6992Smrginitialize_posix_glob='
735bcb6992Smrg  test "$posix_glob" != "?" || {
745bcb6992Smrg    if (set -f) 2>/dev/null; then
755bcb6992Smrg      posix_glob=
765bcb6992Smrg    else
775bcb6992Smrg      posix_glob=:
785bcb6992Smrg    fi
795bcb6992Smrg  }
805bcb6992Smrg'
81bd1da9d7Smrg
825bcb6992Smrgposix_mkdir=
835bcb6992Smrg
845bcb6992Smrg# Desired mode of installed file.
855bcb6992Smrgmode=0755
86bd1da9d7Smrg
87bd1da9d7Smrgchgrpcmd=
885bcb6992Smrgchmodcmd=$chmodprog
895bcb6992Smrgchowncmd=
905bcb6992Smrgmvcmd=$mvprog
91bd1da9d7Smrgrmcmd="$rmprog -f"
925bcb6992Smrgstripcmd=
935bcb6992Smrg
94bd1da9d7Smrgsrc=
95bd1da9d7Smrgdst=
96bd1da9d7Smrgdir_arg=
975bcb6992Smrgdst_arg=
985bcb6992Smrg
995bcb6992Smrgcopy_on_change=false
100bd1da9d7Smrgno_target_directory=
101bd1da9d7Smrg
1025bcb6992Smrgusage="\
1035bcb6992SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104bd1da9d7Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105bd1da9d7Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106bd1da9d7Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107bd1da9d7Smrg
108bd1da9d7SmrgIn the 1st form, copy SRCFILE to DSTFILE.
109bd1da9d7SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110bd1da9d7SmrgIn the 4th, create DIRECTORIES.
111bd1da9d7Smrg
112bd1da9d7SmrgOptions:
1135bcb6992Smrg     --help     display this help and exit.
1145bcb6992Smrg     --version  display version info and exit.
1155bcb6992Smrg
1165bcb6992Smrg  -c            (ignored)
1175bcb6992Smrg  -C            install only if different (preserve the last data modification time)
1185bcb6992Smrg  -d            create directories instead of installing files.
1195bcb6992Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1205bcb6992Smrg  -m MODE       $chmodprog installed files to MODE.
1215bcb6992Smrg  -o USER       $chownprog installed files to USER.
1225bcb6992Smrg  -s            $stripprog installed files.
1235bcb6992Smrg  -t DIRECTORY  install into DIRECTORY.
1245bcb6992Smrg  -T            report an error if DSTFILE is a directory.
125bd1da9d7Smrg
126bd1da9d7SmrgEnvironment variables override the default commands:
1275bcb6992Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1285bcb6992Smrg  RMPROG STRIPPROG
129bd1da9d7Smrg"
130bd1da9d7Smrg
1315bcb6992Smrgwhile test $# -ne 0; do
132bd1da9d7Smrg  case $1 in
1335bcb6992Smrg    -c) ;;
1345bcb6992Smrg
1355bcb6992Smrg    -C) copy_on_change=true;;
136bd1da9d7Smrg
1375bcb6992Smrg    -d) dir_arg=true;;
138bd1da9d7Smrg
139bd1da9d7Smrg    -g) chgrpcmd="$chgrpprog $2"
1405bcb6992Smrg	shift;;
141bd1da9d7Smrg
142bd1da9d7Smrg    --help) echo "$usage"; exit $?;;
143bd1da9d7Smrg
1445bcb6992Smrg    -m) mode=$2
1455bcb6992Smrg	case $mode in
1465bcb6992Smrg	  *' '* | *'	'* | *'
1475bcb6992Smrg'*	  | *'*'* | *'?'* | *'['*)
1485bcb6992Smrg	    echo "$0: invalid mode: $mode" >&2
1495bcb6992Smrg	    exit 1;;
1505bcb6992Smrg	esac
1515bcb6992Smrg	shift;;
152bd1da9d7Smrg
153bd1da9d7Smrg    -o) chowncmd="$chownprog $2"
1545bcb6992Smrg	shift;;
155bd1da9d7Smrg
1565bcb6992Smrg    -s) stripcmd=$stripprog;;
157bd1da9d7Smrg
1585bcb6992Smrg    -t) dst_arg=$2
159899129b3Smrg	# Protect names problematic for 'test' and other utilities.
160899129b3Smrg	case $dst_arg in
161899129b3Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162899129b3Smrg	esac
1635bcb6992Smrg	shift;;
164bd1da9d7Smrg
1655bcb6992Smrg    -T) no_target_directory=true;;
166bd1da9d7Smrg
167bd1da9d7Smrg    --version) echo "$0 $scriptversion"; exit $?;;
168bd1da9d7Smrg
1695bcb6992Smrg    --)	shift
170bd1da9d7Smrg	break;;
1715bcb6992Smrg
1725bcb6992Smrg    -*)	echo "$0: invalid option: $1" >&2
1735bcb6992Smrg	exit 1;;
1745bcb6992Smrg
1755bcb6992Smrg    *)  break;;
176bd1da9d7Smrg  esac
1775bcb6992Smrg  shift
178bd1da9d7Smrgdone
179bd1da9d7Smrg
1805bcb6992Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1815bcb6992Smrg  # When -d is used, all remaining arguments are directories to create.
1825bcb6992Smrg  # When -t is used, the destination is already specified.
1835bcb6992Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1845bcb6992Smrg  for arg
1855bcb6992Smrg  do
1865bcb6992Smrg    if test -n "$dst_arg"; then
1875bcb6992Smrg      # $@ is not empty: it contains at least $arg.
1885bcb6992Smrg      set fnord "$@" "$dst_arg"
1895bcb6992Smrg      shift # fnord
1905bcb6992Smrg    fi
1915bcb6992Smrg    shift # arg
1925bcb6992Smrg    dst_arg=$arg
193899129b3Smrg    # Protect names problematic for 'test' and other utilities.
194899129b3Smrg    case $dst_arg in
195899129b3Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196899129b3Smrg    esac
1975bcb6992Smrg  done
1985bcb6992Smrgfi
1995bcb6992Smrg
2005bcb6992Smrgif test $# -eq 0; then
201bd1da9d7Smrg  if test -z "$dir_arg"; then
202bd1da9d7Smrg    echo "$0: no input file specified." >&2
203bd1da9d7Smrg    exit 1
204bd1da9d7Smrg  fi
205899129b3Smrg  # It's OK to call 'install-sh -d' without argument.
206bd1da9d7Smrg  # This can happen when creating conditional directories.
207bd1da9d7Smrg  exit 0
208bd1da9d7Smrgfi
209bd1da9d7Smrg
2105bcb6992Smrgif test -z "$dir_arg"; then
211899129b3Smrg  do_exit='(exit $ret); exit $ret'
212899129b3Smrg  trap "ret=129; $do_exit" 1
213899129b3Smrg  trap "ret=130; $do_exit" 2
214899129b3Smrg  trap "ret=141; $do_exit" 13
215899129b3Smrg  trap "ret=143; $do_exit" 15
2165bcb6992Smrg
2175bcb6992Smrg  # Set umask so as not to create temps with too-generous modes.
2185bcb6992Smrg  # However, 'strip' requires both read and write access to temps.
2195bcb6992Smrg  case $mode in
2205bcb6992Smrg    # Optimize common cases.
2215bcb6992Smrg    *644) cp_umask=133;;
2225bcb6992Smrg    *755) cp_umask=22;;
2235bcb6992Smrg
2245bcb6992Smrg    *[0-7])
2255bcb6992Smrg      if test -z "$stripcmd"; then
2265bcb6992Smrg	u_plus_rw=
2275bcb6992Smrg      else
2285bcb6992Smrg	u_plus_rw='% 200'
2295bcb6992Smrg      fi
2305bcb6992Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2315bcb6992Smrg    *)
2325bcb6992Smrg      if test -z "$stripcmd"; then
2335bcb6992Smrg	u_plus_rw=
2345bcb6992Smrg      else
2355bcb6992Smrg	u_plus_rw=,u+rw
2365bcb6992Smrg      fi
2375bcb6992Smrg      cp_umask=$mode$u_plus_rw;;
2385bcb6992Smrg  esac
2395bcb6992Smrgfi
2405bcb6992Smrg
241bd1da9d7Smrgfor src
242bd1da9d7Smrgdo
243899129b3Smrg  # Protect names problematic for 'test' and other utilities.
244bd1da9d7Smrg  case $src in
245899129b3Smrg    -* | [=\(\)!]) src=./$src;;
246bd1da9d7Smrg  esac
247bd1da9d7Smrg
248bd1da9d7Smrg  if test -n "$dir_arg"; then
249bd1da9d7Smrg    dst=$src
2505bcb6992Smrg    dstdir=$dst
2515bcb6992Smrg    test -d "$dstdir"
2525bcb6992Smrg    dstdir_status=$?
253bd1da9d7Smrg  else
2545bcb6992Smrg
255bd1da9d7Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256bd1da9d7Smrg    # might cause directories to be created, which would be especially bad
257bd1da9d7Smrg    # if $src (and thus $dsttmp) contains '*'.
258bd1da9d7Smrg    if test ! -f "$src" && test ! -d "$src"; then
259bd1da9d7Smrg      echo "$0: $src does not exist." >&2
260bd1da9d7Smrg      exit 1
261bd1da9d7Smrg    fi
262bd1da9d7Smrg
2635bcb6992Smrg    if test -z "$dst_arg"; then
264bd1da9d7Smrg      echo "$0: no destination specified." >&2
265bd1da9d7Smrg      exit 1
266bd1da9d7Smrg    fi
2675bcb6992Smrg    dst=$dst_arg
268bd1da9d7Smrg
269bd1da9d7Smrg    # If destination is a directory, append the input filename; won't work
270bd1da9d7Smrg    # if double slashes aren't ignored.
271bd1da9d7Smrg    if test -d "$dst"; then
272bd1da9d7Smrg      if test -n "$no_target_directory"; then
2735bcb6992Smrg	echo "$0: $dst_arg: Is a directory" >&2
274bd1da9d7Smrg	exit 1
275bd1da9d7Smrg      fi
2765bcb6992Smrg      dstdir=$dst
2775bcb6992Smrg      dst=$dstdir/`basename "$src"`
2785bcb6992Smrg      dstdir_status=0
2795bcb6992Smrg    else
2805bcb6992Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2815bcb6992Smrg      dstdir=`
2825bcb6992Smrg	(dirname "$dst") 2>/dev/null ||
2835bcb6992Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2845bcb6992Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2855bcb6992Smrg	     X"$dst" : 'X\(//\)$' \| \
2865bcb6992Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2875bcb6992Smrg	echo X"$dst" |
2885bcb6992Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2895bcb6992Smrg		   s//\1/
2905bcb6992Smrg		   q
2915bcb6992Smrg		 }
2925bcb6992Smrg		 /^X\(\/\/\)[^/].*/{
2935bcb6992Smrg		   s//\1/
2945bcb6992Smrg		   q
2955bcb6992Smrg		 }
2965bcb6992Smrg		 /^X\(\/\/\)$/{
2975bcb6992Smrg		   s//\1/
2985bcb6992Smrg		   q
2995bcb6992Smrg		 }
3005bcb6992Smrg		 /^X\(\/\).*/{
3015bcb6992Smrg		   s//\1/
3025bcb6992Smrg		   q
3035bcb6992Smrg		 }
3045bcb6992Smrg		 s/.*/./; q'
3055bcb6992Smrg      `
3065bcb6992Smrg
3075bcb6992Smrg      test -d "$dstdir"
3085bcb6992Smrg      dstdir_status=$?
309bd1da9d7Smrg    fi
310bd1da9d7Smrg  fi
311bd1da9d7Smrg
3125bcb6992Smrg  obsolete_mkdir_used=false
3135bcb6992Smrg
3145bcb6992Smrg  if test $dstdir_status != 0; then
3155bcb6992Smrg    case $posix_mkdir in
3165bcb6992Smrg      '')
3175bcb6992Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3185bcb6992Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3195bcb6992Smrg	umask=`umask`
3205bcb6992Smrg	case $stripcmd.$umask in
3215bcb6992Smrg	  # Optimize common cases.
3225bcb6992Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3235bcb6992Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3245bcb6992Smrg
3255bcb6992Smrg	  *[0-7])
3265bcb6992Smrg	    mkdir_umask=`expr $umask + 22 \
3275bcb6992Smrg	      - $umask % 100 % 40 + $umask % 20 \
3285bcb6992Smrg	      - $umask % 10 % 4 + $umask % 2
3295bcb6992Smrg	    `;;
3305bcb6992Smrg	  *) mkdir_umask=$umask,go-w;;
3315bcb6992Smrg	esac
3325bcb6992Smrg
3335bcb6992Smrg	# With -d, create the new directory with the user-specified mode.
3345bcb6992Smrg	# Otherwise, rely on $mkdir_umask.
3355bcb6992Smrg	if test -n "$dir_arg"; then
3365bcb6992Smrg	  mkdir_mode=-m$mode
3375bcb6992Smrg	else
3385bcb6992Smrg	  mkdir_mode=
3395bcb6992Smrg	fi
3405bcb6992Smrg
3415bcb6992Smrg	posix_mkdir=false
3425bcb6992Smrg	case $umask in
3435bcb6992Smrg	  *[123567][0-7][0-7])
3445bcb6992Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3455bcb6992Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3465bcb6992Smrg	    ;;
3475bcb6992Smrg	  *)
3485bcb6992Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3495bcb6992Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3505bcb6992Smrg
3515bcb6992Smrg	    if (umask $mkdir_umask &&
3525bcb6992Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3535bcb6992Smrg	    then
3545bcb6992Smrg	      if test -z "$dir_arg" || {
3555bcb6992Smrg		   # Check for POSIX incompatibilities with -m.
3565bcb6992Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357899129b3Smrg		   # other-writable bit of parent directory when it shouldn't.
3585bcb6992Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3595bcb6992Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3605bcb6992Smrg		   case $ls_ld_tmpdir in
3615bcb6992Smrg		     d????-?r-*) different_mode=700;;
3625bcb6992Smrg		     d????-?--*) different_mode=755;;
3635bcb6992Smrg		     *) false;;
3645bcb6992Smrg		   esac &&
3655bcb6992Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3665bcb6992Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3675bcb6992Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3685bcb6992Smrg		   }
3695bcb6992Smrg		 }
3705bcb6992Smrg	      then posix_mkdir=:
3715bcb6992Smrg	      fi
3725bcb6992Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3735bcb6992Smrg	    else
3745bcb6992Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3755bcb6992Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3765bcb6992Smrg	    fi
3775bcb6992Smrg	    trap '' 0;;
3785bcb6992Smrg	esac;;
3795bcb6992Smrg    esac
380bd1da9d7Smrg
3815bcb6992Smrg    if
3825bcb6992Smrg      $posix_mkdir && (
3835bcb6992Smrg	umask $mkdir_umask &&
3845bcb6992Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3855bcb6992Smrg      )
3865bcb6992Smrg    then :
3875bcb6992Smrg    else
388bd1da9d7Smrg
3895bcb6992Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3905bcb6992Smrg      # or it failed possibly due to a race condition.  Create the
3915bcb6992Smrg      # directory the slow way, step by step, checking for races as we go.
392bd1da9d7Smrg
3935bcb6992Smrg      case $dstdir in
3945bcb6992Smrg	/*) prefix='/';;
395899129b3Smrg	[-=\(\)!]*) prefix='./';;
3965bcb6992Smrg	*)  prefix='';;
3975bcb6992Smrg      esac
398bd1da9d7Smrg
3995bcb6992Smrg      eval "$initialize_posix_glob"
400bd1da9d7Smrg
4015bcb6992Smrg      oIFS=$IFS
4025bcb6992Smrg      IFS=/
4035bcb6992Smrg      $posix_glob set -f
4045bcb6992Smrg      set fnord $dstdir
405bd1da9d7Smrg      shift
4065bcb6992Smrg      $posix_glob set +f
4075bcb6992Smrg      IFS=$oIFS
4085bcb6992Smrg
4095bcb6992Smrg      prefixes=
4105bcb6992Smrg
4115bcb6992Smrg      for d
4125bcb6992Smrg      do
413899129b3Smrg	test X"$d" = X && continue
4145bcb6992Smrg
4155bcb6992Smrg	prefix=$prefix$d
4165bcb6992Smrg	if test -d "$prefix"; then
4175bcb6992Smrg	  prefixes=
4185bcb6992Smrg	else
4195bcb6992Smrg	  if $posix_mkdir; then
4205bcb6992Smrg	    (umask=$mkdir_umask &&
4215bcb6992Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4225bcb6992Smrg	    # Don't fail if two instances are running concurrently.
4235bcb6992Smrg	    test -d "$prefix" || exit 1
4245bcb6992Smrg	  else
4255bcb6992Smrg	    case $prefix in
4265bcb6992Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4275bcb6992Smrg	      *) qprefix=$prefix;;
4285bcb6992Smrg	    esac
4295bcb6992Smrg	    prefixes="$prefixes '$qprefix'"
4305bcb6992Smrg	  fi
4315bcb6992Smrg	fi
4325bcb6992Smrg	prefix=$prefix/
4335bcb6992Smrg      done
4345bcb6992Smrg
4355bcb6992Smrg      if test -n "$prefixes"; then
4365bcb6992Smrg	# Don't fail if two instances are running concurrently.
4375bcb6992Smrg	(umask $mkdir_umask &&
4385bcb6992Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4395bcb6992Smrg	  test -d "$dstdir" || exit 1
4405bcb6992Smrg	obsolete_mkdir_used=true
441bd1da9d7Smrg      fi
4425bcb6992Smrg    fi
443bd1da9d7Smrg  fi
444bd1da9d7Smrg
445bd1da9d7Smrg  if test -n "$dir_arg"; then
4465bcb6992Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4475bcb6992Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4485bcb6992Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4495bcb6992Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450bd1da9d7Smrg  else
451bd1da9d7Smrg
452bd1da9d7Smrg    # Make a couple of temp file names in the proper directory.
453bd1da9d7Smrg    dsttmp=$dstdir/_inst.$$_
454bd1da9d7Smrg    rmtmp=$dstdir/_rm.$$_
455bd1da9d7Smrg
456bd1da9d7Smrg    # Trap to clean up those temp files at exit.
457bd1da9d7Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458bd1da9d7Smrg
459bd1da9d7Smrg    # Copy the file name to the temp name.
4605bcb6992Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461bd1da9d7Smrg
462bd1da9d7Smrg    # and set any options; do chmod last to preserve setuid bits.
463bd1da9d7Smrg    #
464bd1da9d7Smrg    # If any of these fail, we abort the whole thing.  If we want to
465bd1da9d7Smrg    # ignore errors from any of these, just make sure not to ignore
466bd1da9d7Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
467bd1da9d7Smrg    #
4685bcb6992Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4695bcb6992Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4705bcb6992Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4715bcb6992Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4725bcb6992Smrg
4735bcb6992Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4745bcb6992Smrg    if $copy_on_change &&
4755bcb6992Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4765bcb6992Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4775bcb6992Smrg
4785bcb6992Smrg       eval "$initialize_posix_glob" &&
4795bcb6992Smrg       $posix_glob set -f &&
4805bcb6992Smrg       set X $old && old=:$2:$4:$5:$6 &&
4815bcb6992Smrg       set X $new && new=:$2:$4:$5:$6 &&
4825bcb6992Smrg       $posix_glob set +f &&
4835bcb6992Smrg
4845bcb6992Smrg       test "$old" = "$new" &&
4855bcb6992Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4865bcb6992Smrg    then
4875bcb6992Smrg      rm -f "$dsttmp"
4885bcb6992Smrg    else
4895bcb6992Smrg      # Rename the file to the real destination.
4905bcb6992Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4915bcb6992Smrg
4925bcb6992Smrg      # The rename failed, perhaps because mv can't rename something else
4935bcb6992Smrg      # to itself, or perhaps because mv is so ancient that it does not
4945bcb6992Smrg      # support -f.
4955bcb6992Smrg      {
4965bcb6992Smrg	# Now remove or move aside any old file at destination location.
4975bcb6992Smrg	# We try this two ways since rm can't unlink itself on some
4985bcb6992Smrg	# systems and the destination file might be busy for other
4995bcb6992Smrg	# reasons.  In this case, the final cleanup might fail but the new
5005bcb6992Smrg	# file should still install successfully.
5015bcb6992Smrg	{
5025bcb6992Smrg	  test ! -f "$dst" ||
5035bcb6992Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
5045bcb6992Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5055bcb6992Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
5065bcb6992Smrg	  } ||
5075bcb6992Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5085bcb6992Smrg	    (exit 1); exit 1
5095bcb6992Smrg	  }
5105bcb6992Smrg	} &&
5115bcb6992Smrg
5125bcb6992Smrg	# Now rename the file to the real destination.
5135bcb6992Smrg	$doit $mvcmd "$dsttmp" "$dst"
5145bcb6992Smrg      }
5155bcb6992Smrg    fi || exit 1
5165bcb6992Smrg
5175bcb6992Smrg    trap '' 0
5185bcb6992Smrg  fi
519bd1da9d7Smrgdone
520bd1da9d7Smrg
521bd1da9d7Smrg# Local variables:
522bd1da9d7Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
523bd1da9d7Smrg# time-stamp-start: "scriptversion="
524bd1da9d7Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5255bcb6992Smrg# time-stamp-time-zone: "UTC"
5265bcb6992Smrg# time-stamp-end: "; # UTC"
527bd1da9d7Smrg# End:
528