Convert it to .mobi first. If your headings are <h1> <h2> or <h3> and the text in the headings includes any of the words:chapter, part, epilogue, prologue, section or book; then the default structure detection will catch those and create a TOC at the end of the book (you can tweak the Xpath expression if it doesn't to to include the appropriate trigger words, or make it use various <p> classes instead to get the TOC created).
Then convert the .mobi back to HTMLZ, and you can then move the TOC part from the end to wherever you want it.
That's the simplest automatic method I've found.
The REAL Joe