# Path: install/packages.py
# def install(installType):
# 
# Path: install/bro.py
# def install(chosenInterface,webServer):
# 
# Path: install/criticalStack.py
# def get_user_input(input_string):
# def install(csKey):
# 
# Path: install/logstash.py
# def get_user_input(input_string):
# def install(esServer,esUser,esPass):
# 
# Path: install/kibana.py
# def install(chosenInterfaceIP):
# def importDashboard(jsonFileName):
# def importIndexMapping(jsonFileName):
# 
# Path: install/sweetSecurity.py
# def installClient(chosenInterface):
# def installServer():
# def addWebCreds(address,user,pwd,location):
# 
# Path: install/apache.py
# def get_user_input(input_string):
# def install(installType,chosenInterface,chosenIP):
# 
# Path: install/fail2ban.py
# def install():


import argparse, fcntl, getpass, json, os, re, shutil, socket, sqlite3, struct, sys
from time import sleep
from install import packages
from install import bro
from install import criticalStack
from install import elasticSearch
from install import logstash
from install import kibana
from install import sweetSecurity
from install import apache
from install import fail2ban
if __name__ == "__main__":

    # Check if user is root first...
    if os.getuid() != 0:
        sys.exit("Must run as root/sudo")

    while True:
        question = "\033[1m1 - \033[4mFull Install\033[0m: Bro IDS, Critical Stack, ELK Stack, Apache, Sweet Security\n"
        question += "\033[1m2 - \033[4mSensor Only\033[0m: Bro IDS, Critical Stack, Logstash, Sweet Security\n"
        question += "\033[1m3 - \033[4mWeb Server Only\033[0m: Elasticsearch, Kibana, Apache\n"
        question += "\033[1mChoose an Installation Type (1-3)\033[0m: "
        installType = get_user_input(question)
        if not re.match(r'^[1-3]$', installType):
            print("Choose 1, 2, or 3.")
        else:
            break

    memAvailable = 0
    memInfo = os.popen('free -t -m').read()
    for line in memInfo.splitlines():
        if line.rstrip().startswith('Mem:'):
            memAvailable = int(line.split()[1])
    # Give room for swap space
    if memAvailable < 900:
        sys.exit('Less than 1GB of memory. You need more than this to continue.')
    if installType == '1':
        if memAvailable < 1800:
            sys.exit('Less than 2GB of memory available.  Consider splitting installs across multiple devices.')

    # Install prerequisites

# Path: install/packages.py
# def install(installType):
# 
# Path: install/bro.py
# def install(chosenInterface,webServer):
# 
# Path: install/criticalStack.py
# def get_user_input(input_string):
# def install(csKey):
# 
# Path: install/logstash.py
# def get_user_input(input_string):
# def install(esServer,esUser,esPass):
# 
# Path: install/kibana.py
# def install(chosenInterfaceIP):
# def importDashboard(jsonFileName):
# def importIndexMapping(jsonFileName):
# 
# Path: install/sweetSecurity.py
# def installClient(chosenInterface):
# def installServer():
# def addWebCreds(address,user,pwd,location):
# 
# Path: install/apache.py
# def get_user_input(input_string):
# def install(installType,chosenInterface,chosenIP):
# 
# Path: install/fail2ban.py
# def install():


# Path: setup.py
import argparse, fcntl, getpass, json, os, re, shutil, socket, sqlite3, struct, sys
from time import sleep
from install import packages
from install import bro
from install import criticalStack
from install import elasticSearch
from install import logstash
from install import kibana
from install import sweetSecurity
from install import apache
from install import fail2ban

if __name__ == "__main__":

    # Check if user is root first...
    if os.getuid() != 0:
        sys.exit("Must run as root/sudo")

    while True:
        question = "\033[1m1 - \033[4mFull Install\033[0m: Bro IDS, Critical Stack, ELK Stack, Apache, Sweet Security\n"
        question += "\033[1m2 - \033[4mSensor Only\033[0m: Bro IDS, Critical Stack, Logstash, Sweet Security\n"
        question += "\033[1m3 - \033[4mWeb Server Only\033[0m: Elasticsearch, Kibana, Apache\n"
        question += "\033[1mChoose an Installation Type (1-3)\033[0m: "
        installType = get_user_input(question)
        if not re.match(r'^[1-3]$', installType):
            print("Choose 1, 2, or 3.")
        else:
            break

    memAvailable = 0
    memInfo = os.popen('free -t -m').read()
    for line in memInfo.splitlines():
        if line.rstrip().startswith('Mem:'):
            memAvailable = int(line.split()[1])
    # Give room for swap space
    if memAvailable < 900:
        sys.exit('Less than 1GB of memory. You need more than this to continue.')
    if installType == '1':
        if memAvailable < 1800:
            sys.exit('Less than 2GB of memory available.  Consider splitting installs across multiple devices.')

    # Install prerequisites

    packages.install(installType)