172445Sassar#!/bin/sh
272445Sassar#
372445Sassar# install - install a program, script, or datafile
472445Sassar# This comes from X11R5 (mit/util/scripts/install.sh).
572445Sassar#
672445Sassar# Copyright 1991 by the Massachusetts Institute of Technology
772445Sassar#
872445Sassar# Permission to use, copy, modify, distribute, and sell this software and its
972445Sassar# documentation for any purpose is hereby granted without fee, provided that
1072445Sassar# the above copyright notice appear in all copies and that both that
1172445Sassar# copyright notice and this permission notice appear in supporting
1272445Sassar# documentation, and that the name of M.I.T. not be used in advertising or
1372445Sassar# publicity pertaining to distribution of the software without specific,
1472445Sassar# written prior permission.  M.I.T. makes no representations about the
1572445Sassar# suitability of this software for any purpose.  It is provided "as is"
1672445Sassar# without express or implied warranty.
1772445Sassar#
1872445Sassar# Calling this script install-sh is preferred over install.sh, to prevent
1972445Sassar# `make' implicit rules from creating a file called install from it
2072445Sassar# when there is no Makefile.
2172445Sassar#
2272445Sassar# This script is compatible with the BSD install script, but was written
2372445Sassar# from scratch.  It can only install one file at a time, a restriction
2472445Sassar# shared with many OS's install programs.
2572445Sassar
2672445Sassar
2772445Sassar# set DOITPROG to echo to test this script
2872445Sassar
2972445Sassar# Don't use :- since 4.3BSD and earlier shells don't like it.
3072445Sassardoit="${DOITPROG-}"
3172445Sassar
3272445Sassar
3372445Sassar# put in absolute paths if you don't have them in your path; or use env. vars.
3472445Sassar
3572445Sassarmvprog="${MVPROG-mv}"
3672445Sassarcpprog="${CPPROG-cp}"
3772445Sassarchmodprog="${CHMODPROG-chmod}"
3872445Sassarchownprog="${CHOWNPROG-chown}"
3972445Sassarchgrpprog="${CHGRPPROG-chgrp}"
4072445Sassarstripprog="${STRIPPROG-strip}"
4172445Sassarrmprog="${RMPROG-rm}"
4272445Sassarmkdirprog="${MKDIRPROG-mkdir}"
4372445Sassar
4472445Sassartransformbasename=""
4572445Sassartransform_arg=""
4672445Sassarinstcmd="$mvprog"
4772445Sassarchmodcmd="$chmodprog 0755"
4872445Sassarchowncmd=""
4972445Sassarchgrpcmd=""
5072445Sassarstripcmd=""
5172445Sassarrmcmd="$rmprog -f"
5272445Sassarmvcmd="$mvprog"
5372445Sassarsrc=""
5472445Sassardst=""
5572445Sassardir_arg=""
5672445Sassar
5772445Sassarwhile [ x"$1" != x ]; do
5872445Sassar    case $1 in
5972445Sassar	-c) instcmd="$cpprog"
6072445Sassar	    shift
6172445Sassar	    continue;;
6272445Sassar
6372445Sassar	-d) dir_arg=true
6472445Sassar	    shift
6572445Sassar	    continue;;
6672445Sassar
6772445Sassar	-m) chmodcmd="$chmodprog $2"
6872445Sassar	    shift
6972445Sassar	    shift
7072445Sassar	    continue;;
7172445Sassar
7272445Sassar	-o) chowncmd="$chownprog $2"
7372445Sassar	    shift
7472445Sassar	    shift
7572445Sassar	    continue;;
7672445Sassar
7772445Sassar	-g) chgrpcmd="$chgrpprog $2"
7872445Sassar	    shift
7972445Sassar	    shift
8072445Sassar	    continue;;
8172445Sassar
8272445Sassar	-s) stripcmd="$stripprog"
8372445Sassar	    shift
8472445Sassar	    continue;;
8572445Sassar
8672445Sassar	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
8772445Sassar	    shift
8872445Sassar	    continue;;
8972445Sassar
9072445Sassar	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
9172445Sassar	    shift
9272445Sassar	    continue;;
9372445Sassar
9472445Sassar	*)  if [ x"$src" = x ]
9572445Sassar	    then
9672445Sassar		src=$1
9772445Sassar	    else
9872445Sassar		# this colon is to work around a 386BSD /bin/sh bug
9972445Sassar		:
10072445Sassar		dst=$1
10172445Sassar	    fi
10272445Sassar	    shift
10372445Sassar	    continue;;
10472445Sassar    esac
10572445Sassardone
10672445Sassar
10772445Sassarif [ x"$src" = x ]
10872445Sassarthen
10972445Sassar	echo "install:	no input file specified"
11072445Sassar	exit 1
11172445Sassarelse
11272445Sassar	true
11372445Sassarfi
11472445Sassar
11572445Sassarif [ x"$dir_arg" != x ]; then
11672445Sassar	dst=$src
11772445Sassar	src=""
11872445Sassar	
11972445Sassar	if [ -d $dst ]; then
12072445Sassar		instcmd=:
12172445Sassar		chmodcmd=""
12272445Sassar	else
12372445Sassar		instcmd=mkdir
12472445Sassar	fi
12572445Sassarelse
12672445Sassar
12772445Sassar# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
12872445Sassar# might cause directories to be created, which would be especially bad 
12972445Sassar# if $src (and thus $dsttmp) contains '*'.
13072445Sassar
13172445Sassar	if [ -f $src -o -d $src ]
13272445Sassar	then
13372445Sassar		true
13472445Sassar	else
13572445Sassar		echo "install:  $src does not exist"
13672445Sassar		exit 1
13772445Sassar	fi
13872445Sassar	
13972445Sassar	if [ x"$dst" = x ]
14072445Sassar	then
14172445Sassar		echo "install:	no destination specified"
14272445Sassar		exit 1
14372445Sassar	else
14472445Sassar		true
14572445Sassar	fi
14672445Sassar
14772445Sassar# If destination is a directory, append the input filename; if your system
14872445Sassar# does not like double slashes in filenames, you may need to add some logic
14972445Sassar
15072445Sassar	if [ -d $dst ]
15172445Sassar	then
15272445Sassar		dst="$dst"/`basename $src`
15372445Sassar	else
15472445Sassar		true
15572445Sassar	fi
15672445Sassarfi
15772445Sassar
15872445Sassar## this sed command emulates the dirname command
15972445Sassardstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
16072445Sassar
16172445Sassar# Make sure that the destination directory exists.
16272445Sassar#  this part is taken from Noah Friedman's mkinstalldirs script
16372445Sassar
16472445Sassar# Skip lots of stat calls in the usual case.
16572445Sassarif [ ! -d "$dstdir" ]; then
16672445SassardefaultIFS='	
16772445Sassar'
16872445SassarIFS="${IFS-${defaultIFS}}"
16972445Sassar
17072445SassaroIFS="${IFS}"
17172445Sassar# Some sh's can't handle IFS=/ for some reason.
17272445SassarIFS='%'
17372445Sassarset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
17472445SassarIFS="${oIFS}"
17572445Sassar
17672445Sassarpathcomp=''
17772445Sassar
17872445Sassarwhile [ $# -ne 0 ] ; do
17972445Sassar	pathcomp="${pathcomp}${1}"
18072445Sassar	shift
18172445Sassar
18272445Sassar	if [ ! -d "${pathcomp}" ] ;
18372445Sassar        then
18472445Sassar		$mkdirprog "${pathcomp}"
18572445Sassar	else
18672445Sassar		true
18772445Sassar	fi
18872445Sassar
18972445Sassar	pathcomp="${pathcomp}/"
19072445Sassardone
19172445Sassarfi
19272445Sassar
19372445Sassarif [ x"$dir_arg" != x ]
19472445Sassarthen
19572445Sassar	$doit $instcmd $dst &&
19672445Sassar
19772445Sassar	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
19872445Sassar	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
19972445Sassar	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
20072445Sassar	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
20172445Sassarelse
20272445Sassar
20372445Sassar# If we're going to rename the final executable, determine the name now.
20472445Sassar
20572445Sassar	if [ x"$transformarg" = x ] 
20672445Sassar	then
20772445Sassar		dstfile=`basename $dst`
20872445Sassar	else
20972445Sassar		dstfile=`basename $dst $transformbasename | 
21072445Sassar			sed $transformarg`$transformbasename
21172445Sassar	fi
21272445Sassar
21372445Sassar# don't allow the sed command to completely eliminate the filename
21472445Sassar
21572445Sassar	if [ x"$dstfile" = x ] 
21672445Sassar	then
21772445Sassar		dstfile=`basename $dst`
21872445Sassar	else
21972445Sassar		true
22072445Sassar	fi
22172445Sassar
22272445Sassar# Make a temp file name in the proper directory.
22372445Sassar
22472445Sassar	dsttmp=$dstdir/#inst.$$#
22572445Sassar
22672445Sassar# Move or copy the file name to the temp name
22772445Sassar
22872445Sassar	$doit $instcmd $src $dsttmp &&
22972445Sassar
23072445Sassar	trap "rm -f ${dsttmp}" 0 &&
23172445Sassar
23272445Sassar# and set any options; do chmod last to preserve setuid bits
23372445Sassar
23472445Sassar# If any of these fail, we abort the whole thing.  If we want to
23572445Sassar# ignore errors from any of these, just make sure not to ignore
23672445Sassar# errors from the above "$doit $instcmd $src $dsttmp" command.
23772445Sassar
23872445Sassar	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
23972445Sassar	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
24072445Sassar	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
24172445Sassar	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
24272445Sassar
24372445Sassar# Now rename the file to the real destination.
24472445Sassar
24572445Sassar	$doit $rmcmd -f $dstdir/$dstfile &&
24672445Sassar	$doit $mvcmd $dsttmp $dstdir/$dstfile 
24772445Sassar
24872445Sassarfi &&
24972445Sassar
25072445Sassar
25172445Sassarexit 0
252