#!/usr/bin/env python
# Copyright (c) 2014, Paessler AG <support@paessler.com>
# All rights reserved.
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
# and the following disclaimer in the documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
# or promote products derived from this software without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
import os
import subprocess
import uuid

from setuptools import setup, find_packages
from setuptools.command.install import install as _install

def read(path):
    with open(path, 'r') as f:
        return f.read()

class bcolor:
    GREEN = '\033[92m'
    RED = '\033[91m'
    YELLOW = '\033[93m'
    CYAN = '\033[96m'
    END = '\033[0m'

class Configure(_install):
    probe_conf = {}
    config_old = {}
    config_old['name'] = "Python MiniProbe"
    config_old['gid'] = str(uuid.uuid4())
    config_old['server'] = ""
    config_old['port'] = "443"
    config_old['baseinterval'] = "60"
    config_old['key'] = ""
    config_old['cleanmem'] = ""
    config_old['announced'] = "0"
    config_old['protocol'] = "1"
    config_old['debug'] = ""
    config_old['subprocs'] = "10"
    path = './miniprobe/probe.conf'

    def run(self):
        conf_avail = False
        if not os.getuid() == 0:
            print bcolor.RED + "You must run me as root user!" + bcolor.END
            print bcolor.RED + "Rerun me with sudo " + __file__ + bcolor.END
            sys.exit(2)
        _install.do_egg_install(self)
        print ""
        print bcolor.CYAN + "Welcome to the Miniprobe (Python) for PRTG installer" + bcolor.END
        if self.file_check(self.path):
            print ""
            probe_config_exists = "%s" % str(raw_input(bcolor.YELLOW + "A config file was already found. Do you want to reconfigure [y/N]: " + bcolor.END)).rstrip().lstrip()
            if probe_config_exists.lower() == "y":
                config_old = self.read_config(self.path)
                self.get_config(config_old)
            else:
                print ""
                uninstall = "%s" % str(raw_input(bcolor.YELLOW + "Do you want to Uninstall or Restart the service [u/R]: " + bcolor.END)).rstrip().lstrip()
                if uninstall.lower() == "u":
                    self.remove_config()
                    conf_avail = False
                else:
                    conf_avail = True
        else:
            conf_avail = self.get_config(self.config_old)
            if conf_avail:
                print subprocess.call("update-rc.d prtgprobe defaults", shell=True)
                print bcolor.GREEN + "Starting Mini Probe" + bcolor.END
                print subprocess.call("/etc/init.d/prtgprobe start", shell=True)
                print bcolor.GREEN + "Done. You now can start/stop the Mini Probe using '/etc/init.d/prtgprobe start' or '/etc/init.d/prtgprobe stop'" + bcolor.END
            else:
                print "Exiting!"
                sys.exit()