/*
 * LEGAL NOTICE
 *
 * Copyright (c) 1994 WSC Investment Services, Inc.
 * All Rights Reserved.
 * 
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND WSC INVESTMENT SERVICES (WSCIS)
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT
 * SHALL WSCIS BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR 
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE 
 * USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Users of this software agree to return to WSCIS any improvements 
 * or extensions that they make and grant WSCIS the rights to 
 * redistribute these changes.
 *
 * Export of this software is permitted only after complying with the
 * regulations of the U.S. Deptartment of Commerce relating to the
 * Export of Technical Data.
 */

#import "HKSendPr.h"
#import <hk/HKArchInfo.h>
#import <hk/util.h>
#import <pwd.h>
#import <libc.h>

#define ORGANIZATION	"DEFAULT_ORGANIZATION"


@implementation HKSendPr

- init
{
    [super init];

    emailTo = NULL;
    emailFrom = NULL;
    emailReply = NULL;

    submitterID = NULL;
    originator = NULL;
    organization = NULL;
    confidential = NO;
    synopsis = NULL;
    severity = HK_NonCriticalSeverity;
    priority = HK_LowPriority;
    category = NULL;
    bugClass = HK_SoftwareBug;
    release = NULL;
    archInfo = [[HKArchInfo alloc] init];

    description = NULL;
    howToRepeat = NULL;
    howToFix = NULL;

    [self setup];
    return self;
}


- free
{
    HK_SET(emailTo, NULL);
    HK_SET(emailFrom, NULL);
    HK_SET(emailReply, NULL);
    HK_SET(submitterID, NULL);
    HK_SET(originator, NULL);
    HK_SET(synopsis, NULL);
    HK_SET(category, NULL);
    HK_SET(release, NULL);
    HK_SET(description, NULL);
    HK_SET(howToRepeat, NULL);
    HK_SET(howToFix, NULL);

    [archInfo free];

    return [super free];
}

- copyFromZone:(NXZone *)zone
{
    typedef struct {
	@defs(HKSendPr)
    } _HKSendPr;

    id aCopy = [super copyFromZone:zone];

    ((_HKSendPr *)aCopy)->emailTo = NULL;
    ((_HKSendPr *)aCopy)->emailFrom = NULL;
    ((_HKSendPr *)aCopy)->emailReply = NULL;
    ((_HKSendPr *)aCopy)->submitterID = NULL;
    ((_HKSendPr *)aCopy)->originator = NULL;
    ((_HKSendPr *)aCopy)->synopsis = NULL;
    ((_HKSendPr *)aCopy)->category = NULL;
    ((_HKSendPr *)aCopy)->release = NULL;
    ((_HKSendPr *)aCopy)->description = NULL;
    ((_HKSendPr *)aCopy)->howToRepeat = NULL;
    ((_HKSendPr *)aCopy)->howToFix = NULL;

    [aCopy setEmailTo:emailTo];
    [aCopy setEmailFrom:emailFrom];
    [aCopy setEmailReply:emailReply];
    [aCopy setSubmitterID:submitterID];
    [aCopy setOriginator:originator];
    [aCopy setSynopsis:synopsis];
    [aCopy setCategory:category];
    [aCopy setRelease:release];
    [aCopy setDescription:description];
    [aCopy setHowToRepeat:howToRepeat];
    [aCopy setHowToFix:howToFix];

    ((_HKSendPr *)aCopy)->archInfo = [archInfo copyFromZone:zone];

    return aCopy;
}


- setup;
{
    const char *ptr;
    struct passwd *pwd;
    
    HK_SET(submitterID, "net");
    confidential = NO;
    severity = HK_NonCriticalSeverity;
    priority = HK_LowPriority;
    bugClass = HK_SoftwareBug;

    pwd = getpwuid(getuid());
    if (pwd) {
	char buffer[100];

	if (!(ptr = pwd->pw_gecos)) {
	    ptr = pwd->pw_name;
	}
	if (ptr) {
	    HK_SET(originator, ptr);
	}

	sprintf(buffer, "%s <%s>",
		HK_CHECK(pwd->pw_gecos),
		HK_CHECK(pwd->pw_name));
	HK_SET(emailFrom, buffer);
	HK_SET(emailReply, buffer);
    }

    
    ptr = getenv(ORGANIZATION);
    if (ptr) {
	HK_SET(organization, ptr);
    }

    return self;
}


- (const char *)emailTo;
{
    return emailTo;
}
- (const char *)emailFrom;
{
    return emailFrom;
}
- (const char *)emailReply;
{
    return emailReply;
}
- (const char *)submitterID;
{
    return submitterID;
}
- (const char *)originator;
{
    return originator;
}
- (const char *)organization;
{
    return organization;
}
- (BOOL)confidential;
{
    return confidential;
}
- (const char *)synopsis;
{
    return synopsis;
}
- (HKSeverity)severity;
{
    return severity;
}
- (HKPriority)priority;
{
    return priority;
}
- (const char *)category;
{
    return category;
}
- (HKBugClass)bugClass;
{
    return bugClass;
}
- (const char *)release;
{
    return release;
}
- (const char *)environment;
{
    return [archInfo environment];
}
- (const char *)system;
{
    return [archInfo system];
}
- (const char *)architecture;
{
    return [archInfo architecture];
}
- (const char *)description;
{
    return description;
}
- (const char *)howToRepeat;
{
    return howToRepeat;
}
- (const char *)howToFix;
{
    return howToFix;
}


- setEmailTo:(const char *)emailAddress;
{
    HK_SET(emailTo, emailAddress);
    return self;
}

- setEmailFrom:(const char *)emailAddress;
{
    HK_SET(emailFrom, emailAddress);
    return self;
}

- setEmailReply:(const char *)emailAddress;
{
    HK_SET(emailReply, emailAddress);
    return self;
}

- setSubmitterID:(const char *)aSubmitter;
{
    HK_SET(submitterID, aSubmitter);
    return self;
}

- setOriginator:(const char *)anOriginator;
{
    HK_SET(originator, anOriginator);
    return self;
}

- setOrganization:(const char *)anOrganization;
{
    HK_SET(organization, anOrganization);
    return self;
}

- setConfidential:(BOOL)aFlag;
{
    confidential =  confidential;
    return self;
}

- setSynopsis:(const char *)aSynopsis;
{
    HK_SET(synopsis, aSynopsis);
    return self;
}

- setSeverity:(HKSeverity)severityLevel;
{
    severity = severityLevel;
    return self;
}
    
- setPriority:(HKPriority)aPriority;
{
    priority = aPriority;
    return self;
}

- setCategory:(const char *)aCategory;
{
    HK_SET(category, aCategory);
    return self;
}

- setBugClass:(HKBugClass)aBugClass;
{
    bugClass = aBugClass;
    return self;
}

- setRelease:(const char *)aRelease;
{
    HK_SET(release, aRelease);
    return self;
}

- setDescription:(const char *)aDescription;
{
    HK_SET(description, aDescription);
    return self;
}

- setHowToRepeat:(const char *)aDescription;
{
    HK_SET(howToRepeat, aDescription);
    return self;
}

- setHowToFix:(const char *)aDescription;
{
    HK_SET(howToFix, aDescription);
    return self;
}


- (BOOL)send
{
    return [HKMailComposer sendMailTo:emailTo
			withContent:self];
}


#define HK_ISEMPTY(a)  ((a) ? ((strlen(a) == 0)?YES:NO) : YES)

- writeMailToStream:(FILE *)stream;
{
    const char *HKSeverity_String[] = {
	"non-critical",
	"serious",
	"critical",
	0
    };

    const char *HKPriority_String[] = {
	"low",
	"medium",
	"high",
	0
    };

    const char *HKBugClass_String[] = {
	"sw-bug",
	"doc-bug",
	"change-request",
	"support",
	0
    };

//    Email To is already set when calling sendMailTo: withContent:
//    fprintf(stream, "To: %s\n", HK_CHECK(emailTo));
    fprintf(stream, "Subject: %s\n", HK_CHECK(synopsis));
    fprintf(stream, "From: %s\n", HK_CHECK(emailFrom));
    fprintf(stream, "Reply-To: %s\n", HK_CHECK(emailReply));
    fprintf(stream, "\n");
    fprintf(stream, ">Submitter-Id: %s\n", HK_CHECK(submitterID));
    fprintf(stream, ">Originator:   %s\n", HK_CHECK(originator));
    fprintf(stream,
	    ">Organization:%s%s\n",
	    HK_ISEMPTY(organization)?"":"\n",
	    HK_CHECK(organization));

    fprintf(stream, ">Confidential: %s\n", confidential?"yes":"no");
    fprintf(stream, ">Synopsis:     %s\n", HK_CHECK(synopsis));

    fprintf(stream, ">Severity:     %s\n", HKSeverity_String[severity]);
    fprintf(stream, ">Priority:     %s\n", HKPriority_String[priority]);
    fprintf(stream, ">Category:     %s\n", HK_CHECK(category));
    fprintf(stream, ">Class:        %s\n", HKBugClass_String[bugClass]);
    fprintf(stream, ">Release:      %s\n", HK_CHECK(release));

    fprintf(stream,
	    ">Environment:%s%s\n",
	    HK_ISEMPTY([archInfo environment])?"":"\n",
	    HK_CHECK([archInfo environment]));
    fprintf(stream, ">System:       %s\n", HK_CHECK([archInfo system]));
    fprintf(stream, ">Architecture: %s\n", HK_CHECK([archInfo architecture]));

    fprintf(stream,
	    ">Description:%s%s\n",
	    HK_ISEMPTY(description)?"":"\n",
	    HK_CHECK(description));
    fprintf(stream,
	    ">howToRepeat:%s%s\n",
	    HK_ISEMPTY(howToRepeat)?"":"\n",
	    HK_CHECK(howToRepeat));
    fprintf(stream,
	    ">howToFix:%s%s\n",
	    HK_ISEMPTY(howToFix)?"":"\n",
	    HK_CHECK(howToFix));

    return self;
}

    
    

@end
