#!/usr/bin/perl
# DrakxTV
# $Id: drakxtv 233182 2008-01-23 12:16:18Z tv $

# Copyright (C) 2002-2008 Mandriva (tvignaud@mandriva.com)
#
# 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, 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 lib qw(/usr/lib/libDrakX);

use common;
use standalone; #- warning, standalone must be loaded very first, for 'explanations'

use interactive;
use detect_devices;
use lang;
use log;

$ugtk2::wm_icon = "/usr/share/mcc/themes/default/tv-mdk.png";

my $in = 'interactive'->vnew;

my @devices = detect_devices::getTVcards();
push @devices, { driver => 'bttv', description => 'dummy' } if $::testing && !@devices;
my ($devices, $devices_ok) = partition { detect_devices::isTVcardConfigurable($_) } @devices;

my $modules_conf;

# handle TV cards which driver needs to be configured b/c it cannot autodetect the card & tuner types:
if (@devices = @$devices) {
    my $not_canceled = 1;
    # TODO: That need some work for multiples TV cards
    each_index {
	   if (($< == 0 || $::testing) && (grep { detect_devices::isTVcardConfigurable($_) } @devices)) {
		  require harddrake::v4l;
		  require modules;

		  $modules_conf ||= modules::any_conf->read;
		  $not_canceled &&= harddrake::v4l::config($in, $modules_conf, $_->{driver});
		  $modules_conf->write;
	   }
    } @devices;
}

# handle TV cards that do not require any driver configuration:
if (@devices = @$devices_ok) {
    require modules;
    $modules_conf ||= modules::any_conf->read;
    $modules_conf->write;
}

# we failed to detect any TV card:
if (is_empty_array_ref($devices) && is_empty_array_ref($devices_ok)) {
    $in->ask_warn(N("No TV Card detected!"), formatAlaTeX(
                                                          #-PO: keep the double empty lines between sections, this is formatted a la LaTeX
                                                          N("No TV Card has been detected on your machine. Please verify that a Linux-supported Video/TV Card is correctly plugged in.")));
}
$in->exit(0) if defined $in;


# TODO:
# - offer to sort channels after
# - use Video-Capture-V4l-0.221 ?
# - configure kwintv and zapping ? => they've already wizards :-(
# - install xawtv if needed through consolhelper