/*
* /giis/main.c The main procedure call.
*
* Copyright (C) 2005. G.Lakshmipathi.<lakshmipathi_g@rediffmail.com>
*
*/

#include "giis.h"

/*
* main() Responds to user inputs and calls appropriate procedure.
*/
 
int main(int argc,char *argv[])
{
int i,ans=-1,fp,line=1;
char a,x;
char sumkey[]="Enter Any key to Continue :-)";

do{
	if(argc>2){
	printf("giis : invalid option \n");
	printf("Type 'giis -h' for help \n");
	return 1;
	}
	
		/* process command line */

	if(argc==2){
	if(strcmp(argv[1],"-m")==0)	/* menu */
	ans=444;
	if(strcmp(argv[1],"-i")==0)	/* install */
	ans=0;
	if(strcmp(argv[1],"-u")==0)	/* update */
	ans=1;
	if(strcmp(argv[1],"-g")==0)	/* recover */	
	ans=2;
	if(strcmp(argv[1],"-d")==0)	/* System info. */	
	ans=3;
	if(strcmp(argv[1],"-f")==0)	/* giis info. */	
	ans=4;
	if(strcmp(argv[1],"-U")==0)	/* uninstall */	
	ans=5;
	if(strcmp(argv[1],"-a")==0)	/* author */	
	ans=6;
	if(strcmp(argv[1],"-t")==0)	/* quotes */	
	ans=7;
	if(strcmp(argv[1],"-q")==0)	/* quits */	
	ans=8;
	
	if(strcmp(argv[1],"-h")==0){	/* help */
	printf("\nUsage: giis [OPTION]\n");
	printf("\nPossible options available with giis :");
	printf("\n  -a\t\t Info. about the author.");
	printf("\n  -d\t\t Displays your file system informations.");
	printf("\n  -f\t\t Displays giis system files.");
	printf("\n  -g\t\t Recovers the Given or all deleted files.");
	printf("\n  -h\t\t Prints this menu.");
	printf("\n  -i\t\t Will perform installation of giis.");
	printf("\n  -m\t\t Menu based user interaction.");
	printf("\n  -q\t\t Exit giis");
	printf("\n  -t\t\t Some quotes and thoughts.");
	printf("\n  -u\t\t Updates giis to reflect current status.");
	printf("\n  -U\t\t Uninstalls giis.\n"); 
	printf("\nReport bugs to <lakshmipathi.g@gmail.com>\n");
	return 1;
	}
	if((ans!=444) &&  (ans<0 || ans>8)){
		printf("giis : invalid option \n");
		printf("Type 'giis -h' for help.\n");
		return 1;
		}

	
	}
	if(argc==1)					/* default is menu.*/
	ans=444;

	if(ans==444){
	system("clear");
	printf("\n\t\t ===================================================");
	printf("\n\t\t  gET iT i sAY. giis - Ext3/Ext2 File Undelete Tool ");
	printf("\n\t\t ===================================================\n");
	printf("\n\tPress 0 : Install \n\tPress 1 : Update Now\n\tPress 2 : File Recovery");
	printf("\n\tPress 3 : Your File System Details");
	printf("\n\tPress 4 : giis File Details \n\tPress 5 : Uninstall");
	printf("\n\tPress 6 : About giis ");
	printf("\n\tPress 7 : Quotes \n\tPress 8 : Exit");
	printf("\n\n\t\tEnter Your choice : ");
	scanf("%d",&ans);
	} 
	
	if(ans==4){
		fp=open(INSTALL_DIR1,0);
		if(errno==ENOENT){
		printf("\n\t\tgiis Not installed...\n");
		printf("\t\t\t...So install giis and then try this..\n\n ");
		close(fp);
		return 1;
		}
		close(fp);
		
		i=read_dir_info_file();
		CHECK
		printf("\n File Info File");
		i=read_file_info_file();
		CHECK

		return 1;
		}


	if(ans==8){
		printf("\n\t Quiting giis....");
		printf("\n\n\t Bye...Bye...!!!\n\n");
		return 1;
		}
		
	if(ans==5){
		i=remove_giis();
		CHECK
		return 1;
		}
		
	if(ans==6){
	
		system("clear");
		fp=open("/giis/hai",0);
		CHECK
		i=read(fp,&a,1);
		CHECK
		while(i>0){
		write(1,&a,1);
		i=read(fp,&a,1);
		}
		close(fp);
		return 1;
		}
	if(ans==7){
	
		system("clear");
		fp=open("/giis/quotes",0);
		CHECK
		i=read(fp,&a,1);
		CHECK
			while(i>0){
			if(a=='\n')
			line++;
			write(1,&a,1);
			i=read(fp,&a,1);
			if((line%24)==0){
			line++;
			write(1,sumkey,strlen(sumkey));
			read(0,&x,1);
			}
			}
		close(fp);
		return 1;
		}

			
		

}while(!(ans>=0 && ans<=8));

	/* Check for installation */
	if(ans==0){
	install=1;	 
	fp=open(INSTALL_DIR1,0);
	if(fp!=-1){
		printf("\n\t\tgiis already installed in your system...\n");
		printf("\t\tTry giis -U or manually delete /giis to install freshly...\n ");
		close(fp);
		return 2;
		}
	}
	
	if(ans==1 || ans==2 || ans==3){
	fp=open(INSTALL_DIR1,0);
	if(errno==ENOENT){
		printf("\n\t\tgiis Not installed...\n");
		printf("\t\t\t...So install giis and then try this ;)\n ");
		close(fp);
		return 1;
	}
	close(fp);
	}
	
	printf("\nSearching for file system....");
	if(init()==-1){
		printf("\n\nFile System not is Initialized....Aborting....\n");
		exit(1);	
	}
		
if(ans==0)
{
	install=1;update_file=0;/* Used to call update_dir_info_file() */
	update_dir=0;
	fs.inode_number=ROOT_INODE; /* Get Root inode offset to fs.root_inode_offset.First.*/
	i=find_inode_offset();
	CHECK
	
	i=read_inode();
	CHECK

	i=set_content_offset();
	CHECK
	if(dir){ 	/* If inode is a directory then Show its content  */
		i=read_dir();
		CHECK
		i=show_dir();     
		CHECK
		}
}

			/* Display File System Details */
	
	if(ans==3){
		fs.inode_number=ROOT_INODE; /* Get Root inode offset */
		i=find_inode_offset();
		CHECK
		i=fs_display();
		CHECK
		return 1;
	}


if(ans==0 || ans==1){
if(ans==1){
install=0;
update_file=1;update_dir=1;
}

i=search4dir();		    /* searches for directory in DIR_INFO_FILE */	
CHECK
	if(ans==0){
	get_sample_data();
	printf("\n\n\t\tgiis - Installtion completed...");
	}
}

	
if(ans==2){

	
	printf("\n\nHow do you want to recover files?");
	printf("\n\t Press 0 : Get All User files ");
	printf("\n\t Press 1 : Get Specific User files");
	printf("\n\t Press 2 : force giis - giis dump");
	printf("\n\t Press 3 : Get By File Format\n");
	printf("\n\t\tEnter Your Choice : ");
	scanf("%d",&ans);
			
		if(ans>=0 && ans <=3)
		use_uid=ans;
		else{
		printf("\n\n\t\tPlease Enter correct Choice");
		return 1;
		}
				
		install=0;
		get_it();			/* Search for deleted file */
	}		
return 1;
}
