#!/usr/bin/perl -w
##    -*-perl-*-
##    newshark - Maintains your .newsrc
##
##    Copyright (C) 2001  Gerard Lanois
##                        gerard@users.sourceforge.net
##                        P.O. Box 507264
##                        San Diego, CA 92150-7264
##    
##
##    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
##    (at your option) 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use strict;

use Getopt::Std;
use News::Newsrc;
use Set::IntSpan;

my %opt;
my $newsrcname = ".newsrc";
my $group;

sub process_options()
{
    my $error = !getopts('f:ls:u:c:r:Uwv', \%opt);

    my $options = 0;

    if ($opt{'l'}) {
        $group = $opt{'l'};
        $options++;
    }

    if ($opt{'f'}) {
        $newsrcname = $opt{'f'};
    }

    my $count = 0;
    $count++ if ($opt{'l'});
    $count++ if ($opt{'s'});
    $count++ if ($opt{'u'});
    $count++ if ($opt{'c'});
    $count++ if ($opt{'r'});

    if ($opt{'s'}) {
        $group = $opt{'s'};
        $options++;
    }

    if ($opt{'u'}) {
        $group = $opt{'u'};
        $options++;
    }

    if ($opt{'c'}) {
        $group = $opt{'c'};
        $options++;
    }

    if ($opt{'r'}) {
        $group = $opt{'r'};
        $options++;
    }


    if ($count > 1) {
        print "ERROR: Can only specify one of -l, -s, -u, -c, or -r\n\n";
        $error = 1;
    }
    
    if ($opt{'w'}) {
        print_warranty();
        exit(-1);
    }

    if ($error || $opt{'U'} || !$options) {
        print_usage();
        exit(-1);
    }
}

sub print_usage() {
    print << "EOU";
Usage: newshark [switches]
    -l             - List contents of newsrc
    -f [filename]  - Use filename instead of .newsrc
    -s [group]     - Subscribe to group.
    -u [group]     - Unsubscribe group.
    -c [group]     - Catch up group (mark all articles from 1 - max as read)
    -r [group]     - Reset group to all unread.
    -v    
    -U Print out usage summary.
    -w Print out warranty information.

Examples:
    newshark -s comp.graphics.api.opengl
    newshark -f newsrc_nfdc -c alt.binaries.pictures.autos
EOU
}

sub print_warranty() {
    print << "EOW";
------------------------------------------------------------------------------
BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
EOW
}



############################################################################

process_options();


if ($opt{'l'}) {
    open(NEWSRC, "< $newsrcname") or die "ERROR: Can't open $newsrcname\n";
    while (<NEWSRC>) {
        print;
    }
    close(NEWSRC);
}
else {
    my $newsrc = new News::Newsrc 
        or die "ERROR: new News::Newsrc failed.\n";
    $newsrc->load($newsrcname)
        or die "ERROR: News::Newsrc->load(".$newsrcname.") failed.\n";

    if ($opt{'s'}) {
        if ($newsrc->exists($group) && $newsrc->subscribed($group)) {
            $opt{'v'} && print "Already subscribed to $group\n";
        }
        else {
            $newsrc->add_group($group);
            $newsrc->subscribe($group);
            $opt{'v'} && print "Subscribed to $group\n";
            $newsrc->save;
        }
    }
    elsif ($opt{'u'}) {
        if (!$newsrc->exists($group)) {
            $opt{'v'} && print "Not subscribed to $group\n";
        }
        else {
            $newsrc->unsubscribe($group);
            $opt{'v'} && print "Unsubscribed to $group\n";
            $newsrc->save;
        }
    }
    elsif ($opt{'r'}) {
        if (!$newsrc->exists($group)) {
            $opt{'v'} && print "Not subscribed to $group\n";
        }
        else {
            $newsrc->set_articles($group, "");
            $opt{'v'} && print "Reset $group to unread\n";
            $newsrc->save;
        }
    }
    elsif ($opt{'c'}) {
        if (!$newsrc->exists($group)) {
            $opt{'v'} && print "Not subscribed to $group\n";
        }
        else {
            my $articles = Set::IntSpan->new($newsrc->get_articles($group));
            my $max = $articles->max;
            if (defined $max) {
                $newsrc->mark_range($group, 1, $max);
                $opt{'v'} && print "Caught up $group\n";
                $newsrc->save;
            }
        }
    }
}
