#!/usr/bin/perl

$deb_last_IP=141;
$fin_last_IP=165;
$num_poste=1;
$salle='e25';





sub make_config_file {
        my ($IP_new,$hostname_new,$name_config)=@_;

	#répertoire ou stocker les fichiers de configurations fabriqués
	$rep_pour_config='/export/home/marc/kleber_gygabit/config/';

	$reseau='kleber.linux';

	#ligne à modifier dans auto_kleber.cfg
	$nom1="'IPADDR' => '192.168.117.151'";
	$nom2="'HOSTNAME' => 'p01-e25.kleber.linux'";
	$nom_fichier='/export/home/marc/kleber_gygabit/config/auto_kleber.cfg';
	open(FICHIERVAR, $nom_fichier) or print "Impossible ouvrir $nom_fichier \n";
		@contenu=<FICHIERVAR>;
	close(FICHIERVAR);

	my 	$chaine = join "" , @contenu;

	#morceau à changer dans $nom1
	$nom1=~m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/s;
	$IP_old=$1;
	$nom1_new=$nom1;
	$nom1_new=~s/$IP_old/$IP_new/s;
	print 'installation avec IP : '."$nom1_new\n";

	#morceau à changer dans $nom2
	$nom2=~m/.*'(.*?)'$/s;
	$hostname_all_old=$1;
	$hostname_all_new=$hostname_new.'.'.$reseau;
	$nom2_new=$nom2;
	$nom2_new=~s/$hostname_all_old/$hostname_all_new/s;
	print 'et avec nom : '."$nom2_new\n";

	#modification de la config

	$newconfig=$chaine;
	$newconfig=~s/$nom1/$nom1_new/gs;
	$newconfig=~s/$nom2/$nom2_new/gs;
	#print "$newconfig\n";
	$name_config_all=$rep_pour_config.$name_config;
	open(FICHIERVAR, '>', $name_config) or print "Impossible ouvrir $name_config \n";
        print FICHIERVAR $newconfig;
	close(FICHIERVAR);

}

sub format_disket {

#nécessite d'avoir le fichier image network.img dans le répertoire du programme
        my ($IP_new,$hostname_new,$name_config)=@_;

my $chaine1="
default linux
prompt 1
timeout 1
display boot.msg
F1 help.msg
F2 advanced.msg
F3 boot.msg
label linux
  kernel vmlinuz
  append auto_install=config/";
my $chaine2=" automatic=met:nfs,ser:192.168.114.155,dir:/var/distro/9.2,int:eth0,netw:static,ip:";
my $chaine3=",netm:255.255.0.0,gat: initrd=network.rdz ramdisk_size=128000 root=/dev/ram3  acpi=ht vga=788\n";
	#pour salle E21
	#$config="auto"."$i".'_21.cfg';
	#pour salle E25
	$chaine=$chaine1.$name_config.$chaine2.$IP_new.$chaine3;

	print "$i\n\n";
	print(`/usr/bin/fdformat /dev/fd0u1440`);
	print(`umount /mnt/floppy`);
	#pour salle E21
	#print(`dd if=network.img of=/dev/fd0`);
	#pour salle E25
	#print(`dd if=network_gigabit_usb.img of=/dev/fd0`);
	print(`dd if=network_gigabit_usb.img of=/dev/fd0`);
	print(`umount /mnt/floppy`);
	print(`mount /mnt/floppy`);
	open(FICHIERVAR,'>','/mnt/floppy/syslinux.cfg') or die "Impossible ouvrir  \n";
        @contenu=<FICHIERVAR>;
	print FICHIERVAR $chaine;
        close(FICHIERVAR);
	print "$chaine\n";
	print(`umount /mnt/floppy`);
	print(`mount /mnt/floppy`);
	print(`cat /mnt/floppy/syslinux.cfg`);
	print(`umount /mnt/floppy`);
	print("config pour poste $hostname_new terminee\n");
	readline(STDIN);

}





for($num=$deb_last_IP ; $num <= $fin_last_IP ; $num++ ) {
my	$IP='192.168.117.';
my	$IP_new=$IP.$num;
my	$poste='p'.$num_poste;
my	$hostname_new=$poste.'-'."$salle";
my	$name_config=$hostname_new.'.pl';
# deux sous programmes pour être sur de la concordance des noms et des IPs
#en pratique ne faire fonctionner que un sur deux....;

	make_config_file($IP_new,$hostname_new,$name_config);
	#format_disket($IP_new,$hostname_new,$name_config);

	$num_poste++;

}

