install-sh revision 357bfbb2
1357bfbb2Smrg#!/bin/sh
2357bfbb2Smrg# install - install a program, script, or datafile
3357bfbb2Smrg
4357bfbb2Smrgscriptversion=2005-05-14.22
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
42357bfbb2Smrg# from scratch.  It can only install one file at a time, a restriction
43357bfbb2Smrg# shared with many OS's install programs.
44357bfbb2Smrg
45357bfbb2Smrg# set DOITPROG to echo to test this script
46357bfbb2Smrg
47357bfbb2Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
48357bfbb2Smrgdoit="${DOITPROG-}"
49357bfbb2Smrg
50357bfbb2Smrg# put in absolute paths if you don't have them in your path; or use env. vars.
51357bfbb2Smrg
52357bfbb2Smrgmvprog="${MVPROG-mv}"
53357bfbb2Smrgcpprog="${CPPROG-cp}"
54357bfbb2Smrgchmodprog="${CHMODPROG-chmod}"
55357bfbb2Smrgchownprog="${CHOWNPROG-chown}"
56357bfbb2Smrgchgrpprog="${CHGRPPROG-chgrp}"
57357bfbb2Smrgstripprog="${STRIPPROG-strip}"
58357bfbb2Smrgrmprog="${RMPROG-rm}"
59357bfbb2Smrgmkdirprog="${MKDIRPROG-mkdir}"
60357bfbb2Smrg
61357bfbb2Smrgchmodcmd="$chmodprog 0755"
62357bfbb2Smrgchowncmd=
63357bfbb2Smrgchgrpcmd=
64357bfbb2Smrgstripcmd=
65357bfbb2Smrgrmcmd="$rmprog -f"
66357bfbb2Smrgmvcmd="$mvprog"
67357bfbb2Smrgsrc=
68357bfbb2Smrgdst=
69357bfbb2Smrgdir_arg=
70357bfbb2Smrgdstarg=
71357bfbb2Smrgno_target_directory=
72357bfbb2Smrg
73357bfbb2Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
74357bfbb2Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
75357bfbb2Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
76357bfbb2Smrg   or: $0 [OPTION]... -d DIRECTORIES...
77357bfbb2Smrg
78357bfbb2SmrgIn the 1st form, copy SRCFILE to DSTFILE.
79357bfbb2SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
80357bfbb2SmrgIn the 4th, create DIRECTORIES.
81357bfbb2Smrg
82357bfbb2SmrgOptions:
83357bfbb2Smrg-c         (ignored)
84357bfbb2Smrg-d         create directories instead of installing files.
85357bfbb2Smrg-g GROUP   $chgrpprog installed files to GROUP.
86357bfbb2Smrg-m MODE    $chmodprog installed files to MODE.
87357bfbb2Smrg-o USER    $chownprog installed files to USER.
88357bfbb2Smrg-s         $stripprog installed files.
89357bfbb2Smrg-t DIRECTORY  install into DIRECTORY.
90357bfbb2Smrg-T         report an error if DSTFILE is a directory.
91357bfbb2Smrg--help     display this help and exit.
92357bfbb2Smrg--version  display version info and exit.
93357bfbb2Smrg
94357bfbb2SmrgEnvironment variables override the default commands:
95357bfbb2Smrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
96357bfbb2Smrg"
97357bfbb2Smrg
98357bfbb2Smrgwhile test -n "$1"; do
99357bfbb2Smrg  case $1 in
100357bfbb2Smrg    -c) shift
101357bfbb2Smrg        continue;;
102357bfbb2Smrg
103357bfbb2Smrg    -d) dir_arg=true
104357bfbb2Smrg        shift
105357bfbb2Smrg        continue;;
106357bfbb2Smrg
107357bfbb2Smrg    -g) chgrpcmd="$chgrpprog $2"
108357bfbb2Smrg        shift
109357bfbb2Smrg        shift
110357bfbb2Smrg        continue;;
111357bfbb2Smrg
112357bfbb2Smrg    --help) echo "$usage"; exit $?;;
113357bfbb2Smrg
114357bfbb2Smrg    -m) chmodcmd="$chmodprog $2"
115357bfbb2Smrg        shift
116357bfbb2Smrg        shift
117357bfbb2Smrg        continue;;
118357bfbb2Smrg
119357bfbb2Smrg    -o) chowncmd="$chownprog $2"
120357bfbb2Smrg        shift
121357bfbb2Smrg        shift
122357bfbb2Smrg        continue;;
123357bfbb2Smrg
124357bfbb2Smrg    -s) stripcmd=$stripprog
125357bfbb2Smrg        shift
126357bfbb2Smrg        continue;;
127357bfbb2Smrg
128357bfbb2Smrg    -t) dstarg=$2
129357bfbb2Smrg	shift
130357bfbb2Smrg	shift
131357bfbb2Smrg	continue;;
132357bfbb2Smrg
133357bfbb2Smrg    -T) no_target_directory=true
134357bfbb2Smrg	shift
135357bfbb2Smrg	continue;;
136357bfbb2Smrg
137357bfbb2Smrg    --version) echo "$0 $scriptversion"; exit $?;;
138357bfbb2Smrg
139357bfbb2Smrg    *)  # When -d is used, all remaining arguments are directories to create.
140357bfbb2Smrg	# When -t is used, the destination is already specified.
141357bfbb2Smrg	test -n "$dir_arg$dstarg" && break
142357bfbb2Smrg        # Otherwise, the last argument is the destination.  Remove it from $@.
143357bfbb2Smrg	for arg
144357bfbb2Smrg	do
145357bfbb2Smrg          if test -n "$dstarg"; then
146357bfbb2Smrg	    # $@ is not empty: it contains at least $arg.
147357bfbb2Smrg	    set fnord "$@" "$dstarg"
148357bfbb2Smrg	    shift # fnord
149357bfbb2Smrg	  fi
150357bfbb2Smrg	  shift # arg
151357bfbb2Smrg	  dstarg=$arg
152357bfbb2Smrg	done
153357bfbb2Smrg	break;;
154357bfbb2Smrg  esac
155357bfbb2Smrgdone
156357bfbb2Smrg
157357bfbb2Smrgif test -z "$1"; then
158357bfbb2Smrg  if test -z "$dir_arg"; then
159357bfbb2Smrg    echo "$0: no input file specified." >&2
160357bfbb2Smrg    exit 1
161357bfbb2Smrg  fi
162357bfbb2Smrg  # It's OK to call `install-sh -d' without argument.
163357bfbb2Smrg  # This can happen when creating conditional directories.
164357bfbb2Smrg  exit 0
165357bfbb2Smrgfi
166357bfbb2Smrg
167357bfbb2Smrgfor src
168357bfbb2Smrgdo
169357bfbb2Smrg  # Protect names starting with `-'.
170357bfbb2Smrg  case $src in
171357bfbb2Smrg    -*) src=./$src ;;
172357bfbb2Smrg  esac
173357bfbb2Smrg
174357bfbb2Smrg  if test -n "$dir_arg"; then
175357bfbb2Smrg    dst=$src
176357bfbb2Smrg    src=
177357bfbb2Smrg
178357bfbb2Smrg    if test -d "$dst"; then
179357bfbb2Smrg      mkdircmd=:
180357bfbb2Smrg      chmodcmd=
181357bfbb2Smrg    else
182357bfbb2Smrg      mkdircmd=$mkdirprog
183357bfbb2Smrg    fi
184357bfbb2Smrg  else
185357bfbb2Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
186357bfbb2Smrg    # might cause directories to be created, which would be especially bad
187357bfbb2Smrg    # if $src (and thus $dsttmp) contains '*'.
188357bfbb2Smrg    if test ! -f "$src" && test ! -d "$src"; then
189357bfbb2Smrg      echo "$0: $src does not exist." >&2
190357bfbb2Smrg      exit 1
191357bfbb2Smrg    fi
192357bfbb2Smrg
193357bfbb2Smrg    if test -z "$dstarg"; then
194357bfbb2Smrg      echo "$0: no destination specified." >&2
195357bfbb2Smrg      exit 1
196357bfbb2Smrg    fi
197357bfbb2Smrg
198357bfbb2Smrg    dst=$dstarg
199357bfbb2Smrg    # Protect names starting with `-'.
200357bfbb2Smrg    case $dst in
201357bfbb2Smrg      -*) dst=./$dst ;;
202357bfbb2Smrg    esac
203357bfbb2Smrg
204357bfbb2Smrg    # If destination is a directory, append the input filename; won't work
205357bfbb2Smrg    # if double slashes aren't ignored.
206357bfbb2Smrg    if test -d "$dst"; then
207357bfbb2Smrg      if test -n "$no_target_directory"; then
208357bfbb2Smrg	echo "$0: $dstarg: Is a directory" >&2
209357bfbb2Smrg	exit 1
210357bfbb2Smrg      fi
211357bfbb2Smrg      dst=$dst/`basename "$src"`
212357bfbb2Smrg    fi
213357bfbb2Smrg  fi
214357bfbb2Smrg
215357bfbb2Smrg  # This sed command emulates the dirname command.
216357bfbb2Smrg  dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
217357bfbb2Smrg
218357bfbb2Smrg  # Make sure that the destination directory exists.
219357bfbb2Smrg
220357bfbb2Smrg  # Skip lots of stat calls in the usual case.
221357bfbb2Smrg  if test ! -d "$dstdir"; then
222357bfbb2Smrg    defaultIFS='
223357bfbb2Smrg	 '
224357bfbb2Smrg    IFS="${IFS-$defaultIFS}"
225357bfbb2Smrg
226357bfbb2Smrg    oIFS=$IFS
227357bfbb2Smrg    # Some sh's can't handle IFS=/ for some reason.
228357bfbb2Smrg    IFS='%'
229357bfbb2Smrg    set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
230357bfbb2Smrg    shift
231357bfbb2Smrg    IFS=$oIFS
232357bfbb2Smrg
233357bfbb2Smrg    pathcomp=
234357bfbb2Smrg
235357bfbb2Smrg    while test $# -ne 0 ; do
236357bfbb2Smrg      pathcomp=$pathcomp$1
237357bfbb2Smrg      shift
238357bfbb2Smrg      if test ! -d "$pathcomp"; then
239357bfbb2Smrg        $mkdirprog "$pathcomp"
240357bfbb2Smrg	# mkdir can fail with a `File exist' error in case several
241357bfbb2Smrg	# install-sh are creating the directory concurrently.  This
242357bfbb2Smrg	# is OK.
243357bfbb2Smrg	test -d "$pathcomp" || exit
244357bfbb2Smrg      fi
245357bfbb2Smrg      pathcomp=$pathcomp/
246357bfbb2Smrg    done
247357bfbb2Smrg  fi
248357bfbb2Smrg
249357bfbb2Smrg  if test -n "$dir_arg"; then
250357bfbb2Smrg    $doit $mkdircmd "$dst" \
251357bfbb2Smrg      && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
252357bfbb2Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
253357bfbb2Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
254357bfbb2Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
255357bfbb2Smrg
256357bfbb2Smrg  else
257357bfbb2Smrg    dstfile=`basename "$dst"`
258357bfbb2Smrg
259357bfbb2Smrg    # Make a couple of temp file names in the proper directory.
260357bfbb2Smrg    dsttmp=$dstdir/_inst.$$_
261357bfbb2Smrg    rmtmp=$dstdir/_rm.$$_
262357bfbb2Smrg
263357bfbb2Smrg    # Trap to clean up those temp files at exit.
264357bfbb2Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
265357bfbb2Smrg    trap '(exit $?); exit' 1 2 13 15
266357bfbb2Smrg
267357bfbb2Smrg    # Copy the file name to the temp name.
268357bfbb2Smrg    $doit $cpprog "$src" "$dsttmp" &&
269357bfbb2Smrg
270357bfbb2Smrg    # and set any options; do chmod last to preserve setuid bits.
271357bfbb2Smrg    #
272357bfbb2Smrg    # If any of these fail, we abort the whole thing.  If we want to
273357bfbb2Smrg    # ignore errors from any of these, just make sure not to ignore
274357bfbb2Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
275357bfbb2Smrg    #
276357bfbb2Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
277357bfbb2Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
278357bfbb2Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
279357bfbb2Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
280357bfbb2Smrg
281357bfbb2Smrg    # Now rename the file to the real destination.
282357bfbb2Smrg    { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
283357bfbb2Smrg      || {
284357bfbb2Smrg	   # The rename failed, perhaps because mv can't rename something else
285357bfbb2Smrg	   # to itself, or perhaps because mv is so ancient that it does not
286357bfbb2Smrg	   # support -f.
287357bfbb2Smrg
288357bfbb2Smrg	   # Now remove or move aside any old file at destination location.
289357bfbb2Smrg	   # We try this two ways since rm can't unlink itself on some
290357bfbb2Smrg	   # systems and the destination file might be busy for other
291357bfbb2Smrg	   # reasons.  In this case, the final cleanup might fail but the new
292357bfbb2Smrg	   # file should still install successfully.
293357bfbb2Smrg	   {
294357bfbb2Smrg	     if test -f "$dstdir/$dstfile"; then
295357bfbb2Smrg	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
296357bfbb2Smrg	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
297357bfbb2Smrg	       || {
298357bfbb2Smrg		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
299357bfbb2Smrg		 (exit 1); exit 1
300357bfbb2Smrg	       }
301357bfbb2Smrg	     else
302357bfbb2Smrg	       :
303357bfbb2Smrg	     fi
304357bfbb2Smrg	   } &&
305357bfbb2Smrg
306357bfbb2Smrg	   # Now rename the file to the real destination.
307357bfbb2Smrg	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
308357bfbb2Smrg	 }
309357bfbb2Smrg    }
310357bfbb2Smrg  fi || { (exit 1); exit 1; }
311357bfbb2Smrgdone
312357bfbb2Smrg
313357bfbb2Smrg# The final little trick to "correctly" pass the exit status to the exit trap.
314357bfbb2Smrg{
315357bfbb2Smrg  (exit 0); exit 0
316357bfbb2Smrg}
317357bfbb2Smrg
318357bfbb2Smrg# Local variables:
319357bfbb2Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
320357bfbb2Smrg# time-stamp-start: "scriptversion="
321357bfbb2Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
322357bfbb2Smrg# time-stamp-end: "$"
323357bfbb2Smrg# End:
324