Program AlarmClock;

Uses Crt,DOS,Exec,Graphics,Intuition;

{$I style.inc}

Const
  VNM='1.01';
  VDT='01-Jun-94';

Type
  tFont=Record
    Font_Name:String; {font_name}
    Font_No:Integer;  {TOPAZ_SIXTY}
    Font_Ddd1:ShortInt; {FS_NORMAL}
    Font_Ddd2:ShortInt; {FPF_ROMFONT}
  End;

Var
  Close:pIntuiMessage;
  MyNewWindow:tNewWindow;
  FontName:String;
  WTitle,STitle:String;
  MyFont:tFont;
  MyWindow: pWindow;
  L:LongInt;
  ERR,AHR,AMN:Integer;
  FineSec,DayNum:Word;
  DHR,DMN:String[2];
  DT:DateTime;
  QTF,ABORT:Boolean;

Procedure WriteWin(XP,YP,PC:Integer;TXT:String);
Begin
  With MyWindow^ Do
  Begin
    SetAPen(RPort,PC);
    Move_(RPort,XP,YP);
    Text_(RPort,@TXT,Length(TXT));
  End;
End;

Procedure GetParams;
Begin
  If ParamStr(1)='?' Then
  Begin
    Style(0,1);
    Style(1,1);
    Style(3,1);
    WriteLn('AlarmClock v',VNM,' by Zebedee ',#$a9,'1994 Cybertek (',VDT,')');
    Style(0,1);
    WriteLn;
    Style(2,1);
    WriteLn('Usage: ALARM HH:MM');
    Style(0,1);
    Halt(10);
    Write('$VER: Alarm v1.01 (01-Jun-94) ');
  End;
  If ParamCount<>1 Then
  Begin
    WriteLn('Error: Invalid alarm time');
    Halt(0);
  End;
  Val(Copy(ParamStr(1),1,2),AHR,ERR);
  DHR:=Copy(ParamStr(1),1,2);
  If ERR<>0 Then
  Begin
    WriteLn('ERROR: Invalid hours');
    Halt(10);
  End;
  Val(Copy(ParamStr(1),4,2),AMN,ERR);
  DMN:=Copy(ParamStr(1),4,2);
  If ERR<>0 Then
  Begin
    WriteLn('ERROR: Invalid minutes');
    Halt(10);
  End;
End;

Procedure DoTheBusiness;
Var
  LB:LongInt;
  DAYNAME:String[10];
  INFO:String[22];
  Sec100:Word;
Begin
  QTF:=False;
  With MyWindow^ Do
  Begin
    LB:=BitMask(UserPort^.MP_SIGBIT);
  End;
  Repeat
    GetTime(DT.Hour,DT.Min,DT.Sec,FineSec);
    GetDate(DT.Year,DT.Month,DT.Day,DayNum);
    Case DayNum Of
      0:DAYNAME:='Sun';
      1:DAYNAME:='Mon';
      2:DAYNAME:='Tue';
      3:DAYNAME:='Wed';
      4:DAYNAME:='Thu';
      5:DAYNAME:='Fri';
      6:DAYNAME:='Sat';
    End;
    INFO:=DAYNAME+' ';
    Str(DT.Day,DAYNAME);
    If DT.Day<10 Then INFO:=INFO+'0';
    INFO:=INFO+DAYNAME;
    INFO:=INFO+'-';
    Case DT.Month Of
      1:DAYNAME:='Jan';
      2:DAYNAME:='Feb';
      3:DAYNAME:='Mar';
      4:DAYNAME:='Apr';
      5:DAYNAME:='May';
      6:DAYNAME:='Jun';
      7:DAYNAME:='Jul';
      8:DAYNAME:='Aug';
      9:DAYNAME:='Sep';
      10:DAYNAME:='Oct';
      11:DAYNAME:='Nov';
      12:DAYNAME:='Dec';
    End;
    INFO:=INFO+DAYNAME+'-';
    Str(DT.Year,DAYNAME);
    DAYNAME:=Copy(DAYNAME,3,2);
    INFO:=INFO+DAYNAME+' ';
    Str(DT.Hour,DAYNAME);
    If DT.Hour<10 Then INFO:=INFO+'0';
    INFO:=INFO+DAYNAME+':';
    Str(DT.Min,DAYNAME);
    If DT.Min<10 Then INFO:=INFO+'0';
    INFO:=INFO+DAYNAME+':';
    Str(DT.Sec,DAYNAME);
    If DT.Sec<10 Then INFO:=INFO+'0';
    INFO:=INFO+DAYNAME;
    WriteWin(4,18,1,INFO+#0);
    If (DT.Hour=AHR) and (DT.Min=AMN) Then QTF:=True;
    With MyWindow^ Do
    Begin
      Close:=pIntuiMessage(GetMsg(MyWindow^.UserPort));
    End;
    If Close^.Code=0 Then
    Begin
      QTF:=True;
      ABORT:=True;
    End;
  Until QTF;
End;

Procedure OpenThatWindow(CDE:Integer);
Begin
  GfxBase:=pGfxBase(OpenLibrary('graphics.library',0));
  IntuitionBase:=pIntuitionBase(OpenLibrary('intuition.library',0));
  With MyFont Do
  Begin
    PasToC('Topaz.font',FontName);
    Font_Name:=FontName;
    Font_No:=TOPAZ_SIXTY;
    Font_Ddd1:=FS_NORMAL;
    Font_Ddd2:=FPF_ROMFONT;
  End;
  With MyNewWindow Do
  Begin
    DetailPen:=0;
    BlockPen:=1;
    Case CDE Of
      0:Begin
          LeftEdge:=170;
          TopEdge:=78;
          Width:=300;
          Height:=100;
          PasToC('AlarmClock v1.01',WTitle);
          Title:=@WTitle;
          Flags:=SMART_REFRESH or ACTIVATE;
        End;
      1:Begin
          LeftEdge:=410;
          TopEdge:=0;
          Width:=200;
          Height:=22;
          PasToC('Alarm v1.01',WTitle);
          Title:=@WTitle;
          Flags:=WINDOWCLOSE or SMART_REFRESH or 
                   ACTIVATE or WINDOWDRAG or WINDOWDEPTH;
        End;
      2:Begin
          LeftEdge:=170;
          TopEdge:=78;
          Width:=300;
          Height:=100;
          PasToC('Notice',WTitle);
          Title:=@WTitle;
          Flags:=WINDOWCLOSE or SMART_REFRESH or ACTIVATE;
        End;
      3:Begin
          LeftEdge:=170;
          TopEdge:=78;
          Width:=300;
          Height:=100;
          PasToC('Notice',WTitle);
          Title:=@WTitle;
          Flags:=SMART_REFRESH or ACTIVATE;
        End;
    End;
    IDCMPFlags:=CLOSEWINDOW_;
    Type_:=WBENCHSCREEN;
    FirstGadget:=NIL;
    CheckMark:=NIL;
    Screen:=NIL;
    BitMap:=NIL;
    MinWidth:=0;
    MinHeight:=0;
    MaxWidth:=0;
    MaxHeight:=0;
  End;
  MyWindow:=OpenWindow(@MyNewWindow);
  If MyWindow=NIL Then
  Begin
    WriteLn('Error: Couldn''t open the time information window');
    Halt(2);
  End; 
End;

Procedure ShowTitle;
Begin
  With MyWindow^ Do
  Begin
    SetDrMd(RPort,0);
    WriteWin(82,26,1,'Alarm Clock v1.01'+#0);
    WriteWin(80,25,2,'Alarm Clock v1.01'+#0);
    SetDrMd(RPort,1);
    WriteWin(40,65,1,#$a9+'1994 A Cybertek Production'+#0);
    WriteWin(36,36,2,'Coded by Zebedee of Cybertek'+#0);
    WriteWin(104,45,2,'1 June 1994'+#0);
  End;
End;

Procedure CloseThatWindow;
Begin
  CloseWindow(MyWindow);
  CloseLibrary(Pointer(IntuitionBase));
  CloseLibrary(Pointer(GfxBase));
End;

Procedure Main;
Var
  WB:Boolean;
Begin
  GetParams;
  OpenThatWindow(0);
  ShowTitle;
  WriteWin(40,87,3,'AlarmClock is set to '+DHR+':'+DMN+#0);
  Delay(2500);
  CloseThatWindow;
  OpenThatWindow(1);
  DoTheBusiness;
  CloseThatWindow;
  If ABORT Then
  Begin
    OpenThatWindow(3);
    ShowTitle;
    WriteWin(52,87,3,'User abort - Terminating'+#0);
    Delay(2500);
    CloseThatWindow;
  End Else Begin
    WB:=WBenchToFront;
    If WB Then
    Begin
      OpenThatWindow(2);
      ShowTitle;
      WriteWin(100,87,3,'Wakey wakey!'+#0);
      With MyWindow^ Do
      Begin
        L:=Wait(BitMask(UserPort^.MP_SIGBIT));
      End;
      CloseThatWindow;
    End Else
      WriteLn('Error: Couldn''t bring WBench to front screen');
  End;
End;

Begin
  Main;
End.