summaryrefslogtreecommitdiff
path: root/pinyadmin/sbin/pinyconfig
blob: d6cb795c6a4dc5357b49c674b320f37297a3380f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/perl

use strict;
use warnings;

use Piny;

use Getopt::Long qw(:config bundling auto_abbrev);

my ( $fast, $user, $help ) = ( 0, 0, 0 );

GetOptions (
  "fast|f" => \$fast,
  "user|u" => \$user,
  "help|h" => \$help
);

my ( $reponame, $attr, $value ) = @ARGV;

sub usage {
  print <<EOF;
Usage: $0 [--fast] <reponame|--user> [attribute] [value]
Summary of options:
  --fast, -f    Skip rebuild
  --user, -u    Configure user
EOF

  exit ( 1 );
};

if ( $help ) {
  &usage;
};

my $repo;
my $config;
my $env = Piny::Environment->instance( );

if ( $user ) {
  ( $attr, $value ) = @ARGV;
  $config = $env->user->config;
} else {
  ( $reponame, $attr, $value ) = @ARGV;
  if ( not defined $reponame ) {
    &usage;
  };
  $repo = Piny::Repo->new( $reponame );
  $config = $repo->config;
};

if ( not defined $attr ) {

  my $tweakables = $config->all_tweakables;

  foreach my $t ( sort keys %$tweakables ) {
    my $v = $tweakables->{$t};
    if ( defined $v ) {
      print "$t => $v\n";
    } else {
      print "$t undefined\n";
    };
  };

  if ( defined $repo ) {
    print "description => " . $repo->description . "\n";
  };

} else {

  $attr = lc $attr;
  $attr =~ s/\./_/g;

  if ( defined $value ) {
    unless ( $env->user->uid == 0 or $user or $repo->owner->uid == $env->user->uid ) {
      print "You are not the owner of that repo!\n";
      exit( 3 );
    };

    undef $@;
    eval {
      if ( defined $repo and $attr eq "description" ) {
        $repo->description( $value );
      } else {
        $config->$attr( $value );
      };
    };
    if ( $@ ) {
      print STDERR "$attr is not a legal tweakable, or $value is not a legal value for that tweakable.\n$@\n";
      exit( 4 );
    };
    if ( defined $repo and $attr eq "description" ) {
      if ( $value ne $repo->description ) {
        print STDERR "Failed to set $attr (perhaps an override is in place)\n";
        exit( 5 );
      };
    } elsif ( $value ne $config->$attr ) {
      print STDERR "Failed to set $attr (perhaps an override is in place)\n";
      exit( 5 );
    };
    if ( $attr =~ /sharedrepository|ikiwiki/i and not $user and not $fast ) { # FIXME: define in Config.pm instead of here
      $repo->rebuild; # FIXME: check for actual change in value
    };
  };

  undef $@;
  eval {
    if ( defined $repo and $attr eq "description" ) {
      print "description = " . $repo->description . "\n";
    } else {
      print "$attr = " . $config->$attr . "\n";
    };
  };
  if ( $@ ) {
    print STDERR "$attr is not a legal tweakable, or its current value is illegal.\n$@\n";
    exit( 6 );
  };

};