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