#!/usr/bin/perl -w
# [EMAIL PROTECTED] 2004/6/15
# GNU GPL

my $path='/';
my $pwd='/';
my $base;
my $depth=0;
my $name;
my $nick;
my $tab;
my $find_args;
my $dir;

my @files;
# Initailise the leaf array.  Here we say / currently contains 0 leaves.
my @leaf; $leaf[0]=0;
my @types = qw(ac 3ds ase dfx flt md2 obj vrml vrml1 atg);

$find_args=' -follow -type f -name "*.'.pop(@types).'"';
foreach $i (@types) {
  $find_args.=" -or -name \"*.$i\"";
}

$base=$ENV{PWD} unless ($base=shift());
$base=~/^--?h/ and &Help();
chdir($base) or die "could not chdir to $base";
@files = `find * $find_args`;
@files = sort @files;
foreach $i (@files) {
  $i=~s!^([^/].*)!/$1!;
}

print '<?xml version="1.0"?>'."\n";
print "\n";
print "<PropertyList>\n";
print "  <name type=\"string\">Library</name>\n";

foreach $path (@files) {
  $path=~s/^\.//;
  $name=$path;
  $path=~s/(.*)\/.*/$1/;
  chomp($path);
  $name=~s/.*\/(.*)/$1/;
  chomp($name);
  $nick=$name;
  $nick=~s/(.*)\..*/$1/;

  ($pwd, $depth, @leaf) = &GotoRightDir($path, $pwd, $depth, @leaf);

  # Now assume $path eq $pwd
  $tab = &Tab($depth);
  print "$tab<model n=\"$leaf[$depth]\">\n";
  print "$tab  <name type=\"string\">${nick}</name>\n";
  print "$tab  <path type=\"string\">${base}${path}/${name}</path>\n";
  print "$tab</model>\n";
  $leaf[$depth]++;
}

# Clean up
&GotoRightDir($path, $pwd, $depth, @leaf);
print "</PropertyList>\n";
exit;

sub GotoRightDir {
  # Ascend or descecnd directories until $path eq $pwd
  # Make appropriate STDOUT along the way

  my ($path, $pwd, $depth, @leaf) = @_;
  my $branch;

  # Just in case
  $path='/' unless $path;

  while ("$path" ne "$pwd") {
    if ($path=~/^$pwd/) {
      # Descend to the next dir
      # branch is the next directory in $path after $pwd
      $branch=$path;
      $branch=~s/$pwd//;
      $branch=~s/^\/?([^\/]*).*/$1/;
      # / is a special case
      $pwd=~s!/$!!;
      $pwd.="/$branch";

      # $depth is still set to the parent's level
      $tab = &Tab($depth);
      print "${tab}<folder n=\"$leaf[$depth]\">\n";
      print "$tab  <name type=\"string\">$branch</name>\n";

      # Keep track of parent's number of leaves
      $leaf[$depth++]++;
      # This is a new dir, and has no leaves yet
      $leaf[$depth]=0;
    } else {
      # Ascend toward /
      $pwd=~s![^/]*$!!;
      $pwd=~s!^(/.*)/$!$1!;
      # Set $depth to parent's level
      $depth--;

      $tab = &Tab($depth);
      print "${tab}</folder>\n";
    }
  }
  return $pwd, $depth, @leaf;
}

sub Tab {
  # Generate an indentation based on the parent's level
  my $depth = shift;
  my $tab='';

  for ($i=0; $i<=$depth; $i++) {
    $tab.='  ';
  }
  return $tab;
}

sub Help {
  print "usage: $0 [-h] [path]\n\n";
  print "Builds an fgsd xml object library and outputs it to STDOUT,\n";
  print "If a path is given the library will be built with that as the root,\n";
  print "otherwise the pwd will be used as the root.\n";
  print "[path] may contain symlinks to other directories.\n";
  exit 0;
}

_______________________________________________
Flightgear-devel mailing list
[EMAIL PROTECTED]
http://mail.flightgear.org/mailman/listinfo/flightgear-devel

Reply via email to