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

const char *SPIT_history[] =
{
"------------------------------------------------------------------",
"                                                                  ",
"                         Author v1.07                             ",
"                                                                  ",
"                    by FrANK G. Barchard, EAC                     ",
"                                                                  ",
"                   Main program - Jun 14, 2000                    ",
"                                                                  ",
"------------------------------------------------------------------",
"                                                                  ",
" Version Date   SE History:                                       ",
" ------- ----   -- --------                                       ",
" 1.00    970101 FB based on mkdate                                ",
" 1.06    000101 FB unix friendly and use registry for author      ",
" 1.07    000614 FB history and -a option                          ",
"                                                                  ",
"------------------------------------------------------------------",
"\0"
};

#if 0
/*
;
; ABSTRACT
;
; author - outputs author and date
;
; SUMMARY
;
; author <outputfile>
;    @file     response file, containing a list of options and files
;    =file     write to a file
;    -p<str>   prefix string
;    -s<str>   suffix string
;    -n<str>   name of variable (REALauthor default)
;    -a<str>   author string
;    -v0       no console output
;    -v1       prints source file name and errors
;    -v2       status line for each file (default)
;    -v3       additional info
;
; DESCRIPTION
;
; AUTHOR saves the user name and current time to a C file.
; This file can be built into a project to indicate the build date and
; author.
;
; On Windows machines, the registry is used to get the author's name.  The
; registry entry is in the key,
; "\\HKEY_CURRENT_USER\\Software\\Microsoft\\MS Setup (ACME)\\User Info",
; and it is the "DefName" entry which is used.
;
; On other platforms, an environment variable, "USER" is used.
;
; SAMPLE OUTPUT
;
; > author out.c
; > type out.c
; char *REALauthor="AUTHOR: Kenney Wong, Tuesday 04:40PM Apr 01, 1998";
;
; END ABSTRACT
;
*/
#endif

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

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "stdgimex.h"


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

#include    "spit.h"

const char TextVersion[]="1.07 (Samba) 2007.12.4";
#define TOOL_VERSION 107
#define TOOL_NAME "author"

char OPTprefix[SPITMAXLINE];
char OPTvarname[SPITMAXLINE];
char OPTsuffix[SPITMAXLINE];
char OPTauthor[SPITMAXLINE];

void mixcase(char *NameStr)
{
    int i;
    int hasupper=0;
    int haslower=0;

    for (i=0; i<(int)strlen(NameStr); ++i)
    {
        if (NameStr[i]>='A' && NameStr[i]<='Z')
            hasupper = 1;
        if (NameStr[i]>='a' && NameStr[i]<='z')
            haslower = 1;
    }
    if (!haslower)
    {
        strlwr(NameStr);
        hasupper = 0;
    }
    if (!hasupper)
    {
        int prevspace=1;
        for (i=0; i<(int)strlen(NameStr); ++i)
        {
            if (prevspace && NameStr[i]>='a' && NameStr[i]<='z')
                NameStr[i] = (char)(NameStr[i]+'A'-'a');
            prevspace = (unsigned char)NameStr[i]<=(unsigned char)' ';
        }
    }
}


#if defined(_MSC_VER)

char *getuserregistry(char *path,char *entry)
{
    int err;
    int ok;
    HKEY hKey;
    DWORD valuelen;
    unsigned char valuebuf[256];
    unsigned char *valueptr=0;

    err = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_ALL_ACCESS, &hKey);
    ok = !err;
    if (ok)
    {
        valuelen = 256;
        RegQueryValueEx(hKey, entry, 0, 0, &valuebuf[0], &valuelen);
        RegCloseKey(hKey);
        if (valuelen>1 && valuelen!=256)
            valueptr = valuebuf;
    }
    return((char *)valueptr);
}

char *getuser(char *NameStr)
{
    int  NameLength = SPITMAXLINE;
    char *s;

    GetUserName(NameStr, (LPDWORD)&NameLength);
    s = getuserregistry("Software\\Microsoft\\MS Setup (ACME)\\User Info","DefName");
    if (s)
        strcpy(NameStr,s);
    mixcase(NameStr);
    return(NameStr);
}

#else

char *getuser(char *NameStr)
{
    char *s = getenv("USER");

    if (s != NULL)
        strcpy(NameStr,s);

    mixcase(NameStr);
    return(NameStr);
}

#endif

char *PSgetdate(char *dateptr)
{
#ifdef __BEOS__
    return("date unknown");
#else
    time_t time_of_day;

    time_of_day = time( NULL );
    strftime( dateptr, SPITMAXLINE, "%A %I:%M%p %b %d, %Y", localtime( &time_of_day ) );

    return(dateptr);
#endif
}

void SPIT_reset(void)
{
    OPTminargs = 0;
    OPTbanner = 0;
    OPTkeepopts = 1;
    strcpy(OPTvarname,"REALauthor");
    strcpy(OPTprefix,"char *%s=\"AUTHOR: ");
    strcpy(OPTsuffix,"\";\n");
    strcpy(OPTauthor,"%s");

    return;
}

char *optionhelp[]=
{
    "    @file     response file, containing a list of options and files\n",
    "    =file     write to a file\n",
    "    -p<str>   prefix string\n",
    "    -s<str>   suffix string\n",
    "    -n<str>   name of variable (REALauthor default)\n",
    "    -a<str>   author string\n",
    "    -v0       no console output\n",
    "    -v1       prints source file name and errors\n",
    "    -v2       status line for each file (default)\n",
    "    -v3       additional info\n",
    "\n",
    0,
};


void SPIT_usage(const char *prgname, const char *envname, const char *urlname, int utype)
{
    char **hlp;

    printf("AUTHOR v%s (C) 1998 FrANK G. Barchard, EAC\n",TextVersion);

    if (utype)
        printf("USAGE: %s <outputfile>\n", prgname);

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

        case 2:
            puts("    -p prefix string             -v verbose");
            puts("    -s suffix string             -a author string");
          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: AUTHOR prints the user name and current time");
          printf("         SPIT environment variable is %s\n",envname);
            puts("");

            hlp = optionhelp;
            while (*hlp)
            {
                printf("%s",*hlp);
                ++hlp;
            }
            break;

         case 4:
            puts("");
            {
                hlp = optionhelp;
                while (*hlp)
                {
                    if (SPIT_strstr(*hlp,envname))
                        printf("%s",*hlp);
                    ++hlp;
                }
                puts("");
            }
            break;


        default:
            break;
    }
}


char *SPIT_opt(const char *optptr)
{
    switch (*optptr|(char)' ')
    {
        case 'a': return(SPIT_getstropt( OPTauthor,optptr+1, "author name or formatter", "", SPITYES, SPITMAXLINE));
        case 'n': return(SPIT_getstropt( OPTvarname,optptr+1, "variable name", "", SPITYES, SPITMAXLINE));
        case 'p': return(SPIT_getstropt( OPTprefix,optptr+1, "prefix string", "", SPITYES, SPITMAXLINE));
        case 's': return(SPIT_getstropt( OPTsuffix,optptr+1, "suffix string", "", SPITYES, SPITMAXLINE));
        default: ;
    }
    return((char *) 0);
}

int main(int argc,char **argv)
{
    int i;
    int isopt;
    int more;
    char author[SPITMAXLINE];
    char namebuf[SPITMAXLINE];
    char datebuf[SPITMAXLINE];
    char prefixbuf[SPITMAXLINE];
    char authorbuf[SPITMAXLINE];
    int errcode;
    const char *filename;
    int ok=0;
    GSTREAM *g;

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

        if (!SPIThelpgiven)
        {

            sprintf(prefixbuf,OPTprefix,OPTvarname);
            sprintf(authorbuf,OPTauthor,getuser(namebuf));
            if (authorbuf[0])
                strcat(authorbuf,", ");

            sprintf(author,"%s%s%s%s",prefixbuf,authorbuf,PSgetdate(datebuf),OPTsuffix);

            filename = NULL;
            for (i=0; i<SPITargnum; ++i)
            {
                isopt = 0;
                if (SPITsargv[i][0]=='-' || SPITsargv[i][0]=='+' || (SPITsargv[i][0]==OPTdot && SPITsargv[i][1]!='.' && SPITsargv[i][1]!='/' && SPITsargv[i][1]!='\\'))
                {
                    isopt = Zop(SPITsargv[i]+1)?1:0;
                }
                else if (SPITsargv[i][0]=='=')
                {
                    isopt = Zop(SPITsargv[i])?1:0;
                    filename = SPITsargv[i]+1;
                }

                if (!isopt)
                {
                    filename = SPITdargv[i];
                    break;
                }
            }
            if (filename)
            {
                g = gwopen(filename);
                if (g)
                {
                    ok = gwrite(g,author,strlen(author));
                    gclose(g);
                }
                if (OPTverbose>=4)
                    printf("%s\n",author);
            }
            else
            {
                if (OPTverbose)
                    printf("%s\n",author);
                ok = 1;
            }
        }
    } while (more>0);

    errcode = ok?0:100;
    return(errcode);
}

