Index: trunk/bin/bkmkd
===================================================================
--- trunk/start	(revision 102)
+++ trunk/bin/bkmkd	(revision 103)
@@ -3,10 +3,10 @@
 
 use FindBin;
-use lib "$FindBin::RealBin/lib";
+use lib "$FindBin::RealBin/../lib";
 
 use Getopt::Long;
 use YAML;
 use Plack::Runner;
-use BookmarksApp;
+use File::Pid;
 use File::Spec::Functions qw{catfile};
 
@@ -18,19 +18,43 @@
 -e $CONFIG_FILE or die "Config file $CONFIG_FILE not found\n";
 my $config = YAML::LoadFile($CONFIG_FILE);
-my $app = BookmarksApp->new({ config => $config })->to_app;
+my $command = shift or die "Usage: $0 <start|stop>\n";
 
-my $server_root = $config->{server_root} || '.';
-my $listen      = ':' . ($config->{port} || 5000);
-my $access_log  = catfile($server_root, 'access');
-my $error_log   = catfile($server_root, 'errors');
-my $pid_file    = catfile($server_root, 'pid');
+my %run = (
+    start => sub {
+        my $config = shift;
+        require BookmarksApp;
+        my $app = BookmarksApp->new({ config => $config })->to_app;
 
-my $runner = Plack::Runner->new(server => 'Starman');
-$runner->parse_options(
-    '--daemonize',
-    '--listen',     $listen,
-    '--pid',        $pid_file,
-    '--error-log',  $error_log,
-    '--access-log', $access_log,
+        my $server_root = $config->{server_root} || '.';
+        my $listen      = ':' . ($config->{port} || 5000);
+        my $access_log  = catfile($server_root, 'access');
+        my $error_log   = catfile($server_root, 'errors');
+        my $pid_file    = catfile($server_root, 'pid');
+
+        my $runner = Plack::Runner->new(server => 'Starman');
+        $runner->parse_options(
+            '--daemonize',
+            '--listen',     $listen,
+            '--pid',        $pid_file,
+            '--error-log',  $error_log,
+            '--access-log', $access_log,
+        );
+        $runner->run($app);
+    },
+
+    stop => sub {
+        my $config = shift;
+        my $server_root = $config->{server_root} || '.';
+
+        my $pid_file = File::Pid->new({
+                file => catfile($server_root, 'pid'),
+            });
+
+        if (my $pid = $pid_file->running) {
+            kill 'TERM', $pid;
+        }
+    },
 );
-$runner->run($app);
+
+exists $run{$command} or die "Unrecognized command $command\n";
+$run{$command}->($config);
