/**
 * Scout - The Amiga System Monitor
 *
 *------------------------------------------------------------------
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * You must not use this source code to gain profit of any kind!
 *
 *------------------------------------------------------------------
 *
 * @author Andreas Gelhausen
 * @author Richard Körber <rkoerber@gmx.de>
 */



#include "system_headers.h"

static char initialdrawer[TEXTLENGTH + 1];
static char initialfile[FILENAMELENGTH + 1] = "prt:";
static char printfilename[FILENAMELENGTH + 1] = "prt:";

int MyRequest (char *gadgets, char *fmt, ...)
{
   struct   EasyStruct myreqES;
   struct   Window *myreq_window;
   int      result;
   ULONG    iflags = 0;
   char     buf[256];

   va_list args;
   va_start(args,fmt);
   RawDoFmt(fmt, args, (void (*)())"\x16\xc0\x4e\x75", buf);

   get (WI_Main, MUIA_Window_Window, &myreq_window);

   myreqES.es_StructSize = sizeof(struct EasyStruct);
   myreqES.es_Flags = 0;
   myreqES.es_Title = "Scout Request";
   myreqES.es_TextFormat = buf;
   myreqES.es_GadgetFormat = gadgets;
   result = EasyRequest (myreq_window, &myreqES, &iflags);

   va_end (args);
   return (result);
}

static BOOL FileRequest (void) {
   struct   Window         *aslfr_window = NULL;
   struct   FileRequester  *requester;
   BOOL     result = FALSE;
   char     endchar;

   get (WI_Main, MUIA_Window_Window, &aslfr_window);

   if (aslfr_window) {
      if (requester = AllocAslRequestTags (ASL_FileRequest,
                      ASLFR_Window, aslfr_window,
                      ASLFR_PrivateIDCMP, TRUE,
                      ASLFR_SleepWindow, TRUE,
                      NULL)) {

         result = AslRequestTags (requester,
                  ASLFR_TitleText, "Choose file or printer!",
                  ASLFR_InitialDrawer, initialdrawer,
                  ASLFR_InitialFile, initialfile,
                  ASLFR_DoSaveMode, TRUE,
                  NULL);

         strncpy (initialdrawer, requester->fr_Drawer, TEXTLENGTH);
         strncpy (initialfile, requester->fr_File, FILENAMELENGTH);

         strcpy (printfilename, initialdrawer);
         endchar = printfilename[strlen(printfilename) -1];

         if ((strlen(initialdrawer) != 0) && (endchar != '/') && (endchar != ':'))
            strcat (printfilename, "/");
         strcat (printfilename, initialfile);

         FreeAslRequest (requester);
      }
   }
   return (result);
}

BPTR PrintHandle;
LONG PrintHandleMode = MODE_NEWFILE;

BPTR HandlePrintStart (char *filename) {
   PrintHandle = (BPTR)NULL;

   if (AP_Scout) {
      if ((! filename) && (! FileRequest()))
         return ((BPTR)NULL);
      ApplicationSleep();
   } else if ((! filename) || (filename[0] == '\0')) {
      return (Output());
   }
   if (filename) {
      strncpy(printfilename, filename, FILENAMELENGTH);
   }
   if (! (PrintHandle = Open (printfilename, PrintHandleMode))) {
      if (PrintHandleMode == MODE_OLDFILE) {
         PrintHandle = Open (printfilename, MODE_NEWFILE);
      }
      if (! PrintHandle)
         MyRequest ("Continue", "Couldn't open file \'%s\'!", printfilename);
   }
   if (PrintHandle) {
      Seek (PrintHandle, 0, OFFSET_END);
   }
   return (PrintHandle);
}

void HandlePrintStop (void) {
   if (AP_Scout) {
      AwakeApplication();
   }
   if (PrintHandle) {
      Close (PrintHandle);
   }
}


