#!/usr/bin/env perl
use warnings;
use strict;

use Net::FTP;

# Usage: ftp-ls SERVER PATH

my $server = shift;
my $dir    = shift;
my $ftp    = new Net::FTP($server, Passive => 1)
  or die "Unable to connect to FTP server: $!";

$ftp->login or die "Unable to log in to FTP server: ", $ftp->message;
$ftp->cwd($dir) or die "Unable to change to $dir: ", $ftp->message;
my $contents = $ftp->dir;
die "Unable to list contents" unless defined $contents;

for (@$contents) {
    if (/^-.*?(\S*)$/) {
        print "$1\n";
    } elsif (/^d.*?(\S*)$/) {
        print "$1/\n";
    } elsif (/^l.*?(\S*) -> \S*$/) {
        print "$1@\n";
    }
}
