#!/usr/bin/perl -w

# $searchbase is directory where URL resolves (without link-xxxx)
# $hidden is inaccessible dir with permanent symlinks

$searchbase = "/www/webl/default";
$hidden = "/www/webl/hidden";
$time_to_live = 25 * 60;    # 25 minutes, extend to 3-4 times invocation time
$report_url = "http://www.iain.com/linkin/report.php";

# 12 bit random string without caring about integer size
#
$rand1 = int(rand(65536));
$rand2 = int(rand(65536));
$rand3 = int(rand(65536));

$link = sprintf("link-%04x%04x%04x", $rand1, $rand2, $rand3);

symlink($hidden, "$searchbase/$link");

opendir(DIR, $dir = "$searchbase") or die "$dir: $!";
@links = readdir(DIR);
closedir DIR;
@links = grep(/^link-/, @links);

$now = time;

foreach $test (@links) {
   $testfile = "$searchbase/$test";
   @vals = lstat($testfile);
   # modification time is [9]
   if (($now - $vals[9]) > $time_to_live) {
      unlink $testfile;
   }
}

$cmd = "wget -q -O - $report_url?link=$link";

system("$cmd > /dev/null");
