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

const char *SPIT_history[] =
{
"------------------------------------------------------------------",
"                                                                  ",
" hashid - converts filename strings into smaller ids              ",
"                                                                  ",
"  Version Date   Author History:                                  ",
"  ------- ------ ------ --------                                  ",
"     0.01 940331 gshaw  alpha, not released                       ",
"     1.00 940331 gshaw  first release                             ",
"     1.01 950724 frank  max ids SPITargnum                        ",
"                                                                  ",
"------------------------------------------------------------------",
"\0"
};

/* setup up SPIT */

#include "spit.h"
#include "hashidi.h"
#include "hashidi.cpp"

const char TextVersion[]="1.01 (Samba) 2007.12.4";
#define TOOL_VERSION 101
#define TOOL_NAME "hashid"

int                     OPTHImaxidlen;
int                     OPTHImaxstrings;

#if 0
;
; ABSTRACT
;
; hashid - converts filename strings into smaller ids
;
; SUMMARY
;
; hashid -options <sourcestrings>
;   @file     response file, containing a list of options and strings
;   =file     specify a output file ie: source=dest
;   -l        specify length of id (default 4)
;   -n        specify max number of shapes (default 255)
;   -v0       no console output
;   -v1       prints source file name and errors
;   -v2       status line for each file (default)
;   -v3       additional information
;
; DESCRIPTION
;
; The program hashid converts int filename strings to smaller ids by removing
; unneeded characters.
;
; RETURNS
;
; Number of strings hashed.
;
; SEE ALSO
;
; cel2shp
;
; SAMPLE OUTPUT
;
; > hashid testfile testfile2 testfile3
; hashid v1.00 (bunny) 940331 by Gerry Shaw, EAC
; testfile = tstf
; testfile2 = tst2
; testfile3 = tst3
;
; END ABSTRACT
;
#endif

/* SPIT routines */

void SPIT_reset(void)
{
    OPTHImaxidlen = 4;
    OPTHImaxstrings = 0;
    return;
}

void SPIT_usage(const char *prgname, const char *envname, const char *urlname, int utype)
{
    printf("hashid v%s by Gerry Shaw, EAC\n", TextVersion);
    if (utype)
        printf("USAGE: %s -options [files]\n", prgname);

    switch (utype)
    {
        case 2:
              puts("    -l  length of hashed name    -n  number of unique files");
              puts("     &  response file             =  output file");
            printf("type '%s -h' for more help\n", prgname);
          printf("type '%s -help' for web page (%s)\n", prgname, urlname);
            exit(0);

        case 3:
              puts("");
            printf("VERSION: %s v%s, Spit v%.2f\n", SPIT_MACHINE, TextVersion, (float)SPIT_VERSION/100.0f);
              puts("SUMMARY: hashid converts filename strings into smaller ids");
            printf("         SPIT environment variable is %s\n", envname);
              puts("");
              puts("    @file     response file, containing a list of options and files");
            printf("    =file     specify a output file ie %s source=dest\n", prgname);
              puts("    -l        specify length of id (default 4)");
              puts("    -n        specify max number of shapes (default 255)");
              puts("    -v0       no console output");
              puts("    -v1       prints source file name and errors");
              puts("    -v2       status line for each file (default)");
              puts("    -v3       additional information");
            exit(0);

        default:
            break;
    }
}


char* SPIT_opt(const char* optptr)
{
    switch (*optptr | (char)' ')
    {
        case 'l':
            return(SPIT_getopt( &OPTHImaxidlen,optptr+1, "Maximum ID length", 1, 256));
            break;

        case 'n':
            return(SPIT_getopt( &OPTHImaxstrings,optptr+1, "Maximum strings", 1, 256));
            break;


        default:
            ;
    }
    return (char*)0;
}


int main(int argc, char* argv[])
{
    int more;
    char newname[255];
    int i=0;

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

        if (!OPTHImaxstrings)
            OPTHImaxstrings = SPITargnum;

        if (SPITargnum)
        {
            HASHID_init(OPTHImaxstrings, OPTHImaxidlen,1,0);
            for (i=0; i<SPITargnum; ++i)
            {
                if (OPTverbose>=2)
                {
                    HASHID_apply(newname, SPITsargv[i]);
                    if (OPTverbose>=3)
                        printf("%s = %s\n", SPITsargv[i], newname);
                    else
                        printf("%s\n", newname);
                }
            }
            HASHID_restore();
        }
    } while (more>0);


    return i;
}
