/***************************************************************************
                          hacha_speed_join.c  -  description
                             -------------------
    begin                : dom sep 1 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.                                   *
 *                                                                         *
 ***************************************************************************/
#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 hacha_speed_join (char first_file_name[], char file_name[], char total_size[], char partial_size[], int pos_init_file, char control)
{
/* join files :P */
	/* returns 0 all correct
                    1 incorrect size any part
                    2 can't open file to write
                    3 can't open first_file
                    4 can't open any part
 */
    double size_file (char[]);
    double astod (char[]);
    void itoa (int,char[],int);
    
	int result = 0;
    int NumParts;
    FILE *current_file;
    FILE *real_file;
    double total_a,total;
    double partial;
	double auxF;
    char patron_name[Kfile_name_size];
    int leng_patron;
    char current[Kfile_name_size];
    char auxS[Kfile_name_size];
    int correct = 1;
 /*   int auxI,i=0;*/
/*    int bytte;*/
/*   char bytte;*/
    int count_file = 0;

    int *buffer;

    /* calculate number parts */
    NumParts = floor(atof(total_size) / atof(partial_size));
    auxF = (atof(total_size) / atof(partial_size)) - floor(atof(total_size) / atof(partial_size));
    if ( auxF > 0) NumParts++;

    /* calculate file size */
    total = atof(total_size);
    total_a = total;
    partial = atof(partial_size);

    buffer = (int *)calloc(partial,sizeof(int));
    
    /* get name of all files */
    strcpy(patron_name,first_file_name); /* copy first file name */
    patron_name[strlen(patron_name)-1] = '\0'; /* delete extension '0' */
    leng_patron = strlen(patron_name);
    strcpy(current,patron_name);

    /* GO! */

    current_file=fopen(first_file_name,"r");
    /*    current_file=fopen(first_file_name,"rb"); */
    if (current_file == NULL) /* check if is opened */
    {
			result = 3;
			if (first_file_name==NULL)
			{
				printf("\aError: file not opened, check if exist\n");
			}
			else
			{
				printf("\aError: file \"%s\" not opened, check if exist\n",first_file_name);
			}
	}
	else /* file is opened */
    {

			fseek(current_file,pos_init_file,SEEK_CUR); /* move poss in file */

			real_file=fopen(file_name,"w");
/*			real_file=fopen(file_name,"wb");*/
			if (real_file == NULL)
			{
				result = 2;
				if (file_name==NULL)
				{
					printf("\aError: splited file cannot be opened, check permission\n");
				}
				else
				{
					printf("\aError: splited file \"%s\" file cannot be opened, check permission\n",file_name);
				}
			}
			else
			{
				itoa(count_file,auxS,10);
				strcat(current,auxS); /* generate file name */

				printf("file: %s",current);

				/*  control file size */
				if (partial != (size_file(current) -  pos_init_file))
				{
					result = 4;
					printf("\a\nError: file \"%s\" incorrect size (%.0lf), stop join\n",current,size_file(current));
				}
				else
				{
					fread(buffer,1,partial,current_file);
					fwrite(buffer,1,partial,real_file);
					total_a = total_a - partial;


					fclose(current_file);
	
					
					current[leng_patron] = '\0';
					count_file++;
					printf(" - OK (%.2lf\%)\n",100-(total_a/total)*100);

					while( (count_file < (NumParts - 1)) && correct )
					{
						itoa(count_file,auxS,10);
						strcat(current,auxS);

						current_file=fopen(current,"r");
/*					current_file=fopen(current,"rb");*/
						if(current_file == NULL)
						{
							printf("\a\nError: file \"%s\" don't exist or hacha can't find/open it\n",current);
							correct = 0;
							result = 1;
						}
						else
						{
							printf("file: %s",current);
						

							if (partial != size_file(current))
							{
								result = 1;
								correct = 0;
								printf("\a\nError: file \"%s\" incorrect size (%.0lf), stop joined\n",current,size_file(current));
							}
							else
							{
				     			fread(buffer,1,partial,current_file);
								fwrite(buffer,1,partial,real_file);
								total_a = total_a - partial;

								fclose(current_file);
								current[leng_patron] = '\0';
								count_file++;
								printf(" - OK (%.2lf\%)\n",100-(total_a/total)*100);
							}

						}
						
					}

						if(correct && (count_file < NumParts))
						{
							
							itoa(count_file,auxS,10);
							strcat(current,auxS);

							current_file=fopen(current,"r");
							/*					current_file=fopen(current,"rb");*/
							if(current_file == NULL)
							{
								printf("\a\nError: file \"%s\" don't exist or hacha can't find/open it\n",current);
								result = 1;
							}
							else
							{
								printf("file: %s",current);

								if (total_a != size_file(current))
								{
									result = 1;
									correct = 0;
									printf("\a\nError: file \"%s\" incorrect size, stop joined\n",current);
								}
								else
								{
									fread(buffer,1,total_a,current_file);
									fwrite(buffer,1,total_a,real_file);
				
									total_a = 0;

									fclose(current_file);
									printf(" - OK (%.2lf\%)\n",100-(total_a/total)*100);
								}
							}
						}
				}
				fclose(real_file);
				free(buffer);
			}
		}

		return result;
}