#!/usr/bin/perl

## @file datetimeconfig
# @brief This module is used to configure the date and time settings
# @details This script will set current system date and time
#          if there are no cli arguments provided then
#          user will be prompted to enter them.
#
# @author Alex Arkhipov
# @date   January 2007
#
# @version 1.00 - Initial version
# @version 1.01 - POD section was added; the usage is displayed by the
#          common function - print_pod_usage() now.
# @version 2.00 - Added timezone configuration
# @version 2.01 - Two colons were removed before shared functions and variables.
# @version 2.10 - 'check_timezone' has been modified. Now it checks existence of
#          the 'zone' attribute in the 'timezone.xml' file. Invocation of the
#          'getParams' has been changed.
#

=pod

=head1 NAME

B<datetimeconfig> - set system date and time.

=head1 SYNOPSIS

B<datetimeconfig> [B<--show>]|[B<--help>]

B<datetimeconfig> [B<-mon> I<< <1-12> >>] [B<-year> I<< <year> >>] [B<-day>
I<< <1-31> >>] [B<-hour> I<< <0-23> >>] [B<-min> I<< <0-59> >>] [B<-timezone> I<< <abbreviation> >>]

=head1 DESCRIPTION

B<datetimeconfig> configures the system date and time in command line or interactive mode.
The latter mode will be launched if there are no CLI arguments provided.

=head1 OPTIONS

=over 4

=item B<--show>

Print the current Timezone, Date and Time configuration.

=item B<-mon month>

Specify the current month. The month must have the numeric format (value is
between 1 and 12)

=item B<-year year>

Specify the current year. The year is later than 1990.

=item B<-day E<lt>1-31E<gt>>

Specify the current day. The day must have the numeric format (value is
between 1 and 31)

=item B<-hour hour>

Specify the current hour. The year is between 0 and 23.

=item B<-min minutes>

Specify the currect minutes (between 0 and 59).

=item B<-timezone abbreviation>

Specify the desired timezone.  Refer to Nortel NTP documentation for more information. 

=item B<--help>

Print this help.

=back

=head1 CAVEATS

B<datetimeconfig> can be run only under account from the timeadmin group.

=head1 SEE ALSO

B<ntpconfig>, B<timeadj>

=cut

use lib "/opt/nortel/base/lib";

use warnings;
use strict;
use Getopt::Long;

use common_variables;
use common_functions;
use baseParams;
use baseParamsUI;
use baselog;

sub main();

my $BASE_YEAR = 1900;

my %data = (
            'mon'      => 0,
            'day'      => 0,
            'year'     => $BASE_YEAR,
            'hour'     => 0,
            'min'      => 0,
            'timezone' => ""
           );

# Hash with refs to check function
my %check_data = (
                  'mon'      => \&check_mon,
                  'day'      => \&check_day,
                  'year'     => \&check_year,
                  'hour'     => \&check_hour,
                  'min'      => \&check_min,
                  'timezone' => \&check_timezone
                 );

exit(main());

## @fn sub main()
# @brief Main entry point of the command
# @param
# @return $OK - date, time modified successfully.
# @return $ERROR - Errors occured.
sub main()
{
   if (defined $ARGV[0] and $ARGV[0] =~ /help$/)
   {
      print_pod_usage();
      return $OK;
   }

   my $usrData = {};
   if (!defined(getParams($usrData)))
   {
      baselog($LOG_ERROR, "Cannot get current timezone\n", $TRUE);
      return $ERROR;
   }
   my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();

   if ((scalar(@ARGV) == 1) && ($ARGV[0] eq "--show"))
   {
      my $tzdescr = getTimezoneDescr(getTimezonesInfo({'zone' => $usrData->{'timezone'}}));
      printf "\nTimezone, Date and Time configuration: \n\n";
      printf "     Current Timezone: $tzdescr\n";
      printf "         Current Date: %d/%d/%d\n", $mon + 1, $mday, $year + $BASE_YEAR;
      printf "         Current Time: %02d:%02d:%02d\n", $hour, $min, $sec;
      return $OK;
   } ## end if ((scalar(@ARGV) == ...

   # Set default values
   $data{'mon'}      = $mon + 1;
   $data{'day'}      = $mday;
   $data{'year'}     = $BASE_YEAR + $year;
   $data{'hour'}     = $hour;
   $data{'min'}      = $min;
   $data{'timezone'} = $usrData->{'timezone'};

   # If there are any arguments then try to extract date from arguments
   if (scalar(@ARGV) > 0)
   {
      my $i = 0;
      while (defined($ARGV[$i]))
      {
         if ($ARGV[$i] !~ /^\-\w+$/)
         {
            print_pod_usage("Incorrect option $ARGV[$i] is provided");
            return $ERROR;
         }
         $i += 2;
      } ## end while (defined($ARGV[$i])...
      if (!GetOptions(\%data, 'mon=i', 'day=i', 'year=i', 'min=i', 'hour=i', 'timezone=s'))
      {
         print_pod_usage();
         return $ERROR;
      }

      # Check values
      foreach my $key (keys(%data))
      {
         if (!$check_data{$key}->($data{$key}))
         {
            print "Incorrect value for parameter '$key'\n";
            return $ERROR;
         }
      } ## end foreach my $key (keys(%data...

      # Set new date and time
      my $date = sprintf "%02d%02d%02d%02d%4d", $data{'mon'}, $data{'day'}, $data{'hour'}, $data{'min'}, $data{'year'};
      $usrData->{'timezone'} = $data{'timezone'};

      signals_ignore();
      if (setupTimeZone(\%$usrData))
      {
         baselog($LOG_INFO, "TimeZone has been setup successfully\n", $TRUE);
      }
      else
      {
         baselog($LOG_ERROR, "Timezone setup failed\n", $TRUE);
      }

      unless (setupDateTime($date))
      {
         baselog($LOG_ERROR, "Date set failed\n", $TRUE);
         return $ERROR;
      }
      signals_default();
   } ## end if (scalar(@ARGV) > 0)
   else
   {
      signals_ignore();

      # Prompt user for new date and time
      configTimeZone($usrData);
      configDateTime();

      if (setupTimeZone($usrData))
      {
         baselog($LOG_INFO, "TimeZone has been setup successfully\n", $TRUE);
      }
      else
      {
         baselog($LOG_ERROR, "Timezone setup failed\n", $TRUE);
      }
      signals_default();

   } ## end else [ if (scalar(@ARGV) > 0)
   my $d = qx/date/;
   print "System time and date have been changed to $d";
   return $OK;
} ## end sub main()

# Set of check functions
# All of them accept parameter value and return $TRUE or $FALSE
## @fn check_mon($)
# @brief Check the input value for the month
# @param $mon - number defining the month
# @return $TRUE - Valid.
# @return $FALSE - Not Valid.
sub check_mon($)
{
   my $mon = shift(@_);
   return (($mon < 1 or $mon > 12) ? $FALSE : $TRUE);
}

## @fn check_day($)
# @brief Check the input value for the day
# @param $day - number defining the day
# @return $TRUE - Valid.
# @return $FALSE - Not Valid.
sub check_day($)
{
   my $day = shift(@_);
   return (($day < 1 or $day > 31) ? $FALSE : $TRUE);
}

## @fn check_year($)
# @brief Check the input value for the year
# @param $year - number defining the year
# @return $TRUE - Valid.
# @return $FALSE - Not Valid.
sub check_year($)
{
   my $year = shift(@_);
   if (($year < $MIN_YEAR) or ($year > $MAX_YEAR))
   {
      baselog($LOG_ERROR, "The YEAR value must be between $MIN_YEAR and $MAX_YEAR.", $TRUE);
      return $FALSE;
   }
   return $TRUE;
} ## end sub check_year($)

## @fn check_hour($)
# @brief Check the input value for the hour
# @param $hour - number defining the hour
# @return $TRUE - Valid.
# @return $FALSE - Not Valid.
sub check_hour($)
{
   my $hour = shift(@_);
   return (($hour < 0 or $hour > 23) ? $FALSE : $TRUE);
}

## @fn check_min($)
# @brief Check the input value for the minute
# @param $min - number defining the minute
# @return $TRUE - Valid.
# @return $FALSE - Not Valid.
sub check_min($)
{
   my $min = shift(@_);
   return (($min < 0 or $min > 59) ? $FALSE : $TRUE);
}

## @fn check_timezone($)
# @brief Check the input value for the timezone
# @param $timezone - string defining the timezone
# @return $TRUE - Valid.
# @return $FALSE - Not Valid.
sub check_timezone($)
{
   my $timezone = shift(@_);
   return validateTimeZone($timezone);
}

__END__
