#!/bin/sh
# adapted from bsdinst in X11R5 sources

# bsd install usage:
# install [-cs] [-m mode] [-o owner] [-g group] file1 file2
# install [-cs] [-m mode] [-o owner] [-g group] file1 ...  fileN directory

dst=""
src=""
strip=":"
copy=mv
mode=":"
chown=":"
chgrp=":"

while [ x$1 != x ]; do
    case $1 in 
	-d) if [ ! -d "$2" ]; then
	    	mkdir $2		# Make a dir
	    fi
	    exit 0;;

	-c) copy=cp		# copy instead of move
	    shift
	    continue;;

	-m) chmod="chmod $2"	# -m mode
	    shift
	    shift
	    continue;;

	-o) chown="chown $2"	# -o owner
	    shift
	    shift
	    continue;;

	-g) chgrp="chgrp $2"	# -g group
	    shift
	    shift
	    continue;;

	-s) strip="strip"	# strip after install
	    shift
	    continue;;

	*)  src="$src $dst"
	    dst=$1
	    shift
	    continue;;
    esac
done

if [ -z "$src" ]; then
	echo install: no files specified
	exit 1
fi
N=0; for i in $src; do
	N=`expr $N + 1`
	if [ ! -r $i ]; then
		echo install: can\'t find $i
		exit 1
	fi
done
if [ $N != "1" -a ! -d "$dst" ]; then
	echo install: $dst is not a directory
	exit 1
fi


if [ ! -d "$dst" ]; then
	if $copy $src $dst; then
		$strip $dst/$src
		$chmod $dst/$src
		$chown $dst/$src
		$chgrp $dst/$src
	fi
else
	$copy $src $dst
	cd $dst
	$strip $src
	$chmod $src
	$chown $src
	$chgrp $src
fi

exit 0
