#! /usr/bin/perl
#                              -*- Mode: Cperl -*-
# image.preinst ---
# Author           : Manoj Srivastava ( srivasta@tiamat.datasync.com )
# Created On       : Sun Jun 14 03:38:02 1998
# Created On Node  : tiamat.datasync.com
# Last Modified By : Manoj Srivastava
# Last Modified On : Mon Apr 13 14:21:59 2009
# Last Machine Used: anzu.internal.golden-gryphon.com
# Update Count     : 100
# Status           : Unknown, Use with caution!
# HISTORY          :
# Description      :
#
#

#
#use strict; #for debugging

use Debconf::Client::ConfModule qw(:all);
version('2.0');
my $capb = capb("backup");

$| = 1;

# Predefined values:
my $version           = "=V";
my $move_image        = '';     # target machine defined
my $kimage            = "=K";   # Should be empty, mostly
my $image_dir         = "=D";   # where the image is located
my $clobber_modules   = '';     # target machine defined
my $initrd            = "=I";   # 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              = "=A";   #  should be same as dpkg --print-architecture
my $kernel_arch       = "=B";
my $package_name       = "=ST-image-$version";
my $kernel_pkg_version = "=KPV";

#known variables
my $image_dest     = "/";
my $realimageloc   = "/$image_dir/";
my $have_conffile  = "";
my $CONF_LOC       = '/etc/kernel-img.conf';
my $silent_modules = '';
my $warn_reboot = 'Yes';        # Warn that we are installing a version of
                                # the kernel we are running

my $modules_base = '/lib/modules';

die "Pre inst Internal error. Aborting." unless $version;

exit 0 if $ARGV[0] =~ /abort-upgrade/;
exit 1 unless $ARGV[0] =~ /(install|upgrade)/;

# 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 m/^\s*move_image\s*=\s*(no|false|0)\s*$/i;
      $clobber_modules = '' if m/^\s*clobber_modules\s*=\s*(no|false|0)\s*$/i;
      $silent_modules  = '' if m/^\s*silent_modules\s*=\s*(no|false|0)\s*$/i;
      $warn_reboot     = '' if m/^\s*warn_reboot\s*=\s*(no|false|0)\s*$/i;
      $ignore_depmod_err = ''
        if m/^\s*ignore_depmod_err\s*=\s*(no|false|0)\s*$/i;
      $relink_src_link = '' if m/^\s*relink_src_link\s*=\s*(no|false|0)\s*$/i;
      $relink_build_link = ''
        if m/^\s*relink_build_link\s*=\s*(no|false|0)\s*$/i;
      $force_build_link = ''
        if m/^\s*force_build_link\s*=\s*(no|false|0)\s*$/i;

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

      $image_dest    = "$1" if m/^\s*image_dest\s*=\s*(\S+)/i;
      $postinst_hook = "$1" if m/^\s*postinst_hook\s*=\s*(\S+)/i;
      $postrm_hook   = "$1" if m/^\s*postrm_hook\s*=\s*(\S+)/i;
      $preinst_hook  = "$1" if m/^\s*preinst_hook\s*=\s*(\S+)/i;
      $prerm_hook    = "$1" if m/^\s*prerm_hook\s*=\s*(\S+)/i;
      $mkimage       = "$1" if m/^\s*mkimage\s*=\s*(.+)$/i;
      $ramdisk       = "$1" if m/^\s*ramdisk\s*=\s*(.+)$/i;
    } ## end while (<CONF>)
    close CONF;
    $have_conffile = "Yes";
    $have_conffile = "Yes";    # stop perl complaining
  } ## end if ( open( CONF, "$CONF_LOC"...))
} ## end if ( -r "$CONF_LOC" &&...)

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

# 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$//;

sub check {
  my $version     = shift;
  my $lib_modules = "$modules_base/$version";
  my $message     = '';

  if ( -d "$lib_modules" ) {
    opendir( DIR, $lib_modules ) || die "can’t opendir $lib_modules: $!";
    my @children = readdir(DIR);
    if ( $#children > 1 ) {
      my @dirs = grep { -d "$lib_modules/$_" } @children;
      if ( $#dirs > 1 ) {    # we have subdirs
        my $dir_message = '';
        for my $dir (@dirs) {
          if ( $dir =~ /kernel$/ ) {
            $dir_message = "An older install was detected.\n";
          }
          else {
            $dir_message = "Module sub-directories were detected.\n"
              unless $dir_message;
          }
        } ## end for my $dir (@dirs)
        $message += $dir_message if $dir_message;
      } ## end if ( $#dirs > 1 )

      my @links = grep { -l "$lib_modules/$_" } @children;
      if ( $#links > -1 ) {
        my $links_message = '';
        for my $link (@links) {
          next if ( $link =~ /^build$/ );
          next if ( $link =~ /^source$/ );
          $links_message
            = "Symbolic links were detected in $modules_base/$version.\n";
        } ## end for my $link (@links)
        $message += $links_message if $links_message;
      } ## end if ( $#links > -1 )
      my @files = grep { -f "$lib_modules/$_" } @children;
      $message += "Additional files also exist in $modules_base/$version.\n"
        if ( $#files > -1 );
    } ## end if ( $#children > 1 )
  } ## end if ( -d "$lib_modules")
  else { $message .= "$lib_modules does not exist. "; }
  return $message;
} ## end sub check

if ( -d "$modules_base/$version" ) {
  my $errors = check($version);
  warn "Info:\n$errors\n" if $errors;
}

if ( -d "$modules_base/$version/kernel" ) {
  if ($clobber_modules) {
    my $ret = system(
      "mv $modules_base/$version/kernel $modules_base/${version}_kernel_$$");
    my $seen;
    if ($ret) {
      my $note = "${package_name}/preinst/failed-to-move-modules-$version";

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

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

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

      $ret = subst( "$note", 'dest', "${version}/kernel_$$" );
      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";
      }
      exit 1;
    } ## end if ($ret)
  } ## end if ($clobber_modules)
  elsif ( $silent_modules !~ m/YES/i ) {
    my $ret;
    my $seen;
    my $answer;
    my $question = "${package_name}/preinst/overwriting-modules-$version";

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

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

    $ret = subst( "$question", 'package', "$package_name" );
    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";
    }
    $invisible = $ret if $ret == 30;

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

    ( $ret, $answer ) = get("$question");
    die "Error retreiving answer for $question: $answer" if $ret;

    $answer =~ s/^\s+//;
    $answer =~ s/\s+$//;
    print STDERR "Ok, aborting, since modules for this image already exist.\n"
      unless $answer =~ /^(f|n)/i;
    if ( $answer !~ /^(f|n)/i && $invisible ) {
      my $note = "${package_name}/preinst/abort-overwrite-$version";

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

      ( $ret, $seen ) = fset( "$note", 'seen', 'false' );
      die "Error setting debconf flags 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";
      }
    } ## end if ( $answer !~ /^(f|n)/i...)
    exit 1 unless $answer =~ /^(f|n)/i;
  } ## end elsif ( $silent_modules !~...)
  else {
    print STDERR <<EOF;
The directory $modules_base/$version still exists. Continuing as directed.
EOF
  }
} ## end if ( -d "$modules_base/$version/kernel")

if ( -f "$modules_base/$version/modules.dep" && $warn_reboot ) {
  my $running = '';
  chop( $running = `uname -r` );
  if ( $running eq $version ) {
    my $note = "${package_name}/preinst/already-running-this-$version";

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

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

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

    $ret = subst( "$note", 'running', "$running" );
    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";
    }
  } ## end if ( $running eq $version)
} ## end if ( -f "$modules_base/$version/modules.dep"...)

# set the env var stem
$ENV{'STEM'} = "=ST";

sub exec_script {
  my $type   = shift;
  my $script = shift;
  print STDERR "Running $type hook script $script.\n";
  system("$script $version $realimageloc$kimage-$version")
    && print STDERR "User $type hook script [$script] ";
  if ($?) {
    if ( $? == -1 ) {
      print STDERR "failed to execute: $!\n";
    }
    elsif ( $? & 127 ) {
      printf STDERR "died with signal %d, %s coredump\n",
        ( $? & 127 ), ( $? & 128 ) ? 'with' : 'without';
    }
    else {
      printf STDERR "exited with value %d\n", $? >> 8;
    }
    exit $? >> 8;
  } ## end if ($?)
} ## end sub exec_script

sub run_hook {
  my $type   = shift;
  my $script = shift;
  if ( $script =~ m,^/, ) {

    # Full path provided for the hook script
    if ( -x "$script" ) {
      &exec_script( $type, $script );
    }
    else {
      die "The provided $type hook script [$script] could not be run.\n";
    }
  } ## end if ( $script =~ m,^/, )
  else {
    # Look for it in a safe path
    for my $path ( '/bin', '/sbin', '/usr/bin', '/usr/sbin' ) {
      if ( -x "$path/$script" ) {
        &exec_script( $type, "$path/$script" );
        return 0;
      }
    } ## end for my $path ( '/bin', ...)

    # No luck
    print STDERR "Could not find $type hook script [$script].\n";
    die "Looked in: '/bin', '/sbin', '/usr/bin', '/usr/sbin'\n";
  } ## end else [ if ( $script =~ m,^/, )]
} ## end sub run_hook

# Set up the env variable containing our arguments
my $out;
for (@ARGV) {
  s,','\\'',g;
  $out .= " '$_'";
}
$ENV{'DEB_MAINT_PARAMS'}       = "$out";
$ENV{'KERNEL_PACKAGE_VERSION'} = "$kernel_pkg_version";

if ( !$initrd ) {
  $ENV{INITRD} = 'No';
}
## Run user hook script here, if any
if ( -d "/etc/kernel/preinst.d" ) {
  print STDERR "Examining /etc/kernel/preinst.d/\n";
  system( "run-parts --verbose --exit-on-error --arg=$version"
      . " --arg=$realimageloc$kimage-$version"
      . " /etc/kernel/preinst.d" )
    && die "Failed to process /etc/kernel/preinst.d";
} ## end if ( -d "/etc/kernel/preinst.d")
if ( -d "/etc/kernel/preinst.d/$version" ) {
  print STDERR "Examining /etc/kernel/preinst.d/$version.\n";
  system( "run-parts --verbose --exit-on-error --arg=$version"
      . " --arg=$realimageloc$kimage-$version"
      . " /etc/kernel/preinst.d/$version" )
    && die "Failed to process /etc/kernel/preinst.d/$version";
} ## end if ( -d "/etc/kernel/preinst.d/$version")

if ( -x "$preinst_hook" ) {
  &run_hook( "preinst", $preinst_hook );
}

print STDERR "Done.\n";

exit 0;

__END__
