<commit_before>#! /usr/bin/perl -w

use strict;
( my $progname = $0 ) =~ s#^.*/##;

sub find_from_table($);
sub find_in_dir($$);
sub search_and_exit;
sub show_table($);

if ( "$ARGV[0]" eq "--help" ){
  print <<END;
This script find a script from two keywords.
Usage: $progname word1 word2
First we check user table and search in CSGSCRIPTDIR and than in CSGSHARE
Then we check csg table and search in CSGSHARE

Hacky usage: $progname --direct scriptname
then we try to find scriptname in global,pwd,CSGSCRIPTDIR,CSGSHARE
END
  exit 0;
}

my $csg_table="csg_table";
my $user_table="csg_table";

(my $csgshare=$ENV{'CSGSHARE'}) || die "CSGSHARE not defined\n";
(my $csgscriptdir=$ENV{'CSGSCRIPTDIR'}) ||die "CSGSCRIPTDIR not defined\n";

$csg_table="$csgshare/$csg_table";
$user_table="$csgscriptdir/$user_table";

my $scriptname=undef;

if ("$ARGV[0]" eq "--status" ){
  print "csg table status\n";
  show_table($csg_table);
  print "Check sums\n";
  system('md5sum $CSGSHARE/MD5SUM');
  system('cd $CSGSHARE; md5sum -c $CSGSHARE/MD5SUM || echo WARNING: You have modified csg scripts, better copy them and to user scripts dir');
  print "user table status\n";
  ( -r "$user_table" ) && show_table($user_table);
  exit 0;
}

###################MAIN PROGRAMM#######################

( $#ARGV < 1 ) && die "$progname needs two arguments\n";

#--direct options
if ( "$ARGV[0]" eq "--direct" ) {
  search_and_exit($ARGV[1]," ",$ENV{'PWD'},$csgscriptdir,$csgshare);
  die "Could find script $ARGV[1]\n";
}

#find script in user_table
if (( -r "$user_table" ) && ($scriptname=find_from_table($user_table))) {
  #first script dir, then csgshare
  search_and_exit($scriptname,$csgscriptdir,$csgshare);
}

#search in csg_table
( $scriptname=find_from_table($csg_table) ) || die "Could not find script matching '$ARGV[0] $ARGV[1]'\n";
search_and_exit($scriptname,$csgshare) || die "Could find script '$scriptname'\n";

######################FUNCTIONS##########################
sub find_from_table($) {
  my $found=undef;
  open(FILE,"<$_[0]") || die "Could not read $_[0]\n";
  while (<FILE>) {
    next if /^#/;
    next if /^\s*$/;
    $found="$1" if /^\s*$ARGV[0]\s+$ARGV[1]\s+(.*?)$/;
  }
  return $found;
}

sub find_in_dir($$) {
  my $args="";
  ( my $script=$_[0] ) || die "find_in_dir: first argument missing\n";
  ( my $dir=$_[1] ) || die "find_in_dir: second argument missing\n";
  #remove script arguments
  $script="$1" if  ($_[0] =~ s/^(\S+).*?$/$1/);
  $args="$1" if ($_[0] =~ /^\S+\s+(.*?)$/); 
  #empty means, add no dir<commit_msg>source_wrapper.pl end if user script was not found<commit_after>#! /usr/bin/perl -w

use strict;
( my $progname = $0 ) =~ s#^.*/##;

sub find_from_table($);
sub find_in_dir($$);
sub search_and_exit;
sub show_table($);

if ( "$ARGV[0]" eq "--help" ){
  print <<END;
This script find a script from two keywords.
Usage: $progname word1 word2
First we check user table and search in CSGSCRIPTDIR and than in CSGSHARE
Then we check csg table and search in CSGSHARE

Hacky usage: $progname --direct scriptname
then we try to find scriptname in global,pwd,CSGSCRIPTDIR,CSGSHARE
END
  exit 0;
}

my $csg_table="csg_table";
my $user_table="csg_table";

(my $csgshare=$ENV{'CSGSHARE'}) || die "CSGSHARE not defined\n";
(my $csgscriptdir=$ENV{'CSGSCRIPTDIR'}) ||die "CSGSCRIPTDIR not defined\n";

$csg_table="$csgshare/$csg_table";
$user_table="$csgscriptdir/$user_table";

my $scriptname=undef;

if ("$ARGV[0]" eq "--status" ){
  print "csg table status\n";
  show_table($csg_table);
  print "Check sums\n";
  system('md5sum $CSGSHARE/MD5SUM');
  system('cd $CSGSHARE; md5sum -c $CSGSHARE/MD5SUM || echo WARNING: You have modified csg scripts, better copy them and to user scripts dir');
  print "user table status\n";
  ( -r "$user_table" ) && show_table($user_table);
  exit 0;
}

###################MAIN PROGRAMM#######################

( $#ARGV < 1 ) && die "$progname needs two arguments\n";

#--direct options
if ( "$ARGV[0]" eq "--direct" ) {
  search_and_exit($ARGV[1]," ",$ENV{'PWD'},$csgscriptdir,$csgshare);
  die "Could find script $ARGV[1]\n";
}

#find script in user_table
if (( -r "$user_table" ) && ($scriptname=find_from_table($user_table))) {
  #first script dir, then csgshare
  search_and_exit($scriptname,$csgscriptdir,$csgshare);
  die "Could not find user script '$scriptname' in any dir, check for typos\n";
}

#search in csg_table
( $scriptname=find_from_table($csg_table) ) || die "Could not find script matching '$ARGV[0] $ARGV[1]'\n";
search_and_exit($scriptname,$csgshare) || die "Could not find script '$scriptname' in any dir\n";

######################FUNCTIONS##########################
sub find_from_table($) {
  my $found=undef;
  open(FILE,"<$_[0]") || die "Could not read $_[0]\n";
  while (<FILE>) {
    next if /^#/;
    next if /^\s*$/;
    $found="$1" if /^\s*$ARGV[0]\s+$ARGV[1]\s+(.*?)$/;
  }
  return $found;
}

sub find_in_dir($$) {
  my $args="";
  ( my $script=$_[0] ) || die "find_in_dir: first argument missing\n";
  ( my $dir=$_[1] ) || die "find_in_dir: second argument missing\n";
  #remove script arguments
  $script="$1" if  ($_[0] =~ s/^(\S+).*?$/$1/);
  $args="$1" if ($_[0] =~ /^\S+\s+(.*?)$/); 
  #empty means, add no dir
{'fraction_non_alphanumeric': 0.2184018440261237, 'fraction_numerical': 0.01114099116404149, 'mean_word_length': 4.843995510662177, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4809800', 'n_tokens_mistral': 2045, 'n_tokens_neox': 2010, 'n_words': 517}