#!/usr/bin/env python
import string
#import pdb
import os
import sys
#import posix
import pwd
import fcntl

# needs to do: 
# get a new ID for the 
# new entry. 
os.chdir("/afs/sipb/project/www/knowledge-base/sipbml")
id = int(open("current_id",'r').read())
id += 1
# 
# This needs a way to DTRT with file locking. 
f = open("current_id",'w')
fcntl.flock(f,fcntl.LOCK_EX)
f.write(repr(id) + "\n")
fcntl.flock(f,fcntl.LOCK_UN)
f.close()
# get user details. 
#
name = pwd.getpwuid(os.getuid())[4]

# make a template with 
# the relevant details. 
template = """
<sipb><head>
<title></title>
<subject></subject>
<status>Pick one: Maintained. Unmaintained.</status>
<maturity>Pick one: Complete. Draft. Rough draft.</maturity>
<keywords></keywords>
<author order="1" """ +'id="'+repr(os.getuid())+ '">' + name + """</author>
</head>
<body>
Any ordinary XHTML can go here. 
Also, there are tags such as 
"footnote" which you might find handy.
The composer will move <footnote>the contents of the 
note</footnote> to the bottom with a link to and from
where they written. 
</body>
</sipb>"""

id = repr(id)

f = open(id,'w').write(template)
status = 'edit'

# invoke $EDITOR on it. 
while status[0] == 'e' : 
	os.system(os.environ["VISUAL"] + " " + id)
	status = raw_input('What now? edit, save, quit >')

# parse and process the file. Done!
if status[0] == 's' : 
	print "Generating an HTML file... "
# That last word in the command line is what 
# tells the parser to give us the title for RCS.
# kbedit will ask for the author tags.
	p = os.popen('python python/sipb1.py ' + id + ' ../html/'+id+' title')
	title=p.read()
	p.close()
	print "Introducing your file to RCS, using the title.."	
	p = os.popen('rcs -i ' + id,'w')
	p.write(title + '\n.\n')
	p.close()




