#!/usr/bin/perl -w
#################################################################
#Copyright (c) 2000, 2001, 2002 Ryszard Pydo <rysiek@users.sf.net>
#Niniejszy program jest oprogramowaniem wolnodostpnym; moesz go
#rozprowadza dalej i/lub modyfikowa na warunkach Powszechnej Licencji
#Publicznej GNU, wydanej przez Fundacj Wolnodostpnego Oprogramowania
#- wedug wersji 2-giej tej Licencji lub ktrej z pniejszych wersji.

#Niniejszy program rozpowszechniany jest z nadziej, i bdzie on
#uyteczny - jednak BEZ JAKIEJKOLWIEK GWARANCJI, nawet domylnej
#gwarancji PRZYDATNOCI HANDLOWEJ albo PRZYDATNOCI DO OKRELONYCH
#ZASTOSOWA. W celu uzyskania bliszych informacji - Powszechna
#Licencja Publiczna GNU.

#Z pewnoci wraz z niniejszym programem otrzymae te egzemplarz
#Powszechnej Licencji Publicznej GNU; jeli nie - napisz do Free
#Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
###############################################################
use strict;
use Getopt::Long;

my $podincluded=0;

my $version="lstat-2";
my $subversion="lstat-2.3";

my $lstathttp='http://lstat.sf.net';


my $man = 0;
my $help = 0;
my $verbose =0;

my $prefix="";
my $libdir="";
my $wwwdir="";
my $bindir="";
my $etcdir="";
my $pagesdir="";
my $templatesdir="";
my $rrddir="";
my $objectsdir="";
my $initrcdir="";

my $pngurl="/lstat/";
my $apache=0;
my $updateconf=1;
my $httpdconf="";
my $wwwuser="";
my $wwwgroup="";
my $lockfile="";
my $proglock=1;

my $mounts=1;
my $fsystems="";
my @filesystems;

my $interfaces=1;
my $inter="";
my @inters;

my $disks=1;
my @diskstab;
my $diskinfo="";
my $iso=0;

my $lstatconf="";
my $mod_perl1=0;
my $mod_perl2=0;

GetOptions ('help|?' => \$help,man => \$man, 'v|verbose!' => \$verbose,
           '--with-prefix=s' => \$prefix,
	   '--with-lib=s' => \$libdir,
	   '--with-www=s' => \$wwwdir,
	   '--with-bin=s' => \$bindir,
	   '--with-etc=s' => \$etcdir,
	   '--with-templates=s' => \$templatesdir,
   	   '--with-rrd=s' => \$rrddir,
	   '--with-pages=s' => \$pagesdir,
	   '--with-objects=s' => \$objectsdir,
	   '--with-initrc=s' => \$initrcdir,
	   '--with-pngurl=s' => \$pngurl,
	   '--with-mountpoint=s' => \@filesystems,
	   '--with-interface=s' => \@inters,
	   '--with-disk=s' => \@diskstab,
	   '--lock!' => \$proglock,
	   '--iso-8859-2!' => \$iso,
	   '--with-lockfile=s' => \$lockfile,
	   '--apache!' => \$apache,
	   '--mod_perl1!' => \$mod_perl1,
	   '--mod_perl2!' => \$mod_perl2,
	   '--update_apache_conf!' => \$updateconf,
	   '--mounts!' => \$mounts,
	   '--interfaces!' => \$interfaces,
	   '--disks!' => \$disks,
	   '--with-wwwuser=s' => \$wwwuser,
	   '--with-wwwgroup=s' => \$wwwgroup,
	   '--with-httpdconf=s' => \$httpdconf,
	   '--with-lstatconf=s' => \$lstatconf

	   ) or $help=1;
if ( $help ) {
 showhelp ();
 exit (1) ;
}

$prefix="/usr/local/lstat" if (! $prefix);
addslash (\$prefix);
$libdir=$prefix.'lib/' if (! $libdir);
addslash (\$libdir);
$wwwdir=$prefix.'www/' if (! $wwwdir);
addslash (\$wwwdir);
$bindir=$prefix.'bin/' if (! $bindir);
addslash (\$bindir);
$etcdir=$prefix.'etc/' if (! $etcdir);
addslash (\$etcdir);
$templatesdir=$etcdir.'templates/' if (! $templatesdir);
addslash (\$templatesdir);
$objectsdir=$etcdir.'objects/' if (! $objectsdir);
addslash (\$objectsdir);
$pagesdir=$etcdir.'pages/' if (! $pagesdir);
addslash (\$pagesdir);
$rrddir=$etcdir.'rrd/' if (! $rrddir);
addslash (\$rrddir);
$initrcdir='/etc/rc.d/init.d/' if (! $initrcdir);
addslash (\$rrddir);
$httpdconf='/etc/httpd/conf/httpd.conf' if (! $httpdconf);
$pngurl='/lstat/' if (! $pngurl);
addslash (\$pngurl);

$lockfile=$etcdir.'lstat.lck' if (! $lockfile );
$lockfile='' if (! $proglock);
if (!$wwwuser and !$wwwgroup) {
 $apache =1;
 print "Configuring for Apache\n";
}
print "Configuring with Apache 1.3.XX mod_perl support \n" if ($mod_perl1);
print "Configuring with Apache 2.0.XX mod_perl support \n" if ($mod_perl2);
#test RRDtool library
eval "use RRDs";
if ($@) {
 print  "ERROR:\nCan't find library RRDs\.pm file on library path: \n$!\nPlease install RRDtool.\n";
 exit (1);
}
#test others libraries
my @to_test = ('CGI','Carp','Socket');
foreach my $l (@to_test) {
 eval "use $l";
 if ($@) {
 print "Cant't load perl library: $l !";
 exit 1;
 }
}

my @command =("w","users","sh","ipchains","df","fping","ifconfig","install","perl","chmod","iptables","uptime","htpasswd");
my %dict;
my @cache;
my $gifdir="";
my $gifhttp="";
my @content;


$dict{config} = $prefix.$version.'/';
$dict{version} = $subversion;
$dict{http} = $lstathttp;

my $pathline = $ENV {'PATH'};
$pathline .= ":/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin";
my @path = split (/\:/,$pathline);

#search for command
foreach my $cmd (@command) {
 my $cmddir="";

 foreach my $dir (@path) {
  if ( -x "$dir/$cmd" ) {
   $cmddir ="$dir/$cmd";
   print "Found $cmd in $dir\n" if ($verbose);
   last;
  }
 }
 $dict {$cmd} = $cmddir;
 if (! $cmddir ) { print "$cmd NOT found\n" if ($verbose);}
}


if ($apache) {
if (!$wwwuser or !$wwwgroup) {
#detect Apache server user and group
open (FILE, "<$httpdconf" ) or die "Cant open Apache httpd.conf in $httpdconf .\n Try $0 --apache --with-httpdconf\n";
@content=<FILE>;
close FILE;
foreach (@content) {
  chomp;
  $wwwuser =$1 if (/^User\s+(.+)$/);
  $wwwgroup =$1 if (/^Group\s+(.+)$/);
 }
if (!$wwwuser or !$wwwgroup) {
  die "Cant detect wwwuser or apache group from file $httpdconf.
Please specify one by additional switches:  --with-wwwuser= and --with-wwwgroup=\n";
 }
}
}
else {$updateconf=0};



#join arguments with ',' separator
my @filesystems2 = split(/,/,join(',',@filesystems));
$fsystems=join (':',@filesystems2); #convert to one line
$dict{'MOUNTS'} = $mounts;

my @inters2 = split(/,/,join(',',@inters));
$inter=join (':',@inters2); #convert to one line
$dict{'INTERFACES'}=$interfaces;

my @diskstab2 = split(/,/,join(',',@diskstab));
$diskinfo=join (':',@diskstab2); #convert to one line
$dict{'DISKS'}=$disks;

if (!$wwwuser or !$wwwgroup) {
  die "\nERROR: Please specify WWW Server's user and group \n Try $0 --with-wwwuser or/and --with-wwwgroup \n";
 }
print "INFO: Using $wwwuser and $wwwgroup as WWW Server user and group\n";
print "INFO: $httpdconf can be updated for LinuxStat directives\n" if ($updateconf);

$dict {"PREFIX"}=$prefix;
$dict {"BINDIR"}=$bindir;
$dict {"ETCDIR"}=$etcdir;
$dict {"LIBDIR"}=$libdir;
$dict {"TEMPLATESDIR"}=$templatesdir;
$dict {"OBJECTSDIR"}=$objectsdir;
$dict {"RRDDIR"}=$rrddir;
$dict {"PAGESDIR"}=$pagesdir;
$dict {"WWWDIR"}=$wwwdir;
$dict {"PNGURL"}=$pngurl;
$dict {"APACHE"}=$apache;
$dict {"HTTPDCONF"}=$httpdconf;
$dict {"LSTATCONF"}=$lstatconf;
$dict {"WWWUSER"}=$wwwuser;
$dict {"UPDATE_APACHE_CONF"}=$updateconf;
$dict {"WWWGROUP"}=$wwwgroup;
$dict {"MOUNTSINFO"}=$fsystems;
$dict {"INTERFACESINFO"}=$inter;
$dict {"DISKSINFO"}=$diskinfo;
$dict {"INITRCDIR"}=$initrcdir;
$dict {"LSTAT_LCK"}=$lockfile;
$dict {"ISO"}=$iso;
$dict {"MOD_PERL1"}=$mod_perl1;
$dict {"MOD_PERL2"}=$mod_perl2;



#save new config
setcache ('Makefile.in');
my $filename="Makefile";
open FILE2, ">$filename" or die "Cant create $filename: $!\n";
print FILE2 &fraza ('all');
close FILE2;

$filename="vars";
open FILE2, ">$filename" or die "Cant create $filename: $!\n";
foreach my $key (keys %dict) {
 print FILE2 "$key=$dict{$key}\n";
 print "$key=$dict{$key}\n" if ($verbose);

}
close FILE2;

#----------------------------------------------------------------------------------------

sub addslash {
 my $ref = shift;
 $$ref .= '/' if (! ($$ref =~ /\/$/));
}

sub fraza {
my $name=shift;
#przeszukanie calego pliku
my $jest=0;
my $data="";
foreach my $linia (@cache) {
 if ($jest) {
  #juz wczesniej odnaleziono
  if ($linia =~ /^#\@\@\w+\s/) {
   #kolejna fraza
   last;
  } else {
   $data .= $linia;
  }
 }
 if ($linia =~ /^#\@\@$name\s/i ) {
  #znaleziono
  $jest =1;
 }
}
 while ($data =~ /\@\@(\w+)\@\@/g) {
 #zamiana wartosci ze slownika
 if (defined ($dict{$1})) {
 my $rep = $1;
 $data =~ s/\@\@$rep\@\@/$dict{$rep}/g;
 }
 }
 return $data;
}




#read file to cache
sub setcache {
my $filename=shift;
open FILE, "<$filename" or die "Cant open file $filename: $!\n";
@cache=<FILE>;
close FILE;
}

#-----------------print help

sub showhelp {
print <<EOM;
configure  -  collects some information and create Makefile


configure [options]

Options:

	--help            	brief help message
	--verbose -v           	verbose information

	--with-prefix=DIR
	--with-bin=DIR
	--with-lib=DIR
	--with-etc=DIR
	--with-templates=DIR
	--with-pages=DIR
	--with-objects=DIR
	--with-rrd=DIR
	--with-initrc=DIR
	--with-www=DIR
	--with-pngurl=RELATIVE_URL

	--apache
	--mod_perl1
	--mod_perl2
	--with-httpdconf=FILE
	--with-lstatconf=DIR
	--noupdate_apache_conf
	--with-wwwuser=USERNAME
	--with-wwwgroup=GROUPNAME

	--nomounts
	--with-mountpoint=MOUNT1,MOUNT2,...

	--nointerfaces
	--with-interface=INT1,INT2,...

	--nodisks
	--with-disk=DISK1,DISK2,...

	--nolock
	--with-lockfile=FILENAME
	
	--iso-8859-2

--help
Print a brief help message and exits.

--verbose -v
Show diagnostic information

--with-prefix=DIR
main program directory [PREFIX] (default /usr/local/lstat)

--with-bin=DIR
program bin directory (default PREFIX/bin)

--with-lib=DIR
program library directory (default PREFIX/lib)

--with-etc=DIR
program etc directory [ETCDIR] (default PREFIX/etc)

--with-templates=DIR
program HTML templates directory (default ETCDIR/templates)

--with-pages=DIR
program pages directory (files which contain grupped graphs) (default ETCDIR/pages)

--with-objects=DIR
program objects directory (files describing graphs) (default ETCDIR/objects)

--with-rrd=DIR
program rrd directory (files which contain data of graphs) (default ETCDIR/rrd)

--with-initrc=DIR
program init script directory (default /etc/rc.d/init)

--with-www=DIR
program www (HTML, images and CGI) directory (default PREFIX/www)

--with-pngurl=RELATIVE_URL
Relative URL to directory where PNG images are created. (default /lstat/ )

--apache
configure with Apache server support

--mod_perl1
configure with Apache 1.3.XX server mod_perl support

--mod_perl2
configure with Apache 2.0.XX server mod_perl support

--with-httpdconf=FILE
full path to Apache httpd.conf (default /etc/httpd/conf/httpd.conf)

--with-lstatconf=DIR
directory to store file with directives for WWW Server (default the same as httpconf)

--noupdate_apache_conf
without update Apache httpd.conf. If you use Apache, you have to do it manually.

--with-wwwuser=USERNAME
WWW server username (default detect from Apache htttpd.conf or ERROR)

--with-wwwgroup=GROUPNAME
WWW server groupname (default detect from Apache htttpd.conf or ERROR)

--nomounts
Without partition statistic

--with-mountpoint=MOUNT1,MOUNT2,...
Add mount points to partition statistic. (default all local without smbfs, nfs)

--nointerfaces
Without network interfaces statistic.

--with-interface=INT1,INT2,...
Add network interfaces to statistic. INTX - name of interface eg. eth0,ppp0 . (default all without lo)

--nodisks
Without disk statistic.

--with-disk=DISK1,DISK2,...
Add disk to statistic. (default all hdXX and sdXX)

--nolock
Without security lock

--with-lockfile=FILENAME
Path to lock file


--iso-8859-2
compile with polish iso8859-2 support for graphs.
Require RRDTool patch.

EOM
;
}



