#!/usr/bin/python
from BeautifulSoup import BeautifulSoup as bsoup
import BeautifulSoup
import requests
import re
import os


# Get and Parse url variables
URL = 'https://repo.saltstack.com/index.html'
GET_URL = requests.get(URL)
HTML = GET_URL.content
PARSE_HTML = bsoup(HTML)

# Miscellaneous variables
TMP_DOCKER_DIR = os.path.join('/tmp', 'docker')
#/tmp/docker/os/install_type/


os_tabs = ['tab1-debian', 'tab2-debian', 'tab3-debian', 'tab1-redhat',
           'tab2-redhat', 'tab3-redhat']

def det_os_family(os):
    return {'tab1-debian': 'debian',
            'tab2-debian': 'debian',
            'tab3-debian': 'debian',
            'tab1-redhat': 'redhat',
            'tab2-redhat': 'redhat',
            'tab3-redhat': 'redhat',
           }[os]

def det_os_versions(os_v):
    return {'debian': ['debian7', 'debian8'],
            'redhat': ['redhat5', 'redhat6', 'redhat7'],
           }[os_v]

def determine_release(current_os):
    '''
    Helper method to determine release type
    '''
    if 'tab1' in current_os:
        release='latest'
    elif 'tab2' in current_os:
        release='major'
    elif 'tab3' in current_os:
        release='minor'
    return release


def parse_html_method(tab_os, os_v):
    '''
    Parse the index.html for install commands
    '''
    os_instruction = []

    for tag in PARSE_HTML.findAll(attrs={'id' : tab_os}):
        # grab all instructions for a specific os and release
        # for example grab debian7 for latest release
        for tab_os_v in tag.findAll(attrs={'class': re.compile(os_v + ".*")}):
            for cmd in tab_os_v.findAll(attrs={'class': 'language-bash'}):
                if cmd not in os_instruction:
                    os_instruction.append(cmd)
        # get all instructions that run on both veresion of each os_family
        for cmd_all in tag.findAll('code', attrs={'class': None}):
            if cmd_all not in os_instruction:
                os_instruction.append(cmd_all)
    return os_instruction


def write_to_file(current_os, steps, release, os_v):
    '''
    Write installation instructions to Dockerfile
    '''
    distro=current_os.split('-')[1]
    docker_dir=os.path.join(TMP_DOCKER_DIR, os_v, release)
    print(docker_dir)
    docker_file=os.path.join(docker_dir, "install_salt.sh")

    if not os.path.exists(docker_dir):
        os.makedirs(docker_dir)
    print('=============================Writing to {0}'.format(docker_file))

    check_steps = []
    for step in steps:
        text = (''.join(step.findAll(text=True)))
        if text not in check_steps:
            check_steps.append(text)

    with open(docker_file, 'w') as outfile:
        for step in check_steps:
            print(step)
            outfile.write(step)
            outfile.write('\n')

for current_os in os_tabs:
    os_family = det_os_family(current_os)
    os_versions = det_os_versions(os_family)
    release = determine_release(current_os)
    for os_v in os_versions:
        os_instr = parse_html_method(current_os, os_v)
        write_to_file(current_os, os_instr, release, os_v)

@@ -488,16 +488,72 @@
 -redhat'
+, 'tab1-ubuntu', 'tab2-ubuntu',%0A           'tab3-ubuntu'
 %5D%0A%0Adef d
@@ -784,32 +784,143 @@
 hat': 'redhat',%0A
+            'tab1-ubuntu': 'ubuntu',%0A            'tab2-ubuntu': 'ubuntu',%0A            'tab3-ubuntu': 'ubuntu',%0A
            %7D%5Bos%5D
@@ -1047,24 +1047,84 @@
 'redhat7'%5D,%0A
+            'ubuntu': %5B'ubuntu12', 'ubuntu14', 'ubuntu16'%5D,%0A
            %7D

add ubuntu