#!/usr/bin/python
# -*- coding: utf-8 -*-

#
# gPodder (a media aggregator / podcast client)
# Copyright (C) 2005-2007 Thomas Perl <thp at perli.net>
#
# 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., 51 Franklin Street, Fifth Floor, Boston, 
# MA  02110-1301, USA.
#

"""gPodder media aggregator"""

# PLEASE DO NOT CHANGE FORMAT OF __version__ LINE (setup.py reads this)

__author__    = "Thomas Perl <thp@perli.net>"
__version__   = "0.9.0"
__date__      = "2007-03-06"
__copyright__ = "Copyright (c) 2005-2007 %s. All rights reserved." % __author__
__licence__   = "GPL"

import sys,os
from optparse import OptionParser

import locale
import gettext

# gettext variables
APP = 'gpodder'
DIR = '/usr/share/locale/'

def main(argv=None):
    # gettext support
    #locale.setlocale( locale.LC_ALL, '')
    gettext.bindtextdomain( APP, DIR)
    gettext.textdomain( APP)
    gettext.install( APP)
    
    if argv is None:
        argv = sys.argv

    parser = OptionParser(usage="usage: %%prog [options] arg1 arg2\n\n%s" %__doc__,
                          version = "%%prog %s %s" %(__version__, __date__))

    parser.add_option("-v", "--verbose",
                      action="store_true", dest="verbose", default=False,
                      help=_("Be more verbose"))

    parser.add_option("--debug",
                      action="store_true", dest="debug", default=False,
                      help=_("Run local version in current directory"))
    
    parser.add_option("-l", "--list",
                      action="store_true", dest="list", default=False,
                      help=_("List all channel subscriptions"))

    parser.add_option("-r", "--run",
                      action="store_true", dest="run", default=False,
                      help=_("Update channel list, download new podcasts"))

    parser.add_option("-u", "--update",
                      action="store_true", dest="update", default=False,
                      help=_("Update channel list and exit"))
    
    parser.add_option("-a", "--add", dest="add",
                      help=_("Subscribe to channel from FEEDURL"), metavar="FEEDURL")
    
    parser.add_option("-d", "--delete", dest="delete",
                      help=_("Delete channel with ID n"), metavar="n")


    (options, args) = parser.parse_args(argv)

    if options.verbose:
        print "Options: "+str(options)
        print "Args: "+str(args)

    if options.debug:
        sys.path = [ './src/', '../src/' ] + sys.path
    
    # wget installation detection
    from gpodder import console
    which_wget = console.testForWget()
    if which_wget == "":
        print _("Error: cannot find wget.")
	return 20
    # which_wget
    
    if options.list:
        console.list_channels()

    elif options.run:
        console.run()
        
    elif options.update:
        console.update()

    elif options.add:
        if options.add != None and options.add != "" and (options.add[:4] == "http" or options.add[:3] == "ftp"):
            console.add_channel(options.add)
        else:
            print _('Cannot add channel: %s') % options.add

    elif options.delete:
        try:
            chid = int(options.delete)
            console.del_channel(chid)
            print _("Channel IDs may have changed, list before deleting again.")
        except ValueError:
            print _('Not a valid channel ID: %s') %options.delete

    else:
        #default run gui
        from gpodder import gpodder
        from gpodder.SimpleGladeApp import bindtextdomain
        import gtk.glade
        if options.debug:
            gpodder.glade_dir = '../data'
            gpodder.locale_dir = 'data/locale/'
            gpodder.icon_dir = 'data/gpodder.png'
            gpodder.artwork_dir = 'data/artwork/'
        bindtextdomain( 'gpodder', gpodder.locale_dir)
        gpodder.main( __version__)


if __name__ == "__main__":
    sys.exit(main())

