#!/usr/bin/env python

# .sig engine by TeknoHog <http://www.iki.fi/teknohog/> 2000

#SQL fetch disabled for now
#cacheFile = "/home/teknohog/sig/daquotes"

sigTop = "Mr Risto A. Paju ::: http://www.iki.fi/teknohog/\n\n"

# The quotes must be separated by blank lines
# i.e. lines with only a \n, no spaces!
cycleFile = "/home/teknohog/sig/sciquotes"

# This must be a FIFO, not a regular file:
# $ mknod .signature p
sigFile = "/home/teknohog/.signature"

# A safety measure: I once got /home full when downloading stuff,
# as a result the cycleFile was lost because when attempting to rewrite.
# Use this to save the contents of cycleFile (on a different
# filesystem!) in case cannot write cycleFile. 
dumpFile = "/tmp/sigmake-dump"

#disabled silly web page update for now
#w3File = "/home/teknohog/public_html/finalpage.html"

#import string, time, sys
from string import join #,split
from time import sleep

#import MySQL, whrandom

#def getsig(IDint):
#    IDtext = `IDint`
#    rawtext = (DB.do("select sentence from homepage2 where ID=" + IDtext))[0][0]
#    textlist = split(rawtext)
#    
#    quote = ""
#    while len(textlist):
#	tempstring = ""
#	while len(tempstring) < 72:
#	    if len(textlist):
#		tempstring = tempstring + textlist.pop(0) + " "
#	    else: 
#		break
#	quote = quote + tempstring + "\n"
#
#    cache = ReadFile(cacheFile)
#    cache.append(quote + "\n")
#    cacheStuff = join(cache, "")
#    WriteFile(cacheFile, cacheStuff)
#	
#    return quote

def sigcycle():
    quotes = ReadFile(cycleFile)
    theQuote = []

    while quotes[0] == "\n":
        quotes.pop(0)

    while quotes[0] != "\n":
        theQuote.append(quotes.pop(0))

    quotes.append("\n")
    quotes.extend(theQuote)

    newQuoteStuff = join(quotes, "")

    try:
        WriteFile(cycleFile, newQuoteStuff)
    except:
        # safety measure to save quotestuff if, e.g. /home is suddenly full..
        WriteFile(dumpFile, newQuoteStuff)
        print "Error writing " + cycleFile + ", exiting."
        from sys import exit
        exit()

    quoteString = join(theQuote, "")

    return quoteString

def ReadFile(file):
    File = open(file, "r")
    contents = File.readlines()
    File.close()
    return contents

def WriteFile(filename, content):
    File = open(filename, "w")
    File.write(content)
    File.close()

#def FindQuote():
#    try:
#	DB = MySQL.connect("remote.database.machine", "user")
#	DB.selectdb("database")
#	maxID = (DB.do("select max(ID) from homepage2"))[0][0]
#	id = int(whrandom.random() * maxID + 1)
#	quote = getsig(id)
#	DB.close()
#    
#    except MySQL.error:
#    quote = sigcycle()

#    return quote

#def W3update():
#    for j in range(0, len(quote)):
#	if quote[j] == "\n":
#	    quote = quote[:j] + "<br>" + quote[j+1:]
#
#    w3page = ReadFile(w3File)
#    startpos = w3page.index("<!--quote start-->\n")
#    i = startpos + 1
#    while not w3page[i] == "<!--quote end-->\n":
#	w3page.pop(i)
#    w3page.insert(i, quote+"\n")
#    webstring = join(w3page, "")
#    WriteFile(w3File, webstring)

while 1:
    try:
        quote = sigcycle()
    
        sleep(0.5) # important - try what happens without this :-)
    
        theSig = sigTop + quote
        WriteFile(sigFile, theSig)
    except KeyboardInterrupt:
        pass # eliminate termination from Ctl-C on shell




