#-----------------------------------------------------------------
#
# File: QAR_OPT.pm
#
# Copyright 2009 Programming Research Ltd
#
# Version: 1.2
#
# Written: C Waddingham, 30-October-2009
#
# Purpose: 
#
#        Where the po options are:
#             -po qar::title=<document title for front page>
#             -po qar::project_name=<project name for front page>
#             -po qar::date_type=UK
#             -po qar::date_type=US
#             -po qar::logo_file=<path of file>
#             -po qar::no_appendix
#             -po qar::no_version
#             -po qar::output_path=<path for result files>
#             -po qar::op=<path for result files>
#             -po qar::report_format=<html|xhtml>
#             -po qar::report_type=<Code Review Report/Warning Listing/..>
#             -po "qar::report_type=Code Review Report" 
#             -po "qar::report_type=Compliance Report"  
#             -po "qar::report_type=General Report"
#             -po "qar::report_type=Quality Report" 
#             -po "qar::report_type=Suppression Report"
#             -po qar::user_reports_dir=<path of folder containing report programs>
#             -po qar::user_reports_name=<Users name for user reports folder>
#             -po qar::ud=<path of folder>
#             -po qar::viewing_program=<path of program>
#             -po qar::prdf2target_xsl_file=<path of file>
#
#-----------------------------------------------------------------

#-- package QAR_OPT: ---------------------------------------------

package QAR_OPT;

BEGIN
{
   require QAR_UTIL;
   require QAR_CMAF_DIR;

   use strict;
   use integer;

   our $qar = "qar";

   our $op = "";    # This is the QAC/PP -op setting as opposed to the -po setting
   our $lof_file = "";
   our $settings_file = "";

   our $hr_options_values = 
      { "date_type"            => "UK",
        "title"                => "",
        "project_name"         => "",
        "logo_file"            => "",
        "no_appendix"          => "false()",
        "no_version"           => "false()",
        "output_path"          => "",
        "op"                   => "",
        "report_format"        => "",
        "report_type"          => "", 
        "user_reports_dir"     => "",
        "user_reports_name"    => "standard",
        "ud"                   => "",
        "viewing_program"      => "",
        "prdf2target_xsl_file" => "",
      };

   our $platform_is_windows = $^O =~ /^MSWin/o;
}

#--------------------------------------------------------

sub SET_REPORT_TYPE
{
   # Usage: SET_REPORT_TYPE( $report_type );
   #
   $$QAR_OPT::hr_options_values{ "report_type" } = shift;
}

#--------------------------------------------------------

sub PRDF2TARGET_XSL_FILE
{
   # Usage: $prdf2target_xsl_file = PRDF2TARGET_XSL_FILE()
   #

   if ( $$QAR_OPT::hr_options_values{ "prdf2target_xsl_file" } eq "" )
   {
      return QAR_CMAF_DIR::TRANSFORMS_DIR() .
             "prdf2" . 
             REPORT_FORMAT_REQUIRED() .
             ".xsl";
   }
   else
   {
      return $$QAR_OPT::hr_options_values{ "prdf2target_xsl_file" };
   }
}

# ----------------------------------------------------------------

sub SET_USER_REPORTS_DIR
{
   # Usage: SET_USER_REPORTS_DIR( $path );
   #
   $$QAR_OPT::hr_options_values{ "user_reports_dir" } = shift;
}

#-------------------------------------------------------------------

sub TITLE
{
   # Usage: $dir = TITLE();
   #
   return $$QAR_OPT::hr_options_values{ "title" };
}

#--------------------------------------------------------

sub SET_OUTPUT_PATH
{
   # Usage: SET_OUTPUT_PATH( $output_path );
   #
   $$QAR_OPT::hr_options_values{ "output_path" } = shift;
}

#-------------------------------------------------------------------

sub REPORT_FORMAT
{
   # Usage: $report_format = REPORT_FORMAT();
   #
   return $$QAR_OPT::hr_options_values{ "report_format" };
}

#--------------------------------------------------------

sub REPORT_FORMAT_REQUIRED
{
   # Usage: $report_format = REPORT_FORMAT_REQUIRED();
   #
   if ( ! exists( $$QAR_OPT::hr_options_values{ "report_format" } ) ||
        ( $$QAR_OPT::hr_options_values{ "report_format" } eq "" )
      )
   {
      # Try to deduce REPORT_FORMAT from VIEWING_PROGRAM

      if ( VIEWING_PROGRAM() eq "" )
      {
         # Try and get default VIEWING_PROGRAM
         #
   	 if ( $QAR_OPT::platform_is_windows )
   	 {
   	    my $browser_command = QAR_UTIL::LOOKUP_DEFAULT_BROWSER_IN_REGISTRY();
   
   	    if ( $browser_command ne "" )
   	    {
   	       # Now have browser program and any arguments.
   	       # Get the first term i.e. the browser program.
   	       #
   	       my @args = QAR_UTIL::SPLIT_STRING_ALLOWING_SPECIFIC_QUOTED_TERMS
   			     ( " \t", $browser_command, "\"" );
   	 
   	       if ( $#args > -1 )
   	       {
   		  SET_VIEWING_PROGRAM( $args[ 0 ] );
   	       }
   	    }
   	 }
      }

      if ( VIEWING_PROGRAM() ne "" )
      {
   	 # Give warning if viewing program is not compatible with report_format
   	 #
   	 if ( $QAR_OPT::platform_is_windows )
   	 {
   	    my $brow_prog = QAR_UTIL::BASE_NAME( VIEWING_PROGRAM() );

   	    if ( ( lc( $brow_prog ) eq "iexplore.exe" ) ||
   		 ( lc( $brow_prog ) eq "iexplore" )
   	       )
   	    {
   	       # Here with IE browser
   	       # This browser requires graphics to be in VML.
   	       #
               SET_REPORT_FORMAT( "html" );
   	    }
   	    else
   	    {
   	       # Here with any browser except IE, e.g. firefox
   	       # These browsers require graphics to be in SVG.
   	       #
               SET_REPORT_FORMAT( "xhtml" );
   	    }
   	 }
      }

      if ( ! exists( $$QAR_OPT::hr_options_values{ "report_format" } ) ||
   	   ( $$QAR_OPT::hr_options_values{ "report_format" } eq "" )
   	 )
      {
         SET_REPORT_FORMAT( QAR_UTIL::INPUT( "Enter report_format ( html | xhtml ): " ) );
      }
   }

   return $$QAR_OPT::hr_options_values{ "report_format" };
}

#--------------------------------------------------------

sub SET_REPORT_FORMAT
{
   # Usage: SET_REPORT_FORMAT( $report_format );
   #
   $$QAR_OPT::hr_options_values{ "report_format" } = shift;
}

#--------------------------------------------------------

sub SET_VIEWING_PROGRAM
{
   # Usage: SET_VIEWING_PROGRAM( $viewing_program );
   #
    $$QAR_OPT::hr_options_values{ "viewing_program" } = shift;
}

#-------------------------------------------------------------------

sub OUTPUT_PATH
{
   # Usage: $output_path = OUTPUT_PATH();
   #
   return QAR_UTIL::APPLY_FINAL_SEP
            ( QAR_UTIL::GET_FILE_PATH_ABSOLUTE
                ( $$QAR_OPT::hr_options_values{ "output_path" } )
            );
}

#-------------------------------------------------------------------

sub VIEWING_PROGRAM
{
   # Usage: $viewing_program = VIEWING_PROGRAM();
   #
   return $$QAR_OPT::hr_options_values{ "viewing_program" };
}

#-------------------------------------------------------------------

sub REPORT_TYPE
{
   # Usage: $report_type = REPORT_TYPE();
   #
   return $$QAR_OPT::hr_options_values{ "report_type" };
}

#-------------------------------------------------------------------

sub DATE_TYPE
{
   # Usage: $date_type = DATE_TYPE();
   #
   return $$QAR_OPT::hr_options_values{ "date_type" };
}

#-------------------------------------------------------------------

sub NO_VERSION
{
   # Usage: $no_version = NO_VERSION();
   #
   return $$QAR_OPT::hr_options_values{ "no_version" };
}

#-------------------------------------------------------------------

sub NO_APPENDIX
{
   # Usage: $no_appendix = NO_APPENDIX();
   #
   return $$QAR_OPT::hr_options_values{ "no_appendix" };
}

#-------------------------------------------------------------------

sub PROJECT_NAME
{
   # Usage: $project_name = PROJECT_NAME();
   #
   return $$QAR_OPT::hr_options_values{ "project_name" };
}

#-------------------------------------------------------------------

sub LOGO_FILE
{
   # Usage: $logo_file = LOGO_FILE();
   #
   return $$QAR_OPT::hr_options_values{ "logo_file" };
}

#-------------------------------------------------------------------

sub USER_REPORTS_DIR
{
   # Usage: $dir = USER_REPORTS_DIR();
   #
   return QAR_UTIL::APPLY_FINAL_SEP( $$QAR_OPT::hr_options_values{ "user_reports_dir" } );
}

#--------------------------------------------------------

sub PARSE_COMMAND_LINE
{
   # Usage: PARSE_COMMAND_LINE( @ARGV );
   # Sets set of global vars
   #
   my @args = @_;

   return "" if ( $#args == -1 );

   my $ar_parsed_options = [ "-op", "-outputpath", "-cmaf", "-via", "-list", "-po" ];
   
   my $iarg = 0;

   ARG:
   while ( $iarg <= $#args )
   {
      my $arg = $args[ $iarg ];

      next ARG if ( $arg =~ /^\s*\*/o );
      
      my $param = "";
      my $lcarg = lc( $arg );

      OPT:
      foreach my $opt ( @$ar_parsed_options )
      {
         if ( $lcarg eq $opt )
         {
            QAR_UTIL::DIEX( "Error $arg supplied as last option.\n",
                  $QAR_UTIL::die_config_error
                ) if ( $iarg >= $#args );

            $param = $args[ $iarg + 1 ];
            ++$iarg;

            last OPT;
         }
         elsif ( $lcarg =~ /^$opt/ )
         {
            $param = substr( $arg, length( $opt ) );

            last OPT;
         }
      }

      if ( $param ne "" )
      {
         if ( $lcarg =~ /^-op/o )
         {
             $QAR_OPT::op = QAR_UTIL::APPLY_FINAL_SEP( $param );
         }
         if ( $lcarg =~ /^-outputpath/o )
         {
             $QAR_OPT::op = QAR_UTIL::APPLY_FINAL_SEP( $param );
         }
         if ( $lcarg =~ /^-cmaf/o )
         {
            QAR_CMAF_DIR::SET_CMAF_DIR( $param );
         }
         elsif ( $lcarg =~ /^-via/o )
         {
            $QAR_OPT::settings_file = $param;
            PARSE_VIA_FILE( $param );
         }
         elsif ( $lcarg =~ /^-list/o )
         {
            $QAR_OPT::lof_file = $param;
            PARSE_VIA_FILE( $param );
         }
         elsif ( $lcarg =~ /^-po/o )
         {
            PARSE_PO_PARAM( $param );
         }
      }
   }
   continue
   {
      ++$iarg;
   }

   return $args[ 0 ];
}   

# ----------------------------------------------------------------

sub PARSE_VIA_FILE
{
   # Usage: PARSE_VIA_FILE( $filename );
   #
   my $filename = shift;

   my @lines = QAR_UTIL::LOAD_FILE( $filename );

   LINE:
   foreach my $line ( @lines )
   {
      next LINE if ( $line =~ /^\s*\*/o );
      next LINE if ( $line =~ /^\s*$/o );

      my $quote_char;
      if ( $QAR_OPT::platform_is_windows )
      {
         $quote_char = "\"";
      }
      else
      {
         $quote_char = "'";
      }

      my @args = QAR_UTIL::SPLIT_STRING_ALLOWING_SPECIFIC_QUOTED_TERMS( " \t", $line, $quote_char );

      # Here with @args is a line of a -via or -list file.
      # Validate that any non-option, ie file, has both .err and .met for the current
      # -op setting.
      #
      VALIDATE_ERR_AND_MET( @args );

      PARSE_COMMAND_LINE( @args );
   }
}

# ----------------------------------------------------------------

sub VALIDATE_ERR_AND_MET
{
   # Usage: VALIDATE_ERR_AND_MET( @args );
   #
   my @args = @_;

   if ( $#args == -1 )
   {
      return;
   }

   if ( $args[ 0 ] !~ /^\-/o )
   {
      # Here with non-option ie file(s)
      #
      foreach my $file ( @args )
      {
         my $full_file = $QAR_OPT::op . QAR_UTIL::BASE_NAME( $file );

         my $err_file = $full_file . ".err";

         if ( ! -e $err_file )
         {
   	    QAR_UTIL::DIEX( "Error $err_file not found.\n",
   		  $QAR_UTIL::die_config_error
   		);
         }
         elsif ( -s $err_file == 0 )
         {
   	    QAR_UTIL::DIEX( "Error $err_file empty.\n",
   		  $QAR_UTIL::die_config_error
   		);
         }

         my $met_file = $full_file . ".met";

         if ( ! -e $met_file )
         {
   	    QAR_UTIL::DIEX( "Error $met_file not found.\n",
   		  $QAR_UTIL::die_config_error
   		);
         }
         elsif ( -s $met_file == 0 )
         {
   	    QAR_UTIL::DIEX( "Error $met_file empty.\n",
   		  $QAR_UTIL::die_config_error
   		);
         }
      }
   }
}

# ----------------------------------------------------------------

sub PARSE_PO_PARAM
{
   # Usage: PARSE_PO_PARAM( $param );
   # Sets set of global vars
   #
   my $param = shift;

   $param = QAR_UTIL::TRIM( $param );
 
   if ( $param =~ /^$QAR_OPT::qar\:\:no_appendix$/i )
   {
      $$QAR_OPT::hr_options_values{ "no_appendix" } = "true()";

      return;
   }
   elsif ( $param =~ /^$QAR_OPT::qar\:\:no_version$/i )
   {
      $$QAR_OPT::hr_options_values{ "no_version" } = "true()";

      return;
   }
   elsif ( $param =~ /^$QAR_OPT::qar\:\:(ud|user_reports_dir)=(.*)/i )
   {
      SET_USER_REPORTS_DIR( $2 );

      return;
   }
   elsif ( $param =~ /^$QAR_OPT::qar\:\:op=(.*)/i )
   {
      SET_OUTPUT_PATH( $1 );

      return;
   }
   elsif ( $param =~ /^$QAR_OPT::qar\:\:user_reports_name=(.*)/i )
   {
      my $urn = $1;

      my $path = LOOKUP_USER_REPORT( $urn );

      if ( ( $path ne "" ) && 
           ( QAR_UTIL::DIR_EXISTS( $path ) )
         )
      {
         SET_USER_REPORTS_DIR( $path );
      }
      else
      {
         QAR_UTIL::DIEX( "-po $QAR_OPT::qar\:\:user_reports_name=$urn does not exist\n",
               $QAR_UTIL::die_config_error
             );
      }

      return;
   }
   else
   {
      KEY:
      foreach my $key ( keys %$QAR_OPT::hr_options_values )
      {
         if ( $param =~ /^$QAR_OPT::qar\:\:$key\=?(.*)/i )
         { 
            $$QAR_OPT::hr_options_values{ $key } = $1;

            return;
         }
      }
   }

   QAR_UTIL::DIEX( "Unknown option -po $param\n", $QAR_UTIL::die_program_termination )
      if ( $param =~ /^$QAR_OPT::qar\:\:/i );
}

# ----------------------------------------------------------------

sub LOOKUP_USER_REPORT
{
   # Usage: $user_report_dir = LOOKUP_USSER_REPORT( $user_report_name );
   #
   my $user_report_name = shift;

   my %folders = GET_REPORT_FOLDER_NAMES_FROM_PROPERTY_FILE();

   if ( exists( $folders{ $user_report_name } ) )
   {
      return $folders{ $user_report_name };
   }

   return "";
}
   
# ----------------------------------------------------------------

sub GET_REPORT_FOLDER_NAMES_FROM_PROPERTY_FILE
{ 
   # Usage: %report_folder_names = 
   #    GET_REPORT_FOLDER_NAMES_FROM_PROPERTY_FILE();
   #

   if ( ! -e QAR_PROPERTIES_FILEPATH() )
   {
      return ();
   }

   my @props = QAR_UTIL::LOAD_FILE( QAR_PROPERTIES_FILEPATH() );

   # Structure is:
   # * logical viewer : format : file
   #
   # * alternate report folder name : path
   #
   # e.g.
   # IP (HTML):html:C:\Program Files\Internet Explorer\Iexplore.exe
   # 
   # my:C:\Program files\PRQA\QAR\user_reports

   my %report_folder_names = ();

   my $flag = "Doing viewers";

   LINE:
   foreach my $line ( @props )
   {
      $line =~ s/[\n\r]+$//o;

      if ( $line =~ /^\s*$/o )
      {
         $flag = "Doing folders";
     
         next LINE;
      }

      if ( $flag eq "Doing folders" )
      {
         my ( $name, $arg ) = split( ":", $line, 2 );
         if ( defined( $name ) &&
              defined( $arg ) &&
              ( $name ne "" ) &&
              ( $arg ne "" )
            )
         {
            $report_folder_names{ $name } = $arg;
         }
      }
   }

   return %report_folder_names;
}

#-- eof QAR_OPT.pm -------------------------------------------------------

1
