#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell
#
# A Gtk2 TV show viewer
#
# Copyright (C) Petri Vakevainen since 2004 GPL
#

=pod

=head1 NAME

gshowtv - A TV show schedule viewer

=head1 SYNOPSIS

B<gshowtv> [options] [xmltvfile(s)]

 Options:
   --parse | -p                  quietly parse the xmltv data
   --slave | -s                  slave mode for parsing
   --debug | -d                  enable debug messages to STDERR

  The xmltvfile is mandatory if running with --parse

=head1 DESCRIPTION

I<gshowtv> is a TV program schedule viewer and a Personal Video Recorder GUI.

GShow TV is a TV program schedule viewer and a Personal Video Recorder GUI.
GShow TV's basic purpose is to provide a nice GUI for viewing tv program
schedule information and for recording the programs. GShow TV doesn't
itself do the recording of the selected programs, rather it uses any
PVR solution that exists. GShow TV is globally usable as it uses XMLTV
to access the program schedules, and xmltv has support for multitude of
countries. (mid 2004 support for Canada, the USA, the UK, Germany,
Austria, Finland, Spain, Italy, the Netherlands, Hungary, Denmark,
Japan, Sweden, France, Norway, Portugal and Romania)

=head1 FILES

=over

=item B<$HOME/.gshowtv/>        Configuration directory

=item B<$HOME/.gshowtv/images>  Channel images are stored into this directory

=back

=head1 BUGS

=over

=item None known

=head1 AUTHOR

Petri Vakevainen <Petri.Vakevainen@iki.fi>

=head1 SEE ALSO

B<xmltv>(3) B<xmltv-druid>(1)

=cut

package GShowTV;

use utf8;
use strict;
use warnings;
use lib 'lib';

# Set basic io to UTF8
binmode(STDERR, ":utf8");
binmode(STDOUT, ":utf8");
binmode(STDIN,  ":utf8");

# Localization initialization
use POSIX qw /setlocale/;
use Locale::TextDomain qw/gshowtv/;
use Locale::Messages qw /bind_textdomain_filter bind_textdomain_codeset turn_utf_8_on LC_MESSAGES/;

setlocale( LC_MESSAGES, "" );

BEGIN {
    bind_textdomain_filter  'gshowtv', \&turn_utf_8_on;
    bind_textdomain_codeset 'gshowtv', 'utf-8';
}

# Find out the requested locale for displaying the manual. TODO Use scrollkeeper
my $lang = $ENV{'LANGUAGE'}; 
$lang    = $ENV{'LANG'} unless defined ($lang);
$lang    = $ENV{'LC_MESSAGES'} unless defined ($lang);
$ENV{'REQUESTED_LANGUAGE'} = $lang;

# Disable localization if target localization doesn't exist. This has to be done 
# before Gtk2->init() to disable translations provided by Gtk2.

$ENV{'LANGUAGE'} = "" unless ($GShowTV::Constants::LOCALIZATION_ENABLED);

# Rest of our imports
use Getopt::Long;
use Pod::Usage;
use GShowTV::Data::Parser;
use GShowTV::Debug;
use GShowTV::Main;
use Gtk2;

###############################################################################
# Begin program
###############################################################################

$GShowTV::Constants::run_command = $0;

## Option holders

my $opt_parse    = 0;
my $opt_slave    = 0;

## Initialize required directories

mkdir $GShowTV::Constants::confdir  unless (-e $GShowTV::Constants::confdir);
mkdir $GShowTV::Constants::imagedir unless (-e $GShowTV::Constants::imagedir);
mkdir $GShowTV::Constants::datadir  unless (-e $GShowTV::Constants::datadir);

###############################################################################
# Parse options and see what should be run
###############################################################################

Getopt::Long::Configure ("bundling");

# Read in the options and decide which mode of operation is needed

GetOptions ("parse|p" => \$opt_parse,
			"debug|d" => \$GShowTV::Debug::DEBUG,
			"slave|s" => \$opt_slave) || pod2usage(2);

# Handle the two special cases of gshowtv's potential running.

if ($opt_parse) {
  pod2usage(2) if (!defined (@ARGV) || scalar @ARGV == 0);

  GShowTV::Data::Parser::parse($GShowTV::Constants::confdir, $GShowTV::Constants::datadir, $opt_slave, @ARGV);
  exit;
}

Gtk2->init();

Gtk2::SimpleList->add_column_type('color',
	type     => 'Gtk2::Gdk::Color',
	renderer => 'Gtk2::CellRendererText',
	attr     => 'hidden'
);

my $a = GShowTV::Main->new();
$a->run();
