{PICS2L.INC  Pascal Integrated Communications System Overlays}
{ 6/10/87  Ver 1.6 Copyright 1987 by Les Archambault}

overlay procedure wrapup;
{ Disconnect, update and close all files}
  var
    i, j, time_on, time_left: integer;
    t: tad_array;
    Tstr: StrTAD;
  begin
    setsect(homdrv,homusr);
    GetTAD(t);
    Tstr := FormTAD(t);
    Writeln(USR);
    if valid_pw then
      begin
        Writeln(USR,'Logoff: ',Tstr);
        timer(time_on, time_left);
        Writeln(USR, 'Verbindung dauerte: ', intstr(time_on,1), ' Min.');
        Writeln(USR, 'Auf Wiedersehen, ', user_rec.fn, '! Ruf mal wieder an...');
        Writeln(USR)
      end;
    mdhangup;
    if valid_pw        { Don't update files if user not initialized }
      then
        begin
          GetTAD(t);
          timer(time_on, time_left);
          if (login_t[3]=t[3]) and (user_rec.access<=250)  then
          user_rec.time_today := user_rec.time_today + time_on
          else user_rec.time_today:=0;
          user_rec.time_total := user_rec.time_total + time_on;
          user_rec.laston := t;
          if temp_hi_lmr>user_rec.lasthi then user_rec.lasthi:=temp_hi_lmr;
          PutRec(DatF, user_loc, user_rec);
          log(3, ' ');
          i := login_t[1];
          j := login_t[2];
          while j <> t[2] do
            begin
              stat_rec.busy_per_hour[j] := stat_rec.busy_per_hour[j] + 60 - i;
              i := 0;
              j := succ(j) mod 24
            end;
          stat_rec.busy_per_hour[j] := stat_rec.busy_per_hour[j] + t[1] - i;
          seek(stat_file, 0);
          Write(stat_file, stat_rec)
        end;
    CloseFile(DatF);
    CloseIndex(IdxF);
    Close(logr_file);
    Close(stat_file);
    Close(nwin_file);
    Close(summ_file);
    Close(mesg_file);
    if macro_file_exists then close(macro_file);
    mdhangup;
  end;

overlay procedure setup;

  procedure CheckUsrFile(fn:FileName);
  var
    test_file  : file;
  begin
    assign(test_file,user_drv + fn + ext);
    {$I-} Reset(test_file); {$I+}
    OK := (ioresult=0) and (filesize(test_file)>0);
    close(test_file);
  end;

  procedure Info(ftyp:Str20; fn:FileName);
  begin
    Write(BEL,ftyp,' File(s) nicht gefunden. Lege an ', fn, ext);
  end;

var
  i            : integer;
  t            : tad_array;
begin
  abort            := FALSE;
  ansi             := FALSE;
  valid_pw         := FALSE;
  fini             := FALSE;
  nonstop          := FALSE;
  connected        := FALSE;
  local_online     := FALSE;
  local_copy       := TRUE;
  printer_copy     := FALSE;
  remote_online    := FALSE;
  remote_copy      := FALSE;
  macro_file_exists:= FALSE;
  AreaReq          :='';
  SectReq          :='';

  mode             := message_mode;       { Start system in message mode }
  st_switch        := TRUE;               { Default file size display - in 'k' }
  new_dir          := FALSE;              { Reset directory flag }
  up_down_display  := TRUE;               { Show up/downloads for files mode }
  extra_time       := 0;                  { None until upload complete }
  op_chat          := FALSE;              { chat not initiated }

  user_rec.nulls      := 2;               { 2 nulls until recognition }
  user_rec.shift_lock := FALSE;           { Heute kann jeder Comp Kleinschreibung }
  user_rec.noisy      := TRUE;            { Prompt bell initially on }
  user_rec.columns    := def_chars;
  user_rec.lines      := def_lines;

  timeout := 60;                          { Allow one minute for input }

  InitIndex;                              { Get files ready for use }

  CheckUsrFile(user_data);
  If OK then CheckUsrFile(user_indx);
  If OK then
  begin
     OpenFile (DatF, user_drv + user_data + ext, SizeOf(user_rec));
     OpenIndex(IdxF, user_drv + user_indx + ext, len_ln + len_fn, 0);
  end else
  begin
    Info('User',user_drv + user_data);
    MakeFile(DatF, user_drv + user_data + ext, SizeOf(user_rec));
    Write(', ', user_drv + user_indx, ext);
    MakeIndex(IdxF, user_drv + user_indx + ext, len_ln + len_fn, 0);
    Writeln
  end;

  {$I-} Reset(logr_file) {$I+};
  OK:=(ioresult=0);
  if (not OK) or (OK and (filesize(logr_file)=0)) then
  begin
    Info('Log', logr_drv + logr_name);
    Rewrite(logr_file);
    logr_rec.user := 0;
    Write(logr_file, logr_rec);
    Writeln
  end;

  {$I-} Reset(summ_file) {$I+};
  OK := (IOresult = 0);
  if OK then
  begin
    {$I-} Reset(mesg_file) {$I+};
    OK := (IOresult = 0)
  end;
  if (not OK) or (OK and (filesize(summ_file)=0)) then
  begin
    Info('Message', summ_drv + summ_name);
    Rewrite(summ_file);
    summ_rec.num := 0;
    Write(summ_file, summ_rec);
    Write(', ', mesg_drv + mesg_name, ext);
    Rewrite(mesg_file);
    Writeln
  end;

  {$I-} Reset(stat_file) {$I+};
  OK:=(ioresult=0);
  if (not OK) or (OK and (filesize(stat_file)=0)) then
  begin
    Info('Statistik', stat_drv + stat_name);
    Rewrite(stat_file);
    GetTAD(stat_rec.date);
    for i := 0 to 23 do stat_rec.busy_per_hour[i] := 0;
    for i := 0 to  6 do stat_rec.busy_per_day[i]  := 0;
    Writeln
  end else read(stat_file,stat_rec);

  {$I-} Reset(nwin_file) {$I+};
  If IOresult <> 0 then
  begin
    Info('Newin', nwin_drv + nwin_name);
    Rewrite(nwin_file);
    Writeln
  end;

  if cold then
  begin
    log(0, '');
    cold := FALSE
  end
end;

overlay procedure wait_for_user;

  procedure Warning(m:StrStd);
  begin
    writeln(m,' -- DISK SPEICHER ZU KLEIN !');
  end;

var
  ch           : char;
  count_limit,
  count,
  counter,
  delay_count,
  Rcv_Space,
  Hom_space,i  : integer;
  t            : tad_array;

  procedure GiveCopyRight;
  begin
    putstat(' ' + version + ' vom ' + ver_date + copyright);
  end;

begin
  ClrScr;
  count_limit:=trunc(6000*(mhz/4.0));
  if delay_down then
  begin
    putstat(' Warte auf Bestaetigung fuer verzoegertes Shutdown...');
    mdbusy;
  end else
  begin
    GiveCopyRight;
    mdinit;
  end;
  gettad(t);
  Rcv_space:=Diskfree(RcvDrv,RcvUsr);
  Hom_space:=Diskfree(homdrv,homusr);
  If macro_in_progress then
  begin
    macro_in_progress:=false;
    if macro_file_exists then
    begin
      close(macro_file);
      macro_file_exists:=false;
    end;
    gettad(t);
    macro_done:=t[3];
    local_online:=false;
  end;
  count := 0;
  counter:=0;
  delay_count:=0;

  repeat
    If (auto_macro)                                  and
       (macro_done<>t[3])                            and
       ((t[2]>=auto_macro_start) or (not clock))     and
       (length(macro)>0) and (not macro_in_progress) then
    begin
      Assign(macro_file,'MACRO.LST');
      {$I-} Reset(macro_file); {$I+}
      if ioresult=0 then
      begin
        macro_file_exists:=true;
        writeln('Starte Auto Makro Operation.');
        macro_in_progress:=true;
      end else close(macro_file);
      if (not macro_file_exists) and (length(macro)>0) then
      begin
        writeln('Starte Auto Makro Operation.');
        macro_in_progress:=true;
        next_inpstr:=macro;
        repeat
          i:=pos('^M',next_inpstr);
          if i>0 then
          begin
            delete(next_inpstr,i,2);
            insert(CR,next_inpstr,i);
          end;
        until i=0;
        cmd_queue:=next_inpstr;
        next_inpstr:='';
        mult_cmds:=true;
      end;
    end;

    if (counter>30000) or (counter=0) then
    begin
      if delay_down then
      begin
        clrscr;
        writeln;
        gotoxy(5,20);
        writeln('COUNT DOWN FUER REAKTIVIERUNG DES SYSTEMS... ',75-delay_count);
        writeln;
      end;
      gotoxy(10,15);
      If Rcv_space<maxfree_uplds   then Warning('UPLOADS GESPERRT');
      If Hom_space<maxfree_logs    then Warning('NEUE LOGINS GESPERRT');
      If Hom_space<maxfree_mslimit then Warning('BRIEFE BEGRENZT');
      If (Hom_space<maxfree_abs) or (Rcv_space<maxfree_abs) then
      begin
        writeln;
        writeln('EXTREME DISK SPEICHER PROBLEME !!!!!!!!!');
      end;
(*      gettad(t);   { hier nur fuer CPC.CLK -B.B. } *)
      counter:=1;
    end;
    count := succ(count);
    counter:=succ(counter);
    if counter=10000 then clrscr;
    if count > count_limit then
    begin
      if (not clock) then
      begin
        hour_count:=hour_count+(1.0*time_adjust);
        if hour_count>(14400.0*(mhz/4.0)) then
        begin
          global_date[3]:=succ(global_date[3]);
          if global_date[3]>31 then
          begin
            global_date[3]:=1; {day}
            global_date[4]:=succ(global_date[4]); {month}
            if global_date[4]>12 then
            begin
              global_date[4]:=1;
              global_date[5]:=succ(global_date[5]); {year}
            end;
          end;
          hour_count:=0.0;
        end;
      end;
      gettad(t);               (* WAR  auskommentiert wg. CPC.CLK -B.B. *)
(*      putstat('');                                       auskommentiert *)
      GotoXY(succ(Random(79)), succ(Random(23)));
      GiveCopyRight;
      count := 0;
      if delay_down then delay_count:=succ(delay_count);
    end;
    if delay_down then
    begin
      Write(BEL);
      if delay_count>=75 then
      begin
        delay_down:=false;
        delay_count:=0;
        mdhangup;
      end;
    end;
    ch := GetChar;
    if (ch = LF) or (ch=CR) then write_system_status;
    if ch = ETX then
    begin
      putstat(' Leitung durch Modem besetzen? <J/N>');
      read(KBD, ch);
      if UpCase(ch) = 'J' then mdbusy
                          else mdhangup;
      putstat(' PICS beenden...');
      log(1, '');
      CloseFile(DatF);
      CloseIndex(IdxF);
      Close(sysm_file);
      Close(summ_file);
      Close(mesg_file);
      Close(logr_file);
      Close(stat_file);
      Close(nwin_file);
      fini := TRUE
    end else if (ch=FF) or (macro_in_progress) then  {control L}
    begin
      putstat(' Lokale Benutzung angefordert');
      mdbusy;
      rate := 1200;                   { Pretend we're running at 1200 bps }
      local_online := TRUE
    end else if mdring then
    begin
      putstat(' Anruf wurde registriert...');
      mdans;
      remote_online := ch_carck;
      remote_copy   := remote_online;
      if remote_online then putstat(' Verbindung hergestellt mit ' + intstr(rate, 3) + ' Baud')
    end
  until fini or local_online or remote_online;
  delay_down := FALSE
end;

Overlay Procedure Benutzer_Eintrag;
{ automatisiert den Benutzer-Eintrag und erleichtert dem Sysop
  somit enorm die Arbeit                                       }
var
  Continue     : Boolean;
begin
  Continue := FALSE;
  writeln(usr);
  Continue := ask('Moechtest Du Dich jetzt eintragen');
  if Continue then
  Repeat
    with user_rec do
    begin
      writeln(usr);
      writeln(usr, 'Bitte die folgenden Angaben wahrheitsgemaess machen! Sie werden');
      writeln(usr, 'keinem Dritten zugaenglich gemacht oder zu Werbezwecken missbraucht.');
      writeln(usr);
      cy := prompt('Dein Wohnort (mit PLZ) ',len_cy,'ESL');
      ad := prompt('Strasse und Nr.        ',len_ad,'EL');
      ph := prompt('Deine Telefon-Nummer   ',len_ph,'EL');
      comp := prompt('Dein Computer-System   ',len_comp,'EL');
      Writeln(USR);
      Writeln(USR, 'Und hier noch einmal alles in der Uebersicht:');
      Writeln(USR);
      Writeln(USR, 'Du bist also ', fn, ' ', ln);
      Writeln(USR, 'Dein Wohnort (mit PLZ): ', cy);
      Writeln(USR, 'Strasse und Hausnr.   : ', ad);
      Writeln(USR, 'Deine Telefon-Nummer  : ', ph);
      Writeln(USR, 'Dein Computer-System  : ', comp);
      Writeln(USR);
      continue := ask('Sind alle Angaben richtig gemacht');
      if continue then
         if ((ad = ' ')  or (cy = ' ')  or (ph = ' '))  or
            ((ad = '')   or (cy = '')   or (ph = ''))   or
            ((ad = '  ') or (cy = '  ') or (ph = '  ')) then
         begin
           writeln(usr);
           writeln(usr, 'Da waren wohl einige Fehler drin! Also nochmal bitte...');
           writeln(usr);
           Continue := FALSE;
         end else
         begin
           access := 15;
           limit  := 30;
           log(9,'Antrag!');
           writeln(usr);
           list ('Y');
         end
    end
  until (not online) or continue;
end;

procedure dummy3;
{ FORCE NEW OVERLAY }
begin
end;

{end of PICS2L.INC }

