#!/usr/bin/perl -w
#
# $Revision: 1.4 $ $Date: 2012-07-26 17:11:48-04 $
# $Source: /home/vogelke/bin/RCS/path2uri,v $
# $Host: sys7.com $
# $UUID: 78bf0dd4-8f05-33dd-bcac-1207da4786fb $
#
#< path2uri: encode a pathname as a local URI.

use Cwd 'realpath';
use strict;

if (@ARGV) {
    foreach (@ARGV) {
        encode($_);
    }
} else {
    while (<>) {
        chomp;
        encode($_);
    }
}

exit(0);

sub encode {
    my $path = realpath(shift);

    # Had to add '/' to the list of safe characters,
    # or the first slash would be encoded.

    $path =~ s/([^a-z0-9\/\-_.!~*'()+])/sprintf "%%%02X", ord($1)/gei;
    print "file://$path\n";
}
