#!/usr/bin/env python
#
# This file is part of NNTSC.
#
# Copyright (C) 2013-2017 The University of Waikato, Hamilton, New Zealand.
#
# Authors: Shane Alcock
#          Brendon Jones
#          Andy Bell
#
# All rights reserved.
#
# This code has been developed by the WAND Network Research Group at the
# University of Waikato. For further information please see
# http://www.wand.net.nz/
#
# NNTSC is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# NNTSC 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 NNTSC; if not, write to the Free Software Foundation, Inc.
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# Please report any bugs, questions or comments to contact@wand.net.nz
#

import sys
import getopt

from libnntsc.database import DBInsert
from libnntsc.influx import InfluxInsertor
from libnntsc.configurator import *
from libnntsc.importer import import_parsers
from libnntsc.parsers.rrd import insert_rrd_streams
from libnntsc.dberrorcodes import *
import libnntscclient.logger as logger

def print_usage(prog):
    print "Usage for %s" % (prog)
    print
    print "Available options:"
    print "   -C <filename> "
    print "          Specifies the location of the configuration file"
    print "   -F "
    print "          Wipe and rebuild the database"
    print "          WARNING: all time series data will be lost"
    print "   -h "
    print "          Display this usage test"
    print
    sys.exit(0)

clean_db = False
enabled_modules = []

opts, rest = getopt.getopt(sys.argv[1:], 'D:C:Fh')

for o, a in opts:
    if o == '-C':
        conf_fname = a
    if o == '-F':
        clean_db = True
    if o == '-h':
        print_usage(sys.argv[0])

nntsc_conf = load_nntsc_config(conf_fname)
if nntsc_conf == 0:
    sys.exit(1)

dbconf = get_nntsc_db_config(nntsc_conf)
if dbconf == {}:
    sys.exit(1)

for module in nntsc_conf.options('modules'):
    if nntsc_conf.get('modules', module) == "yes":
        enabled_modules.append(module)
modules = import_parsers(enabled_modules)

if dbconf["name"] == "":
    print >> sys.stderr, "No database name specified in the config file. Please edit your config file (%s)" % (conf_fname)
    sys.exit(1)

db = DBInsert(dbconf["name"], dbconf["user"], dbconf["pass"], dbconf["host"])
db.connect_db(15)

try:
    db.build_databases(modules, new=clean_db)
except DBQueryException as e:
    logger.log("Failed to create tables for nntsc database")
    sys.exit(1)

influxconf = get_influx_config(nntsc_conf)

if influxconf == {}:
    logger.log("Invalid options for influx database")
    sys.exit(1)

influxdb = None
if influxconf["useinflux"] and "amp" in modules.keys():
    try:
        influxdb = InfluxInsertor(
            influxconf["name"], influxconf["user"], influxconf["pass"],
            influxconf["host"], influxconf["port"])
    except DBQueryException as e:
        logger.log("Failed to connect to InfluxDB")
        sys.exit(1)
    if clean_db:
        try:
            influxdb.new_db()
        except DBQueryException as e:
            logger.log("Failed to empty InfluxDB")
            sys.exit(1)
    try:
        influxdb.create_retention_policies(influxconf["keepdata"])
    except DBQueryException as e:
        logger.log("Failed to create retention policies for influx")
        sys.exit(1)

    for base, mod in modules.items():
        mod.create_cqs(db, influxdb)

if 'rrd' in enabled_modules:

    rrd_conf = get_nntsc_config(nntsc_conf, 'rrd', 'rrdlist')
    if rrd_conf == "NNTSCConfigError" or rrd_conf == "NNTSCConfigMissing":
        sys.exit(0)

    try:
        insert_rrd_streams(db, influxdb, rrd_conf)
    except DBQueryException as e:
        if e.code == DB_DATA_ERROR:
            logger.log("Failed to create RRD streams -- check RRD List for invalid content")
            sys.exit(1)

        if e.code == DB_GENERIC_ERROR:
            logger.log("Database error while creating RRD streams -- may need to manually check database")
            sys.exit(1)



# vim: set sw=4 tabstop=4 softtabstop=4 expandtab :
