xref: /freebsd/crypto/openssh/install-sh (revision f374ba41)
1ce3adf43SDag-Erling Smørgrav#!/bin/sh
2ce3adf43SDag-Erling Smørgrav# install - install a program, script, or datafile
347dd1d1bSDag-Erling Smørgrav
4f374ba41SEd Mastescriptversion=2020-11-14.01; # UTC
547dd1d1bSDag-Erling Smørgrav
647dd1d1bSDag-Erling Smørgrav# This originates from X11R5 (mit/util/scripts/install.sh), which was
747dd1d1bSDag-Erling Smørgrav# later released in X11R6 (xc/config/util/install.sh) with the
847dd1d1bSDag-Erling Smørgrav# following copyright and license.
9ce3adf43SDag-Erling Smørgrav#
1047dd1d1bSDag-Erling Smørgrav# Copyright (C) 1994 X Consortium
11ce3adf43SDag-Erling Smørgrav#
1247dd1d1bSDag-Erling Smørgrav# Permission is hereby granted, free of charge, to any person obtaining a copy
1347dd1d1bSDag-Erling Smørgrav# of this software and associated documentation files (the "Software"), to
1447dd1d1bSDag-Erling Smørgrav# deal in the Software without restriction, including without limitation the
1547dd1d1bSDag-Erling Smørgrav# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1647dd1d1bSDag-Erling Smørgrav# sell copies of the Software, and to permit persons to whom the Software is
1747dd1d1bSDag-Erling Smørgrav# furnished to do so, subject to the following conditions:
1847dd1d1bSDag-Erling Smørgrav#
1947dd1d1bSDag-Erling Smørgrav# The above copyright notice and this permission notice shall be included in
2047dd1d1bSDag-Erling Smørgrav# all copies or substantial portions of the Software.
2147dd1d1bSDag-Erling Smørgrav#
2247dd1d1bSDag-Erling Smørgrav# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2347dd1d1bSDag-Erling Smørgrav# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2447dd1d1bSDag-Erling Smørgrav# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2547dd1d1bSDag-Erling Smørgrav# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2647dd1d1bSDag-Erling Smørgrav# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2747dd1d1bSDag-Erling Smørgrav# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2847dd1d1bSDag-Erling Smørgrav#
2947dd1d1bSDag-Erling Smørgrav# Except as contained in this notice, the name of the X Consortium shall not
3047dd1d1bSDag-Erling Smørgrav# be used in advertising or otherwise to promote the sale, use or other deal-
3147dd1d1bSDag-Erling Smørgrav# ings in this Software without prior written authorization from the X Consor-
3247dd1d1bSDag-Erling Smørgrav# tium.
3347dd1d1bSDag-Erling Smørgrav#
3447dd1d1bSDag-Erling Smørgrav#
3547dd1d1bSDag-Erling Smørgrav# FSF changes to this file are in the public domain.
36ce3adf43SDag-Erling Smørgrav#
37ce3adf43SDag-Erling Smørgrav# Calling this script install-sh is preferred over install.sh, to prevent
3847dd1d1bSDag-Erling Smørgrav# 'make' implicit rules from creating a file called install from it
39ce3adf43SDag-Erling Smørgrav# when there is no Makefile.
40ce3adf43SDag-Erling Smørgrav#
41ce3adf43SDag-Erling Smørgrav# This script is compatible with the BSD install script, but was written
4247dd1d1bSDag-Erling Smørgrav# from scratch.
43ce3adf43SDag-Erling Smørgrav
44f374ba41SEd Mastetab='	'
4547dd1d1bSDag-Erling Smørgravnl='
4647dd1d1bSDag-Erling Smørgrav'
47f374ba41SEd MasteIFS=" $tab$nl"
48ce3adf43SDag-Erling Smørgrav
49f374ba41SEd Maste# Set DOITPROG to "echo" to test this script.
50ce3adf43SDag-Erling Smørgrav
5147dd1d1bSDag-Erling Smørgravdoit=${DOITPROG-}
52f374ba41SEd Mastedoit_exec=${doit:-exec}
53ce3adf43SDag-Erling Smørgrav
5447dd1d1bSDag-Erling Smørgrav# Put in absolute file names if you don't have them in your path;
5547dd1d1bSDag-Erling Smørgrav# or use environment vars.
56ce3adf43SDag-Erling Smørgrav
5747dd1d1bSDag-Erling Smørgravchgrpprog=${CHGRPPROG-chgrp}
5847dd1d1bSDag-Erling Smørgravchmodprog=${CHMODPROG-chmod}
5947dd1d1bSDag-Erling Smørgravchownprog=${CHOWNPROG-chown}
6047dd1d1bSDag-Erling Smørgravcmpprog=${CMPPROG-cmp}
6147dd1d1bSDag-Erling Smørgravcpprog=${CPPROG-cp}
6247dd1d1bSDag-Erling Smørgravmkdirprog=${MKDIRPROG-mkdir}
6347dd1d1bSDag-Erling Smørgravmvprog=${MVPROG-mv}
6447dd1d1bSDag-Erling Smørgravrmprog=${RMPROG-rm}
6547dd1d1bSDag-Erling Smørgravstripprog=${STRIPPROG-strip}
66ce3adf43SDag-Erling Smørgrav
6747dd1d1bSDag-Erling Smørgravposix_mkdir=
6847dd1d1bSDag-Erling Smørgrav
6947dd1d1bSDag-Erling Smørgrav# Desired mode of installed file.
7047dd1d1bSDag-Erling Smørgravmode=0755
7147dd1d1bSDag-Erling Smørgrav
72f374ba41SEd Maste# Create dirs (including intermediate dirs) using mode 755.
73f374ba41SEd Maste# This is like GNU 'install' as of coreutils 8.32 (2020).
74f374ba41SEd Mastemkdir_umask=22
75f374ba41SEd Maste
76f374ba41SEd Mastebackupsuffix=
7747dd1d1bSDag-Erling Smørgravchgrpcmd=
7847dd1d1bSDag-Erling Smørgravchmodcmd=$chmodprog
7947dd1d1bSDag-Erling Smørgravchowncmd=
8047dd1d1bSDag-Erling Smørgravmvcmd=$mvprog
81ce3adf43SDag-Erling Smørgravrmcmd="$rmprog -f"
8247dd1d1bSDag-Erling Smørgravstripcmd=
83ce3adf43SDag-Erling Smørgrav
8447dd1d1bSDag-Erling Smørgravsrc=
8547dd1d1bSDag-Erling Smørgravdst=
8647dd1d1bSDag-Erling Smørgravdir_arg=
8747dd1d1bSDag-Erling Smørgravdst_arg=
8847dd1d1bSDag-Erling Smørgrav
8947dd1d1bSDag-Erling Smørgravcopy_on_change=false
90f374ba41SEd Masteis_target_a_directory=possibly
9147dd1d1bSDag-Erling Smørgrav
9247dd1d1bSDag-Erling Smørgravusage="\
9347dd1d1bSDag-Erling SmørgravUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9447dd1d1bSDag-Erling Smørgrav   or: $0 [OPTION]... SRCFILES... DIRECTORY
9547dd1d1bSDag-Erling Smørgrav   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9647dd1d1bSDag-Erling Smørgrav   or: $0 [OPTION]... -d DIRECTORIES...
9747dd1d1bSDag-Erling Smørgrav
9847dd1d1bSDag-Erling SmørgravIn the 1st form, copy SRCFILE to DSTFILE.
9947dd1d1bSDag-Erling SmørgravIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10047dd1d1bSDag-Erling SmørgravIn the 4th, create DIRECTORIES.
10147dd1d1bSDag-Erling Smørgrav
10247dd1d1bSDag-Erling SmørgravOptions:
10347dd1d1bSDag-Erling Smørgrav     --help     display this help and exit.
10447dd1d1bSDag-Erling Smørgrav     --version  display version info and exit.
10547dd1d1bSDag-Erling Smørgrav
10647dd1d1bSDag-Erling Smørgrav  -c            (ignored)
107f374ba41SEd Maste  -C            install only if different (preserve data modification time)
10847dd1d1bSDag-Erling Smørgrav  -d            create directories instead of installing files.
10947dd1d1bSDag-Erling Smørgrav  -g GROUP      $chgrpprog installed files to GROUP.
11047dd1d1bSDag-Erling Smørgrav  -m MODE       $chmodprog installed files to MODE.
11147dd1d1bSDag-Erling Smørgrav  -o USER       $chownprog installed files to USER.
112f374ba41SEd Maste  -p            pass -p to $cpprog.
11347dd1d1bSDag-Erling Smørgrav  -s            $stripprog installed files.
114f374ba41SEd Maste  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11547dd1d1bSDag-Erling Smørgrav  -t DIRECTORY  install into DIRECTORY.
11647dd1d1bSDag-Erling Smørgrav  -T            report an error if DSTFILE is a directory.
11747dd1d1bSDag-Erling Smørgrav
11847dd1d1bSDag-Erling SmørgravEnvironment variables override the default commands:
11947dd1d1bSDag-Erling Smørgrav  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12047dd1d1bSDag-Erling Smørgrav  RMPROG STRIPPROG
121f374ba41SEd Maste
122f374ba41SEd MasteBy default, rm is invoked with -f; when overridden with RMPROG,
123f374ba41SEd Masteit's up to you to specify -f if you want it.
124f374ba41SEd Maste
125f374ba41SEd MasteIf -S is not specified, no backups are attempted.
126f374ba41SEd Maste
127f374ba41SEd MasteEmail bug reports to bug-automake@gnu.org.
128f374ba41SEd MasteAutomake home page: https://www.gnu.org/software/automake/
12947dd1d1bSDag-Erling Smørgrav"
13047dd1d1bSDag-Erling Smørgrav
13147dd1d1bSDag-Erling Smørgravwhile test $# -ne 0; do
132ce3adf43SDag-Erling Smørgrav  case $1 in
13347dd1d1bSDag-Erling Smørgrav    -c) ;;
134ce3adf43SDag-Erling Smørgrav
13547dd1d1bSDag-Erling Smørgrav    -C) copy_on_change=true;;
136ce3adf43SDag-Erling Smørgrav
13747dd1d1bSDag-Erling Smørgrav    -d) dir_arg=true;;
138ce3adf43SDag-Erling Smørgrav
139ce3adf43SDag-Erling Smørgrav    -g) chgrpcmd="$chgrpprog $2"
14047dd1d1bSDag-Erling Smørgrav        shift;;
141ce3adf43SDag-Erling Smørgrav
14247dd1d1bSDag-Erling Smørgrav    --help) echo "$usage"; exit $?;;
143ce3adf43SDag-Erling Smørgrav
14447dd1d1bSDag-Erling Smørgrav    -m) mode=$2
14547dd1d1bSDag-Erling Smørgrav        case $mode in
146f374ba41SEd Maste          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14747dd1d1bSDag-Erling Smørgrav            echo "$0: invalid mode: $mode" >&2
14847dd1d1bSDag-Erling Smørgrav            exit 1;;
14947dd1d1bSDag-Erling Smørgrav        esac
15047dd1d1bSDag-Erling Smørgrav        shift;;
151ce3adf43SDag-Erling Smørgrav
15247dd1d1bSDag-Erling Smørgrav    -o) chowncmd="$chownprog $2"
15347dd1d1bSDag-Erling Smørgrav        shift;;
154ce3adf43SDag-Erling Smørgrav
155f374ba41SEd Maste    -p) cpprog="$cpprog -p";;
156f374ba41SEd Maste
15747dd1d1bSDag-Erling Smørgrav    -s) stripcmd=$stripprog;;
15847dd1d1bSDag-Erling Smørgrav
159f374ba41SEd Maste    -S) backupsuffix="$2"
160f374ba41SEd Maste        shift;;
161f374ba41SEd Maste
162f374ba41SEd Maste    -t)
163f374ba41SEd Maste        is_target_a_directory=always
164f374ba41SEd Maste        dst_arg=$2
16547dd1d1bSDag-Erling Smørgrav        # Protect names problematic for 'test' and other utilities.
16647dd1d1bSDag-Erling Smørgrav        case $dst_arg in
16747dd1d1bSDag-Erling Smørgrav          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16847dd1d1bSDag-Erling Smørgrav        esac
16947dd1d1bSDag-Erling Smørgrav        shift;;
17047dd1d1bSDag-Erling Smørgrav
171f374ba41SEd Maste    -T) is_target_a_directory=never;;
17247dd1d1bSDag-Erling Smørgrav
17347dd1d1bSDag-Erling Smørgrav    --version) echo "$0 $scriptversion"; exit $?;;
17447dd1d1bSDag-Erling Smørgrav
17547dd1d1bSDag-Erling Smørgrav    --) shift
17647dd1d1bSDag-Erling Smørgrav        break;;
17747dd1d1bSDag-Erling Smørgrav
17847dd1d1bSDag-Erling Smørgrav    -*) echo "$0: invalid option: $1" >&2
17947dd1d1bSDag-Erling Smørgrav        exit 1;;
18047dd1d1bSDag-Erling Smørgrav
18147dd1d1bSDag-Erling Smørgrav    *)  break;;
18247dd1d1bSDag-Erling Smørgrav  esac
18347dd1d1bSDag-Erling Smørgrav  shift
18447dd1d1bSDag-Erling Smørgravdone
18547dd1d1bSDag-Erling Smørgrav
186f374ba41SEd Maste# We allow the use of options -d and -T together, by making -d
187f374ba41SEd Maste# take the precedence; this is for compatibility with GNU install.
188f374ba41SEd Maste
189f374ba41SEd Masteif test -n "$dir_arg"; then
190f374ba41SEd Maste  if test -n "$dst_arg"; then
191f374ba41SEd Maste    echo "$0: target directory not allowed when installing a directory." >&2
192f374ba41SEd Maste    exit 1
193f374ba41SEd Maste  fi
194f374ba41SEd Mastefi
195f374ba41SEd Maste
19647dd1d1bSDag-Erling Smørgravif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19747dd1d1bSDag-Erling Smørgrav  # When -d is used, all remaining arguments are directories to create.
19847dd1d1bSDag-Erling Smørgrav  # When -t is used, the destination is already specified.
19947dd1d1bSDag-Erling Smørgrav  # Otherwise, the last argument is the destination.  Remove it from $@.
20047dd1d1bSDag-Erling Smørgrav  for arg
20147dd1d1bSDag-Erling Smørgrav  do
20247dd1d1bSDag-Erling Smørgrav    if test -n "$dst_arg"; then
20347dd1d1bSDag-Erling Smørgrav      # $@ is not empty: it contains at least $arg.
20447dd1d1bSDag-Erling Smørgrav      set fnord "$@" "$dst_arg"
20547dd1d1bSDag-Erling Smørgrav      shift # fnord
206ce3adf43SDag-Erling Smørgrav    fi
20747dd1d1bSDag-Erling Smørgrav    shift # arg
20847dd1d1bSDag-Erling Smørgrav    dst_arg=$arg
20947dd1d1bSDag-Erling Smørgrav    # Protect names problematic for 'test' and other utilities.
21047dd1d1bSDag-Erling Smørgrav    case $dst_arg in
21147dd1d1bSDag-Erling Smørgrav      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212ce3adf43SDag-Erling Smørgrav    esac
213ce3adf43SDag-Erling Smørgrav  done
21447dd1d1bSDag-Erling Smørgravfi
215ce3adf43SDag-Erling Smørgrav
21647dd1d1bSDag-Erling Smørgravif test $# -eq 0; then
21747dd1d1bSDag-Erling Smørgrav  if test -z "$dir_arg"; then
21847dd1d1bSDag-Erling Smørgrav    echo "$0: no input file specified." >&2
219ce3adf43SDag-Erling Smørgrav    exit 1
22047dd1d1bSDag-Erling Smørgrav  fi
22147dd1d1bSDag-Erling Smørgrav  # It's OK to call 'install-sh -d' without argument.
22247dd1d1bSDag-Erling Smørgrav  # This can happen when creating conditional directories.
22347dd1d1bSDag-Erling Smørgrav  exit 0
224ce3adf43SDag-Erling Smørgravfi
225ce3adf43SDag-Erling Smørgrav
22647dd1d1bSDag-Erling Smørgravif test -z "$dir_arg"; then
227f374ba41SEd Maste  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228f374ba41SEd Maste    if test ! -d "$dst_arg"; then
229f374ba41SEd Maste      echo "$0: $dst_arg: Is not a directory." >&2
230f374ba41SEd Maste      exit 1
231f374ba41SEd Maste    fi
232f374ba41SEd Maste  fi
233f374ba41SEd Mastefi
234f374ba41SEd Maste
235f374ba41SEd Masteif test -z "$dir_arg"; then
23647dd1d1bSDag-Erling Smørgrav  do_exit='(exit $ret); exit $ret'
23747dd1d1bSDag-Erling Smørgrav  trap "ret=129; $do_exit" 1
23847dd1d1bSDag-Erling Smørgrav  trap "ret=130; $do_exit" 2
23947dd1d1bSDag-Erling Smørgrav  trap "ret=141; $do_exit" 13
24047dd1d1bSDag-Erling Smørgrav  trap "ret=143; $do_exit" 15
24147dd1d1bSDag-Erling Smørgrav
24247dd1d1bSDag-Erling Smørgrav  # Set umask so as not to create temps with too-generous modes.
24347dd1d1bSDag-Erling Smørgrav  # However, 'strip' requires both read and write access to temps.
24447dd1d1bSDag-Erling Smørgrav  case $mode in
24547dd1d1bSDag-Erling Smørgrav    # Optimize common cases.
24647dd1d1bSDag-Erling Smørgrav    *644) cp_umask=133;;
24747dd1d1bSDag-Erling Smørgrav    *755) cp_umask=22;;
24847dd1d1bSDag-Erling Smørgrav
24947dd1d1bSDag-Erling Smørgrav    *[0-7])
25047dd1d1bSDag-Erling Smørgrav      if test -z "$stripcmd"; then
25147dd1d1bSDag-Erling Smørgrav        u_plus_rw=
25247dd1d1bSDag-Erling Smørgrav      else
25347dd1d1bSDag-Erling Smørgrav        u_plus_rw='% 200'
25447dd1d1bSDag-Erling Smørgrav      fi
25547dd1d1bSDag-Erling Smørgrav      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25647dd1d1bSDag-Erling Smørgrav    *)
25747dd1d1bSDag-Erling Smørgrav      if test -z "$stripcmd"; then
25847dd1d1bSDag-Erling Smørgrav        u_plus_rw=
25947dd1d1bSDag-Erling Smørgrav      else
26047dd1d1bSDag-Erling Smørgrav        u_plus_rw=,u+rw
26147dd1d1bSDag-Erling Smørgrav      fi
26247dd1d1bSDag-Erling Smørgrav      cp_umask=$mode$u_plus_rw;;
26347dd1d1bSDag-Erling Smørgrav  esac
26447dd1d1bSDag-Erling Smørgravfi
26547dd1d1bSDag-Erling Smørgrav
26647dd1d1bSDag-Erling Smørgravfor src
26747dd1d1bSDag-Erling Smørgravdo
26847dd1d1bSDag-Erling Smørgrav  # Protect names problematic for 'test' and other utilities.
26947dd1d1bSDag-Erling Smørgrav  case $src in
27047dd1d1bSDag-Erling Smørgrav    -* | [=\(\)!]) src=./$src;;
27147dd1d1bSDag-Erling Smørgrav  esac
27247dd1d1bSDag-Erling Smørgrav
27347dd1d1bSDag-Erling Smørgrav  if test -n "$dir_arg"; then
274ce3adf43SDag-Erling Smørgrav    dst=$src
27547dd1d1bSDag-Erling Smørgrav    dstdir=$dst
27647dd1d1bSDag-Erling Smørgrav    test -d "$dstdir"
27747dd1d1bSDag-Erling Smørgrav    dstdir_status=$?
278f374ba41SEd Maste    # Don't chown directories that already exist.
279f374ba41SEd Maste    if test $dstdir_status = 0; then
280f374ba41SEd Maste      chowncmd=""
281f374ba41SEd Maste    fi
282ce3adf43SDag-Erling Smørgrav  else
283ce3adf43SDag-Erling Smørgrav
28447dd1d1bSDag-Erling Smørgrav    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285ce3adf43SDag-Erling Smørgrav    # might cause directories to be created, which would be especially bad
286ce3adf43SDag-Erling Smørgrav    # if $src (and thus $dsttmp) contains '*'.
28747dd1d1bSDag-Erling Smørgrav    if test ! -f "$src" && test ! -d "$src"; then
28847dd1d1bSDag-Erling Smørgrav      echo "$0: $src does not exist." >&2
289ce3adf43SDag-Erling Smørgrav      exit 1
290ce3adf43SDag-Erling Smørgrav    fi
291ce3adf43SDag-Erling Smørgrav
29247dd1d1bSDag-Erling Smørgrav    if test -z "$dst_arg"; then
29347dd1d1bSDag-Erling Smørgrav      echo "$0: no destination specified." >&2
294ce3adf43SDag-Erling Smørgrav      exit 1
29547dd1d1bSDag-Erling Smørgrav    fi
29647dd1d1bSDag-Erling Smørgrav    dst=$dst_arg
29747dd1d1bSDag-Erling Smørgrav
298f374ba41SEd Maste    # If destination is a directory, append the input filename.
29947dd1d1bSDag-Erling Smørgrav    if test -d "$dst"; then
300f374ba41SEd Maste      if test "$is_target_a_directory" = never; then
30147dd1d1bSDag-Erling Smørgrav        echo "$0: $dst_arg: Is a directory" >&2
30247dd1d1bSDag-Erling Smørgrav        exit 1
30347dd1d1bSDag-Erling Smørgrav      fi
30447dd1d1bSDag-Erling Smørgrav      dstdir=$dst
305f374ba41SEd Maste      dstbase=`basename "$src"`
306f374ba41SEd Maste      case $dst in
307f374ba41SEd Maste	*/) dst=$dst$dstbase;;
308f374ba41SEd Maste	*)  dst=$dst/$dstbase;;
309f374ba41SEd Maste      esac
31047dd1d1bSDag-Erling Smørgrav      dstdir_status=0
311ce3adf43SDag-Erling Smørgrav    else
312f374ba41SEd Maste      dstdir=`dirname "$dst"`
31347dd1d1bSDag-Erling Smørgrav      test -d "$dstdir"
31447dd1d1bSDag-Erling Smørgrav      dstdir_status=$?
31547dd1d1bSDag-Erling Smørgrav    fi
316ce3adf43SDag-Erling Smørgrav  fi
317ce3adf43SDag-Erling Smørgrav
318f374ba41SEd Maste  case $dstdir in
319f374ba41SEd Maste    */) dstdirslash=$dstdir;;
320f374ba41SEd Maste    *)  dstdirslash=$dstdir/;;
321f374ba41SEd Maste  esac
322f374ba41SEd Maste
32347dd1d1bSDag-Erling Smørgrav  obsolete_mkdir_used=false
324ce3adf43SDag-Erling Smørgrav
32547dd1d1bSDag-Erling Smørgrav  if test $dstdir_status != 0; then
32647dd1d1bSDag-Erling Smørgrav    case $posix_mkdir in
32747dd1d1bSDag-Erling Smørgrav      '')
32847dd1d1bSDag-Erling Smørgrav        # With -d, create the new directory with the user-specified mode.
32947dd1d1bSDag-Erling Smørgrav        # Otherwise, rely on $mkdir_umask.
33047dd1d1bSDag-Erling Smørgrav        if test -n "$dir_arg"; then
33147dd1d1bSDag-Erling Smørgrav          mkdir_mode=-m$mode
33247dd1d1bSDag-Erling Smørgrav        else
33347dd1d1bSDag-Erling Smørgrav          mkdir_mode=
33447dd1d1bSDag-Erling Smørgrav        fi
33547dd1d1bSDag-Erling Smørgrav
33647dd1d1bSDag-Erling Smørgrav        posix_mkdir=false
337f374ba41SEd Maste	# The $RANDOM variable is not portable (e.g., dash).  Use it
338f374ba41SEd Maste	# here however when possible just to lower collision chance.
33947dd1d1bSDag-Erling Smørgrav	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34047dd1d1bSDag-Erling Smørgrav
341f374ba41SEd Maste	trap '
342f374ba41SEd Maste	  ret=$?
343f374ba41SEd Maste	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344f374ba41SEd Maste	  exit $ret
345f374ba41SEd Maste	' 0
346f374ba41SEd Maste
347f374ba41SEd Maste	# Because "mkdir -p" follows existing symlinks and we likely work
348f374ba41SEd Maste	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349f374ba41SEd Maste	# directory is successfully created first before we actually test
350f374ba41SEd Maste	# 'mkdir -p'.
35147dd1d1bSDag-Erling Smørgrav	if (umask $mkdir_umask &&
352f374ba41SEd Maste	    $mkdirprog $mkdir_mode "$tmpdir" &&
353f374ba41SEd Maste	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354ce3adf43SDag-Erling Smørgrav	then
35547dd1d1bSDag-Erling Smørgrav	  if test -z "$dir_arg" || {
35647dd1d1bSDag-Erling Smørgrav	       # Check for POSIX incompatibilities with -m.
35747dd1d1bSDag-Erling Smørgrav	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35847dd1d1bSDag-Erling Smørgrav	       # other-writable bit of parent directory when it shouldn't.
35947dd1d1bSDag-Erling Smørgrav	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360f374ba41SEd Maste	       test_tmpdir="$tmpdir/a"
361f374ba41SEd Maste	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36247dd1d1bSDag-Erling Smørgrav	       case $ls_ld_tmpdir in
36347dd1d1bSDag-Erling Smørgrav		 d????-?r-*) different_mode=700;;
36447dd1d1bSDag-Erling Smørgrav		 d????-?--*) different_mode=755;;
36547dd1d1bSDag-Erling Smørgrav		 *) false;;
36647dd1d1bSDag-Erling Smørgrav	       esac &&
367f374ba41SEd Maste	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368f374ba41SEd Maste		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36947dd1d1bSDag-Erling Smørgrav		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37047dd1d1bSDag-Erling Smørgrav	       }
37147dd1d1bSDag-Erling Smørgrav	     }
37247dd1d1bSDag-Erling Smørgrav	  then posix_mkdir=:
37347dd1d1bSDag-Erling Smørgrav	  fi
374f374ba41SEd Maste	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375ce3adf43SDag-Erling Smørgrav	else
37647dd1d1bSDag-Erling Smørgrav	  # Remove any dirs left behind by ancient mkdir implementations.
377f374ba41SEd Maste	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378ce3adf43SDag-Erling Smørgrav	fi
37947dd1d1bSDag-Erling Smørgrav	trap '' 0;;
38047dd1d1bSDag-Erling Smørgrav    esac
381ce3adf43SDag-Erling Smørgrav
38247dd1d1bSDag-Erling Smørgrav    if
38347dd1d1bSDag-Erling Smørgrav      $posix_mkdir && (
38447dd1d1bSDag-Erling Smørgrav        umask $mkdir_umask &&
38547dd1d1bSDag-Erling Smørgrav        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38647dd1d1bSDag-Erling Smørgrav      )
38747dd1d1bSDag-Erling Smørgrav    then :
38847dd1d1bSDag-Erling Smørgrav    else
389ce3adf43SDag-Erling Smørgrav
390f374ba41SEd Maste      # mkdir does not conform to POSIX,
39147dd1d1bSDag-Erling Smørgrav      # or it failed possibly due to a race condition.  Create the
39247dd1d1bSDag-Erling Smørgrav      # directory the slow way, step by step, checking for races as we go.
393ce3adf43SDag-Erling Smørgrav
39447dd1d1bSDag-Erling Smørgrav      case $dstdir in
39547dd1d1bSDag-Erling Smørgrav        /*) prefix='/';;
39647dd1d1bSDag-Erling Smørgrav        [-=\(\)!]*) prefix='./';;
39747dd1d1bSDag-Erling Smørgrav        *)  prefix='';;
39847dd1d1bSDag-Erling Smørgrav      esac
399ce3adf43SDag-Erling Smørgrav
40047dd1d1bSDag-Erling Smørgrav      oIFS=$IFS
40147dd1d1bSDag-Erling Smørgrav      IFS=/
402f374ba41SEd Maste      set -f
40347dd1d1bSDag-Erling Smørgrav      set fnord $dstdir
404ce3adf43SDag-Erling Smørgrav      shift
405f374ba41SEd Maste      set +f
40647dd1d1bSDag-Erling Smørgrav      IFS=$oIFS
407ce3adf43SDag-Erling Smørgrav
40847dd1d1bSDag-Erling Smørgrav      prefixes=
40947dd1d1bSDag-Erling Smørgrav
41047dd1d1bSDag-Erling Smørgrav      for d
41147dd1d1bSDag-Erling Smørgrav      do
41247dd1d1bSDag-Erling Smørgrav        test X"$d" = X && continue
41347dd1d1bSDag-Erling Smørgrav
41447dd1d1bSDag-Erling Smørgrav        prefix=$prefix$d
41547dd1d1bSDag-Erling Smørgrav        if test -d "$prefix"; then
41647dd1d1bSDag-Erling Smørgrav          prefixes=
417ce3adf43SDag-Erling Smørgrav        else
41847dd1d1bSDag-Erling Smørgrav          if $posix_mkdir; then
419f374ba41SEd Maste            (umask $mkdir_umask &&
42047dd1d1bSDag-Erling Smørgrav             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42147dd1d1bSDag-Erling Smørgrav            # Don't fail if two instances are running concurrently.
42247dd1d1bSDag-Erling Smørgrav            test -d "$prefix" || exit 1
42347dd1d1bSDag-Erling Smørgrav          else
42447dd1d1bSDag-Erling Smørgrav            case $prefix in
42547dd1d1bSDag-Erling Smørgrav              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42647dd1d1bSDag-Erling Smørgrav              *) qprefix=$prefix;;
42747dd1d1bSDag-Erling Smørgrav            esac
42847dd1d1bSDag-Erling Smørgrav            prefixes="$prefixes '$qprefix'"
429ce3adf43SDag-Erling Smørgrav          fi
43047dd1d1bSDag-Erling Smørgrav        fi
43147dd1d1bSDag-Erling Smørgrav        prefix=$prefix/
432ce3adf43SDag-Erling Smørgrav      done
43347dd1d1bSDag-Erling Smørgrav
43447dd1d1bSDag-Erling Smørgrav      if test -n "$prefixes"; then
43547dd1d1bSDag-Erling Smørgrav        # Don't fail if two instances are running concurrently.
43647dd1d1bSDag-Erling Smørgrav        (umask $mkdir_umask &&
43747dd1d1bSDag-Erling Smørgrav         eval "\$doit_exec \$mkdirprog $prefixes") ||
43847dd1d1bSDag-Erling Smørgrav          test -d "$dstdir" || exit 1
43947dd1d1bSDag-Erling Smørgrav        obsolete_mkdir_used=true
44047dd1d1bSDag-Erling Smørgrav      fi
44147dd1d1bSDag-Erling Smørgrav    fi
442ce3adf43SDag-Erling Smørgrav  fi
443ce3adf43SDag-Erling Smørgrav
44447dd1d1bSDag-Erling Smørgrav  if test -n "$dir_arg"; then
44547dd1d1bSDag-Erling Smørgrav    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44647dd1d1bSDag-Erling Smørgrav    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44747dd1d1bSDag-Erling Smørgrav    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44847dd1d1bSDag-Erling Smørgrav      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449ce3adf43SDag-Erling Smørgrav  else
450ce3adf43SDag-Erling Smørgrav
45147dd1d1bSDag-Erling Smørgrav    # Make a couple of temp file names in the proper directory.
452f374ba41SEd Maste    dsttmp=${dstdirslash}_inst.$$_
453f374ba41SEd Maste    rmtmp=${dstdirslash}_rm.$$_
454ce3adf43SDag-Erling Smørgrav
45547dd1d1bSDag-Erling Smørgrav    # Trap to clean up those temp files at exit.
45647dd1d1bSDag-Erling Smørgrav    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457ce3adf43SDag-Erling Smørgrav
45847dd1d1bSDag-Erling Smørgrav    # Copy the file name to the temp name.
459f374ba41SEd Maste    (umask $cp_umask &&
460f374ba41SEd Maste     { test -z "$stripcmd" || {
461f374ba41SEd Maste	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462f374ba41SEd Maste	 # which would cause strip to fail.
463f374ba41SEd Maste	 if test -z "$doit"; then
464f374ba41SEd Maste	   : >"$dsttmp" # No need to fork-exec 'touch'.
465f374ba41SEd Maste	 else
466f374ba41SEd Maste	   $doit touch "$dsttmp"
467f374ba41SEd Maste	 fi
468f374ba41SEd Maste       }
469f374ba41SEd Maste     } &&
470f374ba41SEd Maste     $doit_exec $cpprog "$src" "$dsttmp") &&
471ce3adf43SDag-Erling Smørgrav
47247dd1d1bSDag-Erling Smørgrav    # and set any options; do chmod last to preserve setuid bits.
47347dd1d1bSDag-Erling Smørgrav    #
474ce3adf43SDag-Erling Smørgrav    # If any of these fail, we abort the whole thing.  If we want to
475ce3adf43SDag-Erling Smørgrav    # ignore errors from any of these, just make sure not to ignore
47647dd1d1bSDag-Erling Smørgrav    # errors from the above "$doit $cpprog $src $dsttmp" command.
47747dd1d1bSDag-Erling Smørgrav    #
47847dd1d1bSDag-Erling Smørgrav    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47947dd1d1bSDag-Erling Smørgrav    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48047dd1d1bSDag-Erling Smørgrav    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48147dd1d1bSDag-Erling Smørgrav    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482ce3adf43SDag-Erling Smørgrav
48347dd1d1bSDag-Erling Smørgrav    # If -C, don't bother to copy if it wouldn't change the file.
48447dd1d1bSDag-Erling Smørgrav    if $copy_on_change &&
48547dd1d1bSDag-Erling Smørgrav       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48647dd1d1bSDag-Erling Smørgrav       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487f374ba41SEd Maste       set -f &&
48847dd1d1bSDag-Erling Smørgrav       set X $old && old=:$2:$4:$5:$6 &&
48947dd1d1bSDag-Erling Smørgrav       set X $new && new=:$2:$4:$5:$6 &&
490f374ba41SEd Maste       set +f &&
49147dd1d1bSDag-Erling Smørgrav       test "$old" = "$new" &&
49247dd1d1bSDag-Erling Smørgrav       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49347dd1d1bSDag-Erling Smørgrav    then
49447dd1d1bSDag-Erling Smørgrav      rm -f "$dsttmp"
49547dd1d1bSDag-Erling Smørgrav    else
496f374ba41SEd Maste      # If $backupsuffix is set, and the file being installed
497f374ba41SEd Maste      # already exists, attempt a backup.  Don't worry if it fails,
498f374ba41SEd Maste      # e.g., if mv doesn't support -f.
499f374ba41SEd Maste      if test -n "$backupsuffix" && test -f "$dst"; then
500f374ba41SEd Maste        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501f374ba41SEd Maste      fi
502f374ba41SEd Maste
50347dd1d1bSDag-Erling Smørgrav      # Rename the file to the real destination.
50447dd1d1bSDag-Erling Smørgrav      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50547dd1d1bSDag-Erling Smørgrav
50647dd1d1bSDag-Erling Smørgrav      # The rename failed, perhaps because mv can't rename something else
50747dd1d1bSDag-Erling Smørgrav      # to itself, or perhaps because mv is so ancient that it does not
50847dd1d1bSDag-Erling Smørgrav      # support -f.
50947dd1d1bSDag-Erling Smørgrav      {
51047dd1d1bSDag-Erling Smørgrav        # Now remove or move aside any old file at destination location.
51147dd1d1bSDag-Erling Smørgrav        # We try this two ways since rm can't unlink itself on some
51247dd1d1bSDag-Erling Smørgrav        # systems and the destination file might be busy for other
51347dd1d1bSDag-Erling Smørgrav        # reasons.  In this case, the final cleanup might fail but the new
51447dd1d1bSDag-Erling Smørgrav        # file should still install successfully.
51547dd1d1bSDag-Erling Smørgrav        {
51647dd1d1bSDag-Erling Smørgrav          test ! -f "$dst" ||
517f374ba41SEd Maste          $doit $rmcmd "$dst" 2>/dev/null ||
51847dd1d1bSDag-Erling Smørgrav          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519f374ba41SEd Maste            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52047dd1d1bSDag-Erling Smørgrav          } ||
52147dd1d1bSDag-Erling Smørgrav          { echo "$0: cannot unlink or rename $dst" >&2
52247dd1d1bSDag-Erling Smørgrav            (exit 1); exit 1
52347dd1d1bSDag-Erling Smørgrav          }
52447dd1d1bSDag-Erling Smørgrav        } &&
525ce3adf43SDag-Erling Smørgrav
526ce3adf43SDag-Erling Smørgrav        # Now rename the file to the real destination.
52747dd1d1bSDag-Erling Smørgrav        $doit $mvcmd "$dsttmp" "$dst"
52847dd1d1bSDag-Erling Smørgrav      }
52947dd1d1bSDag-Erling Smørgrav    fi || exit 1
530ce3adf43SDag-Erling Smørgrav
53147dd1d1bSDag-Erling Smørgrav    trap '' 0
53247dd1d1bSDag-Erling Smørgrav  fi
53347dd1d1bSDag-Erling Smørgravdone
534ce3adf43SDag-Erling Smørgrav
53547dd1d1bSDag-Erling Smørgrav# Local variables:
536f374ba41SEd Maste# eval: (add-hook 'before-save-hook 'time-stamp)
53747dd1d1bSDag-Erling Smørgrav# time-stamp-start: "scriptversion="
53847dd1d1bSDag-Erling Smørgrav# time-stamp-format: "%:y-%02m-%02d.%02H"
539f374ba41SEd Maste# time-stamp-time-zone: "UTC0"
54047dd1d1bSDag-Erling Smørgrav# time-stamp-end: "; # UTC"
54147dd1d1bSDag-Erling Smørgrav# End:
542