#coding=utf-8
# Project Chost
#
# Copyright IBM, Corp. 2013
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

import os
import threading

from ConfigParser import SafeConfigParser


__version__ = "1.1.1"
__release__ = "0"

DEFAULT_LOG_LEVEL = "debug"
VERSION_FILE = '/etc/default/version'
chostLock = threading.Lock()

# 门户页面默认logo路径
DEFAULT_SITELOGO_PATH = '/static/images/res/default_logo.png'

# 门户默认添加的站点
DEFAULT_SITES = [{'id':'filedownload',
                  'site_name':'资源下载',
                  'site_url':'http://<<<localip>>>/pubs/',
                  'logo_path':'/static/images/res/filedownload_logo.png'},
                 {'id':'svmanager',
                  'site_name':'云服务器管理台',
                  'site_url':'https://<<<localip>>>/massclouds-svmanager',
                  'logo_path':'/static/images/res/svmanager.png'}]


# 运行端和客户端注册信息的cache文件
SIGN_HOSTS_FILE = '/var/lib/mplatform/sign_hosts_file'

# 产品的门户配置文件目录
PRODUCT_SITE_CFG_PATH = '/mcos/cfg/'

def get_version():
    version = ''
    if os.path.exists(VERSION_FILE):
        verfile = open(VERSION_FILE,'r')
        lines = verfile.readlines()
        for line in lines:
            if 'VERSION' in line:
                version = line[8:]
                break
    return version

def get_release():
    release = ''
    if os.path.exists(VERSION_FILE):
        verfile = open(VERSION_FILE,'r')
        lines = verfile.readlines()
        for line in lines:
            if 'BUILD_DATE' in line:
                release = line[11:]
                break
    return release

def get_ntptask_path():
    return os.path.join(paths.state_dir, 'ntptask')


class Paths(object):

    def __init__(self):
        self.prefix = self.get_prefix()
        self.state_dir = '/var/lib/mplatform'
        self.log_dir = '/var/log/mplatform'
        self.conf_dir = '/etc/mplatform'
        self.sys_conf_dir = '/etc/sysconfig'
        self.src_dir = '/usr/lib/python2.7/site-packages/mplatform'
        #os.system('mkdir /var/lib/mplatform')
        #os.system('mkdir /etc/mplatform')
        #os.system('cp /usr/lib/python2.7/site-packages/mplatform/nginx.conf.in /etc/mplatform/nginx.conf.in')
        #os.system('cp /usr/lib/python2.7/site-packages/mplatform/uwsgi_params /etc/mplatform/uwsgi_params')

    def get_prefix(self):
            base = os.path.dirname(__file__)
            return base
            #print base
             #base =

    def add_prefix(self, subdir):
        return os.path.join(self.prefix, subdir)



paths = Paths()



def _get_config():
    config = SafeConfigParser()
    config.add_section("server")
    config.set("server", "host", '0.0.0.0')
    config.set("server", "proxy_port", "80")
    config.set("server", "port", "8000")
    config.set("server", "path", paths.get_prefix())
    config.add_section("logging")
    #config.set("logging", "log_dir", paths.log_dir)
    #config.set("logging", "log_level", DEFAULT_LOG_LEVEL)
    #config.set("logging", "debug_log_level", DEFAULT_LOG_LEVEL)


   # config_file = os.path.join(paths.conf_dir, 'mplatform.conf')
   # if os.path.exists(config_file):
    #    config.read(config_file)
    return config

config = _get_config()


if __name__ == '__main__':
    print paths.prefix
