install-sh revision 55682
155682Smarkm#! /bin/sh
255682Smarkm#
355682Smarkm# install - install a program, script, or datafile
455682Smarkm# This comes from X11R5.
555682Smarkm#
655682Smarkm# Calling this script install-sh is preferred over install.sh, to prevent
755682Smarkm# `make' implicit rules from creating a file called install from it
855682Smarkm# when there is no Makefile.
955682Smarkm#
1055682Smarkm# This script is compatible with the BSD install script, but was written
1155682Smarkm# from scratch.
1255682Smarkm#
1355682Smarkm
1455682Smarkm
1555682Smarkm# set DOITPROG to echo to test this script
1655682Smarkm
1755682Smarkm# Don't use :- since 4.3BSD and earlier shells don't like it.
1855682Smarkmdoit="${DOITPROG-}"
1955682Smarkm
2055682Smarkm
2155682Smarkm# put in absolute paths if you don't have them in your path; or use env. vars.
2255682Smarkm
2355682Smarkmmvprog="${MVPROG-mv}"
2455682Smarkmcpprog="${CPPROG-cp}"
2555682Smarkmchmodprog="${CHMODPROG-chmod}"
2655682Smarkmchownprog="${CHOWNPROG-chown}"
2755682Smarkmchgrpprog="${CHGRPPROG-chgrp}"
2855682Smarkmstripprog="${STRIPPROG-strip}"
2955682Smarkmrmprog="${RMPROG-rm}"
3055682Smarkmmkdirprog="${MKDIRPROG-mkdir}"
3155682Smarkm
3255682Smarkmtranformbasename=""
3355682Smarkmtransform_arg=""
3455682Smarkminstcmd="$mvprog"
3555682Smarkmchmodcmd="$chmodprog 0755"
3655682Smarkmchowncmd=""
3755682Smarkmchgrpcmd=""
3855682Smarkmstripcmd=""
3955682Smarkmrmcmd="$rmprog -f"
4055682Smarkmmvcmd="$mvprog"
4155682Smarkmsrc=""
4255682Smarkmdst=""
4355682Smarkmdir_arg=""
4455682Smarkm
4555682Smarkmwhile [ x"$1" != x ]; do
4655682Smarkm    case $1 in
4755682Smarkm	-c) instcmd="$cpprog"
4855682Smarkm	    shift
4955682Smarkm	    continue;;
5055682Smarkm
5155682Smarkm	-d) dir_arg=true
5255682Smarkm	    shift
5355682Smarkm	    continue;;
5455682Smarkm
5555682Smarkm	-m) chmodcmd="$chmodprog $2"
5655682Smarkm	    shift
5755682Smarkm	    shift
5855682Smarkm	    continue;;
5955682Smarkm
6055682Smarkm	-o) chowncmd="$chownprog $2"
6155682Smarkm	    shift
6255682Smarkm	    shift
6355682Smarkm	    continue;;
6455682Smarkm
6555682Smarkm	-g) chgrpcmd="$chgrpprog $2"
6655682Smarkm	    shift
6755682Smarkm	    shift
6855682Smarkm	    continue;;
6955682Smarkm
7055682Smarkm	-s) stripcmd="$stripprog"
7155682Smarkm	    shift
7255682Smarkm	    continue;;
7355682Smarkm
7455682Smarkm	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
7555682Smarkm	    shift
7655682Smarkm	    continue;;
7755682Smarkm
7855682Smarkm	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
7955682Smarkm	    shift
8055682Smarkm	    continue;;
8155682Smarkm
8255682Smarkm	*)  if [ x"$src" = x ]
8355682Smarkm	    then
8455682Smarkm		src=$1
8555682Smarkm	    else
8655682Smarkm		# this colon is to work around a 386BSD /bin/sh bug
8755682Smarkm		:
8855682Smarkm		dst=$1
8955682Smarkm	    fi
9055682Smarkm	    shift
9155682Smarkm	    continue;;
9255682Smarkm    esac
9355682Smarkmdone
9455682Smarkm
9555682Smarkmif [ x"$src" = x ]
9655682Smarkmthen
9755682Smarkm	echo "install:	no input file specified"
9855682Smarkm	exit 1
9955682Smarkmelse
10055682Smarkm	true
10155682Smarkmfi
10255682Smarkm
10355682Smarkmif [ x"$dir_arg" != x ]; then
10455682Smarkm	dst=$src
10555682Smarkm	src=""
10655682Smarkm	
10755682Smarkm	if [ -d $dst ]; then
10855682Smarkm		instcmd=:
10955682Smarkm	else
11055682Smarkm		instcmd=mkdir
11155682Smarkm	fi
11255682Smarkmelse
11355682Smarkm
11455682Smarkm# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
11555682Smarkm# might cause directories to be created, which would be especially bad 
11655682Smarkm# if $src (and thus $dsttmp) contains '*'.
11755682Smarkm
11855682Smarkm	if [ -f $src -o -d $src ]
11955682Smarkm	then
12055682Smarkm		true
12155682Smarkm	else
12255682Smarkm		echo "install:  $src does not exist"
12355682Smarkm		exit 1
12455682Smarkm	fi
12555682Smarkm	
12655682Smarkm	if [ x"$dst" = x ]
12755682Smarkm	then
12855682Smarkm		echo "install:	no destination specified"
12955682Smarkm		exit 1
13055682Smarkm	else
13155682Smarkm		true
13255682Smarkm	fi
13355682Smarkm
13455682Smarkm# If destination is a directory, append the input filename; if your system
13555682Smarkm# does not like double slashes in filenames, you may need to add some logic
13655682Smarkm
13755682Smarkm	if [ -d $dst ]
13855682Smarkm	then
13955682Smarkm		dst="$dst"/`basename $src`
14055682Smarkm	else
14155682Smarkm		true
14255682Smarkm	fi
14355682Smarkmfi
14455682Smarkm
14555682Smarkm## this sed command emulates the dirname command
14655682Smarkmdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
14755682Smarkm
14855682Smarkm# Make sure that the destination directory exists.
14955682Smarkm#  this part is taken from Noah Friedman's mkinstalldirs script
15055682Smarkm
15155682Smarkm# Skip lots of stat calls in the usual case.
15255682Smarkmif [ ! -d "$dstdir" ]; then
15355682SmarkmdefaultIFS='	
15455682Smarkm'
15555682SmarkmIFS="${IFS-${defaultIFS}}"
15655682Smarkm
15755682SmarkmoIFS="${IFS}"
15855682Smarkm# Some sh's can't handle IFS=/ for some reason.
15955682SmarkmIFS='%'
16055682Smarkmset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
16155682SmarkmIFS="${oIFS}"
16255682Smarkm
16355682Smarkmpathcomp=''
16455682Smarkm
16555682Smarkmwhile [ $# -ne 0 ] ; do
16655682Smarkm	pathcomp="${pathcomp}${1}"
16755682Smarkm	shift
16855682Smarkm
16955682Smarkm	if [ ! -d "${pathcomp}" ] ;
17055682Smarkm        then
17155682Smarkm		$mkdirprog "${pathcomp}"
17255682Smarkm	else
17355682Smarkm		true
17455682Smarkm	fi
17555682Smarkm
17655682Smarkm	pathcomp="${pathcomp}/"
17755682Smarkmdone
17855682Smarkmfi
17955682Smarkm
18055682Smarkmif [ x"$dir_arg" != x ]
18155682Smarkmthen
18255682Smarkm	$doit $instcmd $dst &&
18355682Smarkm
18455682Smarkm	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
18555682Smarkm	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
18655682Smarkm	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
18755682Smarkm	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
18855682Smarkmelse
18955682Smarkm
19055682Smarkm# If we're going to rename the final executable, determine the name now.
19155682Smarkm
19255682Smarkm	if [ x"$transformarg" = x ] 
19355682Smarkm	then
19455682Smarkm		dstfile=`basename $dst`
19555682Smarkm	else
19655682Smarkm		dstfile=`basename $dst $transformbasename | 
19755682Smarkm			sed $transformarg`$transformbasename
19855682Smarkm	fi
19955682Smarkm
20055682Smarkm# don't allow the sed command to completely eliminate the filename
20155682Smarkm
20255682Smarkm	if [ x"$dstfile" = x ] 
20355682Smarkm	then
20455682Smarkm		dstfile=`basename $dst`
20555682Smarkm	else
20655682Smarkm		true
20755682Smarkm	fi
20855682Smarkm
20955682Smarkm# Make a temp file name in the proper directory.
21055682Smarkm
21155682Smarkm	dsttmp=$dstdir/#inst.$$#
21255682Smarkm
21355682Smarkm# Move or copy the file name to the temp name
21455682Smarkm
21555682Smarkm	$doit $instcmd $src $dsttmp &&
21655682Smarkm
21755682Smarkm	trap "rm -f ${dsttmp}" 0 &&
21855682Smarkm
21955682Smarkm# and set any options; do chmod last to preserve setuid bits
22055682Smarkm
22155682Smarkm# If any of these fail, we abort the whole thing.  If we want to
22255682Smarkm# ignore errors from any of these, just make sure not to ignore
22355682Smarkm# errors from the above "$doit $instcmd $src $dsttmp" command.
22455682Smarkm
22555682Smarkm	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
22655682Smarkm	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
22755682Smarkm	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
22855682Smarkm	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
22955682Smarkm
23055682Smarkm# Now rename the file to the real destination.
23155682Smarkm
23255682Smarkm	$doit $rmcmd -f $dstdir/$dstfile &&
23355682Smarkm	$doit $mvcmd $dsttmp $dstdir/$dstfile 
23455682Smarkm
23555682Smarkmfi &&
23655682Smarkm
23755682Smarkm
23855682Smarkmexit 0
239