#!/usr/bin/perl

###############################
#
###############################


my $username_file="user_name";

# This function generates random strings of a given length
sub generate_random_string
{
    my $length_of_randomstring=shift;# the length of 
	 # the random string to generate

	my @chars=('a'..'z','A'..'Z');
    my $random_string;
    foreach (1..$length_of_randomstring) 
    {
	# rand @chars will generate a random 
	# number between 0 and scalar @chars
	$random_string.=$chars[rand @chars];
    }
    return $random_string;
}

#Generate the random string
my $random_string=&generate_random_string(8);


my $algorithm=shift;
my $bootserver=shift;
my $file=shift;
my $hostname=shift;
my $port=shift;
print $file;


#get the ipaddress of the node
use Socket;
$ipaddr = inet_ntoa(inet_aton($hostname));

print "Random string: ".$random_string."\n";
system ("echo $random_string >> $username_file $hostname $ipaddr $port");
print "Length: ".length($random_string)."\n";


open (F,$file) || die "Can't open - $!\n";
@File = <F>;
close (F);

open (F,">$file") || die "Can't open - $!\n";
for (@File) {
    s/algorithm/$algorithm/;
    s/bootserver/$bootserver/;s/$search/$replace/;
    s/dummyid/$random_string/;
    push(@contents,$_);
    print F $_;
}
#    printf @contents;

# Go to the beginning of the file
#seek(F,0,0);

# Update the contents with the data from our array
#print F (@contents);
close(F);

