#!/usr/bin/perl
# Configure a production server
# v1.2
#
# Patrice LaFlamme
# patrix@netwinder.org
#


#
# MAIN - just launches the other subs
#
sub main {

 
 # Parse arguments: dev = install for devel server; ship = install for
 # shipping server.  No arguments given means dev.
 $type = $ARGV[0];
 if ($type eq "") {
        $type = "ship";
        print "No server type specified, assuming shipping...\n";
        print "Use \"setup-server.pl dev\" for development server\n";
        print "or \"setup-server.pl ship\" for shipping server.\n\n";
 }
 # Devel and shipping servers use different install scripts for now. Copy
 # the correct one.
 system("cp /vncroot/root/install.$type /vncroot/root/install");

 # Determine IP address, eth interface
 print "Finding ip address...\n";
 find_info();

 # Find all the images and create /vncroot/root/install.conf file
 print "Creating install.conf...\n";
 create_conf($type);

 # Edit /etc/dhcpd.conf
 print "Creating /etc/dhcpd.conf...\n";
 edit_dhcpd();

 # Edit /etc/hosts
 print "Editing /etc/hosts...\n";
 edit_hosts();

 # Edit /etc/exports
 print "Creating /etc/exports...\n";
 edit_exports();

 # Edit /vncroot/etc/fstab
 print "Creating /vncroot/etc/fstab...\n";
 edit_client_fstab();

 print "\n *** You may now reboot ***\n";

}

#
# Create /vncroot/root/install.conf file
#
sub create_conf {

open (FILE, ">/vncroot/root/install.conf") or die "Could not open file: $!";

 my $type = $_[0];

 # Finds all the images in the /home/ftp/pub directory
 if ($type eq "dev") {
  @images = `cd /home/ftp/pub; ls dm* gs* ws* os* sd* base* 2>/dev/null`;
 }
 if ($type eq "ship") {
  @images = `cd /home/ftp/pub; ls dm-2.1-12* ws* gs* 2>/dev/null`;
 }

 my @firm = `cd /vncroot/root; ls nettrom*.bin 2>/dev/null`;
 my $len = @firm;
 my $firmware = @firm[$len-1];
 my @citrix = `cd /vncroot/root; ls nettrom.citrix* 2>/dev/null`;
 my $len = @citrix;
 my $citrix_firm = @citrix[$len-1];
 chomp ($firmware, $citrix_firm);

 # Config file
 print FILE <<EOF;
# Don't forget to change this for each build created!
# \$Id: setup-server.pl,v 1.3 1999/08/26 13:30:16 patrix Exp $
build = 10

# Default firmware for diskless machines
firmware = $firmware

EOF

 if ($type eq "dev") {
  print FILE <<EOF;
# Default firmware for Citrix clients
citrix = $citrix_firm

EOF
 }

 # Adds entries for every image found
 my $len = @images;
 for ($i = 0; $i < $len; $i++) {
  $images[$i] =~ m/(^[a-z]*)-(\d*\.\d*)-(\d*)/;
  $image = $1;
  $version = $2;
  $build = $3;

  # Very flaky code here, change as soon as we get more info
  if ($image eq 'os') {
   $partition = "scheme_3";
  } elsif ($image eq 'dm' and $build > 13) {
   $partition = "scheme_3";
  } else {
   $partition = "scheme_2";
  }
  chomp ($images[$i], $image, $version, $build, $partition);

  # Enf of flaky code

  if ($image eq "dm" ) {
   print FILE <<EOF;
# Development Machine build $build
image = DM drive image
	version = $version build $build
	file = $images[$i]
	partition = $partition

EOF
  } elsif ($image eq "sd") {
   print FILE <<EOF;
# Sophisticated Desktop build $build
image = SD drive image
	version = $version build $build
	file = $images[$i]
	partition = $partition

EOF
  } elsif ($image eq "gs") {
   print FILE <<EOF;
# Group Server
image = GS drive image
	version = $version release $build
	file = $images[$i]
	partition = $partition

EOF
  } elsif ($image eq "ws") {
   print FILE <<EOF;
# Web Server
image = WS drive image
	version = $version release $build
	file = $images[$i]
	partition = $partition

EOF
  } elsif ($image eq "os") {
   print FILE <<EOF;
# Office Server
image = OfficeServer
	version = $version release $build
	file = $images[$i]
	partition = $partition

EOF
  } elsif ($image eq "base") {
   print FILE <<EOF;
# Base
image = Base
	version = $version build $build
	file = $images[i]
	partition = $partition

EOF
  }
 }
 close FILE;

}

#
# Configure /etc/hosts
#
sub edit_hosts {
 my ($address, $address2, $name);

 $shouldEdit = `grep PRODUCTION /etc/hosts >>/dev/null|| echo 1`;
 
 if ($shouldEdit) {
  # Custom hostnames can be added to /vncroot/root/hosts.in
  system ("cat /vncroot/root/hosts.in >> /etc/hosts 2>/dev/null");

  # Open /etc/hosts to append stuff
  open (FILE, ">>/etc/hosts") or die "Could not open file: $!";

  # Add this line so that stuff is not uselessly added each time
  # script is run.
  print FILE "# PRODUCTION server stuff starts here\n";
 
  # Write the data to /etc/hosts, in format:
  # <ip address>	client-<ip>.netwinder.org	client-<ip>
 
  for ($i = 1; $i<=253; $i++) {
   $address = $subnet . $i;
   $address2 = "Client-" . $i;
   $name = $address2 . ".netwinder.org" . " $address2";
   print FILE "$address	$name\n";
  }
 }
 close FILE;

}

#
# Find ip address, subnet, etc
#
sub find_info {
 # Find ip address of eth1

 $ip = `/sbin/ifconfig eth1 |grep inet |cut -d ' ' -f 12|cut -d ':' -f 2`;
 chomp $ip;
 print "IP address of eth1: $ip\n";

 # Get the subnet (first 3 numbers of the IP address)
 $subnet = $ip;
 chomp $subnet;
 $subnet =~ /(^[0-9]*\.[0-9]*\.[0-9]*\.)/;
 $subnet = $1;

}

#
# Configure dhcpd server
#
sub edit_dhcpd {

 open (FILE, ">/etc/dhcpd.conf") or die "Could not open file: $!";

 print FILE "subnet " . $subnet ."0 netmask 255.255.255.0 {\n";
 print FILE "	range " . $subnet ."1 " . $subnet ."253;\n";
 print FILE "	default-lease-time 120;\n";
 print FILE "	max-lease-time 120;\n\n";
 print FILE "	option subnet-mask 255.255.255.0;\n";
 print FILE "	option routers " . $subnet . "1;\n";
 print FILE "	option domain-name \"netwinder.org\";\n";
 print FILE "	option broadcast-address " . $subnet . "255;\n\n";
 print FILE "	server-name \"$ip\";\n";
 print FILE "	filename \"vmlinux_vnc.rev4\";\n";
 print FILE "	option root-path \"$ip:/vncroot\";\n";
 print FILE "}\n";

 close FILE;

 system 'touch /etc/dhcpd.leases';
 system 'chkconfig --level 35 dhcpd on';

}

#
# Configure NFS exports file
#
sub edit_exports {

 # Custom exports can be added to /vncroot/root/exports.in
 system("cat /vncroot/root/exports.in > /etc/exports 2>/dev/null");

 open (FILE, ">>/etc/exports") or die "Could not open file: $!";
                                 
 print FILE "/vncroot	" . $subnet . "0/255.255.255.0(rw, no_root_squash)\n";

 close FILE;
}

#
# Configure fstab for client machines
#
sub edit_client_fstab {

 open (FILE, ">/vncroot/etc/fstab") or die "Could not open file: $!";

 print FILE <<EOF;
#
# /etc/fstab
#
# You should be using fstool (control-panel) to edit this!
#
# <device>    <mountpoint>   <filesystemtype> <options> <dump> <fsckorder>
$ip:/vncroot  /                       nfs     defaults        1 1
none                    /proc                   proc    defaults        0 0
/dev/hda1               /mnt/hda1               ext2    defaults,noauto 1 1
/dev/hda2               /mnt/hda2               ext2    defaults,noauto 1 1
/dev/hda3               /mnt/hda3               ext2    defaults,noauto 1 1
/dev/hda4               /mnt/hda4               ext2    defaults,noauto 1 1
EOF

 close FILE;

}

# Beginning of script
main();
