program Kermit(Input,Output);

{ - This is the Kermit main program }

imports System from System;
imports MenuUtils from MenuUtils;
imports KermitLineIO from KermitLineIO;
imports KermitConnect from KermitConnect;
imports KermitScreen from KermitScreen;
imports KermitRead from KermitRead;
imports KermitSend from KermitSend;
imports KermitGlobals from KermitGlobals;
imports KermitParameters from KermitParameters;

imports KermitFile from KermitFile;

const MsegFile = 'sys:Util>Segfiles>Kermit.MSEG';
      HLPFile  = 'sys:Util>Segfiles>Kermit.HLP';

var 
    PList, TPList   : pPListEntry;
    MNComm          : MainCommType;
    Arg1, Arg2      : FNameType;


procedure DoCommand; 

    handler CtlCAbort;
    begin
        CtrlCPending := false;
        writeln;
        exit( DoCommand );
    end;

    handler HelpKey(var RetStr: Sys9S);
    begin
        RetStr := '?';
    end;

begin
        { Get a command and return the parse list }
    GetPList( RootMenu, PList );
        
    if PList = NIL then
        MnComm := MainEmptyLine
    else
        MnComm := Recast( PList^.Selection, MainCommType );
                            
    case MnComm of

        MainSend:
            begin
                ParseArgs( PList^.NextPList^.Arg, Arg1, Arg2 );
                FlushBuffer( Idev );
                if SetReadFile( Arg1, Arg2 ) >= FNoError then
                    CurrState := SendSwitch
                else
                    Writeln('?Wildcards in filenames do not match');
            end;

        MainReceive:
            begin
                ParseArgs( PList^.NextPList^.Arg, Arg1, Arg2 );
                FlushBuffer( Idev );
                if (Arg1='') and (Arg2<>'') then begin
                    writeln('?Give two file names if rename intended');
                end else 
      
                    if SetWriteFile( Arg1, Arg2 ) >= FNoError then
                        CurrState := ReadSwitch
                    else
                        writeln('?Wildcards in filenames do not match');
            end;

        MainGet:
            begin
                ParseArgs( PList^.NextPList^.Arg, Arg1, Arg2 );
                FlushBuffer( IDev );
                if Arg2='' then 
                    Arg2 := Arg1;
                if Arg1='' then
                    writeln('?Send what file?')
                else
                    if SetWriteFile( Arg1, Arg2 )>=FNoError then begin
                        CurrState := ReceiveInit( Arg1 );
                        CurrState := ReadSwitch;
                    end else
                        writeln('?Wildcards in filenames do not match');
            end;

        MainQuit,MainExit:
            ;

        MainBye:
            begin                                                
                if (not SendGComm( GCLogout, '' )) then
                    writeln('?Unable to logout remote server')
                else
                    MnComm := MainQuit;
            end;                                              

        MainFinish: 
            begin                                                
                if (not SendGComm( GCFinish, '' )) then
                    writeln('?Unable to tell remote server to finish');
            end; 

        MainConnect:
            begin
                write( '[Connected to remote computer - type ' );
                ShowKey( EscKey );
                write( ',''C'' or press tabswitch to return]' );
                Terminal( EscKey );
                write( chr(13),'[Connection closed - back at Perq]' );
                writeln('                                                ');
            end;

        MainSet:
            SetCommand( PList^.NextPList );

        MainShow:
            ShowCommand( PList^.NextPList );
        
        MainTake:
            begin
                if not PushCmdFile( PList^.NextPList^.Arg ) then
                    if not PushCmdFile( Concat(PList^.NextPList^.Arg, '.Cmd') )
                    then
                        writeln('?No command file: ', PList^.NextPList^.Arg);
            end;

        MainStatus:
            StatusCommand;
        
        MainEmptyLine:
            ;

        otherwise:
            begin
                writeln( 'Wrong command' );
                writeln;
            end;
    end;
    
end;



begin
    InitParameters;
    InitScreen;
    InitMenues;
    InitFile;
    writeln;
    writeln;
    writeln( KermitMessage );
    writeln;
    RootMenu := GetMenu( MsegFile, HLPFile );
    InitLine;

    repeat  { Kermit main loop }
        DoCommand;
        DestroyPList( PList );

    until MnComm IN [MainQuit,MainExit];

    SetSaveFile( '' );  { Force Kermit to close the log file }
    CleanUpLine;
    CleanUpParameters;
    DestroyMenues;
    CleanUpScreen;
end.
