
# Copyright (c) 2015 Calin Crisan
# This file is part of motionEyeOS.
#
# motionEyeOS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

import logging
import os.path

from config import additional_config

import streameyectl


CONFIG_TXT = '/boot/config.txt'
MONITOR = '/data/etc/monitor_1'

MONITOR_SCRIPT = '''#!/bin/bash

net_tmp=/tmp/netspeed.tmp 
temp=$(($(cat /sys/devices/virtual/thermal/thermal_zone0/temp) / 1000))
load=$(cat /proc/loadavg | cut -d ' ' -f 2)
recv=$(cat /proc/net/dev | grep -v 'lo:' | tr -s ' ' | cut -d ' ' -f 3 | tail -n +3 | awk '{s+=$1} END {print s}')
send=$(cat /proc/net/dev | grep -v 'lo:' | tr -s ' ' | cut -d ' ' -f 11 | tail -n +3 | awk '{s+=$1} END {print s}')
total=$(($recv + $send))

if [ -e $net_tmp ]; then
    prev_total=$(cat $net_tmp)
    speed=$(($total - $prev_total))
else
    speed=0
fi

echo $total > $net_tmp
speed=$(($speed / 1024))

echo -n "$temp&deg;|$load|${speed}kB/s"
'''

def _get_board_settings():
    gpu_mem = 128
    camera_led = True
    arm_freq = None

    if os.path.exists(CONFIG_TXT):
        logging.debug('reading board settings from %s' % CONFIG_TXT)

        with open(CONFIG_TXT) as f:
            for line in f:
                line = line.strip()
                if not line or line.startswith('#'):
                    continue

                parts = line.split('=', 1)
                if len(parts) != 2:
                    continue
                
                name, value = parts
                name = name.strip()
                value = value.strip()

                if name == 'gpu_mem':
                    gpu_mem = int(value)
                
                elif name == 'arm_freq':
                    arm_freq = int(value)
                    
                elif name == 'disable_camera_led':
                    camera_led = value == '0'
    

    s = {
        'gpuMem': gpu_mem,
        'cameraLed': camera_led
    }

    logging.debug('board settings: gpu_mem=%(gpuMem)s, camera_led=%(cameraLed)s' % s)

    return s


def _set_board_settings(s):
    s.setdefault('gpuMem', 128)
    s.setdefault('cameraLed', True)
    
    old_settings = _get_board_settings()
    if s == old_settings:
        return # nothing has changed

    seen = set()

    logging.debug('writing board settings to %s: ' % CONFIG_TXT + 
            'gpu_mem=%(gpuMem)s, camera_led=%(cameraLed)s' % s)

    lines = []
    if os.path.exists(CONFIG_TXT):
        with open(CONFIG_TXT) as f:
            lines = f.readlines()

        for i, line in enumerate(lines):
            line = line.strip()
            if not line:
                continue

            line = line.strip('#')

            try:
                name, _ = line.split('=', 1)
                name = name.strip()

            except:
                continue
            
            seen.add(name)

            if name.startswith('gpu_mem'):
                lines[i] = '%s=%s' % (name, s['gpuMem'])

            elif name == 'disable_camera_led':
                lines[i] = 'disable_camera_led=%s' % ['1', '0'][s['cameraLed']]

    if 'gpu_mem' not in seen:
        lines.append('gpu_mem=%s' % s['gpuMem'])

    if 'disable_camera_led' not in seen:
        lines.append('disable_camera_led=%s' % ['1', '0'][s['cameraLed']])
        
    logging.debug('remounting /boot read-write')
    if os.system('mount -o remount,rw /boot'):
        logging.error('failed to remount /boot read-write')

    with open(CONFIG_TXT, 'w') as f:
        for line in lines:
            if not line.strip():
                continue
            if not line.endswith('\n'):
                line += '\n'
            f.write(line)


def _get_sys_mon():
    return os.access(MONITOR, os.X_OK)


def _set_sys_mon(sys_mon):
    if sys_mon:
        if os.access(MONITOR, os.X_OK):
            pass
        
        else:
            if os.path.exists(MONITOR):
                os.system('chmod +x %s' % MONITOR)
            
            else:
                with open(MONITOR, 'w') as f:
                    f.write(MONITOR_SCRIPT)
                
                os.system('chmod +x %s' % MONITOR)

    else:
        if os.access(MONITOR, os.X_OK):
            os.system('chmod -x %s' % MONITOR)


@additional_config
def boardSeparator():
    return {
        'type': 'separator',
        'section': 'expertSettings'
    }


@additional_config
def gpuMem():
    return {
        'label': 'GPU Memory',
        'description': 'set the amount of memory reserved for the GPU (choose at least 96MB if you use the CSI camera board)',
        'type': 'number',
        'min': '16',
        'max': '944',
        'section': 'expertSettings',
        'reboot': True,
        'get': _get_board_settings,
        'set': _set_board_settings,
        'get_set_dict': True
    }


@additional_config
def cameraLed():
    return {
        'label': 'Enable CSI Camera Led',
        'description': 'control the led on the CSI camera board',
        'type': 'bool',
        'section': 'expertSettings',
        'reboot': True,
        'get': _get_board_settings,
        'set': _set_board_settings,
        'get_set_dict': True
    }


@additional_config
def sysMon():
    return {
        'label': 'Enable System Monitoring',
        'description': 'when this is enabled, system monitoring info will be overlaid on top of the first camera frame',
        'type': 'bool',
        'section': 'expertSettings',
        'reboot': False,
        'get': _get_sys_mon,
        'set': _set_sys_mon
    }

