#!/usr/net/bin/perl
# Mklist version 0.1
# Usage: mklist [-rtbg] [-R rootdir] [-h host] dir
# -r means recursively do subdirectories
# -t include all text files
# -b include binary and text files
# -g full text grep search of files rather than title only
# dir is starting directory relative to root (if empty use root)

# This program produces a file consisting of lines of the form
# title<tab>url\n   or   title<tab>url<tab>path2file\n 
# if the -g option is used.  It assumes that the only address translation
# from a  URL to the file path is prepending the root directory.

# This program has been placed in the public domain by its author
# John Franks

# You must edit the following fields filling in you root directory and host.

$host="host.univ.edu";
$rootdir="/data/root";

##########################################################################
require "getopts.pl";
&Getopts( "rtbgR:h:");
$opt_t |= $opt_b;  #Option -b implies option -t

$host= $opt_h ? $opt_h : $host;
$rootdir= $opt_R ? $opt_R : $rootdir;
$startdir=$ARGV[0];

&dodir($startdir);

sub dodir {
	local($dir) = @_;
	local($path) = $rootdir.$dir;
	local($file);
	opendir( DIR, $path) || die "Can't open $path";
	local( @filenames) = readdir(DIR);
	closedir(DIR);

	foreach $file (@filenames) {
		next if ($file =~ /^\./);
		if ( $file =~ /\.html$/) {
			&do_file( $dir, $file, "h");
		} elsif ( $opt_t && (-T "$path/$file")) {
			&do_file( $dir, $file, "t");
		} elsif ( $opt_b && (-B "$path/$file")
				&& !(-d "$path/$file")) {
			&do_file( $dir, $file, "b");
		}
		&dodir( "$dir/$file") if (( -d "$path/$file") && $opt_r );
		
	}
}

sub do_file {
	local( $dir, $file, $type) = @_;
	local( $filepath) = $rootdir.$dir."/".$file;

	if ( $type eq "b" ) {
		$title = "Binary file: $file";
	} elsif ( $type =~ /[ht]/ ) {
		open( TEXT_FILE, $filepath)
				|| die "Can't open $filepath\n";
		local( $line) = "";
		local( $linenum) = 1;
		$title = "Text file: $file";
		$foundtitle = 0;
		while ( $line = <TEXT_FILE>) {
			last if ($linenum > 5);
			$linenum++;
			if ($line =~ s/^.*<title>//i ) {
				chop( $line);
				$line =~ s!</title>.*$!!i;
				$title = $line;
				$title =~ s/^\s*//;
				$title =~ s/\s*$//;
				$foundtitle = 1;
				last;
			}
		}
		close( TEXT_FILE);
	}
	if ( ($type =~ /[bt]/) || $foundtitle) {
		if ( $opt_g ) {
			printf( "%s\thttp://%s%s/%s\t%s\n",
				$title, $host, $dir, $file, $filepath);
		} else {
			printf( "%s\thttp://%s%s/%s\n",
				$title, $host, $dir, $file);
		}
	}
}
