#!/usr/bin/perl

$wget_options="--passive-ftp --dot-style=mega -c --glob=on --retr-symlinks";


@debs_cd1=qw(net/ftp
	     net/rsh-client
	     net/traceroute
	     net/rdate
	     interpreters/perl-5.005 
	     utils/strace 
	     admin/psmisc 
	     libs/zlib1g 
	     libs/libgpmg1 );

@debs_cd2=qw(admin/cfengine admin/hdparm libs/dialog  oldlibs/ncurses3.4 net/rsync);

@debs=(@debs_cd1,@debs_cd2);

sub ask_for_base {
  $title_base="Where can I fetch the file base2_2.tgz?";
  $message_base=">I need the file base2_2.tgz. 
>I can get it from an official Debian Potato mirror. 
>Which mirror should I use?
";
  @choices_base=("cdrom","use my official bin 1 CD",
		 "net","use a mirror on the Internet",
		 "local","use my local mirror");
  $width="70";
  $num_item_base=3;
  rhs_menu($title_base,$message_base,$width,$num_item_base,@choices_base) || exit 0;
  if ($dialog_result eq "cdrom") { return &get_base_from_cd}
  if ($dialog_result eq "net") { return &get_base_from_net}
  if ($dialog_result eq "local") { return &get_base_from_local}
}

sub ask_for_debs {
  my $title_debs="Where can I fetch other needed deb packages?";
  my $message_debs=">I need to fetch some official debian packages. 
>I can get them from an official Debian Potato mirrors. 
>Which method should I use to find those files?";
  my @choices_debs=("cdrom","use my official CD set",
		 "ftp","fetch from an official ftp Debian mirror on the Internet",
		 "local","use my local mirror");
  my $width="70";
  my $num_item_debs=3;
  rhs_menu($title_debs,$message_debs,$width,$num_item_debs,@choices_debs) || exit 0;
  if ($dialog_result eq "cdrom") { return &get_debs_using_cdrom; };
  if ($dialog_result eq "ftp") { return &get_debs_using_wget; };
  if ($dialog_result eq "local") { return &get_debs_using_local; };
  return undef;
}

sub ask_for_cd{
  my $cd_number=$_[0];
  system("umount /cdrom");
  if (&rhs_yesno("CD $cd_number",">Please insert the Debian 2.2 binary $cd_number CD.
>Select \"Yes\" when done.
>Select \"No\" to cancel.",50)) 
    { my $good_cd;
      system("mount /cdrom");
      if (-e "/cdrom/.disk/info") {
	$good_cd=(not system "grep -c Binary-$cd_number /cdrom/.disk/info >/dev/null");
      }
      unless ( $good_cd) {rhs_msgbox("Error","This is not Debian Potato  Binary-$cd_number CD.",50)}
      return $good_cd;
    }
  else {
    return undef;
  }
}


sub ask_for_local_mirror {
  my $title="Getting base from a local mirror";
  my $message=">Give me the path to your local Debian potato mirror. 
>It should contain the \"dists\" directory.";
  my $width=80;
  if (rhs_inputbox($title,$message,$width,$debiandir_local)){ 
    my $base="$dialog_result/dists/potato/main/disks-$debarch/current/base2_2.tgz";
    if (! -e "$base") 
      { 
	rhs_msgbox("Error","There's no file\n$base",70);
	return undef;
      }
    else 
      {
	return $dialog_result;
      }
  } else {
    return undef;
  }
}

sub ask_for_remote_mirror {
  my $title="Getting base from a remote mirror";
  #we cannot use http because wget only supports globs with ftp
  my $message=">Give me the path to your favorite remote (ftp) Debian potato mirror. 
>It should contain the \"dists\" directory";
  my $width=80;
  if (rhs_inputbox($title,$message,$width,"ftp.debian.org/debian")) 
    {
      my $remote_debiandir_to_test=$dialog_result;
      #to test if the mirror is good we try to download a small file.
      my $testfile="dists/potato/main/disks-$debarch/current/md5sum.txt";
      if (system("wget $wget_options -P $nfsroot/tmp ftp://$remote_debiandir_to_test/$testfile"))
	{
	  rhs_msgbox("Error","It'seems that $remote_debiandir_to_test is not a valid mirror.",70);
	  return undef;
	}
      else
	{
	  unlink "$nfsroot/tmp/md5sum.txt";
	  return $remote_debiandir_to_test;
      }
    } 
  else 
    {
      return undef;
    }
}

sub get_base_from_cd {
  $debiandir="/cdrom";
  my $base="$debiandir/dists/potato/main/disks-$debarch/current/base2_2.tgz";
  return &ask_for_cd("1") ? $base : undef;
}

sub get_base_from_net {
  my $remote_debiandir = &ask_for_remote_mirror;
  if ($remote_debiandir){
  my $remote_base="$remote_debiandir/dists/potato/main/disks-$debarch/current/base2_2.tgz";
    if (system("wget $wget_options -P /tmp ftp://$remote_base") == 0) {
      return "/tmp/base2_2.tgz";
    }
    else {
      rhs_msgbox("Error","I can't get ftp://$remote_base.",70)}
      return undef;
  }
}

sub get_base_from_local {
  my $base = &ask_for_local_mirror;
  return $base ? "$base/dists/potato/main/disks-$debarch/current/base2_2.tgz" : undef;
}


sub install_base {
  print STDERR "\nInstalling base system ($base) in $nfsroot...";
  unless (-d "$nfsroot") {dosystem("mkdir -p $nfsroot");
			  dosystem("chmod 0777 $nfsroot");}
  $nfsroot =~ /[a-zA-Z0-9]/ or error "$nfsroot does not seem a valid target\n";
  $taropt="xzpf";
  if ($verbose){$taropt="xzvpf";};
  dosystem("tar $taropt $base -C $nfsroot");
  unlink("$nfsroot/sbin/unconfigured.sh");
  unlink("$nfsroot/root/setup.sh");
  unlink("$nfsroot/root/.bash_profile");
  rename("$nfsroot/root/.profile.real","$nfsroot/root/.profile");
  print STDERR "done\n";
  
}

sub get_debs_using_cdrom {
  my $debiandir="/cdrom/debian/dists/potato/";
  my $base="$debiandir/main/disks-$debarch/current/base2_2.tgz";
  #process first CD
  &ask_for_cd("1") or return undef;
  my @pack=();
  foreach (@debs_cd1) {
    push @pack,"$debiandir/main/binary-$debarch/${_}_*deb";
  }
  dosystem("cp @pack $nfsroot/tmp/");
  #process second CD
  &ask_for_cd("2") or return undef;
  foreach (@debs_cd2) {
    push @pack,"$debiandir/main/binary-$debarch/${_}_*deb";
  }
  dosystem("cp @pack $nfsroot/tmp/");
  return "$nfsroot/tmp/*.deb";
}

sub get_debs_using_wget {
  my $remote_debiandir = &ask_for_remote_mirror;
  my @pack=();
  if ($remote_debiandir) {
    foreach (@debs) {
      push @pack,"ftp://$remote_debiandir/dists/potato/main/binary-$debarch/${_}_*deb";
    }
    dosystem("wget $wget_options -P $nfsroot/tmp @pack");
    return "$nfsroot/tmp/*.deb";  
  } else {
    return undef;
  }
}

sub get_debs_using_local {
  my $local_debiandir = &ask_for_local_mirror;
  if ($local_debiandir) {
    foreach (@debs) {
      dosystem("cp $local_debiandir/dists/potato/main/binary-$debarch/${_}_*deb $nfsroot/tmp/");
    }
    return "$nfsroot/tmp/*.deb";
  } else {
    return undef;
  }
}

sub install_debs {
  $endopt="";
  unless ($verbose) {$endopt=">/dev/null 2>&1"}
  dosystem("dpkg --root=$nfsroot  --unpack --force-depends --force-auto-select $debs $endopt");
  #rwhod refuse to configure because /proc is not mounted in the nfsroot
  #anyway we don't need any daemon to be started now
  rename("$nfsroot/usr/sbin/rwhod","$nfsroot/usr/sbin/rwhod.disable");  
  dosystem("chroot $nfsroot /usr/bin/dpkg --configure --pending $endopt");
}


$base_located=0;
$debs_located=0;

$base = &ask_for_base until $base;
&install_base;
$debs = &ask_for_debs until $debs;
&install_debs;
&dosystem ("test -b $nfsroot/dev/ubd0 || mknod $nfsroot/dev/ubd0 b 98 0");
&dosystem ("test -b $nfsroot/dev/ubd1 || mknod $nfsroot/dev/ubd1 b 98 1");
$done=1;
