#!/usr/bin/env perl
use strict;
use warnings;
use lib 'lib';
use Getopt::Long;
use JSTAPd::Server;
use Path::Class;

# From 'prove': Allow cuddling the paths with the -ignore_case
@ARGV = map { /^(-I)(.+)/ ? ($1,$2) : $_ } @ARGV;

my @includes;
my $dir = 'jt';
my $port = 1978;
my $host = '127.0.0.1';
my $new = '';

Getopt::Long::Configure("no_ignore_case", "pass_through");
GetOptions(
    "new|n=s"      => \$new,
    "d|dir=s"      => \$dir,
    "p|port=s"      => \$port,
    "a|addr=s"      => \$host,
    'I=s@'         => \@includes,
    "h|help",      => \my $help,
);

pod2usage(0) if $help;
lib->import(@includes) if @includes;

if ($new) {
    my $d = Path::Class::Dir->new($new);
    die "$d is exists." if -f $d || -d $d;
    print "create $d\n";
    $d->mkpath;

    my $f = $d->file('index');
    print "create $f\n";
    my $fh = $f->openw;
    print $fh <<'END';
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>index</title>
$HEAD
    </head>
    <body id="body">
$BODY
    </body>
</html>
END

    $f = $d->file('conf.pl');
    print "create $f\n";
    $fh = $f->openw;
    print $fh <<'END';
# JSTAPd config
my $config = {
    jstapd_prefix => '____jstapd',
    apiurl        => qr{^/(?!____jstapd(?:__api))},
#    apiurl        => qr{^/(?!____jstapd(?:__api)|jslib/)},
};
#$config->{urlmap} = [
#    { qr!^/jslib/! => '../jslib/' },
#];

# browser auto open
# for run_once mode (prove -vl foo.t or prove -vlr jstap/)
# this example for Mac OS X
# $config->{auto_open_command} = 'open -a Safari %s';
# or $ENV{JSTAP_AUTO_OPEN_COMMAND} = 'open -a Safari %s';

$config;
END

    $f = $d->file('01_base.t');
    print "create $f\n";
    $fh = $f->openw;
    print $fh <<'END';
use JSTAPd::Suite;

sub client_script {
    return <<'DONE';
tests(6);
ok(1, 'ok 1');
ok(!0, 'ok 0');
is('test', 'test', 'is');
isnt('test', 'dev', 'isnt');
like('test', new RegExp('es'), 'like');
is(tap$('test').innerHTML, 'DATA', 'getElementById');
DONE
}

sub html_body {
    return <<'DONE';
<div id='test'>DATA</div>
DONE
}
END

    exit;
}

JSTAPd::Server->new( dir => $dir, port => $port, host => $host )->run;

__END__

=head1 NAME

jstapd - 

=head1 SYNOPSIS

# start server
jstapd -d testdir
  -p 1978     - listen port
  -a 0.0.0.0  - listen address

# create new test
jstapd -n foo/bar/dirname

