# -*- coding: utf-8 -*-
from setuphelpers import *

r"""
Usable WAPT package functions: install(), uninstall(), session_setup(), audit(), update_package()

"""
# Declaring global variables - Warnings: 1) WAPT context is only available in package functions; 2) Global variables are not persistent between calls


def install():

    if service_installed('telegraf'):
        if service_is_running('telegraf'):
            service_stop('telegraf')
        #service_delete('telegraf')
    if not service_installed('telegraf'):
        servicecmd = " --service install "
    else:
        servicecmd = ""
    InfluxData = makepath(programfiles,'InfluxData')
    if not isdir(InfluxData):
        mkdirs(InfluxData)
    dst = makepath(InfluxData,'telegraf')
    if isfile(makepath(dst,'telegraf.exe')):
        filecopyto(makepath('telegraf-%s' % control.get_software_version(),'telegraf.exe'), makepath(dst,'telegraf.exe'))
    else:
        copytree2('telegraf-%s' % control.get_software_version(), dst)

    if servicecmd:
        run(r'"%s\telegraf.exe" %s --config "%s\telegraf.conf' % (dst,servicecmd,dst) )
    try:
        service_start('telegraf')
    except:
        pass

