/***************************************************************************
                          main.c  -  description
                             -------------------
    begin                : vie ago 23 21:36:46 CEST 2002
    copyright            : (C) 2002 by deabru
    email                : deabru@nubbis.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


#define Kiocont 5 /* pos of que control bit */
#define Kioname 14/* when init the file cuted name */
#define Kfile_name_size 256 /* max file name size */
#define Ktamnum 30 /* max number size */

int main(int argc, char *argv[])
{
/* definitions */
    int get_information (char[], char[], char[], char[], int *, char *);
    char control_information ( char[], char[], char[]);
    void show_help();
    void itoa (int,char[],int);
    void iftoa (double ,char []);
    int hacha_join (char[], char[], char[], char[], int, char);
    int hacha_speed_join (char [], char [], char [], char [], int , char );
    int hacha_speed_split (char [], char [], double , double );
    int hacha_split (char [], char [], double , double );
    double astod (char[]);
    double size_file (char[]);
    int sizetopart(double,double);
    double parttosize(double, int);
    void rmextension(char [], char[]);

    
/* variables and arrays */

    char file_name[Kfile_name_size];
    char master_name[Kfile_name_size];
    char total_size[Ktamnum];
    char partial_size[Ktamnum];
    char parts[Ktamnum];
/*    char auxS[Kfile_name_size];*/
    int pos_init_file;
    char control;
    int flag = 0;
    int control_parts = 0;
    int control_master = 0;
    int control_low_mem = 0;
    char auxC;
    int auxI;
    int result = 1;
    int i;
/*    double auxD;*/
/* init code */

    if (argc <2)
    {
			printf("\nHACHA for linux version %s, (nubbis GPL utility for split/join big files):\n\n",VERSION); 	printf("\tusage: hacha <command> [options] <file>\n");
			printf("\thacha --help, hacha -h or hacha h for more options\n\n");
			result = 1;
    }
    else
    {
			printf("\n");
			if(argv[1][0] != '-' )
			{
				if ( strlen(argv[1]) == 1)
				{
				switch (argv[1][0])
				{
					case 'i':
					case 'I':
					putchar('\n');
					if (argc < 3)
					{
						printf("\n\aError, usage: hacha i <file.0>\n\n");
					}
					else
					{
						if (argc < 4 && argv[2][0]!='-') /* i is used without another switches */
						{
							if(!get_information(argv[2],file_name,total_size,partial_size,&pos_init_file,&control))
							{
								/* test if file exist and another things */
								printf("HACHA for linux information:\n\n");
								printf("\tFile Name = %s\n",file_name);
								auxI = (atof(total_size) / atof(partial_size));
								if (atof(total_size) / atof(partial_size)) auxI++;
								printf("\tTotal Parts = %d\n",auxI);
								printf("\tTotal Size = %s\n",total_size);
								printf("\tPartial Size = %s\n",partial_size);
								printf("\tPos Init File = %d\n",pos_init_file);
								auxC = control_information(file_name,total_size, partial_size);
								printf("\tControl Bit = %c Control self = %c\n",control,auxC);

								if (control == auxC)
								{
									printf("\nControl bit correct, check info correct!\n\n");
								}
								else
								{
									printf("\a\nIncorrect control bit, Check info failed...\n\n");
								}

								result = 0;
							}
							else /* file don't exist */
							{
								putchar('\n');
								result = 1;
							}
						}
					}
					break;

					case 'h':
					case 'H':
					show_help();
					break;

					case 'j':
					case 'J':

					if (argc < 3)
					{
						printf("\aError, usage: hacha j <file.0>\n\n");
					}
					else
					{
						printf("HACHA for linux version %s:\n\n",VERSION);
						if (argc < 4 ) 
						{
							if(!get_information(argv[2],file_name,total_size,partial_size,&pos_init_file,&control))
							{
								/* test if file exist and another things */
								printf("Check info: ");

								auxC = control_information(file_name,total_size, partial_size);
								if (control == auxC)
								{
									printf("Control bit correct, check info correct!\n");
									printf("Join %s of %d parts\n",file_name,sizetopart(astod(total_size),astod(partial_size)));
									if (!hacha_speed_join(argv[2],file_name,total_size,partial_size,pos_init_file,control))
									{
										printf("\njoined complete... \"%s\" created\n",file_name);
										result = 0;
									}
									else
									{
										printf("\a\nError: joined incomplete... \"%s\" not created\n",file_name);
										result = 1;
									}

								}
								else
								{
									printf("\aIncorrect control bit, Check info failed...\n\n");
									result = 1;
								}

							}
							else /* file don't exist */
							{
								putchar('\n');
								result = 1;
							}
						}
						else
						{
							if (argc < 5)
							{
								if (!strcmp(argv [2],"-l") || !strcmp(argv [2],"-L"))
								{
							
									if(!get_information(argv[3],file_name,total_size,partial_size,&pos_init_file,&control))
									{
										/* test if file exist and another things */
										printf("Check info: ");

										auxC = control_information(file_name,total_size, partial_size);
										if (control == auxC)
										{
											printf("Control bit correct, check info correct!\n");
											printf("Join %s of %d parts, low men activate... join process could be slow\n",file_name,sizetopart(astod(total_size),astod(partial_size)));
											if (!hacha_join(argv[3],file_name,total_size,partial_size,pos_init_file,control))
											{
												printf("\njoined complete... \"%s\" created\n",file_name);
												result = 0;
											}
											else
											{
												printf("\a\nError: joined incomplete... \"%s\" not created\n",file_name);
												result = 1;
											}

										}
										else
										{
											printf("\aIncorrect control bit, Check info failed...\n\n");
											result = 1;
										}
										
									}

								}
								else
								{
									printf("Error,  switch not found. Type hacha --help, hacha -h or hacha h for help\n\n");
									result = 1;
								}
							}
							else
							{
								printf("\aError: too many options, type hacha --help, hacha -h or hacha h for help\n\n");
							}
						}
					}
					break;

					/* split file */
					case 's':
					case 'S':

					printf("HACHA for linux version %s:\n\n",VERSION);
					if (argc < 3)
					{
						printf("\aError, usage: hacha s <file.0>\n\n");
					}
					else
					{
						flag = 0;
						strcpy(file_name,argv[2]);
						strcpy(partial_size,"1.44M");
						rmextension(file_name,master_name);
						result = 0;
						
						if( argc < 4)
						{
							flag = 1;
						}
						else
						{
							i = 2;
							while ( !result &&( i < argc))
							{
								if (argv[i][0] !='-')
								{
									strcpy(file_name,argv[i]);
									flag = 1;
									result = 1;
								}
								else
								{
									switch (argv[i][1])
									{
										case 'n':
										case 'N':
										case 'm':
										case 'M':
											if (argc <(i+1))
											{
												printf("Error,  type hacha --help, hacha -h or hacha h for help\n\n");
												result = 1;
											}
											else
											{
												control_master = 1;
												strcpy(master_name,argv[i+1]);
												i++;
											}
											break;
											
											case 's':
											case 'S':
											if (argc < (i+1))
											{
												printf("Error,  type hacha --help, hacha -h or hacha h for help\n\n");
												result = 1;
											}
											else
											{
													strcpy(partial_size,argv[i+1]);
													i++;
											}
											break;
											

											case 'p':
											case 'P':
											if (argc < (i+1))
											{
												printf("Error,  type hacha --help, hacha -h or hacha h for help\n\n");
												result = 1;
											}
											else
											{
												control_parts = 1;
												strcpy(parts,argv[i+1]);
												i++;
											}
											break;


											case 'l':
											case 'L':

											control_low_mem = 1;

											break;
											
											default:
											printf("Error,  switch not found. Type hacha --help, hacha -h or hacha h for help\n\n");
											result = 1;
											break;

									}
								}
								i++;
							}						
						}

						result = 1;
						
						if (control_parts)
						{
							iftoa(parttosize(size_file(file_name), atoi(parts)),partial_size);
						}
						if (!control_master)
						{
							rmextension(file_name,master_name);
						}

					/*	printf("fefe");
						printf("Split %s in %d parts\n",file_name,sizetopart(size_file(file_name),astod(partial_size)));
						printf("flag = %d",flag);
						printf("file_name = %s\nmaster_name = %s\nsize_file = %lf\npartial_size = %lf\n",file_name,master_name, size_file(file_name), astod(partial_size));
						*/
						if (flag)
						{
							printf("Split %s in %d parts",file_name,sizetopart(size_file(file_name),astod(partial_size)));

							if (control_low_mem)
							{
								printf(", low men option activated. Split process could be slow\n");
								if (!hacha_split (file_name,master_name, size_file(file_name), astod(partial_size)))
								{
									printf("\nsplit complete... \"%s\" splited. %d files created\n",file_name,sizetopart(size_file(file_name),astod(partial_size)));
									result = 0;
								}
								else
								{
									printf("\a\nError: split incomplete... \"%s\" not splited\n",file_name);
									result = 1;
								}
							}
							else
							{
								printf("\n");
								if (!hacha_speed_split (file_name,master_name, size_file(file_name), astod(partial_size)))
								{
									printf("\nsplit complete... \"%s\" splited. %d files created\n",file_name,sizetopart(size_file(file_name),astod(partial_size)));
									result = 0;
								}
								else
								{
									printf("\a\nError: split incomplete... \"%s\" not splited\n",file_name);
									result = 1;
								}
							
							}
						}
					}
					break;

					default:
					printf("\aError: Unknow command \"%s\", type hacha --help, hacha -h or hacha h for help\n\n",argv[1]);
					break;
				}
				}
				else
				{
					printf("\aError: Unknow command \"%s\", type hacha --help, hacha -h or hacha h for help\n\n",argv[1]);
				}
			}
			else
			{
				if (argc < 3)
				{
					switch (argv[1][1])
					{
						case 'h':
						show_help();
						break;

						case '-':
						if (!strcmp(argv[1],"--help"))
						{
							show_help();
						}
						else
						{
							printf("\aError: Unknow switch \"%s\", type hacha --help, hacha -h or hacha h for help\n\n",argv[1]);		
						}
						break;

						default:
						printf("\aError: Unknow switch \"%s\", type hacha --help, hacha -h or hacha h for help\n\n",argv[1]);
						break;

						
					}
				}
				else
				{
					printf("\aError: too many options, type hacha --help, hacha -h or hacha h for help\n\n");
				}
			}
		}
		return result;
	

/*  printf("Hello, world!\n");

  return EXIT_SUCCESS;*/
}
