#!/usr/bin/perl
#
# D. Bortolotti 4 Maggio '06
# Modifica dell'originale per consentire configurazione a sistemi DHCP

use Socket;
use Sys::Hostname;

################ Configuration name files

$cupsclient     = "/etc/cups/client.conf";
$cupsclientorig = "/etc/cups/client.conf.orig";
$cupsdaemon     = "/etc/cups/cupsd.conf";
$cupsdaemonorig = "/etc/cups/cupsd.conf.orig";
$lpoptions      = "/etc/cups/lpoptions";


################ Check Ip and Subnet address
$host   = hostname();
$addr   = inet_ntoa(scalar(gethostbyname($host)) || 'localhost');
$addr   =~ m/(\d+)\.(\d+)\.(\d+)/;
$subnet = $3;
if ($subnet eq '10' || $subnet eq '12')  {
    $area = "i";
    $defaultprinter = "phaseribn";
    }
elsif ($subnet eq '11' || $subnet eq '102')  {
    $area = "m";
    $defaultprinter = "phaserm";
} else {

    print "=======================================================================\n\n";    
    print "Indica la sede principale di lavoro (Irnerio = 1, Morassutti = 2)\n\n";
    print "per definire server e stampante di default\nIrnerio: Server = prserveri, stampante = phaseribn\nMorassutti: Server = prserverm, stampante = lpsccm\n\n";
    print "=======================================================================\n";

    $sede = "3";
    while ($sede ne "1" and $sede ne "2") {
       print "Valori di input previsti: [1/2] ";
       chop ($sede = <STDIN>);
    }

    if ($sede eq "1") {
       
      $area = "i";
      $defaultprinter = "phaseribn"; 

    } else {
      
      $area = "m";
      $defaultprinter = "phaserm";
    }

}

# Cups server definition
$prserver       = "prserver$area.bo.infn.it";

# Stop cups service
system ("/sbin/service cups stop");
system ("/sbin/chkconfig cups off");

# Restore original configuration files
if (-e "$cupsclientorig") { 
   system ("/bin/cp -f $cupsclientorig $cupsclient"); 
} 
if (-e "$cupsdaemonorig") { 
   system ("/bin/cp -f $cupsdaemonorig $cupsdaemon"); 
} 
else {
   system ("/bin/cp -f $cupsdaemon $cupsdaemonorig");
}


# Modify parameters client.conf file
#system ("exec perl -pi -e 's|#ServerName myhost.domain.com|ServerName $prserver|g' $cupsclient");
system ("/bin/rm -f $cupsclient");
system ("exec echo \"ServerName $prserver\" >> $cupsclient");


# Modify parameters cupsd.conf file 
system ("perl -pi -e 's|Port 631\n|#Port 631\n|g' $cupsdaemon");
system ("perl -pi -e 's|#BrowseAllow address|BrowseAllow 127.0.0.1|g' $cupsdaemon");
system ("perl -pi -e 's|#BrowseDeny address|BrowseDeny All|g' $cupsdaemon");
system ("perl -pi -e 's|#BrowseInterval 30|BrowseInterval 0|g' $cupsdaemon");
system ("perl -pi -e 's|#BrowseOrder deny,allow|BrowseOrder deny,allow|g' $cupsdaemon");
system ("perl -pi -e 's|<Location />|<Location />\nAllow From $addr|g' $cupsdaemon");

# Create a new lpoptions file 
open(OPTIONS,">$lpoptions");
print OPTIONS "Default $defaultprinter\n";
print OPTIONS <<EOF;
Dest phaseribn-two number-up=2
Dest phaseribn-simplex sides=one-sided
Dest phaserm-two number-up=2
Dest phaserm-simplex sides=one-sided
Dest phaseric-two number-up=2
Dest phasermc-two number-up=2
Dest lpscci-two number-up=2
Dest phaseri-two number-up=2
Dest lps2-two number-up=2
Dest lpstg-two number-up=2
EOF
close(OPTIONS);

# Uncomment following lines if you want to define local printers
#system ("/sbin/service cups start") ;
#system ("/sbin/chkconfig cups on") ;

