#!/usr/bin/perl

use strict;
use warnings;

my @files = split("\n", `find .`);
my $last_mod = 0;
my $f_mod = 0;
foreach my $file ( @files ) {
       my $mod_time = `stat $file | grep Modify | awk '{print \$2" "\$3}'`;
       chomp $mod_time;
       my $c = $mod_time;
       $c =~ tr/-\://d;
       $c =~ tr/ //d;
       if ( $c > $last_mod ) {
               $last_mod = $c;
               $f_mod = $mod_time;
       }
}

sub colorize($) {
       my $string = shift;

       # For "colorizing" the page
       my @fade_in_c_arr = (
               142,143,144,145,146,240,244,250,
       );

       my $fade_len = @fade_in_c_arr;

       my @input_char_arr = split("", $string);

       my @colored_char_ar;
       foreach my $char ( @input_char_arr ) {
               if ( $char eq "\n" || $char eq " " || $char eq '\\' || $char eq '/' ) {
                       push(@colored_char_ar,$char);
                       next;
               }
               my $c = $fade_in_c_arr[rand @fade_in_c_arr];
               push(@colored_char_ar, "\x1B[38;5;$c" . "m" . "$char" . "\x1B[0m");
       }

       my $c_string = join("",@colored_char_ar);
       return $c_string;

}

sub printer($$$) {
       my $string = shift;
       my $type = shift;
       my $rest = shift;
       if ( defined $ENV{'QUERY_STRING'} && $ENV{'QUERY_STRING'} eq "plain" ) {
               print("$type$string$rest\n");
       } else {
               my $c_string = colorize($string);
               print("$type$c_string$rest\n");
       }
}

my $HOME_PATH = "/";
if ( defined $ENV{'QUERY_STRING'} && $ENV{'QUERY_STRING'} eq "plain" ) {
       $HOME_PATH = "/foobar.cgi?plain";
}

# GOPHERMAP GEN HERE
if ( defined $ENV{'QUERY_STRING'} && $ENV{'QUERY_STRING'} eq "plain" ) {
       print("i                        CHATE.IO\tchate.io\t70\n");
} else {
       print("i ▄████▄   ██░ ██  ▄▄▄     ▄▄▄█████▓▓█████       ██▓ ▒█████\tchate.io\t70\n");
       print("i▒██▀ ▀█  ▓██░ ██▒▒████▄   ▓  ██▒ ▓▒▓█   ▀      ▓██▒▒██▒  ██▒\tchate.io\t70\n");
       print("i▒▓█    ▄ ▒██▀▀██░▒██  ▀█▄ ▒ ▓██░ ▒░▒███        ▒██▒▒██░  ██▒\tchate.io\t70\n");
       print("i▒▓▓▄ ▄██▒░▓█ ░██ ░██▄▄▄▄██░ ▓██▓ ░ ▒▓█  ▄      ░██░▒██   ██░\tchate.io\t70\n");
       print("i▒ ▓███▀ ░░▓█▒░██▓ ▓█   ▓██▒ ▒██▒ ░ ░▒████▒ ██▓ ░██░░ ████▓▒░\tchate.io\t70\n");
       print("i░ ░▒ ▒  ░ ▒ ░░▒░▒ ▒▒   ▓▒█░ ▒ ░░   ░░ ▒░ ░ ▒▓▒ ░▓  ░ ▒░▒░▒░\tchate.io\t70\n");
       print("i  ░  ▒    ▒ ░▒░ ░  ▒   ▒▒ ░   ░     ░ ░  ░ ░▒   ▒ ░  ░ ▒ ▒░\tchate.io\t70\n");
       print("i░         ░  ░░ ░  ░   ▒    ░         ░    ░    ▒ ░░ ░ ░ ▒\tchate.io\t70\n");
       print("i░ ░       ░  ░  ░      ░  ░           ░  ░  ░   ░      ░ ░\tchate.io\t70\n");
       print("i░                                           ░\tchate.io\t70\n");
       print("i\tchate.io\t70\n");
}
printer("        _.-^ Thanks for visiting The Chateau ^-._","i","\tchate.io\t70");
print("i\tchate.io\t70\n");
printer("   Last Modification: $f_mod UTC","i","\tchate.io\t70");
my $uniq_ips = `cat ./existing_ips | wc -l`;
chomp $uniq_ips;
printer("             Uniq Visitors since Jan 2022 : $uniq_ips","i","\tchate.io\t70");
print("i\tchate.io\t70\n");
printer("About Me","0","\tblogs/about-me.org.txt\tchate.io\t70");
printer("Ramblings and Phlog","1","\t/blogs/blogs.cgi\tchate.io\t70");
print("i --> All posts are .org mode formatted,\tchate.io\t70\n");
print("i     so elpher users can use M-x org-mode\tchate.io\t70\n");
print("i     for nicer formatting\tchate.io\t70\n");
print("i\tchate.io\t70\n");
printer("SpeskTV (Inspired by baud.baby baudVision)","i","\tchate.io\t70");
printer("  --> Connect via VLC or MPV at http://spwhome.tplinkdns.com:29327","i","\tchate.io\t70");
printer("Warez","0","\t/warez.txt\tchate.io\t70");
print("i\tchate.io\t70\n");
printer("Cool Places","i","\tchate.io\t70");
printer("  --> baud.baby","1","\t/\tbaud.baby\t70");
printer("  --> hngopher.com (HN Gopher Proxy)","1","\t/\thngopher.com\t70");
printer("  --> Linkerror","1","\t/\tgopher.linkerror.com\t70");
printer("  --> gopher.black","1","\t/\tgopher.black\t70");
printer("  --> tilde.club","1","\t/\ttilde.club\t70");
printer("  --> RPoD (1436.ninja)","1","\t/\t1436.ninja\t70");
printer("  --> gopherpedia (Wikipedia Gopher Proxy)","1","\t/\tgopherpedia.com\t70");
printer("  --> thelambdalab.xyz","1","\t/\tthelambdalab.xyz\t70");
print("i\tchate.io\t70\n");
printer("Git Proxy","1","\t/git/go_git.cgi\tchate.io\t70");
print("i  --> This takes you to a CGI script that will proxy\tchate.io\t70\n");
print("i      HTTP requests to my git server, so that you can\tchate.io\t70\n");
print("i      browse my git repos\tchate.io\t70\n");
print("i      --> Note: Some clients won't render certain file exts,\tchate.io\t70\n");
print("i          which may cause some repos to appear empty.\tchate.io\t70\n");
print("i\tchate.io\t70\n");
printer("Mastodon Proxy","0","\t/masto/go_mast.cgi\tchate.io\t70");
print("i  --> Proxy of what I'm posting on Mastodon\tchate.io\t70\n");
print("i\tchate.io\t70\n");
printer("My Music","0","\t/music.txt\tchate.io\t70");
print("i\tchate.io\t70\n");
printer("Script that generates this page","0","\t/foobar.txt\tchate.io\t70");
print("i\tchate.io\t70\n");
print("1Plain\t/foobar.cgi?plain\tchate.io\t70\tchate.io\t70\n");
print("1Colorized\t/\tchate.io\t70\tchate.io\t70\n");
print("iIf the site looks weird, use the Plain entry above\tchate.io\t70\n");
print("i  --> Sometimes info lines are filtered in some clients\tchate.io\t70\n");
print("i      when using the ?plain query, I'm not sure why yet\tchate.io\t70\n");
print("i\tchate.io\t70\n");
print("iThis gopher site expects a client that either supports or\tchate.io\t70\n");
print("ifilters ANSI escape sequences. I recommend:\tchate.io\t70\n");
print("i--> Elpher\tchate.io\t70\n");
print("i--> cgo\tchate.io\t70\n");
print("i--> phetch\tchate.io\t70\n");
print("i--> lagrange\tchate.io\t70\n");
print("i--> castor\tchate.io\t70\n");
print("i--> curl (use gopher://)\tchate.io\t70\n");