#include <vdr/plugin.h>
#include <string.h>
#include "setup.h"

const char *configFile = "lcdcontrol.conf";

cLcdControlSetup LcdControlSetup;

cLcdControlSetup::cLcdControlSetup(void)
{
  strcpy(serialPort, "/dev/ttySX");
  autoOn = 0;
  configDir = "";
  strcpy(cmdAspectRatio16by9,"");
  strcpy(cmdAspectRatioAuto,"");
  strcpy(cmdPoweron, "");
  strcpy(cmdPowerOff, "");
  strcpy(cmdAspectRatio4by3,"");
  strcpy(cmdAspectRatio14by9,"");
  strcpy(cmdAspectRatioSpectral,"");
  strcpy(cmdAspectRatioFull,"");
  strcpy(cmdAspectRatioZoom1,"");
  strcpy(cmdAspectRatioZoom2,"");
  strcpy(cmdUp,"");
  strcpy(cmdDown,"");
  strcpy(cmdLeft,"");
  strcpy(cmdRight,"");
  strcpy(cmdOk,"");
  strcpy(cmdBack,"");
  strcpy(cmdMenu,"");
}

bool cLcdControlSetup::SetupParse(const char *Name, const char *Value){

	if (!strcasecmp(Name, "serialPort"))      strcpy(serialPort,Value);
	else if (!strcasecmp(Name, "autoOn"))     autoOn = atoi(Value);
    else
      return false;
    return true;
}

void cLcdControlSetup::ParseConfigFile(){
	
	char datei[255];
    sprintf (datei, "%s/%s", LcdControlSetup.configDir, configFile);
    FILE *fp = fopen(datei, "r");
    char var[512],
         value[512],
         line[512];

    if (fp) {
        while (fgets(line, sizeof(line), fp)) {
           memset(var, 0, sizeof(var));
           memset(value, 0, sizeof(value));
           if (sscanf(line, "%[^ \t=]%*[\t ]=%*[\t ]%[^\n]", var, value) == 2) {
                
                if ( strcmp(var,"asmode16by9") == 0 ) strcpy(cmdAspectRatio16by9,value); 
                if ( strcmp(var,"asmodeauto") == 0 ) strcpy(cmdAspectRatioAuto,value);
                if ( strcmp(var,"asmode4by3") == 0 ) strcpy(cmdAspectRatio4by3,value);
                if ( strcmp(var,"asmode14by9") == 0 ) strcpy(cmdAspectRatio14by9,value);
                if ( strcmp(var,"asmodespectral") == 0 ) strcpy(cmdAspectRatioSpectral,value);
                if ( strcmp(var,"asmodefull") == 0 ) strcpy(cmdAspectRatioFull,value);
                if ( strcmp(var,"asmodezoom1") == 0 ) strcpy(cmdAspectRatioZoom1,value);
                if ( strcmp(var,"asmodezoom2") == 0 ) strcpy(cmdAspectRatioZoom2,value);
                if ( strcmp(var,"poweron") == 0) strcpy(cmdPoweron,value);
                if ( strcmp(var,"poweroff") == 0) strcpy(cmdPowerOff,value);
                if ( strcmp(var,"up") == 0) strcpy(cmdUp,value);
                if ( strcmp(var,"down") == 0) strcpy(cmdDown,value);
                if ( strcmp(var,"left") == 0) strcpy(cmdLeft,value);
                if ( strcmp(var,"right") == 0) strcpy(cmdRight,value);
                if ( strcmp(var,"ok") == 0) strcpy(cmdOk,value);
                if ( strcmp(var,"back") == 0) strcpy(cmdBack,value);
                if ( strcmp(var,"menu") == 0) strcpy(cmdMenu,value);                
           }
        }
        fclose(fp);
    }

}


