install-sh revision fb23d3a8
164ce7165Smrg#!/bin/sh
264ce7165Smrg# install - install a program, script, or datafile
364ce7165Smrg
4fb23d3a8Smrgscriptversion=2009-04-28.21; # UTC
564ce7165Smrg
664ce7165Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
764ce7165Smrg# later released in X11R6 (xc/config/util/install.sh) with the
864ce7165Smrg# following copyright and license.
964ce7165Smrg#
1064ce7165Smrg# Copyright (C) 1994 X Consortium
1164ce7165Smrg#
1264ce7165Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1364ce7165Smrg# of this software and associated documentation files (the "Software"), to
1464ce7165Smrg# deal in the Software without restriction, including without limitation the
1564ce7165Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1664ce7165Smrg# sell copies of the Software, and to permit persons to whom the Software is
1764ce7165Smrg# furnished to do so, subject to the following conditions:
1864ce7165Smrg#
1964ce7165Smrg# The above copyright notice and this permission notice shall be included in
2064ce7165Smrg# all copies or substantial portions of the Software.
2164ce7165Smrg#
2264ce7165Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2364ce7165Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2464ce7165Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2564ce7165Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2664ce7165Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2764ce7165Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2864ce7165Smrg#
2964ce7165Smrg# Except as contained in this notice, the name of the X Consortium shall not
3064ce7165Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3164ce7165Smrg# ings in this Software without prior written authorization from the X Consor-
3264ce7165Smrg# tium.
3364ce7165Smrg#
3464ce7165Smrg#
3564ce7165Smrg# FSF changes to this file are in the public domain.
3664ce7165Smrg#
3764ce7165Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3864ce7165Smrg# `make' implicit rules from creating a file called install from it
3964ce7165Smrg# when there is no Makefile.
4064ce7165Smrg#
4164ce7165Smrg# This script is compatible with the BSD install script, but was written
4264ce7165Smrg# from scratch.
4364ce7165Smrg
4464ce7165Smrgnl='
4564ce7165Smrg'
4664ce7165SmrgIFS=" ""	$nl"
4764ce7165Smrg
4864ce7165Smrg# set DOITPROG to echo to test this script
4964ce7165Smrg
5064ce7165Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51fb23d3a8Smrgdoit=${DOITPROG-}
5264ce7165Smrgif test -z "$doit"; then
5364ce7165Smrg  doit_exec=exec
5464ce7165Smrgelse
5564ce7165Smrg  doit_exec=$doit
5664ce7165Smrgfi
5764ce7165Smrg
5864ce7165Smrg# Put in absolute file names if you don't have them in your path;
5964ce7165Smrg# or use environment vars.
6064ce7165Smrg
61fb23d3a8Smrgchgrpprog=${CHGRPPROG-chgrp}
62fb23d3a8Smrgchmodprog=${CHMODPROG-chmod}
63fb23d3a8Smrgchownprog=${CHOWNPROG-chown}
64fb23d3a8Smrgcmpprog=${CMPPROG-cmp}
65fb23d3a8Smrgcpprog=${CPPROG-cp}
66fb23d3a8Smrgmkdirprog=${MKDIRPROG-mkdir}
67fb23d3a8Smrgmvprog=${MVPROG-mv}
68fb23d3a8Smrgrmprog=${RMPROG-rm}
69fb23d3a8Smrgstripprog=${STRIPPROG-strip}
70fb23d3a8Smrg
71fb23d3a8Smrgposix_glob='?'
72fb23d3a8Smrginitialize_posix_glob='
73fb23d3a8Smrg  test "$posix_glob" != "?" || {
74fb23d3a8Smrg    if (set -f) 2>/dev/null; then
75fb23d3a8Smrg      posix_glob=
76fb23d3a8Smrg    else
77fb23d3a8Smrg      posix_glob=:
78fb23d3a8Smrg    fi
79fb23d3a8Smrg  }
80fb23d3a8Smrg'
8164ce7165Smrg
8264ce7165Smrgposix_mkdir=
8364ce7165Smrg
8464ce7165Smrg# Desired mode of installed file.
8564ce7165Smrgmode=0755
8664ce7165Smrg
87fb23d3a8Smrgchgrpcmd=
8864ce7165Smrgchmodcmd=$chmodprog
8964ce7165Smrgchowncmd=
90fb23d3a8Smrgmvcmd=$mvprog
9164ce7165Smrgrmcmd="$rmprog -f"
92fb23d3a8Smrgstripcmd=
93fb23d3a8Smrg
9464ce7165Smrgsrc=
9564ce7165Smrgdst=
9664ce7165Smrgdir_arg=
97fb23d3a8Smrgdst_arg=
98fb23d3a8Smrg
99fb23d3a8Smrgcopy_on_change=false
10064ce7165Smrgno_target_directory=
10164ce7165Smrg
102fb23d3a8Smrgusage="\
103fb23d3a8SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10464ce7165Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10564ce7165Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10664ce7165Smrg   or: $0 [OPTION]... -d DIRECTORIES...
10764ce7165Smrg
10864ce7165SmrgIn the 1st form, copy SRCFILE to DSTFILE.
10964ce7165SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11064ce7165SmrgIn the 4th, create DIRECTORIES.
11164ce7165Smrg
11264ce7165SmrgOptions:
113fb23d3a8Smrg     --help     display this help and exit.
114fb23d3a8Smrg     --version  display version info and exit.
115fb23d3a8Smrg
116fb23d3a8Smrg  -c            (ignored)
117fb23d3a8Smrg  -C            install only if different (preserve the last data modification time)
118fb23d3a8Smrg  -d            create directories instead of installing files.
119fb23d3a8Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120fb23d3a8Smrg  -m MODE       $chmodprog installed files to MODE.
121fb23d3a8Smrg  -o USER       $chownprog installed files to USER.
122fb23d3a8Smrg  -s            $stripprog installed files.
123fb23d3a8Smrg  -t DIRECTORY  install into DIRECTORY.
124fb23d3a8Smrg  -T            report an error if DSTFILE is a directory.
12564ce7165Smrg
12664ce7165SmrgEnvironment variables override the default commands:
127fb23d3a8Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128fb23d3a8Smrg  RMPROG STRIPPROG
12964ce7165Smrg"
13064ce7165Smrg
13164ce7165Smrgwhile test $# -ne 0; do
13264ce7165Smrg  case $1 in
133fb23d3a8Smrg    -c) ;;
134fb23d3a8Smrg
135fb23d3a8Smrg    -C) copy_on_change=true;;
13664ce7165Smrg
137fb23d3a8Smrg    -d) dir_arg=true;;
13864ce7165Smrg
13964ce7165Smrg    -g) chgrpcmd="$chgrpprog $2"
140fb23d3a8Smrg	shift;;
14164ce7165Smrg
14264ce7165Smrg    --help) echo "$usage"; exit $?;;
14364ce7165Smrg
14464ce7165Smrg    -m) mode=$2
14564ce7165Smrg	case $mode in
14664ce7165Smrg	  *' '* | *'	'* | *'
14764ce7165Smrg'*	  | *'*'* | *'?'* | *'['*)
14864ce7165Smrg	    echo "$0: invalid mode: $mode" >&2
14964ce7165Smrg	    exit 1;;
15064ce7165Smrg	esac
151fb23d3a8Smrg	shift;;
15264ce7165Smrg
15364ce7165Smrg    -o) chowncmd="$chownprog $2"
154fb23d3a8Smrg	shift;;
15564ce7165Smrg
156fb23d3a8Smrg    -s) stripcmd=$stripprog;;
15764ce7165Smrg
158fb23d3a8Smrg    -t) dst_arg=$2
159fb23d3a8Smrg	shift;;
16064ce7165Smrg
161fb23d3a8Smrg    -T) no_target_directory=true;;
16264ce7165Smrg
16364ce7165Smrg    --version) echo "$0 $scriptversion"; exit $?;;
16464ce7165Smrg
16564ce7165Smrg    --)	shift
16664ce7165Smrg	break;;
16764ce7165Smrg
16864ce7165Smrg    -*)	echo "$0: invalid option: $1" >&2
16964ce7165Smrg	exit 1;;
17064ce7165Smrg
17164ce7165Smrg    *)  break;;
17264ce7165Smrg  esac
173fb23d3a8Smrg  shift
17464ce7165Smrgdone
17564ce7165Smrg
176fb23d3a8Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17764ce7165Smrg  # When -d is used, all remaining arguments are directories to create.
17864ce7165Smrg  # When -t is used, the destination is already specified.
17964ce7165Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18064ce7165Smrg  for arg
18164ce7165Smrg  do
182fb23d3a8Smrg    if test -n "$dst_arg"; then
18364ce7165Smrg      # $@ is not empty: it contains at least $arg.
184fb23d3a8Smrg      set fnord "$@" "$dst_arg"
18564ce7165Smrg      shift # fnord
18664ce7165Smrg    fi
18764ce7165Smrg    shift # arg
188fb23d3a8Smrg    dst_arg=$arg
18964ce7165Smrg  done
19064ce7165Smrgfi
19164ce7165Smrg
19264ce7165Smrgif test $# -eq 0; then
19364ce7165Smrg  if test -z "$dir_arg"; then
19464ce7165Smrg    echo "$0: no input file specified." >&2
19564ce7165Smrg    exit 1
19664ce7165Smrg  fi
19764ce7165Smrg  # It's OK to call `install-sh -d' without argument.
19864ce7165Smrg  # This can happen when creating conditional directories.
19964ce7165Smrg  exit 0
20064ce7165Smrgfi
20164ce7165Smrg
20264ce7165Smrgif test -z "$dir_arg"; then
20364ce7165Smrg  trap '(exit $?); exit' 1 2 13 15
20464ce7165Smrg
20564ce7165Smrg  # Set umask so as not to create temps with too-generous modes.
20664ce7165Smrg  # However, 'strip' requires both read and write access to temps.
20764ce7165Smrg  case $mode in
20864ce7165Smrg    # Optimize common cases.
20964ce7165Smrg    *644) cp_umask=133;;
21064ce7165Smrg    *755) cp_umask=22;;
21164ce7165Smrg
21264ce7165Smrg    *[0-7])
21364ce7165Smrg      if test -z "$stripcmd"; then
21464ce7165Smrg	u_plus_rw=
21564ce7165Smrg      else
21664ce7165Smrg	u_plus_rw='% 200'
21764ce7165Smrg      fi
21864ce7165Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21964ce7165Smrg    *)
22064ce7165Smrg      if test -z "$stripcmd"; then
22164ce7165Smrg	u_plus_rw=
22264ce7165Smrg      else
22364ce7165Smrg	u_plus_rw=,u+rw
22464ce7165Smrg      fi
22564ce7165Smrg      cp_umask=$mode$u_plus_rw;;
22664ce7165Smrg  esac
22764ce7165Smrgfi
22864ce7165Smrg
22964ce7165Smrgfor src
23064ce7165Smrgdo
23164ce7165Smrg  # Protect names starting with `-'.
23264ce7165Smrg  case $src in
233fb23d3a8Smrg    -*) src=./$src;;
23464ce7165Smrg  esac
23564ce7165Smrg
23664ce7165Smrg  if test -n "$dir_arg"; then
23764ce7165Smrg    dst=$src
23864ce7165Smrg    dstdir=$dst
23964ce7165Smrg    test -d "$dstdir"
24064ce7165Smrg    dstdir_status=$?
24164ce7165Smrg  else
24264ce7165Smrg
24364ce7165Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24464ce7165Smrg    # might cause directories to be created, which would be especially bad
24564ce7165Smrg    # if $src (and thus $dsttmp) contains '*'.
24664ce7165Smrg    if test ! -f "$src" && test ! -d "$src"; then
24764ce7165Smrg      echo "$0: $src does not exist." >&2
24864ce7165Smrg      exit 1
24964ce7165Smrg    fi
25064ce7165Smrg
251fb23d3a8Smrg    if test -z "$dst_arg"; then
25264ce7165Smrg      echo "$0: no destination specified." >&2
25364ce7165Smrg      exit 1
25464ce7165Smrg    fi
25564ce7165Smrg
256fb23d3a8Smrg    dst=$dst_arg
25764ce7165Smrg    # Protect names starting with `-'.
25864ce7165Smrg    case $dst in
259fb23d3a8Smrg      -*) dst=./$dst;;
26064ce7165Smrg    esac
26164ce7165Smrg
26264ce7165Smrg    # If destination is a directory, append the input filename; won't work
26364ce7165Smrg    # if double slashes aren't ignored.
26464ce7165Smrg    if test -d "$dst"; then
26564ce7165Smrg      if test -n "$no_target_directory"; then
266fb23d3a8Smrg	echo "$0: $dst_arg: Is a directory" >&2
26764ce7165Smrg	exit 1
26864ce7165Smrg      fi
26964ce7165Smrg      dstdir=$dst
27064ce7165Smrg      dst=$dstdir/`basename "$src"`
27164ce7165Smrg      dstdir_status=0
27264ce7165Smrg    else
27364ce7165Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27464ce7165Smrg      dstdir=`
27564ce7165Smrg	(dirname "$dst") 2>/dev/null ||
27664ce7165Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27764ce7165Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27864ce7165Smrg	     X"$dst" : 'X\(//\)$' \| \
27964ce7165Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28064ce7165Smrg	echo X"$dst" |
28164ce7165Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28264ce7165Smrg		   s//\1/
28364ce7165Smrg		   q
28464ce7165Smrg		 }
28564ce7165Smrg		 /^X\(\/\/\)[^/].*/{
28664ce7165Smrg		   s//\1/
28764ce7165Smrg		   q
28864ce7165Smrg		 }
28964ce7165Smrg		 /^X\(\/\/\)$/{
29064ce7165Smrg		   s//\1/
29164ce7165Smrg		   q
29264ce7165Smrg		 }
29364ce7165Smrg		 /^X\(\/\).*/{
29464ce7165Smrg		   s//\1/
29564ce7165Smrg		   q
29664ce7165Smrg		 }
29764ce7165Smrg		 s/.*/./; q'
29864ce7165Smrg      `
29964ce7165Smrg
30064ce7165Smrg      test -d "$dstdir"
30164ce7165Smrg      dstdir_status=$?
30264ce7165Smrg    fi
30364ce7165Smrg  fi
30464ce7165Smrg
30564ce7165Smrg  obsolete_mkdir_used=false
30664ce7165Smrg
30764ce7165Smrg  if test $dstdir_status != 0; then
30864ce7165Smrg    case $posix_mkdir in
30964ce7165Smrg      '')
31064ce7165Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31164ce7165Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31264ce7165Smrg	umask=`umask`
31364ce7165Smrg	case $stripcmd.$umask in
31464ce7165Smrg	  # Optimize common cases.
31564ce7165Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31664ce7165Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31764ce7165Smrg
31864ce7165Smrg	  *[0-7])
31964ce7165Smrg	    mkdir_umask=`expr $umask + 22 \
32064ce7165Smrg	      - $umask % 100 % 40 + $umask % 20 \
32164ce7165Smrg	      - $umask % 10 % 4 + $umask % 2
32264ce7165Smrg	    `;;
32364ce7165Smrg	  *) mkdir_umask=$umask,go-w;;
32464ce7165Smrg	esac
32564ce7165Smrg
32664ce7165Smrg	# With -d, create the new directory with the user-specified mode.
32764ce7165Smrg	# Otherwise, rely on $mkdir_umask.
32864ce7165Smrg	if test -n "$dir_arg"; then
32964ce7165Smrg	  mkdir_mode=-m$mode
33064ce7165Smrg	else
33164ce7165Smrg	  mkdir_mode=
33264ce7165Smrg	fi
33364ce7165Smrg
33464ce7165Smrg	posix_mkdir=false
33564ce7165Smrg	case $umask in
33664ce7165Smrg	  *[123567][0-7][0-7])
33764ce7165Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33864ce7165Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33964ce7165Smrg	    ;;
34064ce7165Smrg	  *)
34164ce7165Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34264ce7165Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34364ce7165Smrg
34464ce7165Smrg	    if (umask $mkdir_umask &&
34564ce7165Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34664ce7165Smrg	    then
34764ce7165Smrg	      if test -z "$dir_arg" || {
34864ce7165Smrg		   # Check for POSIX incompatibilities with -m.
34964ce7165Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35064ce7165Smrg		   # other-writeable bit of parent directory when it shouldn't.
35164ce7165Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35264ce7165Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35364ce7165Smrg		   case $ls_ld_tmpdir in
35464ce7165Smrg		     d????-?r-*) different_mode=700;;
35564ce7165Smrg		     d????-?--*) different_mode=755;;
35664ce7165Smrg		     *) false;;
35764ce7165Smrg		   esac &&
35864ce7165Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35964ce7165Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36064ce7165Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36164ce7165Smrg		   }
36264ce7165Smrg		 }
36364ce7165Smrg	      then posix_mkdir=:
36464ce7165Smrg	      fi
36564ce7165Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36664ce7165Smrg	    else
36764ce7165Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36864ce7165Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36964ce7165Smrg	    fi
37064ce7165Smrg	    trap '' 0;;
37164ce7165Smrg	esac;;
37264ce7165Smrg    esac
37364ce7165Smrg
37464ce7165Smrg    if
37564ce7165Smrg      $posix_mkdir && (
37664ce7165Smrg	umask $mkdir_umask &&
37764ce7165Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37864ce7165Smrg      )
37964ce7165Smrg    then :
38064ce7165Smrg    else
38164ce7165Smrg
38264ce7165Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38364ce7165Smrg      # or it failed possibly due to a race condition.  Create the
38464ce7165Smrg      # directory the slow way, step by step, checking for races as we go.
38564ce7165Smrg
38664ce7165Smrg      case $dstdir in
387fb23d3a8Smrg	/*) prefix='/';;
388fb23d3a8Smrg	-*) prefix='./';;
389fb23d3a8Smrg	*)  prefix='';;
39064ce7165Smrg      esac
39164ce7165Smrg
392fb23d3a8Smrg      eval "$initialize_posix_glob"
39364ce7165Smrg
39464ce7165Smrg      oIFS=$IFS
39564ce7165Smrg      IFS=/
396fb23d3a8Smrg      $posix_glob set -f
39764ce7165Smrg      set fnord $dstdir
39864ce7165Smrg      shift
399fb23d3a8Smrg      $posix_glob set +f
40064ce7165Smrg      IFS=$oIFS
40164ce7165Smrg
40264ce7165Smrg      prefixes=
40364ce7165Smrg
40464ce7165Smrg      for d
40564ce7165Smrg      do
40664ce7165Smrg	test -z "$d" && continue
40764ce7165Smrg
40864ce7165Smrg	prefix=$prefix$d
40964ce7165Smrg	if test -d "$prefix"; then
41064ce7165Smrg	  prefixes=
41164ce7165Smrg	else
41264ce7165Smrg	  if $posix_mkdir; then
41364ce7165Smrg	    (umask=$mkdir_umask &&
41464ce7165Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41564ce7165Smrg	    # Don't fail if two instances are running concurrently.
41664ce7165Smrg	    test -d "$prefix" || exit 1
41764ce7165Smrg	  else
41864ce7165Smrg	    case $prefix in
41964ce7165Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42064ce7165Smrg	      *) qprefix=$prefix;;
42164ce7165Smrg	    esac
42264ce7165Smrg	    prefixes="$prefixes '$qprefix'"
42364ce7165Smrg	  fi
42464ce7165Smrg	fi
42564ce7165Smrg	prefix=$prefix/
42664ce7165Smrg      done
42764ce7165Smrg
42864ce7165Smrg      if test -n "$prefixes"; then
42964ce7165Smrg	# Don't fail if two instances are running concurrently.
43064ce7165Smrg	(umask $mkdir_umask &&
43164ce7165Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43264ce7165Smrg	  test -d "$dstdir" || exit 1
43364ce7165Smrg	obsolete_mkdir_used=true
43464ce7165Smrg      fi
43564ce7165Smrg    fi
43664ce7165Smrg  fi
43764ce7165Smrg
43864ce7165Smrg  if test -n "$dir_arg"; then
43964ce7165Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44064ce7165Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44164ce7165Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44264ce7165Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44364ce7165Smrg  else
44464ce7165Smrg
44564ce7165Smrg    # Make a couple of temp file names in the proper directory.
44664ce7165Smrg    dsttmp=$dstdir/_inst.$$_
44764ce7165Smrg    rmtmp=$dstdir/_rm.$$_
44864ce7165Smrg
44964ce7165Smrg    # Trap to clean up those temp files at exit.
45064ce7165Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45164ce7165Smrg
45264ce7165Smrg    # Copy the file name to the temp name.
45364ce7165Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45464ce7165Smrg
45564ce7165Smrg    # and set any options; do chmod last to preserve setuid bits.
45664ce7165Smrg    #
45764ce7165Smrg    # If any of these fail, we abort the whole thing.  If we want to
45864ce7165Smrg    # ignore errors from any of these, just make sure not to ignore
45964ce7165Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46064ce7165Smrg    #
461fb23d3a8Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462fb23d3a8Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463fb23d3a8Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464fb23d3a8Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465fb23d3a8Smrg
466fb23d3a8Smrg    # If -C, don't bother to copy if it wouldn't change the file.
467fb23d3a8Smrg    if $copy_on_change &&
468fb23d3a8Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469fb23d3a8Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470fb23d3a8Smrg
471fb23d3a8Smrg       eval "$initialize_posix_glob" &&
472fb23d3a8Smrg       $posix_glob set -f &&
473fb23d3a8Smrg       set X $old && old=:$2:$4:$5:$6 &&
474fb23d3a8Smrg       set X $new && new=:$2:$4:$5:$6 &&
475fb23d3a8Smrg       $posix_glob set +f &&
476fb23d3a8Smrg
477fb23d3a8Smrg       test "$old" = "$new" &&
478fb23d3a8Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479fb23d3a8Smrg    then
480fb23d3a8Smrg      rm -f "$dsttmp"
481fb23d3a8Smrg    else
482fb23d3a8Smrg      # Rename the file to the real destination.
483fb23d3a8Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484fb23d3a8Smrg
485fb23d3a8Smrg      # The rename failed, perhaps because mv can't rename something else
486fb23d3a8Smrg      # to itself, or perhaps because mv is so ancient that it does not
487fb23d3a8Smrg      # support -f.
488fb23d3a8Smrg      {
489fb23d3a8Smrg	# Now remove or move aside any old file at destination location.
490fb23d3a8Smrg	# We try this two ways since rm can't unlink itself on some
491fb23d3a8Smrg	# systems and the destination file might be busy for other
492fb23d3a8Smrg	# reasons.  In this case, the final cleanup might fail but the new
493fb23d3a8Smrg	# file should still install successfully.
494fb23d3a8Smrg	{
495fb23d3a8Smrg	  test ! -f "$dst" ||
496fb23d3a8Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497fb23d3a8Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498fb23d3a8Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499fb23d3a8Smrg	  } ||
500fb23d3a8Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
501fb23d3a8Smrg	    (exit 1); exit 1
502fb23d3a8Smrg	  }
503fb23d3a8Smrg	} &&
504fb23d3a8Smrg
505fb23d3a8Smrg	# Now rename the file to the real destination.
506fb23d3a8Smrg	$doit $mvcmd "$dsttmp" "$dst"
507fb23d3a8Smrg      }
508fb23d3a8Smrg    fi || exit 1
50964ce7165Smrg
51064ce7165Smrg    trap '' 0
51164ce7165Smrg  fi
51264ce7165Smrgdone
51364ce7165Smrg
51464ce7165Smrg# Local variables:
51564ce7165Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51664ce7165Smrg# time-stamp-start: "scriptversion="
51764ce7165Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518fb23d3a8Smrg# time-stamp-time-zone: "UTC"
519fb23d3a8Smrg# time-stamp-end: "; # UTC"
52064ce7165Smrg# End:
521