#!/bin/sh
# usage:  <td-path>*

while [ $# != 0 ]
do
	D=$1 ; shift
	
	if [ -f $D ]
	then
#		# backup
		S=`nextfile $D`
		mv $D $S
		
#		# copy
		cat $S > $D
		
#		# drop package header #:
		sed -e "s/#://g" < $D > $D.new && mv $D.new $D

#		# convert package separator
		sed -e "s/:\([A-Za-z0-9+@$%^&*_=~<>/!?-]\)/.\1/g" < $D > $D.new && mv $D.new $D
		
#		# add trailing : to field names
		sed -e "s/^\([a-z][a-zA-Z0-9-]*\)/\1:/" < $D > $D.new && mv $D.new $D

# change export types
cp $D $D.new && ed -s $D.new <<EOF
g/^\([ 	]*\)(constant\$/s/^\([ 	]*\)(constant\$/\1(literal/
g/^\([ 	]*\)(accessor-macro\$/s/^\([ 	]*\)(accessor-macro\$/\1(macro/
g/^\([ 	]*\)(open-macro\$/s/^\([ 	]*\)(open-macro\$/\1(macro/
g/^\([ 	]*\)(structure-accessor\$/s/^\([ 	]*\)(structure-accessor\$/\1(function/
g/^\([ 	]*\)(reader\$/s/^\([ 	]*\)(reader\$/\1(function/
g/^\([ 	]*\)(writer\$/s/^\([ 	]*\)(writer\$/\1(function/
g/^\([ 	]*\)(constructor\$/s/^\([ 	]*\)(constructor\$/\1(function/
g/^\([ 	]*\)(predicate\$/s/^\([ 	]*\)(predicate\$/\1(function/
g/^\([ 	]*\)(printer\$/s/^\([ 	]*\)(printer\$/\1(function/
g/^\([ 	]*\)(fast-constructor\$/s/^\([ 	]*\)(fast-constructor\$/\1(constructor/
g/^\([ 	]*\)(structure\$/s/^\([ 	]*\)(structure\$/\1(class/
g/^\([ 	]*\)(syntax-node\$/s/^\([ 	]*\)(syntax-node\$/\1(class/
g/^\([ 	]*\)(accessor\$/s/^\([ 	]*\)(accessor\$/\1(object/
g/^\([ 	]*\)(static-variable\$/s/^\([ 	]*\)(static-variable\$/\1(global-variable/
w
q
EOF
mv $D.new $D
		
		echo translated $D
	else
		echo "* file $D doesn\'t exist"
	fi
done
	
