#!/bin/sh
# @(#)$Mu: tools/mutstall,v 1.12 $
##
## mutstall
##	This is a bare bones install program, it knows about
##	-o (owner), -g (group) and -m (mode) and copies files.
##	Owner, group, and mode arguments are just passed off to
##	chown, chgrp, and chmod (respectively).
##
## Copyright (C) 1996  Eric A. Howe
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##
##   Authors:	Matthew D. Francey
##		Eric A. Howe (mu@trends.net)
##

##
## initialize
##
me=`basename $0`
version='$Revision: 1.12 $'
version=`echo ${version} | cut -f2 -d' '`
usage() {
	cat <<_DONE_DONE_DONE_
${me} [-h] [-V] [-o owner] [-g group] [-m mode] source target
	Install files.

	-h		Display this explanation and exit.
	-V		Display version and exit.
	-o owner	chown to owner.
	-g group	chgrp to group.
	-m mode		chmod to mode.
	source		What to install.
	target		Where to put it.
_DONE_DONE_DONE_
}

version() {
	echo "${me} version ${version}"
}

##
## deal with the command line options
##
finished="no"
verbose="no"
owner=""
group=""
mode=""
while [ $# -gt 0 -a ${finished} != "yes" ]; do
	case X"$1" in
	X-h)	usage;			exit;;
	X-V)	version;		exit;;
	X-o)	shift;	owner=$1;	shift;;
	X-g)	shift;	group=$1;	shift;;
	X-m)	shift;	mode=$1;	shift;;
	X--)	finished="yes";		shift;;
	X-*)	echo "${me} : unknown switch '$1'" 1>&2
		usage
		exit 1;;
	X*)	finished="yes"
		;;
	esac
done

##
## get the source and target
##
if [ $# -ne 2 ]; then
	echo "${me} : bad arguments" 1>&2
	exit 1
fi
source=$1
bsource=`basename ${source}`
if [ -d $2 ]; then
	target=$2/${bsource}
else
	target=$2
fi

cp ${source} ${target}
if [ $? -ne 0 ]; then
	echo "${me} : could not copy ${source} to ${target}" 1>&2
	exit 1
fi
if [ X${owner} != X ]; then
	chown ${owner} ${target}
	if [ $? -ne 0 ]; then
		echo "${me} : could not chown ${owner} ${target}" 1>&2
		rm -f ${target}
		exit 1
	fi
fi
if [ X${group} != X ]; then
	chgrp ${group} ${target}
	if [ $? -ne 0 ]; then
		echo "${me} : could not chgrp ${group} ${target}" 1>&2
		rm -f ${target}
		exit 1
	fi
fi
if [ X${mode} != X ]; then
	chmod ${mode} ${target}
	if [ $? -ne 0 ]; then
		echo "${me} : could not chmod ${mode} ${target}" 1>&2
		rm -f ${target}
		exit 1
	fi
fi
