/* John Curtis' program to convert Turbo .FIL files to QuickBBS .BBS
   files. */
   
#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>
#include    <fcntl.h>
#include    <portab.h>
#include    <osbind.h>
#include    <dirent.h>
#include    <dos.h>
#include    "fbbs_cvt.h"

#define     SCR_LINE_LENGTH     81  /* Allow extra byte for '\0' */
#define     MAX_LINE_LENGTH     256 /* Allow extra byte for '\0' */
#define     TEMP_LINE_STORAGE   500

/* Prototypes for functions defined in D:\PROJECT2\FBBS_CVT.C */
int main(void);
void TurboFil(char *filename);
void cls(void);
void FbbsFiles(char *fileout, long numFiles );
void get_title(void);

TFILE   *turboPtr;

int main( void )
{
int x = 4 ;

struct FILEINFO info ;      /*  Defined to get names on all *FIL's
                                in the current directory . */

    get_title() ;
    
    Fsetdta(&info) ;
    
    if( !Fsfirst( "*.FIL",0 )) /* Read default directory & get all *.FIL's */
        do
        {

            printf("   Now Converting %s ...\n", info.name ) ;
            x++ ;
            
            TurboFil( info.name ) ; /* Run Function to get
                                       all the information from
                                       the *.FIL...
                                     */
            if( x == 24 ) 
            {
                x=4 ;
                cls() ;
                get_title() ;
            }
                    
        } while (!Fsnext()) ;    /* Keeps going until all *.FIL's are Processed */
    else
        {
            printf("\a\a\n  You must run 'FBBS_CVT.TOS ' in the same\n" ) ;
            printf("  Place as your *.FIL Files...\n" ) ;
            printf("\n  Press any Key to Exit!!!\n" );
            getch() ;
        }
    return 0 ;
}

void TurboFil( char *filename )
{
    FILE *fp;
    long numberOfFiles;
    
    /* Open the TURBO.FIL file and get number of records */
    
    if(( fp = fopen( filename, "rb" )) == NULL )
        {
        cls();
        printf( "\n\n\n   Error opening filename : %s\n", filename );
        printf( "\n\   Hit Any Key to Continue\n" ) ;
        getch() ;
        cls() ;
        exit( 1 ) ;
        }
    else
        {
        numberOfFiles = filelength( fileno(fp) ) / sizeof( TFILE );
        turboPtr = malloc( filelength ( fileno(fp) ) );

        if ( !turboPtr )
            {
            printf ( "Not enough memory available. Sorry." );
            printf ( "Press any key to exit..." );
            getch();
            fclose ( fp );
            exit ( 1 );
            }
        }

/* If there's enough memory, read in complete .FIL file in one go. */
        
        fread( turboPtr, numberOfFiles * sizeof( TFILE ), 1, fp );
        fclose ( fp );
        
        FbbsFiles( filename, numberOfFiles ) ;

        free ( turboPtr );  /* Give back the memory previously malloc'd. */
}

void cls( void )        /* Just something to clear the Screen */
{
    printf( "\033E" ) ;
    Cursconf( 0,0 ) ;
}

void FbbsFiles( char *fileout, long numFiles )
{
int     n, len, x ;
long    i;
char    dot[SCR_LINE_LENGTH], descript[SCR_LINE_LENGTH] ;
char    temp[TEMP_LINE_STORAGE];     /* Used to store combined string, 
                                        Downloads, Filename and Description. */
                                     
char    fbbsdesc[TEMP_LINE_STORAGE]; /* Collects all 5 TurboII fdesc[1 to 5] 
                                        into 1 file                  ^ *8)    */

FILE *pt ;

    len = strlen( fileout ) ;
        
    fileout[len-3] = '\0' ;       /* Remove FIL from filename */
    
    strcat( fileout, "BBS" ) ;    /* Add BBS to filename */
    
    if(( pt = fopen( fileout, "w" )) == NULL )
        {
        printf( "\n   Something Happened, Couldn't write to '%s'\n", fileout ) ;
        printf( "\n   Hit Any Key To Exit!!!\n" ) ;
        getch() ;
        }

    else
        {
        sprintf( descript, "Description For %s", fileout ) ; 

        len = strlen( descript ) ;
        sprintf( temp, "* %-*s *\n", len, descript ) ;
        for( x = 0; x < len+4 && x < 80 ; x++ )
            dot[x] = '*' ;
        
        dot[x] = '\0' ;

        strcat( dot, "\n" ) ;
        
        fputs( dot, pt ) ;
        fputs( temp, pt ) ;
        fputs( dot, pt ) ;

    /* Title is now written and now the files are written  */
    
        for( i = 0; i < numFiles; i++)
            {
            strcpy( fbbsdesc, turboPtr -> fdesc[0] ) ;

            for ( n = 1; n < 5; n++ )
                {
                if ( strlen ( turboPtr -> fdesc[n] ) > 0)
                    {
                    strcat ( fbbsdesc, " " );
                    strcat( fbbsdesc, turboPtr -> fdesc[n] );
                    }
                }


            if( turboPtr -> frd_cnt > 0 )
                sprintf( temp, "\n%-13s[%ld] %s", turboPtr -> fname,
                       turboPtr -> frd_cnt, fbbsdesc ) ;
            
            else
                sprintf( temp, "\n%-13s%s", turboPtr -> fname, fbbsdesc ) ;
            
            if( strlen( temp ) > MAX_LINE_LENGTH )
                temp[MAX_LINE_LENGTH] = '\0' ;

            fputs( temp, pt ) ;
            turboPtr++; /*   Do it all again for next file! */
            }
        }
    fclose( pt ) ;
}

void get_title( void )
{
char    *program = "TurboII To Files.BBS Convertor " ;
char    *author  = "Written By John Curtis" ;
char    *advert  = "C&C SoftWare    1994" ;
char    *version = "v0.11" ;

    cls() ;
    
    printf( "                   %s %s\n", program, version ) ;
    printf( "                          %s\n", author ) ;
    printf( "                           %s\n", advert ) ;
    
}
