#!/bin/sh

copy="mv" ;
progs=
n_progs=0
destdir=
strip=
next=
owner=
mode=
group=
add_prog=
test=

for arg do
	case ${next} in
		-m)
			mode=${arg}
			next=
			;;
		-o)
			owner=${arg}
			next=
			;;
		-g)
			group=${arg}
			next=
			;;
		*)
			case ${arg} in
				-c)
					copy="cp -p"
					;;
				-s)
					strip="strip"
					;;
				-[mog])
					next=${arg}
					;;
				-t)
					test="echo "
					;;
				-*)
					echo "$0: unknown option: ${arg}"
					exit 1
					;;
				*)
					if [ ! -z "${destdir}" ]; then
						if [ ! -f ${destdir} ]; then
							if [ -d ${destdir} ]; then
								echo "$0: '${destdir}' is a directory"
							else
								echo "$0: '${destdir}' does not exist"
							fi
							exit 1
						fi
						if [ -z "${progs}" ]; then
							progs="${destdir}"
						else 
							progs="${progs} ${destdir}"
						fi
						n_progs=`expr ${n_progs} + 1`;
					fi
					destdir=${arg}
					;;
			esac
			;;
	esac
done

if [ -z "${progs}" ]; then
	echo "$0: no programs specified"
	exit 1
fi
if [ -z "${destdir}" ]; then
	echo "$0: destination directory not specified"
	exit 1
fi
if [ -d ${destdir} ]; then
	targets=
	for prog in ${progs}; do
		targets="${targets} ${destdir}/${prog}"
	done
elif [ ${n_progs} -gt 1 ]; then
	echo "$0: can only copy one target to a file"
	exit 1
else
	targets=${destdir}
fi

${test} ${copy} ${progs} ${destdir}
if [ ! -z "${strip}" ]; then
	${test} strip ${targets} ;
fi
if [ ! -z "${owner}" ]; then
	${test} chown ${owner} ${targets} ;
fi
if [ ! -z "${group}" ]; then
	${test} chgrp ${group} ${targets} ;
fi
if [ ! -z "${mode}" ]; then
	${test} chmod ${mode} ${targets} ;
fi
