#include "StdAfx.h"
#include "HaspDongle.h"
#include "6046Hasp.h"
#include "6046Options.h"

#include <vector>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

void    ConfigureHasp   (   const bool &rbEnable, const bool &rbDemo );
void    DumpHasp        (   void );

CWinApp theApp;

int _tmain( int argc, TCHAR *argv[], TCHAR *envp[] )
{
    int     iErrorCode  = 0;
    bool    bInitialize = false;
    bool    bPrintDump  = false;
    bool    bDemo       = false;

    for ( int iParam = 1; iParam < argc; iParam++ )
    {
        if ( ( argv[ iParam ] [ 0 ] == '-' ) || 
             ( argv[ iParam ] [ 0 ] == '/' )  )
        {
            switch ( argv[ iParam ][ 1 ] )
            {
                case '?':
                case 'h':
                case 'H':

                    printf( "HaspDongle [-d] [-i] [-p]\n" );
                    printf( " where      -d is Demo mode\n" );
                    printf( "            -p is Dump\n" );
                    printf( "            -i is Initiailize\n" );

                    exit( EXIT_SUCCESS );

                    break;

                case 'd':
                case 'D':

                    bDemo = true;
                    break;

                case 'p':
                case 'P':

                    bPrintDump = true;
                    break;

                case 'i':
                case 'I':

                    bInitialize = true;
                    break;
            }
        }
    }

    if ( ! AfxWinInit( ::GetModuleHandle( NULL ), NULL, ::GetCommandLine(), 0 ) )
    {
        throw _T( "Fatal Error: MFC initialization failed" );
    }

    try
    {
        //_putenv("TZ=GMT0" );
        //_putenv("TZ=PST8PDT");
        //_tzset();

        if ( bInitialize )
        {
            ConfigureHasp( true, bDemo );
        }

        C6046HaspOptions Hasp;

        //if ( Hasp.IsKeyPresent() )
        {
            printf( "PLC enabled flag (initial)   : %d\n", Hasp.IsPayloadControllerEnabled() );
        }
        //else
        //{
        //    printf( "Invalid PLC key\n" );
        //}

        if ( bPrintDump )
        {
            DumpHasp();
        }
    }
    catch ( LPCTSTR lpszMessage )
    {
        iErrorCode = -1;

        printf( "%s\n", static_cast<const char *>( lpszMessage ) );
    }
    catch ( CHaspException e )
    {
        iErrorCode = -2;

        CString Err;

        switch( e.GetStatus() )
        {
            case -12:

                Err.Format( "Pass keys incorrect." );
                break;

            case -3:
            case -1002:

                Err.Format( "No Hasp connected. Error HASPERR_INVALID_RESONHASP." );
                break;

            default:

                Err.Format( "Hasp Status Code: %d", e.GetStatus() );
                break;
        }

        printf( "%s\n", static_cast<const char *>( static_cast<LPCTSTR>( Err ) ) );
    }
    catch ( ... )
    {
        iErrorCode = -3;
        printf( "Unspecified exception caught.\n" );
    }

    return iErrorCode;
}

void ConfigureHasp( const bool &rbEnable, const bool &rbDemo )
{
#if defined ( _RESON_INTERNAL_ )

    C6046Hasp Hasp;

    std::vector<WORD> Memory6046Options( 16, 0x0000 );
    
    ASSERT( Memory6046Options.size() == 16 );
    
    Hasp.Configure( C6046Hasp::resonHaspVersion4,                       // Version 4 layout.
                        false,                                          // Clear all HASP memory.
                            &Memory6046Options[ 0 ],                    // Write 6046 attributs.
                                Memory6046Options.size(),               // Ditto.
                                    C6046Hasp::haspLayoutPLCOptions,    // Block starts at PLC options.
                                        rbDemo );                       // Set demo mode (expires system time + 30 days).

    printf( "SecureCheck(): Version: %u\n\n", Hasp.SecureCheck( C6046Hasp::haspLayoutVersion ) );

    C6046HaspOptions Hasp2;

    Hasp2.SetPayloadControllerEnable( rbEnable );

#endif
}

void DumpHasp( void )
{
#if defined ( _RESON_INTERNAL_ )

    C6046Hasp Hasp;

    Hasp.Dump();

#endif

}
