#!/usr/bin/python

import os
import sys
import cgi
import re
import css_template

print "Content-Type: text/html\n\n"

pattern =  re.compile(r"^[a-f0-9]{32}$") # allow only letters, digits and dots
#list = os.popen('find -daystart -name "*.xml" -print0 | xargs -0 ls -t')
#list = os.popen('ls -t|grep .xml.bz2 2>/dev/null |head -20')
list = os.popen('tail -20 upload/list.txt')
#totals = os.popen('ls |grep -c .xml.bz2').readline()
#subs_today = os.popen('ls -l|grep .xml.bz2|grep -c "`date +"%b %e"`"').readline()
#subs_today = os.popen('find -daystart -name "*.xml" -print0 | xargs -0 ls -t|grep -c xml').readline()
raw_subs_today = open('upload/today.txt', 'r')
subs_today = raw_subs_today.read()
tot_raw = open('upload/total.txt', 'r')
totals = tot_raw.read()
lineno = 19
printlist = []
formdata = cgi.FieldStorage()

title = 'Ubuntu hardware database\n' 

refresh = '<META HTTP-EQUIV=REFRESH CONTENT=\"600;">\n'
head = '<html>\n <head>\n  '+refresh+'<title>'+title+'</title>\n '+css_template.css+'</head>\n<body class="section-wiki">\n<div class="documentContent" id="region-content"><table id="portal-columns">'
#head = '<html>\n <head>\n  '+refresh+'<title>'+title+'</title>\n </head>\n<body class="section-wiki">\n<div class="documentContent" id="region-content"><table id="portal-columns">'
foot = '</table></div></body>\n</html>\n'

print head

for line in list:
	prt_lne = line.split('.')
	printlist.append('<tr><td width="20"></td><td class=content width="50"><small>'+str(lineno)+'</small></td><td class=content width="300"><small><a href="/?xml='+prt_lne[0]+'">'+prt_lne[0]+'</a></small></td>\n<td></td></tr>\n')
	lineno=lineno-1

print '<tr><td colspan="4"><br><img src="./ubuntulogo.png"><p></td></tr>\n<tr><td width="20"><img src="./img/hwdb.png"></td><td colspan="3"><h2>'+title+'</h2></td></tr>\n<tr><td colspan="4"><b>Submissions</b> Total: '+str(totals)+' Today: '+subs_today+'\n<p></td></tr>\n'
print '<tr><td colspan="4"><small>Page autorefresh: every 10min</small></td></tr>\n'

if formdata:
	if len(formdata["xml"].value) > 35:
		sys.exit(1)
	cpu_data = []
	if not pattern.search(formdata["xml"].value):
    		print '<tr><td></td><td colspan="2" width="250" height="200"><h1>Error ! </h1><small>please contact <a href="mailto:\
                hwdb@ubuntu.com">hwdb@ubuntu.com</a></small><br><br></td><td></td></tr>'
		print '<tr><td class="content" colspan="4"><a href="/">Home</a></td></tr></table>'
		print foot
		sys.exit(1)
	print '<tr><td colspan="4"><h3><b>Ubuntu hardware database entry : </b>'+formdata["xml"].value+'</h3><p><small>This is an interim page, that does not show more then some basic data from the dataset.</small><br>\
	<small>If you dont see any cpu or memory data below, you might have sent a broken file,<br> this is most likely the case if hal is not running or has the wrong (non ubuntu hoary or breezy) version.</small></td></tr>'
	raw_notebook_detect = os.popen('bzgrep ">Notebook<" '+formdata["xml"].value+'.xml.bz2')
	raw_portable_detect = os.popen('bzgrep ">Portable<" '+formdata["xml"].value+'.xml.bz2')
	raw_smart_batt = os.popen('bzgrep battery '+formdata["xml"].value+'.xml.bz2|grep present')
	raw_ibook_detect = os.popen('bzgrep "motherboard: iBook" '+formdata["xml"].value+'.xml.bz2')
	raw_pbook_detect = os.popen('bzgrep "motherboard: PowerBook" '+formdata["xml"].value+'.xml.bz2')
	if raw_notebook_detect.readline() or raw_portable_detect.readline() or raw_smart_batt.readline() or raw_ibook_detect.readline() or raw_pbook_detect.readline():
		laptop_detect = 1
		print '<tr><td colspan=4><img src="./img/laptop.png"><br><small>Detected as laptop </small><p></td></tr>'
	else:
		print '<tr><td colspan=4><img src="./img/hal-computer.png"><br><small>Couldnt detect laptop <br>(assuming Desktop)</small><p></td></tr>'

	raw_mem_max = os.popen('bzgrep -i MemTotal '+formdata["xml"].value+'.xml.bz2')
	raw_swap_max = os.popen('bzgrep -i SwapTotal '+formdata["xml"].value+'.xml.bz2')
	mem_data = raw_mem_max.read().split()
	swap_data = raw_swap_max.read().split()
	mem_data.pop()
	swap_data.pop()
		
	if not len(os.popen('bzgrep "PowerMac" '+formdata["xml"].value+'.xml.bz2').readline()):
		raw_cpu_vendor = os.popen('bzgrep vendor_id '+formdata["xml"].value+'.xml.bz2|grep cpuinfo')
		raw_cpu_speed = os.popen('bzgrep -i "cpu[_ ]MHz" '+formdata["xml"].value+'.xml.bz2')
		raw_cpu_model = os.popen('bzgrep "model[_ ]name" '+formdata["xml"].value+'.xml.bz2')
		cpu_model = raw_cpu_model.read().split()
		cpu_data = raw_cpu_vendor.read().split()
		cpu_speed = raw_cpu_speed.read().split()
		mhz = 'MHz'
	else:
		raw_cpu_vendor = os.popen('bzgrep ">Apple Com" '+formdata["xml"].value+'.xml.bz2')
		raw_cpu_speed = os.popen('bzgrep \'="clock\' '+formdata["xml"].value+'.xml.bz2')
		raw_cpu_model = os.popen('bzgrep motherboard '+formdata["xml"].value+'.xml.bz2')
		cpu_model = raw_cpu_model.read().split(':')
		cpu_model[0] = ''
		cpu_data.append('Apple Computer Inc.')# = raw_cpu_vendor.read().split()
		cpu_speed = raw_cpu_speed.read().split()
		mhz = ''
	print '<tr><td></td><td width="30" align="center"><img src="./img/hal-cpu.png"></td><td bgcolor="#fcfcfc" width="360" class="content"><small>Vendor: '+cpu_data.pop().lstrip('type="string">')+'<br>Speed: '+cpu_speed.pop().lstrip('type="string">')+' '+mhz+'<br>Model: '
	for line in cpu_model:
		print line
	print '</small></td><td></td></tr>'
	print '<tr><td colspan="4" height="10"></td></tr>'
	print '<tr><td></td><td align="center"><img src="./img/hal-memory.png"></td><td bgcolor="#fcfcfc" width="250" class="content"><small>Memory: '+str(int(mem_data.pop().lstrip('type="string">'))/1000)+' MB<br>Swap: '+str(int(swap_data.pop().lstrip('type="string">'))/1000)+' MB</small></td><td></td></tr>'
	raw_videodev = os.popen("bzgrep -A 30 \'section name=\"Device\"\' "+formdata["xml"].value+".xml.bz2 |grep driver")
	videodev = raw_videodev.readline()
	videodev = videodev.rstrip('</property>\n')
	videodev = videodev.split('>')
	depth = os.popen('bzgrep defaultdepth '+formdata["xml"].value+'.xml.bz2')
	depth = depth.read()
	depth = depth.rstrip('</property>\n')
	depth = depth.split('>')
	modes = os.popen("bzgrep \'<property name=\"modes\">\' "+formdata["xml"].value+".xml.bz2 |tail -1")
	modes = modes.read()
	modes = modes.rstrip('</property>\n')
	modes = modes.split('>')
	raw_videoram = os.popen('bzgrep VideoRAM '+formdata["xml"].value+'.xml.bz2')
	videoram = raw_videoram.read()
	videoram = videoram.split('VideoRAM:')
	print '<tr><td colspan="4" height="10"></td></tr>'
	print '<tr><td></td><td align="center"><img src="./img/videodev.png"></td><td bgcolor="#fcfcfc" width="250" class="content"><small>\nVideodriver: '+videodev[1]+'<br>\nColordepth: '+depth[1]+'<br>\nVideomodes: '+modes[1]+'<br>'
	if videoram[0]:
		vram = videoram[1].split('kByte')
		print 'Videoram: '+vram[0]+' kByte<br>'
	print '</small></td><td></td></tr>'
	print '<tr><td colspan="4" height="20"></td></tr>'
	print '<tr><td class="content" colspan="4"><a href="/">Home</a></td></tr>'
	print foot
	sys.exit(0)

i = 0
print '<tr><td colspan="4"><div class="contentActions">\n\
        <h5 class="hiddenStructure">Stats</h5>\n\
        <ul class="actionItems">\n\
        </ul></div>\n<br><table width="100%">\n'
#            <li id="contentaction-contents">\n\
#                <a class=""\n\
#                   href="/ppc.html"\n\
#                   accesskey="p">\n\
#                    <small>powerpc systems</small>\n\
#                </a>\n\
#            </li>\n\
#            <li id="contentaction-searchwiki">\n\
#                <a class=""\n\
#                   href="/amd64.html"\n\
#                   accesskey="a">\n\
#                    <small>amd64 systems</small>\n\
#                </a>\n\
#            </li>\n\
#        </ul></div>\n<br><table width="100%">\n'
print '<tr><td colspan="4"><small>This is the interim Front Page of the Ubuntu Hardware Database the above links lead you to some very basic statistics, below you can fill in a hwdb id to look up a record.</small></td></tr>'
print '<tr><td colspan="4"><h3>Search for an entry</h3><br></td></tr>'
print '<tr><td colspan="4"><div class="DocumentContent"><form action="/"><small><input type="text" size="40" name="xml" maxlength="32"></small></form><br><small>Copy the id from the device database client into the searchfield to find your own entry</small></div></td></tr>'
#print '<tr><td colspan="4"><div class="DocumentContent">Service in maintenance (searching diasbled)</div></td></tr>'
print '<tr><td colspan="4"><h5><b>The 20 latest submissions</b> (newest on top) </h5><br></td></tr>'
#print '<tr><td colspan="4">Linklist temporary disabled (maintenance mode)</td></tr>'

printlist.reverse()

for line in printlist:
	if i<21:
	    print line
	i=i+1
print '</table>'
print foot

