/*
 * <insert one-line description of what the program does>
 * Copyright (c) <2008-2009>, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
/*
 * Initial main.c file generated by Glade. Edit as required.
 * Glade will not overwrite this file.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <fcntl.h>
#include <sys/stat.h>
#include <gtk/gtk.h>
#include <signal.h>
#include "GUI/interface.h"
#include "GUI/support.h"
#include "eggtrayicon.h"
#include "GUI/tray.h"
#include "GUI/error.h"
#include "GUI/macro.h"
#include "GUI/resmgmt.h"
#include "InfoMgmt/InfoMgmt.h"
#include <unistd.h>//Add:zhangxian Date:20081230 
#include <sys/types.h>//Add:zhangxian Date:20081230 
#include <pwd.h>//Add:zhangxian Date:20081230 
#include <sys/file.h> //Add:zhangxian Date:20081230
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

GtkWidget *AgentWindow;
GtkWidget *WarningWindow;
GtkWidget *ProgressWindow;
GtkWidget *RebootWindow;
GtkWidget *AboutWindow;

int lockfd;
int sign ;
int g_enable_debug = 0;
char rootlockpath[30] = "/root/.tdlock";//char lockpath[FILEPATH_MAX_LEN] = "/tmp/theftdeterrent/.tdlock";//Add:zhangxian Date:20081230
char normalHome[80] ="/home/";//Add:zhangxian Date:20081230
char theft[25] = "/.tdlock";//Add:zhangxian Date:20081230
char lockpath[FILEPATH_MAX_LEN];//Add:zhangxian Date:20081230

void GetUserNameRemoveLockFile(void)
{
	char *name= getpwuid(geteuid())->pw_name;
	char *rname = "root";

	if(0==strcmp(name,rname))
	{
		system ("rm -f /root/theftdeterrent/.tdlock");
	}
	else
	{
	    char temp[80];
    	snprintf(temp,sizeof(temp),"%s",name);
    	snprintf(lockpath,sizeof(lockpath),strcat(strcat(normalHome,temp),theft));
	    system (strcat("rm -f ",lockpath));	
	}
}
void terminate(int signo)/*..SIGTERM..*/
{
     /* char tmp[LOG_MAX_LEN];

        memset(tmp,0,LOG_MAX_LEN);

        time_t timeNow;
        struct tm * pTmNow;
        time(&timeNow);
        pTmNow = localtime(&timeNow);

	fprintf(lockfile,"Program killed at %s\n",asctime(pTmNow));*/

	WritestrLog(MODULE_GUI,"program is terminated.");
	close(lockfd);
	GetUserNameRemoveLockFile();
	//system ("rm -f /tmp/theftdeterrent/.tdlock");//Add:zhangxian Date:20081230
	exit(0);
}
void sigint(int signo)
{
	WritestrLog(MODULE_GUI,"program is stoped by ctrl+c");
	close(lockfd);
	GetUserNameRemoveLockFile();
//	system ("rm -f /tmp/theftdeterrent/.tdlock");
	exit(0);
}
void sigsegv(int signo)
{
	WritestrLog(MODULE_GUI,"program meets segment fault.");
	close(lockfd);
	GetUserNameRemoveLockFile();
//	system ("rm -f /tmp/theftdeterrent/.tdlock");
	exit(0);
}

void sigquit(int signo)
{
	WritestrLog(MODULE_GUI,"program is quit");
	close(lockfd);
	GetUserNameRemoveLockFile();
//	system ("rm -f /tmp/theftdeterrent/.tdlock");
	exit(0);
}


#define GUI_ERR_AGENT_DISABLE -1

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

/* Only run once*/
   //mode_t fdmode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
   	umask(0000);
 	char *name= getpwuid(geteuid())->pw_name;
	const char *rname = "root";
	if( strcmp(name,rname)==0)
	{
		snprintf(lockpath,sizeof(lockpath),"%s",rootlockpath);
		
	}
	else
	{
		char temp[80];
    	snprintf(temp,sizeof(temp),"%s",name);
    	snprintf(lockpath,sizeof(lockpath),strcat(strcat(normalHome,temp),theft));

	}
    lockfd   =   open(lockpath,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR);
    if( lockfd   <   0)  
     {  
    	 return 1;
     }  
     else
     {
	  sign = flock(lockfd,LOCK_EX|LOCK_NB);
        if(sign < 0)
	   {

		  exit(0);
	 	 return 1;
	 
	    }
	 
      }

     OtherInfo_Debug(&g_enable_debug);
	
	//exit the program if the Enabled status is false
	OtherInfo otherInfo;
	OtherInfo_Construct(&otherInfo);
	if (! otherInfo.Enable ){
		WritestrLog(MODULE_GUI, "tcagent is disabled, now is exiting");
		return GUI_ERR_AGENT_DISABLE;
	}

	/*if ((lockfd = open(lockpath, O_RDONLY))!= -1)
	{
		
		WritestrLog(MODULE_GUI,"theftdeterrent may be already running!");
 		WritestrLog(MODULE_GUI,"if not, please remove /tmp/theftdeterrent/.tdlock file.");
printf(lockpath);
printf("In file %s func  %s, line:%d\n", __FILE__,__FUNCTION__, __LINE__);
		//ErrMsgBox(GUI_ERR_TWO_INSTANCE);
                return 1;
	}
  
   if ((lockfd = open(lockpath, O_CREAT|O_RDWR|O_EXCL, fdmode))== -1)
   {
	WritestrLog(MODULE_GUI,"theftdeterrent may be already running!");
	WritestrLog(MODULE_GUI,"if not, please remove /tmp/theftdeterrent/.tdlock file.");
	//ErrMsgBox(GUI_ERR_TWO_INSTANCE);
printf(lockpath);
printf("In file %s func  %s, line:%d\n", __FILE__,__FUNCTION__, __LINE__);
 	return 1;

   }
   else
   {
       write(lockfd,"theftdeterrent is running. \n",strlen("theftdeterrent is running. \n"));
   }*/


/* if (signal(SIGTERM,terminate) == -1)
 {
	  WritestrLog(MODULE_GUI,"fail in signal register!");
 printf("In file %s func  %s, line:%d\n", __FILE__,__FUNCTION__, __LINE__);
         return 1;
 }

 if (signal(SIGINT,sigint) == -1)
 {
	printf("In file %s func  %s, line:%d\n", __FILE__,__FUNCTION__, __LINE__);
  WritestrLog(MODULE_GUI,"fail in signal register!");
          return 1;
 }
 if (signal(SIGSEGV,sigsegv) == -1)

 {
printf("In file %s func  %s, line:%d\n", __FILE__,__FUNCTION__, __LINE__);
	  WritestrLog(MODULE_GUI,"fail in signal register!");
          return 1;
 }
 if (signal(SIGQUIT,sigquit) == -1)
 {
	printf("In file %s func  %s, line:%d\n", __FILE__,__FUNCTION__, __LINE__);
  WritestrLog(MODULE_GUI,"fail in signal register!");
          return 1;
 }
*/
  
#ifdef ENABLE_NLS
  bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);
#endif

  gtk_set_locale ();
  gtk_init (&argc, &argv);
  char pixmappath[FILEPATH_MAX_LEN ];
  getrespath(THEFTCONTROL_PIXMAPS,pixmappath);
  add_pixmap_directory (pixmappath);
  //add_pixmap_directory (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps");

  /*
   * The following code was added by Glade to create one of each component
   * (except popup menus), just so that you see something after building
   * the project. Delete any components that you don't want shown initially.
   */
  AgentWindow = create_AgentWindow ();
//  WarningWindow = create_WarningWindow ();
//  ProgressWindow = create_DownloadWindow ();
  RebootWindow = create_RebootWindow ();
  AboutWindow = create_AboutWindow ();

  WritestrLog(MODULE_GUI, "test entry");
//  gtk_widget_hide (AgentWindow);
  FormTCAgent();
//  if( TCAgentForm_Load())
//  	{
// 	printf("In file %s func  %s, line:%d\n", __FILE__,__FUNCTION__, __LINE__);
//
//	return 1;
// 	} modify:zhangxian date:2008-12-10
 gtk_window_set_resizable(GTK_WIDGET(AgentWindow), FALSE);
 //gtk_window_set_resizable(WarningWindow, FALSE);
// gtk_window_set_resizable(ProgressWindow, FALSE);
 gtk_window_set_resizable(GTK_WIDGET(RebootWindow), FALSE);
 gtk_window_set_resizable(GTK_WIDGET(AboutWindow), FALSE);
 //put the program into the system tray
 // create_tray ("normal.png",AgentWindow);

 UpdateUIbyStatus();//modify:zhangxian Date:20090109 Reason:start Agent quickly.//UpdateUIbyStatus();
 gtk_main ();
 return 0;
}
