117683Spst#! /bin/sh
217683Spst#
317683Spst# install - install a program, script, or datafile
426175Sfenner# This comes from X11R5 (mit/util/scripts/install.sh).
517683Spst#
626175Sfenner# Copyright 1991 by the Massachusetts Institute of Technology
726175Sfenner#
826175Sfenner# Permission to use, copy, modify, distribute, and sell this software and its
926175Sfenner# documentation for any purpose is hereby granted without fee, provided that
1026175Sfenner# the above copyright notice appear in all copies and that both that
1126175Sfenner# copyright notice and this permission notice appear in supporting
1226175Sfenner# documentation, and that the name of M.I.T. not be used in advertising or
1326175Sfenner# publicity pertaining to distribution of the software without specific,
1426175Sfenner# written prior permission.  M.I.T. makes no representations about the
1526175Sfenner# suitability of this software for any purpose.  It is provided "as is"
1626175Sfenner# without express or implied warranty.
1726175Sfenner#
1817683Spst# Calling this script install-sh is preferred over install.sh, to prevent
1917683Spst# `make' implicit rules from creating a file called install from it
2017683Spst# when there is no Makefile.
2117683Spst#
2217683Spst# This script is compatible with the BSD install script, but was written
2326175Sfenner# from scratch.  It can only install one file at a time, a restriction
2426175Sfenner# shared with many OS's install programs.
2517683Spst
2617683Spst
2717683Spst# set DOITPROG to echo to test this script
2817683Spst
2917683Spst# Don't use :- since 4.3BSD and earlier shells don't like it.
3017683Spstdoit="${DOITPROG-}"
3117683Spst
3217683Spst
3317683Spst# put in absolute paths if you don't have them in your path; or use env. vars.
3417683Spst
3517683Spstmvprog="${MVPROG-mv}"
3617683Spstcpprog="${CPPROG-cp}"
3717683Spstchmodprog="${CHMODPROG-chmod}"
3817683Spstchownprog="${CHOWNPROG-chown}"
3917683Spstchgrpprog="${CHGRPPROG-chgrp}"
4017683Spststripprog="${STRIPPROG-strip}"
4117683Spstrmprog="${RMPROG-rm}"
4217683Spstmkdirprog="${MKDIRPROG-mkdir}"
4317683Spst
4417683Spsttransformbasename=""
4517683Spsttransform_arg=""
4617683Spstinstcmd="$mvprog"
4717683Spstchmodcmd="$chmodprog 0755"
4817683Spstchowncmd=""
4917683Spstchgrpcmd=""
5017683Spststripcmd=""
5117683Spstrmcmd="$rmprog -f"
5217683Spstmvcmd="$mvprog"
5317683Spstsrc=""
5417683Spstdst=""
5517683Spstdir_arg=""
5617683Spst
5717683Spstwhile [ x"$1" != x ]; do
5817683Spst    case $1 in
5917683Spst	-c) instcmd="$cpprog"
6017683Spst	    shift
6117683Spst	    continue;;
6217683Spst
6317683Spst	-d) dir_arg=true
6417683Spst	    shift
6517683Spst	    continue;;
6617683Spst
6717683Spst	-m) chmodcmd="$chmodprog $2"
6817683Spst	    shift
6917683Spst	    shift
7017683Spst	    continue;;
7117683Spst
7217683Spst	-o) chowncmd="$chownprog $2"
7317683Spst	    shift
7417683Spst	    shift
7517683Spst	    continue;;
7617683Spst
7717683Spst	-g) chgrpcmd="$chgrpprog $2"
7817683Spst	    shift
7917683Spst	    shift
8017683Spst	    continue;;
8117683Spst
8217683Spst	-s) stripcmd="$stripprog"
8317683Spst	    shift
8417683Spst	    continue;;
8517683Spst
8617683Spst	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
8717683Spst	    shift
8817683Spst	    continue;;
8917683Spst
9017683Spst	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
9117683Spst	    shift
9217683Spst	    continue;;
9317683Spst
9417683Spst	*)  if [ x"$src" = x ]
9517683Spst	    then
9617683Spst		src=$1
9717683Spst	    else
9817683Spst		# this colon is to work around a 386BSD /bin/sh bug
9917683Spst		:
10017683Spst		dst=$1
10117683Spst	    fi
10217683Spst	    shift
10317683Spst	    continue;;
10417683Spst    esac
10517683Spstdone
10617683Spst
10717683Spstif [ x"$src" = x ]
10817683Spstthen
10917683Spst	echo "install:	no input file specified"
11017683Spst	exit 1
11117683Spstelse
11217683Spst	true
11317683Spstfi
11417683Spst
11517683Spstif [ x"$dir_arg" != x ]; then
11617683Spst	dst=$src
11717683Spst	src=""
11817683Spst	
11917683Spst	if [ -d $dst ]; then
12017683Spst		instcmd=:
12117683Spst	else
12217683Spst		instcmd=mkdir
12317683Spst	fi
12417683Spstelse
12517683Spst
12617683Spst# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
12717683Spst# might cause directories to be created, which would be especially bad 
12817683Spst# if $src (and thus $dsttmp) contains '*'.
12917683Spst
13017683Spst	if [ -f $src -o -d $src ]
13117683Spst	then
13217683Spst		true
13317683Spst	else
13417683Spst		echo "install:  $src does not exist"
13517683Spst		exit 1
13617683Spst	fi
13717683Spst	
13817683Spst	if [ x"$dst" = x ]
13917683Spst	then
14017683Spst		echo "install:	no destination specified"
14117683Spst		exit 1
14217683Spst	else
14317683Spst		true
14417683Spst	fi
14517683Spst
14617683Spst# If destination is a directory, append the input filename; if your system
14717683Spst# does not like double slashes in filenames, you may need to add some logic
14817683Spst
14917683Spst	if [ -d $dst ]
15017683Spst	then
15117683Spst		dst="$dst"/`basename $src`
15217683Spst	else
15317683Spst		true
15417683Spst	fi
15517683Spstfi
15617683Spst
15717683Spst## this sed command emulates the dirname command
15817683Spstdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
15917683Spst
16017683Spst# Make sure that the destination directory exists.
16117683Spst#  this part is taken from Noah Friedman's mkinstalldirs script
16217683Spst
16317683Spst# Skip lots of stat calls in the usual case.
16417683Spstif [ ! -d "$dstdir" ]; then
16517683SpstdefaultIFS='	
16617683Spst'
16717683SpstIFS="${IFS-${defaultIFS}}"
16817683Spst
16917683SpstoIFS="${IFS}"
17017683Spst# Some sh's can't handle IFS=/ for some reason.
17117683SpstIFS='%'
17217683Spstset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
17317683SpstIFS="${oIFS}"
17417683Spst
17517683Spstpathcomp=''
17617683Spst
17717683Spstwhile [ $# -ne 0 ] ; do
17817683Spst	pathcomp="${pathcomp}${1}"
17917683Spst	shift
18017683Spst
18117683Spst	if [ ! -d "${pathcomp}" ] ;
18217683Spst        then
18317683Spst		$mkdirprog "${pathcomp}"
18417683Spst	else
18517683Spst		true
18617683Spst	fi
18717683Spst
18817683Spst	pathcomp="${pathcomp}/"
18917683Spstdone
19017683Spstfi
19117683Spst
19217683Spstif [ x"$dir_arg" != x ]
19317683Spstthen
19417683Spst	$doit $instcmd $dst &&
19517683Spst
19617683Spst	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
19717683Spst	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
19817683Spst	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
19917683Spst	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
20017683Spstelse
20117683Spst
20217683Spst# If we're going to rename the final executable, determine the name now.
20317683Spst
20417683Spst	if [ x"$transformarg" = x ] 
20517683Spst	then
20617683Spst		dstfile=`basename $dst`
20717683Spst	else
20817683Spst		dstfile=`basename $dst $transformbasename | 
20917683Spst			sed $transformarg`$transformbasename
21017683Spst	fi
21117683Spst
21217683Spst# don't allow the sed command to completely eliminate the filename
21317683Spst
21417683Spst	if [ x"$dstfile" = x ] 
21517683Spst	then
21617683Spst		dstfile=`basename $dst`
21717683Spst	else
21817683Spst		true
21917683Spst	fi
22017683Spst
22117683Spst# Make a temp file name in the proper directory.
22217683Spst
22317683Spst	dsttmp=$dstdir/#inst.$$#
22417683Spst
22517683Spst# Move or copy the file name to the temp name
22617683Spst
22717683Spst	$doit $instcmd $src $dsttmp &&
22817683Spst
22917683Spst	trap "rm -f ${dsttmp}" 0 &&
23017683Spst
23117683Spst# and set any options; do chmod last to preserve setuid bits
23217683Spst
23317683Spst# If any of these fail, we abort the whole thing.  If we want to
23417683Spst# ignore errors from any of these, just make sure not to ignore
23517683Spst# errors from the above "$doit $instcmd $src $dsttmp" command.
23617683Spst
23717683Spst	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
23817683Spst	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
23917683Spst	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
24017683Spst	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
24117683Spst
24217683Spst# Now rename the file to the real destination.
24317683Spst
24417683Spst	$doit $rmcmd -f $dstdir/$dstfile &&
24517683Spst	$doit $mvcmd $dsttmp $dstdir/$dstfile 
24617683Spst
24717683Spstfi &&
24817683Spst
24917683Spst
25017683Spstexit 0
251