#!/usr/local/bin/perl
#  File:
#	info.cgi
#  Date:
#	July 6, 2000
#  Author:
#	Philip Putnam, based on code by Alex Krohn
#  Description:
#	Builds a dynamic details page in place of wasting valuable disk space.
#	On the other hand, now ther're potential bandwidth problems :-)
#  Comments:
#	This script was written as a replacement for detail.cgi by Jerry Su.
#	It consists mostly of code snippets from jump.cgi, modify.cgi, and
#	nph-build.cgi.	The last few pieces I figured out on my own.  Unlike
#	Jerry's version, mine loads up an error page instead of redirecting to
#	the root url or	a blank page.
#  Support:
#	Since I'm not a perl expert, I can't claim this script is flawless.
#	Please post your questions or comments regarding this script to the
#	Gossamer-Threads Links 2.0 Customization forum.
# =====================================================================
#
#   Setup Notes:
#       Make sure the require statement below points to the config file.    

# Required Librariers
# --------------------------------------------------------
eval {
    ($0 =~ m,(.*)/[^/]+,)   && unshift (@INC, "$1");    # Get the script location: UNIX /
    ($0 =~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1");    # Get the script location: Windows \

    require "admin/links.cfg";              # Change this to full path to links.cfg if you have problems.
    require "$db_lib_path/db_utils.pl";
    require "$db_lib_path/links.def";
    $build_use_templates ?
        require "$db_lib_path/site_html_templates.pl" :
        require "$db_lib_path/site_html.pl";
};
if ($@) {
    print "Content-type: text/plain\n\n";
    print "Error including libraries: $@\n";
    print "Make sure they exist, permissions are set properly, and paths are set correctly.";
    exit;
}

# ========================================================

eval { &main; };                            # Trap any fatal errors so the program hopefully 
if ($@) { &cgierr("fatal error: $@"); }     # never produces that nasty 500 server error page.
exit;   # There are only two exit calls in the script, here and in in &cgierr. 

sub main {
# --------------------------------------------------------  
  %in = &parse_form();

  my (%rec) = &get_record ($in{'ID'});

  unless ($in{'ID'}) {
    &html_print_headers;
    print &site_html_detailed_error ("No link specified."); #future versions will load a form instead of an error
  }
  elsif ($rec{$db_key} eq $in{'ID'}) {
    $title_unlinked = &build_unlinked_title ("$rec{'Category'}/$rec{'Title'}");
    $title_linked = &build_linked_title ("$rec{'Category'}/$rec{'Title'}");
    &html_print_headers;
    print &site_html_detailed (%rec);
  }
  else {
    &html_print_headers;
    print &site_html_detailed_error ("Link $in{'ID'} not found.");
  }
}

###
# Can't require nph-build -- have to copy this stuff  :-)
###

sub build_linked_title {
# --------------------------------------------------------
# Returns a string of the current category broken up 
# by section, with each part linked to the respective section.

    my $input = shift;
    my (@dirs, $dir, $output, $path, $last);

    @dirs = split (/\//, $input);
    $last = &build_clean(pop @dirs);
    
    $output = qq| <A HREF="$build_root_url/">$build_site_title</A> >|;
    foreach $dir (@dirs) {
        $path .= "/$dir";
        $dir = &build_clean ($dir);
        $output .= qq| <A HREF="$build_root_url$path/">$dir</A> >|;
    }
    $output .= " $last";
    
    return $output;
}

sub build_unlinked_title {
# --------------------------------------------------------
# Returns a string of the current category broken up by section.
# Useful for printing in the title.

    my $input = shift;
    my (@dirs, $dir, $output);

    @dirs = split (/\//, $input);
    
    $output = qq|$build_site_title > |;
    
    foreach $dir (@dirs) {
        $dir = &build_clean ($dir);
        $output .= " $dir >";
    }
    chop ($output);  
    return $output;
}

###
#  End of stuff we copied from nph-build.
###

sub error {
# ------------------------------------------
#
  print "Content-type: text/html\n\n";
  print "Error: $_[0]\n";
  exit;
}

