/*
 * UPIWIN - Micro Pi Windowing Framework Kernel
 * Copyright (C) 2019 Amy Bowersox/Erbosoft Metaverse Design Solutions
 *
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *-------------------------------------------------------------------------
 */
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scode.h"
#include "log.h"
#include "config.h"
#include "gpio.h"
#include "fbinit.h"
#include "resources.h"
#include "fontengine.h"
#include "time_func.h"
#include "ep_init.h"
#include "sysinput.h"

int main(int argc, char *argv[])
{
  HRESULT hr;

  /* initialization sequence */
  Time_init();
  hr = Config_setup(argc, argv);
  if (FAILED(hr))
    return EXIT_FAILURE;
  else if (hr != S_OK)
    return EXIT_SUCCESS;
  if (FAILED(Rsrc_setup()))
    return EXIT_FAILURE;
  if (FAILED(Fb_setup()))
    return EXIT_FAILURE;
  if (FAILED(FontEng_setup()))
    return EXIT_FAILURE;
  if (FAILED(Gpio_setup()))
    return EXIT_FAILURE;
  if (FAILED(Epython_setup()))
    return EXIT_FAILURE;
  if (FAILED(Sys_enable_input()))
    return EXIT_FAILURE;
  Log(LINFO, "Pausing at startup.");
  sleep(2); /* wait to show off splash screen */

  Fb_clear();
  if (FAILED(Epython_run()))
    return EXIT_FAILURE;

  Log(LINFO, "Script returned with exit code %d", Sys_Exit_Code);
  return Sys_Exit_Code;
}
