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

/*------------------------------------------------------------------*/
/*                                                                  */
/*                      ACDsee Frontend v1.03                       */
/*                                                                  */
/*                     by Frank Barchard, EAC                       */
/*                                                                  */
/*                   Main program - Sep 29, 2001                    */
/*                                                                  */
/*------------------------------------------------------------------*/
/*                                                                  */
/* Version Date   SE History:                                       */
/* ------- ----   -- --------                                       */
/*    1.00 000404 FB initial version based on DB                    */
/*    1.01 000503 FB cleaned up printfs                             */
/*    1.02 000529 FB quote name                                     */
/*    1.03 010929 FB doc and ash                                    */
/*                                                                  */
/*------------------------------------------------------------------*/


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

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

const char TextVersion[]="1.03 (Samba) 2007.12.4";

static void expandtofullpath(char *fullname,const char *pathname)
{
    char name[512];
    char *newname;

    GetFullPathName(pathname,512,name,&newname);
    strcpy(fullname,name);
    if (OPTverbose>=4)
        printf("name %s (%s) <- %s\n",name,newname,pathname);

    return;
}



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

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

char *ACDC_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 ACDC_fileexist(char *nextname)
{
    int ok=0;
    GSTREAM *g;
    g = gopen(nextname);
    if (g)
    {
        ok = 1;
        gclose(g);
    }
    return(ok);
}


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

    int x=0;
    char *extensions[]=
    {
        "",
        ".3df",".444",".act",".ai",".als",".ani",".anim",".anm",".avi",
        ".bam",".bbm",".bif",".bmp",".cel",".chr",".clp",".cmp",".cmv",
        ".cur",".dct",".dcx",".dds",".dsh",".eps",".epsf",".exe",".ffn",
        ".ffs",".flc",".fli",".flm",".fsh",".gif",".gmx",".gs",".hsi",".icb",
        ".ico",".idec",".iff",".ijl",".ijpg",".ilbm",".inv",".ipu",".jfif",
        ".jpeg",".jpg",".jpg",".kdc",".lay",".lbm",".mad",".mat",".mdl",
        ".mg",".moov",".mov",".mpeg",".mpg",".msh",".mvi",".new",".nfn",
        ".nsh",".omf",".pal",".pal",".pbm",".pcc",".pct",".pcx",".pdd",
        ".pfn",".pfs",".pgm",".pic",".pic",".pict",".pix",".png",".pnm",
        ".ppm",".ps",".psd",".psh",".pvr",".qt",".raw",".raw",".rgb",".rgs",
        ".rla",".rlb",".rle",".s3d",".s3t",".s3tc",".sani",".sc",".sct",
        ".sfn",".sfs",".sgi",".sim",".ssh",".str",".tga",".tga",".tgq",
        ".tgv",".tif",".tiff",".tim",".tqi",".twai",".twn",".txp",".txs",
        ".txt",".uv",".van",".vda",".vqt",".vram",".vrm",".vsh",".vst",
        ".xa",".xar",".yuv",".tpl",".ion",".ash",
        "",0,
    };

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

    return(nextname);
}


#if 0
/*
;
; ABSTRACT
;
; acdc - tool to launch acdsee
;
; SUMMARY
;
; acdc [-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
;
; DESCRIPTION
;
; A command line tool to launch ACDsee.
; ACDsee is normally launched from the GUI, and isn't in the path.
; ACDC simply launches ACDsee using 'start acdsee', passing any arguments
; along.
;
; RETURNS
;
; 0 if no errors else number of errors
;
; SEE ALSO
;
; e, msdev
;
; SAMPLE OUTPUT
;
; acdc bob
;
; END ABSTRACT
;
*/
#endif

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

char *optionhelp[]=
{
    "     -v0            no console output\n",
    "     -v             more verbose\n",
    0,
};


void ACDC_usage(void)
{
    char **hlp;

    printf("ACDC v%s by Frank Barchard,EAC\n",TextVersion);
    printf("USAGE: ACDC -options [files]\n");
    puts("");
    printf("VERSION: %s v%s\n",SPITmachine,TextVersion);
    printf("SUMMARY: ACDC expands pathname/extensions and launches ACDsee\n");
    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]=='?')
        {
            ACDC_usage();
            exit(10);
        }
    }

    strcpy(commandline,"start acdsee5 ");

    /* get all file names */
    for (i=1; i<argc; ++i)
    {
        char fullname[512];
        nextname = argv[i];
        if (first && !ACDC_isoption(argv[i]))
        {
            nextname = ACDC_findfile(argv[i]);
            first = 0;
        }

        expandtofullpath(fullname,nextname);

        strcat(commandline,"\"");
        strcat(commandline,fullname);
        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] = "acdc";
    argv[1] = lpszCmdLine;
    return main(2,argv);
}
#endif

