#!/usr/bin/env perl

# Copyright C 2002-2003 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

use strict;

my %matches;
my ($pattern, $filename, $match);

die "usage: filename-complete [pattern] [root]" if @ARGV > 2;

$pattern = $ARGV[0];
if ($pattern =~ /^$/) {
    $pattern = "/";
}
if ($pattern !~ /^.*\/$/) {
    $filename = `dirname $pattern`;
    chomp $filename;
} else {
    $filename = $pattern;
}

close STDERR;
open IN, "find $filename -maxdepth 1 |";

while ($match = <IN>) {
    chomp $match;
    if (index($match, $pattern) eq 0) {
	my $next_dir = index($match, "/", length($pattern));
	if ($next_dir != -1) {
	    $match = substr($match, 0, $next_dir + 1);
	}
	if ($match =~ /.*\/\..*/) {
		next;
	}
	if ($match !~ /^.*\/$/) {
		if (-d $match) {
			$match .= "/";
		}
	} else {
		if ($match =~ /^$pattern$/) {
			next;
		}
	}
	if (!exists $matches{$match}) {
	    $matches{$match} = "";
	    print "$match\n";
	}
    }
}
