#!/usr/bin/python
import os
import sys
import neo_cgi
import neo_util

neo_cgi.cgiWrap(sys.stdin, sys.stdout, os.environ)

ncgi = neo_cgi.CGI() 
ncgi.parse()

ncgi.hdf.readFile("welcome.hdf")

bookmarksNode = ncgi.hdf.getObj("Page.Bookmarks")
query = ncgi.hdf.getObj("Query")

node = bookmarksNode.child()
lastNode = node.name();
while node != None:
  node = node.next()
  if node:
    lastNode = node.name()

index = str(int(lastNode) + 1)
bookmarksNode.setValue(index + ".Name", query.getValue("Name", ""))
bookmarksNode.setValue(index + ".URL", query.getValue("URL", ""))

ncgi.hdf.removeTree("Query")
ncgi.hdf.removeTree("CGI")
ncgi.hdf.removeTree("HTTP")
ncgi.hdf.writeFile("bookmarkAdded.hdf")

ncgi.display("welcome.cst")