import os
import sys
import config

class Home:

    def __init__(self):
        self.devices_filename = None
        self.credentials_filename = None
        self.configdir = None

        self.home = self.get_home()
        if not self.home:
            print 'Warning, home directory not set. Home storage disabled.'

        self.init_configdir()

    def get_home(self):
        if sys.platform == 'win32':
            return os.environ.get('USERPROFILE')
        else:
            return os.environ.get('HOME')
            

    def init_configdir(self):

        if not self.home: return

        self.configdir = os.path.join(self.home, '.dnstester', config.version)
        if not os.path.isdir(self.configdir):
            try:
                os.makedirs(self.configdir)
            except:
                self.configdir = None
                return

        self.devices_filename = os.path.join(self.configdir, 'devices.xml')
        self.credentials_filename = os.path.join(self.configdir, 'credentials.xml')
