#!/usr/bin/perl -w

#Copyright (C) 1999-2001 by  Sbastien Chaumat <schaumat@ens-lyon.fr>
#                        and Loc Prylli <lprylli@lhpca.univ-lyon1.fr>

#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.

#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    A copy of the GNU General Public License is available as
#    `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
#    or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html.  You
#    can also obtain it by writing to the Free Software Foundation, Inc.,
#    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

use File::Copy;
use Getopt::Long;
require "dialog.pl";

Getopt::Long::Configure("no_auto_abbrev");


#defaults settings
$confdir='/etc/replicator';
$sharedir='/usr/share/replicator';
$sharedir_on_server='/usr/share/replicator';
$scriptsdir_on_server='/usr/share/replicator';
$scriptsdir_in_miniroot='/usr/sbin/';
$postinst = "$confdir/repli-postinst";
$conffile= "$confdir/replicator.conf";
$default_rules_file="update_rules.default";
$user_rules_file="$confdir/update_rules";
$hostname_file="/tmp/hostname";

#caution: it won't work if $miniroot_server is different from $model
$debarch=`dpkg --print-architecture`;
chomp($debarch);
  
$verbose=0;
$debiandir_local='/usr/local/debian';
#@networks structure:
$NETWORK=0;
$DOMAINNAME=1;
$NETMASK=2;
$GATEWAY=3;
$BROADCAST=4;
$TARGETS=5;

#
#read the configuration
#

sub check_perms {
  my $file=$_[0];
  ($uid,$perms)=(stat($file))[4,2];
  ($uid==0) or error("The file $file must be owned by root.");
  ($perms & 0033)==0 or error("The file $file must be writable/executable only by root.");
}


sub loadconf{
  if(-r $conffile) { 
    $run_from_cvs || check_perms($conffile);
    require($conffile);}
  else {die "Erreur: no file $conffile available\n;"}
 
  if (!&use_dhcp) 
    {
      $host=`uname -n`;
    }
  else {$host=`cat $hostname_file`}
  chomp($host);
    $specific_conffile=$conffile."_".$host;
  if (-r $specific_conffile) {
    $run_from_cvs || check_perms($conffile);
    require($specific_conffile);}
  

$readconf=1;
}
#
#common subs
#
sub dosystem {
  my ($package,$filename,$line) = caller;
  if ($verbose) {print STDERR "\nexec: @_\n";}
  system(@_) and &error("command failed!!! at $package, $filename, $line : @_");
}

sub docopy {
  if ($verbose) {print STDERR "\ncopying: $_[0] $_[1]\n";}
  (-r $_[0]) or &error("$_[0] not readable"); 
  copy(@_) or &error("unable to copy $_[0] to $_[1]");
}

sub error {
  &rhs_clear;
  #because we use the dialog library we must write the message to STDERR before die.
  print STDERR "ERROR: @_\n";
  die;
}

sub echo_to {
	my ($fname,$string) = @_;
	my $f = new FileHandle ">$fname" or &error("opening $fname:$!\n");
	print $f $string;
	$f->close;
}

sub check_var {
  foreach $vv  (@_) {
    if ($$vv) {
      if ($verbose) {
	print "\$$vv=$$vv\n"
      }
    } 
    else {&error("\$$vv is not defined. Check /etc/replicator/replicator.conf.")}
  } 
}

sub check_list_var {
  foreach $vv  ( @_ ) {
    if (@$vv) {
      if ($verbose){
	print "\@$vv=@$vv\n"
      }
    } 
    else {&error("\@$vv is not defined. Check /etc/replicator/replicator.conf.")}
  } 
}

sub get_ip_of {
  my $hostname=$_[0];
  $ad=gethostbyname($hostname);
  $ad=join(".",unpack("C4",$ad));
  $ad or &error("cannot find IP address for $hostname.\n Check /etc/hosts or your DNS.");
  if ($ad eq "127.0.0.1") {&error("I got $ad when looking for $hostname's IP.\n Check /etc/hosts or your DNS.")}
  return "$ad";
}

sub use_dhcp{
  $cmdline=`cat /proc/cmdline`;
  if ($cmdline=~/ip=dhcp/){$use_dhcp=1}
  else {$use_dhcp=0}
}

sub use_unstable{
  my $file='/target/etc/debian_version';
  if (!-e $file){&error("The file $file is missing")} 
  else {
    $unstable=`grep unstable $file`;
  }
}

&loadconf;
