#pragma -         /* Maxon-Cpp 1.0 Einstellung fuer C-Mode :-) */

/* $VER: TGZ 1.1 (03/09/1997) - Tar Archiver and GZip Script in one command  */
/* (C) 1997 by Finn Jacobs. Feel free to copy. Don't try to manipulate this  */
/* becoz' if you program something in this source-code it would cause bugs   */
/* forever ! :-)                                                             */

#include <clib/dos_protos.h>
#include <stdio.h>
#include <stdlib.h>
#include <own_string.h>
#include <string.h>

void main(int argc, char *argv[])
{
	int x = 1;

	int i, j=1, done=1;
	long ret;
	char string[2048], dummy[2048];
	char zeichen;
	FILE *filehandle;

	printf("\nTGZ 1.1 (03/09-1997) written by Finn Jacobs <fjacobs@computerlabor.math.uni-kiel.de>\n");
	if (argc < 3 )
	{
		printf("Usage: \033[1mTGZ <archivefile> <directory> [<file1>,<file2>,...]\033[0m\n\n");
		printf("       Examples:\n");
		printf("               \033[1mtgz test ram:\033[0m\n");
		printf("               (creates an archive 'test.tgz' with contents of RAM:)\n\n");
		printf("               \033[1mtgz test C:List C:Copy Devs:System-Configuratio\033[0m\n");
		printf("               (creates an archive 'test.tgz' with the files C:List, C:Copy\n");
		printf("                and Devs:System-Configuration)\n\n");
		printf(" If exists an archive with the name test.tgz and you don't want to delete it\n");
		printf(" by pressing <n>o then an archive name 'test.tar.gz' will be created.\n\n");
		if ((argc > 1) && (argc < 3)) printf("\nnot enough arguments!\n");
		exit(5);
	}

	printf("Adding files ... ");
	strcpy(dummy,"TAR -cf ");

	strcpy(string,"");
	sprintf(string, "%s.TAR ", argv[1]);
	stringcat(dummy, string);
	j++;
	
	for (i = j; i < argc; i++)
	{
		strcpy(string,"");
		sprintf(string, "%s ", argv[i]);
		stringcat(dummy, string);
	}

	if (x == 0) printf("%s\n", dummy); else Execute(dummy, 0,0);

	printf("Done!\nCrunching archivefile ... ");
	strcpy(dummy, "gzip ");
	strcpy(string,"");
	sprintf(string, "%s.TAR", argv[1]);
	stringcat(dummy, string);

	if (x == 0) printf("%s\n", dummy); else Execute(dummy, 0,0);

	printf("Done!\n");

	strcpy(string, "");
	sprintf(string, "%s.TGZ", argv[1]);
	filehandle = fopen(string, "rb");
	if (!(filehandle == 0))
	{
		strcpy(dummy, "");
		sprintf(dummy, "Ein Archiv mit dem Namen %s existiert bereits. Ueberschreiben (y/n) ?", string);
		printf("%s ", dummy);

		while (done == 1)
		{
			zeichen = (char) getchar();
			switch (zeichen) {
			 case 'y' :	{
						fclose(filehandle);
						strcpy(string, "");
						sprintf(string, "delete %s.TGZ", argv[1]);
						if (x == 0) printf("%s\n", string); else Execute(string, 0,0);
						strcpy(string, "");
						sprintf(string, "rename %s.TAR.gz %s.TGZ", argv[1], argv[1]);
						if (x == 0) printf("%s\n", string); else Execute(string, 0,0);
						printf("%s.TGZ wurde erstellt !\n", argv[1]);
						exit(5);
					}
			 case 'n' :	{
						fclose(filehandle);
						printf("%s.TAR.GZ wurde erstellt !\n", argv[1]);
						exit(5);
					}
			}
		}
	}
	strcpy(string, "");
	sprintf(string, "rename %s.TAR.gz %s.TGZ", argv[1], argv[1]);
	if (x == 0) printf("%s\n", string); else Execute(string, 0,0);
	printf("%s.TGZ wurde erstellt !\n", argv[1]);
}


/* TAR   -       TAR Archiver (Unix)                                */
/* GZIP  -       GZIP Compression Tool 1.2.4 (Unix)                 */
/* UnTGZ -       UnTGZ Extract-Tool 1.3 (Unix-Like)                 */
/*       -       All Software is free and in the archive included.  */

