install-sh revision 228692
128219Smsmith#!/bin/sh
255939Snsouch# install - install a program, script, or datafile
328219Smsmith
428219Smsmithscriptversion=2009-04-28.21; # UTC
528219Smsmith
628219Smsmith# This originates from X11R5 (mit/util/scripts/install.sh), which was
728219Smsmith# later released in X11R6 (xc/config/util/install.sh) with the
828219Smsmith# following copyright and license.
928219Smsmith#
1028219Smsmith# Copyright (C) 1994 X Consortium
1128219Smsmith#
1228219Smsmith# Permission is hereby granted, free of charge, to any person obtaining a copy
1328219Smsmith# of this software and associated documentation files (the "Software"), to
1428219Smsmith# deal in the Software without restriction, including without limitation the
1528219Smsmith# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1628219Smsmith# sell copies of the Software, and to permit persons to whom the Software is
1728219Smsmith# furnished to do so, subject to the following conditions:
1828219Smsmith#
1928219Smsmith# The above copyright notice and this permission notice shall be included in
2028219Smsmith# all copies or substantial portions of the Software.
2128219Smsmith#
2228219Smsmith# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2328219Smsmith# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2428219Smsmith# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2528219Smsmith# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2650477Speter# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2728219Smsmith# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2828219Smsmith#
2928219Smsmith# Except as contained in this notice, the name of the X Consortium shall not
3028219Smsmith# be used in advertising or otherwise to promote the sale, use or other deal-
3128219Smsmith# ings in this Software without prior written authorization from the X Consor-
3229020Sbde# tium.
3329020Sbde#
3428219Smsmith#
3528257Smsmith# FSF changes to this file are in the public domain.
3628257Smsmith#
3743293Sdillon# Calling this script install-sh is preferred over install.sh, to prevent
3828257Smsmith# `make' implicit rules from creating a file called install from it
3928257Smsmith# when there is no Makefile.
4038061Smsmith#
4138061Smsmith# This script is compatible with the BSD install script, but was written
4228219Smsmith# from scratch.
4338061Smsmith
4438061Smsmithnl='
4538061Smsmith'
4628219SmsmithIFS=" ""	$nl"
4738061Smsmith
4838061Smsmith# set DOITPROG to echo to test this script
4928219Smsmith
5042475Snsouch# Don't use :- since 4.3BSD and earlier shells don't like it.
5142475Snsouchdoit=${DOITPROG-}
5242475Snsouchif test -z "$doit"; then
5328219Smsmith  doit_exec=exec
5442475Snsouchelse
5542475Snsouch  doit_exec=$doit
5642475Snsouchfi
5738061Smsmith
5855939Snsouch# Put in absolute file names if you don't have them in your path;
5955939Snsouch# or use environment vars.
6055939Snsouch
6128219Smsmithchgrpprog=${CHGRPPROG-chgrp}
6238061Smsmithchmodprog=${CHMODPROG-chmod}
6338061Smsmithchownprog=${CHOWNPROG-chown}
6428219Smsmithcmpprog=${CMPPROG-cmp}
6528219Smsmithcpprog=${CPPROG-cp}
6628219Smsmithmkdirprog=${MKDIRPROG-mkdir}
6728219Smsmithmvprog=${MVPROG-mv}
6828219Smsmithrmprog=${RMPROG-rm}
6928219Smsmithstripprog=${STRIPPROG-strip}
7028219Smsmith
7136739Sphkposix_glob='?'
7228219Smsmithinitialize_posix_glob='
7328219Smsmith  test "$posix_glob" != "?" || {
7438061Smsmith    if (set -f) 2>/dev/null; then
7538061Smsmith      posix_glob=
7638061Smsmith    else
7738061Smsmith      posix_glob=:
7838061Smsmith    fi
7938061Smsmith  }
8028219Smsmith'
8128219Smsmith
8228219Smsmithposix_mkdir=
8328219Smsmith
8428219Smsmith# Desired mode of installed file.
8528219Smsmithmode=0755
8639134Snsouch
8728219Smsmithchgrpcmd=
8828219Smsmithchmodcmd=$chmodprog
8928219Smsmithchowncmd=
9028219Smsmithmvcmd=$mvprog
9128219Smsmithrmcmd="$rmprog -f"
9228219Smsmithstripcmd=
9328219Smsmith
9428219Smsmithsrc=
9528219Smsmithdst=
9628219Smsmithdir_arg=
9728219Smsmithdst_arg=
9828219Smsmith
9928219Smsmithcopy_on_change=false
10028219Smsmithno_target_directory=
10128219Smsmith
10228219Smsmithusage="\
10328219SmsmithUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10455939Snsouch   or: $0 [OPTION]... SRCFILES... DIRECTORY
10555939Snsouch   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10655939Snsouch   or: $0 [OPTION]... -d DIRECTORIES...
10755939Snsouch
10855939SnsouchIn the 1st form, copy SRCFILE to DSTFILE.
10955939SnsouchIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11055939SnsouchIn the 4th, create DIRECTORIES.
11155939Snsouch
11255939SnsouchOptions:
11355939Snsouch     --help     display this help and exit.
11455939Snsouch     --version  display version info and exit.
11555939Snsouch
11655939Snsouch  -c            (ignored)
11755939Snsouch  -C            install only if different (preserve the last data modification time)
11855939Snsouch  -d            create directories instead of installing files.
11955939Snsouch  -g GROUP      $chgrpprog installed files to GROUP.
12055939Snsouch  -m MODE       $chmodprog installed files to MODE.
12155939Snsouch  -o USER       $chownprog installed files to USER.
12255939Snsouch  -s            $stripprog installed files.
12355939Snsouch  -t DIRECTORY  install into DIRECTORY.
12455939Snsouch  -T            report an error if DSTFILE is a directory.
12555939Snsouch
12628219SmsmithEnvironment variables override the default commands:
12738061Smsmith  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12828219Smsmith  RMPROG STRIPPROG
12928219Smsmith"
13028219Smsmith
13128219Smsmithwhile test $# -ne 0; do
13228219Smsmith  case $1 in
13342475Snsouch    -c) ;;
13442475Snsouch
13528219Smsmith    -C) copy_on_change=true;;
13638061Smsmith
13738061Smsmith    -d) dir_arg=true;;
13838061Smsmith
13938061Smsmith    -g) chgrpcmd="$chgrpprog $2"
14038061Smsmith	shift;;
14128219Smsmith
14238061Smsmith    --help) echo "$usage"; exit $?;;
14338061Smsmith
14438061Smsmith    -m) mode=$2
14538061Smsmith	case $mode in
14638061Smsmith	  *' '* | *'	'* | *'
14738061Smsmith'*	  | *'*'* | *'?'* | *'['*)
14838061Smsmith	    echo "$0: invalid mode: $mode" >&2
14938061Smsmith	    exit 1;;
15045342Speter	esac
15138061Smsmith	shift;;
15238061Smsmith
15338061Smsmith    -o) chowncmd="$chownprog $2"
15438061Smsmith	shift;;
15538061Smsmith
15638061Smsmith    -s) stripcmd=$stripprog;;
15738061Smsmith
15838061Smsmith    -t) dst_arg=$2
15938061Smsmith	shift;;
16038061Smsmith
16138061Smsmith    -T) no_target_directory=true;;
16238061Smsmith
16338061Smsmith    --version) echo "$0 $scriptversion"; exit $?;;
16428219Smsmith
16528219Smsmith    --)	shift
16628219Smsmith	break;;
16738061Smsmith
16838061Smsmith    -*)	echo "$0: invalid option: $1" >&2
16938061Smsmith	exit 1;;
17038061Smsmith
17138061Smsmith    *)  break;;
17238061Smsmith  esac
17338061Smsmith  shift
17438061Smsmithdone
17538061Smsmith
17638061Smsmithif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17755939Snsouch  # When -d is used, all remaining arguments are directories to create.
17855939Snsouch  # When -t is used, the destination is already specified.
17955939Snsouch  # Otherwise, the last argument is the destination.  Remove it from $@.
18055939Snsouch  for arg
18155939Snsouch  do
18255939Snsouch    if test -n "$dst_arg"; then
18355939Snsouch      # $@ is not empty: it contains at least $arg.
18428219Smsmith      set fnord "$@" "$dst_arg"
18528219Smsmith      shift # fnord
18655939Snsouch    fi
18728219Smsmith    shift # arg
188118607Sjhb    dst_arg=$arg
18938061Smsmith  done
19038061Smsmithfi
19138061Smsmith
19238061Smsmithif test $# -eq 0; then
19338061Smsmith  if test -z "$dir_arg"; then
19438061Smsmith    echo "$0: no input file specified." >&2
19538061Smsmith    exit 1
19638061Smsmith  fi
19738061Smsmith  # It's OK to call `install-sh -d' without argument.
19838061Smsmith  # This can happen when creating conditional directories.
19938061Smsmith  exit 0
20038061Smsmithfi
201185003Sjhb
20255939Snsouchif test -z "$dir_arg"; then
20355939Snsouch  trap '(exit $?); exit' 1 2 13 15
20428219Smsmith
20528219Smsmith  # Set umask so as not to create temps with too-generous modes.
20655939Snsouch  # However, 'strip' requires both read and write access to temps.
20728219Smsmith  case $mode in
20828219Smsmith    # Optimize common cases.
209185003Sjhb    *644) cp_umask=133;;
21055939Snsouch    *755) cp_umask=22;;
21155939Snsouch
212185003Sjhb    *[0-7])
21328219Smsmith      if test -z "$stripcmd"; then
21428257Smsmith	u_plus_rw=
21528257Smsmith      else
21642475Snsouch	u_plus_rw='% 200'
21728257Smsmith      fi
21828257Smsmith      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21928219Smsmith    *)
22028219Smsmith      if test -z "$stripcmd"; then
22128219Smsmith	u_plus_rw=
22228219Smsmith      else
22328257Smsmith	u_plus_rw=,u+rw
22428257Smsmith      fi
22528257Smsmith      cp_umask=$mode$u_plus_rw;;
22628257Smsmith  esac
22728257Smsmithfi
22828257Smsmith
22928257Smsmithfor src
23028257Smsmithdo
23128257Smsmith  # Protect names starting with `-'.
23228257Smsmith  case $src in
23328257Smsmith    -*) src=./$src;;
23455939Snsouch  esac
23528257Smsmith
23655939Snsouch  if test -n "$dir_arg"; then
23755939Snsouch    dst=$src
23842475Snsouch    dstdir=$dst
23955939Snsouch    test -d "$dstdir"
24055939Snsouch    dstdir_status=$?
24138061Smsmith  else
24255939Snsouch
24328219Smsmith    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24428219Smsmith    # might cause directories to be created, which would be especially bad
24559712Sn_hibma    # if $src (and thus $dsttmp) contains '*'.
24655939Snsouch    if test ! -f "$src" && test ! -d "$src"; then
24755939Snsouch      echo "$0: $src does not exist." >&2
24855939Snsouch      exit 1
24928219Smsmith    fi
25055939Snsouch
25155939Snsouch    if test -z "$dst_arg"; then
25255939Snsouch      echo "$0: no destination specified." >&2
25355939Snsouch      exit 1
25455939Snsouch    fi
25555939Snsouch
25655939Snsouch    dst=$dst_arg
25755939Snsouch    # Protect names starting with `-'.
25855939Snsouch    case $dst in
25959712Sn_hibma      -*) dst=./$dst;;
26028219Smsmith    esac
26128219Smsmith
26228219Smsmith    # If destination is a directory, append the input filename; won't work
26338061Smsmith    # if double slashes aren't ignored.
26438061Smsmith    if test -d "$dst"; then
26555939Snsouch      if test -n "$no_target_directory"; then
26638061Smsmith	echo "$0: $dst_arg: Is a directory" >&2
26728219Smsmith	exit 1
268      fi
269      dstdir=$dst
270      dst=$dstdir/`basename "$src"`
271      dstdir_status=0
272    else
273      # Prefer dirname, but fall back on a substitute if dirname fails.
274      dstdir=`
275	(dirname "$dst") 2>/dev/null ||
276	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277	     X"$dst" : 'X\(//\)[^/]' \| \
278	     X"$dst" : 'X\(//\)$' \| \
279	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280	echo X"$dst" |
281	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282		   s//\1/
283		   q
284		 }
285		 /^X\(\/\/\)[^/].*/{
286		   s//\1/
287		   q
288		 }
289		 /^X\(\/\/\)$/{
290		   s//\1/
291		   q
292		 }
293		 /^X\(\/\).*/{
294		   s//\1/
295		   q
296		 }
297		 s/.*/./; q'
298      `
299
300      test -d "$dstdir"
301      dstdir_status=$?
302    fi
303  fi
304
305  obsolete_mkdir_used=false
306
307  if test $dstdir_status != 0; then
308    case $posix_mkdir in
309      '')
310	# Create intermediate dirs using mode 755 as modified by the umask.
311	# This is like FreeBSD 'install' as of 1997-10-28.
312	umask=`umask`
313	case $stripcmd.$umask in
314	  # Optimize common cases.
315	  *[2367][2367]) mkdir_umask=$umask;;
316	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317
318	  *[0-7])
319	    mkdir_umask=`expr $umask + 22 \
320	      - $umask % 100 % 40 + $umask % 20 \
321	      - $umask % 10 % 4 + $umask % 2
322	    `;;
323	  *) mkdir_umask=$umask,go-w;;
324	esac
325
326	# With -d, create the new directory with the user-specified mode.
327	# Otherwise, rely on $mkdir_umask.
328	if test -n "$dir_arg"; then
329	  mkdir_mode=-m$mode
330	else
331	  mkdir_mode=
332	fi
333
334	posix_mkdir=false
335	case $umask in
336	  *[123567][0-7][0-7])
337	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339	    ;;
340	  *)
341	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343
344	    if (umask $mkdir_umask &&
345		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346	    then
347	      if test -z "$dir_arg" || {
348		   # Check for POSIX incompatibilities with -m.
349		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350		   # other-writeable bit of parent directory when it shouldn't.
351		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353		   case $ls_ld_tmpdir in
354		     d????-?r-*) different_mode=700;;
355		     d????-?--*) different_mode=755;;
356		     *) false;;
357		   esac &&
358		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361		   }
362		 }
363	      then posix_mkdir=:
364	      fi
365	      rmdir "$tmpdir/d" "$tmpdir"
366	    else
367	      # Remove any dirs left behind by ancient mkdir implementations.
368	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369	    fi
370	    trap '' 0;;
371	esac;;
372    esac
373
374    if
375      $posix_mkdir && (
376	umask $mkdir_umask &&
377	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378      )
379    then :
380    else
381
382      # The umask is ridiculous, or mkdir does not conform to POSIX,
383      # or it failed possibly due to a race condition.  Create the
384      # directory the slow way, step by step, checking for races as we go.
385
386      case $dstdir in
387	/*) prefix='/';;
388	-*) prefix='./';;
389	*)  prefix='';;
390      esac
391
392      eval "$initialize_posix_glob"
393
394      oIFS=$IFS
395      IFS=/
396      $posix_glob set -f
397      set fnord $dstdir
398      shift
399      $posix_glob set +f
400      IFS=$oIFS
401
402      prefixes=
403
404      for d
405      do
406	test -z "$d" && continue
407
408	prefix=$prefix$d
409	if test -d "$prefix"; then
410	  prefixes=
411	else
412	  if $posix_mkdir; then
413	    (umask=$mkdir_umask &&
414	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415	    # Don't fail if two instances are running concurrently.
416	    test -d "$prefix" || exit 1
417	  else
418	    case $prefix in
419	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420	      *) qprefix=$prefix;;
421	    esac
422	    prefixes="$prefixes '$qprefix'"
423	  fi
424	fi
425	prefix=$prefix/
426      done
427
428      if test -n "$prefixes"; then
429	# Don't fail if two instances are running concurrently.
430	(umask $mkdir_umask &&
431	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432	  test -d "$dstdir" || exit 1
433	obsolete_mkdir_used=true
434      fi
435    fi
436  fi
437
438  if test -n "$dir_arg"; then
439    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443  else
444
445    # Make a couple of temp file names in the proper directory.
446    dsttmp=$dstdir/_inst.$$_
447    rmtmp=$dstdir/_rm.$$_
448
449    # Trap to clean up those temp files at exit.
450    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451
452    # Copy the file name to the temp name.
453    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454
455    # and set any options; do chmod last to preserve setuid bits.
456    #
457    # If any of these fail, we abort the whole thing.  If we want to
458    # ignore errors from any of these, just make sure not to ignore
459    # errors from the above "$doit $cpprog $src $dsttmp" command.
460    #
461    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465
466    # If -C, don't bother to copy if it wouldn't change the file.
467    if $copy_on_change &&
468       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470
471       eval "$initialize_posix_glob" &&
472       $posix_glob set -f &&
473       set X $old && old=:$2:$4:$5:$6 &&
474       set X $new && new=:$2:$4:$5:$6 &&
475       $posix_glob set +f &&
476
477       test "$old" = "$new" &&
478       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479    then
480      rm -f "$dsttmp"
481    else
482      # Rename the file to the real destination.
483      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484
485      # The rename failed, perhaps because mv can't rename something else
486      # to itself, or perhaps because mv is so ancient that it does not
487      # support -f.
488      {
489	# Now remove or move aside any old file at destination location.
490	# We try this two ways since rm can't unlink itself on some
491	# systems and the destination file might be busy for other
492	# reasons.  In this case, the final cleanup might fail but the new
493	# file should still install successfully.
494	{
495	  test ! -f "$dst" ||
496	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499	  } ||
500	  { echo "$0: cannot unlink or rename $dst" >&2
501	    (exit 1); exit 1
502	  }
503	} &&
504
505	# Now rename the file to the real destination.
506	$doit $mvcmd "$dsttmp" "$dst"
507      }
508    fi || exit 1
509
510    trap '' 0
511  fi
512done
513
514# Local variables:
515# eval: (add-hook 'write-file-hooks 'time-stamp)
516# time-stamp-start: "scriptversion="
517# time-stamp-format: "%:y-%02m-%02d.%02H"
518# time-stamp-time-zone: "UTC"
519# time-stamp-end: "; # UTC"
520# End:
521