#!/bin/sh
# usage: trans-descr module-name source-file dest-file package
M=$1 ; shift
S=$1 ; shift
D=$1 ; shift
P=$1 ; shift

# copy
cat $S > $D

# remove #:
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 keywords
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

# drop files
grep -v "^files:" < $D > $D.new && mv $D.new $D

# drop reloadablep
grep -v "^reloadablep:" < $D > $D.new && mv $D.new $D

# change test-files into test-file-p (asuming testfile name is module name)
sed -e "/^test-files:/s/^.*$/test-file-p:      t/" < $D > $D.new && mv $D.new $D

# change read-environment
sed -e "/^read-environment:/s/^.*$/package:      $P/" < $D > $D.new && mv $D.new $D

# change "defmodule foo" into "name foo type module"
sed -e "s/^defmodule:[ 	][ 	]*\([a-z][a-zA-Z0-9-]*\)/type:         module   name:         \1   type: module/" < $D > $D.new && mv $D.new $D
