#!/usr/bin/env python

"""
Copyright 2014 Sam Dodrill <shadowh511@gmail.com>

This software is under GPL.
"""

from os import system

try:
    raw_input = raw_input
except NameError:
    raw_input = input

art = """
 _______ __                              __          __       _______ ______ ______     __
|    ___|  |.-----.--------.-----.-----.|  |_.---.-.|  |_____|_     _|   __ \      |.--|  |
|    ___|  ||  -__|        |  -__|     ||   _|  _  ||  |______||   |_|      <   ---||  _  |
|_______|__||_____|__|__|__|_____|__|__||____|___._||__|     |_______|___|__|______||_____|"""

welcome ="""
Welcome to the Elemental-IRCd Configuration script. This script will help you choose the
best compile flags for your installation of Elemental-IRCd.
"""

class Configure():
    def promptUser(self, prompt, default):
        inp = raw_input("%s [%s]> " % (prompt, default))
        if inp == "":
            return default
        return inp

    def promptYesNo(self, prompt, defult=True):
        inp = False if self.promptUser(prompt, "Y") == "N" else True
        return inp

    def go(self):
        configflags = ["./configure"]
        print(art)
        print(welcome)

        print("Please specify where you would like to install Elemental-IRCd.")
        installloc = self.promptUser("Install location?", "$HOME/ircd")

        print("")
        print("Please specify the maximum nickname length. This must be the same across")
        print("all servers in your network or you risk desyncs. The maximum is 50.")

        nicklen = 0
        nicklen_valid = False
        while not nicklen_valid:
            try:
                nicklen = int(self.promptUser("Maximum nickname length?", "31"))
                if nicklen < 51:
                    nicklen_valid = True
            except ValueError:
                nicklen_valid = False
        
            if not nicklen_valid:
                print("Error: you must choose an integer value under 50.")

        print("")

        print("Would you like to disable small network support? This increases the size")
        print("of a few buffers in the code and can increase performance on large networks.")
        smallnet = self.promptYesNo("Small network? (Y/N)")

        configflags.append("--prefix=%s" % installloc)
        configflags.append("--with-nicklen=%s" % nicklen)
        if not smallnet:
            configflags.append("--enable-small-net")
        else:
            configflags.append("--disable-small-net")

        print("\nThat should be it for now. Running %s" % " ".join(configflags))
        raw_input("Press enter to continue... ")
        system(" ".join(configflags))
        print(art)

        print("""
Next, run `make` and `make install`. Then copy %s/etc/example.conf to
%s/etc/ircd.conf and read through the example configuration completely to make
sure your install is tailored to your needs. After that, run %s/bin/ircd
and start to connect clients and servers as you need.

If you have any problems, please check the documentation in the doc/ folder
of the source repository. If problems persist please stop by #elemental-ircd
on irc.yolo-swag.com and ask. Running Elemental-IRCd in insane conditions may
make support either very difficult or at most impossible.""" % (installloc, installloc, installloc))
            
system("clear")
c = Configure()
try:
    c.go()
except (KeyboardInterrupt, EOFError):
    print("\nInterrupted, exiting!")

# vim: set ts=4 sw=4 tw=0 et
