#!/usr/bin/env perl

use strict;
use warnings;

use Glib qw/TRUE FALSE/;
use Gtk2 '-init';
use BBS::Perm qw/Feed IP URI/;
use File::HomeDir;
use File::Spec::Functions 'catfile';
use Getopt::Long;

my %args;
die 'unknown option' unless GetOptions( \%args, 'config|c=s', 'help|h' );

my $USAGE = <<'END';
USAGE: bbs-perm key1 key2
EXAMPLES:
    bbs-perm newsmth                             # load newsmth
    bbs-perm newsmth bash                        # load both newsmth and bash
    bbs-perm --config /path/to/bbspermrc newsmth # load both newsmth
END

if ( $args{help} ) {
    print $USAGE;
    exit;
}

my $config = $args{config} || catfile( File::HomeDir->my_home, '.bbspermrc' );

if ( !-e $config ) {
    print "no config file, maybe you forgot to run bbs-perm-config --init";
    exit;
}

my $perm = BBS::Perm->new(
    config => { file  => $config },
    perm   => { accel => 1 },
);

my $window = $perm->window;
$window->signal_connect( destroy => sub { Gtk2->main_quit; } );

my $vbox = Gtk2::VBox->new;
my $boxes = $perm->config->setting('global')->{boxes}
  || [ 'term', 'feed', 'ip' ];
for (@$boxes) {
    my $box = lc $_;
    if ( $box eq 'term' ) {
        $vbox->pack_start( $perm->term->widget, TRUE,  TRUE,  0 );
    }
    else {
        $vbox->pack_start( $perm->$box->widget, FALSE, FALSE, 0 );
    }
}
$window->add($vbox);
$window->show_all;
#$window->maximize;
$window->set_keep_above(1);

if (@ARGV) {
    for (@ARGV) {
        $perm->connect($_);
    }
}
else {
    $perm->connect();
}

Gtk2->main;

