#!/usr/bin/perl

#Copyright (C) 1999-2001 by  Sbastien Chaumat <schaumat@ens-lyon.fr>
#                        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-licences/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.

use FileHandle;

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

sub good_disk;
sub find_disk;
sub calc_part_bound;

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

sub init_kbd {
@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;
if ($kbd eq "default") {
 dosystem("$loadkeys $default_kmap".".gz");
 }
else {
 dosystem("loadkeys $kbd");
 }
}

sub save_kbd{
  dosystem("chroot /target install-keymap $kbd")
}

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{
  &dosystem("repli-install --real all");
  &save_kbd;
  &dosystem("mount -t proc proc /target/proc");
  &docopy("$confdir/repli-postinst","/target/$confdir/repli-postinst");
  &dosystem("cd /target/etc && chroot /target /bin/bash $confdir/repli-postinst");
}

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

init_kbd;

if (&use_dhcp){
  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=">%-s \t  %-s \t %-s \n";
  $mp="mount_point";
  $part="min_size";
  $fs="max_size";
  $tt=sprintf $f,$mp,$part,$fs;
  foreach $p (@autopart_specs) {
    my $mp = $p->[0];
    my $min= $p->[1];
    my $max = $p->[2];
    $tt.=sprintf $f,$mp,$min,$max;
  }
  rhs_yesno("Automatic partitioning",">Do the installation automatically?? 
>
>This will partition $disk ($status) according to (\@autopart_specs):
>
$tt
>
> Non-swap filesystems will be created with command : $mkfs",80) || abort;
  rhs_yesno("Danger",">$disk will be erased!!!
>
>Do you REALLY want to continue and partition the harddisk like this:
>
$tt
>
>mkfs command : $mkfs
",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=">%-20s  %-16s %-7s \n";
  $f=">%-s \t  %-s \t %-s \n";
  $mp="mount_point";
  $part="partition_number";
  $fs="format?";
  $tt=sprintf $f,$mp,$part,$fs;
  foreach $p (@manualpart_specs) {
    my $mp = $p->[0];
    my $part = $p->[1];
    my $format = $p->[2];
    if ($format) { $fs="Yes"} else {$fs="No"}
    $tt.=sprintf $f,$mp,$part,$fs;
  }
  rhs_yesno("Manual partitioning",">You can now switch to another console and partition the disk.
>
> The disk for replication is $disk ($status)
>
> Non-swap filesystems will be created with command : $mkfs
>
>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 (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.";
}
