Program BldMsg;

Uses {$U /ccutil/cclib}
  ccDefn,
  CCcrtIO;

Type
  Str20 = String[20];
  MsgRcd = Packed Record
             Grp : Integer;
             Msg : Integer;
             Txt : String[80]
             End;

Var
  INFNam,
  OUFNam : Str20;
  INF : Text;
  OuF : File of MsgRcd;
  Counter,
  N1,                                { eAGroupe@ eAnumbere@ }
  N2  : Integer;                     { eAMessagee@ eAnumbere@ }
  B   : Char;                        { eABlank!e@ }
  LI  : String[80];                  { eAMessagee@ eAtexte@ }


  Function BrkPress : Boolean; External;

  Procedure ExitGracefully;

  Begin
    WriteLn;
    WriteLn('Program terminated by user- output file not written!');
    Close(OuF)
  End;


  Procedure OutError;

  Begin
    Write(chr(7));
    WriteLn;
    WriteLn('An error was encountered writing the output file. Check to make');
    WriteLn('sure there is enough room on the output volume for the compiled');
    WriteLn('file (CRUNCHing may help).');
    Exit(BldMsg)
  End;


  Procedure Capitalize(Var IS : Str20);

  Var
    Counter,
    Glotzy  : Integer;

  Begin
    For Counter := 1 To Length(IS) Do
      If IS[Counter] in ['a'..'z'] Then Begin
        Glotzy := Ord(IS[Counter]);
        If Glotzy > 127 Then Glotzy := Glotzy - 256;
        IS[Counter] := Chr(Ord(IS[Counter])-32)
        End
  End;


  Procedure PutGrp(GN : Integer);

  { eAPute@ eAalle@ eAmessagese@ eAfrome@ eAgroupe@ eA"GN"e@ eAine@ eAoutpute@ eAfile.e@ eAEnterede@ eAwithe@ eAfirste@ }
  { eAmessagee@ eAcontentse@ eAine@ eAN1,e@ eAN2,e@ eAande@ eATXT.e@ }

  Var
    Counter : Integer;

    Procedure PutMsg;

    { eAPute@ eAae@ eAsinglee@ eAmessagee@ eAine@ eAthee@ eAoutpute@ eAfilee@ }

    Begin
      If BrkPress Then ExitGracefully;
      OuF^.Grp := n1;
      OuF^.Msg := N2;
      OuF^.Txt := LI;
      Put(OuF)
    End;

  Begin   { eAPutGrpe@ }
    Write('Message group ',GN:0,': ');
    PutMsg;                            { eAPute@ eAthee@ eAfirste@ eAmsge@ eAwee@ eAfounde@ }
    Counter := 1;
    Repeat                             { eAUntile@ eAnewe@ eAgroupe@ }
      ReadLn(INF,N1,N2,B,LI);          { eAGete@ eAae@ eAmessage...e@ }
      If ArgC > 1 Then WriteLn(N1,' ',N2,' ',LI);
      If N1 = GN Then Begin            { eAIse@ eAite@ eAine@ eAthee@ eAcurrente@ eAgroup?e@ }
        PutMsg;                        { eAYep,e@ eApute@ eAite@ eAine@ eAthee@ eAoutpute@ eAfilee@ }
        Counter := Counter + 1         { eAInce@ eAthee@ eAmessagee@ eAcounter...e@ }
        End
    Until EOF(INF) or (N1 <> GN);
    WriteLn(Counter:0,' messages.')
  End;


Begin           { eABLDMSGe@ }
  CCcrtIOinit;
  CrtAction(EraseAll);
  CrtTpgm := 'BldMsg';
  CrtTvrs := '2.1';
  CrtTcpy := 'Copyright 1984 Corvus Systems, Inc.';
  CrtTitle('System message file builder utility');
  GotoXY(0,6);
  Write('Name of message file to compile? ');
  ReadLn(INFNam);
  WriteLn;
  If Length(INFNam) = 0 Then Exit(BldMsg);
  Capitalize(INFNam);
  If Pos('.TEXT',INFNam) = 0 Then INFNam := Concat(INFNam,'.TEXT');

  {$I-}
  Reset(INF,INFNam);
  {$I+}

  If IOResult <> 0 Then Begin
    Write(chr(7));
    WriteLn;
    WriteLn('An error was encountered opening the input file. Check to make');
    WriteLn('sure that you have spelled the file name correctly and that the');
    WriteLn('file does exist.');
    Exit(BldMsg)
    End;

  OUFNam := Copy(INFNam,1,Length(INFNam)-5); { eADrope@ eAthee@ eA".TEXT"e@ eAsuffixe@ }

  {$I-}
  ReWrite(OuF,OUfNam);
  {$I+}

  If IOResult <> 0 Then OutError;            { eAErrore@ eAopeninge@ eAoutpute@ eAfilee@ }
  N1 := 0;                                   { eASete@ eAupe@ eAfore@ eAfirste@ eAreade@ }
  Repeat
    If N1 = 0 Then Repeat                    { eAReade@ eAuntile@ eAlegite@ eAmessagee@ }
                     ReadLn(INF,N1,N2,B,LI)
                   Until N1 > 0;
    PutGrp(N1)                           { eAReade@ eAande@ eAstoree@ eAae@ eAmessagee@ eAgroupe@ }
  Until EOF(INF);                        { eAEnde@ eAofe@ eAfile...e@ }

  Close(OuF,Lock);                       { eAWe'ree@ eAdone...e@ }
  WriteLn;
  WriteLn('Done.');
  If ARGC > 0 Then Begin
    WriteLn;
    WriteLn('Check of compacted file: ');
    WriteLn;
    Reset(OuF,OufNam);
    Repeat
      Get(OuF);
      If BrkPress Then ExitGracefully;
      With Ouf^ Do WriteLn(Grp,' ',Msg,' ',Txt)
    Until EOF(Ouf)
    End
End.
