#!/usr/bin/perl
#
# Perl script to take the contents of a directory tree
# and place them in an FvwmMenu. This script is designed
# for my MusicMenu and is based off fvwm-menu-directory.


use strict;
use Getopt::Long;

# Defaults of global variables
my $menu = "/home/jaimos";
my $subMenuFunc = "BuildFvwmMusicMenu";
my $fileComm = "xmms";
my $dir = "";
my $menuTitle = "Music Menu";
my $startsWith = "";
my $defaultFileIcon = "";
my $defaultDirIcon = "";


GetOptions(
  "menu=s"		=> \$menu,
  "file-icon=s"		=> \$defaultFileIcon,
  "dir-icon=s"		=> \$defaultDirIcon,
  "sub-menu-func=s"	=> \$subMenuFunc,
  "command=s"		=> \$fileComm,
);

# $menu = $dir|$startsWith
if ( $menu =~ /(.+)\|(.+)/ ) {
  $dir = $1;
  $startsWith = $2;
} else { $dir = $menu; }

# Sets the title of the menu
my $esc_dir = $dir;
$esc_dir =~ s/\'/\\\'/g;
$menuTitle = `basename $esc_dir`;
chop ($menuTitle);
$menuTitle =~ s/_/ /g;

# Initilize the menu
print qq(DestroyMenu recreate "$menu"\nAddToMenu "$menu"\n);
print qq(+ DynamicPopDownAction DestroyMenu "$menu"\n);
print qq(+ MissingSubmenuFunction $subMenuFunc\n);

# Set the first item (title) of the menu, using the icon
# $dir.png if it exists.
my $titleIcon = "$dir.png";
if ( -f $titleIcon ) {
  print qq(+ "$menuTitle\%$titleIcon\%" Exec $fileComm "$dir"\n+ "" Nop\n);
} elsif ( $defaultDirIcon ) {
  print qq(+ "$menuTitle\%$defaultDirIcon\%" Exec $fileComm "$dir"\n+ "" Nop\n);
} else {
  print qq(+ "$menuTitle" Exec $fileComm "$dir"\n+ "" Nop\n);
}

# Get the list of all files in $dir
opendir(DIR, "$dir");
my @files = readdir(DIR);
closedir(DIR);
@files = grep /^[^.]/, @files; # Remove all hidden (.*) files from list
@files = grep !/png$/, @files; # Remove all .png files from list

# Only list files matching /^$startsWith/
if ( $startsWith ) {
  @files = grep /^$startsWith/, @files;
}

# Sort the list with directories first, althabetically
@files = sort { -d $b <=> -d $a || $a cmp $b } @files;

# Add each member of the list to the MusicMenu
foreach (@files) {
  next if $_ eq '.' or $_ eq '..'; # Just to be sure (unsure if its needed)

  my $filePath = "$dir/$_";
  my $itemName = $_;
  $itemName =~ s/_/ /g;
  my $iconName = "$filePath.png";
  
  if ( -d $filePath ) { # Put a directory into the menu

    # Set the Dir Icon in the order of
    # $filePath.png > $defaultDirIcon > no icon
    if ( -f $iconName ) {
      print qq(+ "$itemName\%$iconName\%" Popup "$filePath"\n);
    } elsif ( $defaultDirIcon ) {
      print qq(+ "$itemName\%$defaultDirIcon\%" Popup "$filePath"\n);
    } else {
      print qq(+ "$itemName" Popup "$filePath"\n);
    }
  
  } elsif ( -f $filePath ) { # Put a file into the mneu

    # Set the File Icon in the order of
    # $filePath.png > $defaultFileIcon > no icon
    if ( -f $iconName ) {
      print qq(+ "$itemName\%$iconName\%" Exec $fileComm "$filePath"\n);
    } elsif ( $defaultFileIcon ) {
      print qq(+ "$itemName\%$defaultFileIcon\%" Exec $fileComm "$filePath"\n);
    } else {
      print qq(+ "$itemName" Exec $fileComm "$filePath"\n);
    }
  
  } else { # This Should NOT happen
    print qq(+ "Error: $filePath" Nop\n);
  }
}


