#!/usr/bin/perl
##@file  cron_backup_scheduler
# @brief This script manage cron's scheduler for backup task
# @details
# @author Kate Bereznyakova
# @param $ARGV[0]   - key description of operation type:
#                        -c - add backup task to cron
#                        -r - remove backup task from cron
#                        -s - print crontab task
# @param $ARGV[1]   - configuration script path (for add task type)
#                     this path will be used as parameter for backup task
# @date
# @version 1.20 - printCrontabTask function is implemented. New option '-s' support is added.
# @version 1.11 - Two colons were removed before shared functions and variables.
# @version 1.10 - usage of new base_crontab API
# @version 1.00 - Initial version
# @note

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

use common_variables;
use common_functions;
use base_crontab;
use recovery::backup;
use baselog;
use CommonInstallUtils;

use strict;
use warnings;

my $CRONTAB_OWNER = $ROOT_ACCT;
my $BACKUP_OWNER  = $NORTEL_ACCT;

sub main();
sub addTask($);
sub printCrontabTask($);

exit(main());

sub main()
{
   my $res = $OK;

   if (warning_if_not_root_uid())
   {
      return $ERROR;
   }

   if (defined(@ARGV))
   {
      if (($ARGV[0] eq "-c") && ($ARGV[1] ne ""))
      {
         if (not addTask($ARGV[1]))
         {
            baselog($LOG_ERROR, "Cannot add the task to the schedule table.\n", $TRUE);
            $res = $ERROR;
         }
      } ## end if (($ARGV[0] eq "-c")...
      elsif ($ARGV[0] eq "-r")
      {
         if (not removeCrontabTask($CRONTAB_OWNER, $backup_command))
         {
            baselog($LOG_ERROR, "Cannot remove the task from the schedule table.\n", $TRUE);
            $res = $ERROR;
         }
      } ## end elsif ($ARGV[0] eq "-r")
      elsif ($ARGV[0] eq "-s")
      {
         my $crontabTask = getCrontabTask($CRONTAB_OWNER, $backup_command);
         if (not defined $crontabTask)
         {
            baselog($LOG_ERROR, "Cannot get the task from the schedule table.\n", $TRUE);
            $res = $ERROR;
         }
         elsif (not scalar @$crontabTask)
         {
            print "Backup was not scheduled.\n";
         }
         else
         {
            printCrontabTask($crontabTask);
         }
      } ## end elsif ($ARGV[0] eq "-s")
      else
      {
         baselog($LOG_ERROR, "Incorrect parameter provided.\n", $TRUE);
         $res = $ERROR;
      }
   } ## end if (defined(@ARGV))
   else
   {
      baselog($LOG_ERROR, "Insufficient number of parameters provided.\n", $TRUE);
      $res = $ERROR;
   }

   return $res;
} ## end sub main()

## @fn addTask($ConfigFilePath)
# @brief Add backup task in cron's scheduler - allow to define exactly time
# @details
# @param $ConfigFilePath - path to the configuration script
# @return none
# @author
# @date
# @note
sub addTask($)
{
   my $ConfigFilePath = shift @_;

   #remove the previous scheduled backup task.
   if (not removeCrontabTask($CRONTAB_OWNER, $backup_command))
   {
      baselog($LOG_ERROR, "Cannot remove the old task from the schedule table prior to add a new one.\n", $TRUE);
      return $FALSE;
   }

   my $task_cmd  = qq|$RUNUSER - $BACKUP_OWNER -c "$backupScriptPath $ConfigFilePath"|;
   my $task_time = "";

   # default backup time - every Sunday's midnight
   if (getYNResponse("Do you want to use default backup time (midnight Sunday)", 'Y'))
   {
      $task_time = '0 0 * * 7';
   }
   else
   {  # do not use default backup time
      print "The value in the brackets is the default one.";
      print " Use asterisk if you are going not to use this parameter.\n\n";
      my $minute = getTimeValue(0, 0, "Enter the minutes (0-59)", 0, 59, "0");
      my $hour   = getTimeValue(0, 0, "Enter the hour (0-23)",    0, 23, "*");

      # day of month
      my $dom   = getTimeValue(1, 0, "Enter day of month (1-31)", 1, 31, "*");
      my $month = getTimeValue(1, 0, "Enter month (1-12)",        1, 12, "*");

      # day of week
      my $dow = getTimeValue(7, 0, "Enter day of week (1-7)", 1, 7, "*");

      $task_time = "$minute $hour $dom $month $dow";

      baselog($LOG_DEBUG, "schedule time '$task_time'");
   } ## end else [ if (getYNResponse("Do you want to use default backup time (midnight Sunday)"...

   return addCrontabTask($CRONTAB_OWNER, $task_time, $task_cmd);
} ## end sub addTask($)

## @fn printCrontabTask($crontab)
# @brief This subroutine prints crontab task
# @details Receives reference to array and prints contained hash(es) in the 
#           readable format. Crontab task(s) stores in hash(es).
# @param $crontab - Reference to array contains reference(s) to cron task hash(es).
# @return none
# @author Ilya Kudelin
# @date
# @note
sub printCrontabTask($)
{
   my $crontab = shift @_;
   foreach my $task (@$crontab)
   {
      print "Backup configured:\n";
      print "\tMinute: " . $task->{"min"} . "\n";
      print "\tHour: " . $task->{"hour"} . "\n";
      print "\tMonth: " . $task->{"month"} . "\n";
      print "\tDay of month: " . $task->{"dom"} . "\n";
      print "\tDay of week: " . $task->{"dow"} . "\n";
      print "Star symbol(*) means any number.\n";
   } ## end foreach my $task (@$crontab...
} ## end sub printCrontabTask($)
