1357bfbb2Smrg#!/bin/sh
2357bfbb2Smrg# install - install a program, script, or datafile
3357bfbb2Smrg
483e03a40Smrgscriptversion=2009-04-28.21; # UTC
5357bfbb2Smrg
6357bfbb2Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7357bfbb2Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8357bfbb2Smrg# following copyright and license.
9357bfbb2Smrg#
10357bfbb2Smrg# Copyright (C) 1994 X Consortium
11357bfbb2Smrg#
12357bfbb2Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13357bfbb2Smrg# of this software and associated documentation files (the "Software"), to
14357bfbb2Smrg# deal in the Software without restriction, including without limitation the
15357bfbb2Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16357bfbb2Smrg# sell copies of the Software, and to permit persons to whom the Software is
17357bfbb2Smrg# furnished to do so, subject to the following conditions:
18357bfbb2Smrg#
19357bfbb2Smrg# The above copyright notice and this permission notice shall be included in
20357bfbb2Smrg# all copies or substantial portions of the Software.
21357bfbb2Smrg#
22357bfbb2Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23357bfbb2Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24357bfbb2Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25357bfbb2Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26357bfbb2Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27357bfbb2Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28357bfbb2Smrg#
29357bfbb2Smrg# Except as contained in this notice, the name of the X Consortium shall not
30357bfbb2Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31357bfbb2Smrg# ings in this Software without prior written authorization from the X Consor-
32357bfbb2Smrg# tium.
33357bfbb2Smrg#
34357bfbb2Smrg#
35357bfbb2Smrg# FSF changes to this file are in the public domain.
36357bfbb2Smrg#
37357bfbb2Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38357bfbb2Smrg# `make' implicit rules from creating a file called install from it
39357bfbb2Smrg# when there is no Makefile.
40357bfbb2Smrg#
41357bfbb2Smrg# This script is compatible with the BSD install script, but was written
4283e03a40Smrg# from scratch.
4383e03a40Smrg
4483e03a40Smrgnl='
4583e03a40Smrg'
4683e03a40SmrgIFS=" ""	$nl"
47357bfbb2Smrg
48357bfbb2Smrg# set DOITPROG to echo to test this script
49357bfbb2Smrg
50357bfbb2Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5183e03a40Smrgdoit=${DOITPROG-}
5283e03a40Smrgif test -z "$doit"; then
5383e03a40Smrg  doit_exec=exec
5483e03a40Smrgelse
5583e03a40Smrg  doit_exec=$doit
5683e03a40Smrgfi
57357bfbb2Smrg
5883e03a40Smrg# Put in absolute file names if you don't have them in your path;
5983e03a40Smrg# or use environment vars.
6083e03a40Smrg
6183e03a40Smrgchgrpprog=${CHGRPPROG-chgrp}
6283e03a40Smrgchmodprog=${CHMODPROG-chmod}
6383e03a40Smrgchownprog=${CHOWNPROG-chown}
6483e03a40Smrgcmpprog=${CMPPROG-cmp}
6583e03a40Smrgcpprog=${CPPROG-cp}
6683e03a40Smrgmkdirprog=${MKDIRPROG-mkdir}
6783e03a40Smrgmvprog=${MVPROG-mv}
6883e03a40Smrgrmprog=${RMPROG-rm}
6983e03a40Smrgstripprog=${STRIPPROG-strip}
7083e03a40Smrg
7183e03a40Smrgposix_glob='?'
7283e03a40Smrginitialize_posix_glob='
7383e03a40Smrg  test "$posix_glob" != "?" || {
7483e03a40Smrg    if (set -f) 2>/dev/null; then
7583e03a40Smrg      posix_glob=
7683e03a40Smrg    else
7783e03a40Smrg      posix_glob=:
7883e03a40Smrg    fi
7983e03a40Smrg  }
8083e03a40Smrg'
81357bfbb2Smrg
8283e03a40Smrgposix_mkdir=
8383e03a40Smrg
8483e03a40Smrg# Desired mode of installed file.
8583e03a40Smrgmode=0755
86357bfbb2Smrg
87357bfbb2Smrgchgrpcmd=
8883e03a40Smrgchmodcmd=$chmodprog
8983e03a40Smrgchowncmd=
9083e03a40Smrgmvcmd=$mvprog
91357bfbb2Smrgrmcmd="$rmprog -f"
9283e03a40Smrgstripcmd=
9383e03a40Smrg
94357bfbb2Smrgsrc=
95357bfbb2Smrgdst=
96357bfbb2Smrgdir_arg=
9783e03a40Smrgdst_arg=
9883e03a40Smrg
9983e03a40Smrgcopy_on_change=false
100357bfbb2Smrgno_target_directory=
101357bfbb2Smrg
10283e03a40Smrgusage="\
10383e03a40SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104357bfbb2Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105357bfbb2Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106357bfbb2Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107357bfbb2Smrg
108357bfbb2SmrgIn the 1st form, copy SRCFILE to DSTFILE.
109357bfbb2SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110357bfbb2SmrgIn the 4th, create DIRECTORIES.
111357bfbb2Smrg
112357bfbb2SmrgOptions:
11383e03a40Smrg     --help     display this help and exit.
11483e03a40Smrg     --version  display version info and exit.
11583e03a40Smrg
11683e03a40Smrg  -c            (ignored)
11783e03a40Smrg  -C            install only if different (preserve the last data modification time)
11883e03a40Smrg  -d            create directories instead of installing files.
11983e03a40Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12083e03a40Smrg  -m MODE       $chmodprog installed files to MODE.
12183e03a40Smrg  -o USER       $chownprog installed files to USER.
12283e03a40Smrg  -s            $stripprog installed files.
12383e03a40Smrg  -t DIRECTORY  install into DIRECTORY.
12483e03a40Smrg  -T            report an error if DSTFILE is a directory.
125357bfbb2Smrg
126357bfbb2SmrgEnvironment variables override the default commands:
12783e03a40Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12883e03a40Smrg  RMPROG STRIPPROG
129357bfbb2Smrg"
130357bfbb2Smrg
13183e03a40Smrgwhile test $# -ne 0; do
132357bfbb2Smrg  case $1 in
13383e03a40Smrg    -c) ;;
13483e03a40Smrg
13583e03a40Smrg    -C) copy_on_change=true;;
136357bfbb2Smrg
13783e03a40Smrg    -d) dir_arg=true;;
138357bfbb2Smrg
139357bfbb2Smrg    -g) chgrpcmd="$chgrpprog $2"
14083e03a40Smrg	shift;;
141357bfbb2Smrg
142357bfbb2Smrg    --help) echo "$usage"; exit $?;;
143357bfbb2Smrg
14483e03a40Smrg    -m) mode=$2
14583e03a40Smrg	case $mode in
14683e03a40Smrg	  *' '* | *'	'* | *'
14783e03a40Smrg'*	  | *'*'* | *'?'* | *'['*)
14883e03a40Smrg	    echo "$0: invalid mode: $mode" >&2
14983e03a40Smrg	    exit 1;;
15083e03a40Smrg	esac
15183e03a40Smrg	shift;;
152357bfbb2Smrg
153357bfbb2Smrg    -o) chowncmd="$chownprog $2"
15483e03a40Smrg	shift;;
155357bfbb2Smrg
15683e03a40Smrg    -s) stripcmd=$stripprog;;
157357bfbb2Smrg
15883e03a40Smrg    -t) dst_arg=$2
15983e03a40Smrg	shift;;
160357bfbb2Smrg
16183e03a40Smrg    -T) no_target_directory=true;;
162357bfbb2Smrg
163357bfbb2Smrg    --version) echo "$0 $scriptversion"; exit $?;;
164357bfbb2Smrg
16583e03a40Smrg    --)	shift
166357bfbb2Smrg	break;;
16783e03a40Smrg
16883e03a40Smrg    -*)	echo "$0: invalid option: $1" >&2
16983e03a40Smrg	exit 1;;
17083e03a40Smrg
17183e03a40Smrg    *)  break;;
172357bfbb2Smrg  esac
17383e03a40Smrg  shift
174357bfbb2Smrgdone
175357bfbb2Smrg
17683e03a40Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17783e03a40Smrg  # When -d is used, all remaining arguments are directories to create.
17883e03a40Smrg  # When -t is used, the destination is already specified.
17983e03a40Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18083e03a40Smrg  for arg
18183e03a40Smrg  do
18283e03a40Smrg    if test -n "$dst_arg"; then
18383e03a40Smrg      # $@ is not empty: it contains at least $arg.
18483e03a40Smrg      set fnord "$@" "$dst_arg"
18583e03a40Smrg      shift # fnord
18683e03a40Smrg    fi
18783e03a40Smrg    shift # arg
18883e03a40Smrg    dst_arg=$arg
18983e03a40Smrg  done
19083e03a40Smrgfi
19183e03a40Smrg
19283e03a40Smrgif test $# -eq 0; then
193357bfbb2Smrg  if test -z "$dir_arg"; then
194357bfbb2Smrg    echo "$0: no input file specified." >&2
195357bfbb2Smrg    exit 1
196357bfbb2Smrg  fi
197357bfbb2Smrg  # It's OK to call `install-sh -d' without argument.
198357bfbb2Smrg  # This can happen when creating conditional directories.
199357bfbb2Smrg  exit 0
200357bfbb2Smrgfi
201357bfbb2Smrg
20283e03a40Smrgif test -z "$dir_arg"; then
20383e03a40Smrg  trap '(exit $?); exit' 1 2 13 15
20483e03a40Smrg
20583e03a40Smrg  # Set umask so as not to create temps with too-generous modes.
20683e03a40Smrg  # However, 'strip' requires both read and write access to temps.
20783e03a40Smrg  case $mode in
20883e03a40Smrg    # Optimize common cases.
20983e03a40Smrg    *644) cp_umask=133;;
21083e03a40Smrg    *755) cp_umask=22;;
21183e03a40Smrg
21283e03a40Smrg    *[0-7])
21383e03a40Smrg      if test -z "$stripcmd"; then
21483e03a40Smrg	u_plus_rw=
21583e03a40Smrg      else
21683e03a40Smrg	u_plus_rw='% 200'
21783e03a40Smrg      fi
21883e03a40Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21983e03a40Smrg    *)
22083e03a40Smrg      if test -z "$stripcmd"; then
22183e03a40Smrg	u_plus_rw=
22283e03a40Smrg      else
22383e03a40Smrg	u_plus_rw=,u+rw
22483e03a40Smrg      fi
22583e03a40Smrg      cp_umask=$mode$u_plus_rw;;
22683e03a40Smrg  esac
22783e03a40Smrgfi
22883e03a40Smrg
229357bfbb2Smrgfor src
230357bfbb2Smrgdo
231357bfbb2Smrg  # Protect names starting with `-'.
232357bfbb2Smrg  case $src in
23383e03a40Smrg    -*) src=./$src;;
234357bfbb2Smrg  esac
235357bfbb2Smrg
236357bfbb2Smrg  if test -n "$dir_arg"; then
237357bfbb2Smrg    dst=$src
23883e03a40Smrg    dstdir=$dst
23983e03a40Smrg    test -d "$dstdir"
24083e03a40Smrg    dstdir_status=$?
241357bfbb2Smrg  else
24283e03a40Smrg
243357bfbb2Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244357bfbb2Smrg    # might cause directories to be created, which would be especially bad
245357bfbb2Smrg    # if $src (and thus $dsttmp) contains '*'.
246357bfbb2Smrg    if test ! -f "$src" && test ! -d "$src"; then
247357bfbb2Smrg      echo "$0: $src does not exist." >&2
248357bfbb2Smrg      exit 1
249357bfbb2Smrg    fi
250357bfbb2Smrg
25183e03a40Smrg    if test -z "$dst_arg"; then
252357bfbb2Smrg      echo "$0: no destination specified." >&2
253357bfbb2Smrg      exit 1
254357bfbb2Smrg    fi
255357bfbb2Smrg
25683e03a40Smrg    dst=$dst_arg
257357bfbb2Smrg    # Protect names starting with `-'.
258357bfbb2Smrg    case $dst in
25983e03a40Smrg      -*) dst=./$dst;;
260357bfbb2Smrg    esac
261357bfbb2Smrg
262357bfbb2Smrg    # If destination is a directory, append the input filename; won't work
263357bfbb2Smrg    # if double slashes aren't ignored.
264357bfbb2Smrg    if test -d "$dst"; then
265357bfbb2Smrg      if test -n "$no_target_directory"; then
26683e03a40Smrg	echo "$0: $dst_arg: Is a directory" >&2
267357bfbb2Smrg	exit 1
268357bfbb2Smrg      fi
26983e03a40Smrg      dstdir=$dst
27083e03a40Smrg      dst=$dstdir/`basename "$src"`
27183e03a40Smrg      dstdir_status=0
27283e03a40Smrg    else
27383e03a40Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27483e03a40Smrg      dstdir=`
27583e03a40Smrg	(dirname "$dst") 2>/dev/null ||
27683e03a40Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27783e03a40Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27883e03a40Smrg	     X"$dst" : 'X\(//\)$' \| \
27983e03a40Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28083e03a40Smrg	echo X"$dst" |
28183e03a40Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28283e03a40Smrg		   s//\1/
28383e03a40Smrg		   q
28483e03a40Smrg		 }
28583e03a40Smrg		 /^X\(\/\/\)[^/].*/{
28683e03a40Smrg		   s//\1/
28783e03a40Smrg		   q
28883e03a40Smrg		 }
28983e03a40Smrg		 /^X\(\/\/\)$/{
29083e03a40Smrg		   s//\1/
29183e03a40Smrg		   q
29283e03a40Smrg		 }
29383e03a40Smrg		 /^X\(\/\).*/{
29483e03a40Smrg		   s//\1/
29583e03a40Smrg		   q
29683e03a40Smrg		 }
29783e03a40Smrg		 s/.*/./; q'
29883e03a40Smrg      `
29983e03a40Smrg
30083e03a40Smrg      test -d "$dstdir"
30183e03a40Smrg      dstdir_status=$?
302357bfbb2Smrg    fi
303357bfbb2Smrg  fi
304357bfbb2Smrg
30583e03a40Smrg  obsolete_mkdir_used=false
30683e03a40Smrg
30783e03a40Smrg  if test $dstdir_status != 0; then
30883e03a40Smrg    case $posix_mkdir in
30983e03a40Smrg      '')
31083e03a40Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31183e03a40Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31283e03a40Smrg	umask=`umask`
31383e03a40Smrg	case $stripcmd.$umask in
31483e03a40Smrg	  # Optimize common cases.
31583e03a40Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31683e03a40Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31783e03a40Smrg
31883e03a40Smrg	  *[0-7])
31983e03a40Smrg	    mkdir_umask=`expr $umask + 22 \
32083e03a40Smrg	      - $umask % 100 % 40 + $umask % 20 \
32183e03a40Smrg	      - $umask % 10 % 4 + $umask % 2
32283e03a40Smrg	    `;;
32383e03a40Smrg	  *) mkdir_umask=$umask,go-w;;
32483e03a40Smrg	esac
32583e03a40Smrg
32683e03a40Smrg	# With -d, create the new directory with the user-specified mode.
32783e03a40Smrg	# Otherwise, rely on $mkdir_umask.
32883e03a40Smrg	if test -n "$dir_arg"; then
32983e03a40Smrg	  mkdir_mode=-m$mode
33083e03a40Smrg	else
33183e03a40Smrg	  mkdir_mode=
33283e03a40Smrg	fi
33383e03a40Smrg
33483e03a40Smrg	posix_mkdir=false
33583e03a40Smrg	case $umask in
33683e03a40Smrg	  *[123567][0-7][0-7])
33783e03a40Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33883e03a40Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33983e03a40Smrg	    ;;
34083e03a40Smrg	  *)
34183e03a40Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34283e03a40Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34383e03a40Smrg
34483e03a40Smrg	    if (umask $mkdir_umask &&
34583e03a40Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34683e03a40Smrg	    then
34783e03a40Smrg	      if test -z "$dir_arg" || {
34883e03a40Smrg		   # Check for POSIX incompatibilities with -m.
34983e03a40Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35083e03a40Smrg		   # other-writeable bit of parent directory when it shouldn't.
35183e03a40Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35283e03a40Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35383e03a40Smrg		   case $ls_ld_tmpdir in
35483e03a40Smrg		     d????-?r-*) different_mode=700;;
35583e03a40Smrg		     d????-?--*) different_mode=755;;
35683e03a40Smrg		     *) false;;
35783e03a40Smrg		   esac &&
35883e03a40Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35983e03a40Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36083e03a40Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36183e03a40Smrg		   }
36283e03a40Smrg		 }
36383e03a40Smrg	      then posix_mkdir=:
36483e03a40Smrg	      fi
36583e03a40Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36683e03a40Smrg	    else
36783e03a40Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36883e03a40Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36983e03a40Smrg	    fi
37083e03a40Smrg	    trap '' 0;;
37183e03a40Smrg	esac;;
37283e03a40Smrg    esac
373357bfbb2Smrg
37483e03a40Smrg    if
37583e03a40Smrg      $posix_mkdir && (
37683e03a40Smrg	umask $mkdir_umask &&
37783e03a40Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37883e03a40Smrg      )
37983e03a40Smrg    then :
38083e03a40Smrg    else
381357bfbb2Smrg
38283e03a40Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38383e03a40Smrg      # or it failed possibly due to a race condition.  Create the
38483e03a40Smrg      # directory the slow way, step by step, checking for races as we go.
385357bfbb2Smrg
38683e03a40Smrg      case $dstdir in
38783e03a40Smrg	/*) prefix='/';;
38883e03a40Smrg	-*) prefix='./';;
38983e03a40Smrg	*)  prefix='';;
39083e03a40Smrg      esac
391357bfbb2Smrg
39283e03a40Smrg      eval "$initialize_posix_glob"
393357bfbb2Smrg
39483e03a40Smrg      oIFS=$IFS
39583e03a40Smrg      IFS=/
39683e03a40Smrg      $posix_glob set -f
39783e03a40Smrg      set fnord $dstdir
398357bfbb2Smrg      shift
39983e03a40Smrg      $posix_glob set +f
40083e03a40Smrg      IFS=$oIFS
40183e03a40Smrg
40283e03a40Smrg      prefixes=
40383e03a40Smrg
40483e03a40Smrg      for d
40583e03a40Smrg      do
40683e03a40Smrg	test -z "$d" && continue
40783e03a40Smrg
40883e03a40Smrg	prefix=$prefix$d
40983e03a40Smrg	if test -d "$prefix"; then
41083e03a40Smrg	  prefixes=
41183e03a40Smrg	else
41283e03a40Smrg	  if $posix_mkdir; then
41383e03a40Smrg	    (umask=$mkdir_umask &&
41483e03a40Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41583e03a40Smrg	    # Don't fail if two instances are running concurrently.
41683e03a40Smrg	    test -d "$prefix" || exit 1
41783e03a40Smrg	  else
41883e03a40Smrg	    case $prefix in
41983e03a40Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42083e03a40Smrg	      *) qprefix=$prefix;;
42183e03a40Smrg	    esac
42283e03a40Smrg	    prefixes="$prefixes '$qprefix'"
42383e03a40Smrg	  fi
42483e03a40Smrg	fi
42583e03a40Smrg	prefix=$prefix/
42683e03a40Smrg      done
42783e03a40Smrg
42883e03a40Smrg      if test -n "$prefixes"; then
42983e03a40Smrg	# Don't fail if two instances are running concurrently.
43083e03a40Smrg	(umask $mkdir_umask &&
43183e03a40Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43283e03a40Smrg	  test -d "$dstdir" || exit 1
43383e03a40Smrg	obsolete_mkdir_used=true
434357bfbb2Smrg      fi
43583e03a40Smrg    fi
436357bfbb2Smrg  fi
437357bfbb2Smrg
438357bfbb2Smrg  if test -n "$dir_arg"; then
43983e03a40Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44083e03a40Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44183e03a40Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44283e03a40Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443357bfbb2Smrg  else
444357bfbb2Smrg
445357bfbb2Smrg    # Make a couple of temp file names in the proper directory.
446357bfbb2Smrg    dsttmp=$dstdir/_inst.$$_
447357bfbb2Smrg    rmtmp=$dstdir/_rm.$$_
448357bfbb2Smrg
449357bfbb2Smrg    # Trap to clean up those temp files at exit.
450357bfbb2Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451357bfbb2Smrg
452357bfbb2Smrg    # Copy the file name to the temp name.
45383e03a40Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454357bfbb2Smrg
455357bfbb2Smrg    # and set any options; do chmod last to preserve setuid bits.
456357bfbb2Smrg    #
457357bfbb2Smrg    # If any of these fail, we abort the whole thing.  If we want to
458357bfbb2Smrg    # ignore errors from any of these, just make sure not to ignore
459357bfbb2Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460357bfbb2Smrg    #
46183e03a40Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46283e03a40Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46383e03a40Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46483e03a40Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46583e03a40Smrg
46683e03a40Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46783e03a40Smrg    if $copy_on_change &&
46883e03a40Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46983e03a40Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47083e03a40Smrg
47183e03a40Smrg       eval "$initialize_posix_glob" &&
47283e03a40Smrg       $posix_glob set -f &&
47383e03a40Smrg       set X $old && old=:$2:$4:$5:$6 &&
47483e03a40Smrg       set X $new && new=:$2:$4:$5:$6 &&
47583e03a40Smrg       $posix_glob set +f &&
47683e03a40Smrg
47783e03a40Smrg       test "$old" = "$new" &&
47883e03a40Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47983e03a40Smrg    then
48083e03a40Smrg      rm -f "$dsttmp"
48183e03a40Smrg    else
48283e03a40Smrg      # Rename the file to the real destination.
48383e03a40Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48483e03a40Smrg
48583e03a40Smrg      # The rename failed, perhaps because mv can't rename something else
48683e03a40Smrg      # to itself, or perhaps because mv is so ancient that it does not
48783e03a40Smrg      # support -f.
48883e03a40Smrg      {
48983e03a40Smrg	# Now remove or move aside any old file at destination location.
49083e03a40Smrg	# We try this two ways since rm can't unlink itself on some
49183e03a40Smrg	# systems and the destination file might be busy for other
49283e03a40Smrg	# reasons.  In this case, the final cleanup might fail but the new
49383e03a40Smrg	# file should still install successfully.
49483e03a40Smrg	{
49583e03a40Smrg	  test ! -f "$dst" ||
49683e03a40Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49783e03a40Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49883e03a40Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49983e03a40Smrg	  } ||
50083e03a40Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50183e03a40Smrg	    (exit 1); exit 1
50283e03a40Smrg	  }
50383e03a40Smrg	} &&
50483e03a40Smrg
50583e03a40Smrg	# Now rename the file to the real destination.
50683e03a40Smrg	$doit $mvcmd "$dsttmp" "$dst"
50783e03a40Smrg      }
50883e03a40Smrg    fi || exit 1
50983e03a40Smrg
51083e03a40Smrg    trap '' 0
51183e03a40Smrg  fi
512357bfbb2Smrgdone
513357bfbb2Smrg
514357bfbb2Smrg# Local variables:
515357bfbb2Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516357bfbb2Smrg# time-stamp-start: "scriptversion="
517357bfbb2Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51883e03a40Smrg# time-stamp-time-zone: "UTC"
51983e03a40Smrg# time-stamp-end: "; # UTC"
520357bfbb2Smrg# End:
521