122347Spst#! /bin/sh
222347Spst#
322347Spst# install - install a program, script, or datafile
422347Spst# This comes from X11R5.
522347Spst#
622347Spst# Calling this script install-sh is preferred over install.sh, to prevent
722347Spst# `make' implicit rules from creating a file called install from it
822347Spst# when there is no Makefile.
922347Spst#
1022347Spst# This script is compatible with the BSD install script, but was written
1122347Spst# from scratch.
1222347Spst#
1322347Spst
1422347Spst
1522347Spst# set DOITPROG to echo to test this script
1622347Spst
1722347Spst# Don't use :- since 4.3BSD and earlier shells don't like it.
1822347Spstdoit="${DOITPROG-}"
1922347Spst
2022347Spst
2122347Spst# put in absolute paths if you don't have them in your path; or use env. vars.
2222347Spst
2322347Spstmvprog="${MVPROG-mv}"
2422347Spstcpprog="${CPPROG-cp}"
2522347Spstchmodprog="${CHMODPROG-chmod}"
2622347Spstchownprog="${CHOWNPROG-chown}"
2722347Spstchgrpprog="${CHGRPPROG-chgrp}"
2822347Spststripprog="${STRIPPROG-strip}"
2922347Spstrmprog="${RMPROG-rm}"
3022347Spstmkdirprog="${MKDIRPROG-mkdir}"
3122347Spst
3222347Spsttranformbasename=""
3322347Spsttransform_arg=""
3422347Spstinstcmd="$mvprog"
3522347Spstchmodcmd="$chmodprog 0755"
3622347Spstchowncmd=""
3722347Spstchgrpcmd=""
3822347Spststripcmd=""
3922347Spstrmcmd="$rmprog -f"
4022347Spstmvcmd="$mvprog"
4122347Spstsrc=""
4222347Spstdst=""
4322347Spstdir_arg=""
4422347Spst
4522347Spstwhile [ x"$1" != x ]; do
4622347Spst    case $1 in
4722347Spst	-c) instcmd="$cpprog"
4822347Spst	    shift
4922347Spst	    continue;;
5022347Spst
5122347Spst	-d) dir_arg=true
5222347Spst	    shift
5322347Spst	    continue;;
5422347Spst
5522347Spst	-m) chmodcmd="$chmodprog $2"
5622347Spst	    shift
5722347Spst	    shift
5822347Spst	    continue;;
5922347Spst
6022347Spst	-o) chowncmd="$chownprog $2"
6122347Spst	    shift
6222347Spst	    shift
6322347Spst	    continue;;
6422347Spst
6522347Spst	-g) chgrpcmd="$chgrpprog $2"
6622347Spst	    shift
6722347Spst	    shift
6822347Spst	    continue;;
6922347Spst
7022347Spst	-s) stripcmd="$stripprog"
7122347Spst	    shift
7222347Spst	    continue;;
7322347Spst
7422347Spst	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
7522347Spst	    shift
7622347Spst	    continue;;
7722347Spst
7822347Spst	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
7922347Spst	    shift
8022347Spst	    continue;;
8122347Spst
8222347Spst	*)  if [ x"$src" = x ]
8322347Spst	    then
8422347Spst		src=$1
8522347Spst	    else
8622347Spst		# this colon is to work around a 386BSD /bin/sh bug
8722347Spst		:
8822347Spst		dst=$1
8922347Spst	    fi
9022347Spst	    shift
9122347Spst	    continue;;
9222347Spst    esac
9322347Spstdone
9422347Spst
9522347Spstif [ x"$src" = x ]
9622347Spstthen
9722347Spst	echo "install:	no input file specified"
9822347Spst	exit 1
9922347Spstelse
10022347Spst	true
10122347Spstfi
10222347Spst
10322347Spstif [ x"$dir_arg" != x ]; then
10422347Spst	dst=$src
10522347Spst	src=""
10622347Spst	
10722347Spst	if [ -d $dst ]; then
10822347Spst		instcmd=:
10922347Spst	else
11022347Spst		instcmd=mkdir
11122347Spst	fi
11222347Spstelse
11322347Spst
11422347Spst# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
11522347Spst# might cause directories to be created, which would be especially bad 
11622347Spst# if $src (and thus $dsttmp) contains '*'.
11722347Spst
11822347Spst	if [ -f $src -o -d $src ]
11922347Spst	then
12022347Spst		true
12122347Spst	else
12222347Spst		echo "install:  $src does not exist"
12322347Spst		exit 1
12422347Spst	fi
12522347Spst	
12622347Spst	if [ x"$dst" = x ]
12722347Spst	then
12822347Spst		echo "install:	no destination specified"
12922347Spst		exit 1
13022347Spst	else
13122347Spst		true
13222347Spst	fi
13322347Spst
13422347Spst# If destination is a directory, append the input filename; if your system
13522347Spst# does not like double slashes in filenames, you may need to add some logic
13622347Spst
13722347Spst	if [ -d $dst ]
13822347Spst	then
13922347Spst		dst="$dst"/`basename $src`
14022347Spst	else
14122347Spst		true
14222347Spst	fi
14322347Spstfi
14422347Spst
14522347Spst## this sed command emulates the dirname command
14622347Spstdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
14722347Spst
14822347Spst# Make sure that the destination directory exists.
14922347Spst#  this part is taken from Noah Friedman's mkinstalldirs script
15022347Spst
15122347Spst# Skip lots of stat calls in the usual case.
15222347Spstif [ ! -d "$dstdir" ]; then
15322347SpstdefaultIFS='	
15422347Spst'
15522347SpstIFS="${IFS-${defaultIFS}}"
15622347Spst
15722347SpstoIFS="${IFS}"
15822347Spst# Some sh's can't handle IFS=/ for some reason.
15922347SpstIFS='%'
16022347Spstset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
16122347SpstIFS="${oIFS}"
16222347Spst
16322347Spstpathcomp=''
16422347Spst
16522347Spstwhile [ $# -ne 0 ] ; do
16622347Spst	pathcomp="${pathcomp}${1}"
16722347Spst	shift
16822347Spst
16922347Spst	if [ ! -d "${pathcomp}" ] ;
17022347Spst        then
17122347Spst		$mkdirprog "${pathcomp}"
17222347Spst	else
17322347Spst		true
17422347Spst	fi
17522347Spst
17622347Spst	pathcomp="${pathcomp}/"
17722347Spstdone
17822347Spstfi
17922347Spst
18022347Spstif [ x"$dir_arg" != x ]
18122347Spstthen
18222347Spst	$doit $instcmd $dst &&
18322347Spst
18422347Spst	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
18522347Spst	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
18622347Spst	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
18722347Spst	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
18822347Spstelse
18922347Spst
19022347Spst# If we're going to rename the final executable, determine the name now.
19122347Spst
19222347Spst	if [ x"$transformarg" = x ] 
19322347Spst	then
19422347Spst		dstfile=`basename $dst`
19522347Spst	else
19622347Spst		dstfile=`basename $dst $transformbasename | 
19722347Spst			sed $transformarg`$transformbasename
19822347Spst	fi
19922347Spst
20022347Spst# don't allow the sed command to completely eliminate the filename
20122347Spst
20222347Spst	if [ x"$dstfile" = x ] 
20322347Spst	then
20422347Spst		dstfile=`basename $dst`
20522347Spst	else
20622347Spst		true
20722347Spst	fi
20822347Spst
20922347Spst# Make a temp file name in the proper directory.
21022347Spst
21122347Spst	dsttmp=$dstdir/#inst.$$#
21222347Spst
21322347Spst# Move or copy the file name to the temp name
21422347Spst
21522347Spst	$doit $instcmd $src $dsttmp &&
21622347Spst
21722347Spst	trap "rm -f ${dsttmp}" 0 &&
21822347Spst
21922347Spst# and set any options; do chmod last to preserve setuid bits
22022347Spst
22122347Spst# If any of these fail, we abort the whole thing.  If we want to
22222347Spst# ignore errors from any of these, just make sure not to ignore
22322347Spst# errors from the above "$doit $instcmd $src $dsttmp" command.
22422347Spst
22522347Spst	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
22622347Spst	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
22722347Spst	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
22822347Spst	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
22922347Spst
23022347Spst# Now rename the file to the real destination.
23122347Spst
23222347Spst	$doit $rmcmd -f $dstdir/$dstfile &&
23322347Spst	$doit $mvcmd $dsttmp $dstdir/$dstfile 
23422347Spst
23522347Spstfi &&
23622347Spst
23722347Spst
23822347Spstexit 0
239