#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#       combabase.py
#
#       Copyright 2014 BFR <info@freie-radios.de>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; Version 3 of the License
#
#       This program 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 General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, the license can be downloaded here:
#
#       http://www.gnu.org/licenses/gpl.html

# Meta
__version__ = '0.1.1'
__license__ = "GNU General Public License (GPL) Version 3"
__version_info__ = (0, 1, 1)
__author__ = 'Michael Liebler <michael-liebler@radio-z.net>'

# massively enhanced by Gottfried Gaisbauer <gottfried.gaisbauer@servus.at>

"""
Comba Base Class  - lade Config
"""
import os
import sys
#import StringIO
#import ConfigParser
import socket

from io import StringIO
from configparser import RawConfigParser
from configparser import ConfigParser


class ConfigReader(object):

    def set(self, key, value):
        """
        Eine property setzen
        @type    key: string
        @param   key: Der Key
        @type    value: mixed
        @param   value: Beliebiger Wert
        """
        if(key == "securitylevel"):
            self.__dict__[key] = int(value)
        else:
            self.__dict__[key] = value

# ------------------------------------------------------------------------------------------ #
    def get(self, key, default=None):
        """
        Eine property holen
        @type    key: string
        @param   key: Der Key
        @type    default: mixed
        @param   default: Beliebiger Wert

        """
        if key not in self.__dict__:
            if default:
                self.set(key, default)
            else:
                print("WARNING: Key "+key+" not found!")
                return None

        if key == "debug":
            return self.__dict__[key].count("y")

        return self.__dict__[key]

# ------------------------------------------------------------------------------------------ #
    def loadConfig(self):
        """
        Set config defaults and load settings from file
        :return:
        """
        ini_path = self.get('configpath', '/etc/aura/aura.ini')

        if not os.path.isfile(ini_path):
            print(ini_path + " not found  :(")
            sys.exit(1)

        # INI einlesen
        f = open(ini_path, 'r')
        ini_str = '[root]\n' + f.read()
        f.close()

        self.configDefaults = {
            'secondspertrack' : '1800',
            'audiobase' :       '/var/audio/rec',
            'altaudiobase' :    '/var/audio/preprod',
            'calendarurl' :     'http://localhost/index.php?option=com_jimtawl&view=calendar&format=json&from=#datefrom#&to=#dateto#',
            'communication':    'zmq',
            'playlistdir':      '/var/audio/playlists',
            'archivebase':      '/var/audio/archive/',
            'controllerport':   '9099',
            'logdir' :          '/var/log/comba',
            'loglevel':         'info',
            'securitylevel':     '0',
            'adminmail':        '',
            'frommail':        '',
            'calendar_precache_days': '7',
            'servername':        socket.getfqdn(),
            'serviceport':      '8080',
            'stream' : "",
            'stream_type' : "",
            'stream_host' : "",
            'stream_port' : "",
            'stream_mountpoint' : "",
            'stream_admin_user' : "",
            'stream_admin_password' : "",
        }
        # readfp is deprecated since 3.2
        if sys.version_info <= (3, 2):
            ini_str = StringIO(ini_str)

            config = RawConfigParser(self.configDefaults)
            config.readfp(ini_str)
        else:
            config = ConfigParser(self.configDefaults)
            config.read_string(ini_str)

        for key, value in config.items('root'):
            self.set(key, config.get('root', key).replace('"', '').strip())