#!/usr/bin/env perl

use warnings;
use strict;  

our $VERSION = '1.1.3';

# find our location and use it for searching for libraries
BEGIN {
   use FindBin;
   use File::Spec;
   use lib File::Spec->catdir($FindBin::Bin);
}

use sysinfoconf;

$TESTING = 1;

my $seedFile = 'sysinfo-client.seed';
my $sysinfo2 = '/etc/sysinfo/sysinfo.conf';


sub moveFiles {
   # an extremely basic installer for sysinfo-client
   
   # create all the directories we need, set permissions
   for my $dir ( $binDir, $confDir, @moduleDirs, @scriptDirs ) {
      next if -d $dir;
      if ( $TESTING ) {
         print "mkdir -p $dir\n";
         print "chmod 0700 $dir\n";
      } else {
         `mkdir -p $dir`;
         `chmod 0700 $dir`;
      }
   }
   # copy the modules and scripts
   # all modules start with 0600, and scripts with 0700
   # modules will be "turned on" by setting their permissions to 0700
   # in the osinstall specialty scrips
   for my $dir ( 'modules', 'scripts' ) {
      if ( $TESTING ) {
         print "cp -av $dir $binDir\n";
         print "rm -fR $binDir/$dir/.svn\n";
         print "chmod 0700 $binDir/$dir\n";
         $dir eq 'scripts' ? print "chmod -fR 0700 $binDir/$dir/*\n" : print "chmod -fR 0600 $binDir/$dir/*\n";
      } else {
         `cp -av $dir $binDir`;
         `rm -fR $binDir/$dir/.svn`;
         `chmod 0700 $binDir/$dir`;
         $dir eq 'scripts' ? `chmod -fR 0700 $binDir/$dir/*` : `chmod -fR 0600 $binDir/$dir/*`;
      }
   }

   # copy the binary files and the documentation. Default all to
   # 0600
   for my $file ( 'sysinfo-client','notes', 'sysinfo-client.conf.template','configure.pl', 'uninstall.pl', 'sysinfoconf.pm' ) {
      if ( $TESTING ) {
         print "cp $file $binDir\n";
         print "chmod 0600 $binDir/$file\n";
      } else {
         `cp $file $binDir`;
         `chmod 0600 $binDir/$file`;
      }
   }

   # Set permissions for the executable files
   for my $file ( "$binDir/sysinfo-client", "$binDir/configure.pl", "$binDir/uninstall.pl" ) {
      if ( $TESTING ) {
         print "chmod 0700 $file\n";
      } else {
         `chmod 0700 $file`;
      }
   }
   
   # Everything is owned by root
   foreach my $dir ( $binDir, $confDir ) {
      if ( $TESTING ) {
         print "chown -fR root:root $dir\n";
      } else {
         `chown -fR root:root $dir`;
      }
   }

}

# simply attempts to detect the operating system so we can do OS
# specific actions at the end.   
sub getOperatingSystem {
   my @OSTypes = ( 'ipfire','debian' );
   my $OS = `uname -a`;
   foreach my $osType ( @OSTypes ) {
      return $osType if $OS =~ m/$osType/i;
   }
   return '';
} # getOperatingSystem


sub convertSysinfo2 {
   my $client_name;
   my $iMailResults;
   my $mailTo;
   my $mailSubject;
   my $mailCC;
   my $mailBCC;
   my $mailServer;
   my $mailServerPort;
   my $mailFrom;
   my $SENDMAIL;
   my $clientName;
   my $serialNumber;
   my $hostname;
   my $transports = {}; # holds transportation mechanisms
   open SEED, "<$sysinfo2" or die "Could not open $sysinfo2: $!\n";
   my $temp = join( '', <SEED> );
   close SEED;
   eval( $temp );
   if ( $iMailResults ) {
      $temp = {};
      $$temp{'-name-'} = 'SendEmail';
      $$temp{'mailTo'} = $mailTo if $mailTo;
      $$temp{'$mailFrom'} = $mailFrom if $mailFrom;
      $$temp{'mailCC'} = $mailCC if $mailCC;
      $$temp{'mailServer'} = $mailServer if $mailServer;
      $$temp{'mailServer'} .= ":$mailServerPort" if $mailServerPort;
      $$transports{'1'} = $temp;
   }   
   $clientName = $client_name if ( $client_name );
   return ( $clientName,$hostname,$serialNumber,$transports );
}

# for installation, just go and see if we have anything we can grab
# to make a config. We'll do them in a particular order, with each
# subsequent stage overriding the previous one.
# first, look for any old sysinfo2 config, then, look for an existing
# sysinfo3 config, then look for a seed file
sub makeConfig {
   if ( -f $sysinfo2 && &yesno( 'Old sysinfo 2 config exists, load it for defaults' ) ) {
      print "Loading defaults from sysinfo2 config $sysinfo2\n";
      # NOTE: the return values are all placed into globals!!!!
      ( $clientName,$hostname,$serialNumber,$transports ) = &convertSysinfo2();
   }
   # if they have a current sysinfo3 config, see if we can load it
   if ( -f $sysinfo3 && &yesno( 'I found an existing sysinfo3 configuration, Load it? ' ) ) {
      print "Loading defaults from existing config $sysinfo3\n";
      my $client_name;
      open SEED, "<$sysinfo3" or die "Could not open $sysinfo3: $!\n";
      my $temp = join( '', <SEED> );
      close SEED;
      eval( $temp );
   }
   # seed files are expected to be in the correct format already
   # and expected to override everything.
   if ( -f $seedFile  && &yesno( 'Add installation seed file? ' ) ) {
      print "Loading seed file $seedFile\n";
      open SEED, "<$seedFile" or die "Could not open $seedFile: $!\n";
      my $temp = join( '', <SEED> );
      close SEED;
      eval( $temp );
   }
}


&processParameters( @ARGV );

&moveFiles();

exit ;

if ( &yesno( "Preseed the configuration file?" ) ) {
   &makeConfig();
   print "Writing Config file; run configure.pl to modify it\n";
   &writeConfig( $confDir, $confName, &showConf() );
   #print &showConf();
   #&yesno( "Press enter to continue" );
}

my $os = &getOperatingSystem();
if ( $os && &yesno( "This appears to be an $os system, may I automatically set up a few things" ) ) {
   require "$os.pm";
   &systemSpecificInstall();
}

exec( "perl $binDir/configure.pl" ) if &yesno( "Would you like to configure the package now? " );

