I've just joined the forum and I would like to point out my little command line tool to generate the NCX, the html ToC and the opf from an XHTML source. You can find it here
I tried to follow as much as possible the Kindle Publishing Guidelines
and it should work without many problems with the kindlegen tool from Amazon (ToC at the beginning, "text" guide to mark the beginning of the text).
You can choose how many levels in the table of contents as you want. The items in each level are chosen using the -q option followed by an XPath expression. You can add more levels by simply add more '-q XPath' options.
-q "//h1" -q "//h2[@class='toc']"
The XPath query are quite simple: the //h1 will select all the h1 headers he finds and the second query looks for all the h2 tag headers with the attribute class set to "toc" (or you can use != instead of = to select all headers with the class attribute not equal to "toc").
Using the parameter -l VALUE you can also collapse the NCX structure to the desired amount: your book has 2 main parts divided in chapters. You want the chapters to be properly indented in the html toc, but you also want to see each chapter mark in the bottom of the Kindle screen. You can use this tool with the option -l 2 to put both parts and chapters at the same navigation level (the html toc will still be properly indented).
The tool is still in alfa so very likely after you generate the files you still have to edit the opf file to set the cover name (it defaults to Cover.jpg). And remember to backup your files!
I'm not a programmer so the code is quite a mess and there space for a lot of improvements, if anybody find it useful...