#!/usr/bin/perl -w

use strict;
use CGI qw(:standard);

my $num = param('num');

$num =~ /(\d*)(\/(\d*)\#*)?/;
my($topic,$post) = ($1,$3);

if (-e $topic.".txt") {
        open(IN, $topic.".txt") || die "Cant open file $topic";
                my @list = <IN>;
        close(IN) || die "Cant close file $topic";

	$topic = $list[0]; $topic =~ /(\d*)/; $topic=$1;
        if($post && ($post<scalar(@list))) { $post=$list[$post+1]; $post =~ /(\d*)/; $post=$1;} else { $post = ''; }

        if($post) {
                print "Location: http://forum.ifiction.ru/viewtopic.php?pid=$post#p$post\n\n";
        } else {
                print "Location: http://forum.ifiction.ru/viewtopic.php?id=$topic\n\n";
        }

} else {
        print "Location: http://forum.ifiction.ru\n\n";
}
