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