#include "goahead.h"
#include <stdio.h>  
#include <fcntl.h>  
//#include <sys/types.h>  
//#include <sys/stat.h>  
//#include <sys/ioctl.h>  
#include <time.h>  
//#include <linux/rtc.h>  
//#include <linux/capability.h>
#include "sysinfo.h"

static const char *rtcname;

int web_get_sys_para(int eid, Webs *wp, int argc, char **argv)
{
 char *sec_name,*key_name;
 char var[128];
 if(jsArgs(argc,argv,"%s %s",&sec_name,&key_name)<2) 
  {
	websError(wp,400,"Insufficient args\n");
	return -1;
  }
 ini_gets(sec_name,key_name,"   ",var,sizeof(var),sys_cfg_file);
 return (int) websWrite(wp,"\"%s\"",var);
}

void sys_reboot(Webs *wp)
{
 system("reboot");//系统设置	
}

void ethx_process(Webs *wp)
{
/*第11行开始 
auto eth0
iface eth0 inet static
address 192.168.1.199
netmask 255.255.255.0
network 192.168.1.0
gateway 192.168.1.1
*/
 char *str1,*str2,*str3;
 char ipaddr[32];
 char netmask[32];
 char gateway[32];
 char cmd[128];
 
 ini_gets("SYSCFG","ip1","192.168.1.199",ipaddr,sizeof(ipaddr),sys_cfg_file);
 ini_gets("SYSCFG","netmask1","255.255.255.0",netmask,sizeof(netmask),sys_cfg_file);
 ini_gets("SYSCFG","gateway1","192.168.1.1",gateway,sizeof(gateway),sys_cfg_file);
 str1=websGetVar(wp,"ip1",ipaddr);
 if(str1!=NULL)
   {
     if((str2=websGetVar(wp,"netmask1",netmask))!=NULL)
       if((str3=websGetVar(wp,"gateway1",gateway))!=NULL) 
         {    
		  //printf("%s %s %s \n",ipaddr,netmask,gateway);      
          sprintf(cmd,"sed -i '13c address %s' /etc/network/interfaces",str1);
          system(cmd);
		  sprintf(cmd,"sed -i '14c netmask %s' /etc/network/interfaces",str2);
          system(cmd);
		  sprintf(cmd,"sed -i '16c gateway %s' /etc/network/interfaces",str3);
          system(cmd);
		  ini_puts("SYSCFG","ip1",str1,sys_cfg_file);
		  ini_puts("SYSCFG","netmask1",str2,sys_cfg_file);
		  ini_puts("SYSCFG","gateway1",str3,sys_cfg_file);	
         }
   }
 ini_gets("SYSCFG","ip2","192.169.1.199",ipaddr,sizeof(ipaddr),sys_cfg_file);
 ini_gets("SYSCFG","netmask2","255.255.255.0",netmask,sizeof(netmask),sys_cfg_file);
 ini_gets("SYSCFG","gateway2","192.169.1.1",gateway,sizeof(gateway),sys_cfg_file);
 str1=websGetVar(wp,"ip2",ipaddr);
 if(str1!=NULL)
   {
     if((str2=websGetVar(wp,"netmask2",netmask))!=NULL)
       if((str3=websGetVar(wp,"gateway2",gateway))!=NULL) 
         {
          sprintf(cmd,"sed -i '20c address %s' /etc/network/interfaces",str1);
          system(cmd);
		  sprintf(cmd,"sed -i '21c netmask %s' /etc/network/interfaces",str2);
          system(cmd);
		  sprintf(cmd,"sed -i '23c gateway %s' /etc/network/interfaces",str3);
          system(cmd);
		  ini_puts("SYSCFG","ip2",str1,sys_cfg_file);
		  ini_puts("SYSCFG","netmask2",str2,sys_cfg_file);
		  ini_puts("SYSCFG","gateway2",str3,sys_cfg_file); 	
         }     
   }
 websRedirect(wp,"/system.jst"); 	
}

int save_sntp_server(char *server)
{
 char cmd[128];
 sprintf(cmd,"sed -i '8c SNTP_SERVER=\"%s\"' /usr/sbin/ntpdate",server);
 system(cmd);
 ini_puts("SYSCFG","sntp_server",server,sys_cfg_file);
 return 0;	 
}

void sntp_process(Webs *wp)
{
 char *str;
 char sntp_server[65];
 ini_gets("SYSCFG","sntp_server","192.168.1.11",sntp_server,sizeof(sntp_server),sys_cfg_file);
 str=websGetVar(wp,"sntp_server",sntp_server);
 if(str!=NULL)
   {
     save_sntp_server(str);
   }
 websRedirect(wp,"/system.jst");	
}


//设置系统时钟 
int SetSysDateAndTime(struct tm *time_tm)  
{  
    struct timeval  time_tv;  
    time_t          timep;  
        int         ret;           
    //time_tm->tm_year     -= 1900;  
    //time_tm->tm_mon      -= 1;  
    time_tm->tm_wday     = 0;  
    time_tm->tm_yday     = 0;  
    time_tm->tm_isdst    = 0;  
  
    timep = mktime(time_tm);  
    time_tv.tv_sec  = timep;  
    time_tv.tv_usec = 0;  
  
    ret = settimeofday(&time_tv, NULL);  
    if(ret!=0)  
    {  
      fprintf(stderr,"settimeofday failed\n");  
      return -2;  
    }       
    return 0;  
}  

/*
static int rtc_xopen(const char **default_rtc,int flags)  
{  
 int rtc;  
  
 if(!*default_rtc) {  
   *default_rtc = "/dev/rtc";  
   rtc = open(*default_rtc, flags);  
   if (rtc >= 0)  
      return rtc;  
   *default_rtc = "/dev/rtc0";  
   rtc = open(*default_rtc, flags);  
   if (rtc >= 0)  
      return rtc;  
   *default_rtc = "/dev/misc/rtc";  
 }  
  
 return open(*default_rtc,flags);  
}  
  
//utc=0本地时间,否则格林威治时间  
static void write_rtc(time_t t,int utc)  
{    
#define RTC_SET_TIME _IOW('p',0x0a,struct rtc_time)  
 struct tm *timetm;  
 int rtc = rtc_xopen(&rtcname,O_WRONLY);  
  
 timetm = (struct tm *)(utc?gmtime(&t):localtime(&t));  
 timetm->tm_isdst = 0;  
  
 ioctl(rtc,RTC_SET_TIME,timetm);  
  
 close(rtc);  
}  */

//utc>0格林威治时间,否则本地时间 
//系统时间同步到硬件时钟 
void SetHWClockFromSysClock(int utc)  
{  
  //struct timeval tv;    
  //gettimeofday(&tv,NULL);    
  //write_rtc(tv.tv_sec,utc);  
  system("hwclock -w");
}

void rtc_process(Webs *wp)
{
 char *str;
 time_t now;
 unsigned int i;
 struct tm uptime;
 str=websGetVar(wp,"year","2015");
 if(str!=NULL)
 {
  i=atoi(str);
  uptime.tm_year=i-1900;
 } 
 str=websGetVar(wp,"month","9");
 if(str!=NULL)
 {
  i=atoi(str);
  uptime.tm_mon=i-1;
 }
 str=websGetVar(wp,"day","1");
 if(str!=NULL)
 {
  i=atoi(str);
  uptime.tm_mday=i;
 }  
 str=websGetVar(wp,"hour","0");
 if(str!=NULL)
 {
  i=atoi(str);
  uptime.tm_hour=i;
 } 
 str=websGetVar(wp,"minute","0");
 if(str!=NULL)
 {
  i=atoi(str);
  uptime.tm_min=i;
 } 
 str=websGetVar(wp,"second","0");
 if(str!=NULL)
 {
  i=atoi(str);
  uptime.tm_sec=i;
 }
 SetSysDateAndTime(&uptime);
 SetHWClockFromSysClock(1);//表示格林威治时间 
 //now=mktime(&uptime);	
 websRedirect(wp,"/system.jst");	
}

void ied_rename_process(Webs *wp)
{
 char *str;
 char old_name[65];
 char cmd[128];
 ini_gets("SYSCFG","ied_name","TIED01",old_name,sizeof(old_name),sys_cfg_file);
 str=websGetVar(wp,"ied_name",old_name);
 if(str!=NULL)
  {
    if(strlen(str)>2)
	  {
	  	sprintf(cmd,". /mk_ied.sh %s",str);
	  	ini_puts("SYSCFG","ied_name",str,sys_cfg_file);
  	    system(cmd);//执行命令	
  	  }	
  }
  websRedirect(wp,"/iedrename.jst");	
}