#!/usr/bin/perl

use strict;
use warnings;

use Future::AsyncAwait;

use IO::Async::Loop;
use Net::Async::Webservice::S3;

my $loop = IO::Async::Loop->new;

my %config = do {
   open my $rc, "<", ".s3rc" or die "Cannot read .s3rc config - $!\n";
   map { chomp; m/^(.*?)=(.*)$/ } <$rc>;
};

my ( $bucket, $prefix ) = split m{/}, ( $config{bucket} || die "Could not find 'bucket' in config\n" ), 2;
my $s3 = Net::Async::Webservice::S3->new(
   access_key => ( $config{access_key} || die "Could not find 'access_key' in config\n" ),
   secret_key => ( $config{secret_key} || die "Could not find 'secret_key' in config\n" ),
   ssl        => $config{ssl},
   bucket     => $bucket,
   prefix     => $prefix,
);

$loop->add( $s3 );

my ( $keys, $prefixes ) = await $s3->list_bucket(
   prefix => shift @ARGV,
   delimiter => "/",
);

print "$_->{key}\n" for @$keys;
print "$_\n" for @$prefixes;
