// CAT_FILE.BTS - Copy a file to the STalker terminal window.
//
// Modifications made by Scott Dowdle. GEnie address - L.DOWDLE1
//
// This is a modified version of the original that is included on the
// STalker master disk by Gribnif.  The basic features that have been
// added are...
//
// 1) A welcome message,
//
// 2) A -more- like paging option with Full Window [19 lines] and a
//    Arrange STalker & STeno Windows size window [11 lines] 
//    defaults, and
//
// 3) A quit option every screenfull.
//
// *NOTE*  If you hit any key while a screenfull of text is being
// displayed, the screen will pause until you hit another key and 
// then it will continue filling the screen until it reaches the 
// chosen page length.  This is sort of a backup method to do paging 
// if you have changed your screen size to something other than one 
// of the defaults.  I wish there were an easy way to notifiy the 
// script STalker's window size and do the sceen paging by order, BUT.
//
// *NOTE*  As with the original, if you abort the script operation by 
// hitting [SHIFT][UNDO] you will end up NOT PROPERLY CLOSING THE 
// FILE that you were reading... therefore, use the quit option from 
// within the script to abort reading... or you can read the entire 
// document to the EOF and it will close also.

int page;

function show_file(string file)
     string    buffer[256];
     int       fd;
     int       linecount;
     int       whatnow;

     if (fd = file_open(file, 0)) < 0 then
          printf("Error %d opening '%s'\r\n", fd, file);
          return;
     endif

     linecount = 0;

    while file_gets(buffer, sizeof(buffer), fd) > 0 do
        if instat() then
            getchar();  // Read the one that's waiting
            getchar();  // Wait for another one
        endif
        linecount = linecount + 1;
        if (linecount == page) then
             printf("[--- HIT ANY KEY TO CONTINUE, or Q to QUIT! ---]");
             whatnow = getchar();
                  if ((whatnow == 81) OR (whatnow == 113)) then
                               // 81  =  Decimal value for Q
                               // 113 =  Decimal value for q
                       printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
                       printf("\r\n\n[--- Closing file and EXITING! ---]\r\n\n");
                       file_close(fd);
                       return;                    
                  endif
             printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
             linecount = 0;
        endif
        puts(buffer);

    endwhile

     file_close(fd);
endfunction


function main()
    int which;
    string    source_file[14];
    string  source_full[128];
    string  source_path[128];

     which = alert("[0][|    Welcome to  CAT_FILE!     | |       Screen Lines?|][19|11|Cancel]");
     if which == 1 then
     	page = 19;
     endif
	 if which == 2 then
		page = 11;
	 endif
	 if which == 3 then
		return;
	 endif

     source_path[0] = file_get_drive();
     source_path[1] = ':';
     file_get_dir(0, source_path + 2);
     strcat(source_path, "\\*.*");
     source_file[0] = 0;

     if fsel(source_path, source_file, source_full, "File to Display?") then
          if source_file[0] == 0 then
               return;
          endif

          show_file(source_full);
     endif
endfunction
