#!/usr/bin/env perl

use 5.010;
use warnings;
use strict;

use Mojolicious::Lite;
use File::Find;
use File::Path;
use Getopt::Std;
use open qw<:encoding(UTF-8)>;
use App::Repo qw< digest packages >;

my %switch = ();
my $pid = $$;
getopts('d:hs', \%switch);

sub start {
    mkpath("/tmp/_repo/");
    system("rm -rf /tmp/_repo/repo_*");
    open(my $fh,">", "/tmp/_repo/repo_$pid") || die "cant open /tmp/_repo/repo_$pid: $!";
    print $fh $pid;
    close $fh;

    my $packages = packages($switch{d});
    my $base_dir = $switch{d}; $base_dir =~ s/(.*?)(\/deb)/$1/;
    unlink( "$base_dir/Packages", "$base_dir/Packages.gz" );
    open($fh,">>", "$base_dir/Packages") || die "cant open $base_dir/Packages: $!";
    
    for my $package (@$packages){
        for(@$package){
            print $fh "$_\n";
        }
    };
    close $fh;
    system("cd $base_dir && gzip Packages");

    plugin( 'Directory', root => "$base_dir" )->start;
}

sub stop {
    
    find( sub{ 
            if(/repo_/){
                s/(repo_)(.*)/$2/;
                say "killing $_";
                system("kill -9 $_");
            }}, "/tmp/_repo/" )
}

sub usage {
    say "\nUsage:\n\tstart:\trepo -d /path/to/deb/directory daemon";
    say "\tstart in background:    nohup repo -d /path/to/deb daemon&";
    say "\tstop:\trepo -s";
    say "\thelp\trepo -h";
    say "\n\n";
}

if(defined $switch{d}){
    start();
} elsif(defined $switch{h}){
    usage();
} elsif(defined $switch{s}){
    stop();
}

=head1 repo

Creates Packages.gz and starts repository on localhost:3000

=head1 SYNOPSIS

repo creates Packages.gz file needed by APT client to read content of repository, then starts repository running on Mojolicious server on localhost:3000

=head1 USAGE

start:                  C<repo -d /path/to/deb daemon>

start in background:    C<nohup repo -d /path/to/deb daemon&>

stop:                   C<repo -s>

usage:                  C<repo -h>

=cut



