#! /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           = "2.6.26-1-686";
my $link_in_boot      = "";   # Should be empty, mostly
my $no_symlink        = "";   # Should be empty, mostly
my $reverse_symlink   = "";   # Should be empty, mostly
my $do_symlink        = "Yes";  # target machine defined
my $do_boot_enable    = "Yes";  # target machine defined
my $do_bootfloppy     = "Yes";  # target machine defined
my $do_bootloader     = "Yes";  # target machine defined
my $move_image        = '';     # target machine defined
my $kimage            = "";   # Should be empty, mostly
my $loader            = ""; # lilo, silo, quik, palo, vmelilo, nettrom, arcboot or delo
my $image_dir         = "/boot";        # where the image is located
my $clobber_modules   = '';          # target machine defined
my $relative_links    = "";          # target machine defined
my $initrd            = "YES";        # initrd kernel
my $mkimage           = "";   # command to generate the initrd image
my $do_initrd         = '';     # Normally we do not
my $warn_initrd       = 'YES';  # Normally we do
my $use_hard_links    = ''; # hardlinks do not work across fs boundaries
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 $minimal_swap      = '';          # Do not swap symlinks
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 $official_image    = "YES";  # only true for official images
my $arch              = "i386"; #  should be same as dpkg --print-installation-architecture
my $kernel_arch       = "x86";
my $ramdisk           = "mkinitramfs-kpkg mkinitrd.yaird";  # List of tools to create initial ram fs.
my $initrddep         = "D";  # List of dependencies for such tools
my $package_name    = "linux-image-$version";

my $Loader          = "NoLOADER"; #
$Loader             = "LILO"     if $loader =~ /^lilo/io;
$Loader             = "SILO"     if $loader =~ /^silo/io;
$Loader             = "QUIK"     if $loader =~ /^quik/io;
$Loader             = "yaboot"   if $loader =~ /^yaboot/io;
$Loader             = "PALO"     if $loader =~ /^palo/io;
$Loader             = "NETTROM"  if $loader =~ /^nettrom/io;
$Loader             = "VMELILO"  if $loader =~ /^vmelilo/io;
$Loader             = "ZIPL"     if $loader =~ /^zipl/io;
$Loader             = "ELILO"    if $loader =~ /^elilo/io;
$Loader             = "ARCBOOT"  if $loader =~ /^arcboot/io;
$Loader             = "DELO"     if $loader =~ /^delo/io;

#known variables
my $image_dest      = "/";
my $realimageloc    = "/$image_dir/";
my $have_conffile   = "";
my $silent_modules  = '';
my $silent_loader   = '';
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*$/;

      $do_symlink      = "" if /do_symlinks\s*=\s*(no|false|0)\s*$/ig;
      $no_symlink      = "" if /no_symlinks\s*=\s*(no|false|0)\s*$/ig;
      $reverse_symlink = "" if /reverse_symlink\s*=\s*(no|false|0)\s*$/ig;
      $link_in_boot    = "" if /image_in_boot\s*=\s*(no|false|0)\s*$/ig;
      $link_in_boot    = "" if /link_in_boot\s*=\s*(no|false|0)\s*$/ig;
      $move_image      = "" if /move_image\s*=\s*(no|false|0)\s*$/ig;
      $clobber_modules = '' if /clobber_modules\s*=\s*(no|false|0)\s*$/ig;
      $do_boot_enable  = '' if /do_boot_enable\s*=\s*(no|false|0)\s*$/ig;
      $do_bootfloppy   = '' if /do_bootfloppy\s*=\s*(no|false|0)\s*$/ig;
      $relative_links  = '' if /relative_links \s*=\s*(no|false|0)\s*$/ig;
      $do_bootloader   = '' if /do_bootloader\s*=\s*(no|false|0)\s*$/ig;
      $do_initrd       = '' if /do_initrd\s*=\s*(no|false|0)\s*$/ig;
      $warn_initrd     = '' if /warn_initrd\s*=\s*(no|false|0)\s*$/ig;
      $use_hard_links  = '' if /use_hard_links\s*=\s*(no|false|0)\s*$/ig;
      $silent_modules  = '' if /silent_modules\s*=\s*(no|false|0)\s*$/ig;
      $silent_loader   = '' if /silent_loader\s*=\s*(no|false|0)\s*$/ig;
      $minimal_swap    = '' if /minimal_swap\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;

      $do_symlink      = "Yes" if /do_symlinks\s*=\s*(yes|true|1)\s*$/ig;
      $no_symlink      = "Yes" if /no_symlinks\s*=\s*(yes|true|1)\s*$/ig;
      $reverse_symlink = "Yes" if /reverse_symlinks\s*=\s*(yes|true|1)\s*$/ig;
      $link_in_boot    = "Yes" if /image_in_boot\s*=\s*(yes|true|1)\s*$/ig;
      $link_in_boot    = "Yes" if /link_in_boot\s*=\s*(yes|true|1)\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;
      $do_boot_enable  = "Yes" if /do_boot_enable\s*=\s*(yes|true|1)\s*$/ig;
      $do_bootfloppy   = "Yes" if /do_bootfloppy\s*=\s*(yes|true|1)\s*$/ig;
      $do_bootloader   = "Yes" if /do_bootloader\s*=\s*(yes|true|1)\s*$/ig;
      $relative_links  = "Yes" if /relative_links\s*=\s*(yes|true|1)\s*$/ig;
      $do_initrd       = "Yes" if /do_initrd\s*=\s*(yes|true|1)\s*$/ig;
      $warn_initrd     = "Yes" if /warn_initrd\s*=\s*(yes|true|1)\s*$/ig;
      $use_hard_links  = "Yes" if /use_hard_links\s*=\s*(yes|true|1)\s*$/ig;
      $silent_modules  = 'Yes' if /silent_modules\s*=\s*(yes|true|1)\s*$/ig;
      $silent_loader   = 'Yes' if /silent_loader\s*=\s*(yes|true|1)\s*$/ig;
      $minimal_swap    = 'Yes' if /minimal_swap\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$//;

if ($link_in_boot) {
  $image_dest = "/$image_dir/";
  $image_dest =~ s|^/*|/|o;
}

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

# Official powerpc images may silently upgrade
if ($official_image =~ /^\s*YES\s*$/o && $arch =~ m/powerpc/) {
  $silent_modules = 'Yes';
}
$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;   }
else                              { $kimage = "vmlinuz"; } # Default

# search for the boot loader in the path
my $loader_exec;
($loader_exec = $loader)  =~ s|.*/||;
my ($loaderloc) = grep -x, map "$_/$loader_exec",
  map { length($_) ? $_ : "." }  split /:/, $ENV{PATH};

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";
  }
}

if (! -e "$kimage" && ! $no_symlink && ! $reverse_symlink && 
    ! $have_conffile ) {
  my $ret;
  my $seen;
  my $question = "${package_name}/postinst/create-kimage-link-$version";

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

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

if ($initrd && -l "$image_dest/initrd" ) {
  my $ret;
  my $seen;
  my $question = "${package_name}/postinst/old-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";
  }
}

if ($initrd && -l "$image_dir/initrd" && ! $link_in_boot) {
  my $ret;
  my $seen;
  my $question = "${package_name}/postinst/old-dir-initrd-link-$version";

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

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

  ($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";
  }
}


# 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__

