/*
    Ixprefs v.2.4--ixemul.library configuration program
    Copyright © 1995,1996 Kriton Kyrimis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <exec/types.h>
#include <dos/dos.h>
#include <intuition/intuition.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/gadtools.h>
#include "ixemul.h"
#include "ixprefs.h"

void cleanup(void);
void displayprefs(void);
void showrequester(struct Window*, char *, char *);
extern int parse_cli_commands(int argc, char *argv[]);

extern struct ixemul_base *ixemulbase;
int translateslash, membuf, blocks, cases, suppress,
    amigawildcard, noflush, ignoreenv, networking, enforcerhit,
    profilemethod;

char ixprefs_version[] = "$VER: ixprefs 2.4 (23.09.96)";

int
main(int argc, char *argv[])
{
  long status;

  if (ixemulbase->ix_lib.lib_Version < 45) {
    showrequester(NULL,
	"This program requires ixemul.library version 45 or higher",
	"EXIT");
    return RETURN_FAIL;
  }

  (void)ixprefsrestore();	/* load ixprefs settings from ixemulbase */
  
  if (argc >= 2) {
    return parse_cli_commands(argc, argv);
  }

  atexit(cleanup);

  status = SetupScreen();
  if (status != 0) {
    fprintf(stderr, "SetupScreen failed, status = %ld\n", status);
    return RETURN_FAIL;
  }
  ixprefsTop=Scr->BarHeight+1;
  status = OpenixprefsWindow();
  if (status != 0) {
    fprintf(stderr, "OpenixprefsWindow failed, status = %ld\n", status);
    return RETURN_FAIL;
  }

  displayprefs();

  while (1) {
    WaitPort(ixprefsWnd->UserPort);
    status = HandleixprefsIDCMP();
    if (status == 0) {
      return RETURN_OK;
    }
  }
  return 0;
}

void
cleanup()
{
  CloseixprefsWindow();
  CloseDownScreen();
}

void
check(int which)
{
  ixprefsGadgets[which]->Flags |= GFLG_SELECTED;
}

void
uncheck(int which)
{
  ixprefsGadgets[which]->Flags &= ~GFLG_SELECTED;
}

void
showchecked(int which, int checkit)
{
  if (checkit) {
    check(which);
  }else{
    uncheck(which);
  }
  RefreshGList(ixprefsGadgets[which], ixprefsWnd, NULL, 1);
}

void
shownum(int which, int num)
{
  sprintf(GetString(ixprefsGadgets[which]), "%d", num);
  GetNumber(ixprefsGadgets[which]) = num;
  RefreshGList(ixprefsGadgets[which], ixprefsWnd, NULL, 1);
}

void
showcycle(int which, int num)
{
  GT_SetGadgetAttrs(ixprefsGadgets[which], ixprefsWnd, NULL,
		    GTCY_Active, num, TAG_DONE);
  RefreshGList(ixprefsGadgets[which], ixprefsWnd, NULL, 1);
}

void
displayprefs(void)
{
  if (Scr == NULL)
    return;
  showchecked(GDX_amigawildcard, amigawildcard);
  showchecked(GDX_case, cases);
  showchecked(GDX_translateslash, translateslash);
  shownum(GDX_membuf, membuf);
  shownum(GDX_blocks, blocks);
  showchecked(GDX_suppress, suppress);
  showchecked(GDX_noflush, noflush);
  showchecked(GDX_ignoreenv, ignoreenv);
  showchecked(GDX_enforcerhit, enforcerhit);
  showcycle(GDX_networking, networking);
  showcycle(GDX_profilemethod, profilemethod);
}

void
showrequester(struct Window *win, char *text, char *buttontext)
{
  static struct EasyStruct easy = {
    sizeof(struct EasyStruct),
    0,
    "ixprefs",
    NULL,
    NULL,
  };

  if (win /*|| strchr(buttontext, '|')*/) {
    easy.es_TextFormat = (UBYTE *)text;
    easy.es_GadgetFormat = (UBYTE *)buttontext;
    EasyRequest(NULL, &easy, NULL);
  }else{
    fprintf(stderr, "%s\n", text);
  }
}
