#!/bin/sh

PORT=""
OPTS=""

while [ $# != 0 ] ;do
	case $1 in
		--editor)
			shift
			EDITOR="$1"
			if [ "${EDITOR}" = "native" ] ;then
				EDITOR="open -t"
			fi
			;;
		-*)
			OPTS="${OPTS} $1"
			;;
		*)
			PORT="${PORT} $1"
			;;
	esac
	shift
done

if [ "${EDITOR}" != "" -a "${PORT}" != "" ] ;then
	for p in $PORT ;do
		WD="`port work ${p}`"
		if [ -d ${WD} ] ;then
			#echo ${EDITOR} ${OPTS} ${WD}/.macports.${p}.state
			${EDITOR} ${OPTS} ${WD}/.macports.${p}.state
		fi
	done
else
	echo "Usage: `basename $0` [--editor app] [-options] <port1> [port2 [port3 [...]]]"
	echo "This utility allows you to edit the statefile from a port's work directory"
	echo "Use --editor native to invoke the default native text editor"
	echo "Remember to quote spaces in the editor app name or in options that take an argument"
	echo "	(e.g. `basename $0` \"-t tabsize\") foo"
fi
