package AutodocSupport::FileSupport;

###############################################################################
###############################################################################
##
##    Written by Adam Swift (c) 1995 by Information Technology Solutions
##                           All rights reserved.
##
##	  This notice may not be removed from this source code.
##
##    This program is included in the MiscKit by permission from the author
##    and its use is governed by the MiscKit license, found in the file
##    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
##    for a list of all applicable permissions and restrictions.
##
##    Because the AutoDoc is licensed free of charge, there is no warranty 
##    for the program.  Copyright holder, Information Technology Solutions, 
##    is providing this program "as is" and this program is distributed in 
##    the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
##    even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
##    PARTICULAR PURPOSE.
##
###############################################################################
###############################################################################

require 5.000;
require Exporter;

@ISA      = qw(Exporter);
@EXPORT   = qw(file_expandpath
	       file_findnewest
	       filesupport_version);

$module_version = '$Revision: 1.0 $';
$module_version =~ s!(\$\w+: | \$)!!g;
$module_id	= '$Id: FileSupport.pm,v 1.0 1995/07/28 16:01:59 aswift Exp $';
$module_id      =~ s!(\$\w+: | \$)!!g;
$module_name	= $module_id;
$module_name	=~ s!^([^\,]+).*$!$1!;

#############################################################################
#
# NAME:       module_version
#
# ACTION:     returns the version number of this module
#
# RETURN:     the module version
#
#############################################################################
sub module_version
{
    return "$module_name (rev-$module_version)";
}

##
# Stupid hack!
##
sub filesupport_version
{
    return module_version();
}

#############################################################################
#
# NAME:       expand_filepath
#
# ACTION:     Replace the file path '~[user]' '.' or '..' references 
#             with an expanded file path starting with '/'
#
# ARGUMENTS:  A file path
#
# RETURN:     The expanded file path
#
#############################################################################
sub file_expandpath
{
    local ($filepath, $expanddir);
    $filepath = $_[0];

    # If the path starts with '/' no expansion is needed.
    return $filepath if ($filepath =~ m!^/!);

    # If the path starts with '~' then user HOME from the user ENV
    if (($filepath =~ m!^~$!) || ($filepath =~ m!^~/!)) {
	$filepath =~ s!^~!$ENV{'HOME'}!;
	return $filepath;
    }


    # Otherwise, get lazy and make the filesystem to expand it for us
    if ($filepath =~ m!^([^/]+)!) {
	$expanddir = $1;
	
	# Check to see if the path is simply a file name (no directories)
	# if so, add a './' and expand from there.
	if (($expanddir eq $filepath) && !(-d $expanddir)) {
	    $filepath = "./$filepath";
	    $expanddir = ".";
	}

	if ($expanddir ne "") {
	    open (EXPDIR, "cd $expanddir; pwd |");
	    $expanddir = <EXPDIR>;
	    close (EXPDIR);
	    chop $expanddir if ($expanddir =~ m!\n$!);
	    
	    # if it is a directory, replace it in $filepath
	    if (($expanddir ne "") && (-d $expanddir)) {
		$filepath =~ s!^[^/]+!$expanddir!;
	    }
	}
    }
    
    return $filepath;
}


#############################################################################
#
# NAME:       file_findnewest
#
# ACTION:     Finds the newest file of all files passed as arguments and 
#             returns it (the one with the latest time stamp).
#
# ARGUMENTS:  Any number of file paths
#
# RETURN:     The newest file's path
#
#############################################################################
sub file_findnewest
{
    local ($newest_file, $newest_age, $file, $fileage);

    $newest_file = "";
    $newest_age = 0;

    foreach $file (@_) {
	if (-r $file) {
	    $fileage = -M $file;
	    if ($fileage > $newest_age) {
		$newest_file = $file;
		$newest_age  = $fileage;
	    }
	}
    }
    return $newest_file;
}


1;
    
