#!/usr/bin/python
#

import urllib2
import string
import sys
import time
import datetime
import socket

def read_accepted():

    global dhcp_accepted
    dhcp_accepted = []
    try:
        infile = open('/tmp/dhcp.leases', 'r')
    except(IOError), e:
        print "couldn't open accepted"
    else:
        filelist = infile.readlines()
        infile.close()
        for item in filelist:
            item=item.strip()
            macaddress=item.split(" ")[1]
            if macaddress not in dhcp_accepted:
                    dhcp_accepted.append(macaddress)
                    #print macaddress

def read_offered():
    global dhcp_offered
    dhcp_offered = []
    try:
        today = datetime.date.today()

        host = socket.gethostname()
        filename='/var/log/'
        filename+=str(today)
        filename+='/'
        filename+=host
        filename+='/dnsmasq'
        infile = open(filename, 'r')
    except(IOError), e:
        print "couldn't open offered"
    else:
        filelist = infile.readlines()
        infile.close()
        for item in filelist:
            item=item.strip()
            if item.find("DHCPOFFER") != -1 :
                macaddress=item.rpartition(" ")[2]
                if macaddress not in dhcp_offered:
                    dhcp_offered.append(macaddress)
                    #print macaddress



def read_current():
    global dhcp_current
    dhcp_current = []
    try:
        infile = open('/etc/dnsmasq.mvpmc.conf', 'r')
    except(IOError), e:
        print "couldn't open current"
    else:
        filelist = infile.readlines()
        infile.close()
        for item in filelist:
            item=item.strip()
            if item.find("dhcp-host") != -1 :
                macaddress=item.partition(",")[2]

                if macaddress not in dhcp_current :
                    dhcp_current.append(macaddress)
                   # print macaddress







def make_html():
    global dhcp_accepted
    global dhcp_offered
    buttonform_top='''
        <div class="content" style="solid #aaa; width:500px; height:300px; overflow:auto; color:#FFF;text-align:left;">
         <form action="/mvp_process.py" method="get">
         '''


    buttonform_bottom='''
            Mac Address:
            <input type="text" name="activemvp">

            </br>
            <input id="submitbutton" type="submit" />
        </form>
        </div>
        '''
    checkboxline = '''
     <input type="checkbox"  name="activemvp" value="%s"  %s  />  %s <br />
    '''
    print '<br />'
    da = set(dhcp_accepted)
    dof = set(dhcp_offered)
    dc = set(dhcp_current)
    possible_mac= dof  -dc
    #possible_mac= dof  -da - dc

    print buttonform_top
    for mac in dhcp_current:
        print checkboxline % (mac ,' checked="yes"' , mac)
    for mac in possible_mac:
        print checkboxline % (mac , " " ,mac)
    print buttonform_bottom


#-------------------
global dhcp_accepted
global dhcp_offered
global dhcp_current




read_offered()
#print "--"
#print '</br>'
read_accepted()
#print "--"
#print '</br>'
read_current()
#print "--"
#print '</br>'



#possible_mac= (dof - da)
#possible_mac = dof
#print "should be unchecked:"
#print possible_mac


make_html()


