1#!/bin/sh
2# install - install a program, script, or datafile
3
4if [ -d /usr/xpg4/bin ]
5then
6   PATH=/usr/xpg4/bin:$PATH
7   export PATH
8fi
9
10scriptversion=2005-11-07.23
11
12# This script was modified by URLfilerDB to
13# - ignore and warn when "-o <user>" if installer is not root (to be able to make packages as non-root)
14
15iamroot="no"
16if test `id -u` -eq 0
17then
18   iamroot="yes"
19fi
20
21# This originates from X11R5 (mit/util/scripts/install.sh), which was
22# later released in X11R6 (xc/config/util/install.sh) with the
23# following copyright and license.
24#
25# Copyright (C) 1994 X Consortium
26#
27# Permission is hereby granted, free of charge, to any person obtaining a copy
28# of this software and associated documentation files (the "Software"), to
29# deal in the Software without restriction, including without limitation the
30# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
31# sell copies of the Software, and to permit persons to whom the Software is
32# furnished to do so, subject to the following conditions:
33#
34# The above copyright notice and this permission notice shall be included in
35# all copies or substantial portions of the Software.
36#
37# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
40# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
41# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
42# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43#
44# Except as contained in this notice, the name of the X Consortium shall not
45# be used in advertising or otherwise to promote the sale, use or other deal-
46# ings in this Software without prior written authorization from the X Consor-
47# tium.
48#
49#
50# FSF changes to this file are in the public domain.
51#
52# Calling this script install-sh is preferred over install.sh, to prevent
53# `make' implicit rules from creating a file called install from it
54# when there is no Makefile.
55#
56# This script is compatible with the BSD install script, but was written
57# from scratch.  It can only install one file at a time, a restriction
58# shared with many OS's install programs.
59
60# set DOITPROG to echo to test this script
61
62# Don't use :- since 4.3BSD and earlier shells don't like it.
63doit="${DOITPROG-}"
64
65# put in absolute paths if you don't have them in your path; or use env. vars.
66
67mvprog="${MVPROG-mv}"
68cpprog="${CPPROG-cp}"
69chmodprog="${CHMODPROG-chmod}"
70chownprog="${CHOWNPROG-chown}"
71chgrpprog="${CHGRPPROG-chgrp}"
72stripprog="${STRIPPROG-strip}"
73rmprog="${RMPROG-rm}"
74mkdirprog="${MKDIRPROG-mkdir}"
75
76posix_glob=
77posix_mkdir=
78
79# Symbolic mode for testing mkdir with directories.
80# It is the same as 755, but also tests that "u+" works.
81test_mode=u=rwx,g=rx,o=rx,u+wx
82
83# Desired mode of installed file.
84mode=0755
85
86# Desired mode of newly created intermediate directories.
87# It is empty if not known yet.
88intermediate_mode=
89
90chmodcmd=$chmodprog
91chowncmd=
92chgrpcmd=
93stripcmd=
94rmcmd="$rmprog -f"
95mvcmd="$mvprog"
96src=
97dst=
98dir_arg=
99dstarg=
100no_target_directory=
101
102usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
103   or: $0 [OPTION]... SRCFILES... DIRECTORY
104   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
105   or: $0 [OPTION]... -d DIRECTORIES...
106
107In the 1st form, copy SRCFILE to DSTFILE.
108In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
109In the 4th, create DIRECTORIES.
110
111Options:
112-c         (ignored)
113-d         create directories instead of installing files.
114-g GROUP   $chgrpprog installed files to GROUP.
115-m MODE    $chmodprog installed files to MODE.
116-o USER    $chownprog installed files to USER.
117-s         $stripprog installed files.
118-t DIRECTORY  install into DIRECTORY.
119-T         report an error if DSTFILE is a directory.
120--help     display this help and exit.
121--version  display version info and exit.
122
123Environment variables override the default commands:
124  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
125"
126
127while test -n "$1"; do
128  case $1 in
129    -c) shift
130        continue;;
131
132    -d) dir_arg=true
133        shift
134        continue;;
135
136    -g) chgrpcmd="$chgrpprog $2"
137        shift
138        shift
139        continue;;
140
141    --help) echo "$usage"; exit $?;;
142
143    -m) mode=$2
144        shift
145        shift
146        continue;;
147
148    -o)
149	if test $iamroot = no
150	then
151	   echo "warning: install-sh ignores -o option since installer is not root"
152        else
153    	   chowncmd="$chownprog $2"
154	fi
155        shift
156        shift
157        continue;;
158
159    -s) stripcmd=$stripprog
160        shift
161        continue;;
162
163    -t) dstarg=$2
164	shift
165	shift
166	continue;;
167
168    -T) no_target_directory=true
169	shift
170	continue;;
171
172    --version) echo "$0 $scriptversion"; exit $?;;
173
174    *)  # When -d is used, all remaining arguments are directories to create.
175	# When -t is used, the destination is already specified.
176	test -n "$dir_arg$dstarg" && break
177        # Otherwise, the last argument is the destination.  Remove it from $@.
178	for arg
179	do
180          if test -n "$dstarg"; then
181	    # $@ is not empty: it contains at least $arg.
182	    set fnord "$@" "$dstarg"
183	    shift # fnord
184	  fi
185	  shift # arg
186	  dstarg=$arg
187	done
188	break;;
189  esac
190done
191
192if test -z "$1"; then
193  if test -z "$dir_arg"; then
194    echo "$0: no input file specified." >&2
195    exit 1
196  fi
197  # It's OK to call `install-sh -d' without argument.
198  # This can happen when creating conditional directories.
199  exit 0
200fi
201
202test -n "$dir_arg" || trap '(exit $?); exit' 1 2 13 15
203
204for src
205do
206  # Protect names starting with `-'.
207  case $src in
208    -*) src=./$src ;;
209  esac
210
211  if test -n "$dir_arg"; then
212    dst=$src
213    dstdir=$dst
214    test -d "$dstdir"
215    dstdir_status=$?
216  else
217
218    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
219    # might cause directories to be created, which would be especially bad
220    # if $src (and thus $dsttmp) contains '*'.
221    if test ! -f "$src" && test ! -d "$src"; then
222      echo "$0: $src does not exist." >&2
223      exit 1
224    fi
225
226    if test -z "$dstarg"; then
227      echo "$0: no destination specified." >&2
228      exit 1
229    fi
230
231    dst=$dstarg
232    # Protect names starting with `-'.
233    case $dst in
234      -*) dst=./$dst ;;
235    esac
236
237    # If destination is a directory, append the input filename; won't work
238    # if double slashes aren't ignored.
239    if test -d "$dst"; then
240      if test -n "$no_target_directory"; then
241	echo "$0: $dstarg: Is a directory" >&2
242	exit 1
243      fi
244      dstdir=$dst
245      dst=$dstdir/`basename "$src"`
246      dstdir_status=0
247    else
248      # Prefer dirname, but fall back on a substitute if dirname fails.
249      dstdir=`
250	(dirname "$dst") 2>/dev/null ||
251	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
252	     X"$dst" : 'X\(//\)[^/]' \| \
253	     X"$dst" : 'X\(//\)$' \| \
254	     X"$dst" : 'X\(/\)' \| \
255	     .       : '\(.\)' 2>/dev/null ||
256	echo X"$dst" |
257	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ s//\1/; q; }
258		  /^X\(\/\/\)[^/].*/{ s//\1/; q; }
259		  /^X\(\/\/\)$/{ s//\1/; q; }
260		  /^X\(\/\).*/{ s//\1/; q; }
261		  s/.*/./; q'
262      `
263
264      test -d "$dstdir"
265      dstdir_status=$?
266    fi
267  fi
268
269  obsolete_mkdir_used=false
270
271  if test $dstdir_status != 0; then
272    case $posix_mkdir in
273      '')
274	posix_mkdir=false
275	if $mkdirprog -m $test_mode -p -- / >/dev/null 2>&1; then
276	  posix_mkdir=true
277	else
278	  # Remove any dirs left behind by ancient mkdir implementations.
279	  rmdir ./-m "$test_mode" ./-p ./-- 2>/dev/null
280	fi ;;
281    esac
282
283    if
284      $posix_mkdir && {
285
286	# With -d, create the new directory with the user-specified mode.
287	# Otherwise, create it using the same intermediate mode that
288	# mkdir -p would use when creating intermediate directories.
289	# POSIX says that this mode is "$(umask -S),u+wx", so use that
290	# if umask -S works.
291
292	if test -n "$dir_arg"; then
293	  mkdir_mode=$mode
294	else
295	  case $intermediate_mode in
296	    '')
297	      if umask_S=`(umask -S) 2>/dev/null`; then
298		intermediate_mode=$umask_S,u+wx
299	      else
300		intermediate_mode=$test_mode
301	      fi ;;
302	  esac
303	  mkdir_mode=$intermediate_mode
304	fi
305
306	$mkdirprog -m "$mkdir_mode" -p -- "$dstdir"
307      }
308    then :
309    else
310
311      # mkdir does not conform to POSIX, or it failed possibly due to
312      # a race condition.  Create the directory the slow way, step by
313      # step, checking for races as we go.
314
315      case $dstdir in
316	/*) pathcomp=/ ;;
317	-*) pathcomp=./ ;;
318	*)  pathcomp= ;;
319      esac
320
321      case $posix_glob in
322        '')
323	  if (set -f) 2>/dev/null; then
324	    posix_glob=true
325	  else
326	    posix_glob=false
327	  fi ;;
328      esac
329
330      oIFS=$IFS
331      IFS=/
332      $posix_glob && set -f
333      set fnord $dstdir
334      shift
335      $posix_glob && set +f
336      IFS=$oIFS
337
338      for d
339      do
340	test "x$d" = x && continue
341
342	pathcomp=$pathcomp$d
343	if test ! -d "$pathcomp"; then
344	  $mkdirprog "$pathcomp"
345	  # Don't fail if two instances are running concurrently.
346	  test -d "$pathcomp" || exit 1
347	fi
348	pathcomp=$pathcomp/
349      done
350      obsolete_mkdir_used=true
351    fi
352  fi
353
354  if test -n "$dir_arg"; then
355    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
356    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
357    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
358      test -z "$chmodcmd" || $doit $chmodcmd "$mode" "$dst"; } || exit 1
359  else
360
361    # Make a couple of temp file names in the proper directory.
362    dsttmp=$dstdir/_inst.$$_
363    rmtmp=$dstdir/_rm.$$_
364
365    # Trap to clean up those temp files at exit.
366    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
367
368    # Copy the file name to the temp name.
369    $doit $cpprog "$src" "$dsttmp" &&
370
371    # and set any options; do chmod last to preserve setuid bits.
372    #
373    # If any of these fail, we abort the whole thing.  If we want to
374    # ignore errors from any of these, just make sure not to ignore
375    # errors from the above "$doit $cpprog $src $dsttmp" command.
376    #
377    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
378      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
379      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
380      && { test -z "$chmodcmd" || $doit $chmodcmd "$mode" "$dsttmp"; } &&
381
382    # Now rename the file to the real destination.
383    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
384      || {
385	   # The rename failed, perhaps because mv can't rename something else
386	   # to itself, or perhaps because mv is so ancient that it does not
387	   # support -f.
388
389	   # Now remove or move aside any old file at destination location.
390	   # We try this two ways since rm can't unlink itself on some
391	   # systems and the destination file might be busy for other
392	   # reasons.  In this case, the final cleanup might fail but the new
393	   # file should still install successfully.
394	   {
395	     if test -f "$dst"; then
396	       $doit $rmcmd -f "$dst" 2>/dev/null \
397	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
398		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
399	       || {
400		 echo "$0: cannot unlink or rename $dst" >&2
401		 (exit 1); exit 1
402	       }
403	     else
404	       :
405	     fi
406	   } &&
407
408	   # Now rename the file to the real destination.
409	   $doit $mvcmd "$dsttmp" "$dst"
410	 }
411    } || exit 1
412
413    trap '' 0
414  fi
415done
416
417# Local variables:
418# eval: (add-hook 'write-file-hooks 'time-stamp)
419# time-stamp-start: "scriptversion="
420# time-stamp-format: "%:y-%02m-%02d.%02H"
421# time-stamp-end: "$"
422# End:
423