#!/usr/bin/perl

#Copyright (C) 1999-2005 by  Sbastien Chaumat <schaumat@debian.org>
#                        and Loc Prylli <lprylli@lhpca.univ-lyon1.fr>

#    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 of the License, 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.

#    A copy of the GNU General Public License is available as
#    `/usr/share/common-licenses/GPL' in the Debian GNU/Linux distribution
#    or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html.  You
#    can also obtain it by writing to the Free Software Foundation, Inc.,
#    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# changelog
#
# 09-26-2004
#   *change : workaround console-common bug #272480 in save_kbd
#
# 08-29-2004
#   *change : &loadconf called here and no more in repli-common
#
# 08-27-2004
#   *fixed : call to preinst script (was calling postinst, close bug #268354)
#
# 08-10-2004
#  *add : custom fs on every partition with [$PMKFS]
#
#
# 01-03-2004
#   *change : init_kbd and save_kbd loads default only if neither $keymap nor @extra_keymaps
#             list is defined (SEb) -> NEED TEST DOC
#
# 01-01-2004
#   *add : execute bash if the user doesn't ask to reboot : this prevent init from launching a getty 
#          (and ask for login) (SEb)
#
# 12-21-2003
#   *add : $autoreboot (SEb)
#
# 09-20-2003
#   *add : a test to check if the postinst script is readable (SEb)
#   *add : call to preinst script if it exists (SEb)
#   *add : if $nowarn is defined don't warn, the user about partitioning (SEb)
#   *add : if $keymap="same", skip the keyboard choice dialog (SEb)

use FileHandle;

#default values
$nowarn='';
$same_kmap='';

require "dialog.pl";
require("/usr/share/replicator/repli-common");
sub loadconf;
&loadconf;

sub good_disk;
sub find_disk;
sub calc_part_bound;

sub abort {
  error("Aborted by user request!\n");
}

sub init_kbd {
  $kbd="default";
  if ($keymap) {
      unless ($keymap eq "same") {
	  $kbd=$keymap;
      }
  }
  elsif (@extra_keymaps){
    @choices=("default","same keyboard as $model",@extra_keymaps);
    $title="Kernel (not X) keyboard configuration";
    $message_kbd="Choose the right keyboard!";
    $width="70";
    $num_item=@choices/2;
    rhs_menu($title,$message,$width,$num_item,@choices) || exit 0;
    $kbd=$dialog_result;
  }
  #at this point we know which keymap to load
  if ($kbd eq "default") {
    &dosystem("$loadkeys $default_kmap");
  }
  else {
    &dosystem("loadkeys $kbd");
  }
}

sub save_kbd{
  if ($kbd eq "default") {
    &docopy($default_kmap,"/target/$default_kmap" );
  }
  else {
    #install-keymap does not return 0 on success (see bug #272480 for the console-common package)
    &dosystem("chroot /target install-keymap $kbd || true");
  }
}

sub ask_for_hostname {
  rhs_inputbox("Hostname configuration","What is the name of the computer we are installing?",50) || abort;
  my $hostname=$dialog_result;
  if (!($hostname=~/^[a-z0-9-]+$/)) {error("$hostname is not a valid hostname!")};
  return $hostname;
}

sub replicate{
  if(-r $preinst) {
  &dosystem("/bin/sh $preinst");
  }
  &dosystem("repli-install --real all");
  &save_kbd;
  &dosystem("mount -t proc proc /target/proc");
  if(-r $postinst) {
    &docopy("$postinst","/target/$confdir/repli-postinst");
    &dosystem("cd /target/etc && chroot /target /bin/sh $confdir/repli-postinst");
  }
}

#real stuff starts here
$mf="$confdir/$model_info";
if (-r $mf){require($mf)}

init_kbd;

if (&use_dhcp){
  #check if hostname is available
  #TODO
  #only ask_for_hostname if we can't find the hostname
  my $hostname = ask_for_hostname;
  dosystem("hostname $hostname\n");
  &loadconf;
}


#detect destination media


if (!$custom_part){
  &calc_part_bound;
  $disk = find_disk->[0];
  $status="detected by replicator";
  if ($target_disk) {
    $disk=$target_disk;
    $status="forced in $conffile";
  };
  $f=">%-20s \t  %-12s \t %-12s \t %-s \n";
  $mp="mount point";
  $part="min size";
  $fs="max size";
  $mkfs_command="mkfs command";
  $tt=sprintf $f,$mp,$part,$fs,$mkfs_command;
  foreach $p (@autopart_specs) {
    my $mp = $p->[$PDIR];
    my $min= $p->[$PMIN];
    my $max = $p->[$PMAX];
    my $mkfs_command = $p->[$PMKFS] || $mkfs;
    if ( $mp eq "swap") { $mkfs_command = "swap" ;};
    $tt.=sprintf $f,$mp,$min,$max,$mkfs_command;
  }
  unless ($nowarn) {
		  rhs_yesno("Automatic partitioning",">Do the installation automatically?? 
>
>This will partition $disk ($status) according to (\@autopart_specs):
>
$tt",80) || abort;
  rhs_yesno("Danger",">$disk will be erased!!!
>
>Do you REALLY want to continue and partition the harddisk like this:
>
$tt
",80)|| abort;
		 }
    &replicate;
}
else{
  #to avoid checking disk size against @autopart_specs
  $param{'minsize'}=0;
  $disk = find_disk->[0];
  $status="detected by replicator";
  if ($target_disk) {
    $disk=$target_disk;
    $status="forced in $conffile";
  };
  $f=">%-25s \t %-9s \t %-s\n";  
  $mp="mount_point";
  $part="partition";
  $mkfs_command="mkfs command";
  $tt=sprintf $f,$mp,$part,$mkfs_command;
  foreach $p (@manualpart_specs) {
    my $mp = $p->[0];
    my $part = $p->[1];
    my $mkfs_command;
    unless ($mkfs_command = $p->[2]){
      $mkfs_command="none"
    };
    $tt.=sprintf $f,$mp,$part,$mkfs_command;
  }
unless ($nowarn) {
		rhs_yesno("Manual partitioning",">You can now switch to another console and partition the disk.
>
> The disk for replication is $disk ($status)
>
> Your formatting choices are (\@manualpart_specs):
>
$tt",80) || abort;
  rhs_yesno("Danger",">Ready for install?
>
>Disk $disk will be partition/formatted according to:
>
$tt
>mkfs command : $mkfs",80) || abort;
	       }
  &replicate;
}

if ($autoreboot) {
  &dosystem("umount /target/proc");
  &dosystem("shutdown -r now");
}

if (rhs_yesno("Replication terminated!","Reboot?",50)){
  &dosystem("umount /target/proc");
  &dosystem("shutdown -r now");
}
else{
  print STDERR "OK do what you want with this bash shell.\nThe mount table is:\n";
  &dosystem("mount");
  print STDERR "Don't forget to MANUALY umount any /proc filesystem on the target before rebooting.";
  &dosystem("/bin/bash");
}
