/*
 *    SNagLite
 *   A cron-companion
 *   (C) 1996 Martin Samuelsson
 *   All rights reserved.
 */

/* Don't laugh! */
#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/displayinfo.h>
#include <graphics/displayinfo.h>
#include <dos/dos.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <clib/icon_protos.h>
#include <clib/wb_protos.h>
#include <clib/dos_protos.h>
#include <clib/diskfont_protos.h>
#include <string.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <workbench/icon.h>
#include <stdio.h>

#include "SNagLite.h"

char text[80], junk[80], path[80];
char VerStr[]="$VER: SNagLite 1.0 (C) 1996 Martin Samuelsson";
char *TThanks;
char *RThanks="You're welcome.";
char *TRemindme;
char *RRemindme="I'll be back!";
char *TEdit;
char *REdit="Editor coming up!";
char *REditFailN="Could not start editor.";
char *REditFailP="Editor error.";
char *REditNoFail="CronTab edited.";
char *NoReason="I don't have anything to nag about!";
//char *TDelete="_Delete";
//char *RForget="Entry deleted.";
//char *TKeep="_Keep";
//char *RKeep="Entry kept.";
//char *title2="Delete this line?";
char *crontab="S:CronTab";
char *commandline="Sound SYS:SNagLite/ROOSTER_IFF";
char *editor="C:Ed -sticky";

int Waittime = 150;

LONG EditRes = 0;
UBYTE justification = GTJ_CENTER;
BOOL running = TRUE;

//struct DOSBase *DOSBase;
//struct Library *IntuitionBase;
//struct GadToolsBase *GadToolsBase;
//struct IconBase *IconBase;
struct WorkbenchBase *WorkbenchBase;
struct Process *I;

/*
 If an error occurs when starting from WB:
   Two beeps: workbench.library less than v39.
 Three beeps: No wbmsg, very unlikely.
  Four beeps: wbmsg->sm_ArgList is NULL, should not happen either.
  Five beeps: wbarg->wa_Lock is NULL.
   Six beeps: The Info-window failed.
*/

BeepOut(int times)
{
 int olll;
 while(olll<times) {
  DisplayBeep(NULL);
  Delay(12);
  olll++;
 }
 if(WorkbenchBase) {
  CloseLibrary(WorkbenchBase);
  WorkbenchBase=NULL;
 }
 exit(20);
}

wbmain(struct WBStartup *wbmsg)
{
 struct WBArg *wbarg;
 struct FileLock *icondir;
 char *iconname;
 if(wbmsg==NULL) BeepOut(3);
 if((WorkbenchBase = OpenLibrary(WORKBENCH_NAME,39))!=NULL) {

  wbarg = wbmsg->sm_ArgList;
  if(wbarg==NULL) BeepOut(4);
  iconname = wbarg->wa_Name;
  icondir = (struct FileLock *) wbarg->wa_Lock;
  if(icondir==NULL) BeepOut(5);

  if((ULONG)WBInfo((BPTR)icondir, iconname, IntuitionBase->ActiveScreen)==FALSE) BeepOut(6);

  CloseLibrary(WorkbenchBase);
  WorkbenchBase=NULL;
 } else {
  BeepOut(2);
 }
 exit(0);
}

struct DiskObject *
GetConf(char *progname)
{
 struct DiskObject *DOb;
 char *tooltype;

 if((DOb = GetDiskObject(progname))==NULL) {
  return(0);
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "TTHANKS")) {
  TThanks = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "RTHANKS")) {
  RThanks = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "TREMINDME")) {
  TRemindme = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "RREMINDME")) {
  RRemindme = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "TEDIT")) {
  TEdit = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "REDIT")) {
  REdit = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "REDITFAILN")) {
  REditFailN = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "REDITFAILP")) {
  REditFailP = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "REDITNOFAIL")) {
  REditNoFail = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "NOREASON")) {
  NoReason = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "TITLE")) {
  MainWdt = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "CRONTAB")) {
  crontab = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "COMMAND")) {
  commandline = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "EDITOR")) {
  editor = tooltype;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "WAITTIME")) {
  Waittime = (atoi(tooltype))*50;
 }
 if (tooltype = FindToolType(DOb->do_ToolTypes, "JUSTIFICATION")) {
  if(stricmp("right",tooltype)==NULL) {
   justification = GTJ_RIGHT;
  }
  if(stricmp("center",tooltype)==NULL) {
   justification = GTJ_CENTER;
  }
  if(stricmp("left",tooltype)==NULL) {
   justification = GTJ_LEFT;
  }
 
 }
 return(DOb);
}

SetConf()
{
 MainNGad[GD_Thanks].ng_GadgetText = TThanks;
 MainNGad[GD_Remind].ng_GadgetText = TRemindme;
 MainNGad[GD_ForgetIt].ng_GadgetText = TEdit;
}

main(int argc, char **argv)
{
 BOOL Reason = TRUE;
 struct DiskObject *DOb;
 if(strcmp("?",argv[argc-1])==NULL) {
  printf("%s\n",VerStr+6);
  printf("A Cron companion.\n");
  printf("All rights reserved.\n");
  printf("SNagLite nagstring/F\n");
  exit(0);
 }
 if((I=(struct Process *)FindTask(NULL))==NULL) {
  printf("FindTask error.\n");
  exit(20);
 }
 if(NameFromLock(I->pr_HomeDir,path,80)==FALSE) {
  printf("NameFromLock error.\n");
  exit(20);
 }
 sprintf(junk,"%s/%s",path,FilePart(argv[0])); /* Probably equivalent to AddPart() */
 if((DOb = GetConf(junk))!=NULL) {
  SetConf();
 }
 SetupScreen();
 int arg = 1;
 while(arg<argc) {
  if(strlen(text)+strlen(argv[arg]) < 80) {
   strcat(text,argv[arg]);
   if(arg!=argc-1) {
    strcat(text," ");
   }
  } else {
   arg=argc;
  }
  arg++;
 }
 if(strlen(text)==0) {
  strcpy(text,NoReason);
  Reason = FALSE;
 }
 Appear(Reason);
 while(running == TRUE) {
  WaitPort(MainWnd->UserPort);
  running = HandleMainIDCMP();
 }
 CloseMainWindow();
 CloseDownScreen();
 if(DOb) FreeDiskObject(DOb);
 exit(0);
}

Appear(BOOL Reason)
{
 OpenMainWindow();
 GT_SetGadgetAttrs(MainGadgets[GD_Info], MainWnd, NULL, GTTX_Justification, justification, GTTX_Text, text, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_Remind], MainWnd, NULL, GA_Disabled, FALSE, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_ForgetIt], MainWnd, NULL, GA_Disabled, FALSE, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_Thanks], MainWnd, NULL, GA_Disabled, FALSE, TAG_DONE);
 if(Reason == FALSE) {
  GT_SetGadgetAttrs(MainGadgets[GD_Remind], MainWnd, NULL, GA_Disabled, TRUE, TAG_DONE);
 }
 if(Reason == TRUE) {
  SystemTags(commandline,TAG_DONE);
 }
}

Thanks()
{
 GT_SetGadgetAttrs(MainGadgets[GD_Info], MainWnd, NULL, GTTX_Text, RThanks, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_Remind], MainWnd, NULL, GA_Disabled, TRUE, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_ForgetIt], MainWnd, NULL, GA_Disabled, TRUE, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_Thanks], MainWnd, NULL, GA_Disabled, TRUE, TAG_DONE);
 Delay(50);
}

Remindme()
{
 GT_SetGadgetAttrs(MainGadgets[GD_Info], MainWnd, NULL, GTTX_Text, RRemindme, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_Remind], MainWnd, NULL, GA_Disabled, TRUE, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_ForgetIt], MainWnd, NULL, GA_Disabled, TRUE, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_Thanks], MainWnd, NULL, GA_Disabled, TRUE, TAG_DONE);
 Delay(50);
 CloseMainWindow();
 Delay(Waittime);
 Appear(TRUE);
}

Forgetit()
{
 GT_SetGadgetAttrs(MainGadgets[GD_Info], MainWnd, NULL, GTTX_Text, REdit, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_Remind], MainWnd, NULL, GA_Disabled, TRUE, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_ForgetIt], MainWnd, NULL, GA_Disabled, TRUE, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_Thanks], MainWnd, NULL, GA_Disabled, TRUE, TAG_DONE);
 strcpy(junk,editor);
 strcat(junk," ");
 strcat(junk,crontab);
//sprintf(junk,"%s %s",editor,crontab);
 EditRes = system(junk);
//GT_SetGadgetAttrs(MainGadgets[GD_Remind], MainWnd, NULL, GA_Disabled, FALSE, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_ForgetIt], MainWnd, NULL, GA_Disabled, FALSE, TAG_DONE);
 GT_SetGadgetAttrs(MainGadgets[GD_Thanks], MainWnd, NULL, GA_Disabled, FALSE, TAG_DONE);
 if(EditRes > 0) {
  GT_SetGadgetAttrs(MainGadgets[GD_Info], MainWnd, NULL, GTTX_Text, REditFailP, TAG_DONE);
 }
 if(EditRes < 0) {
  GT_SetGadgetAttrs(MainGadgets[GD_Info], MainWnd, NULL, GTTX_Text, REditFailN, TAG_DONE);
 }
 if(EditRes == 0) {
  GT_SetGadgetAttrs(MainGadgets[GD_Info], MainWnd, NULL, GTTX_Text, REditNoFail, TAG_DONE);
 }
}
