# Copyright (c) 2004  Joshua Hoblitt
#
# $Id: Util.pm,v 1.11 2008-03-20 23:21:58 jhoblitt Exp $

package Nebulous::Util;

use strict;
use warnings FATAL => qw( all );

our $VERSION = '0.01';

use base qw( Exporter );

use File::Spec::Functions;
use Log::Log4perl qw( :levels );
use URI::file;
use URI;

my @symbols = qw(
    %LEVELS
    _nuke_file
    _get_file_path
    _get_filehandle
    _open_uri
    parse_neb_key
);

our @EXPORT_OK      = @symbols;
our %EXPORT_TAGS    = ( standard => [ @symbols ] );

our %LEVELS = (
    off     => $OFF,
    fatal   => $FATAL,
    error   => $ERROR,
    warn    => $WARN,
    info    => $INFO,
    debug   => $DEBUG,
    all     => $ALL,
);

sub _nuke_file {
    my $path = shift;

    die "can't unlink file $path: it's a directory" 
        if -d $path;
    die "can't unlink file $path: it doesn't exist"
        unless -e $path;
    unlink $path or die "can't unlink file $path: $!";

    return 1;
}

sub _get_file_path {
    my $uri = shift;

    my $path = URI->new( $uri )->path;

    return $path;
}

sub _get_filehandle {
    my ( $path, $flags ) = @_;

    die "can't open file $path: file doesn't exist" 
        unless -e $path;
    CORE::open( my $fh, $flags, $path )
        or die "can't open file $path: $!";

    return $fh;
}

sub _open_uri {
    my ( $uri , $flags ) = @_;

    $uri = URI->new("$uri");

    my $fh = _get_filehandle($uri->file, $flags);

    return $fh;
}


1;

__END__
