#!/usr/bin/perl -w

package Debconf::Client::ConfModuleX;

# Ripped right out of libapache-sessionx-perl, thanks to Angus Lees.

#
# Add the error checking behaviour that I think
# Debconf::Client::ConfModule should have anyway
#

use Debconf::Client::ConfModule;

use base qw(Exporter);

BEGIN {
  *EXPORT_OK = \@Debconf::Client::ConfModule::EXPORT_OK;
  *EXPORT_TAGS = \%Debconf::Client::ConfModule::EXPORT_TAGS;
}

sub AUTOLOAD {
  my $cmd = our $AUTOLOAD;
  $cmd =~ s|.*:||;

  my ($ret, $text) = &{"Debconf::Client::ConfModule::$cmd"}(@_);
  if ($ret == 0 or $ret >= 30 && $ret < 100) {
    # expected return values
    return wantarray ? ($ret, $text) : $text;
  }

  $cmd = uc $cmd;
  my $msg = $text ? "$text ($ret)" : "code $ret";

  require Carp;
  if ($ret < 10 || $ret >= 110) {
    Carp::confess("Debconf $cmd returned reserved error code? $msg");
  } elsif ($ret < 20) {
    # Dump @_ ?
    Carp::croak("Debconf $cmd: invalid parameters: $msg");
  } elsif ($ret < 30) {
    Carp::confess("Debconf $cmd: syntax error: $msg");
  } else { # $ret < 110
    Carp::confess("Debconf $cmd: debconf internal error: $msg");
  }
}

1;

package main;

sub move_file {
    my ($src, $dst) = @_;
    rename($src, $dst) or system("mv $src $dst");
}

BEGIN { Debconf::Client::ConfModuleX->import(qw(:all)) }

use strict;

version('2.0');
capb('multiselect');

my @alsa_modules = metaget("alsa-common/card-list", "choices");
my %alsa_modules_list = ();
foreach (split(/, /, $alsa_modules[1])) {
  /^([^\s]+)\s(.+)/;
  $alsa_modules_list{$1} = $2;
}
$alsa_modules_list{all} = "";

if (-f "/etc/alsa-source.conf") {
  move_file("/etc/alsa-source.conf", "/etc/alsa/alsa-source.conf");
}

my %debconf_config = ('has_pnp' => '', 'debug' => '', 'cards_to_be_built' => '');

foreach my $key (keys(%debconf_config)) {
  my @tmp_deb = get("alsa-source/$key");
  $debconf_config{$key} = $tmp_deb[1];
}

my @config_lines = ();
if (-f "/etc/alsa/alsa-source.conf") {
  open(ALSACONF, "</etc/alsa/alsa-source.conf");
  foreach (<ALSACONF>) {
    if (/^ALSA_/) {
      chomp;
      push(@config_lines, $_);
    }
  }
  close ALSACONF;
}

my %parsed_config = ();

foreach (@config_lines) {
  my @tmp_array = split(/=/, $_);
  $tmp_array[1] =~ s/"(.*)"/$1/;
  $parsed_config{lc($tmp_array[0])} = $tmp_array[1];
}

if (!exists($parsed_config{alsa_nopnp})) {
	$parsed_config{alsa_nopnp} = "n";
}
if (!exists($parsed_config{alsa_debug})) {
	$parsed_config{alsa_debug} = "n";
}
if (!exists($parsed_config{alsa_cards})) {
	$parsed_config{alsa_cards} = "all";
}

subst("alsa-source/cards_to_be_built", "alsa_modules", $alsa_modules[1]);

# If the config file exists, believe it, otherwise debconf.
if (-f "/etc/alsa/alsa-source.conf") {
  my $tmp_card_array = "";
  foreach (split(/, /, $parsed_config{alsa_cards})) {
    $tmp_card_array .= "$_ $alsa_modules_list{$_}, ";
  }
  $tmp_card_array =~ s/\, $//;
  set("alsa-source/cards_to_be_built", $tmp_card_array);
  if ($parsed_config{alsa_nopnp} eq "y") {
    set("alsa-source/has_pnp", "false");
  } else {
    set("alsa-source/has_pnp", "true");
  }
  if ($parsed_config{alsa_debug} eq "y") {
    set("alsa-source/debug", "true");
  } else {
    set("alsa-source/debug", "false");
  }
} else {
  if ($debconf_config{cards_to_be_built}) {
    set("alsa-source/cards_to_be_built", $debconf_config{cards_to_be_built});
  } else {
    set("alsa-source/cards_to_be_built", "all");
  }
  if ($debconf_config{has_pnp}) {
    set("alsa-source/has_pnp", $debconf_config{has_pnp});
  }
  if ($debconf_config{debug}) {
    set("alsa-source/debug", $debconf_config{debug});
  }
}

input("medium", "alsa-source/has_pnp");
input("medium", "alsa-source/debug");
input("medium", "alsa-source/cards_to_be_built");
go();

