#!/usr/bin/php
<?php
/**
 * dtweb - Device Tree editor for UDOO boards
 * Copyright (C) 2015 Francesco Montefoschi <francesco.monte@gmail.com>
 *
 * This program 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/>.
 *
 * @package dtweb
 * @author  Francesco Montefoschi
 * @license http://www.gnu.org/licenses/gpl-3.0.html  GNU GPL 3.0
 */
 
require "/opt/dtweb/webapp/autoloader.php";

class UdooScreenCtl
{
    const ERROR_WRONG_USAGE = 1;
    const ERROR_RUN_SUPERUSER = 2;
    const ERROR_BOARD_UNSUPPORTED = 3;
    
    private $allowedScreens = array();
    
    public function __construct()
    {
        if (posix_geteuid() != 0) {
            echo "This program must be run as root." . PHP_EOL;
            exit(self::ERROR_RUN_SUPERUSER);
        }

        $board = Service_BoardDetector::boardFromModel();
        switch ($board) {
            case 'qdl':
                $this->allowedScreens = array('hdmi', 'lvds7', 'lvds15');
                break;
            
            case 'neo':
                $this->allowedScreens = array('hdmi', 'lvds7', 'lvds15', 'headless');
                break;
        }
    }
    
    public function show_usage() {
        echo "Use this tool to get/set the screen connected to the board." . PHP_EOL . PHP_EOL;
        echo "Usage: udooscreenctl set <screentype>" . PHP_EOL;
        echo "Usage: udooscreenctl get" . PHP_EOL;
        echo "The parameter <screentype> can be: " . implode(", ", $this->allowedScreens) . "." . PHP_EOL;
        exit(self::ERROR_WRONG_USAGE);
    }

    public function get_screen() {
        $uenvEditor = new Service_UenvEditor();
        return $uenvEditor->getEnv("video_output", "hdmi");
    }

    public function set_screen($screen) {
        if (!in_array($screen, $this->allowedScreens)) {
            $this->show_usage();
        }

        $uenvEditor = new Service_UenvEditor();
        $uenvEditor->setEnv("video_output", $screen);
        $uenvEditor->toFile();
    }
    
    public function __invoke()
    {
        switch ($_SERVER['argc']) {
            case 2:
                if ($_SERVER['argv'][1] == "get") {
                    echo $this->get_screen() . PHP_EOL;
                    exit(0);
                } else {
                    $this->show_usage();
                }
                break;
                
            case 3:
                if ($_SERVER['argv'][1] == "set" && in_array($_SERVER['argv'][2], $this->allowedScreens)) {
                    $this->set_screen($_SERVER['argv'][2]);
                    exit(0);
                } else {
                    $this->show_usage();
                }
                break;
                
            default:
                $this->show_usage();
        }
    }
}

$command = new UdooScreenCtl();
$command();
