#!/usr/bin/perl 
#
#  update-bbstyles -- stylemenu update script
#  Copyright (c) 1999 by Brent A. Fulgham <bfulgham@debian.org>
#  
#  This program is free software, under the terms of the
#  GNU General Public License as published by the Free Software
#  Foundation; either version 2 of the license, or (at your
#  option) any later version.
#
#  This routine is designed to be used in conjunction with 
#  Brad Hughes' Blackbox window manager.
#
#  It can be run in three modes:
#
#    --update  [filename] [title]
#
#        This updates the stylemenu in /usr/share/blackbox/styles with
#        the style file "filename" of name "Title".  Note that Title
#        should be quoted.  This routine places the new style in the
#        menu in alphabetical order.
#
#    --remove  [title]
#
#        This removes the style called "title" from the menu file.
#        Note that title should be quoted. 
#
#    --build
#
#        This creates a brand-new style menu in /usr/share/blackbox/styles
#        based on the files present in the /usr/share/blackbox/style
#        directory.  Note that because the blackbox style format does
#        not contain meta-data for the title, this information is lost
#        and the file name is used for the title.  The stylemenu may
#        be hand-edited to provide the desired title.

#
#  Globals
#
$stylesmenu = "/usr/share/blackbox/styles/stylesmenu";
$stylesdir  = "/usr/share/blackbox/styles";
$tempstyle = "/tmp/styles.temp";

#
#  Initialization
#
sub initialize 
{
    if (($ARGV[0] eq "--update") || ($ARGV[0] eq "-u"))
    {
    	$filename = $ARGV[1]  || die "No filename argument given\n";
	$title = $ARGV[2]     || die "No title argument given\n";
    	return 1;
    }
    elsif (($ARGV[0] eq "--build") || ($ARGV[0] eq "-b"))
    {
    	return 2;
    }
    elsif (($ARGV[0] eq "--remove") || ($ARGV[0] eq "-r"))
    {
        $title = $ARGV[1]     || die "No title argument given\n";
        return 3;
    }
    else 	# Bad input
    {
        print "\n";
    	print "Usage:  update-bbstyles [--update] [filename] [title]\n";
	print "        update-bbstyles [--build]\n\n";
	print "        update-bbstyles [--remove] [title]\n";
	print "  -u, --update          Add an entry to the styles menu\n";
	print "  -b, --build           Build the style menu from the\n";
	print "                        files in the style directory.\n";
	print "  -r, --remove          Remove an entry from the menu.\n\n";
	print "Note:  The build option will destroy any title information\n";
	print "in the menu, because the style files do not contain this\n";
	print "information.  The filename will be used as the style title\n";
	print "in this case.\n";
	exit 1;
    }
}

#
#  Update the style menu
#
sub update
{
    print "Updating the style menu\n";
    opendir(STYLESDIR, $stylesdir) || die "Sorry.  No $stylesdir directory.\n";
    if (-f "$stylesdir/$filename")
    {
    	# Update the style menu
	open(INSTYLE, "$stylesmenu") || die "Couldn't open $stylesmenu\n";
	open(TEMPFILE, ">$tempstyle") || die "Sorry.  Can't write to $tempstyle.\n";
	$used = 0;
	while (<INSTYLE>)
	{
            chomp($_);
	    ($foo, $name, $bar) = split(/[()]/, $_);
            if ($filename gt $name || $used == 1)
            {
		print TEMPFILE "$_\n";
            }
	    else
	    {
	        print TEMPFILE "[style]  ($title)   {$stylesdir/$filename}\n";
		print TEMPFILE "$_\n";
		$used = 1;
	    }
	}
    }
    else
    {
        die "$filename is not present in the $stylesdir directory.\n";
    }
    close(TEMPFILE);
    close(INSTYLE);
    unlink($stylesmenu) || die "Can't remove old style menu: $stylesmenu\n";
    system("mv $tempstyle $stylesmenu") && die "Couldn't move $tempstyle to $stylesmenu\n";
    closedir(STYLESDIR);
}


#
#  Build the style menu from the directory entries
#
sub build
{
    print "Building the style menu\n";
    opendir(STYLESDIR, $stylesdir) || die "Sorry.  No $stylesdir directory.\n";
    open(STYLEFILE, ">$tempstyle") || die "Sorry.  Can't write to $tempstyle.\n";

    @names = readdir(STYLESDIR);
    foreach $name (sort(@names))
    {
        if (-f "$stylesdir/$name" && $name ne "stylesmenu" && $name ne "styles.temp")
	{
            print STYLEFILE "[style] ($name)    {$stylesdir/$name}\n"
	}
    }
    close(STYLEFILE);
    close(TEMPFILE);
    if (-f "$stylesmenu")
    {
    	unlink($stylesmenu) || die "Can't remove old style menu: $stylesmenu\n";
    }
    system("mv $tempstyle $stylesmenu") && die "Couldn't move $tempstyle to $stylesmenu\n";
    closedir(STYLESDIR);
} 

#
#  Remove a Style from the style menu
#
sub remove
{
    print "Removing $title from the style menu\n";
    opendir(STYLESDIR, $stylesdir) || die "Sorry.  No $stylesdir directory.\n";
    open(INSTYLE, "$stylesmenu") || die "Couldn't open $stylesmenu\n";
    open(TEMPFILE, ">$tempstyle") || die "Sorry.  Can't write to $tempstyle.\n";
    while (<INSTYLE>)
    {
        chomp($_);
        @fields = split(/[()]/, $_);
        if ($title ne $fields[1])
        {
	    print TEMPFILE "$_\n";
	}
    }
    close(STYLEFILE);
    close(TEMPFILE);
    unlink($stylesmenu) || die "Can't remove old style menu: $stylesmenu\n";
    system("mv $tempstyle $stylesmenu") && die "Couldn't move $tempstyle to $stylesmenu\n";
    closedir(STYLESDIR);
}

#
#  Main routine
#
$mode = initialize();

if ($mode == 1)		# Update
{
    update();
}
elsif ($mode == 2)	# Build
{
    build();
}
else			# Remove
{
    remove();
}
