126497Sache#!/bin/sh
226497Sache#
326497Sache# install - install a program, script, or datafile
426497Sache# This comes from X11R5.
526497Sache#
626497Sache# $XConsortium: install.sh,v 1.2 89/12/18 14:47:22 jim Exp $
726497Sache#
8119610Sache# Copyright 1991 by the Massachusetts Institute of Technology
9119610Sache#
10119610Sache# Permission to use, copy, modify, distribute, and sell this software and its
11119610Sache# documentation for any purpose is hereby granted without fee, provided that
12119610Sache# the above copyright notice appear in all copies and that both that
13119610Sache# copyright notice and this permission notice appear in supporting
14119610Sache# documentation, and that the name of M.I.T. not be used in advertising or
15119610Sache# publicity pertaining to distribution of the software without specific,
16119610Sache# written prior permission.  M.I.T. makes no representations about the
17119610Sache# suitability of this software for any purpose.  It is provided "as is"
18119610Sache# without express or implied warranty.
19119610Sache#
2026497Sache# This script is compatible with the BSD install script, but was written
2126497Sache# from scratch.
2226497Sache#
2326497Sache
2426497Sache# set DOITPROG to echo to test this script
2526497Sache
2626497Sache# Don't use :- since 4.3BSD and earlier shells don't like it.
2726497Sachedoit="${DOITPROG-}"
2826497Sache
2926497Sache
3026497Sache# put in absolute paths if you don't have them in your path; or use env. vars.
3126497Sache
3226497Sachemvprog="${MVPROG-mv}"
3326497Sachecpprog="${CPPROG-cp}"
3426497Sachechmodprog="${CHMODPROG-chmod}"
3526497Sachechownprog="${CHOWNPROG-chown}"
3626497Sachechgrpprog="${CHGRPPROG-chgrp}"
3726497Sachestripprog="${STRIPPROG-strip}"
3826497Sachermprog="${RMPROG-rm}"
3926497Sachemkdirprog="${MKDIRPROG-mkdir}"
4026497Sache
4126497Sachetranformbasename=""
4226497Sachetransform_arg=""
4326497Sacheinstcmd="$mvprog"
4426497Sachechmodcmd="$chmodprog 0755"
4526497Sachechowncmd=""
4626497Sachechgrpcmd=""
4726497Sachestripcmd=""
4826497Sachermcmd="$rmprog -f"
4926497Sachemvcmd="$mvprog"
5026497Sachesrc=""
5126497Sachedst=""
5226497Sachedir_arg=""
5326497Sache
5426497Sachewhile [ x"$1" != x ]; do
5526497Sache    case $1 in
5626497Sache	-c) instcmd="$cpprog"
5726497Sache	    shift
5826497Sache	    continue;;
5926497Sache
6026497Sache	-d) dir_arg=true
6126497Sache	    shift
6226497Sache	    continue;;
6326497Sache
6426497Sache	-m) chmodcmd="$chmodprog $2"
6526497Sache	    shift
6626497Sache	    shift
6726497Sache	    continue;;
6826497Sache
6926497Sache	-o) chowncmd="$chownprog $2"
7026497Sache	    shift
7126497Sache	    shift
7226497Sache	    continue;;
7326497Sache
7426497Sache	-g) chgrpcmd="$chgrpprog $2"
7526497Sache	    shift
7626497Sache	    shift
7726497Sache	    continue;;
7826497Sache
7926497Sache	-s) stripcmd="$stripprog"
8026497Sache	    shift
8126497Sache	    continue;;
8226497Sache
8326497Sache	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
8426497Sache	    shift
8526497Sache	    continue;;
8626497Sache
8726497Sache	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
8826497Sache	    shift
8926497Sache	    continue;;
9026497Sache
9126497Sache	*)  if [ x"$src" = x ]
9226497Sache	    then
9326497Sache		src=$1
9426497Sache	    else
9526497Sache		# this colon is to work around a 386BSD /bin/sh bug
9626497Sache		:
9726497Sache		dst=$1
9826497Sache	    fi
9926497Sache	    shift
10026497Sache	    continue;;
10126497Sache    esac
10226497Sachedone
10326497Sache
10426497Sacheif [ x"$src" = x ]
10526497Sachethen
10626497Sache	echo "install:	no input file specified"
10726497Sache	exit 1
10826497Sacheelse
10926497Sache	true
11026497Sachefi
11126497Sache
11226497Sacheif [ x"$dir_arg" != x ]; then
11326497Sache	dst=$src
11426497Sache	src=""
11526497Sache	
11626497Sache	if [ -d $dst ]; then
11726497Sache		instcmd=:
11826497Sache	else
11926497Sache		instcmd=mkdir
12026497Sache	fi
12126497Sacheelse
12226497Sache
12326497Sache# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
12426497Sache# might cause directories to be created, which would be especially bad 
12526497Sache# if $src (and thus $dsttmp) contains '*'.
12626497Sache
12726497Sache	if [ -f $src -o -d $src ]
12826497Sache	then
12926497Sache		true
13026497Sache	else
13126497Sache		echo "install:  $src does not exist"
13226497Sache		exit 1
13326497Sache	fi
13426497Sache	
13526497Sache	if [ x"$dst" = x ]
13626497Sache	then
13726497Sache		echo "install:	no destination specified"
13826497Sache		exit 1
13926497Sache	else
14026497Sache		true
14126497Sache	fi
14226497Sache
14326497Sache# If destination is a directory, append the input filename; if your system
14426497Sache# does not like double slashes in filenames, you may need to add some logic
14526497Sache
14626497Sache	if [ -d $dst ]
14726497Sache	then
14826497Sache		dst="$dst"/`basename $src`
14926497Sache	else
15026497Sache		true
15126497Sache	fi
15226497Sachefi
15326497Sache
15426497Sache## this sed command emulates the dirname command
15526497Sachedstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
15626497Sache
15726497Sache# Make sure that the destination directory exists.
15826497Sache#  this part is taken from Noah Friedman's mkinstalldirs script
15926497Sache
16026497Sache# Skip lots of stat calls in the usual case.
16126497Sacheif [ ! -d "$dstdir" ]; then
16226497SachedefaultIFS='	
16326497Sache'
16426497SacheIFS="${IFS-${defaultIFS}}"
16526497Sache
16626497SacheoIFS="${IFS}"
16726497Sache# Some sh's can't handle IFS=/ for some reason.
16826497SacheIFS='%'
16926497Sacheset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
17026497SacheIFS="${oIFS}"
17126497Sache
17226497Sachepathcomp=''
17326497Sache
17426497Sachewhile [ $# -ne 0 ] ; do
17526497Sache	pathcomp="${pathcomp}${1}"
17626497Sache	shift
17726497Sache
17826497Sache	if [ ! -d "${pathcomp}" ] ;
17926497Sache        then
18026497Sache		$mkdirprog "${pathcomp}"
18126497Sache	else
18226497Sache		true
18326497Sache	fi
18426497Sache
18526497Sache	pathcomp="${pathcomp}/"
18626497Sachedone
18726497Sachefi
18826497Sache
18926497Sacheif [ x"$dir_arg" != x ]
19026497Sachethen
19126497Sache	$doit $instcmd $dst &&
19226497Sache
19326497Sache	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
19426497Sache	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
19526497Sache	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
19626497Sache	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
19726497Sacheelse
19826497Sache
19926497Sache# If we're going to rename the final executable, determine the name now.
20026497Sache
20126497Sache	if [ x"$transformarg" = x ] 
20226497Sache	then
20326497Sache		dstfile=`basename $dst`
20426497Sache	else
20526497Sache		dstfile=`basename $dst $transformbasename | 
20626497Sache			sed $transformarg`$transformbasename
20726497Sache	fi
20826497Sache
20926497Sache# don't allow the sed command to completely eliminate the filename
21026497Sache
21126497Sache	if [ x"$dstfile" = x ] 
21226497Sache	then
21326497Sache		dstfile=`basename $dst`
21426497Sache	else
21526497Sache		true
21626497Sache	fi
21726497Sache
21826497Sache# Make a temp file name in the proper directory.
21926497Sache
22026497Sache	dsttmp=$dstdir/#inst.$$#
22126497Sache
22226497Sache# Move or copy the file name to the temp name
22326497Sache
22426497Sache	$doit $instcmd $src $dsttmp &&
22526497Sache
22626497Sache	trap "rm -f ${dsttmp}" 0 &&
22726497Sache
22826497Sache# and set any options; do chmod last to preserve setuid bits
22926497Sache
23026497Sache# If any of these fail, we abort the whole thing.  If we want to
23126497Sache# ignore errors from any of these, just make sure not to ignore
23226497Sache# errors from the above "$doit $instcmd $src $dsttmp" command.
23326497Sache
23426497Sache	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
23526497Sache	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
23626497Sache	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
23726497Sache	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
23826497Sache
23926497Sache# Now rename the file to the real destination.
24026497Sache
24126497Sache	$doit $rmcmd -f $dstdir/$dstfile &&
24226497Sache	$doit $mvcmd $dsttmp $dstdir/$dstfile 
24326497Sache
24426497Sachefi &&
24526497Sache
24626497Sache
24726497Sacheexit 0
248