#!/usr/bin/env python

import os
import sys
import pwd

sys.path.append(os.path.dirname(os.path.realpath(__file__)))
from pyactiveresource.activeresource import ActiveResource

class Zone(ActiveResource): pass

class Record(ActiveResource): pass

class SliceAPIException(Exception): pass
    

class SliceDNS(object):
    def __init__(self, origin):
        self.api_site = "https://%s@api.slicehost.com/" % self.find_api_password()
        Zone.set_site(self.api_site)
        Record.set_site(self.api_site)
        self.zone = None
        self.origin = origin

    def find_api_password(self):
        user = pwd.getpwuid(os.getuid())
        trypaths = [os.path.join(os.getcwd(), "slicehost_api_password"),
                    os.path.join(user.pw_dir, ".slicehost_api_password"),
                    "/etc/slicehost_api_password"]
        for p in trypaths:
            if os.path.exists(p):
                return file(p).read().strip()
        else:
            raise SliceAPIException("No Slicehost API password found. Tried to read it from the following files:\n * %s" %
                               "\n * ".join(trypaths))

    def get_zone(self):
        if self.zone != None:
            return
        self.zone = Zone.find(origin = self.origin)
            

    def update_record(self, name, data):
        self.get_zone()
    
                      
            

if "__main__" == __name__:
    api_site = api_site % find_api_password()
    Zone.set_site(api_site)
    Record.set_site(api_site)

#    if len(sys.argv) <
    # origin entry type 

    zone = Zone.find(origin="efod.se.")[0]
    print zone
    print zone.id

    records = Record.find(name="uterus")
    for record in records:
        print record.name, record.ttl, record.data
#     uterus = Record({"record_type":"A",
#                      "zone_id":zone.id,
#                      "name":"uterus",
#                      "data":"127.0.0.4"})
#     uterus.save()
#    zone.save()
    
    
    
    
    
