# dir2axt.pl
#
# A utility for Novell's ZenWorks for Desktops
#
# Converts a folder structure into an AXT file suitable for importing via
# ConsoleOne (without having to snapshot the installation).
#
# Version 1.0 (1 May 2006)
#
# Iain F. McLaren (imclaren@myrealbox.com)
#
# Usage:
#     1.  Change the src_path and dst_path below to suit your needs.
#
#         src_path defines where on the network the source files are located
#         dst_path defines where they are copied to on the workstation.
#
#     2.  (Optional) change the source and target macros if necessary
#
#     3.  Ensure that appropriate drives are mapped and run the script,
#         redirecting its output to an axt file. e.g.
#
#         perl dir2axt.pl > myapplication.axt
#
#     4.  Import the new AXT file into an appliacation object using ConsoleOne

# Requires: File::Find, the file-finding module
use File::Find;

# Define some constants
#
# NOTES:- 1) Always use double-slashes in folder names
#         2) Do not add slashes that the end of paths
#
$src_path="Z:\\PATH\\TO\\SOURCE\\LOCATION";           # Alter these two lines
$dst_path="C:\\PATH\\TO\\INSTALLATION\\LOCATION";     # to suit your project

$src_macro="%SOURCE_PATH%";                           # These can be changed if
$target_macro="%TARGET_PATH%";                        # necessary, eg %*WINDIR%

# Save confusion with the special characters
$fwd_slash='\/';
$back_slash='\\';

# Initialise some text-holding variables
$dir_to_create="";
$file_to_create="";

# Parse the source path
find(\&sub_find, $src_path);

# $dir_to create should now hold a list of folders we need to create, and
# $file_to_create shoul be a list of files to copy

# Parse the files to correct the unixy slashes and replace the full path with
# the src_path macro

# Fix the unix<>dos directory slashes
$dir_to_create =~ s/$fwd_slash/$back_slash/g;
$file_to_create =~ s/$fwd_slash/$back_slash/g;

# Replace the full source path with the source path macro.
# Not sure why, but the search and replace operator doesn't like back-slashes
# too much, so there's a bit of a fudge here. There's probably a better way of
# doing it...

# replace paths with double-forward slashes (fudge)
$dir_to_create =~ s#\\#$\/\/\/\/#g;
$file_to_create =~ s#\\#$\/\/\/\/#g;
$src_path =~ s#\\#$\/\/\/\/#g;

# Substitute the SOURCE_PATH macro where appropriate
$dir_to_create =~ s/$src_path/$src_macro/gi;
$file_to_create =~ s/$src_path/$src_macro/gi;

# Replace the paths with what we really need (de-fudge)
$dir_to_create =~ s#\/\/\/\/#\\#g;
$file_to_create =~ s#\/\/\/\/#\\#g;
$src_path =~ s#\/\/\/\/#\\#g;

# Output the information in AXT-format text

print "AXT_FILE 3.1\n\n";                             # Change this if required
print "[Macro]\n";
print "Name=SOURCE_PATH\n";
print "Value=$src_path\n\n";

print "[Macro]\n";
print "Name=TARGET_PATH\n";
print "Value=$dst_path\n\n";

# Write out folder entries
@line=split(/\n/,$dir_to_create);
foreach $l (@line) {
  # Replace source path with target path
  $l =~ s/$src_macro/$target_macro/g;
  print "[Directory Create]\n";
  print "Flag=Write Always\n";
  print "Directory=$l\n\n";
  }

# Write out file entries
@line=split(/\n/,$file_to_create);
foreach $l (@line) {
  print "[File Copy]\n";
  print "Flag=Update Create\n";
  print "Source=$l\n";
  # Replace source path with target path
  $l =~ s/$src_macro/$target_macro/g;
  print "Target=$l\n\n";
  }

# Finished. Use ConsoleOne to import the new AXT file
exit;

# Subroutines and functions
sub sub_find {
  # Find files and folders
  my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $therest) = stat($_);
  
  # Add discovered directories to the list to create
  $dir_to_create.="$File::Find::name\n" if (-d $_);
  
  # Add discovered files to the list to create
  $file_to_create.="$File::Find::name\n" if (-f $_);
  }

