/*
 * $XConsortium: xbiff.c,v 1.15 89/12/12 13:55:33 rws Exp $
 *
 * Copyright 1988 Massachusetts Institute of Technology
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of M.I.T. not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  M.I.T. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * Author:  Jim Fulton, MIT X Consortium
 */

#include <stdio.h>
#include <X11/Xatom.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include "SSMailbox.h"
#include <X11/Xaw/Cardinals.h>

extern void exit();
static void quit();

char *ProgramName;

static XrmOptionDescRec options[] = {
{ "-update", "*ssmailbox.update",      XrmoptionSepArg, (caddr_t) NULL },
{ "-file",   "*ssmailbox.file",        XrmoptionSepArg, (caddr_t) NULL },
{ "-volume", "*ssmailbox.volume",      XrmoptionSepArg, (caddr_t) NULL },
{ "-shape",  "*ssmailbox.shapeWindow", XrmoptionNoArg,  (caddr_t) "on" },
{ "-sound",  "*ssmailbox.sound",       XrmoptionSepArg, (caddr_t) NULL },
{ "-noMailSound",  "*ssmailbox.noMailSound",       XrmoptionSepArg, (caddr_t) NULL },
{ "-face",   "*ssmailbox.face",        XrmoptionSepArg, (caddr_t) NULL },
/* #### I'm not sure this stuff works. */
{ "-soundBindings",   "*ssmailbox.soundBindings", XrmoptionSepArg, (caddr_t) NULL },
{ "-faceBindings",    "*ssmailbox.faceBindings",  XrmoptionSepArg, (caddr_t) NULL },
{ "-autoResize",      "*ssmailbox.autoResize",    XrmoptionNoArg, (caddr_t) "on"  },
{ "-noAutoResize",    "*ssmailbox.autoResize",    XrmoptionNoArg, (caddr_t) "off" },
{ "-caseSensitive",   "*ssmailbox.caseSensitive", XrmoptionNoArg, (caddr_t) "on"  },
{ "-noCaseSensitive", "*ssmailbox.caseSensitive", XrmoptionNoArg, (caddr_t) "off" },
{ "-caseInsensitive", "*ssmailbox.caseSensitive", XrmoptionNoArg, (caddr_t) "off" }
};

static XtActionsRec xbiff_actions[] = {
    { "quit", quit },
};
static Atom wm_delete_window;

static void Usage ()
{
    static char *help_message[] = {
"where options include:",
"    -display host:dpy              X server to contact",
"    -geometry geom                 size of mailbox",
"    -file file                     file to watch",
"    -update seconds                how often to check for mail",
"    -volume percentage             how loud to ring the bell or play a sound",
"    -bg color                      background color",
"    -fg color                      foreground color",
"    -rv                            reverse video",
"    -shape                         shape the window",
"    -sound                         sound to play for new mail",
"    -noMailSound                   sound to play when there's no new mail",
"    -soundBindings                 bindings of sounds to message header patterns,",
"    -faceBindings                  bindings of bitmaps to message header patterns,",
NULL};
    char **cpp;

    fprintf (stderr, "usage:  %s [-options ...]\n", ProgramName);
    for (cpp = help_message; *cpp; cpp++) {
	fprintf (stderr, "%s\n", *cpp);
    }
    fprintf (stderr, "\n");
    exit (1);
}


void main (argc, argv)
    int argc;
    char **argv;
{
    Widget toplevel, w;

    ProgramName = argv[0];

    toplevel = XtInitialize ("main", "XBiff++", options, XtNumber (options),
			     &argc, argv);
    if (argc != 1) Usage ();

    /*
     * This is a hack so that f.delete will do something useful in this
     * single-window application.
     */
    XtAppAddActions (XtWidgetToApplicationContext(toplevel),
                     xbiff_actions, XtNumber(xbiff_actions));
    XtOverrideTranslations(toplevel,
		   XtParseTranslationTable ("<Message>WM_PROTOCOLS: quit()"));

    w = XtCreateManagedWidget ("ssmailbox", ssmailboxWidgetClass, toplevel,
			       NULL, 0);
    XtRealizeWidget (toplevel);
    wm_delete_window = XInternAtom (XtDisplay(toplevel), "WM_DELETE_WINDOW",
                                    False);
    (void) XSetWMProtocols (XtDisplay(toplevel), XtWindow(toplevel),
                            &wm_delete_window, 1);
    XtMainLoop ();
}

static void quit (w, event, params, num_params)
    Widget w;
    XEvent *event;
    String *params;
    Cardinal *num_params;
{
    if (event->type == ClientMessage &&
        event->xclient.data.l[0] != wm_delete_window) {
        XBell (XtDisplay(w), 0);
        return;
    }
    XCloseDisplay (XtDisplay(w));
    exit (0);
}
