#!/usr/bin/perl -w
# 
# Open source under the BSD License.
# 
# Copyright (c) 2006 Ivan Griffin (ivan dot griffin at u l dot i e)
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the author nor the names of contributors may be
#       used to endorse or promote products derived from this software without
#       specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

use strict;
my $DEBUG = 0;

sub ParseDir($$$)
{
    my $path = shift;
    my $input_dir = shift;
    my $level = shift;

    &IndentLevel($level);
    print FILEOUT "<node ";
    if ($level != 1) { print FILEOUT "folded=\"true\" "; }
    print FILEOUT "text=\"$input_dir\">\n";
    

    my $dir_path = "";
    if ($path eq "")
    {
       $dir_path = $input_dir; 
    }
    else
    { 
       $dir_path = "$path/$input_dir";
    }
    opendir(DIR, "$dir_path") || die "Can't open $dir_path: $!";
    my @nodots = grep { /[^\.]/ } readdir(DIR);
    closedir(DIR);


    for my $key (sort @nodots)
    {
        print STDERR "Considering $dir_path/$key\n" if ($DEBUG);

        if ((-d "$dir_path/$key") && ($key !~ /\.svn/))
        {
            print STDERR "Recursing into $key\n" if ($DEBUG);
            &ParseDir("$dir_path", "$key", $level+1);
        }

        if (-f "$dir_path/$key")
        {
             if ($key =~ /.[chs]$/)  # search for .c,.h or .s files
             {
                 print STDERR "$level: $dir_path/$key\n" if ($DEBUG);
                 &IndentLevel($level+1);
                 print FILEOUT "<node text=\"$key\"/>\n";
             }
        }
    }

    &IndentLevel($level);
    print FILEOUT "</node>\n";

}

sub IndentLevel($$$)
{
    my $level = shift;
    for (my $i = 0; $i < $level; $i++) { print FILEOUT "  "; }

}

sub Usage()
{
    print STDERR "Usage: $0 <output_filename> <root_directory>\n";
    exit(1);
}


######################################################################
#
# Main
#

&Usage() if ($#ARGV != 1);
&Usage() if (! -d $ARGV[1]);


open(FILEOUT, ">$ARGV[0]") || die "Can't open $ARGV[0]: $!";

print FILEOUT <<EOF ;
<map version="0.8.0">
EOF

ParseDir("", "$ARGV[1]",1);

print FILEOUT <<EOF ;
</map>
EOF

