install-sh revision d983712d
1d983712dSmrg#!/bin/sh
2d983712dSmrg# install - install a program, script, or datafile
3d983712dSmrg
4d983712dSmrgscriptversion=2005-05-14.22
5d983712dSmrg
6d983712dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7d983712dSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8d983712dSmrg# following copyright and license.
9d983712dSmrg#
10d983712dSmrg# Copyright (C) 1994 X Consortium
11d983712dSmrg#
12d983712dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13d983712dSmrg# of this software and associated documentation files (the "Software"), to
14d983712dSmrg# deal in the Software without restriction, including without limitation the
15d983712dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16d983712dSmrg# sell copies of the Software, and to permit persons to whom the Software is
17d983712dSmrg# furnished to do so, subject to the following conditions:
18d983712dSmrg#
19d983712dSmrg# The above copyright notice and this permission notice shall be included in
20d983712dSmrg# all copies or substantial portions of the Software.
21d983712dSmrg#
22d983712dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23d983712dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24d983712dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25d983712dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26d983712dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27d983712dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28d983712dSmrg#
29d983712dSmrg# Except as contained in this notice, the name of the X Consortium shall not
30d983712dSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31d983712dSmrg# ings in this Software without prior written authorization from the X Consor-
32d983712dSmrg# tium.
33d983712dSmrg#
34d983712dSmrg#
35d983712dSmrg# FSF changes to this file are in the public domain.
36d983712dSmrg#
37d983712dSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38d983712dSmrg# `make' implicit rules from creating a file called install from it
39d983712dSmrg# when there is no Makefile.
40d983712dSmrg#
41d983712dSmrg# This script is compatible with the BSD install script, but was written
42d983712dSmrg# from scratch.  It can only install one file at a time, a restriction
43d983712dSmrg# shared with many OS's install programs.
44d983712dSmrg
45d983712dSmrg# set DOITPROG to echo to test this script
46d983712dSmrg
47d983712dSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
48d983712dSmrgdoit="${DOITPROG-}"
49d983712dSmrg
50d983712dSmrg# put in absolute paths if you don't have them in your path; or use env. vars.
51d983712dSmrg
52d983712dSmrgmvprog="${MVPROG-mv}"
53d983712dSmrgcpprog="${CPPROG-cp}"
54d983712dSmrgchmodprog="${CHMODPROG-chmod}"
55d983712dSmrgchownprog="${CHOWNPROG-chown}"
56d983712dSmrgchgrpprog="${CHGRPPROG-chgrp}"
57d983712dSmrgstripprog="${STRIPPROG-strip}"
58d983712dSmrgrmprog="${RMPROG-rm}"
59d983712dSmrgmkdirprog="${MKDIRPROG-mkdir}"
60d983712dSmrg
61d983712dSmrgchmodcmd="$chmodprog 0755"
62d983712dSmrgchowncmd=
63d983712dSmrgchgrpcmd=
64d983712dSmrgstripcmd=
65d983712dSmrgrmcmd="$rmprog -f"
66d983712dSmrgmvcmd="$mvprog"
67d983712dSmrgsrc=
68d983712dSmrgdst=
69d983712dSmrgdir_arg=
70d983712dSmrgdstarg=
71d983712dSmrgno_target_directory=
72d983712dSmrg
73d983712dSmrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
74d983712dSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
75d983712dSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
76d983712dSmrg   or: $0 [OPTION]... -d DIRECTORIES...
77d983712dSmrg
78d983712dSmrgIn the 1st form, copy SRCFILE to DSTFILE.
79d983712dSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
80d983712dSmrgIn the 4th, create DIRECTORIES.
81d983712dSmrg
82d983712dSmrgOptions:
83d983712dSmrg-c         (ignored)
84d983712dSmrg-d         create directories instead of installing files.
85d983712dSmrg-g GROUP   $chgrpprog installed files to GROUP.
86d983712dSmrg-m MODE    $chmodprog installed files to MODE.
87d983712dSmrg-o USER    $chownprog installed files to USER.
88d983712dSmrg-s         $stripprog installed files.
89d983712dSmrg-t DIRECTORY  install into DIRECTORY.
90d983712dSmrg-T         report an error if DSTFILE is a directory.
91d983712dSmrg--help     display this help and exit.
92d983712dSmrg--version  display version info and exit.
93d983712dSmrg
94d983712dSmrgEnvironment variables override the default commands:
95d983712dSmrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
96d983712dSmrg"
97d983712dSmrg
98d983712dSmrgwhile test -n "$1"; do
99d983712dSmrg  case $1 in
100d983712dSmrg    -c) shift
101d983712dSmrg        continue;;
102d983712dSmrg
103d983712dSmrg    -d) dir_arg=true
104d983712dSmrg        shift
105d983712dSmrg        continue;;
106d983712dSmrg
107d983712dSmrg    -g) chgrpcmd="$chgrpprog $2"
108d983712dSmrg        shift
109d983712dSmrg        shift
110d983712dSmrg        continue;;
111d983712dSmrg
112d983712dSmrg    --help) echo "$usage"; exit $?;;
113d983712dSmrg
114d983712dSmrg    -m) chmodcmd="$chmodprog $2"
115d983712dSmrg        shift
116d983712dSmrg        shift
117d983712dSmrg        continue;;
118d983712dSmrg
119d983712dSmrg    -o) chowncmd="$chownprog $2"
120d983712dSmrg        shift
121d983712dSmrg        shift
122d983712dSmrg        continue;;
123d983712dSmrg
124d983712dSmrg    -s) stripcmd=$stripprog
125d983712dSmrg        shift
126d983712dSmrg        continue;;
127d983712dSmrg
128d983712dSmrg    -t) dstarg=$2
129d983712dSmrg	shift
130d983712dSmrg	shift
131d983712dSmrg	continue;;
132d983712dSmrg
133d983712dSmrg    -T) no_target_directory=true
134d983712dSmrg	shift
135d983712dSmrg	continue;;
136d983712dSmrg
137d983712dSmrg    --version) echo "$0 $scriptversion"; exit $?;;
138d983712dSmrg
139d983712dSmrg    *)  # When -d is used, all remaining arguments are directories to create.
140d983712dSmrg	# When -t is used, the destination is already specified.
141d983712dSmrg	test -n "$dir_arg$dstarg" && break
142d983712dSmrg        # Otherwise, the last argument is the destination.  Remove it from $@.
143d983712dSmrg	for arg
144d983712dSmrg	do
145d983712dSmrg          if test -n "$dstarg"; then
146d983712dSmrg	    # $@ is not empty: it contains at least $arg.
147d983712dSmrg	    set fnord "$@" "$dstarg"
148d983712dSmrg	    shift # fnord
149d983712dSmrg	  fi
150d983712dSmrg	  shift # arg
151d983712dSmrg	  dstarg=$arg
152d983712dSmrg	done
153d983712dSmrg	break;;
154d983712dSmrg  esac
155d983712dSmrgdone
156d983712dSmrg
157d983712dSmrgif test -z "$1"; then
158d983712dSmrg  if test -z "$dir_arg"; then
159d983712dSmrg    echo "$0: no input file specified." >&2
160d983712dSmrg    exit 1
161d983712dSmrg  fi
162d983712dSmrg  # It's OK to call `install-sh -d' without argument.
163d983712dSmrg  # This can happen when creating conditional directories.
164d983712dSmrg  exit 0
165d983712dSmrgfi
166d983712dSmrg
167d983712dSmrgfor src
168d983712dSmrgdo
169d983712dSmrg  # Protect names starting with `-'.
170d983712dSmrg  case $src in
171d983712dSmrg    -*) src=./$src ;;
172d983712dSmrg  esac
173d983712dSmrg
174d983712dSmrg  if test -n "$dir_arg"; then
175d983712dSmrg    dst=$src
176d983712dSmrg    src=
177d983712dSmrg
178d983712dSmrg    if test -d "$dst"; then
179d983712dSmrg      mkdircmd=:
180d983712dSmrg      chmodcmd=
181d983712dSmrg    else
182d983712dSmrg      mkdircmd=$mkdirprog
183d983712dSmrg    fi
184d983712dSmrg  else
185d983712dSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
186d983712dSmrg    # might cause directories to be created, which would be especially bad
187d983712dSmrg    # if $src (and thus $dsttmp) contains '*'.
188d983712dSmrg    if test ! -f "$src" && test ! -d "$src"; then
189d983712dSmrg      echo "$0: $src does not exist." >&2
190d983712dSmrg      exit 1
191d983712dSmrg    fi
192d983712dSmrg
193d983712dSmrg    if test -z "$dstarg"; then
194d983712dSmrg      echo "$0: no destination specified." >&2
195d983712dSmrg      exit 1
196d983712dSmrg    fi
197d983712dSmrg
198d983712dSmrg    dst=$dstarg
199d983712dSmrg    # Protect names starting with `-'.
200d983712dSmrg    case $dst in
201d983712dSmrg      -*) dst=./$dst ;;
202d983712dSmrg    esac
203d983712dSmrg
204d983712dSmrg    # If destination is a directory, append the input filename; won't work
205d983712dSmrg    # if double slashes aren't ignored.
206d983712dSmrg    if test -d "$dst"; then
207d983712dSmrg      if test -n "$no_target_directory"; then
208d983712dSmrg	echo "$0: $dstarg: Is a directory" >&2
209d983712dSmrg	exit 1
210d983712dSmrg      fi
211d983712dSmrg      dst=$dst/`basename "$src"`
212d983712dSmrg    fi
213d983712dSmrg  fi
214d983712dSmrg
215d983712dSmrg  # This sed command emulates the dirname command.
216d983712dSmrg  dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
217d983712dSmrg
218d983712dSmrg  # Make sure that the destination directory exists.
219d983712dSmrg
220d983712dSmrg  # Skip lots of stat calls in the usual case.
221d983712dSmrg  if test ! -d "$dstdir"; then
222d983712dSmrg    defaultIFS='
223d983712dSmrg	 '
224d983712dSmrg    IFS="${IFS-$defaultIFS}"
225d983712dSmrg
226d983712dSmrg    oIFS=$IFS
227d983712dSmrg    # Some sh's can't handle IFS=/ for some reason.
228d983712dSmrg    IFS='%'
229d983712dSmrg    set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
230d983712dSmrg    shift
231d983712dSmrg    IFS=$oIFS
232d983712dSmrg
233d983712dSmrg    pathcomp=
234d983712dSmrg
235d983712dSmrg    while test $# -ne 0 ; do
236d983712dSmrg      pathcomp=$pathcomp$1
237d983712dSmrg      shift
238d983712dSmrg      if test ! -d "$pathcomp"; then
239d983712dSmrg        $mkdirprog "$pathcomp"
240d983712dSmrg	# mkdir can fail with a `File exist' error in case several
241d983712dSmrg	# install-sh are creating the directory concurrently.  This
242d983712dSmrg	# is OK.
243d983712dSmrg	test -d "$pathcomp" || exit
244d983712dSmrg      fi
245d983712dSmrg      pathcomp=$pathcomp/
246d983712dSmrg    done
247d983712dSmrg  fi
248d983712dSmrg
249d983712dSmrg  if test -n "$dir_arg"; then
250d983712dSmrg    $doit $mkdircmd "$dst" \
251d983712dSmrg      && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
252d983712dSmrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
253d983712dSmrg      && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
254d983712dSmrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
255d983712dSmrg
256d983712dSmrg  else
257d983712dSmrg    dstfile=`basename "$dst"`
258d983712dSmrg
259d983712dSmrg    # Make a couple of temp file names in the proper directory.
260d983712dSmrg    dsttmp=$dstdir/_inst.$$_
261d983712dSmrg    rmtmp=$dstdir/_rm.$$_
262d983712dSmrg
263d983712dSmrg    # Trap to clean up those temp files at exit.
264d983712dSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
265d983712dSmrg    trap '(exit $?); exit' 1 2 13 15
266d983712dSmrg
267d983712dSmrg    # Copy the file name to the temp name.
268d983712dSmrg    $doit $cpprog "$src" "$dsttmp" &&
269d983712dSmrg
270d983712dSmrg    # and set any options; do chmod last to preserve setuid bits.
271d983712dSmrg    #
272d983712dSmrg    # If any of these fail, we abort the whole thing.  If we want to
273d983712dSmrg    # ignore errors from any of these, just make sure not to ignore
274d983712dSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
275d983712dSmrg    #
276d983712dSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
277d983712dSmrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
278d983712dSmrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
279d983712dSmrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
280d983712dSmrg
281d983712dSmrg    # Now rename the file to the real destination.
282d983712dSmrg    { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
283d983712dSmrg      || {
284d983712dSmrg	   # The rename failed, perhaps because mv can't rename something else
285d983712dSmrg	   # to itself, or perhaps because mv is so ancient that it does not
286d983712dSmrg	   # support -f.
287d983712dSmrg
288d983712dSmrg	   # Now remove or move aside any old file at destination location.
289d983712dSmrg	   # We try this two ways since rm can't unlink itself on some
290d983712dSmrg	   # systems and the destination file might be busy for other
291d983712dSmrg	   # reasons.  In this case, the final cleanup might fail but the new
292d983712dSmrg	   # file should still install successfully.
293d983712dSmrg	   {
294d983712dSmrg	     if test -f "$dstdir/$dstfile"; then
295d983712dSmrg	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
296d983712dSmrg	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
297d983712dSmrg	       || {
298d983712dSmrg		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
299d983712dSmrg		 (exit 1); exit 1
300d983712dSmrg	       }
301d983712dSmrg	     else
302d983712dSmrg	       :
303d983712dSmrg	     fi
304d983712dSmrg	   } &&
305d983712dSmrg
306d983712dSmrg	   # Now rename the file to the real destination.
307d983712dSmrg	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
308d983712dSmrg	 }
309d983712dSmrg    }
310d983712dSmrg  fi || { (exit 1); exit 1; }
311d983712dSmrgdone
312d983712dSmrg
313d983712dSmrg# The final little trick to "correctly" pass the exit status to the exit trap.
314d983712dSmrg{
315d983712dSmrg  (exit 0); exit 0
316d983712dSmrg}
317d983712dSmrg
318d983712dSmrg# Local variables:
319d983712dSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
320d983712dSmrg# time-stamp-start: "scriptversion="
321d983712dSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
322d983712dSmrg# time-stamp-end: "$"
323d983712dSmrg# End:
324