Search
lxdream.org :: lxdream/install-sh
lxdream 0.9.1
released Jun 29
Download Now
filename install-sh
changeset 179:18dacd91aa73
author nkeynes
date Tue Jun 27 14:02:27 2006 +0000 (13 years ago)
permissions -rwxr-xr-x
last change Add default automake files
file annotate diff log raw
nkeynes@179
     1
#!/bin/sh
nkeynes@179
     2
#
nkeynes@179
     3
# install - install a program, script, or datafile
nkeynes@179
     4
#
nkeynes@179
     5
# This originates from X11R5 (mit/util/scripts/install.sh), which was
nkeynes@179
     6
# later released in X11R6 (xc/config/util/install.sh) with the
nkeynes@179
     7
# following copyright and license.
nkeynes@179
     8
#
nkeynes@179
     9
# Copyright (C) 1994 X Consortium
nkeynes@179
    10
#
nkeynes@179
    11
# Permission is hereby granted, free of charge, to any person obtaining a copy
nkeynes@179
    12
# of this software and associated documentation files (the "Software"), to
nkeynes@179
    13
# deal in the Software without restriction, including without limitation the
nkeynes@179
    14
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
nkeynes@179
    15
# sell copies of the Software, and to permit persons to whom the Software is
nkeynes@179
    16
# furnished to do so, subject to the following conditions:
nkeynes@179
    17
#
nkeynes@179
    18
# The above copyright notice and this permission notice shall be included in
nkeynes@179
    19
# all copies or substantial portions of the Software.
nkeynes@179
    20
#
nkeynes@179
    21
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
nkeynes@179
    22
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
nkeynes@179
    23
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
nkeynes@179
    24
# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
nkeynes@179
    25
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
nkeynes@179
    26
# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
nkeynes@179
    27
#
nkeynes@179
    28
# Except as contained in this notice, the name of the X Consortium shall not
nkeynes@179
    29
# be used in advertising or otherwise to promote the sale, use or other deal-
nkeynes@179
    30
# ings in this Software without prior written authorization from the X Consor-
nkeynes@179
    31
# tium.
nkeynes@179
    32
#
nkeynes@179
    33
#
nkeynes@179
    34
# FSF changes to this file are in the public domain.
nkeynes@179
    35
#
nkeynes@179
    36
# Calling this script install-sh is preferred over install.sh, to prevent
nkeynes@179
    37
# `make' implicit rules from creating a file called install from it
nkeynes@179
    38
# when there is no Makefile.
nkeynes@179
    39
#
nkeynes@179
    40
# This script is compatible with the BSD install script, but was written
nkeynes@179
    41
# from scratch.  It can only install one file at a time, a restriction
nkeynes@179
    42
# shared with many OS's install programs.
nkeynes@179
    43
nkeynes@179
    44
nkeynes@179
    45
# set DOITPROG to echo to test this script
nkeynes@179
    46
nkeynes@179
    47
# Don't use :- since 4.3BSD and earlier shells don't like it.
nkeynes@179
    48
doit="${DOITPROG-}"
nkeynes@179
    49
nkeynes@179
    50
nkeynes@179
    51
# put in absolute paths if you don't have them in your path; or use env. vars.
nkeynes@179
    52
nkeynes@179
    53
mvprog="${MVPROG-mv}"
nkeynes@179
    54
cpprog="${CPPROG-cp}"
nkeynes@179
    55
chmodprog="${CHMODPROG-chmod}"
nkeynes@179
    56
chownprog="${CHOWNPROG-chown}"
nkeynes@179
    57
chgrpprog="${CHGRPPROG-chgrp}"
nkeynes@179
    58
stripprog="${STRIPPROG-strip}"
nkeynes@179
    59
rmprog="${RMPROG-rm}"
nkeynes@179
    60
mkdirprog="${MKDIRPROG-mkdir}"
nkeynes@179
    61
nkeynes@179
    62
transformbasename=""
nkeynes@179
    63
transform_arg=""
nkeynes@179
    64
instcmd="$mvprog"
nkeynes@179
    65
chmodcmd="$chmodprog 0755"
nkeynes@179
    66
chowncmd=""
nkeynes@179
    67
chgrpcmd=""
nkeynes@179
    68
stripcmd=""
nkeynes@179
    69
rmcmd="$rmprog -f"
nkeynes@179
    70
mvcmd="$mvprog"
nkeynes@179
    71
src=""
nkeynes@179
    72
dst=""
nkeynes@179
    73
dir_arg=""
nkeynes@179
    74
nkeynes@179
    75
while [ x"$1" != x ]; do
nkeynes@179
    76
    case $1 in
nkeynes@179
    77
	-c) instcmd=$cpprog
nkeynes@179
    78
	    shift
nkeynes@179
    79
	    continue;;
nkeynes@179
    80
nkeynes@179
    81
	-d) dir_arg=true
nkeynes@179
    82
	    shift
nkeynes@179
    83
	    continue;;
nkeynes@179
    84
nkeynes@179
    85
	-m) chmodcmd="$chmodprog $2"
nkeynes@179
    86
	    shift
nkeynes@179
    87
	    shift
nkeynes@179
    88
	    continue;;
nkeynes@179
    89
nkeynes@179
    90
	-o) chowncmd="$chownprog $2"
nkeynes@179
    91
	    shift
nkeynes@179
    92
	    shift
nkeynes@179
    93
	    continue;;
nkeynes@179
    94
nkeynes@179
    95
	-g) chgrpcmd="$chgrpprog $2"
nkeynes@179
    96
	    shift
nkeynes@179
    97
	    shift
nkeynes@179
    98
	    continue;;
nkeynes@179
    99
nkeynes@179
   100
	-s) stripcmd=$stripprog
nkeynes@179
   101
	    shift
nkeynes@179
   102
	    continue;;
nkeynes@179
   103
nkeynes@179
   104
	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
nkeynes@179
   105
	    shift
nkeynes@179
   106
	    continue;;
nkeynes@179
   107
nkeynes@179
   108
	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
nkeynes@179
   109
	    shift
nkeynes@179
   110
	    continue;;
nkeynes@179
   111
nkeynes@179
   112
	*)  if [ x"$src" = x ]
nkeynes@179
   113
	    then
nkeynes@179
   114
		src=$1
nkeynes@179
   115
	    else
nkeynes@179
   116
		# this colon is to work around a 386BSD /bin/sh bug
nkeynes@179
   117
		:
nkeynes@179
   118
		dst=$1
nkeynes@179
   119
	    fi
nkeynes@179
   120
	    shift
nkeynes@179
   121
	    continue;;
nkeynes@179
   122
    esac
nkeynes@179
   123
done
nkeynes@179
   124
nkeynes@179
   125
if [ x"$src" = x ]
nkeynes@179
   126
then
nkeynes@179
   127
	echo "$0: no input file specified" >&2
nkeynes@179
   128
	exit 1
nkeynes@179
   129
else
nkeynes@179
   130
	:
nkeynes@179
   131
fi
nkeynes@179
   132
nkeynes@179
   133
if [ x"$dir_arg" != x ]; then
nkeynes@179
   134
	dst=$src
nkeynes@179
   135
	src=""
nkeynes@179
   136
nkeynes@179
   137
	if [ -d "$dst" ]; then
nkeynes@179
   138
		instcmd=:
nkeynes@179
   139
		chmodcmd=""
nkeynes@179
   140
	else
nkeynes@179
   141
		instcmd=$mkdirprog
nkeynes@179
   142
	fi
nkeynes@179
   143
else
nkeynes@179
   144
nkeynes@179
   145
# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
nkeynes@179
   146
# might cause directories to be created, which would be especially bad
nkeynes@179
   147
# if $src (and thus $dsttmp) contains '*'.
nkeynes@179
   148
nkeynes@179
   149
	if [ -f "$src" ] || [ -d "$src" ]
nkeynes@179
   150
	then
nkeynes@179
   151
		:
nkeynes@179
   152
	else
nkeynes@179
   153
		echo "$0: $src does not exist" >&2
nkeynes@179
   154
		exit 1
nkeynes@179
   155
	fi
nkeynes@179
   156
nkeynes@179
   157
	if [ x"$dst" = x ]
nkeynes@179
   158
	then
nkeynes@179
   159
		echo "$0: no destination specified" >&2
nkeynes@179
   160
		exit 1
nkeynes@179
   161
	else
nkeynes@179
   162
		:
nkeynes@179
   163
	fi
nkeynes@179
   164
nkeynes@179
   165
# If destination is a directory, append the input filename; if your system
nkeynes@179
   166
# does not like double slashes in filenames, you may need to add some logic
nkeynes@179
   167
nkeynes@179
   168
	if [ -d "$dst" ]
nkeynes@179
   169
	then
nkeynes@179
   170
		dst=$dst/`basename "$src"`
nkeynes@179
   171
	else
nkeynes@179
   172
		:
nkeynes@179
   173
	fi
nkeynes@179
   174
fi
nkeynes@179
   175
nkeynes@179
   176
## this sed command emulates the dirname command
nkeynes@179
   177
dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
nkeynes@179
   178
nkeynes@179
   179
# Make sure that the destination directory exists.
nkeynes@179
   180
#  this part is taken from Noah Friedman's mkinstalldirs script
nkeynes@179
   181
nkeynes@179
   182
# Skip lots of stat calls in the usual case.
nkeynes@179
   183
if [ ! -d "$dstdir" ]; then
nkeynes@179
   184
defaultIFS='
nkeynes@179
   185
	'
nkeynes@179
   186
IFS="${IFS-$defaultIFS}"
nkeynes@179
   187
nkeynes@179
   188
oIFS=$IFS
nkeynes@179
   189
# Some sh's can't handle IFS=/ for some reason.
nkeynes@179
   190
IFS='%'
nkeynes@179
   191
set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
nkeynes@179
   192
IFS=$oIFS
nkeynes@179
   193
nkeynes@179
   194
pathcomp=''
nkeynes@179
   195
nkeynes@179
   196
while [ $# -ne 0 ] ; do
nkeynes@179
   197
	pathcomp=$pathcomp$1
nkeynes@179
   198
	shift
nkeynes@179
   199
nkeynes@179
   200
	if [ ! -d "$pathcomp" ] ;
nkeynes@179
   201
        then
nkeynes@179
   202
		$mkdirprog "$pathcomp"
nkeynes@179
   203
	else
nkeynes@179
   204
		:
nkeynes@179
   205
	fi
nkeynes@179
   206
nkeynes@179
   207
	pathcomp=$pathcomp/
nkeynes@179
   208
done
nkeynes@179
   209
fi
nkeynes@179
   210
nkeynes@179
   211
if [ x"$dir_arg" != x ]
nkeynes@179
   212
then
nkeynes@179
   213
	$doit $instcmd "$dst" &&
nkeynes@179
   214
nkeynes@179
   215
	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi &&
nkeynes@179
   216
	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi &&
nkeynes@179
   217
	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi &&
nkeynes@179
   218
	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi
nkeynes@179
   219
else
nkeynes@179
   220
nkeynes@179
   221
# If we're going to rename the final executable, determine the name now.
nkeynes@179
   222
nkeynes@179
   223
	if [ x"$transformarg" = x ]
nkeynes@179
   224
	then
nkeynes@179
   225
		dstfile=`basename "$dst"`
nkeynes@179
   226
	else
nkeynes@179
   227
		dstfile=`basename "$dst" $transformbasename |
nkeynes@179
   228
			sed $transformarg`$transformbasename
nkeynes@179
   229
	fi
nkeynes@179
   230
nkeynes@179
   231
# don't allow the sed command to completely eliminate the filename
nkeynes@179
   232
nkeynes@179
   233
	if [ x"$dstfile" = x ]
nkeynes@179
   234
	then
nkeynes@179
   235
		dstfile=`basename "$dst"`
nkeynes@179
   236
	else
nkeynes@179
   237
		:
nkeynes@179
   238
	fi
nkeynes@179
   239
nkeynes@179
   240
# Make a couple of temp file names in the proper directory.
nkeynes@179
   241
nkeynes@179
   242
	dsttmp=$dstdir/_inst.$$_
nkeynes@179
   243
	rmtmp=$dstdir/_rm.$$_
nkeynes@179
   244
nkeynes@179
   245
# Trap to clean up temp files at exit.
nkeynes@179
   246
nkeynes@179
   247
	trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
nkeynes@179
   248
	trap '(exit $?); exit' 1 2 13 15
nkeynes@179
   249
nkeynes@179
   250
# Move or copy the file name to the temp name
nkeynes@179
   251
nkeynes@179
   252
	$doit $instcmd "$src" "$dsttmp" &&
nkeynes@179
   253
nkeynes@179
   254
# and set any options; do chmod last to preserve setuid bits
nkeynes@179
   255
nkeynes@179
   256
# If any of these fail, we abort the whole thing.  If we want to
nkeynes@179
   257
# ignore errors from any of these, just make sure not to ignore
nkeynes@179
   258
# errors from the above "$doit $instcmd $src $dsttmp" command.
nkeynes@179
   259
nkeynes@179
   260
	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi &&
nkeynes@179
   261
	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi &&
nkeynes@179
   262
	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi &&
nkeynes@179
   263
	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi &&
nkeynes@179
   264
nkeynes@179
   265
# Now remove or move aside any old file at destination location.  We try this
nkeynes@179
   266
# two ways since rm can't unlink itself on some systems and the destination
nkeynes@179
   267
# file might be busy for other reasons.  In this case, the final cleanup
nkeynes@179
   268
# might fail but the new file should still install successfully.
nkeynes@179
   269
nkeynes@179
   270
{
nkeynes@179
   271
	if [ -f "$dstdir/$dstfile" ]
nkeynes@179
   272
	then
nkeynes@179
   273
		$doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null ||
nkeynes@179
   274
		$doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null ||
nkeynes@179
   275
		{
nkeynes@179
   276
		  echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
nkeynes@179
   277
		  (exit 1); exit
nkeynes@179
   278
		}
nkeynes@179
   279
	else
nkeynes@179
   280
		:
nkeynes@179
   281
	fi
nkeynes@179
   282
} &&
nkeynes@179
   283
nkeynes@179
   284
# Now rename the file to the real destination.
nkeynes@179
   285
nkeynes@179
   286
	$doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
nkeynes@179
   287
nkeynes@179
   288
fi &&
nkeynes@179
   289
nkeynes@179
   290
# The final little trick to "correctly" pass the exit status to the exit trap.
nkeynes@179
   291
nkeynes@179
   292
{
nkeynes@179
   293
	(exit 0); exit
nkeynes@179
   294
}
.