#!/usr/bin/perl
use warnings;
use strict;
use autodie;

# ./lighttpd-secdownload.pl ~u2001/foo.txt | xargs -i HEAD -u http://localhost{}

use Digest::MD5 qw(md5 md5_hex md5_base64);
use Data::Dump qw(dump);

my $config;

open(my $secdownload_conf, '<', '/etc/lighttpd/conf-enabled/90-secdownload.conf');
while(<$secdownload_conf>) {
	chomp;
	my ( $n, $v ) = split(/\s+=\s+/);
	$v =~ s/^"//;
	$v =~ s/"$//;
	$config->{$n} = $v;
}

warn "# config ",dump($config);

foreach my $path ( @ARGV ) {

	$path =~ s{^.*?(/\d\d\d\d/)}{$1}; # cleanup path prefix

  	my $t_hex = sprintf('%08x', time() );
	my $md5_hex = md5_hex($config->{'secdownload.secret'}.$path.$t_hex);

	my $link = $config->{'secdownload.uri-prefix'} . $md5_hex . '/' . $t_hex . $path;
	print $ENV{HTML} ? qq|<a href=$link>$path</a>\n| : "$link\n";

}


