{ Create a news topic }
{ Restricted to use on the logged-in filestore }

%external %string (80) Copyright %alias "NEWS_CREATE_(C)_FDC_RMM_85" = -
   "Copyright (C) 1985 Frank D. Cringle and Richard M. Marshall"

%include "News_S:News.Inc"
%include "Inc:Util.Imp"

%include "IE:Terminal.Inc"
%include "IE:IE.Inc"
%include "Inc:FS.Imp"
%include "Inc:FSUtil.Imp"
%include "Managr:AdDefs.Inc"
%include "Managr:Admin.Inc"

%constant %string Temp File = "$News_Temp$"

%begin
   %own %string (255) Param = "",
                      Topic = ""
   %integer Done,
            Size = 0
   %string (255) User Name,
                 Full Name
   %record (Admin fm) Admin

   {APPEND TO INDEX}

   %routine Append to Index      { Append Topic to index file }
      %string (255) Line
      %label eof

      %on %event 3, 9 %start
         -> eof %if Event_Event = 9
         Select Output(0)
         Print String("Can't write to News.Index")
         Newline
         %stop
      %finish

      Open Output(1, News Directory."News.Index")
      Open Input(1, News Directory."News.Index")
      Select Output(1)
      Select Input(1)
      %cycle
         Read Line(Line)
         %if Line = Topic %start
            Close Input
            Abandon Output
            %return
         %finish
         Print String(Line)
         Newline
      %repeat

      eof:
      Print String(Topic)
      Newline
      Close Input
      Close Output
   %end {Append to Index}

   {COUNT PRINT SYMBOL}

   %routine Count Print Symbol (%integer Ch)
      Print Symbol (Ch)
      Size = Size + 1
   %end {Count Print Symbol}

   {COUNT PRINT STRING}

   %routine Count Print String (%string (255) S)
      %integer I

      %for I = 1, 1, Length (S) %cycle
         Print Symbol (Char No (S, I))
      %repeat
      Size = Size + Length (S)
   %end {Count Print String}


   {MAIN CODE OF CREATE}

   Home Host = Current Host
   News Host = Home Host

   Define Param ("Topic", Topic, PAM Major!PAM No Default)
   Define Param ("Infile - editor by default", Param, PAM Major)

   Process Parameters (CLI Param)

   %begin
      %on %event 3 %start
         Select Output(0)
         Print String("Sorry, only authorised wizards can create topics")
         Newline
         Print String("Post a request to ""Topics"".")
         Newline
         %stop
      %finish

      %if %not Create News File(Topic) %start
         Select Output(0)
         Print String("Cannot create topic ".Topic)
         Newline
         %stop
      %finish
   %end

   %if Param = "" %start
      Open Output (2, Temp File)
      Select Output (2)
      Close Output
      Terminal Model = Default Terminal
      Set Up Terminal
      Done = Edit (Temp File, "", Temp File, 1, 1)
      Reset Terminal
      %if Done = No %start
         Print String ("Aborted")
         New Line
         Delete (News Directory.Topic)
         %stop
      %finish
      Param = Temp File
   %finish


   User Name = Current User
   Open Admin DB
   %if Search DB (User Name, Admin) %start
      Full Name = Admin_Prenames . " " . Admin_Surname
   %else
      Full Name = User Name
   %finish
!!!Mail Address = Admin_Mail Addr
   Close DB

   %if Open News File(topic, 1) %start
      Select Output (News Stream)
      Set Output (File Length)

      Open Input (2, Param)
      Select Input (2)

      %begin
         %byte Ch

         %on 9 %start
            -> Eof
         %finish
   
         %cycle
            Read Symbol (Ch)
            Count Print Symbol (Ch)
         %repeat
      Eof:
      %end

      Count Print Symbol (NL)
      Count Print String ("Created by: ")
      Count Print String (User Name)
      %if Full Name # User Name %start
         Count Print String ("   (")
         Count Print String (Full Name)
         Count Print Symbol (')')
      %finish
      Count Print Symbol (NL)

      Count Print String ("Date: ")
      Count Print String (Date)
      Count Print String (" at ")
      Count Print String (Time)
      Count Print Symbol (NL)

      Close Input
      Log Message (Size)
      Close News File
      Append to Index
   %else
      select output(0)
      print string(topic." is in use")
      newline
   %finish
%end
