 /* MPTool   -  First Step : 04/09/97
                Last Modif : 02/04/98
         by HEINRICH Patrick
    Nothing but a working version ;)
    Frozen : V0.04                    */


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

#define         VER     "0.04"
#define         MESS1   " Usage : %s <Option> <Files |Dirs | Volumes>"
#define         OPT1    "  <Option> :    a  <=>  simple time listing and automatically cut wave header\n"
#define         OPT2    "                c  <=>  simple time listing and add duration/kbps in comment\n"
#define         OPT3    "                t  <=>  simple time listing (don't modify your comments)\n"
#define         OPT4    "                w  <=>  cut the wave header of a given file\n\n"
#define         RET     printf("\n")


/* ========================================================================================= */

    FILE   *infp;
    char   *ptr;
    char   path [256];
    long   filesize=0;
    long   mpduration=0;
    long   mptotsize=0;
    int    filetot=0;
    int    dirtot=0;
    int    mptot=0;
    int    wavetot=0;
    int    com=0;
    int    count=2;
    int    automatic=0;
    struct FileInfoBlock *fib3_ptr;
    struct FileLock *File3Lock ;

    struct FileHandle *InFile;
    struct FileHandle *OutFile;
    long bytes_read;
    long buffersize=4096;
    char * tempo=NULL;

/* ========================================================================================= */

    void ExamineIt(char* File2Lock);
    void IsMPEG(char* gimmefile,long zisize);

/* ========================================================================================= */

main(int argc, char *argv[])

{
    printf("\n%c[1;31m MPTool %c[0mV%s '98 by %c[1;32m·HEINRICH Patrick·%c[0m \n",27,27,VER,27,27);
    printf("\n Registered to : %c[0;33mEverybody%c[0m \n\n",27,27);

    if (argc < 3) { printf(MESS1,argv[0]);RET;RET;
                    printf(OPT1);printf(OPT2);printf(OPT3);printf(OPT4);exit(0); }

    switch (*argv[1])

    {

    case 'w':case'W':


    InFile=(struct FileHandle *) Open(argv[2],MODE_OLDFILE);

    tempo=calloc(buffersize,sizeof(char));


    Read(InFile, tempo, 5);
    tempo[4]=NULL;

    if ( (strcmp("RIFF",tempo))!=0 ) {
                                      printf(" File %s has no wave header ! \n\n",argv[2]);
                                      Close(InFile);
                                      exit(0);
                                     }


    if (argc==4) { OutFile=(struct FileHandle *) Open(argv[3],MODE_NEWFILE); }
    else         { strcpy(path,argv[2]);
                   path[(strlen(argv[2])-1)]=-57;
                   OutFile=(struct FileHandle *) Open(path,MODE_NEWFILE);
                 }
    Seek(InFile,70,OFFSET_BEGINNING);

    printf(" Trying to cut the wave header of %s ...\n",argv[2]);

    while ((bytes_read=Read(InFile, tempo, buffersize))==buffersize)

    {
    Write(OutFile, tempo, buffersize);
    }

    Write(OutFile, tempo, bytes_read);


    free(tempo);
    Close(InFile);
    Close(OutFile);

    if (argc==3) {remove(argv[2]);rename(path,argv[2]);}

    printf(" %s has been successfully dewaved !\n\n",argv[2]);

    exit(0);

    case 'c':case'C':

    com=1;

    case 't':case'T':

    ReadyGo:

    /* is argv[count] a directory/file/volume */

    for ( count=2; (count<=(argc-1)) ;count++ )

    {

    JumpIn:

    strcpy(path,argv[count]);
    ptr=path;
    while ((*ptr!=':') && (*ptr!=0)) {ptr++;} ptr++;
    if (*ptr!=0)
                    {
                    while (*ptr!=0) {ptr++;}
                    ptr--;
                    if (*ptr!='/') {} else {*ptr=0;}
                    }

    fib3_ptr = AllocMem( sizeof( struct FileInfoBlock ),0 );

    File3Lock = Lock( path, -2 );
    if ( File3Lock  == NULL ) {printf("\n Can't lock file %s\n",File3Lock);}

    Examine(File3Lock,fib3_ptr);

    if ((*fib3_ptr).fib_DirEntryType<0)
            {
            filesize=(*fib3_ptr).fib_Size;
            IsMPEG(argv[count],filesize);
            UnLock(File3Lock);
            FreeMem( fib3_ptr, sizeof( struct FileInfoBlock ) );
            count++;
            filetot++;
            if (count<=argc-1) goto JumpIn;
            goto EndPart;
            }


    UnLock(File3Lock);
    FreeMem( fib3_ptr, sizeof( struct FileInfoBlock ) );


    if (argc==3)

        {
        printf("\n============================[MPTool]===['98·TMG·]===============================\n\n");
        printf("    MPTool was started from %s\n",path);
        }
        printf("\n============================[MPTool]===['98·TMG·]===============================\n\n");


    ExamineIt(path);
    }

    EndPart:

    printf("\n============================[MPTool]===['98·TMG·]===============================\n");
    if (argc==3) printf("\n %s \n",path);
    printf("\n                         %12i MPEG Files (MPEG or WAVE header)\n",mptot);
    printf("                         %12i Files\n",filetot);
    printf("                         %12i Directories\n",dirtot);
    printf("                         %12i MPEG Files with a WAVE header\n",wavetot);
    if (mptotsize/(1024*1024) >= 2)
    printf("\n                         %12d Mbs of MPEG\n",mptotsize/(1024*1024));
    printf("                         %12d Kbs of MPEG\n",mptotsize/1024);
    printf("                         %12d bytes of MPEG\n",mptotsize);
    printf("\n                             %02i:%02i:%02i Total Duration of the listed files\n",(mpduration/3600),((mpduration-3600*(mpduration/3600))/60),mpduration-3600*(mpduration/3600)-60*((mpduration-3600*(mpduration/3600))/60));
    printf("\n============================[MPTool]===['98·TMG·]===============================\n\n");

    break;

    case 'a':case 'A':

    automatic=1;
    goto ReadyGo;


    default:

    RET;printf(MESS1,argv[0]);RET;RET;printf(OPT1);printf(OPT2);
        printf(OPT3);printf(OPT4);exit(0);


    }


}

/* ========================================================================================= */

void ExamineIt(char* File2Lock)

    {
    int count=0;            /* check 4 dir */
    int fromvol=1;          /* check for a lone volume */
    long size=0;
    char newpath [256];
    char newpass [256];
    char* carptr;
    struct FileInfoBlock *fib_ptr;
    struct FileLock *File1Lock ;


    fib_ptr = AllocMem( sizeof( struct FileInfoBlock ),0 );

    strcpy(newpath,File2Lock);
    carptr=newpath;

    while (*carptr!=':') {carptr++;} carptr++;
    if (*carptr!=0) {fromvol=0;}

    if( fib_ptr == NULL ) { printf("\n Not Enough Memory \n");exit(0); }

    File1Lock = Lock( File2Lock, -2 );
    if ( File1Lock  == NULL ) {printf("\n Can't lock file %s\n",File2Lock);}

    Examine(File1Lock,fib_ptr);

    while (ExNext(File1Lock,fib_ptr))

        {
        if ( ((*fib_ptr).fib_DirEntryType>0) && (fib_ptr!=NULL) )
             {
               dirtot++;
               if (count!=0)
                    {
                    if (fromvol==1)
                        {
                        strcpy(newpath,File2Lock);
                        strcat(newpath,(*fib_ptr).fib_FileName);
                        }
                    else
                        {
                        strcpy(newpath,File2Lock);strcat(newpath,"/");
                        strcat(newpath,(*fib_ptr).fib_FileName);
                        }
                    }
               else
                    {
                    if (fromvol==1)
                        {
                        strcat(newpath,(*fib_ptr).fib_FileName);
                        }
                    else
                        {
                        strcat(newpath,"/");
                        strcat(newpath,(*fib_ptr).fib_FileName);
                        }
                    }
               ExamineIt(newpath);
               count++;
             }


        else {
               filetot++;
               size=(*fib_ptr).fib_Size;
               strcpy(newpass,File2Lock);
               if (fromvol==1)
                    {
                    IsMPEG(strcat(newpass,(*fib_ptr).fib_FileName),size);
                    }
               else
                    {
                    strcat(newpass,"/");
                    IsMPEG(strcat(newpass,(*fib_ptr).fib_FileName),size);
                    }
             }
        }
    UnLock(File1Lock);
    FreeMem( fib_ptr, sizeof( struct FileInfoBlock ) );
    }

/* ========================================================================================= */

void IsMPEG(char* gimmefile,long zisize)

    {
    int           kbps=0;
    int           wave=0;
    int           buffersize=4096;
    struct        FileHandle * InFile;
    struct        FileHandle * OutFile;
    char          path [256];
    char          commy [80];
    char          temp [10];
    char *        tempo;
    long          tmpsize;
    long          bytes_read;
    unsigned char Buffer [10];           // buffer to store some infos from infile

    tmpsize=zisize;
    InFile=(struct FileHandle *) Open(gimmefile,MODE_OLDFILE);

    Read(InFile,Buffer,10);              // reads the header of the file

    Buffer[4]=NULL;
    
    if ( (strcmp("RIFF",Buffer))==0 )    // if there is a WAVE header
      {
       Seek(InFile,70,OFFSET_BEGINNING);
       Read(InFile,Buffer,10);           // reads the real header of the file
       wavetot++;
       wave=1;
      }

    if ( Buffer[0]!=255 )

        {
        Close(InFile); return;
        }


    if (!( (Buffer[1]==251) || (Buffer[1] == 242) || (Buffer[1] == 250) || (Buffer[1] == 252)))

        {
        Close(InFile); return;
        }


    if ((Buffer[1]==250) || (Buffer[1]==251))

      { 
        switch (Buffer[2])

        {

        case 16:    kbps=32;break;
        case 32:    kbps=40;break;
        case 48:    kbps=48;break;
        case 64:    kbps=56;break;
        case 80:    kbps=64;break;
        case 96:    kbps=80;break;
        case 112:   kbps=96;break;
        case 128:   kbps=112;break;
        case 144:   kbps=128;break;
        case 160:   kbps=160;break;
        case 176:   kbps=192;break;
        case 192:   kbps=224;break;
        case 208:   kbps=256;break;
        case 224:   kbps=320;break;

        default: kbps=128;break;
        }
      }

     else

      {

        if (Buffer[1]==252)

            {

            switch (Buffer[2])

            {

            case 16:    kbps=32;break;
            case 32:    kbps=48;break;
            case 48:    kbps=56;break;
            case 64:    kbps=64;break;
            case 80:    kbps=80;break;
            case 96:    kbps=96;break;
            case 112:   kbps=112;break;
            case 128:   kbps=128;break;
            case 144:   kbps=168;break;
            case 160:   kbps=192;break;
            case 176:   kbps=224;break;
            case 192:   kbps=256;break;
            case 208:   kbps=320;break;
            case 224:   kbps=384;break;

            default: Close(InFile); return;
            }
            }
         else {
                if (Buffer[1]==242)

                    {

                    switch (Buffer[2])

                    {

                    case 16:    kbps=8;break;
                    case 32:    kbps=16;break;
                    case 48:    kbps=24;break;
                    case 64:    kbps=32;break;
                    case 80:    kbps=40;break;
                    case 96:    kbps=48;break;
                    case 112:   kbps=56;break;
                    case 128:   kbps=64;break;
                    case 144:   kbps=80;break;
                    case 160:   kbps=96;break;
                    case 176:   kbps=112;break;
                    case 192:   kbps=128;break;
                    case 208:   kbps=144;break;

                    default:Close(InFile); return;
                    }
                    }
                 else {Close(InFile); return;}
               }

      }

    if ( (wave==1) && (automatic==1) )
        {
        printf(" cutting the wave header of file %s\n",gimmefile);
        strcpy(path,gimmefile);
        path[(strlen(gimmefile)-1)]=-57;
        OutFile=(struct FileHandle *) Open(path,MODE_NEWFILE);
        Seek(InFile,70,OFFSET_BEGINNING);

        tempo=calloc(buffersize,sizeof(char));


        while ((bytes_read=Read(InFile, tempo, buffersize))==buffersize)
                 {
                 Write(OutFile, tempo, buffersize);
                 }

        Write(OutFile, tempo, bytes_read);
        free(tempo);
        Close(InFile);
        Close(OutFile);
        remove(gimmefile);
        rename(path,gimmefile);
        }

    else

        {
        Close(InFile);
        }

    mpduration=mpduration+(8*zisize/1000/kbps);
    printf(" %-65.65s %0.2ld:%0.2ld %3ikbps\n",gimmefile,(8*zisize/1000/kbps/60),(8*zisize/1000/kbps)-(8*zisize/1000/kbps/60)*60,kbps);

        if (com==1)
           {
           if ((8*zisize/1000/kbps/60)<10) {strcpy(commy," [0");} else {strcpy(commy," [");}

           stci_d(temp,(8*zisize/1000/kbps/60));
           strcat(commy,temp);
           strcat(commy,":");

           zisize=((8*zisize/1000/kbps)-(8*zisize/1000/kbps/60)*60);

           if ( zisize <10 ) {strcat(commy,"0");}

           stci_d(temp,zisize);
           strcat(commy,temp);
           strcat(commy,"] - [");
           stci_d(temp,kbps);
           strcat(commy,temp);
           strcat(commy,"kbps]");

           SetComment(gimmefile , commy);
           }

    mptot++;
    mptotsize=mptotsize+tmpsize;
    }

/* ========================================================================================= */




/* ========================================================================================= */
/*                                      THE END                                              */
/* ========================================================================================= */




