
/* 
   This is a program to show a small clock on the workbench screen telling
   you what to do during the day.

   This is totally public domain; do as you wish with the source and
   executable. Would you pay money for it?
*/



struct NewWindow newwin = {
	100,30,		/* window XY origin relative to TopLeft of screen */
	460,11,		/* window width and height */
	0,1,		/* detail and block pens */
	CLOSEWINDOW+ACTIVEWINDOW+INACTIVEWINDOW,	/* IDCMP flags */
	WINDOWCLOSE+RMBTRAP+WINDOWDRAG
        +NOCAREREFRESH+SMART_REFRESH,			/* other window flags */
	NULL,		/* first gadget in gadget list */
	NULL,		/* custom CHECKMARK imagery */
	NULL,		/* window title */
	NULL,		/* custom screen pointer */
	NULL,		/* custom bitmap */
	5,5,		/* minimum width and height */
	640,256,	/* maximum width and height */
	WBENCHSCREEN	/* destination screen type */
};

struct MsgPort *timeport;
struct timerequest timereq;
void *IntuitionBase, *GfxBase;
struct IntuiMessage *msg;
struct Window *window;
struct RastPort *rp;
ULONG class;
struct { int min,stop; } start,mtea,lunch,atea,quit;

main()
{
  readfile();
  IntuitionBase = OpenLibrary("intuition.library",0);
  GfxBase = OpenLibrary("graphics.library",0);

  window = OpenWindow(&newwin); rp = window->RPort;
  SetWindowTitles(window,(void *)-1,"The Public Servant's Clock V1.0 by Michael Warner");
  SetDrMd(rp,JAM1); 
  drawtime();
  timeport = CreatePort("clk_timeport",0);
  OpenDevice("timer.device",0,&timereq,0);
  timereq.tr_node.io_Message.mn_ReplyPort = timeport;
  timereq.tr_node.io_Command=TR_ADDREQUEST;  
  timereq.tr_node.io_Flags=0; timereq.tr_node.io_Error=0;
  timereq.tr_time.tv_secs = 60; timereq.tr_time.tv_micro = 0;
  SendIO(&timereq);

  FOREVER {
    Wait(1<<window->UserPort->mp_SigBit|1<<timeport->mp_SigBit);
    if (msg=GetMsg(window->UserPort)) {
      class=msg->Class; ReplyMsg(msg);
      switch (class) {
        case ACTIVEWINDOW:
        case INACTIVEWINDOW: drawtime(); break;
        case CLOSEWINDOW: die("bye"); break;
      }
    }
    else {
      WaitIO(&timereq);		
      drawtime();
      timereq.tr_time.tv_secs = 60;
      timereq.tr_time.tv_micro = 0;
      SendIO(&timereq);
    }
  }
}


die(str)
  char *str;
{
  if (strlen(str)) puts(str);
  if (timeport) {
    if (!CheckIO(&timereq)) {
    AbortIO(&timereq); WaitIO(&timereq);
    }
    CloseDevice(&timereq);
    DeletePort(timeport);
  }
  if (window) CloseWindow(window);
  exit(0);
}


readfile()
{
  FILE *file;
  int p1,p2,p3,p4;
  char ch,str[80];

  file = fopen("s:PublicService.dat","r"); 
  if (!file) die("Couldn't find s:PublicService.dat");

  while (!feof(file)) {
    fgets(str,80,file);
    sscanf(str,"%s %d.%d %d.%d\n",&ch,&p1,&p2,&p3,&p4);
    p1 = p1*60+p2; p2 = p3*60+p4;	/* convert to minutes */
    switch (ch) {
      case 'S': start.min=p1;break;
      case 'M': mtea.min=p1; mtea.stop=p2; break;
      case 'L': lunch.min=p1; lunch.stop=p2; break;
      case 'A': atea.min=p1; atea.stop=p2; break;
      case 'Q': quit.min=p1; break;
    }
  }
  fclose(file);
}


drawtime()		/* draw time in window */
{
  int min;
  static struct DateStamp ds;
  static char str[160];

  DateStamp((long *)&ds); min = ds.ds_Minute; 
  str[0]=0;

  if (min<start.min) { drawstring("Are you here ALREADY?"); return(0); }
  if (min<mtea.min) {
    interval(str,mtea.min-min); strcat(str," until morning tea"); 
    drawstring(str); return(0);
  }
  if (min<mtea.stop) { drawstring("You should be at morning tea!"); return(0); }
  if (min<lunch.min) {
    interval(str,lunch.min-min); strcat(str," until lunch"); 
    drawstring(str); return(0);
  }
  if (min<lunch.stop) { drawstring("You should be at lunch!"); return(0); }
  if (min<atea.min) {
    interval(str,atea.min-min); strcat(str," until afternoon tea"); 
    drawstring(str); return(0);
  }
  if (min<atea.stop) { drawstring("You should be at afternoon tea!"); return(0); }
  if (min<quit.min) {
    interval(str,quit.min-min); strcat(str," until knockoff time"); 
    drawstring(str); return(0);
  }
  drawstring("Don't you have a home to go to?");  return(0);
}

drawstring(str)
  char *str;
{
  register int i;

  SetRast(rp,1); SetAPen(rp,0); 
  Move(rp,4,7); Text(rp,str,strlen(str));
}

char *units[20] = {" ","one","two","three","four","five",
                  "six","seven","eight","nine","ten","eleven",
                  "twelve","thirteen","fourteen","fifteen",
                  "sixteen","seventeen","eighteen","nineteen"};

char *tens[6] = {" "," ","twenty","thirty","forty","fifty"};


interval(str,minutes)
  char *str; int minutes;
{
  register int hr,min;

  hr = minutes/60; min = minutes%60;

  if (hr) {
    strcpy(str,units[hr]);
    strcat(str," hour"); if (hr>1) strcat(str,"s");
  }
  if (min) {
    if (hr) strcat(str," and ");
    if (min<20) {
      strcat(str,units[min]);
    }
    else {
      strcat(str,tens[min/10]);
      if (min%10) { 
        strcat(str,"-"); strcat(str,units[min%10]); 
      }
    }
    strcat(str," minute"); if (min>1) strcat(str,"s");
  }
}

