install.sh revision 26497
126497Sache#!/bin/sh
226497Sache#
326497Sache# install - install a program, script, or datafile
426497Sache# This comes from X11R5.
526497Sache#
626497Sache# $XConsortium: install.sh,v 1.2 89/12/18 14:47:22 jim Exp $
726497Sache#
826497Sache# This script is compatible with the BSD install script, but was written
926497Sache# from scratch.
1026497Sache#
1126497Sache
1226497Sache# set DOITPROG to echo to test this script
1326497Sache
1426497Sache# Don't use :- since 4.3BSD and earlier shells don't like it.
1526497Sachedoit="${DOITPROG-}"
1626497Sache
1726497Sache
1826497Sache# put in absolute paths if you don't have them in your path; or use env. vars.
1926497Sache
2026497Sachemvprog="${MVPROG-mv}"
2126497Sachecpprog="${CPPROG-cp}"
2226497Sachechmodprog="${CHMODPROG-chmod}"
2326497Sachechownprog="${CHOWNPROG-chown}"
2426497Sachechgrpprog="${CHGRPPROG-chgrp}"
2526497Sachestripprog="${STRIPPROG-strip}"
2626497Sachermprog="${RMPROG-rm}"
2726497Sachemkdirprog="${MKDIRPROG-mkdir}"
2826497Sache
2926497Sachetranformbasename=""
3026497Sachetransform_arg=""
3126497Sacheinstcmd="$mvprog"
3226497Sachechmodcmd="$chmodprog 0755"
3326497Sachechowncmd=""
3426497Sachechgrpcmd=""
3526497Sachestripcmd=""
3626497Sachermcmd="$rmprog -f"
3726497Sachemvcmd="$mvprog"
3826497Sachesrc=""
3926497Sachedst=""
4026497Sachedir_arg=""
4126497Sache
4226497Sachewhile [ x"$1" != x ]; do
4326497Sache    case $1 in
4426497Sache	-c) instcmd="$cpprog"
4526497Sache	    shift
4626497Sache	    continue;;
4726497Sache
4826497Sache	-d) dir_arg=true
4926497Sache	    shift
5026497Sache	    continue;;
5126497Sache
5226497Sache	-m) chmodcmd="$chmodprog $2"
5326497Sache	    shift
5426497Sache	    shift
5526497Sache	    continue;;
5626497Sache
5726497Sache	-o) chowncmd="$chownprog $2"
5826497Sache	    shift
5926497Sache	    shift
6026497Sache	    continue;;
6126497Sache
6226497Sache	-g) chgrpcmd="$chgrpprog $2"
6326497Sache	    shift
6426497Sache	    shift
6526497Sache	    continue;;
6626497Sache
6726497Sache	-s) stripcmd="$stripprog"
6826497Sache	    shift
6926497Sache	    continue;;
7026497Sache
7126497Sache	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
7226497Sache	    shift
7326497Sache	    continue;;
7426497Sache
7526497Sache	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
7626497Sache	    shift
7726497Sache	    continue;;
7826497Sache
7926497Sache	*)  if [ x"$src" = x ]
8026497Sache	    then
8126497Sache		src=$1
8226497Sache	    else
8326497Sache		# this colon is to work around a 386BSD /bin/sh bug
8426497Sache		:
8526497Sache		dst=$1
8626497Sache	    fi
8726497Sache	    shift
8826497Sache	    continue;;
8926497Sache    esac
9026497Sachedone
9126497Sache
9226497Sacheif [ x"$src" = x ]
9326497Sachethen
9426497Sache	echo "install:	no input file specified"
9526497Sache	exit 1
9626497Sacheelse
9726497Sache	true
9826497Sachefi
9926497Sache
10026497Sacheif [ x"$dir_arg" != x ]; then
10126497Sache	dst=$src
10226497Sache	src=""
10326497Sache	
10426497Sache	if [ -d $dst ]; then
10526497Sache		instcmd=:
10626497Sache	else
10726497Sache		instcmd=mkdir
10826497Sache	fi
10926497Sacheelse
11026497Sache
11126497Sache# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
11226497Sache# might cause directories to be created, which would be especially bad 
11326497Sache# if $src (and thus $dsttmp) contains '*'.
11426497Sache
11526497Sache	if [ -f $src -o -d $src ]
11626497Sache	then
11726497Sache		true
11826497Sache	else
11926497Sache		echo "install:  $src does not exist"
12026497Sache		exit 1
12126497Sache	fi
12226497Sache	
12326497Sache	if [ x"$dst" = x ]
12426497Sache	then
12526497Sache		echo "install:	no destination specified"
12626497Sache		exit 1
12726497Sache	else
12826497Sache		true
12926497Sache	fi
13026497Sache
13126497Sache# If destination is a directory, append the input filename; if your system
13226497Sache# does not like double slashes in filenames, you may need to add some logic
13326497Sache
13426497Sache	if [ -d $dst ]
13526497Sache	then
13626497Sache		dst="$dst"/`basename $src`
13726497Sache	else
13826497Sache		true
13926497Sache	fi
14026497Sachefi
14126497Sache
14226497Sache## this sed command emulates the dirname command
14326497Sachedstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
14426497Sache
14526497Sache# Make sure that the destination directory exists.
14626497Sache#  this part is taken from Noah Friedman's mkinstalldirs script
14726497Sache
14826497Sache# Skip lots of stat calls in the usual case.
14926497Sacheif [ ! -d "$dstdir" ]; then
15026497SachedefaultIFS='	
15126497Sache'
15226497SacheIFS="${IFS-${defaultIFS}}"
15326497Sache
15426497SacheoIFS="${IFS}"
15526497Sache# Some sh's can't handle IFS=/ for some reason.
15626497SacheIFS='%'
15726497Sacheset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
15826497SacheIFS="${oIFS}"
15926497Sache
16026497Sachepathcomp=''
16126497Sache
16226497Sachewhile [ $# -ne 0 ] ; do
16326497Sache	pathcomp="${pathcomp}${1}"
16426497Sache	shift
16526497Sache
16626497Sache	if [ ! -d "${pathcomp}" ] ;
16726497Sache        then
16826497Sache		$mkdirprog "${pathcomp}"
16926497Sache	else
17026497Sache		true
17126497Sache	fi
17226497Sache
17326497Sache	pathcomp="${pathcomp}/"
17426497Sachedone
17526497Sachefi
17626497Sache
17726497Sacheif [ x"$dir_arg" != x ]
17826497Sachethen
17926497Sache	$doit $instcmd $dst &&
18026497Sache
18126497Sache	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
18226497Sache	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
18326497Sache	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
18426497Sache	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
18526497Sacheelse
18626497Sache
18726497Sache# If we're going to rename the final executable, determine the name now.
18826497Sache
18926497Sache	if [ x"$transformarg" = x ] 
19026497Sache	then
19126497Sache		dstfile=`basename $dst`
19226497Sache	else
19326497Sache		dstfile=`basename $dst $transformbasename | 
19426497Sache			sed $transformarg`$transformbasename
19526497Sache	fi
19626497Sache
19726497Sache# don't allow the sed command to completely eliminate the filename
19826497Sache
19926497Sache	if [ x"$dstfile" = x ] 
20026497Sache	then
20126497Sache		dstfile=`basename $dst`
20226497Sache	else
20326497Sache		true
20426497Sache	fi
20526497Sache
20626497Sache# Make a temp file name in the proper directory.
20726497Sache
20826497Sache	dsttmp=$dstdir/#inst.$$#
20926497Sache
21026497Sache# Move or copy the file name to the temp name
21126497Sache
21226497Sache	$doit $instcmd $src $dsttmp &&
21326497Sache
21426497Sache	trap "rm -f ${dsttmp}" 0 &&
21526497Sache
21626497Sache# and set any options; do chmod last to preserve setuid bits
21726497Sache
21826497Sache# If any of these fail, we abort the whole thing.  If we want to
21926497Sache# ignore errors from any of these, just make sure not to ignore
22026497Sache# errors from the above "$doit $instcmd $src $dsttmp" command.
22126497Sache
22226497Sache	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
22326497Sache	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
22426497Sache	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
22526497Sache	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
22626497Sache
22726497Sache# Now rename the file to the real destination.
22826497Sache
22926497Sache	$doit $rmcmd -f $dstdir/$dstfile &&
23026497Sache	$doit $mvcmd $dsttmp $dstdir/$dstfile 
23126497Sache
23226497Sachefi &&
23326497Sache
23426497Sache
23526497Sacheexit 0
236