#!/usr/bin/perl -w
use strict;

use DBI;
use YAML;
use Bookmarks;

my $dbname = 'new.db';

my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "", { RaiseError => 1, PrintError => 0 });

my $bookmarks = Bookmarks->new({
    dbh => $dbh,
});

my $command = shift;

my %action_for = (
    get => sub {
        my $identifier = shift;
        my $query = $identifier =~ /^\d+$/ ? { id => $identifier } : { uri => $identifier };
        my $bookmark = $bookmarks->get_bookmark($query);

        print $bookmark ? Dump($bookmark) : "Not Found\n";
    },
    add => sub {
        my ($uri, $title, @tags) = @_;
        my $bookmark = $bookmarks->add({ uri => $uri, title => $title, tags => \@tags });
        print Dump($bookmark);
    },
    list => sub {
        my @resources = $bookmarks->get_resources();
        # TODO: list by tags, date, etc.
        # TODO: coordinate this commandline script with the CGI app
        print Dump(\@resources);
    }
);

$action_for{$command}->(@ARGV);

=begin

use YAML;

