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