#!/usr/bin/env perl

package sysinfosetup;

our $VERSION = '1.0.0';
use warnings;
use strict;  

use Exporter;

our @ISA = qw( Exporter );
our @EXPORT = qw( 
                  &logIt
                  &getAnswer
                  &yesno
                  &runCommand
                  &slurp
                  $logFile
                  $DEBUG
                  $dryRun
                );

our $DEBUG = 0;
our $logFile = '/tmp/sysinfo.log';
our $dryRun = 0;

#&logIt( "Operating System is $install{'os'}" );

sub runCommand {
   my $command = shift;
   if ( $command =~ m/cd (.*)/ ) { # cd is not a command but a built in, so we can not just execute it
      chdir $1;
      print "In directory `pwd`" if $DEBUG;
      return 1;
   } else { # just a command, so run it
      print "Running [$command]\n" if $DEBUG;
      return 1 if $dryRun;
      system $command;
      if ($? == -1) {
          die "failed to execute $command: $!\n";
      } elsif ($? & 127) {
          die sprintf( "[%s] died with signal %d\n", $command, ($? & 127) );
      } else {
          die sprintf( "%s returned %d\n", $command, $? >> 8 ) if $? >> 8;
      }
   }
   return 1;
}

sub slurp {
    my $file = shift;
    open my $fh, '<', $file or die;
    local $/ = undef;
    my $cont = <$fh>;
    close $fh;
    return $cont;
}


#######################################################
# function to simply log things
# first parameter is the priority, if <= $logDef->{'log level'} will print
# all subsequent parameters assumed to be strings to sent to the log
# returns 0 on failure
#         1 on success
#         2 if priority > log level
#        -1 if $logDef is unset
# currently, only logs to a file
#######################################################
sub logIt {
   open LOG, ">>$logFile" or die "Could not append to $logFile: $!\n";
   while ( my $t = shift ) {
      print LOG &timeStamp() . "\t$t\n";
   }
   close LOG;
   return 1;
}

#######################################################
#
# timeStamp
#
# return current system date as YYYY-MM-DD HH:MM:SS
#
#######################################################
sub timeStamp {
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
   return sprintf "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec;
}

sub yesno {
   my ( $prompt, $default ) = @_;
   $default = 'yes' unless $default;
   my $answer = &getAnswer( $prompt, $default eq 'yes' ? ('yes','no' ) : ('no', 'yes') );
   return lc( substr( $answer, 0, 1 ) ) eq 'y';
}

# prompt the user for a response, then allow them to enter it
# the first response is considered the default and is printed
# in all caps if more than one exists
# first answer is used if they simply press the Enter
# key. The response is returned all lower case if more than one
# exists.
# it is assumed 
sub getAnswer {
   my ( $prompt, @answers ) = @_;
   $answers[0] = '' unless defined( $answers[0] );
   my $default = $answers[0];
   my $onlyOneAnswer = scalar( @answers ) == 1;
   print $prompt . '[ ';
   $answers[0] = uc $answers[0] unless $onlyOneAnswer;
   print join( ' | ', @answers ) . ' ]: ';
   my $thisAnswer = <>;
   chomp $thisAnswer;
   $thisAnswer = $default unless $thisAnswer;
   return $thisAnswer;
}

1;
