1151497Sru#!/bin/sh
218099Spst# install - install a program, script, or datafile
3151497Sru
4151497Sruscriptversion=2005-07-09.12
5151497Sru
6151497Sru# This originates from X11R5 (mit/util/scripts/install.sh), which was
7151497Sru# later released in X11R6 (xc/config/util/install.sh) with the
8151497Sru# following copyright and license.
918099Spst#
10151497Sru# Copyright (C) 1994 X Consortium
11151497Sru#
12151497Sru# Permission is hereby granted, free of charge, to any person obtaining a copy
13151497Sru# of this software and associated documentation files (the "Software"), to
14151497Sru# deal in the Software without restriction, including without limitation the
15151497Sru# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16151497Sru# sell copies of the Software, and to permit persons to whom the Software is
17151497Sru# furnished to do so, subject to the following conditions:
18151497Sru#
19151497Sru# The above copyright notice and this permission notice shall be included in
20151497Sru# all copies or substantial portions of the Software.
21151497Sru#
22151497Sru# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23151497Sru# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24151497Sru# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25151497Sru# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26151497Sru# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27151497Sru# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28151497Sru#
29151497Sru# Except as contained in this notice, the name of the X Consortium shall not
30151497Sru# be used in advertising or otherwise to promote the sale, use or other deal-
31151497Sru# ings in this Software without prior written authorization from the X Consor-
32151497Sru# tium.
33151497Sru#
34151497Sru#
35151497Sru# FSF changes to this file are in the public domain.
36151497Sru#
3718099Spst# Calling this script install-sh is preferred over install.sh, to prevent
3818099Spst# `make' implicit rules from creating a file called install from it
3918099Spst# when there is no Makefile.
4018099Spst#
4118099Spst# This script is compatible with the BSD install script, but was written
42151497Sru# from scratch.  It can only install one file at a time, a restriction
43151497Sru# shared with many OS's install programs.
4418099Spst
4518099Spst# set DOITPROG to echo to test this script
4618099Spst
4718099Spst# Don't use :- since 4.3BSD and earlier shells don't like it.
4818099Spstdoit="${DOITPROG-}"
4918099Spst
5018099Spst# put in absolute paths if you don't have them in your path; or use env. vars.
5118099Spst
5218099Spstmvprog="${MVPROG-mv}"
5318099Spstcpprog="${CPPROG-cp}"
5418099Spstchmodprog="${CHMODPROG-chmod}"
5518099Spstchownprog="${CHOWNPROG-chown}"
5618099Spstchgrpprog="${CHGRPPROG-chgrp}"
5718099Spststripprog="${STRIPPROG-strip}"
5818099Spstrmprog="${RMPROG-rm}"
5918099Spstmkdirprog="${MKDIRPROG-mkdir}"
6018099Spst
6118099Spstchmodcmd="$chmodprog 0755"
62151497Sruchowncmd=
63151497Sruchgrpcmd=
64151497Srustripcmd=
6518099Spstrmcmd="$rmprog -f"
6618099Spstmvcmd="$mvprog"
67151497Srusrc=
68151497Srudst=
69151497Srudir_arg=
70151497Srudstarg=
71151497Sruno_target_directory=
7218099Spst
73151497Sruusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
74151497Sru   or: $0 [OPTION]... SRCFILES... DIRECTORY
75151497Sru   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
76151497Sru   or: $0 [OPTION]... -d DIRECTORIES...
7718099Spst
78151497SruIn the 1st form, copy SRCFILE to DSTFILE.
79151497SruIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
80151497SruIn the 4th, create DIRECTORIES.
8118099Spst
82151497SruOptions:
83151497Sru-c         (ignored)
84151497Sru-d         create directories instead of installing files.
85151497Sru-g GROUP   $chgrpprog installed files to GROUP.
86151497Sru-m MODE    $chmodprog installed files to MODE.
87151497Sru-o USER    $chownprog installed files to USER.
88151497Sru-s         $stripprog installed files.
89151497Sru-t DIRECTORY  install into DIRECTORY.
90151497Sru-T         report an error if DSTFILE is a directory.
91151497Sru--help     display this help and exit.
92151497Sru--version  display version info and exit.
9318099Spst
94151497SruEnvironment variables override the default commands:
95151497Sru  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
96151497Sru"
9718099Spst
98151497Sruwhile test -n "$1"; do
99151497Sru  case $1 in
100151497Sru    -c) shift
101151497Sru        continue;;
10218099Spst
103151497Sru    -d) dir_arg=true
104151497Sru        shift
105151497Sru        continue;;
10618099Spst
107151497Sru    -g) chgrpcmd="$chgrpprog $2"
108151497Sru        shift
109151497Sru        shift
110151497Sru        continue;;
11118099Spst
112151497Sru    --help) echo "$usage"; exit $?;;
11318099Spst
114151497Sru    -m) chmodcmd="$chmodprog $2"
115151497Sru        shift
116151497Sru        shift
117151497Sru        continue;;
11818099Spst
119151497Sru    -o) chowncmd="$chownprog $2"
120151497Sru        shift
121151497Sru        shift
122151497Sru        continue;;
12318099Spst
124151497Sru    -s) stripcmd=$stripprog
125151497Sru        shift
126151497Sru        continue;;
12718099Spst
128151497Sru    -t) dstarg=$2
129151497Sru	shift
130151497Sru	shift
131151497Sru	continue;;
13218099Spst
133151497Sru    -T) no_target_directory=true
134151497Sru	shift
135151497Sru	continue;;
13618099Spst
137151497Sru    --version) echo "$0 $scriptversion"; exit $?;;
13818099Spst
139151497Sru    *)  # When -d is used, all remaining arguments are directories to create.
140151497Sru	# When -t is used, the destination is already specified.
141151497Sru	test -n "$dir_arg$dstarg" && break
142151497Sru        # Otherwise, the last argument is the destination.  Remove it from $@.
143151497Sru	for arg
144151497Sru	do
145151497Sru          if test -n "$dstarg"; then
146151497Sru	    # $@ is not empty: it contains at least $arg.
147151497Sru	    set fnord "$@" "$dstarg"
148151497Sru	    shift # fnord
149151497Sru	  fi
150151497Sru	  shift # arg
151151497Sru	  dstarg=$arg
152151497Sru	done
153151497Sru	break;;
154151497Sru  esac
155151497Srudone
156151497Sru
157151497Sruif test -z "$1"; then
158151497Sru  if test -z "$dir_arg"; then
159151497Sru    echo "$0: no input file specified." >&2
160151497Sru    exit 1
161151497Sru  fi
162151497Sru  # It's OK to call `install-sh -d' without argument.
163151497Sru  # This can happen when creating conditional directories.
164151497Sru  exit 0
16518099Spstfi
16618099Spst
167151497Srufor src
168151497Srudo
169151497Sru  # Protect names starting with `-'.
170151497Sru  case $src in
171151497Sru    -*) src=./$src ;;
172151497Sru  esac
17318099Spst
174151497Sru  if test -n "$dir_arg"; then
175151497Sru    dst=$src
176151497Sru    src=
17718099Spst
178151497Sru    if test -d "$dst"; then
179151497Sru      mkdircmd=:
180151497Sru      chmodcmd=
181151497Sru    else
182151497Sru      mkdircmd=$mkdirprog
183151497Sru    fi
184151497Sru  else
185151497Sru    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
186151497Sru    # might cause directories to be created, which would be especially bad
187151497Sru    # if $src (and thus $dsttmp) contains '*'.
188151497Sru    if test ! -f "$src" && test ! -d "$src"; then
189151497Sru      echo "$0: $src does not exist." >&2
190151497Sru      exit 1
191151497Sru    fi
19218099Spst
193151497Sru    if test -z "$dstarg"; then
194151497Sru      echo "$0: no destination specified." >&2
195151497Sru      exit 1
196151497Sru    fi
19718099Spst
198151497Sru    dst=$dstarg
199151497Sru    # Protect names starting with `-'.
200151497Sru    case $dst in
201151497Sru      -*) dst=./$dst ;;
202151497Sru    esac
20318099Spst
204151497Sru    # If destination is a directory, append the input filename; won't work
205151497Sru    # if double slashes aren't ignored.
206151497Sru    if test -d "$dst"; then
207151497Sru      if test -n "$no_target_directory"; then
208151497Sru	echo "$0: $dstarg: Is a directory" >&2
209151497Sru	exit 1
210151497Sru      fi
211151497Sru      dst=$dst/`basename "$src"`
212151497Sru    fi
213151497Sru  fi
21418099Spst
215151497Sru  # This sed command emulates the dirname command.
216151497Sru  dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
21718099Spst
218151497Sru  # Make sure that the destination directory exists.
21918099Spst
220151497Sru  # Skip lots of stat calls in the usual case.
221151497Sru  if test ! -d "$dstdir"; then
222151497Sru    case $dstdir in
223151497Sru      /*) pathcomp=/ ;;
224151497Sru      -*) pathcomp=./ ;;
225151497Sru      *)  pathcomp= ;;
226151497Sru    esac
227151497Sru    oIFS=$IFS
228151497Sru    IFS=/
229151497Sru    set fnord $dstdir
230151497Sru    shift
231151497Sru    IFS=$oIFS
23218099Spst
233151497Sru    for d
234151497Sru    do
235151497Sru      test "x$d" = x && continue
23618099Spst
237151497Sru      pathcomp=$pathcomp$d
238151497Sru      if test ! -d "$pathcomp"; then
239151497Sru        $mkdirprog "$pathcomp"
240151497Sru	# mkdir can fail with a `File exist' error in case several
241151497Sru	# install-sh are creating the directory concurrently.  This
242151497Sru	# is OK.
243151497Sru	test -d "$pathcomp" || exit 1
244151497Sru      fi
245151497Sru      pathcomp=$pathcomp/
246151497Sru    done
247151497Sru  fi
24818099Spst
249151497Sru  if test -n "$dir_arg"; then
250151497Sru    $doit $mkdircmd "$dst" \
251151497Sru      && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
252151497Sru      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
253151497Sru      && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
254151497Sru      && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
25518099Spst
256151497Sru  else
257151497Sru    dstfile=`basename "$dst"`
25818099Spst
259151497Sru    # Make a couple of temp file names in the proper directory.
260151497Sru    dsttmp=$dstdir/_inst.$$_
261151497Sru    rmtmp=$dstdir/_rm.$$_
26218099Spst
263151497Sru    # Trap to clean up those temp files at exit.
264151497Sru    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
265151497Sru    trap '(exit $?); exit' 1 2 13 15
26618099Spst
267151497Sru    # Copy the file name to the temp name.
268151497Sru    $doit $cpprog "$src" "$dsttmp" &&
26918099Spst
270151497Sru    # and set any options; do chmod last to preserve setuid bits.
271151497Sru    #
272151497Sru    # If any of these fail, we abort the whole thing.  If we want to
273151497Sru    # ignore errors from any of these, just make sure not to ignore
274151497Sru    # errors from the above "$doit $cpprog $src $dsttmp" command.
275151497Sru    #
276151497Sru    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
277151497Sru      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
278151497Sru      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
279151497Sru      && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
28018099Spst
281151497Sru    # Now rename the file to the real destination.
282151497Sru    { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
283151497Sru      || {
284151497Sru	   # The rename failed, perhaps because mv can't rename something else
285151497Sru	   # to itself, or perhaps because mv is so ancient that it does not
286151497Sru	   # support -f.
28718099Spst
288151497Sru	   # Now remove or move aside any old file at destination location.
289151497Sru	   # We try this two ways since rm can't unlink itself on some
290151497Sru	   # systems and the destination file might be busy for other
291151497Sru	   # reasons.  In this case, the final cleanup might fail but the new
292151497Sru	   # file should still install successfully.
293151497Sru	   {
294151497Sru	     if test -f "$dstdir/$dstfile"; then
295151497Sru	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
296151497Sru	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
297151497Sru	       || {
298151497Sru		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
299151497Sru		 (exit 1); exit 1
300151497Sru	       }
301151497Sru	     else
302151497Sru	       :
303151497Sru	     fi
304151497Sru	   } &&
30518099Spst
306151497Sru	   # Now rename the file to the real destination.
307151497Sru	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
308151497Sru	 }
309151497Sru    }
310151497Sru  fi || { (exit 1); exit 1; }
311151497Srudone
31218099Spst
313151497Sru# The final little trick to "correctly" pass the exit status to the exit trap.
314151497Sru{
315151497Sru  (exit 0); exit 0
316151497Sru}
31718099Spst
318151497Sru# Local variables:
319151497Sru# eval: (add-hook 'write-file-hooks 'time-stamp)
320151497Sru# time-stamp-start: "scriptversion="
321151497Sru# time-stamp-format: "%:y-%02m-%02d.%02H"
322151497Sru# time-stamp-end: "$"
323151497Sru# End:
324