#!/usr/bin/perl

use strict;

use File::Find;
use Cwd;
use File::Path qw(make_path remove_tree);
use File::Spec;

my $project_root_dir  = getcwd();

$project_root_dir =  File::Spec->rel2abs($project_root_dir);

my %seen = ();

# reiniting spek app 
print "reiniting spek app ...\n";
open APP, ">", "$project_root_dir/spek.psgi" or die $!;
print APP<<HERE;

use Kelp::Less;

HERE

close APP;

finddepth(\&wanted, $project_root_dir );

sub wanted { 

    my $file = shift;

    if ($file =~ /^(get|post|update|delete)\.txt$/) {

        my $http_method = $1;

        return if $seen{$File::Find::name};

        my $resource_dir = $File::Find::dir;

        if ( open CODE, ">>", "$resource_dir/$http_method.pm" ){

            open APP, ">>", "$project_root_dir/spek.psgi" or die $!;

            while (my $l =  <CODE>){
                chomp $l;
                print APP $l;
            }

            close APP;
            close CODE;
        }
        
        $seen{$File::Find::name}=1;
   }

}


open APP, ">>", "$project_root_dir/spek.psgi" or die $!;
print APP<<HERE;

run;

HERE

exec "plackup spek.psgi"
