#! /usr/bin/perl
#                              -*- Mode: Cperl -*- 
# config --- 
# Author           : Manoj Srivastava ( srivasta@glaurung.internal.golden-gryphon.com ) 
# Created On       : Thu Nov  3 09:11:38 2005
# Created On Node  : glaurung.internal.golden-gryphon.com
# Last Modified By : Manoj Srivastava
# Last Modified On : Thu Nov  3 09:11:38 2005
# Last Machine Used: glaurung.internal.golden-gryphon.com
# Update Count     : 0
# Status           : Unknown, Use with caution!
# HISTORY          : 
# Description      : 
# 
# arch-tag: 9a70bb3a-30d4-4f4c-96e7-d3feb9a9752e
#

use Debconf::Client::ConfModule qw(:all);
version('2.0');
$|=1;

# Predefined values:
my $version           = "3.0.0-voyage";
my $move_image        = '';     # target machine defined
my $kimage            = "vmlinuz";   # Should be empty, mostly
my $image_dir         = "/boot";        # where the image is located
my $clobber_modules   = '';          # target machine defined
my $initrd            = "YES";        # initrd kernel
my $postinst_hook     = '';          #Normally we do not
my $postrm_hook       = '';          #Normally we do not
my $preinst_hook      = '';          #Normally we do not
my $prerm_hook        = '';          #Normally we do not
my $ignore_depmod_err = '';          # normally we do not
my $relink_src_link   = 'YES'; # There is no harm in checking the link
my $relink_build_link = 'YES'; # There is no harm in checking the link
my $force_build_link  = '';    # There is no harm in checking the link
my $arch              = "amd64"; #  should be same as dpkg --print-installation-architecture
my $kernel_arch       = "x86_64";
my $package_name    = "linux-image-$version";

#known variables
my $image_dest      = "/";
my $realimageloc    = "/$image_dir/";
my $have_conffile   = "";
my $silent_modules  = '';
my $modules_base    = '/lib/modules';
my $CONF_LOC        = '/etc/kernel-img.conf';

# Ignore all invocations except when called on to configure.
exit 0 unless $ARGV[0] =~ /configure/;
chdir('/')           or die "could not chdir to /:$!\n";

# remove multiple leading slashes; make sure there is at least one.
$realimageloc  =~ s|^/*|/|o;
$realimageloc  =~ s|/+|/|o;

if (-r "$CONF_LOC" && -f "$CONF_LOC"  ) {
  if (open(CONF, "$CONF_LOC")) {
    while (<CONF>) {
      chomp;
      s/\#.*$//g;
      next if /^\s*$/;

      $move_image      = "" if /move_image\s*=\s*(no|false|0)\s*$/ig;
      $clobber_modules = '' if /clobber_modules\s*=\s*(no|false|0)\s*$/ig;
      $silent_modules  = '' if /silent_modules\s*=\s*(no|false|0)\s*$/ig;
      $ignore_depmod_err = '' if /ignore_depmod_err\s*=\s*(no|false|0)\s*$/ig;
      $relink_src_link   = '' if /relink_src_link\s*=\s*(no|false|0)\s*$/ig;
      $relink_build_link = '' if /relink_build_link\s*=\s*(no|false|0)\s*$/ig;
      $force_build_link  = '' if /force_build_link\s*=\s*(no|false|0)\s*$/ig;

      $move_image      = "Yes" if /move_image\s*=\s*(yes|true|1)\s*$/ig;
      $clobber_modules = "Yes" if /clobber_modules\s*=\s*(yes|true|1)\s*$/ig;
      $silent_modules  = 'Yes' if /silent_modules\s*=\s*(yes|true|1)\s*$/ig;
      $ignore_depmod_err = 'Yes' if /ignore_depmod_err\s*=\s*(yes|true|1)\s*$/ig;
      $relink_src_link   = 'Yes' if /relink_src_link\s*=\s*(yes|true|1)\s*$/ig;
      $relink_build_link = 'Yes' if /relink_build_link\s*=\s*(yes|true|1)\s*$/ig;
      $force_build_link = 'Yes' if /force_build_link\s*=\s*(yes|true|1)\s*$/ig;

      $image_dest      = "$1"  if /image_dest\s*=\s*(\S+)/ig;
      $postinst_hook   = "$1"  if /postinst_hook\s*=\s*(\S+)/ig;
      $postrm_hook     = "$1"  if /postrm_hook\s*=\s*(\S+)/ig;
      $preinst_hook    = "$1"  if /preinst_hook\s*=\s*(\S+)/ig;
      $prerm_hook      = "$1"  if /prerm_hook\s*=\s*(\S+)/ig;
      $mkimage         = "$1"  if /mkimage\s*=\s*(.+)$/ig;
      $ramdisk         = "$1"  if /ramdisk\s*=\s*(.+)$/ig;
    }
    close CONF;
    $have_conffile = "Yes";
  }
}

# For some versions of kernel-package, we had this warning in the
# postinst, but the rules did not really interpolate the value in.
# Here is a sanity check.
my $pattern = "=" . "I";
$initrd=~ s/^$pattern$//;

$image_dest = "$image_dest/";
$image_dest =~ s|/+$|/|o;

$ENV{KERNEL_ARCH}=$kernel_arch if $kernel_arch;

# Paranoid check to make sure that the correct value is put in there
if    (! $kimage)                     { $kimage = "vmlinuz"; } # Hmm. empty
elsif ($kimage =~ m/^b?zImage$/o)     { $kimage = "vmlinuz"; } # these produce vmlinuz
elsif ($kimage =~ m/^[iI]mage$/o)     { my $nop = $kimage;   }
elsif ($kimage =~ m/^vmlinux$/o)      { my $nop = $kimage;   }
elsif ($kimage =~ m/^xen[0u]-linux$/o){
    $package_name       = "${kimage}-$version";}
else                                  { $kimage = "vmlinuz"; } # Default

if (-d "$kimage" ) {
  my $ret;
  my $seen;
  my $note = "${package_name}/postinst/kimage-is-a-directory";

  ($ret,$seen) = fset ("$note", 'seen', 'false');
  die "Error setting debconf flags in $note: $seen" if $ret;

  $ret = subst("$note", 'kimage', "$kimage");
  die "Error setting debconf substitutions in $note: $seen" if $ret;

  $ret = subst("$note", 'image_dest', "$image_dest");
  die "Error setting debconf substitutions in $note: $seen" if $ret;

  ($ret,$seen) = input('critical', "$note");
  if ($ret && $ret != 30 ) {
    die "Error setting debconf note $note: $seen";
  }

  ($ret,$seen) = go ();
  if ($ret && $ret != 30 ) {
    die "Error asking debconf question $note: $seen";
  }
}

# We used to have System.* files in /
if (-e "/System.map" || -e "/System.old") {
  my $ret;
  my $seen;
  my $question = "${package_name}/postinst/old-dir-initrd-link-$version";

  ($ret,$seen) = input('medium', "$question");
  if ($ret && $ret != 30 ) {
    die "Error setting debconf question $question: $seen";
  }

  ($ret,$seen) = go ();
  if ($ret && $ret != 30 ) {
    die "Error asking debconf question $question: $seen";
  }
}


exit 0;

__END__

