#!/usr/bin/perl 
# http://twitter.com/horipiri

# avoid long QUERY_STRING and open if possible
if (length($ENV{'QUERY_STRING'}) > 32){die "Too long query string. \n"};
open(IN, "$ENV{'QUERY_STRING'}") or die "Can't open file.\n $ENV{'QUERY_STRING'} : $!\n";

# initialize variables
# display update time on the top if the list.
$prev_v  = "Last update: " . localtime((stat($ENV{'QUERY_STRING'}))[9]) ;
$prev_d  = 0; # directory depth
$div_num = 0; # id for <div>
$lst_cnd = 0; # last condition

# print header
&print_header();

# process each line
foreach (<IN>) {
    # calculate current directory depth
    $cur_d = eval(index($_,"+")/3);

    # branch by the difference b/w current and previous directory depth
    if ($cur_d==$prev_d){ # handle as file.
        $prev_v =~ s/\+\-\-/&nbsp;&nbsp;&nbsp;/;
        print $prev_v . "<br>\n";
        $lst_cnd = 0;
    }
    elsif ($cur_d>$prev_d){ # create the tags for directory folding when depth increased
        #print "<a href=\"#\" style=\"font-weight:bold;\" onClick=\"showHide(\'id" . $div_num . "\');return false;\">\n";
        print "<a href=\"#\" onClick=\"showHide(\'id" . $div_num . "\');return false;\">\n";
        #print "<em>";
        $prev_v =~ s/\+\-\-/&nbsp;&nbsp;&nbsp;■&nbsp;/; # remove "+--"
        print "$prev_v";
        #print "</em>";
        print "<br>\n</a>\n";
        print "<div id=\"id" . $div_num . "\" style=\"display: none\">";
        $div_num++; # increase id number for <div> tag
        $lst_cnd = 1;
    }
    else { # close the folding tag with </div>
        $prev_v =~ s/\+\-\-/&nbsp;&nbsp;&nbsp;/;
        print $prev_v . "<br>\n";
        for ($i=eval($prev_d-$cur_d);$i>0;$i--) {
            print "</div>";
        }
        $lst_cnd = 2;
    }

    $prev_v = $_; # replace previous line
    chomp($prev_v);
    $prev_v =~ s/[ |]/&nbsp;/g;
    $prev_d = $cur_d; # replace previous depth
}

# process for last line
$prev_v =~ s/\+\-\-/&nbsp;&nbsp;&nbsp;/;
if ($lst_cnd!=2) {
    print $prev_v;
}
for($i=$cur_d;$i>0;$i--){ # close <div> nesting
    print "</div>"
}
if ($lst_cnd==2) {
    print $prev_v;
}
print "</body></html>";

close(IN);

exit;

# subroutnie to output the header
sub print_header {
print <<"EOM";
Content-type: text/html; charset=UTF-8
Pragma: no-cache
Cache-Control: no-cache
Expires: Thu, 01 Dec 1994 16:00:00 GMT

<html><head>
<title> FTP server file tree </title>
<meta http-equiv=\"Content-Type\" content=\"text/html;CHARSET=UTF-8\">
<script language=\"JavaScript\" type =\"text/javascript\">
<!--
function showHide(targetID) { 
        if( document.getElementById(targetID)) { 
                if( document.getElementById(targetID).style.display == \"none\") {
                    document.getElementById(targetID).style.display = \"block\";
                } else { 
                    document.getElementById(targetID).style.display = \"none\";
                }
        }
}
// --> </script>

<style type=\"text/css\">
<!--
A { text-decoration: none; }
-->
</style>

</head>
<body>
EOM
}