#! /usr/local/bin/perl

printf("%s -> '%s'\n", $ARGV[0], &dodir($ARGV[0],$ARGV[1]));

sub wild_card  {
	local($orig_name, $list) = @_ ;
	local($pat, $ix, $name, $bslash, $orig_dir, $i, $t, $f, $l, $n, $ok) ;

	$name = $orig_name ;
	$bslash = ($name =~ s=([\\])=/=g) ? 1 : 0 ;
	#$name =~ s=/+$== ;
	if ($name =~ /^(.*)[\/]([^\/]*)$/) {
		$orig_dir = $dirname = $1;
		$pat = $2;
	} elsif ($name =~ /^([a-z][:])(.*)$/) {
		$orig_dir = $dirname = $1;
		$pat = $2;
	} else {
		$orig_dir = '' ;
		$dirname = '.';
		$pat = $name;
	}

	$pat =~ y/A-Z/a-z/ ;
	$dirname = "$dirname/" if ($dirname =~ /^[a-z][:]$/) ;
	##print "Looking in $dirname for /$pat/\n" ;
	$match = '' ;
	if (opendir(D,"$dirname"))  {
		undef %files ;  # be safe
		$n = 0 ;
		$l = length($pat) ;
		while (defined($t=readdir(D)))  {
			next if (substr($t,0,1) eq '.') ;
			$f = substr($t,0,$l) ;
			$f =~ y/A-Z/a-z/ ;
			next if ($f ne $pat) ;
			$orig_t = $t ;
			$t =~ y/A-Z/a-z/ ;
			$files{$t} = $orig_t ;
			++$n ;
		}
		closedir(D) ;

		##foreach $file (keys %files)  { print "\t$files{$file}\n"; }

		if ($n == 0) {
			return $orig_name ;
		} elsif ($n == 1) {
			$match = (keys %files)[0] ;
			##$match = $files{$match} ;
		} else {
			for ($i = length($pat);    ; ++$i) {
				$trial_pat = (keys %files)[0] ;
				$trial_pat = substr($trial_pat,0,$i) ;
				$ok = 1 ;
				foreach $file (keys %files)  {
					$f = substr($file,0,$i) ;
					#print "$i $file:'$f'=~/^$trial_pat/" ;
					if ($f eq $trial_pat) {
						#print "->ok=$ok\n" ;
						next ;
					}
					$ok = 0 ;
					#print "->ok=$ok\n" ;
					last ;
				}
				if ($ok == 0) {
					$match = substr($trial_pat,0,$i-1) ;
					last ;
				}
			}
			if ($list != 0) {
				foreach $file (keys %files) {
					print "$files{$file}\t";
				}
				print "\n";
			}
		}
	}
	$dirname =~ s=/+$== ;
	if ($orig_dir eq '') {
		$name = $match ;
	} else {
		$name = "$dirname/$match" ;
	}
	$name =~ s=([/])=\\=g if ($bslash) ;
	return $name ;
}
