#!/bin/sh

if [ "$1" = "" ]; then
	echo "ERROR: Directory not found"
	exit 1
fi

_PORTSDIR=/usr/local/tinderbox/portstrees/FreeBSD/ports

for dir in $*; do
	if [ ! -e ${dir}/Makefile ]; then
		echo "Invalid port"
	else
		CATEG=`cat ${dir}/Makefile | grep CATEGOR | head -1 | sed 's,^.*=[^a-z0-9]*,,g; s/ .*//g'`
		_DIRNAME=`echo ${dir} | sed 's,/*$,,g; s,^.*/,,g'`

		echo "${CATEG}/${_DIRNAME}"

		if [ "${CATEG}" = "" ]; then
			echo "Invalid category - ${CATEG}"
		elif [ "${_DIRNAME}" = "" ]; then
			echo "Invalid port - ${_DIRNAME}"
		elif [ ! -d ${_PORTSDIR}/${CATEG} ]; then
			echo "ERROR: Ports directory doesn't exist - ${_PORTSDIR}/${CATEG}"
		else
			rm -rf ${_PORTSDIR}/${CATEG}/${_DIRNAME}
			cp -R ${dir} ${_PORTSDIR}/${CATEG}/${_DIRNAME}
		fi
	fi
done
