179968Sobrien#!/bin/sh
279968Sobrien#
379968Sobrien# install - install a program, script, or datafile
479968Sobrien# This comes from X11R5 (mit/util/scripts/install.sh).
579968Sobrien#
679968Sobrien# Copyright 1991 by the Massachusetts Institute of Technology
779968Sobrien#
879968Sobrien# Permission to use, copy, modify, distribute, and sell this software and its
979968Sobrien# documentation for any purpose is hereby granted without fee, provided that
1079968Sobrien# the above copyright notice appear in all copies and that both that
1179968Sobrien# copyright notice and this permission notice appear in supporting
1279968Sobrien# documentation, and that the name of M.I.T. not be used in advertising or
1379968Sobrien# publicity pertaining to distribution of the software without specific,
1479968Sobrien# written prior permission.  M.I.T. makes no representations about the
1579968Sobrien# suitability of this software for any purpose.  It is provided "as is"
1679968Sobrien# without express or implied warranty.
1779968Sobrien#
1879968Sobrien# Calling this script install-sh is preferred over install.sh, to prevent
1979968Sobrien# `make' implicit rules from creating a file called install from it
2079968Sobrien# when there is no Makefile.
2179968Sobrien#
2279968Sobrien# This script is compatible with the BSD install script, but was written
2379968Sobrien# from scratch.  It can only install one file at a time, a restriction
2479968Sobrien# shared with many OS's install programs.
2579968Sobrien
2679968Sobrien
2779968Sobrien# set DOITPROG to echo to test this script
2879968Sobrien
2979968Sobrien# Don't use :- since 4.3BSD and earlier shells don't like it.
3079968Sobriendoit="${DOITPROG-}"
3179968Sobrien
3279968Sobrien
3379968Sobrien# put in absolute paths if you don't have them in your path; or use env. vars.
3479968Sobrien
3579968Sobrienmvprog="${MVPROG-mv}"
3679968Sobriencpprog="${CPPROG-cp}"
3779968Sobrienchmodprog="${CHMODPROG-chmod}"
3879968Sobrienchownprog="${CHOWNPROG-chown}"
3979968Sobrienchgrpprog="${CHGRPPROG-chgrp}"
4079968Sobrienstripprog="${STRIPPROG-strip}"
4179968Sobrienrmprog="${RMPROG-rm}"
4279968Sobrienmkdirprog="${MKDIRPROG-mkdir}"
4379968Sobrien
4479968Sobrientransformbasename=""
4579968Sobrientransform_arg=""
4679968Sobrieninstcmd="$mvprog"
4779968Sobrienchmodcmd="$chmodprog 0755"
4879968Sobrienchowncmd=""
4979968Sobrienchgrpcmd=""
5079968Sobrienstripcmd=""
5179968Sobrienrmcmd="$rmprog -f"
5279968Sobrienmvcmd="$mvprog"
5379968Sobriensrc=""
5479968Sobriendst=""
5579968Sobriendir_arg=""
5679968Sobrien
5779968Sobrienwhile [ x"$1" != x ]; do
5879968Sobrien    case $1 in
5979968Sobrien	-c) instcmd="$cpprog"
6079968Sobrien	    shift
6179968Sobrien	    continue;;
6279968Sobrien
6379968Sobrien	-d) dir_arg=true
6479968Sobrien	    shift
6579968Sobrien	    continue;;
6679968Sobrien
6779968Sobrien	-m) chmodcmd="$chmodprog $2"
6879968Sobrien	    shift
6979968Sobrien	    shift
7079968Sobrien	    continue;;
7179968Sobrien
7279968Sobrien	-o) chowncmd="$chownprog $2"
7379968Sobrien	    shift
7479968Sobrien	    shift
7579968Sobrien	    continue;;
7679968Sobrien
7779968Sobrien	-g) chgrpcmd="$chgrpprog $2"
7879968Sobrien	    shift
7979968Sobrien	    shift
8079968Sobrien	    continue;;
8179968Sobrien
8279968Sobrien	-s) stripcmd="$stripprog"
8379968Sobrien	    shift
8479968Sobrien	    continue;;
8579968Sobrien
8679968Sobrien	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
8779968Sobrien	    shift
8879968Sobrien	    continue;;
8979968Sobrien
9079968Sobrien	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
9179968Sobrien	    shift
9279968Sobrien	    continue;;
9379968Sobrien
9479968Sobrien	*)  if [ x"$src" = x ]
9579968Sobrien	    then
9679968Sobrien		src=$1
9779968Sobrien	    else
9879968Sobrien		# this colon is to work around a 386BSD /bin/sh bug
9979968Sobrien		:
10079968Sobrien		dst=$1
10179968Sobrien	    fi
10279968Sobrien	    shift
10379968Sobrien	    continue;;
10479968Sobrien    esac
10579968Sobriendone
10679968Sobrien
10779968Sobrienif [ x"$src" = x ]
10879968Sobrienthen
10979968Sobrien	echo "install:	no input file specified"
11079968Sobrien	exit 1
11179968Sobrienelse
11279968Sobrien	true
11379968Sobrienfi
11479968Sobrien
11579968Sobrienif [ x"$dir_arg" != x ]; then
11679968Sobrien	dst=$src
11779968Sobrien	src=""
11879968Sobrien	
11979968Sobrien	if [ -d $dst ]; then
12079968Sobrien		instcmd=:
12179968Sobrien		chmodcmd=""
12279968Sobrien	else
12379968Sobrien		instcmd=mkdir
12479968Sobrien	fi
12579968Sobrienelse
12679968Sobrien
12779968Sobrien# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
12879968Sobrien# might cause directories to be created, which would be especially bad 
12979968Sobrien# if $src (and thus $dsttmp) contains '*'.
13079968Sobrien
13179968Sobrien	if [ -f $src -o -d $src ]
13279968Sobrien	then
13379968Sobrien		true
13479968Sobrien	else
13579968Sobrien		echo "install:  $src does not exist"
13679968Sobrien		exit 1
13779968Sobrien	fi
13879968Sobrien	
13979968Sobrien	if [ x"$dst" = x ]
14079968Sobrien	then
14179968Sobrien		echo "install:	no destination specified"
14279968Sobrien		exit 1
14379968Sobrien	else
14479968Sobrien		true
14579968Sobrien	fi
14679968Sobrien
14779968Sobrien# If destination is a directory, append the input filename; if your system
14879968Sobrien# does not like double slashes in filenames, you may need to add some logic
14979968Sobrien
15079968Sobrien	if [ -d $dst ]
15179968Sobrien	then
15279968Sobrien		dst="$dst"/`basename $src`
15379968Sobrien	else
15479968Sobrien		true
15579968Sobrien	fi
15679968Sobrienfi
15779968Sobrien
15879968Sobrien## this sed command emulates the dirname command
15979968Sobriendstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
16079968Sobrien
16179968Sobrien# Make sure that the destination directory exists.
16279968Sobrien#  this part is taken from Noah Friedman's mkinstalldirs script
16379968Sobrien
16479968Sobrien# Skip lots of stat calls in the usual case.
16579968Sobrienif [ ! -d "$dstdir" ]; then
16679968SobriendefaultIFS='	
16779968Sobrien'
16879968SobrienIFS="${IFS-${defaultIFS}}"
16979968Sobrien
17079968SobrienoIFS="${IFS}"
17179968Sobrien# Some sh's can't handle IFS=/ for some reason.
17279968SobrienIFS='%'
17379968Sobrienset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
17479968SobrienIFS="${oIFS}"
17579968Sobrien
17679968Sobrienpathcomp=''
17779968Sobrien
17879968Sobrienwhile [ $# -ne 0 ] ; do
17979968Sobrien	pathcomp="${pathcomp}${1}"
18079968Sobrien	shift
18179968Sobrien
18279968Sobrien	if [ ! -d "${pathcomp}" ] ;
18379968Sobrien        then
18479968Sobrien		$mkdirprog "${pathcomp}"
18579968Sobrien	else
18679968Sobrien		true
18779968Sobrien	fi
18879968Sobrien
18979968Sobrien	pathcomp="${pathcomp}/"
19079968Sobriendone
19179968Sobrienfi
19279968Sobrien
19379968Sobrienif [ x"$dir_arg" != x ]
19479968Sobrienthen
19579968Sobrien	$doit $instcmd $dst &&
19679968Sobrien
19779968Sobrien	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
19879968Sobrien	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
19979968Sobrien	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
20079968Sobrien	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
20179968Sobrienelse
20279968Sobrien
20379968Sobrien# If we're going to rename the final executable, determine the name now.
20479968Sobrien
20579968Sobrien	if [ x"$transformarg" = x ] 
20679968Sobrien	then
20779968Sobrien		dstfile=`basename $dst`
20879968Sobrien	else
20979968Sobrien		dstfile=`basename $dst $transformbasename | 
21079968Sobrien			sed $transformarg`$transformbasename
21179968Sobrien	fi
21279968Sobrien
21379968Sobrien# don't allow the sed command to completely eliminate the filename
21479968Sobrien
21579968Sobrien	if [ x"$dstfile" = x ] 
21679968Sobrien	then
21779968Sobrien		dstfile=`basename $dst`
21879968Sobrien	else
21979968Sobrien		true
22079968Sobrien	fi
22179968Sobrien
22279968Sobrien# Make a temp file name in the proper directory.
22379968Sobrien
22479968Sobrien	dsttmp=$dstdir/#inst.$$#
22579968Sobrien
22679968Sobrien# Move or copy the file name to the temp name
22779968Sobrien
22879968Sobrien	$doit $instcmd $src $dsttmp &&
22979968Sobrien
23079968Sobrien	trap "rm -f ${dsttmp}" 0 &&
23179968Sobrien
23279968Sobrien# and set any options; do chmod last to preserve setuid bits
23379968Sobrien
23479968Sobrien# If any of these fail, we abort the whole thing.  If we want to
23579968Sobrien# ignore errors from any of these, just make sure not to ignore
23679968Sobrien# errors from the above "$doit $instcmd $src $dsttmp" command.
23779968Sobrien
23879968Sobrien	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
23979968Sobrien	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
24079968Sobrien	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
24179968Sobrien	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
24279968Sobrien
24379968Sobrien# Now rename the file to the real destination.
24479968Sobrien
24579968Sobrien	$doit $rmcmd -f $dstdir/$dstfile &&
24679968Sobrien	$doit $mvcmd $dsttmp $dstdir/$dstfile 
24779968Sobrien
24879968Sobrienfi &&
24979968Sobrien
25079968Sobrien
25179968Sobrienexit 0
252