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