#!/usr/bin/env perl

#
# rss2imap - an IMAP-based RSS aggreagor
#
# Copyright (C) 2004 Taku Kudo <taku@chasen.org>
#               2005 Yoshinari Takaoka <mumumu@mumumu.org>
#     All rights reserved.
#     This is free software with ABSOLUTELY NO WARRANTY.
#
# You can redistribute it and/or modify it under the terms of the
# GPL2, GNU General Public License version 2.
#
# $Id: rss2imap,v 1.20 2005/10/02 12:36:33 mumumu-org Exp $
#

use strict;
use POSIX qw(setsid);
use RSS2IMAPLIB::Config;
use RSS2IMAPLIB::Rss2imap;
use RSS2IMAPLIB::Common;
use Data::Dumper;


package main;

sub show_usage {
    print <<"EOS"

Usage: rss2imap [options] file1 file2 ...

-s,  --host=HOST           use HOST as an IMAP host [localhost]
-P,  --port=INT            use INT as a port number [143]
-u,  --user=USER           use USER as a user name [$ENV{USER}]
     --password=PASSWORD   use PASSWORD as your password
-p,  --prefix=PREFIX        use PREFIX as a folder prefix name []
                           set "INBOX" when you use courier-imap
-m,  --last-modified-folder=FOLDER
                           user "FOLDER" as an last-modified-folder
                                        [RSS.last-modified]
-c,  --cram-md5            use cram-md5 authentication
-o,  --once                run once and exit
-O   --outfile=filename    output filename. you use this option with
                           -I(--import) or -E(--export) option.
-n,  --nodaemon            run as a forground process 
-i,  --interval=MIN        run at MIN inervals [30]
     --proxy=HOST          use HOST as a http proxy
     --proxy-user=USER     use proxy username
     --proxy-pass=PASS     use proxy password
-S   --use-ssl             use Secure Socket Layer(SSL)
-I   --import [opml file]  generate config file from opml file.
                           you must speficy output file with [-O] option.
-E   --export              generate opml file from config file.
                           you must speficy output file with [-O] option.
-d   --delivery-mode=MODE  mail delivery mode.
                           MODE is "text"(default) or "html"
-h   --help                this help.
-v   --version             version info.

EOS
;
    exit();
}


sub main {

    $| = 1; select (STDERR); $| = 1; select (STDOUT);
    my $config_obj = RSS2IMAPLIB::Config->new();
    my $rss2imap = new RSS2IMAPLIB::Rss2imap($config_obj);
    my %config = %{$config_obj->get_global_configall()};
    my $version = $config_obj->get_version();
    my @config_list = @{$config{'list'}};

    #    print version
    if ( $config{version} ) {
        print  "rss2imap $version\n";
        exit();
    }

    print Data::Dumper::Dumper( \%config ) if ($config{'debug'});
    print Data::Dumper::Dumper( $config_obj->get_site_configall() ) if ($config{'debug'});

    show_usage () if (@{$config{list}} == 0 || $config{help});

    #   Welcome message.
    print "*   Welcome to rss2imap $version !   \n";

    #    import, export process.
    #    option import accepts one filename
    #    execute and exit.
    $config_obj->import_file($config_list[0]) if ($config{import});
    $config_obj->export_file(@config_list) if ($config{export});

    #   prompt imap password (and proxy password if enabled)
    unless ($config{'password'}) {
        print "You require authentication ...\n";
        my $common = RSS2IMAPLIB::Common->new();
        $common->getProxyPass_ifEnabled();
        $RSS2IMAPLIB::Rss2imap::GLOABL_CONFIG->{password} = $common->getPass('imap password: ');
    }

    #    finally run rss2imap!
    if ($config{once_p} || $config{debug}) {

        $rss2imap->run();

    } else {

        {
            # invoke rss2imap once to check whether given parameters are OK
            $rss2imap->connect_test();
        }

        unless (defined $config{nodaemon}) {

            print "started rssimap daemon....\n";
            print "update interval: $config{interval} minutes\n";

            exit() if fork();
            setsid() if ( $^O !~ /Win32/ );
            umask(022);
            open (STDIN, '/dev/null') or die;
            open (STDOUT, '>>/dev/null') or die;
            open (STDERR, '>>/dev/null') or die;
        }

        while (1) {
            eval {
                $rss2imap->run();
            };
            sleep $config{interval} * 60;
        }
    }
}

main ();
