/*

Program     :  AmiExpress FileList to HBBS Filelist convertor.

Filename    :  AX2HBBS

Usage       :  AX2HBBS infile outfile [Year Prefix(2 characters only)]

Author      :  Ben Clifton

Environment :  Amiga

Revision    :  V1.0 17 Dec 1995

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <clib/dos_protos.h>

#define BUFLEN 256

/* GLOBAL Variables */
BOOL First = TRUE;
char DateYearPrefix[5];

/* 12, 22, 32 */

/* haha, bit of code taken from FileMan by Ben Clifton (Oh, thats me!, cool).
 *
 *
 * FM-strFNcpy - copy "num" characters from "start" position in "in" to "out"
 *              and NULL terminate.
 *
 * Note : out is NOT allocated.
 */

void FM_strFNcpy( char *in, char *out, int start, int num )
{
  int loop, i;

  for( i=0, loop = start; loop < num+start; loop++, i++ )
  {
    out[i] = in[loop];
  }
  out[i]=0;
}

/* return a Month string in format "MMM-"
 *
 * the "-" is there cos I`m a lazy bastard!
 */

char *GiveMonth( int num )
{
  switch( num )
  {
    case 1:
      return( "JAN-" );
    case 2:
      return( "FEB-" );
    case 3:
      return( "MAR-" );
    case 4:
      return( "APR-" );
    case 5:
      return( "MAY-" );
    case 6:
      return( "JUN-" );
    case 7:
      return( "JUL-" );
    case 8:
      return( "AUG-" );
    case 9:
      return( "SEP-" );
    case 10:
      return( "OCT-" );
    case 11:
      return( "NOV-" );
    case 12:
      return( "DEC-" );
    default:
      return( "Erk-" );
  }
}

/* You`ll never guess what this does!  , oh shit you have.... never mind.
 *
 * Um, if anyone is dumb enough not to know what this does then:
 *
 * Convert Line - Converts a string, in, in /X format into, out, in HBBS
 *               format.
 *
 * Note : out is written TO not allocated.
 *
 */

BOOL ConvertLine( char *in, char *out )
{
  char temp[BUFLEN], temp2[BUFLEN];
  char *ptr;
  int i, length;
  long filesize;

  out[0] = 0;
  if( in[0] != ' ' )
  {
    /* This is to put a blank line separator between FileIds */
    if( !First )
    {
      strcat( out, "\n" );
    }
    else
    {
      First = FALSE;
    }

    strcat( out, "F " );

    /* Concat FileName top output string */
    FM_strFNcpy( in, temp, 0, 12 );  /* Its the first 12 bytes so get them */
    strcat( out, temp );
    strcat( out, " " );  /* Separator */

    /* Do filesize */
    FM_strFNcpy( in, temp, 14, 7 );
    length = strlen( temp );
    i=-1;
    do
    {
      i++;
      ptr = &temp[i];
    }while( !( (temp[i]>='0') && (temp[i]<='9') ) && i<9 );
    filesize = atol( temp );
    sprintf( temp, "%8d ", filesize );
    strcat( out, temp );

    /* Do Date! */

    /* before we do anything, I`ll pointlessly get a copy of the needed
     * bit of data!  - okay so its not pointless, it lets me change the
     * position easily! */

    FM_strFNcpy( in, temp, 23, 8 );

    /* First we have the Day...*/

    FM_strFNcpy( temp, temp2, 3, 2 );
    strcat( out, temp2 );
    strcat( out, "-" );

    /* Second we have the month, curtosy of a simple case statement! :) */

    FM_strFNcpy( temp, temp2, 0, 2 );
    strcat( out, GiveMonth( atoi(temp2) ) );  /* wow, i love this stuff, it SOOOOOOo cool! <smirk> */

    /* and finally we have the YEAR!  god, is it that already?  where did my life go:-)))) */

    FM_strFNcpy( temp, temp2, 6, 2 );
    strcat( out, DateYearPrefix );
    strcat( out, temp2 );
    strcat( out, " " );

    /* And after all this we get to put the file id line down... */
    ptr = &in[33];
    strcat( out, ptr );
  }
  else
  {
    /*if its not a "F" then its a "I". */
    strcat( out, "I" );
    /* Pad with spaces */
    for( i=0; i<35; i++)
    {
      strcat( out, " " );
    }
    /* :) */  /* I LOVE this method, its SO cheeky! */
    ptr = &in[33];
    strcat( out, ptr );
  }

  return( TRUE );
}

/* Check string, s, to make sure its not garbage. */

BOOL CheckLineForGarbage( char *s )
{
  if( (s[12] == ' ')&&(s[22] == ' ')&&(s[32] == ' ') )
    return( FALSE );
  else
    return( TRUE );
}

/* Main program : To convert an AmiExpress FileList to an HBBS FileList
 *
 * Inputs   :  Input FileName, OutputFileName, Year Prefix
 *
 * Outputs  :  New HBBS format Filelist.
 *
 *
 * Usage    : AX2HBBS infile outfile [Year Prefix(2 characters only)]
 *
 * Example 1: AX2HBBS infile outfile
 * Example 2: AX2HBBS infile outfile 19
 *
 *
 * Notes    : if outfile exists then AX2HBBS will append the data.
 *
 */

void main( int argc, char *argv[] )
{
  BPTR infile, outfile;
  BOOL done = FALSE, error = FALSE;
  char inbuffer[BUFLEN], outbuffer[BUFLEN];
  long openmode;

  if( (argc != 3)&&(argc != 4) )
  {
    Printf( "AX2HBBS by Ben Clifton.\nPurpose : To convert /X FileLists to HBBS FileLists\n");
    Printf( "AX2HBBS infile outfile [Year Prefix(2 characters only)]\n" );
  }
  else
  {
    if( argc == 3 )
    {
      strcpy( DateYearPrefix, "19" );
    }
    else
    {
      /* Make sure we get 2 characters and ONLY 2 characters */
      if( strlen( argv[3]) >= 2 )
      {
        FM_strFNcpy( argv[3], DateYearPrefix, 0, 2 );
      }
      else
      {
        /* don`t know why really...  cos it SHOULD be at least 1995... oh well. */
        DateYearPrefix[0] = '0';
        DateYearPrefix[1] = argv[3][0];
        DateYearPrefix[2] = 0;
      }
    }
    if( !(infile = Open( argv[1], MODE_OLDFILE )))
    {
      Printf( "Error : Unable to find input file\n" );
    }
    else
    {
      Printf( "Skipping Garbage......." );
      while( !done )
      {
        if( !(FGets( infile, inbuffer, BUFLEN-1 )))
        {
          done = TRUE;
          error = TRUE;
          Printf( "Error : Cannot read from infile\n" );
        }
        else
        {
          if( !(CheckLineForGarbage( inbuffer )))
          {
            done = TRUE;
          }
        }
      }
      if( !error )
      {
        Printf( "Done.     No errors.\n" );

        if( outfile = Lock( argv[2], SHARED_LOCK ) )
        {
          First = FALSE;
          openmode = MODE_OLDFILE;
          UnLock( outfile );
        }
        else
        {
          openmode = MODE_NEWFILE;
          First = TRUE;
        }

        if( !(outfile = Open( argv[2], openmode )))
        {
          Printf( "Error : Unable to open outfile.\n" );
          error = TRUE;
        }
        else
        {
          Printf( "Processing............." );
          Seek( outfile, 0, OFFSET_END );
          /* Write out header! */
          if( !(FPuts( outfile,     "\n    +------------------------------------------------+\n" )))            {
            if( !(FPuts( outfile,     "    |   FileList created by AX2HBBS by Ben Clifton   |\n" )))              {
              if( !(FPuts( outfile,   "    | Another cool util from Ruby Knight Productions |\n" )))                {
                if( !(FPuts( outfile, "    +------------------------------------------------+\n" )))
                {
                  done = FALSE;
                  while( !done )
                  {
                    if( ConvertLine( inbuffer, outbuffer ) )
                    {
                      if( FPuts( outfile, outbuffer ))
                      {
                        done = TRUE;
                        error = TRUE;
                        Printf( "Error: writing to outfile\n" );
                      }
                      else
                      {
                        if( !(FGets( infile, inbuffer, BUFLEN-1 )))
                        {
                          done = TRUE;
                        }
                      }
                    }
                  }
                }
                else
                {
                  error = TRUE;
                }
              }
              else
              {
                error = TRUE;
              }
            }
            else
            {
              error = TRUE;
            }
          }
          else
          {
            error = TRUE;
          }
          Close( outfile );
        }
        if( !error )
        {
          Printf( "Finished. No errors.\n" );
        }
        else
        {
          Printf( "Finished. Error has occured!\n" );
        }
      }
      Close( infile );
    }
  }
}

/* Bananas are cool!  - just for Dom! */
