#!/usr/bin/env perl

use 5.010;
use warnings;
use strict;

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

my %switch = ();
$0 = "repo";
my $pid = $$;

getopts('d:p:hs', \%switch);


sub start {
    daemonize();
    stop();
    mkpath("/tmp/.repo/pid");
    open(my $fh,">", "/tmp/.repo/pid/$pid") || die "cant open /tmp/.repo/pid/$pid: $!";
    print $fh $pid;
    close $fh;

    my $packages = packages($switch{d});
    my $base_dir = $switch{d} || $ENV{HOME}; $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('daemon', '-l', "http://*:3000");
}

sub stop {
    find( sub{ 
                unless($_ eq '.' or $_ eq '..'){ 
                    my $pid = "$_" + 1;
                    say "killing: repo $pid";
                    system("kill -9 $pid");
                    unlink("/tmp/.repo/pid/$_");
                }
            }, "/tmp/.repo/pid" );
}

sub usage {
    say "\nUsage:\n\tstart:\trepo -d /path/to/deb";
    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 NAME repo

Creates Packages list 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



