install-sh revision 16910e25
116910e25Smrg#!/bin/sh
216910e25Smrg#
316910e25Smrg# install - install a program, script, or datafile
416910e25Smrg#
516910e25Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
616910e25Smrg# later released in X11R6 (xc/config/util/install.sh) with the
716910e25Smrg# following copyright and license.
816910e25Smrg#
916910e25Smrg# Copyright (C) 1994 X Consortium
1016910e25Smrg#
1116910e25Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1216910e25Smrg# of this software and associated documentation files (the "Software"), to
1316910e25Smrg# deal in the Software without restriction, including without limitation the
1416910e25Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1516910e25Smrg# sell copies of the Software, and to permit persons to whom the Software is
1616910e25Smrg# furnished to do so, subject to the following conditions:
1716910e25Smrg#
1816910e25Smrg# The above copyright notice and this permission notice shall be included in
1916910e25Smrg# all copies or substantial portions of the Software.
2016910e25Smrg#
2116910e25Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2216910e25Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2316910e25Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2416910e25Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2516910e25Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2616910e25Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2716910e25Smrg#
2816910e25Smrg# Except as contained in this notice, the name of the X Consortium shall not
2916910e25Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3016910e25Smrg# ings in this Software without prior written authorization from the X Consor-
3116910e25Smrg# tium.
3216910e25Smrg#
3316910e25Smrg#
3416910e25Smrg# FSF changes to this file are in the public domain.
3516910e25Smrg#
3616910e25Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3716910e25Smrg# `make' implicit rules from creating a file called install from it
3816910e25Smrg# when there is no Makefile.
3916910e25Smrg#
4016910e25Smrg# This script is compatible with the BSD install script, but was written
4116910e25Smrg# from scratch.  It can only install one file at a time, a restriction
4216910e25Smrg# shared with many OS's install programs.
4316910e25Smrg
4416910e25Smrg
4516910e25Smrg# set DOITPROG to echo to test this script
4616910e25Smrg
4716910e25Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
4816910e25Smrgdoit="${DOITPROG-}"
4916910e25Smrg
5016910e25Smrg
5116910e25Smrg# put in absolute paths if you don't have them in your path; or use env. vars.
5216910e25Smrg
5316910e25Smrgmvprog="${MVPROG-mv}"
5416910e25Smrgcpprog="${CPPROG-cp}"
5516910e25Smrgchmodprog="${CHMODPROG-chmod}"
5616910e25Smrgchownprog="${CHOWNPROG-chown}"
5716910e25Smrgchgrpprog="${CHGRPPROG-chgrp}"
5816910e25Smrgstripprog="${STRIPPROG-strip}"
5916910e25Smrgrmprog="${RMPROG-rm}"
6016910e25Smrgmkdirprog="${MKDIRPROG-mkdir}"
6116910e25Smrg
6216910e25Smrgtransformbasename=""
6316910e25Smrgtransform_arg=""
6416910e25Smrginstcmd="$mvprog"
6516910e25Smrgchmodcmd="$chmodprog 0755"
6616910e25Smrgchowncmd=""
6716910e25Smrgchgrpcmd=""
6816910e25Smrgstripcmd=""
6916910e25Smrgrmcmd="$rmprog -f"
7016910e25Smrgmvcmd="$mvprog"
7116910e25Smrgsrc=""
7216910e25Smrgdst=""
7316910e25Smrgdir_arg=""
7416910e25Smrg
7516910e25Smrgwhile [ x"$1" != x ]; do
7616910e25Smrg    case $1 in
7716910e25Smrg	-c) instcmd=$cpprog
7816910e25Smrg	    shift
7916910e25Smrg	    continue;;
8016910e25Smrg
8116910e25Smrg	-d) dir_arg=true
8216910e25Smrg	    shift
8316910e25Smrg	    continue;;
8416910e25Smrg
8516910e25Smrg	-m) chmodcmd="$chmodprog $2"
8616910e25Smrg	    shift
8716910e25Smrg	    shift
8816910e25Smrg	    continue;;
8916910e25Smrg
9016910e25Smrg	-o) chowncmd="$chownprog $2"
9116910e25Smrg	    shift
9216910e25Smrg	    shift
9316910e25Smrg	    continue;;
9416910e25Smrg
9516910e25Smrg	-g) chgrpcmd="$chgrpprog $2"
9616910e25Smrg	    shift
9716910e25Smrg	    shift
9816910e25Smrg	    continue;;
9916910e25Smrg
10016910e25Smrg	-s) stripcmd=$stripprog
10116910e25Smrg	    shift
10216910e25Smrg	    continue;;
10316910e25Smrg
10416910e25Smrg	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
10516910e25Smrg	    shift
10616910e25Smrg	    continue;;
10716910e25Smrg
10816910e25Smrg	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
10916910e25Smrg	    shift
11016910e25Smrg	    continue;;
11116910e25Smrg
11216910e25Smrg	*)  if [ x"$src" = x ]
11316910e25Smrg	    then
11416910e25Smrg		src=$1
11516910e25Smrg	    else
11616910e25Smrg		# this colon is to work around a 386BSD /bin/sh bug
11716910e25Smrg		:
11816910e25Smrg		dst=$1
11916910e25Smrg	    fi
12016910e25Smrg	    shift
12116910e25Smrg	    continue;;
12216910e25Smrg    esac
12316910e25Smrgdone
12416910e25Smrg
12516910e25Smrgif [ x"$src" = x ]
12616910e25Smrgthen
12716910e25Smrg	echo "$0: no input file specified" >&2
12816910e25Smrg	exit 1
12916910e25Smrgelse
13016910e25Smrg	:
13116910e25Smrgfi
13216910e25Smrg
13316910e25Smrgif [ x"$dir_arg" != x ]; then
13416910e25Smrg	dst=$src
13516910e25Smrg	src=""
13616910e25Smrg
13716910e25Smrg	if [ -d "$dst" ]; then
13816910e25Smrg		instcmd=:
13916910e25Smrg		chmodcmd=""
14016910e25Smrg	else
14116910e25Smrg		instcmd=$mkdirprog
14216910e25Smrg	fi
14316910e25Smrgelse
14416910e25Smrg
14516910e25Smrg# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
14616910e25Smrg# might cause directories to be created, which would be especially bad
14716910e25Smrg# if $src (and thus $dsttmp) contains '*'.
14816910e25Smrg
14916910e25Smrg	if [ -f "$src" ] || [ -d "$src" ]
15016910e25Smrg	then
15116910e25Smrg		:
15216910e25Smrg	else
15316910e25Smrg		echo "$0: $src does not exist" >&2
15416910e25Smrg		exit 1
15516910e25Smrg	fi
15616910e25Smrg
15716910e25Smrg	if [ x"$dst" = x ]
15816910e25Smrg	then
15916910e25Smrg		echo "$0: no destination specified" >&2
16016910e25Smrg		exit 1
16116910e25Smrg	else
16216910e25Smrg		:
16316910e25Smrg	fi
16416910e25Smrg
16516910e25Smrg# If destination is a directory, append the input filename; if your system
16616910e25Smrg# does not like double slashes in filenames, you may need to add some logic
16716910e25Smrg
16816910e25Smrg	if [ -d "$dst" ]
16916910e25Smrg	then
17016910e25Smrg		dst=$dst/`basename "$src"`
17116910e25Smrg	else
17216910e25Smrg		:
17316910e25Smrg	fi
17416910e25Smrgfi
17516910e25Smrg
17616910e25Smrg## this sed command emulates the dirname command
17716910e25Smrgdstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
17816910e25Smrg
17916910e25Smrg# Make sure that the destination directory exists.
18016910e25Smrg#  this part is taken from Noah Friedman's mkinstalldirs script
18116910e25Smrg
18216910e25Smrg# Skip lots of stat calls in the usual case.
18316910e25Smrgif [ ! -d "$dstdir" ]; then
18416910e25SmrgdefaultIFS='
18516910e25Smrg	'
18616910e25SmrgIFS="${IFS-$defaultIFS}"
18716910e25Smrg
18816910e25SmrgoIFS=$IFS
18916910e25Smrg# Some sh's can't handle IFS=/ for some reason.
19016910e25SmrgIFS='%'
19116910e25Smrgset - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
19216910e25SmrgIFS=$oIFS
19316910e25Smrg
19416910e25Smrgpathcomp=''
19516910e25Smrg
19616910e25Smrgwhile [ $# -ne 0 ] ; do
19716910e25Smrg	pathcomp=$pathcomp$1
19816910e25Smrg	shift
19916910e25Smrg
20016910e25Smrg	if [ ! -d "$pathcomp" ] ;
20116910e25Smrg        then
20216910e25Smrg		$mkdirprog "$pathcomp"
20316910e25Smrg	else
20416910e25Smrg		:
20516910e25Smrg	fi
20616910e25Smrg
20716910e25Smrg	pathcomp=$pathcomp/
20816910e25Smrgdone
20916910e25Smrgfi
21016910e25Smrg
21116910e25Smrgif [ x"$dir_arg" != x ]
21216910e25Smrgthen
21316910e25Smrg	$doit $instcmd "$dst" &&
21416910e25Smrg
21516910e25Smrg	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi &&
21616910e25Smrg	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi &&
21716910e25Smrg	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi &&
21816910e25Smrg	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi
21916910e25Smrgelse
22016910e25Smrg
22116910e25Smrg# If we're going to rename the final executable, determine the name now.
22216910e25Smrg
22316910e25Smrg	if [ x"$transformarg" = x ]
22416910e25Smrg	then
22516910e25Smrg		dstfile=`basename "$dst"`
22616910e25Smrg	else
22716910e25Smrg		dstfile=`basename "$dst" $transformbasename |
22816910e25Smrg			sed $transformarg`$transformbasename
22916910e25Smrg	fi
23016910e25Smrg
23116910e25Smrg# don't allow the sed command to completely eliminate the filename
23216910e25Smrg
23316910e25Smrg	if [ x"$dstfile" = x ]
23416910e25Smrg	then
23516910e25Smrg		dstfile=`basename "$dst"`
23616910e25Smrg	else
23716910e25Smrg		:
23816910e25Smrg	fi
23916910e25Smrg
24016910e25Smrg# Make a couple of temp file names in the proper directory.
24116910e25Smrg
24216910e25Smrg	dsttmp=$dstdir/_inst.$$_
24316910e25Smrg	rmtmp=$dstdir/_rm.$$_
24416910e25Smrg
24516910e25Smrg# Trap to clean up temp files at exit.
24616910e25Smrg
24716910e25Smrg	trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
24816910e25Smrg	trap '(exit $?); exit' 1 2 13 15
24916910e25Smrg
25016910e25Smrg# Move or copy the file name to the temp name
25116910e25Smrg
25216910e25Smrg	$doit $instcmd "$src" "$dsttmp" &&
25316910e25Smrg
25416910e25Smrg# and set any options; do chmod last to preserve setuid bits
25516910e25Smrg
25616910e25Smrg# If any of these fail, we abort the whole thing.  If we want to
25716910e25Smrg# ignore errors from any of these, just make sure not to ignore
25816910e25Smrg# errors from the above "$doit $instcmd $src $dsttmp" command.
25916910e25Smrg
26016910e25Smrg	if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi &&
26116910e25Smrg	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi &&
26216910e25Smrg	if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi &&
26316910e25Smrg	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi &&
26416910e25Smrg
26516910e25Smrg# Now remove or move aside any old file at destination location.  We try this
26616910e25Smrg# two ways since rm can't unlink itself on some systems and the destination
26716910e25Smrg# file might be busy for other reasons.  In this case, the final cleanup
26816910e25Smrg# might fail but the new file should still install successfully.
26916910e25Smrg
27016910e25Smrg{
27116910e25Smrg	if [ -f "$dstdir/$dstfile" ]
27216910e25Smrg	then
27316910e25Smrg		$doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null ||
27416910e25Smrg		$doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null ||
27516910e25Smrg		{
27616910e25Smrg		  echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
27716910e25Smrg		  (exit 1); exit
27816910e25Smrg		}
27916910e25Smrg	else
28016910e25Smrg		:
28116910e25Smrg	fi
28216910e25Smrg} &&
28316910e25Smrg
28416910e25Smrg# Now rename the file to the real destination.
28516910e25Smrg
28616910e25Smrg	$doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
28716910e25Smrg
28816910e25Smrgfi &&
28916910e25Smrg
29016910e25Smrg# The final little trick to "correctly" pass the exit status to the exit trap.
29116910e25Smrg
29216910e25Smrg{
29316910e25Smrg	(exit 0); exit
29416910e25Smrg}
295