#!/bin/sed -nsf

# begin of file, declare file
1{
  /@file/!{ # no file definition yet, add it
    x
    s,^,    @file\n,
    x
  }
}

# line with doxygen comment (defined as ## at begin of line)
/^##\( \|$\)/{
  s/##\( \|$\)/    / # replace ## comments by 4 spaces indent
  H; d               # hold, evaluate later
}

# variable definition
/^set/{
  x
  s,$, */,                                          # append end of comment
  x
  s,^set \+\([^ =]\+\) *\=\? *\(.*\)\?,void \1;\n,
  H
  s,^void ,/** @var ,                              # prepend function decl
  s,;$,,
  G                                                   # append hold buffer
  p; z; x; d                                          # clear buffer, next line
}

# function definition
/^function/{
  x
  s,$, */,                                          # append end of comment
  x
  s,^function \+\([^ ]\+\) *\(.*\)\?,void \1(\2);\n,
  H
  s,^void ,/** @def ,                       # prepend function decl
  s,;$,,
  G                                                   # append hold buffer
  p; z; x; d                                          # clear buffer, next line
}

# default for non matching lines - print buffer
{
  z; x; # get and clear buffer
  /^$/!{                # only if buffer is not empty
    s,^\n,,             # remove first newline
    s,^\(    \)\?,/** , # prepend doxygen comment start
    s,$, */\n,          # append end of comment
    p; d
  }
}

