<?php
   class MyDB extends SQLite3 {
      function __construct() {
         $this->open('/kirac/db/sungateway.db');
      }
   }

   $db = new MyDB();
   if(!$db) {
      echo $db->lastErrorMsg();
   }

   $db->exec("PRAGMA synchronous = OFF");

   $ip = $_POST['fip'];
   $mask = $_POST['fmask'];
   $gateway = $_POST['fgateway'];
   $serverip = $_POST['fserverip'];
   $serverport = $_POST['fserverport'];
   $topic = $_POST['ftopic'];
   $topicstatus = $_POST['ftopicstatus'];
   $loggerid = $_POST['floggerid'];
   $apn = $_POST['fapn'];
   $debugip = $_POST['fdebugip'];
   $debugport = $_POST['fdebugport'];
   $updateserverip = $_POST['fupdateserverip'];
   $updateserverport = $_POST['fupdateserverport'];
   
   if ($ip=="") $ip="0.0.0.0";
   if ($mask=="") $mask="0.0.0.0";
   if ($gateway=="") $gateway="0.0.0.0";
   if ($serverip=="") $serverip="0.0.0.0";
   if ($topic=="") $topic="mpdl/field-data";
   if ($topicstatus=="") $topicstatus="mpdl/device-status";
   if ($serverport=="") $serverport="0";
   if ($loggerid=="") $loggerid="0";
	if ($apn=="") $apn="internet";
	if ($debugip=="") $debugip="0.0.0.0";
	if ($debugport=="") $debugport="0";
   if ($updateserverip=="") $updateserverip="0.0.0.0";
	if ($updateserverport=="") $updateserverport="0";
	 
   $sql =<<<EOF
   UPDATE tblipconfig
   SET
   ip = '$ip',
   mask = '$mask',
   gateway = '$gateway',
   server_ip = '$serverip',
   server_port = $serverport,
   topic = '$topic',
   topicstatus = '$topicstatus',
   logger_id = '$loggerid',
   apn = '$apn',
   debug_ip = '$debugip',
   debug_port = $debugport,
   update_server_ip = '$updateserverip',
   update_server_port = $updateserverport;
EOF;


   while(1) {
     $ret = $db->query($sql);
     if ($ret) break;
     usleep(10000);
   }

   $db->close();

   /* set ip address & netmask */
   system('rm /etc/init.d/S42ipaddress',$sonuc);
   system('touch /etc/init.d/S42ipaddress;chmod +x /etc/init.d/S42ipaddress',$sonuc);
   $cmd =<<<EOF
   echo ifconfig eth0 '$ip' netmask '$mask' up  > /etc/init.d/S42ipaddress
EOF;
   system($cmd,$sonuc);

   /* set gateway */
   system('rm /etc/init.d/S43gateway',$sonuc);
   system('touch /etc/init.d/S43gateway;chmod +x /etc/init.d/S43gateway',$sonuc);
   $cmd =<<<EOF
   echo route add default gw '$gateway' eth0  > /etc/init.d/S43gateway
EOF;
   system($cmd,$sonuc);


?>
