#include <stdio.h>

#include "QtTypDef.h"
#include "dumpfrme.h"

DWORD Arg[4];
WORD  NumMin=1;
DWORD StartRef = 0;
DWORD EndRef   = 0xffffffff;

bool  ProcArg (char * pArg);
DWORD ParseArg(char * NumStr);

int main(int ArgC, char * ArgV[])
{
    char InpBaseFile[200];
    char OptFileName[200];
    char OutFileName[200];
    char *pStr;
    DWORD  Hour;
    DWORD  Minute;
    BYTE ArgNum;

    if (ArgC<3)
    {
usage:;
        printf("use: PbcCapt2Txt <InputFile> <OptFile> [options] \n"
               "    <InputFile>    .PbcCapt will be added\n"
               "    <OptFile>      .Opt will be added\n"
               "Options:\n"
               "    NumMin=<n>     Number of Minutes to convert\n"
               "    StartRef=<n>   Start Reference Number\n"
               "    EndRef=<n>     End   Reference Number\n"
               "    Arg0=<n>       Arg 0 for Opt file 0x for hex\n"
               "    Arg1=<n>       Arg 1 for Opt file 0x for hex\n"
               "    Arg2=<n>       Arg 2 for Opt file 0x for hex\n"
               "    Arg3=<n>       Arg 3 for Opt file 0x for hex\n"
               );
        return 1;
    }

    for (ArgNum=3 ; ArgNum<ArgC ; ArgNum++)
    {
        if (!ProcArg (ArgV[ArgNum]))
        {
            goto usage;
        }
    }

    strcpy(InpBaseFile, ArgV[1]);

    if (strlen(InpBaseFile)<6)
    {
inp_file_inv:;
        printf ("Invalid Capture File Name %s\n"
                 "It must be in the following form:\n"
                 "<name>HHMM.PbcCapt\n"
                 "Note:\n"
                 "<name> is the user defined portion of the name\n"
                 "HH is the hour   offset\n"
                 "MM is the minute offset\n", InpBaseFile);
        goto usage;
    }
    pStr = &InpBaseFile[strlen(InpBaseFile)-4];

    if ((!isdigit(pStr[0]))     ||
        (!isdigit(pStr[1]))     ||
        (!isdigit(pStr[2]))     ||
        (!isdigit(pStr[3])))
    {
        goto inp_file_inv;
    }

    strcpy(OptFileName, ArgV[2]);
    pStr = &OptFileName[strlen(OptFileName)-1];
    while ( (*pStr!='\\') && (pStr!=OptFileName) )
    {
        pStr--;
    }

    if (*pStr=='\\')
    {
        pStr++;
    }

    strcpy (OutFileName, InpBaseFile);
    strcat (OutFileName,"_");
    strcat (OutFileName,pStr);
    strcat (OutFileName,".txt");
    strcat (OptFileName,".opt");

    pStr = &InpBaseFile[strlen(InpBaseFile)-4];

    Hour    = (pStr[0]-'0')*10;
    Hour   += (pStr[1]-'0');
    Minute  = (pStr[2]-'0')*10;
    Minute += (pStr[3]-'0');

    *pStr = '\0';

    if ( Minute>59 )
    {
        printf("Invalid Capture File Name Minute - %02u (00-59)\n", Minute);
        goto usage;
    }

    *pStr = '\0';

    pStr = PbcDumpText(InpBaseFile, OutFileName, OptFileName, Hour, Minute, NumMin, StartRef, EndRef, Arg[0], Arg[1], Arg[2], Arg[3] );

    if (pStr==0)
    {
        printf("\nSuccess - Created: %s\n", OutFileName);
    }
    else
    {
        printf("\n%s\n", pStr);
        return 2;
    }
    return 0;
}


bool ProcArg (char * pArg)
{
    if (!strnicmp (pArg, "NumMin=", 7))
    {
        pArg += 7;
        sscanf(pArg,"%hu", &NumMin);
        return true;
    }
    if (!strnicmp (pArg, "StartRef=", 9))
    {
        pArg += 9;
        sscanf(pArg,"%u", &StartRef);
        return true;
    }
    if (!strnicmp (pArg, "EndRef=", 7))
    {
        pArg += 7;
        sscanf(pArg,"%u", &EndRef);
        return true;
    }
    if (!strnicmp (pArg, "Arg", 3))
    {
        BYTE ArgNum;
        pArg += 3;
        ArgNum = *pArg++ - '0';
        if (ArgNum>3)
        {
            printf("Invalid Arg Num - %u (0-3)\n", ArgNum);
            return false;
        }
        pArg++; // skip '='
        Arg[ArgNum] = ParseArg(pArg);
        return true;
    }

    printf("Invalid Arg %s", pArg);
    return false;
}

DWORD ParseArg(char * NumStr)
{
    DWORD RetVal=0;

    if ( !strnicmp (NumStr,"0x",2 ) )
    {
        sscanf (&NumStr[2],"%x",&RetVal);
    }
    else
    {
        sscanf (NumStr,"%d",&RetVal);
    }
    return RetVal;
}