/* All2LHA v0.3 basé entièrement sur lzh2lha de Christian Warren */
#include <stdio.h>
#include "exec/types.h"
#include "libraries/dos.h"
#include "exec/memory.h"
#include "fcntl.h"

#include "Prototypes.h"
#include "All2Lha.h"

struct DosBase *DOSBase;

VOID ecrit(string)
register UBYTE *string;
{
	Write(Output(), "[1m[31m\n", 10);
	Write(Output(), string, strlen(string));
	Write(Output(), "[0m\n", 5);
}

VOID ecritsansansi(string)
register UBYTE *string;
{
	Write(Output(), string, strlen(string));
}

VOID efface_fichier(file)
register UBYTE *file;
{
	ecrit("deleting the original file!!");
	DeleteFile(file);
}

VOID sortie(erreur)
register ULONG erreur;
{
	CloseLibrary((struct Library *) DOSBase);
	exit(erreur);
}

int main(argc, argv)
int argc;
char *argv[];
{
	register UWORD i, j, EFFACE = APRES_CREER_LHA;
	UBYTE sourcefile[120], filelha[120], a_exec[200], current_dir[120], comment[80];
	struct FileLock *lock;
	struct FileInfoBlock *info;
	int fichier;

	if (!(DOSBase = (struct DosBase *) OpenLibrary("dos.library", 0)))
		exit(1);

	ecritsansansi("\n[1m[33mALL2LHA v1.0  FreeWare[0m\n");
	getcwd(current_dir, 120);

	if (argc < 2)
	{
		ecritsansansi("Usage: all2lha [-options] file.lzh file.arc file.zoo file.zip file.arj...\n");
		ecritsansansi("       you'll need arc, zoo, unzip, unarj and LHA in a C: directory...\n");
		ecritsansansi("       Options\n");
		ecritsansansi("              -n Dont Delete the original archive.\n");
		ecritsansansi("              -a Delete the original archive before\n");
		ecritsansansi("                 the creation of the '.lha'.\n");
		ecritsansansi("              -n Delete the original archive after\n");
		ecritsansansi("                 the creation of the '.lha'. (Default).\n");
		sortie(0);
	}

	if (!getfa("t:all2lha_rep_temp") && (mkdir("t:all2lha_rep_temp")))
	{
		ecrit("Can not create temp directory!");
		sortie(2);
	}

	chdir("t:all2lha_rep_temp");
	i = 1;
	do
	{
		if (argv[i][0] == '-')
		{
			switch (argv[i][1])
			{
				case 'a':
					EFFACE = AVANT_CREER_LHA;
					break;
				case 'p':
					EFFACE = APRES_CREER_LHA;
					break;
				case 'n':
					EFFACE = N_EFFACE_PAS;
					break;
			}
			i++;
		}
		else
			break;
	} while (TRUE);

	while (i < argc)
	{
		strcpy(sourcefile, argv[i]);
		strmfe(filelha, argv[i], "lha");
		i++;

		strcpy(a_exec, "File -> ");
		strcat(a_exec, sourcefile);
		ecrit(a_exec);

		if (fichier = open(sourcefile, O_RDONLY, 0))
		{
			read(fichier, a_exec, 7);
			close(fichier);
			if (!strnicmp(&a_exec[2], "-lh5-", 5))
			{
				ecrit("The file is already in .lha!");
				ecrit("Renaming to .lha...");
				Rename(sourcefile, filelha);
				continue;
			}
		}

		if (getfa(sourcefile) == -1)
		{
			for (j = 0; j < NB_D_ARCHIVEUR; j++)
			{
				strcpy(a_exec, "Archiver -> ");
				strcat(a_exec, archiveur[j].Extension);
				ecrit(a_exec);

				if (!stricmp(&sourcefile[strlen(sourcefile) - strlen(archiveur[j].Extension)], archiveur[j].Extension))
				{
					strcpy(a_exec, archiveur[j].Desarchive);
					strcat(a_exec, " \"");
					strcat(a_exec, sourcefile);
					strcat(a_exec, "\"");
					ecrit("Un-archiving...");

					if (!system(a_exec))
					{
						if ((info = (struct FileInfoBlock *)
							  AllocMem(sizeof(struct FileInfoBlock), MEMF_PUBLIC | MEMF_CLEAR)))
						{
							if (lock = (struct FileLock *) Lock(sourcefile, SHARED_LOCK))
							{
								if (Examine((BPTR) lock, info))
									strcpy(comment, info->fib_Comment);
								UnLock((BPTR) lock);
							}
							FreeMem(info, sizeof(struct FileInfoBlock));
						}

						strcpy(a_exec, "lha -x -a -m -r -2 a \"");
						strcat(a_exec, filelha);
						strcat(a_exec, "\" #?");

						ecrit("Un-Protecting files...");
						system("Protect t:all2lha_rep_temp/#? +d ALL QUIET");

						if (EFFACE == AVANT_CREER_LHA)
							efface_fichier(sourcefile);

						ecrit("Archiving...");
						if (!system(a_exec))
						{
							SetComment(filelha, comment);
							if (EFFACE == APRES_CREER_LHA)
								efface_fichier(sourcefile);
							break;
						}
						else
							ecrit("Files where not archived correctly!");
					}
					else
						ecrit("File could not be un-Arc properly!");
				}
				else
					ecrit("File dont have the right extension!");
			}
			system("Delete t:all2lha_rep_temp/#? ALL QUIET");
		}
	}

	chdir(current_dir);
	system("Delete t:all2lha_rep_temp ALL QUIET");

	sortie(0);
}
