// (c) 1998-2007 Electronic Arts Inc.

/*------------------------------------------------------------------*/
/*                                                                  */
/*                    Debugger Frontend v1.10                       */
/*                                                                  */
/*                     by Frank Barchard, EAC                       */
/*                                                                  */
/*                   Main program - Mar 11, 2003                    */
/*                                                                  */
/*------------------------------------------------------------------*/
/*                                                                  */
/* Version Date   SE History:                                       */
/* ------- ----   -- --------                                       */
/*    1.00 981112 FB initial version based on DIS                   */
/*    1.01 981116 FB non-spit; pass command line verbatim           */
/*    1.02 990128 FB check for MSDEV env var, else assume in path   */
/*    1.03 990203 FB .dsw, .dsp, .mdp Visual C projects             */
/*    1.04 991130 FB .java                                          */
/*    1.05 000531 FB .xml                                           */
/*    1.06 000927 FB start msdev                                    */
/*    1.07 020730 FB start frontpg                                  */
/*    1.08 021009 FB devenv for .net                                */
/*    1.09 021017 FB sln vcproj cs                                  */
/*    1.10 030311 FB xbe for xbox debugging                         */
/*                                                                  */
/*------------------------------------------------------------------*/


/****************************************************************/
/*  Spit Setup                                                  */
/****************************************************************/

#include <stdio.h>
#include <limits.h>
#include "spit.h"
#include "findfile.h"
#include "wildmatch.h"
#include "stdgimex.h"

const char TextVersion[]="1.10 (Samba) 2007.12.4";
#define TOOL_VERSION 109
#ifdef FRONTPAGE
#define TOOL_NAME "fp"
#else
#define TOOL_NAME "db"
#endif


int DB_isoption(char *argv)
{
    int isopt=0;

    if (argv[0]=='-'
#if !defined(__unix__)
      || argv[0]=='/'
#endif
    )
    {
        isopt = 1;
    }
    return(isopt);
}

char *DB_extension(char *nameptr)
{
    char *s;
    char *ext=NULL;
    s = nameptr+strlen(nameptr)-1;
    while (s>=nameptr)
    {
        if (*s=='.')
        {
            ext = s;
            break;
        }
        if (*s=='/' || *s=='\\' || *s==':')
        {
            break;
        }
        --s;
    }
    return(ext);
}

int DB_fileexist(char *nextname)
{
    int ok=0;
    GSTREAM *g;
    g = gopen(nextname);
    if (g)
    {
        ok = 1;
        gclose(g);
    }
    return(ok);
}


char *DB_findfile(char *name)
{
    static char nextname[SPITMAXLINE];

    int x=0;
    char *extensions[]=
    {
        "",".dsw",".mdp",".dsp",".sln",".vcproj",".xbe",".exe",".opt",".com",
        ".txt",".c",".h",".inc",".asm",".def",".i",".s",
        ".rc",".map",".err",".java",".py",".cpp",".c++",".cs",".mak",".bat",
        ".scr",".rsp",".lnk",".bif",".log",".ps",".asc",
        ".htm",".html",".xml",".dof",".hrc",".tog",".a",".abs",".lst",".ion",
        "",0,
    };

    do
    {
        sprintf(nextname,"%s%s",name,extensions[x]);
        if (DB_fileexist(nextname))
            break;
        ++x;
    } while (extensions[x]);

    return(nextname);
}


#if 0
/*
;
; ABSTRACT
;
; db - tool to call debugger
;
; SUMMARY
;
; db [-options] [files]
;     @file          response file, containing a list of options and files
;     =file          specify a listing file ie source=dest
;     -v0            no console output
;     -v1            prints source file name and errors
;     -v2            status line for each file (default)
;     -v3            additional info
;     /EX <macroname>         - execute a VBScript macro
;     /OUT <filename>         - redirect command line output to a file
;     /USEENV                 - ignore tools.options.directories settings
;     /MAKE [<target>] [...]  - build specified target(s)
;           [<project> - <platform> <configname>]
;           [[<project>|ALL] - [DEBUG|RELEASE|ALL]]
;           /CLEAN            - delete intermediate files but don't build
;           /REBUILD          - clean and build
;           /NORECURSE        - don't build dependent projects
;
;
; DESCRIPTION
;
; Call Visual Studio (debugger/editor) but allow spit options and piping output.
;
; RETURNS
;
; 0 if no errors else number of errors
;
; SEE ALSO
;
; e, msdev
;
; SAMPLE OUTPUT
;
; db bob
;
; END ABSTRACT
;
*/
#endif

/****************************************************************/
/*  SPIT Routines                                               */
/****************************************************************/

char *optionhelp[]=
{
#ifndef FRONTPAGE
    "    /EX <macroname>         - execute a VBScript macro\n",
    "    /OUT <filename>         - redirect command line output to a file\n",
    "    /USEENV                 - ignore tools.options.directories settings\n",
    "    /MAKE [<target>] [...]  - build specified target(s)\n",
    "          [<project> - <platform> <configname>]\n",
    "          [[<project>|ALL] - [DEBUG|RELEASE|ALL]]\n",
    "          /CLEAN            - delete intermediate files but don't build\n",
    "          /REBUILD          - clean and build\n",
    "          /NORECURSE        - don't build dependent projects\n",
#endif
    0,
};


void DB_usage(void)
{
    char **hlp;

    printf(TOOL_NAME " v%s by Frank Barchard,EAC\n",TextVersion);
    printf("USAGE: " TOOL_NAME " /options [file] [parameters]\n");
    puts("");
    printf("VERSION: %s v%s\n",SPITmachine,TextVersion);
#ifdef FRONTPAGE
    printf("SUMMARY: " TOOL_NAME " is a frontend to launch Microsoft Frontpage\n");
#else
    printf("SUMMARY: " TOOL_NAME " is a frontend to launch Visual Studio .NET\n");
#endif
    puts("");
    hlp = optionhelp;
    while (*hlp)
    {
        printf("%s",*hlp);
        ++hlp;
    }
}


int main(int argc, char *argv[])
{
    int i;
    int first=1;
    char *nextname;
    static char commandline[1*1024*1024];

    if (argc>=2)
    {
        if (argv[1][0]=='?' || argv[1][1]=='?')
        {
            DB_usage();
            exit(10);
        }
    }

//    if (getenv("MSDEVDIR"))
//        strcpy(commandline,"%MSDEVDIR%\\Bin\\MSDEV.EXE ");
//    else
#ifdef FRONTPAGE
    strcpy(commandline,"start frontpg ");
#else
    strcpy(commandline,"start devenv ");
#endif
    /* get all file names */
    for (i=1; i<argc; ++i)
    {
        nextname = argv[i];
        if (first && !DB_isoption(argv[i]))
        {
            nextname = DB_findfile(argv[i]);
            first = 0;
        }

        strcat(commandline,nextname);
        strcat(commandline," ");
    }

    puts(commandline);
    system(commandline);

    return(0);
}

#if defined(_MSC_VER)
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpszCmdLine, int cmdShow)
{
    char *argv[2];
    argv[0] = "db";
    argv[1] = lpszCmdLine;
    return main(2,argv);
}
#endif

