#! /usr/bin/env perl use strict; use warnings; # find our location and use it for searching for libraries BEGIN { use FindBin; use File::Spec; # use libraries from the directory this script is in use lib File::Spec->catdir($FindBin::Bin); # and its parent use lib File::Spec->catdir( $FindBin::Bin . '/../' ); eval( 'use YAML::Tiny' ); } use Cwd qw(abs_path); use sysinfosetup; my $scriptDir = abs_path(File::Spec->catdir($FindBin::Bin) ); use Digest::MD5 qw(md5_hex); use File::Copy; # define the version number # see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod use version; our $VERSION = version->declare("v0.1.2"); use Data::Dumper; use File::Basename; use Getopt::Long; Getopt::Long::Configure ("bundling"); # allow -vd --os='debian' our $dryRun; our $DEBUG; our %operatingSystems; my $os; my $help = 0; my $version = 0; my $quiet = 0; my $seedFileName = ''; sub oldPathFromNew { my ($path) = @_; return '' unless defined $path; $path =~ s/sysinfo_client/sysinfo-client/g; return $path; } sub migratePath { my ( $oldPath, $newPath, $description ) = @_; return unless $oldPath && $newPath; return unless -e $oldPath || -l $oldPath; if ( -e $newPath || -l $newPath ) { die "Can not migrate $description from $oldPath to $newPath because both paths exist\n"; } my $command = 'mv ' . shellQuote($oldPath) . ' ' . shellQuote($newPath); print "Migrating $description\n\t$command\n" unless $quiet; &logIt( "Migrating $description with command [$command]" ); return if $dryRun; if ( doCommand( $command ) ) { die "I had an error migrating $description from $oldPath to $newPath\n"; } } sub migrateLegacyInstallPaths { my ( $os, $scriptDir ) = @_; do "$scriptDir/installer_config.pl"; my $newBindir = $operatingSystems{$os}{'bindir'}; my $newConfdir = $operatingSystems{$os}{'confdir'}; my $oldBindir = oldPathFromNew( $newBindir ); my $oldConfdir = oldPathFromNew( $newConfdir ); &migratePath( $oldConfdir, $newConfdir, 'configuration directory' ); if ( defined $operatingSystems{$os}{'crontab'} ) { my $newCron = $operatingSystems{$os}{'crontab'}; $newCron =~ s/^.*\s(\/[^\s]+)$/$1/; my $oldCron = oldPathFromNew( $newCron ); &migratePath( $oldCron, $newCron, 'scheduler entry' ) if $newCron =~ m|^/|; } return if $newBindir eq abs_path( File::Spec->catdir( $scriptDir . '/../' ) ); &migratePath( $oldBindir, $newBindir, 'install directory' ); } sub shellQuote { my ($value) = @_; $value = '' unless defined $value; $value =~ s/'/'"'"'/g; return "'$value'"; } # simple display if --help is passed sub help { use File::Basename; print basename($0) . " $VERSION\n"; print <> 8; } return 0; } ################################################################ # Main Code # ################################################################ # handle any command line parameters that may have been passed in GetOptions ( "os|o=s" => \$os, # pass in the operating system "dryrun|n" => \$dryRun, # do NOT actually do anything "seed|s=s" => \$seedFileName, 'help|h' => \$help, 'version|v' => \$version, 'quiet|q' => \$quiet, 'debug!' => \$DEBUG ) or die "Error parsing command line\n"; if ( $help ) { &help() ; exit; } if ( $version ) { use File::Basename; print basename($0) . " $VERSION\n"; exit; } my $returnValue = 0; # first, find operating system my $command = $scriptDir . '/determineOS'; $os = `$command` unless $os; chomp $os if defined $os; &logIt( "Detected operating system is [$os]" ); die "Could not detect operation system\n" unless $os; &migrateLegacyInstallPaths( $os, $scriptDir ); # now, verify we have all the libraries we need and install them if necessary $command = $scriptDir . '/validateDependencies' . " -o $os" . ( $quiet ? ' -q' : '' ) . ( $DEBUG ? ' --debug' : '') . ( $dryRun ? ' -n' : '' ); &logIt( "Running command $command" ); if ( doCommand( $command ) ) { die "I had an error executing $command\n"; } &logIt( "\tReturn value is $returnValue" ); $command = $scriptDir . '/checkFiles' . " -o $os" . ( $quiet ? ' -q' : '' ) . ( $DEBUG ? ' --debug' : '') . ( $dryRun ? ' -n' : '') . ( $seedFileName ? ' -s ' . shellQuote($seedFileName) : '' ); &logIt( "Running command $command" ); if ( doCommand( $command ) ) { die "I had an error executing $command\n"; } &logIt( "\tReturn value is $returnValue" ); # check permissions $command = $scriptDir . '/permissions' . ( $quiet ? ' -q' : '' ) . ( $DEBUG ? ' --debug' : '') . ( $dryRun ? ' -n' : '' ) . ( $os ? ' -o ' . shellQuote($os) : '' ); &logIt( "Running command $command" ); if ( doCommand( $command ) ) { die "I had an error executing $command\n"; } # run configure $command = $scriptDir . "/../configure" . ( $quiet ? ' -q' : '' ) . ( $dryRun ? ' -n' : '' ); &logIt( "Running command $command" ); if ( doCommand( $command ) ) { die "I had an error executing $command\n"; } &logIt( "\tReturn value is $returnValue" );