# Path: kano_settings/system/get_username.py
# def get_first_username():
#     real_users = get_real_users()
#     if real_users:
#         real_users.sort(key=lambda x: x[pwd_field_id['pw_uid']])
#         if len(real_users) > 0:
#             return real_users[0][pwd_field_id['pw_name']]
# 
#     return None
# 
# Path: kano_settings/common.py
# IMAGES_DIR = os.path.join(media, "Graphics")
# 
# Path: kano_settings/config_file.py
# def get_setting(variable):
# 
#     try:
#         value = read_json(settings_file)[variable]
#     except Exception:
#         if variable not in DEFAULTS:
#             logger.info("Defaults not found for variable: {}".format(variable))
#         value = DEFAULTS[variable]
#     return value
# 
# def set_setting(variable, value):
# 
#     if username == 'root':
#         return
# 
#     logger.debug(u"config_file / set_setting: {} {}".format(variable, value))
# 
#     data = read_json(settings_file)
#     if not data:
#         data = dict()
# 
#     data[variable] = value
#     write_json(settings_file, data)
#     chown_path(settings_file)


import os
import shutil
import hashlib
import subprocess
import signal
import urllib2
import gzip
import re
    import platform
    import pycountry
    import pycountry
    import json
from bs4 import BeautifulSoup
from kano.logging import logger
from kano_settings.system.get_username import get_first_username
from kano.network import set_dns, restore_dns_interfaces, \
    clear_dns_interfaces, refresh_resolvconf
from kano_settings.common import settings_dir
from kano_settings.config_file import get_setting, set_setting
from kano.utils.file_operations import ensure_dir, read_file_contents, \
    write_file_contents, read_file_contents_as_lines, read_json, write_json
from kano.utils.shell import run_bg
from kano.utils.user import get_user_unsudoed


def set_hostname_postinst():
    # when running as post install, get the existing first user and set as host name
    new_hostname = get_first_username()

    if new_hostname is None:
        logger.warn("No first user")
    else:
        set_hostname(new_hostname)


def edit_hosts_file(path, new_hostname):
    try:
        hosts = read_file_contents(path)
        hosts += '\n' + hosts_mod_comment + '\n'
        hosts += '127.0.0.1\t{}\n'.format(new_hostname)
        write_file_contents(path, hosts)
    except:
        logger.error("exception while changing change {}".format(path))


def set_hostname(new_hostname):
    if os.environ['LOGNAME'] != 'root':
        logger.error("Error: Settings must be executed with root privileges")

    # Check username chars
    new_hostname = re.sub('[^a-zA-Z0-9]', '', new_hostname).lower()

    if new_hostname == '':

# Path: kano_settings/system/get_username.py
# def get_first_username():
#     real_users = get_real_users()
#     if real_users:
#         real_users.sort(key=lambda x: x[pwd_field_id['pw_uid']])
#         if len(real_users) > 0:
#             return real_users[0][pwd_field_id['pw_name']]
# 
#     return None
# 
# Path: kano_settings/common.py
# IMAGES_DIR = os.path.join(media, "Graphics")
# 
# Path: kano_settings/config_file.py
# def get_setting(variable):
# 
#     try:
#         value = read_json(settings_file)[variable]
#     except Exception:
#         if variable not in DEFAULTS:
#             logger.info("Defaults not found for variable: {}".format(variable))
#         value = DEFAULTS[variable]
#     return value
# 
# def set_setting(variable, value):
# 
#     if username == 'root':
#         return
# 
#     logger.debug(u"config_file / set_setting: {} {}".format(variable, value))
# 
#     data = read_json(settings_file)
#     if not data:
#         data = dict()
# 
#     data[variable] = value
#     write_json(settings_file, data)
#     chown_path(settings_file)


# Path: kano_settings/system/advanced.py
import os
import shutil
import hashlib
import subprocess
import signal
import urllib2
import gzip
import re
    import platform
    import pycountry
    import pycountry
    import json
from bs4 import BeautifulSoup
from kano.logging import logger
from kano_settings.system.get_username import get_first_username
from kano.network import set_dns, restore_dns_interfaces, \
    clear_dns_interfaces, refresh_resolvconf
from kano_settings.common import settings_dir
from kano_settings.config_file import get_setting, set_setting
from kano.utils.file_operations import ensure_dir, read_file_contents, \
    write_file_contents, read_file_contents_as_lines, read_json, write_json
from kano.utils.shell import run_bg
from kano.utils.user import get_user_unsudoed



def set_hostname_postinst():
    # when running as post install, get the existing first user and set as host name
    new_hostname = get_first_username()

    if new_hostname is None:
        logger.warn("No first user")
    else:
        set_hostname(new_hostname)


def edit_hosts_file(path, new_hostname):
    try:
        hosts = read_file_contents(path)
        hosts += '\n' + hosts_mod_comment + '\n'
        hosts += '127.0.0.1\t{}\n'.format(new_hostname)
        write_file_contents(path, hosts)
    except:
        logger.error("exception while changing change {}".format(path))


def set_hostname(new_hostname):
    if os.environ['LOGNAME'] != 'root':
        logger.error("Error: Settings must be executed with root privileges")

    # Check username chars
    new_hostname = re.sub('[^a-zA-Z0-9]', '', new_hostname).lower()

    if new_hostname == '':

        logger.error("no letters left in username after removing illegal ones")