<commit_before>#!/usr/bin/perl

use FindBin;
use lib "$FindBin::Bin/../lib";

use warnings;
use strict;
use List::MoreUtils qw/uniq/;
use File::Compare;
use File::Copy;
use File::Find;
use File::Spec;
use Cwd;
use File::Path qw(make_path remove_tree);
use File::Basename;
use Data::Dumper;
use Getopt::Long;
use Template;
use LWP::Simple;
# use Modern::Perl;               # needs apt-get libmodern-perl
# use Term::ReadKey;

my $coder;
my $canonical_coder;

BEGIN
{
    eval
    {
     require JSON::XS;
     $coder = JSON::XS->new()->relaxed()->utf8()->allow_nonref();
     # for storing JSON data so it's directly comparable
     $canonical_coder = JSON::XS->new()->canonical()->utf8()->allow_nonref();
    };
    if ($@ )
    {
     warn "Falling back to plain JSON module (you should install JSON::XS)";
     require JSON;
     $coder = JSON->new()->relaxed()->utf8()->allow_nonref();
     # for storing JSON data so it's directly comparable
     $canonical_coder = JSON->new()->canonical()->utf8()->allow_nonref();
    }
}

use constant SKETCH_DEF_FILE => 'sketch.json';

$| = 1;                         # autoflush

my %options =
 (
  verbose    => 0,
  quiet      => 0,
  help       => 0,
  force      => 0,
  # switched depending on root or non-root
  'act-file'   => $> == 0 ? '/etc/cf-sketch/activations.conf' : glob('~/.cf-sketch/activations.conf'),
  configfile => $> == 0 ? '/etc/cf-sketch/cf-sketch.conf' : glob('~/.cf-sketch/cf-sketch.conf'),
  'install-target' => undef,
  'install-source' => local_cfsketches_source(File::Spec->curdir()) || 'https://raw.github.com/cfengine/design-center/master/sketches/cfsketches',
  'make-package' => [],
  cfhome => '/var/cfengine/bin',
  runfile => undef,
 );

my @options_spec =
 (
  "quiet|qq!",
  "help!",
  "verbose|v!",
  "force|f!",
  "cfhome=s",
  "configfile|cf=s",
  "runfile|rf=s",
  "params|p=s",
  "act-file|af=s",
  "install-target|it=s",

  "config!",
  "interactive|x!",
  "repolist|rl=s@",
  # "make-package=s@",
  "install|i=s@",
  "install-source|is=s",
  "remove|r=s@",
  "activate|a=s",                 # activate only one at a time
  "deactivate|d=s",               # deactivate only one at a time
  "test|t=s@",
  "search|s=s@",
  "list|l=s@",
  "list-activations|la!",
  "generate|g!",
 );

GetOptions (
            \%options,
            @options_spec,
           );

if ($options{help})
{
 print <DATA>;
 exit;
}

my $happy_root = -e '/var/cfengine/state/am_policy_hub' ? '/var/cfengine/masterfiles' : '/var/cfengine/inputs';

my $required_version = '3.3.0';
my $version = cfengine_version();

if (!$options{force} && $required_version gt $version)
{
 die "Couldn't ensure CFEngine version [$version] is above required [$required_version], sorry!";
}

if (open(my $cfh, '<', $options{configfile}))
{
 while (<$cfh>)
 {
  # only look at the first line of the file
  my $given = $coder->decode($_);

  foreach (sort keys %$given)
  {<commit_msg>Improved config directory and default install-target determination.<commit_after>#!/usr/bin/perl

use FindBin;
use lib "$FindBin::Bin/../lib";

use warnings;
use strict;
use List::MoreUtils qw/uniq/;
use File::Compare;
use File::Copy;
use File::Find;
use File::Spec;
use Cwd;
use File::Path qw(make_path remove_tree);
use File::Basename;
use Data::Dumper;
use Getopt::Long;
use Template;
use LWP::Simple;
# use Modern::Perl;               # needs apt-get libmodern-perl
# use Term::ReadKey;

my $coder;
my $canonical_coder;

BEGIN
{
    eval
    {
     require JSON::XS;
     $coder = JSON::XS->new()->relaxed()->utf8()->allow_nonref();
     # for storing JSON data so it's directly comparable
     $canonical_coder = JSON::XS->new()->canonical()->utf8()->allow_nonref();
    };
    if ($@ )
    {
     warn "Falling back to plain JSON module (you should install JSON::XS)";
     require JSON;
     $coder = JSON->new()->relaxed()->utf8()->allow_nonref();
     # for storing JSON data so it's directly comparable
     $canonical_coder = JSON->new()->canonical()->utf8()->allow_nonref();
    }
}

use constant SKETCH_DEF_FILE => 'sketch.json';

$| = 1;                         # autoflush

# Configuration directory depending on root/non-root
my $configdir = $> == 0 ? '/etc/cf-sketch' : glob('~/.cf-sketch');

my %options =
 (
  verbose    => 0,
  quiet      => 0,
  help       => 0,
  force      => 0,
  # switched depending on root or non-root
  'act-file'   => "$configdir/activations.conf",
  configfile => "$configdir/cf-sketch.conf",
  'install-target' => undef,
  'install-source' => local_cfsketches_source(File::Spec->curdir()) || 'https://raw.github.com/cfengine/design-center/master/sketches/cfsketches',
  'make-package' => [],
  cfhome => '/var/cfengine/bin',
  runfile => undef,
 );

my @options_spec =
 (
  "quiet|qq!",
  "help!",
  "verbose|v!",
  "force|f!",
  "cfhome=s",
  "configfile|cf=s",
  "runfile|rf=s",
  "params|p=s",
  "act-file|af=s",
  "install-target|it=s",

  "config!",
  "interactive|x!",
  "repolist|rl=s@",
  # "make-package=s@",
  "install|i=s@",
  "install-source|is=s",
  "remove|r=s@",
  "activate|a=s",                 # activate only one at a time
  "deactivate|d=s",               # deactivate only one at a time
  "test|t=s@",
  "search|s=s@",
  "list|l=s@",
  "list-activations|la!",
  "generate|g!",
 );

GetOptions (
            \%options,
            @options_spec,
           );

if ($options{help})
{
 print <DATA>;
 exit;
}

my $happy_root = $> != 0 ? glob("~/.cfagent/inputs") : (-e '/var/cfengine/state/am_policy_hub' ? '/var/cfengine/masterfiles' : '/var/cfengine/inputs');

my $required_version = '3.3.0';
my $version = cfengine_version();

if (!$options{force} && $required_version gt $version)
{
 die "Couldn't ensure CFEngine version [$version] is above required [$required_version], sorry!";
}

if (open(my $cfh, '<', $options{configfile}))
{
 while (<$cfh>)
 {
  # only look at the first line of the file
  my $given = $coder->decode($_);

  foreach (sort keys %$given)
  {
{'fraction_non_alphanumeric': 0.1913714957193218, 'fraction_numerical': 0.004700352526439483, 'mean_word_length': 3.323657474600871, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 71, 'https://': 2, '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': '7530706', 'n_tokens_mistral': 2261, 'n_tokens_neox': 2171, 'n_words': 466}