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

const char *SPIT_history[] =
{
"------------------------------------------------------------------",
"                                                                  ",
"                          URL v1.16                               ",
"                                                                  ",
"                    by FrANK G. Barchard, EAC                     ",
"                                                                  ",
"                   Main program - May 5, 2005                     ",
"                                                                  ",
"------------------------------------------------------------------",
"                                                                  ",
" Version Date   SE History:                                       ",
" ------- ----   -- --------                                       ",
"    1.00 990424 FB based on spitie, makes URLs for enviro         ",
"    1.01 990429 FB drive determines file:// ftp:// or http://     ",
"    1.02 990504 FB file:///C|/dir/file                            ",
"    1.03 000314 FB uses dest name for redirecting target          ",
"    1.04 000908 FB SearchPath to find name                        ",
"    1.05 010625 FB SearchPath find . first                        ",
"    1.06 010720 FB l: enviro.eac.ea.com                           ",
"    1.07 010724 FB t: for tools.eac.ea.com                        ",
"    1.08 010731 FB secure id/pw for xbox/gamecube                 ",
"    1.09 010824 FB //depot/ for local files                       ",
"    1.10 020419 FB //depot/ for local files                       ",
"    1.11 030428 FB remove depot root                              ",
"    1.15 041015 FB ad.ea.com active directory                     ",
"    1.16 050505 FB .. -> ...                                      ",
"                                                                  ",
"------------------------------------------------------------------",
"\0"
};


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

#if defined(_MSC_VER)
#pragma warning(push,1)
#include <windows.h>
#pragma warning(pop)
#endif

#include "spit.h"
#include "gclip.h"

const char TextVersion[]="1.16 (Samba) 2007.12.4";
#define TOOL_VERSION 116
#define TOOL_NAME "url"

/****************************************************************/
/*  Internal Functions                                          */
/****************************************************************/

void CLPtext(const char *str)
{
    GCLIP *gclip = GCLIP_wopen();
    HGLOBAL hData;

    if (gclip)
    {
        hData = GlobalAlloc(GHND | GMEM_SHARE, strlen(str)+1);  /* note freed by clipboard itself */
        if (hData)
        {
            char *p = (char *) GlobalLock(hData);
            if (p)
            {
                strcpy(p, str);
                GCLIP_write(gclip, CF_TEXT, p, strlen(p)+1);
            }
        }
        GCLIP_wclose(gclip);
    }
}

static void fixurl(char *url)
{
    char surl[512];
    int i;
    strcpy(surl,url);
    for (i=0; surl[i]; ++i)
    {
        if (surl[i]==' ')
        {
            *url++ = '%';
            *url++ = '2';
            *url++ = '0';
        }
        else
            *url++ = surl[i];
    }
}

static const char *depotremove(const char *dir)
{
    char *depotpath;

    depotpath = getenv("DEPOT");
    if (!depotpath)
        depotpath = "/env";

    char depotbuf[512];

    int j;
    for (j=0; depotpath[j]; ++j)
    {
        depotbuf[j] = depotpath[j];
        if (depotbuf[j]=='\\')
            depotbuf[j] = '/';
        if (depotbuf[j]==':')
            depotbuf[j] = '|';
    }
    depotbuf[j] = '\0';

    if (!strnicmp(dir,depotbuf,strlen(depotbuf)))
        dir += strlen(depotbuf);

    if (strlen(dir)>2)
        if (dir[1]=='|')
            dir += 2;
    return dir;
}

/*
;
; ABSTRACT
;
; url - make a url for current directory and optional file names
;
; SUMMARY
;
; url -options [files]
;     @file     response file, containing a list of options and files
;     -nologo   disable banner
;     -v0       no console output
;
; DESCRIPTION
;
; URL prints out a URL for the enviro, trough and local files.
;
; RETURNS
;
; no return code
;
; SEE ALSO
;
; huff, Spit
;
; SAMPLE OUTPUT
;
; >L:\enviro>url
; ftp://enviro.eac.ea.com
;
; END ABSTRACT
;
*/

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

void SPIT_reset(void)
{
    OPTbanner = 0;
    OPTminargs = 0;
    OPTwrite = 0;

    return;
}

void SPIT_usage(const char *prgname, const char *envname, const char *urlname, int utype)
{
    printf("URL v%s by FrANK G. Barchard\n",TextVersion);

    if (utype)
        printf("USAGE: %s -options [files]\n", prgname);

    switch (utype)
    {
        case 0:
        case 1:
            break;

        case 2:
            puts("    -v verbose");
            puts("     @ response file");
          printf("type '%s -h' for more help\n", prgname);
          printf("type '%s -help' for web page (%s)\n", prgname, urlname);
            break;

        case 3:
            puts("");
          printf("VERSION: %s v%s, Spit v%.2f\n",SPIT_MACHINE,TextVersion,(float)SPIT_VERSION/100.0f);
            puts("SUMMARY: URL prints out a URL for the enviro & trough volumes");
          printf("         SPIT environment variable is %s\n",envname);
            puts("");
            puts("    @file     response file, containing a list of options and files");
            puts("    -nologo   disable banner");
            puts("    -v0       no console output");
            break;

        default:
            break;
    }
}

char *SPIT_opt(const char*)
{
    return((char *) 0);
}

int main(int argc, char* argv[])
{
    int more;
    int i;

    SPIT_init(SPIT_reset, SPIT_usage, SPIT_opt, SPIT_history);
    do
    {
        more = SPIT_parse(argc, argv, TOOL_NAME, TOOL_VERSION);

        if (!SPIThelpgiven)
        {
            char url[512];
            if (SPITargnum)
            {
                char urls[256*1024];
                urls[0] = 0;

                for (i=0; i<SPITargnum; ++i)
                {
                    char dir[512];
                    int len;
                    int j;

                    strcpy(dir,SPITdargv[i]);
                    if (!(strchr("/\\",dir[0]) || (dir[1]==':' && strchr("/\\",dir[2]))))
                    {
                        len = SearchPath(".",SPITdargv[i],NULL,512,dir,NULL);
                        dir[len] = 0;
                        if (!len)
                        {
                            len = SearchPath(NULL,SPITdargv[i],NULL,512,dir,NULL);
                            dir[len] = 0;
                        }
                        if (!len)
                        {
                            len = GetCurrentDirectory(511, dir);

                            sprintf(dir,"%s/%s",dir,SPITdargv[i]);
                        }
                    }
                    for (j=0; dir[j]; ++j)
                        if (dir[j]=='\\') dir[j] = '/';
                    if (dir[1]==':')
                        dir[1] = '|';

                    if (tolower(dir[0])=='i')
                        sprintf(url,"http://www.eac.ad.ea.com%s",dir+2);
                    else if (tolower(dir[0])=='t')
                        sprintf(url,"http://tools.eac.ad.ea.com%s",dir+2);
                    else if (tolower(dir[0])=='r')
                        sprintf(url,"ftp://ftp.eac.ad.ea.com%s",dir+2);
                    else if (tolower(dir[0])=='l')
                        sprintf(url,"ftp://enviro.eac.ad.ea.com%s",dir+strlen("l|/enviro"));
                    else
                        sprintf(url,"//depot%s",depotremove(dir));

                    fixurl(url);
                    if (OPTverbose)
                        printf("%s\n",url);
                    if (urls[0])
                        strcat(urls,"\n");
                    strcat(urls,url);
                }
                CLPtext(urls);
            }
            else
            {
                char dir[512];
                int len;
                int j;

                len = GetCurrentDirectory(511, dir);

                for (j=0; dir[j]; ++j)
                    if (dir[j]=='\\') dir[j] = '/';

                if (dir[1]==':')
                    dir[1] = '|';

                if (tolower(dir[0])=='i')
                    sprintf(url,"http://www.eac.ad.ea.com%s",dir+2);
                else if (tolower(dir[0])=='t')
                    sprintf(url,"http://tools.eac.ad.ea.com%s",dir+2);
                else if (tolower(dir[0])=='r')
                    sprintf(url,"ftp://ftp.eac.ad.ea.com%s",dir+2);
                else if (tolower(dir[0])=='l')
                    sprintf(url,"http://tools.eac.ad.ea.com/enviro/download.asp?dir=/enviro%s",dir+strlen("l|/enviro"));
                else
                    sprintf(url,"//depot%s/...",depotremove(dir));

                fixurl(url);
                if (OPTverbose)
                    printf("%s\n",url);
                CLPtext(url);
            }
        }
    } while (more>0);

    SPIT_restore();

    return(0);
}


