#!/usr/bin/env perl
use Applify;

option str => listen => 'Listen to a custom URL. Default: http://*:5000', 'http://*:5000';
option bool => stdin => 'Allow standard input from ws', 0;
option bool => single => 'Run only one command, and exit at end', 0;
option str => conduit => 'pty (default) or pipe', 'pty';

documentation 'App::screenorama';
version 'App::screenorama';

sub server {
  my $self = shift;

  require App::screenorama;
  App::screenorama->new(
    conduit => $self->conduit,
    single => $self->single,
    stdin => $self->stdin,
    @_,
  );
}

app {
  my($self, @args) = @_;
  my($server, $program, @program_args);

  while(my $arg = shift @args) {
    push @program_args, $arg if $program;
    $program = shift @args if $arg eq '--';
  }

  $program or die "Usage: screenorama -- <program> [program_args]\n";
  $server = $self->server(program => $program, program_args => \@program_args);
  $server->start('daemon', '-l', $self->listen);

  return 0;
};
