117680Spst#! /bin/sh
217680Spst#
317680Spst# install - install a program, script, or datafile
426180Sfenner# This comes from X11R5 (mit/util/scripts/install.sh).
517680Spst#
626180Sfenner# Copyright 1991 by the Massachusetts Institute of Technology
726180Sfenner#
826180Sfenner# Permission to use, copy, modify, distribute, and sell this software and its
926180Sfenner# documentation for any purpose is hereby granted without fee, provided that
1026180Sfenner# the above copyright notice appear in all copies and that both that
1126180Sfenner# copyright notice and this permission notice appear in supporting
1226180Sfenner# documentation, and that the name of M.I.T. not be used in advertising or
1326180Sfenner# publicity pertaining to distribution of the software without specific,
1426180Sfenner# written prior permission.  M.I.T. makes no representations about the
1526180Sfenner# suitability of this software for any purpose.  It is provided "as is"
1626180Sfenner# without express or implied warranty.
1726180Sfenner#
1817680Spst# Calling this script install-sh is preferred over install.sh, to prevent
1917680Spst# `make' implicit rules from creating a file called install from it
2017680Spst# when there is no Makefile.
2117680Spst#
2217680Spst# This script is compatible with the BSD install script, but was written
2326180Sfenner# from scratch.  It can only install one file at a time, a restriction
2426180Sfenner# shared with many OS's install programs.
2517680Spst
2617680Spst
2717680Spst# set DOITPROG to echo to test this script
2817680Spst
2917680Spst# Don't use :- since 4.3BSD and earlier shells don't like it.
3017680Spstdoit="${DOITPROG-}"
3117680Spst
3217680Spst
3317680Spst# put in absolute paths if you don't have them in your path; or use env. vars.
3417680Spst
3517680Spstmvprog="${MVPROG-mv}"
3617680Spstcpprog="${CPPROG-cp}"
3717680Spstchmodprog="${CHMODPROG-chmod}"
3817680Spstchownprog="${CHOWNPROG-chown}"
3917680Spstchgrpprog="${CHGRPPROG-chgrp}"
4017680Spststripprog="${STRIPPROG-strip}"
4117680Spstrmprog="${RMPROG-rm}"
4217680Spstmkdirprog="${MKDIRPROG-mkdir}"
4317680Spst
4417680Spsttransformbasename=""
4517680Spsttransform_arg=""
4617680Spstinstcmd="$mvprog"
4717680Spstchmodcmd="$chmodprog 0755"
4817680Spstchowncmd=""
4917680Spstchgrpcmd=""
5017680Spststripcmd=""
5117680Spstrmcmd="$rmprog -f"
5217680Spstmvcmd="$mvprog"
5317680Spstsrc=""
5417680Spstdst=""
5517680Spstdir_arg=""
5617680Spst
5717680Spstwhile [ x"$1" != x ]; do
5817680Spst    case $1 in
5917680Spst	-c) instcmd="$cpprog"
6017680Spst	    shift
6117680Spst	    continue;;
6217680Spst
6317680Spst	-d) dir_arg=true
6417680Spst	    shift
6517680Spst	    continue;;
6617680Spst
6717680Spst	-m) chmodcmd="$chmodprog $2"
6817680Spst	    shift
6917680Spst	    shift
7017680Spst	    continue;;
7117680Spst
7217680Spst	-o) chowncmd="$chownprog $2"
7317680Spst	    shift
7417680Spst	    shift
7517680Spst	    continue;;
7617680Spst
7717680Spst	-g) chgrpcmd="$chgrpprog $2"
7817680Spst	    shift
7917680Spst	    shift
8017680Spst	    continue;;
8117680Spst
8217680Spst	-s) stripcmd="$stripprog"
8317680Spst	    shift
8417680Spst	    continue;;
8517680Spst
8617680Spst	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
8717680Spst	    shift
8817680Spst	    continue;;
8917680Spst
9017680Spst	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
9117680Spst	    shift
9217680Spst	    continue;;
9317680Spst
9417680Spst	*)  if [ x"$src" = x ]
9517680Spst	    then
9617680Spst		src=$1
9717680Spst	    else
9817680Spst		# this colon is to work around a 386BSD /bin/sh bug
9917680Spst		:
10017680Spst		dst=$1
10117680Spst	    fi
10217680Spst	    shift
10317680Spst	    continue;;
10417680Spst    esac
10517680Spstdone
10617680Spst
10717680Spstif [ x"$src" = x ]
10817680Spstthen
10917680Spst	echo "install:	no input file specified"
11017680Spst	exit 1
11117680Spstelse
11217680Spst	true
11317680Spstfi
11417680Spst
11517680Spstif [ x"$dir_arg" != x ]; then
11617680Spst	dst=$src
11717680Spst	src=""
11817680Spst	
11917680Spst	if [ -d $dst ]; then
12017680Spst		instcmd=:
12117680Spst	else
12217680Spst		instcmd=mkdir
12317680Spst	fi
12417680Spstelse
12517680Spst
12617680Spst# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
12717680Spst# might cause directories to be created, which would be especially bad 
12817680Spst# if $src (and thus $dsttmp) contains '*'.
12917680Spst
13017680Spst	if [ -f $src -o -d $src ]
13117680Spst	then
13217680Spst		true
13317680Spst	else
13417680Spst		echo "install:  $src does not exist"
13517680Spst		exit 1
13617680Spst	fi
13717680Spst	
13817680Spst	if [ x"$dst" = x ]
13917680Spst	then
14017680Spst		echo "install:	no destination specified"
14117680Spst		exit 1
14217680Spst	else
14317680Spst		true
14417680Spst	fi
14517680Spst
14617680Spst# If destination is a directory, append the input filename; if your system
14717680Spst# does not like double slashes in filenames, you may need to add some logic
14817680Spst
14917680Spst	if [ -d $dst ]
15017680Spst	then
15117680Spst		dst="$dst"/`basename $src`
15217680Spst	else
15317680Spst		true
15417680Spst	fi
15517680Spstfi
15617680Spst
15717680Spst## this sed command emulates the dirname command
15817680Spstdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
15917680Spst
16017680Spst# Make sure that the destination directory exists.
16117680Spst#  this part is taken from Noah Friedman's mkinstalldirs script
16217680Spst
16317680Spst# Skip lots of stat calls in the usual case.
16417680Spstif [ ! -d "$dstdir" ]; then
16517680SpstdefaultIFS='	
16617680Spst'
16717680SpstIFS="${IFS-${defaultIFS}}"
16817680Spst
16917680SpstoIFS="${IFS}"
17017680Spst# Some sh's can't handle IFS=/ for some reason.
17117680SpstIFS='%'
17217680Spstset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
17317680SpstIFS="${oIFS}"
17417680Spst
17517680Spstpathcomp=''
17617680Spst
17717680Spstwhile [ $# -ne 0 ] ; do
17817680Spst	pathcomp="${pathcomp}${1}"
17917680Spst	shift
18017680Spst
18117680Spst	if [ ! -d "${pathcomp}" ] ;
18217680Spst        then
18317680Spst		$mkdirprog "${pathcomp}"
18417680Spst	else
18517680Spst		true
18617680Spst	fi
18717680Spst
18817680Spst	pathcomp="${pathcomp}/"
18917680Spstdone
19017680Spstfi
19117680Spst
19217680Spstif [ x"$dir_arg" != x ]
19317680Spstthen
19417680Spst	$doit $instcmd $dst &&
19517680Spst
19617680Spst	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
19717680Spst	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
19817680Spst	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
19917680Spst	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
20017680Spstelse
20117680Spst
20217680Spst# If we're going to rename the final executable, determine the name now.
20317680Spst
20417680Spst	if [ x"$transformarg" = x ] 
20517680Spst	then
20617680Spst		dstfile=`basename $dst`
20717680Spst	else
20817680Spst		dstfile=`basename $dst $transformbasename | 
20917680Spst			sed $transformarg`$transformbasename
21017680Spst	fi
21117680Spst
21217680Spst# don't allow the sed command to completely eliminate the filename
21317680Spst
21417680Spst	if [ x"$dstfile" = x ] 
21517680Spst	then
21617680Spst		dstfile=`basename $dst`
21717680Spst	else
21817680Spst		true
21917680Spst	fi
22017680Spst
22117680Spst# Make a temp file name in the proper directory.
22217680Spst
22317680Spst	dsttmp=$dstdir/#inst.$$#
22417680Spst
22517680Spst# Move or copy the file name to the temp name
22617680Spst
22717680Spst	$doit $instcmd $src $dsttmp &&
22817680Spst
22917680Spst	trap "rm -f ${dsttmp}" 0 &&
23017680Spst
23117680Spst# and set any options; do chmod last to preserve setuid bits
23217680Spst
23317680Spst# If any of these fail, we abort the whole thing.  If we want to
23417680Spst# ignore errors from any of these, just make sure not to ignore
23517680Spst# errors from the above "$doit $instcmd $src $dsttmp" command.
23617680Spst
23717680Spst	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
23817680Spst	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
23917680Spst	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
24017680Spst	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
24117680Spst
24217680Spst# Now rename the file to the real destination.
24317680Spst
24417680Spst	$doit $rmcmd -f $dstdir/$dstfile &&
24517680Spst	$doit $mvcmd $dsttmp $dstdir/$dstfile 
24617680Spst
24717680Spstfi &&
24817680Spst
24917680Spst
25017680Spstexit 0
251