install-sh revision 102644
1102644Snectar#!/bin/sh
255682Smarkm#
355682Smarkm# install - install a program, script, or datafile
4102644Snectar# This comes from X11R5 (mit/util/scripts/install.sh).
555682Smarkm#
6102644Snectar# Copyright 1991 by the Massachusetts Institute of Technology
7102644Snectar#
8102644Snectar# Permission to use, copy, modify, distribute, and sell this software and its
9102644Snectar# documentation for any purpose is hereby granted without fee, provided that
10102644Snectar# the above copyright notice appear in all copies and that both that
11102644Snectar# copyright notice and this permission notice appear in supporting
12102644Snectar# documentation, and that the name of M.I.T. not be used in advertising or
13102644Snectar# publicity pertaining to distribution of the software without specific,
14102644Snectar# written prior permission.  M.I.T. makes no representations about the
15102644Snectar# suitability of this software for any purpose.  It is provided "as is"
16102644Snectar# without express or implied warranty.
17102644Snectar#
1855682Smarkm# Calling this script install-sh is preferred over install.sh, to prevent
1955682Smarkm# `make' implicit rules from creating a file called install from it
2055682Smarkm# when there is no Makefile.
2155682Smarkm#
2255682Smarkm# This script is compatible with the BSD install script, but was written
23102644Snectar# from scratch.  It can only install one file at a time, a restriction
24102644Snectar# shared with many OS's install programs.
2555682Smarkm
2655682Smarkm
2755682Smarkm# set DOITPROG to echo to test this script
2855682Smarkm
2955682Smarkm# Don't use :- since 4.3BSD and earlier shells don't like it.
3055682Smarkmdoit="${DOITPROG-}"
3155682Smarkm
3255682Smarkm
3355682Smarkm# put in absolute paths if you don't have them in your path; or use env. vars.
3455682Smarkm
3555682Smarkmmvprog="${MVPROG-mv}"
3655682Smarkmcpprog="${CPPROG-cp}"
3755682Smarkmchmodprog="${CHMODPROG-chmod}"
3855682Smarkmchownprog="${CHOWNPROG-chown}"
3955682Smarkmchgrpprog="${CHGRPPROG-chgrp}"
4055682Smarkmstripprog="${STRIPPROG-strip}"
4155682Smarkmrmprog="${RMPROG-rm}"
4255682Smarkmmkdirprog="${MKDIRPROG-mkdir}"
4355682Smarkm
44102644Snectartransformbasename=""
4555682Smarkmtransform_arg=""
4655682Smarkminstcmd="$mvprog"
4755682Smarkmchmodcmd="$chmodprog 0755"
4855682Smarkmchowncmd=""
4955682Smarkmchgrpcmd=""
5055682Smarkmstripcmd=""
5155682Smarkmrmcmd="$rmprog -f"
5255682Smarkmmvcmd="$mvprog"
5355682Smarkmsrc=""
5455682Smarkmdst=""
5555682Smarkmdir_arg=""
5655682Smarkm
5755682Smarkmwhile [ x"$1" != x ]; do
5855682Smarkm    case $1 in
5955682Smarkm	-c) instcmd="$cpprog"
6055682Smarkm	    shift
6155682Smarkm	    continue;;
6255682Smarkm
6355682Smarkm	-d) dir_arg=true
6455682Smarkm	    shift
6555682Smarkm	    continue;;
6655682Smarkm
6755682Smarkm	-m) chmodcmd="$chmodprog $2"
6855682Smarkm	    shift
6955682Smarkm	    shift
7055682Smarkm	    continue;;
7155682Smarkm
7255682Smarkm	-o) chowncmd="$chownprog $2"
7355682Smarkm	    shift
7455682Smarkm	    shift
7555682Smarkm	    continue;;
7655682Smarkm
7755682Smarkm	-g) chgrpcmd="$chgrpprog $2"
7855682Smarkm	    shift
7955682Smarkm	    shift
8055682Smarkm	    continue;;
8155682Smarkm
8255682Smarkm	-s) stripcmd="$stripprog"
8355682Smarkm	    shift
8455682Smarkm	    continue;;
8555682Smarkm
8655682Smarkm	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
8755682Smarkm	    shift
8855682Smarkm	    continue;;
8955682Smarkm
9055682Smarkm	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
9155682Smarkm	    shift
9255682Smarkm	    continue;;
9355682Smarkm
9455682Smarkm	*)  if [ x"$src" = x ]
9555682Smarkm	    then
9655682Smarkm		src=$1
9755682Smarkm	    else
9855682Smarkm		# this colon is to work around a 386BSD /bin/sh bug
9955682Smarkm		:
10055682Smarkm		dst=$1
10155682Smarkm	    fi
10255682Smarkm	    shift
10355682Smarkm	    continue;;
10455682Smarkm    esac
10555682Smarkmdone
10655682Smarkm
10755682Smarkmif [ x"$src" = x ]
10855682Smarkmthen
10955682Smarkm	echo "install:	no input file specified"
11055682Smarkm	exit 1
11155682Smarkmelse
112102644Snectar	:
11355682Smarkmfi
11455682Smarkm
11555682Smarkmif [ x"$dir_arg" != x ]; then
11655682Smarkm	dst=$src
11755682Smarkm	src=""
11855682Smarkm	
11955682Smarkm	if [ -d $dst ]; then
12055682Smarkm		instcmd=:
121102644Snectar		chmodcmd=""
12255682Smarkm	else
123102644Snectar		instcmd=$mkdirprog
12455682Smarkm	fi
12555682Smarkmelse
12655682Smarkm
12755682Smarkm# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
12855682Smarkm# might cause directories to be created, which would be especially bad 
12955682Smarkm# if $src (and thus $dsttmp) contains '*'.
13055682Smarkm
131102644Snectar	if [ -f "$src" ] || [ -d "$src" ]
13255682Smarkm	then
133102644Snectar		:
13455682Smarkm	else
13555682Smarkm		echo "install:  $src does not exist"
13655682Smarkm		exit 1
13755682Smarkm	fi
13855682Smarkm	
13955682Smarkm	if [ x"$dst" = x ]
14055682Smarkm	then
14155682Smarkm		echo "install:	no destination specified"
14255682Smarkm		exit 1
14355682Smarkm	else
144102644Snectar		:
14555682Smarkm	fi
14655682Smarkm
14755682Smarkm# If destination is a directory, append the input filename; if your system
14855682Smarkm# does not like double slashes in filenames, you may need to add some logic
14955682Smarkm
15055682Smarkm	if [ -d $dst ]
15155682Smarkm	then
15255682Smarkm		dst="$dst"/`basename $src`
15355682Smarkm	else
154102644Snectar		:
15555682Smarkm	fi
15655682Smarkmfi
15755682Smarkm
15855682Smarkm## this sed command emulates the dirname command
15955682Smarkmdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
16055682Smarkm
16155682Smarkm# Make sure that the destination directory exists.
16255682Smarkm#  this part is taken from Noah Friedman's mkinstalldirs script
16355682Smarkm
16455682Smarkm# Skip lots of stat calls in the usual case.
16555682Smarkmif [ ! -d "$dstdir" ]; then
166102644SnectardefaultIFS='
167102644Snectar	'
16855682SmarkmIFS="${IFS-${defaultIFS}}"
16955682Smarkm
17055682SmarkmoIFS="${IFS}"
17155682Smarkm# Some sh's can't handle IFS=/ for some reason.
17255682SmarkmIFS='%'
17355682Smarkmset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
17455682SmarkmIFS="${oIFS}"
17555682Smarkm
17655682Smarkmpathcomp=''
17755682Smarkm
17855682Smarkmwhile [ $# -ne 0 ] ; do
17955682Smarkm	pathcomp="${pathcomp}${1}"
18055682Smarkm	shift
18155682Smarkm
18255682Smarkm	if [ ! -d "${pathcomp}" ] ;
18355682Smarkm        then
18455682Smarkm		$mkdirprog "${pathcomp}"
18555682Smarkm	else
186102644Snectar		:
18755682Smarkm	fi
18855682Smarkm
18955682Smarkm	pathcomp="${pathcomp}/"
19055682Smarkmdone
19155682Smarkmfi
19255682Smarkm
19355682Smarkmif [ x"$dir_arg" != x ]
19455682Smarkmthen
19555682Smarkm	$doit $instcmd $dst &&
19655682Smarkm
197102644Snectar	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else : ; fi &&
198102644Snectar	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else : ; fi &&
199102644Snectar	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else : ; fi &&
200102644Snectar	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else : ; fi
20155682Smarkmelse
20255682Smarkm
20355682Smarkm# If we're going to rename the final executable, determine the name now.
20455682Smarkm
20555682Smarkm	if [ x"$transformarg" = x ] 
20655682Smarkm	then
20755682Smarkm		dstfile=`basename $dst`
20855682Smarkm	else
20955682Smarkm		dstfile=`basename $dst $transformbasename | 
21055682Smarkm			sed $transformarg`$transformbasename
21155682Smarkm	fi
21255682Smarkm
21355682Smarkm# don't allow the sed command to completely eliminate the filename
21455682Smarkm
21555682Smarkm	if [ x"$dstfile" = x ] 
21655682Smarkm	then
21755682Smarkm		dstfile=`basename $dst`
21855682Smarkm	else
219102644Snectar		:
22055682Smarkm	fi
22155682Smarkm
22255682Smarkm# Make a temp file name in the proper directory.
22355682Smarkm
22455682Smarkm	dsttmp=$dstdir/#inst.$$#
22555682Smarkm
22655682Smarkm# Move or copy the file name to the temp name
22755682Smarkm
22855682Smarkm	$doit $instcmd $src $dsttmp &&
22955682Smarkm
23055682Smarkm	trap "rm -f ${dsttmp}" 0 &&
23155682Smarkm
23255682Smarkm# and set any options; do chmod last to preserve setuid bits
23355682Smarkm
23455682Smarkm# If any of these fail, we abort the whole thing.  If we want to
23555682Smarkm# ignore errors from any of these, just make sure not to ignore
23655682Smarkm# errors from the above "$doit $instcmd $src $dsttmp" command.
23755682Smarkm
238102644Snectar	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else :;fi &&
239102644Snectar	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else :;fi &&
240102644Snectar	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else :;fi &&
241102644Snectar	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else :;fi &&
24255682Smarkm
24355682Smarkm# Now rename the file to the real destination.
24455682Smarkm
24555682Smarkm	$doit $rmcmd -f $dstdir/$dstfile &&
24655682Smarkm	$doit $mvcmd $dsttmp $dstdir/$dstfile 
24755682Smarkm
24855682Smarkmfi &&
24955682Smarkm
25055682Smarkm
25155682Smarkmexit 0
252