install-sh revision 296373
112657Skvn#! /bin/sh
212657Skvn#
312657Skvn# install - install a program, script, or datafile
412657Skvn# This comes from X11R5 (mit/util/scripts/install.sh).
512657Skvn#
612657Skvn# Copyright 1991 by the Massachusetts Institute of Technology
712657Skvn#
812657Skvn# Permission to use, copy, modify, distribute, and sell this software and its
912657Skvn# documentation for any purpose is hereby granted without fee, provided that
1012657Skvn# the above copyright notice appear in all copies and that both that
1112657Skvn# copyright notice and this permission notice appear in supporting
1212657Skvn# documentation, and that the name of M.I.T. not be used in advertising or
1312657Skvn# publicity pertaining to distribution of the software without specific,
1412657Skvn# written prior permission.  M.I.T. makes no representations about the
1512657Skvn# suitability of this software for any purpose.  It is provided "as is"
1612657Skvn# without express or implied warranty.
1712657Skvn#
1812657Skvn# Calling this script install-sh is preferred over install.sh, to prevent
1912657Skvn# `make' implicit rules from creating a file called install from it
2012657Skvn# when there is no Makefile.
2112657Skvn#
2212657Skvn# This script is compatible with the BSD install script, but was written
2312657Skvn# from scratch.  It can only install one file at a time, a restriction
2412657Skvn# shared with many OS's install programs.
2512657Skvn
2612657Skvn
2712657Skvn# set DOITPROG to echo to test this script
2812657Skvn
2912657Skvn# Don't use :- since 4.3BSD and earlier shells don't like it.
3012657Skvndoit="${DOITPROG-}"
3112657Skvn
3212657Skvn
3312657Skvn# put in absolute paths if you don't have them in your path; or use env. vars.
3412657Skvn
3512657Skvnmvprog="${MVPROG-mv}"
3612657Skvncpprog="${CPPROG-cp}"
3712657Skvnchmodprog="${CHMODPROG-chmod}"
3812657Skvnchownprog="${CHOWNPROG-chown}"
3912657Skvnchgrpprog="${CHGRPPROG-chgrp}"
4012657Skvnstripprog="${STRIPPROG-strip}"
4112657Skvnrmprog="${RMPROG-rm}"
4212657Skvnmkdirprog="${MKDIRPROG-mkdir}"
4312657Skvn
4412657Skvntransformbasename=""
4512657Skvntransform_arg=""
4612657Skvninstcmd="$mvprog"
4712657Skvnchmodcmd="$chmodprog 0755"
4812657Skvnchowncmd=""
4912657Skvnchgrpcmd=""
5012657Skvnstripcmd=""
5112657Skvnrmcmd="$rmprog -f"
5212657Skvnmvcmd="$mvprog"
5312657Skvnsrc=""
5412657Skvndst=""
5512657Skvndir_arg=""
5612657Skvn
5712657Skvnwhile [ x"$1" != x ]; do
5812657Skvn    case $1 in
5912657Skvn	-c) instcmd="$cpprog"
6012657Skvn	    shift
6112657Skvn	    continue;;
6212657Skvn
6312657Skvn	-d) dir_arg=true
6412657Skvn	    shift
6512657Skvn	    continue;;
6612657Skvn
6712657Skvn	-m) chmodcmd="$chmodprog $2"
6812657Skvn	    shift
6912657Skvn	    shift
7012657Skvn	    continue;;
7112657Skvn
7212657Skvn	-o) chowncmd="$chownprog $2"
7312657Skvn	    shift
7412657Skvn	    shift
7512657Skvn	    continue;;
7612657Skvn
7712657Skvn	-g) chgrpcmd="$chgrpprog $2"
7812657Skvn	    shift
7912657Skvn	    shift
8012657Skvn	    continue;;
8112657Skvn
8212657Skvn	-s) stripcmd="$stripprog"
8312657Skvn	    shift
8412657Skvn	    continue;;
8512657Skvn
8612657Skvn	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
8712657Skvn	    shift
8812657Skvn	    continue;;
8912657Skvn
9012657Skvn	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
9112657Skvn	    shift
9212657Skvn	    continue;;
9312657Skvn
9412657Skvn	*)  if [ x"$src" = x ]
9512657Skvn	    then
9612657Skvn		src=$1
9712657Skvn	    else
9812657Skvn		# this colon is to work around a 386BSD /bin/sh bug
9912657Skvn		:
10012657Skvn		dst=$1
10112657Skvn	    fi
10212657Skvn	    shift
10312657Skvn	    continue;;
10412657Skvn    esac
10512657Skvndone
10612657Skvn
10712657Skvnif [ x"$src" = x ]
10812657Skvnthen
10912657Skvn	echo "install:	no input file specified"
11012657Skvn	exit 1
11112657Skvnelse
11212657Skvn	true
11312657Skvnfi
11412657Skvn
11512657Skvnif [ x"$dir_arg" != x ]; then
11612657Skvn	dst=$src
11712657Skvn	src=""
11812657Skvn	
11912657Skvn	if [ -d $dst ]; then
12012657Skvn		instcmd=:
12112657Skvn	else
12212657Skvn		instcmd=mkdir
12312657Skvn	fi
12412657Skvnelse
12512657Skvn
12612657Skvn# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
12712657Skvn# might cause directories to be created, which would be especially bad 
12812657Skvn# if $src (and thus $dsttmp) contains '*'.
12912657Skvn
13012657Skvn	if [ -f $src -o -d $src ]
13112657Skvn	then
13212657Skvn		true
13312657Skvn	else
13412657Skvn		echo "install:  $src does not exist"
13512657Skvn		exit 1
13612657Skvn	fi
13712657Skvn	
13812657Skvn	if [ x"$dst" = x ]
13912657Skvn	then
14012657Skvn		echo "install:	no destination specified"
14112657Skvn		exit 1
14212657Skvn	else
14312657Skvn		true
14412657Skvn	fi
14512657Skvn
14612657Skvn# If destination is a directory, append the input filename; if your system
14712657Skvn# does not like double slashes in filenames, you may need to add some logic
14812657Skvn
14912657Skvn	if [ -d $dst ]
15012657Skvn	then
15112657Skvn		dst="$dst"/`basename $src`
15212657Skvn	else
15312657Skvn		true
15413304Siveresov	fi
15513304Siveresovfi
15613304Siveresov
15713304Siveresov## this sed command emulates the dirname command
15813304Siveresovdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
15913304Siveresov
16013304Siveresov# Make sure that the destination directory exists.
16112657Skvn#  this part is taken from Noah Friedman's mkinstalldirs script
16212657Skvn
16312657Skvn# Skip lots of stat calls in the usual case.
16412657Skvnif [ ! -d "$dstdir" ]; then
16512657SkvndefaultIFS='	
16612657Skvn'
16712657SkvnIFS="${IFS-${defaultIFS}}"
16812657Skvn
16912657SkvnoIFS="${IFS}"
17012657Skvn# Some sh's can't handle IFS=/ for some reason.
17112657SkvnIFS='%'
17212657Skvnset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
17312657SkvnIFS="${oIFS}"
17412657Skvn
17513304Siveresovpathcomp=''
17613304Siveresov
17713304Siveresovwhile [ $# -ne 0 ] ; do
17813304Siveresov	pathcomp="${pathcomp}${1}"
17912657Skvn	shift
18012657Skvn
18112657Skvn	if [ ! -d "${pathcomp}" ] ;
18212657Skvn        then
18312657Skvn		$mkdirprog "${pathcomp}"
18412657Skvn	else
18512657Skvn		true
18612657Skvn	fi
18712657Skvn
18812657Skvn	pathcomp="${pathcomp}/"
18912657Skvndone
19012657Skvnfi
19112657Skvn
19212657Skvnif [ x"$dir_arg" != x ]
19312657Skvnthen
19412657Skvn	$doit $instcmd $dst &&
19512657Skvn
19612657Skvn	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
19712657Skvn	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
19812657Skvn	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
19912657Skvn	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
20012657Skvnelse
20112657Skvn
20212657Skvn# If we're going to rename the final executable, determine the name now.
20312657Skvn
20412657Skvn	if [ x"$transformarg" = x ] 
20512657Skvn	then
20612657Skvn		dstfile=`basename $dst`
20712657Skvn	else
20812657Skvn		dstfile=`basename $dst $transformbasename | 
20912657Skvn			sed $transformarg`$transformbasename
21012657Skvn	fi
21112657Skvn
21212657Skvn# don't allow the sed command to completely eliminate the filename
21312657Skvn
21412657Skvn	if [ x"$dstfile" = x ] 
21512657Skvn	then
21612657Skvn		dstfile=`basename $dst`
21712657Skvn	else
21812657Skvn		true
21912657Skvn	fi
22012657Skvn
22112657Skvn# Make a temp file name in the proper directory.
22212657Skvn
22312657Skvn	dsttmp=$dstdir/#inst.$$#
22412657Skvn
22512657Skvn# Move or copy the file name to the temp name
22612657Skvn
22712657Skvn	$doit $instcmd $src $dsttmp &&
22812657Skvn
22912657Skvn	trap "rm -f ${dsttmp}" 0 &&
23012657Skvn
23112657Skvn# and set any options; do chmod last to preserve setuid bits
23212657Skvn
23312657Skvn# If any of these fail, we abort the whole thing.  If we want to
23412657Skvn# ignore errors from any of these, just make sure not to ignore
23512657Skvn# errors from the above "$doit $instcmd $src $dsttmp" command.
23612657Skvn
23712657Skvn	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
23812657Skvn	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
23912657Skvn	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
24012657Skvn	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
24112657Skvn
24212657Skvn# Now rename the file to the real destination.
24312657Skvn
24412657Skvn	$doit $rmcmd -f $dstdir/$dstfile &&
24512657Skvn	$doit $mvcmd $dsttmp $dstdir/$dstfile 
24612657Skvn
24712657Skvnfi &&
24812657Skvn
24912657Skvn
25012657Skvnexit 0
25112657Skvn