#define PROGVERSION  "0.04"
#define PROGDATE     "23.09.96"
#define PROGNAME     "GEDMake"

/*
    GEDMake © 1996 by CTS

    re-insert TABs in a makefile
    (ie convert 8 Spaces at the beginning of a line to one 'real' TAB)

    HISTORY:

    0.01 17.09.96  initial version, file and error handling
        -19.09.96  open infile and outfile, copy infile to outfile
    0.02 19.09.96  detect begin-of-line
    0.03 21.09.96  count to 8 spaces, convert to one TAB, TABcount
    0.04 23.09.96  usage, prepare for Aminet release

    todo           variable # of spaces (standard GED: 4 spaces for one TAB)
*/

/*+ "std includes" */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*- */
const char Version[] = "$VER: " PROGNAME " " PROGVERSION " (" PROGDATE ")"
" © 1996 Christian T. Steigies";
/*+ "declarations" */
int main( int argc, char *argv[] );
#include "usage.h"
#include "error.h"
/*- */
/*+ "int main ( argc, *argv )" */
int main ( int argc, char *argv[] )

{
    FILE *in,*out;
    char zeichen;
    int beginline;
    int spaces = 0, count = 8, tabcount = 0;
    int i;
/*+ "Copyright" */
    printf ("%s\n",Version+6);
    puts ("Freeware, NO commercial usage ;-)\n");
    puts ("This program comes without ANY WARRANTY, use at your own risk!\n");
/*- "Copyright" */
/*+ "Argumente überprüfen" */
    if (argc < 1)
    {
/*        puts("WOW! less than 1 args! How did you do that?\n"); */
        atexit(&usage);
        atexit(&help);
        exit(EXIT_SUCCESS);
    } /* if (argc < 1) */

    if (argc == 1)
    {
/*        puts("ONE arg...not much\nShall I assume Makefile as <infile>?\n"); */
        atexit(&usage);
        atexit(&help);
        exit(EXIT_SUCCESS);
    } /* if (argc == 1) */

    if (argc == 2)
    {
/*        puts("TWO args...select outfile with filerequester, soon...\n"); */
        atexit(&usage);
        atexit(&help);
        exit(EXIT_SUCCESS);
    } /* if (argc == 2) */

    if (argc == 3)
    {
/*         puts("THREE args...should be sufficient\n"); */
/*
        atexit(&usage);
        atexit(&help);
        exit(EXIT_SUCCESS);
*/
    } /* if (argc == 3) */

    if (argc > 3)
    {
/*        puts("more than THREE args...I don't need THAT many args\n"); */
        atexit(&usage);
        atexit(&help);
        exit(EXIT_SUCCESS);
    } /* if (argc > 2) */
/*- "Argumente überprüfen" */
/*+ "Ein- und Ausgabefiles öffnen" */
    if  ( ( in = fopen(argv[1],"r") ) == 0 )
    {
        atexit(&usage);
        atexit(&fin_error);
        exit(EXIT_SUCCESS);
    } /* Eingabefile zum lesen öffnen */

    if  ( (out = fopen(argv[2],"r") ) == 0 )
    {
        if  ( ( out = fopen(argv[2],"w") ) == 0 )
        {
            atexit(&usage);
            atexit(&fout_error);
            exit(EXIT_SUCCESS);
        }
    } /* Ausgabefile zum schreiben öffnen */
    else
    {
        atexit(&usage);
        atexit(&fout_exists);
        exit(EXIT_SUCCESS);
    } /* file existiert schon! */

/*- "Ein- und Ausgabefiles öffnen" */
/*+ "main loop" */
    zeichen = fgetc(in);
    beginline = 1; /* erste Zeile fängt mit Zeilenanfang an */
    while ( !feof(in) )
    {
        if ( beginline == 1 )
        {
            if ( zeichen == ' ' )
            {
                spaces++;
            }
            else if ( spaces > 0 )
            {
                for (i=1; i <= spaces; i++)
                {
                    fputc(' ',out);
                }
                spaces = 0;
            } /* schon spaces gelesen, nicht space folgt -> spaces ausgeben! */
              /* schöner mit while-Schleife möglich, ohne Variable i */
        }

        if ( spaces == 0 )
        {
            beginline = 0;
        } /* solange spaces gelesen werden gilt Zeilenanfang */

        if ( zeichen == '\n'  )
        {
            beginline = 1;
            spaces = 0;
        } /* Zeilenanfang gilt ab newline Zeichen */

        if ( spaces == 0)
        {
            fputc(zeichen,out);
        } /* wenn kein space gelesen normale Ausgabe */
        else
        {
            if ( spaces >= count )
            {
                spaces -= count;
                fputc('\t',out);
                tabcount++;
            }
        } /* sonst für count spaces ein TAB ausgeben */
        zeichen = fgetc(in);
    }
/*- "" */
/*+ "end" */
    fclose(in);
    fclose(out);
    printf("%i TABs inserted!\n",tabcount);
    puts("\nReady.");
    exit(EXIT_SUCCESS);
/*- "End" */
}
/*- "int main ( argc, *argv )" */


