/******************************************************************************

    MODUL
	CnvConfig.c

    DESCRIPTION
	Converts an old config-file into the actual version or prints an
	old version-file to stdout.

    NOTES

    BUGS

    TODO

    EXAMPLES

    SEE ALSO

    INDEX

    HISTORY
	30. Mar 1993	ada created

******************************************************************************/

/**************************************
		Includes
**************************************/
#include <exec/types.h>
#include <exec/nodes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pragmas/dos_pragmas.h>

#ifndef DOS_DOS_H
typedef void * BPTR;
#endif
/* typedef struct Node NODE; */

#include "global.h"
#include "config.h"


/**************************************
	    Globale Variable
**************************************/
extern struct DOSBase * DOSBase;


/**************************************
      Interne Defines & Strukturen
**************************************/


/**************************************
	    Interne Variable
**************************************/


/**************************************
	   Interne Prototypes
**************************************/
void convert_config0001 (void);
void print_config (void);


struct Config new;
struct Config0001 c1;
struct Config0002 c2;


void convert_config0001 (void)
{
    new.wwcol = c1.wwcol;
    new.winx = c1.winx;
    new.winy = c1.winy;
    new.winwidth = c1.winwidth;
    new.winheight = c1.winheight;
    new.iwinx = c1.iwinx;
    new.iwiny = c1.iwiny;
    new.aslleft = c1.aslleft;
    new.asltop = c1.asltop;
    new.aslwidth = c1.aslwidth;
    new.aslheight = c1.aslheight;
    new.tabstop = c1.tabstop;
    new.margin = c1.margin;
    new.fgpen = c1.fgpen;
    new.bgpen = c1.bgpen;
    new.hgpen = c1.hgpen;
    new.tpen = c1.tpen;
    new.bbpen = c1.bbpen;
    new.insertmode = c1.insertmode;
    new.ignorecase = c1.ignorecase;
    new.wordwrap = c1.wordwrap;
    new.autosplit = c1.autosplit;
    new.autoindent = c1.autoindent;
    new.autounblock = c1.autounblock;
} /* convert_config0001 */


void convert_config0002 (void)
{
    new.wwcol = c2.wwcol;
    new.winx = c2.winx;
    new.winy = c2.winy;
    new.winwidth = c2.winwidth;
    new.winheight = c2.winheight;
    new.iwinx = c2.iwinx;
    new.iwiny = c2.iwiny;
    new.aslleft = c2.aslleft;
    new.asltop = c2.asltop;
    new.aslwidth = c2.aslwidth;
    new.aslheight = c2.aslheight;
    new.tabstop = c2.tabstop;
    new.margin = c2.margin;
    new.fgpen = c2.fgpen;
    new.bgpen = c2.bgpen;
    new.hgpen = c2.hgpen;
    new.tpen = c2.tpen;
    new.bbpen = c2.bbpen;
    new.insertmode = c2.insertmode;
    new.ignorecase = c2.ignorecase;
    new.wordwrap = c2.wordwrap;
    new.autosplit = c2.autosplit;
    new.autoindent = c2.autoindent;
    new.autounblock = c2.autounblock;
} /* convert_config0002 */


void print_config (void)
{
    printf ("wwcol %d\n", new.wwcol);
    printf ("setgeometry %d %d %d %d\n",
	    new.winx, new.winy, new.winwidth, new.winheight);
    printf ("# iconpos %d %d\n", new.iwinx, new.iwiny);
    printf ("# aslgeometry %d %d %d %d\n",
		new.aslleft, new.asltop, new.aslwidth, new.aslheight);
    printf ("tabstop %d\n", new.tabstop);
    printf ("margin %d\n", new.margin);
    printf ("fgpen %d\n", new.fgpen);
    printf ("bgpen %d\n", new.bgpen);
    printf ("hgpen %d\n", new.hgpen);
    printf ("tpen %d\n", new.tpen);
    printf ("bbpen %d\n", new.bbpen);

    printf ("insertmode %s\n", new.insertmode ? "on" : "off");
    printf ("ignorecase %s\n", new.ignorecase ? "on" : "off");
    printf ("wordwrap %s\n", new.wordwrap ? "on" : "off");
    printf ("autosplit %s\n", new.autosplit ? "on" : "off");
    printf ("autoindent %s\n", new.autoindent ? "on" : "off");
    printf ("autounblock %s\n", new.autounblock ? "on" : "off");
} /* print_config */


void main (int argc, char ** argv)
{
    FILE * fh;
    char tmp_buffer[256];
    int out = 0;
    char * outfile = XDME_CONFIG;
    int current;

    if (argc == 2 && argv[1][0] == '?')
    {
usage:
	puts ("CnvConfig converts old config-files\n"
	      "Usage : CnvConfig [-p] [-o <file>]\n\n"
	      "Without arguments it asks whether to write the new config\n"
	      "back or prints it on stdout.\n"
	      "You can specify either -p or -o. -p prints the old config\n"
	      "to stdout. You can redirect this into a file and let XDME\n"
	      "source this file. Or -o <file> writes the new config to\n"
	      "<file>.");
	exit (0);
    }
    else if (argc > 1 && argv[1][0] == '-')
    {
	if (argv[1][1] == 'p')
	    out = 2;
	else if (argv[1][1] == 'o')
	{
	    if (argv[1][2])
		outfile = &argv[1][2];
	    else
	    {
		if (argc != 3)
		    goto usage;

		outfile = argv[2];
	    }

	    out = 1;
	}
	else
	    goto usage;
    }

    if (fh = fopen (XDME_CONFIG, "r"))
    {
	fread (tmp_buffer, sizeof(CONFIG_VERSION)-1, 1, fh);

	if (!strncmp (CONFIG0001, tmp_buffer, sizeof(CONFIG0001)-1) )
	{
	    printf ("# Found config \"" CONFIG0001 "\" ...\n");

	    fgetc (fh);
	    fread (&c1, 1, CONFIG0001_SIZE, fh);

	    convert_config0001 ();
	}
	else if (!strncmp (CONFIG0002, tmp_buffer, sizeof(CONFIG0002)-1) )
	{
	    printf ("# Found config \"" CONFIG0002 "\" ...\n");

	    fgetc (fh);
	    fread (&c2, 1, CONFIG0002_SIZE, fh);

	    convert_config0002 ();
	}
	else if (!strncmp (CONFIG_VERSION, tmp_buffer, sizeof(CONFIG0003)-1) )
	{
	    current = TRUE;

	    printf ("# Found current config ...\n");

	    fread (&new, 1, CONFIG_SIZE, fh);
	}
	else
	{
	    tmp_buffer[sizeof(CONFIG_VERSION)] = 0;

	    printf ("Unknown config-file \"%s\"\n", tmp_buffer);
	    exit (5);
	}

	fclose (fh);

	if (!out)
	{
	    printf ("Do you want\n%s"
		"\t(2) print config to stdout\n"
		"\t(3) exit without doing anything\n"
		":",
		current ? "" : "\t(1) convert \"" XDME_CONFIG "\" to \"" CONFIG_VERSION "\" \n"
		);

	    gets (tmp_buffer);
	    out = atoi (tmp_buffer);
	}

	switch (out)
	{
	case 1:
	    printf ("Copying \"" XDME_CONFIG "\" to \"" XDME_CONFIG ".bak\"\n");
	    DeleteFile (XDME_CONFIG ".bak");
	    Rename (XDME_CONFIG, XDME_CONFIG ".bak");

	    if (fh = fopen (outfile, "w"))
	    {
		printf ("Writing new config-file.\n");
		fwrite (CONFIG_VERSION, sizeof(CONFIG_VERSION)-1, 1, fh);
		fwrite (&new, CONFIG_SIZE, 1, fh);
		fclose (fh);
	    }
	    else
	    {
		printf ("Cannot open \"%s\" for writing.\n"
			"copying \"" XDME_CONFIG ".bak\" back\n" , outfile);
		Rename (XDME_CONFIG ".bak", XDME_CONFIG);
		exit (10);
	    }

	    break;

	case 2:
	    print_config ();
	}
    }
    else
    {
	printf ("Cannot open \"" XDME_CONFIG "\" for reading.\n");
	exit (10);
    }

    exit (0);
}


/******************************************************************************
*****  ENDE CnvConfig.c
******************************************************************************/
