#!/usr/bin/perl
##############################################################################
# Download Counter (LOAD)                Version 3.5                         #
# Copyright 1998-99, UtilMind Solutions  info@utilmind.com                   #
# 				         http://www.utilmind.com             #
# Created 3.XII.1998                     Last Modified 9.V.1999              #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1998-99 UtilMind Solutions.  All Rights Reserved.                #
#                                                                            #
# This program is being distributed as shareware. It may be used and         #
# modified free of charge for personal, academic, government or non-profit   #
# use, so long as this copyright notice and the header above remain intact.  #
# Any commercial use should be registered. Please also send us an email,     #
# and let us know where you are using this script. By using this program     #
# you agree to indemnify UtilMind Solutuons from any liability.              #
#                                                                            #
# Selling the code for this program without prior written consent is         #
# expressly forbidden.  Obtain permission before redistributing this         #
# program over the Internet or in any other medium.  In all cases            #
# copyright and header must remain intact.                                   #
#                                                                            #
# Please check the README file for full details on installation              #
# and registration.                                                          #
##############################################################################
# Set Variables

# This is the counter log filename
$databasefile = 'download.txt';
# This is your main URL path
$YourURL = 'http://s-golos.ru';

##########################################################
# No changes need to be made after these lines
##########################################################
$originallink = "$ENV{'QUERY_STRING'}";

# v3.5 - Get remote file
if ((substr($originallink, 0, 7) eq 'http://') ||
    (substr($originallink, 0, 6) eq 'ftp://')) {
 $link = $originallink;
}
else {
 $link = "$YourURL/$originallink";       # YourURL + Link to file
}

open(DB,"+<$databasefile") || die $!;
flock(DB,2);                  # Locking file
seek(DB,0,0);
@DATA = <DB>;
$SIZE = @DATA;

$i = 0;
while ($i <= $SIZE) {
   ($counter,$oldlink) = split(/`/,$DATA[$i]);
   chop($oldlink);
   if ($oldlink eq $originallink) {
     $counter++;
     $DATA[$i] = "$counter`$originallink\n";
     goto LocateLink;
   }
   $i++;
}

push @DATA, "1`$originallink\n";

LocateLink:

@DATA = sort {($b =~ /(\d+)/)[0] <=> ($a =~ /(\d+)/)[0]} @DATA;

seek(DB,0,0);
print(DB @DATA);
truncate(DB,tell(DB));
flock(DB, 8);                # Unlocking file
close(DB);

print "Location: $link\n\n";
