#!/usr/bin/perl

# NOTE: this script is used by generate-new-scores; it is meant to be copied
#       to and called from the masses/ directory of the checkout being used
#       for the score generation run for the particular scoreset; you
#       shouldn't need to call this script manually
#
# locks the score ranges for the base release rules to their original scores
# from 50_scores.cf
#
# if called with a 1 parameter new rules that aren't in the most current copy
# of the active.list file will be locked to zero so that the GA can ignore
# rules that aren't in the most current update (this is used for zeroing rules
# found in the weekly net checks that are no longer in the nightly non-net
# checks which 6 of 7 updates a week are based on)
#
# <@LICENSE>
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# </@LICENSE>

use strict;
use warnings;

my $scoreset = 0; # default

my %rulescores;
my %currently_active;

my $only_currently_active_rules = (defined $ARGV[0] && $ARGV[0] == 1 ? 1 : 0);

open(CONFIG, "config") or die "Cannot open config file: $!";
while (<CONFIG>) {
  /^\s*SCORESET=(\d)\s*$/;
  $scoreset = $1;
  # don't exit loop in case scoreset appears in config again
}
close CONFIG;

print "Fixing score range for existing rules to current scoreset $scoreset score\n";

open(ORIG, "../rules/50_scores.cf") or die "Cannot open original score file: $!";
while(<ORIG>) {
  if (/^score/) {
    /^score\s+(\S+)\s+(-?[\d.]+)(?:\s+(-?[\d.]+)\s+(-?[\d.]+)\s+(-?[\d.]+))?/;
    my @scores;
    if (defined $3) {
      push @scores, ($2, $3, $4, $5);
    } else {
      push @scores, ($2, $2, $2, $2);
    }
    $rulescores{$1} = $scores[$scoreset];
  }
}
close ORIG;

if ($only_currently_active_rules) {
  open(ACTIVE, "../rules-current/active.list") or die "Cannot open rules-current/active.list: $!";
  while(<ACTIVE>) {
    $currently_active{$1} = undef if (/^(?!#)(\S+)$/);
  }
  close ACTIVE;
}

open(ORIG, "tmp/ranges.data") or die "Cannot open original range.data file: $!";
open(NEW, ">tmp/ranges.data-new") or die "Cannot open range.data-new file: $!";
while (<ORIG>) {
  if (/^(?:(?:-?[\d.]+) ){3}(\S+)$/) {
    if (defined $rulescores{$1}) {
      print NEW "$rulescores{$1} $rulescores{$1} 0 $1\n";
    } else {
      if ($only_currently_active_rules) {
        if (exists $currently_active{$1}) {
          print NEW $_;
        } else {
          print NEW "0 0 0 $1\n";
        }
      } else {
        print NEW $_;
      }
    }
  } else {
    print NEW $_;
  }
}
close ORIG;
close NEW;
