#module NEWSEXTRACT "V4.0"
/*----------------------------------------------------------------------
        N E W S  E X T R A C T . C

 Author:
     Geoff Huston
       Computer Services Centre
       Australian National University

 Functionality:
        Output news items to a file - the only difference between the two
        functions is that the print file is optionally spooled at the
        close of the run.

 Version:
    V1.0    17-Jul-1986     GIH     version checkpointed
    V1.1     1-Aug-1986     GIH     screen code added
    V3.0     6-Apr-1987     GIH
    V4.0    30-Oct-1987     GIH     NEWS Version 4.0 Release

----------------------------------------------------------------------*/
#include "newsinclude.h"
#include "newsdefine.h"
#include "newsextern.h"

/*--------------SHUTDOWN FUNCTION----------------------------------------
 *  p r i n t _ c l o s e
 *
 *  close - and spool the print file - make sure that this is the
 *  last thing done - cause it may TERMINATE news!
 *----------------------------------------------------------------------
 */

print_close()
{
    char q[40],
         com[132];
    $DESCRIPTOR(q_dsc,q);
    short q_len;
    struct stat sb;

    if (!stat(Print_file,&sb))
    {
        get_input(&q_dsc,c$dsc("Print output to which Queue? [No print]"),&q_len);
        q[q_len] = '\0';
        if (!q_len)
        {
            lib$rename_file(c$dsc(Print_file),c$dsc(Print_save_file),0,0,0,0,0,0,0,0,0,0);
            sprintf(err_oline,"Print - File saved as %s",Print_save_file);
            err_line(err_oline);
            return;
        }
        strcpy(com,"PRINT/NOTIFY/DELETE/QUEUE=");
        strcat(com,q);
        strcat(com," ");
        strcat(com,Print_file);
        sprintf(err_oline,"Print: File %s queued to %s(/Delete)",Print_file,q);
        err_line(err_oline);
        if (screen_active) smg$end_pasteboard_update(&pid);
        lib$do_command(c$dsc(com));
    }
}

/*--------------UTILITY FUNCTION-----------------------------------------
 *  f i l e _ p r i n t
 *
 *  Extract a news item into a file
 *----------------------------------------------------------------------
 */

file_print(append,fnam)
    int append;
    char *fnam;
{
    int   all = 0,
          i;
    char  xfrbuf[512];
    FILE  *fpr,
          *fpw;

    if (cli$present(c$dsc("ALL")) == CLI$_PRESENT) all = 1;

    if ((!curr_g) || ((news_context == 1) && (!all)))
    {
        err_line("Error: Print/Extract - No Newsgroup selected");
        return(0);
    }

    if ((!ga[curr_g]->grp_count) || (ga[curr_g]->grp_closed))
    {
        err_line("Error: Print/Extract - Newsgroup is empty");
        return(0);
    }

    if ((curr_i <= 0) && (!all))
    {
        err_line("Error: Print/Extract - No item selected");
        return(0);
    }

    if (!ga[curr_g]->grp_ia) map_items(curr_g);

    if (append) fpw = fopen(fnam,"a");
    else fpw = fopen(fnam,"w");
    if (!fpw)
    {
        sprintf(err_oline,"Error: Print/Extract - cannot open %s",fnam);
        err_line(err_oline);
        return(0);
    }

    if (append) sprintf(err_oline,"\tInfo: Print/Extract - Appending to %s\n",fnam);
    else sprintf(err_oline,"Info: Print/Extract - Listing to %s",fnam);
    err_line(err_oline);
    if (append) fputs("\f\n",fpw);

    if (all) i = 1;
    else i = curr_i;
    do
    {
        if (!(fpr = do_open_item(curr_g,i,"r")))
        {
            sprintf(err_oline,"Error: Print/Extract - file read error #%d",ga[curr_g]->grp_ia[i].itm_num);
            err_line(err_oline);
            continue;
        }
        sprintf(xfrbuf,"NEWS-Posting: Newsgroup <%s>, Item <#%d>, Subject <%s>\n",
            ga[curr_g]->grp_name,ga[curr_g]->grp_ia[i].itm_num,ga[curr_g]->grp_ia[i].itm_title);
        fputs(xfrbuf,fpw);
        sprintf(err_oline,"\tItem: #%d, Subject: %s\n",ga[curr_g]->grp_ia[i].itm_num,ga[curr_g]->grp_ia[i].itm_title);
        err_line(err_oline);
        while (fgets(xfrbuf,510,fpr)) fputs(xfrbuf,fpw);
        fclose(fpr);
    } while ((all) && (++i <= ga[curr_g]->grp_count));
    fclose(fpw);
    return(0);
}

/*--------------COMMAND FUNCTION-----------------------------------------
 *  d o _ e x t r a c t
 *
 *  define verb EXTRACT
 *    routine     do_extract
 *    parameter   P1,         label=FILE,value(type=$outfile)
 *    qualifier   ALL,        nonnegatable
 *    qualifier   APPEND,     nonnegatable
 *
 *  Extract current newsitem into news file
 *----------------------------------------------------------------------
 */

do_extract()
{
    int   append = 0;
    char  fnam[256];
    short fnam_len = 0;
    $DESCRIPTOR(fnam_dsc,fnam);

    clear_err_line();
    if (cli$present(c$dsc("APPEND")) == CLI$_PRESENT) append = 1;
    if (cli$get_value(c$dsc("FILE"),&fnam_dsc,&fnam_len) == CLI$_ABSENT)
    {
        status = get_input_dflt(&fnam_dsc,c$dsc("Extract_File: "),&fnam_len,c$dsc(Extract_file));
        if (status == RMS$_EOF) longjmp(2);
    }
    fnam[fnam_len] = '\0';
    if (!*fnam) strcpy(fnam,Extract_file);

    file_print(append,fnam);
    return(0);
}

/*--------------COMMAND FUNCTION-----------------------------------------
 *  d o _ p r i n t
 *
 *  define verb PRINT
 *    routine     do_print
 *    qualifier   ALL,        nonnegatable
 *
 *  Extract current newsitem into news file for subsequent printing
 *----------------------------------------------------------------------
 */

do_print()
{
    clear_err_line();
    file_print(1,Print_file);
    return(0);
}
