#!/usr/bin/perl -w

#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 abort {
  error("Aborted by user request!\n");
}

sub init_kbd {
@choices=("default","same keyboard as $miniroot_server","fr-pc","french keyboard","us","us keyboard","uk","uk keyboard");
$title="Kernel (not X) keyboard configuration";
$message_kbd="Choose the right keyboard!";
$width="70";
$num_item=4;
rhs_menu($title,$message,$width,$num_item,@choices) || exit 0;
$kbd=$dialog_result;
if ($kbd eq "default") {
#to change for woody miniroot
 dosystem("loadkeys /etc/console-tools/default")
 } 
else { 
 dosystem("loadkeys $kbd");
 }
}

sub save_kbd{
  if (&use_unstable){
    dosystem("mkdir -p /target/etc/console");
    dosystem("dumpkeys > /target/etc/console/boottime.kmap");
    dosystem("gzip -f9 /target/etc/console/boottime.kmap");
  } 
  else {
    dosystem("mkdir -p /target/etc/console-tools");
    dosystem("mkdir -p /target/etc/kbd");
    dosystem("dumpkeys > /target/etc/console-tools/default.kmap");
    dosystem("gzip -f9 /target/etc/console-tools/default.kmap");
    docopy("/target/etc/console-tools/default.kmap.gz","/target/etc/kbd/default.map.gz");
  }
}

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

sub replicate{
  dosystem("repli-install --real all");
  save_kbd;
  dosystem("mount -t proc proc /target/proc");
  docopy("/root/repli-postinst","/target/root/repli-postinst");
  dosystem("cd /target/root && chroot /target /bin/bash /root/repli-postinst");
}

#real stuff starts here
$mf="/root/replicator-model-name";
if (-r $mf){$miniroot_server=`cat $mf`}

init_kbd;

if (&use_dhcp){
  ask_for_hostname;
  echo_to("$hostname_file","$hostname\n");
  &loadconf;
}

if (!$custom_part){
  $f=">%-20s  %-10s %-10s \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 the harddisk according to:
>
$tt",80) || abort;
  rhs_yesno("Danger",">All disks will be erased!!!
>
>Do you REALLY want to continue and partition the harddisk like this:
>
$tt",80)|| abort;
  replicate;
}
else{
  $f=">%-20s  %-16s %-7s \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.
>
>Your formatting choices are:
>
$tt",80) || abort;
  rhs_yesno("Danger",">Ready for install? 
>
>Disks will be formatted according to:
>
$tt",80) || abort;
  replicate;
}

if (rhs_yesno("Replication terminated!","Reboot?",50)){dosystem("shutdown -r now");}
else{
print STDERR "OK do what you want with this bash shell.\nThe mount table is:\n";
dosystem("mount");
}
