#!/usr/bin/perl -w

# $Id: stylesdir.pl.in,v 1.1 2003/07/06 05:16:43 brwill Exp $

use POSIX;

my $title = "styles";
my $stylesdir = "/usr/X11R6/share/kahakai/styles";
my $homestylesdir = "$ENV{HOME}/.kahakai/styles";
@Styles = ();

if (! $ARGV[0]) {
    print_directory($stylesdir);
    print_directory($homestylesdir);
}
else {
    print_directory($ARGV[0]);
}

if (@Styles) {
    print "[title] ($title)\n";
    foreach $Style (@Styles) {
       print $Style;
    }
}                                                                     

sub print_directory {
	my $mode;
	my $temp;
	my @dir_list;

	opendir(DIR, $_[0]) || die "can't opendir $_[0]: $!";
	@dir_list = grep { /^[^\.].*/ || /^\.\..*/} readdir(DIR);
	closedir DIR;

	while ($temp = shift @dir_list) {
		$mode = (stat("$_[0]\/$temp"))[2];
		if (!($temp eq '..' || $temp eq '.')) {
			if (S_ISDIR($mode)) {
				print_directory("$_[0]\/$temp");
			} else {
				if (S_ISREG($mode)) {
					$_ = $temp;
					if (/(.+)\.style$/) {
						$Item = "[item] ($1) \"restart(/usr/X11R6/share/kahakai/../../bin/kahakai --stylefile " .
							"$_[0]\/$temp)\"\n";
						push(@Styles, $Item);
					}
				}
			}
		}
	}
	return 0;
}
