#!/usr/bin/env perl

use strict;
use warnings;

use Getopt::Long;
use File::HomeDir;
use File::Spec::Functions 'catfile';
my %args;
die 'unknown option'
  unless GetOptions( \%args, 'file|f=s', 'help|h' );

my $USAGE = <<'END';
USAGE: bbs-perm-config OPTIONS
EXAMPLES:
    bbs-perm-config init                              # init the config file
    bbs-perm-config --file /path/to/bbspermrc init    # customize config path
    bbs-perm-config global                            # show global configs
    bbs-perm-config global.protocol=ssh               # set global protocol
END

if ( $args{help} || !@ARGV ) {
    print $USAGE;
    exit;
}

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

require BBS::Perm::Config;

for my $arg (@ARGV) {
    next unless $arg;

    if ( $arg eq 'init' ) {
        if ( -e $file && -s $file ) {
            print "$file exists and is not mepty, overwrite? (y/N) ";
            exit unless <STDIN> =~ /y(?:es)?/i;
        }

        open my $fh, '>', $file or die $!;
        local $/;
        print $fh <DATA>;
        close $fh;
        next;
    }

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

    my $config = BBS::Perm::Config::_LoadFile($file);

    if ( $arg =~ /=/ ) {
        my ( $keys, $value ) = split /=/, $arg, 2;
        my @keys     = split /\./, $keys;
        my $item     = $config;
        my $last_key = pop @keys;
        for my $key (@keys) {
            if ( ref $item ) {
                if ( ref $item eq 'HASH' ) {
                    $item = $item->{$key};
                }
                elsif ( ref $item eq 'ARRAY' ) {
                    $item = $item->[$key];
                }
            }
            else {
                die "$item is not a ref";
            }
        }

        if ( ref $item ) {
            if ( ref $item eq 'HASH' ) {
                $item->{$last_key} = $value;
            }
            elsif ( ref $item eq 'ARRAY' ) {
                $item = $item->[$last_key];
            }
        }
        else {
            die "$item is not a ref";
        }

        BBS::Perm::Config::_DumpFile( $file, $config );
    }
    else {
        my @keys = split /\./, $arg;
        my $item = $config;
        for my $key (@keys) {
            if ( ref $item ) {
                if ( ref $item eq 'HASH' ) {
                    $item = $item->{$key};
                }
                elsif ( ref $item eq 'ARRAY' ) {
                    $item = $item->[$key];
                }
            }
            else {
                die "$item is not a ref";
            }
        }
        require Data::Dumper;
        no warnings 'once';
        local $Data::Dumper::Terse = 1;
        print Data::Dumper::Dumper($item);
    }
}

__DATA__
---
global:  
# Default values for all sites
    title: 'bbs-perm'
    protocol: ssh
    port: 22
    agent: 'bbs-perm-agent'
    font: 'WenQuanYi Micro Hei Mono 20'
    timeout: 60
    background_file: /path/to/picture
    background_transparent: 0
    opacity: 0.8
    mouse_autohide: 0
    default:
        - bash
    color:
        background: black
        foreground: white
    plugins:
        uri:
            default: 'http://www.google.com'
            browser: 'firefox -new-tab'
        ip:
            qqwry: /opt/qqwry.dat
        feed:
            label: Feed
    shortcuts:
        copy: 'M-c'
        paste: 'M-v'
        fullscreen: 'CM-f'
        feed: 'C-f'
        left_tab: 'M-['
        right_tab: 'M-]'
newsmth:
# use Alt+j to connect
    shortcut: 'M-j'
    encoding: gbk
    title: guest@newsmth
    site: newsmth.net
    username: guest
bash:
    shortcut: 'M-l'
    title: bash
    protocol: 'bash'
    agent: 'bash'
    timeout: 0

