#!/bin/sh
# @(#) /u/des/public_html/work/cvt_addrs.sh 1.5 98/10/31 11:59:52
# cvt_addrs.sh - convert address DB files to web-able form
#
# input is a file with lines like:
#
# Lastname, Firstnameemail or web addressyear
#
# blank lines and lines beginning with "#" are ignored;
#
# output is HTML code, but not a complete document;
#
# 8/97, D.Singer
PROG=`basename "$0"`
USAGE="
Usage: $PROG [-c] [-w] [-f infile]
-c sort by class (year)
-w produce web addresses output
-f input file name
default sort is by lastname
"
EMAIL_INFILE="email-addrs.db"
WEB_INFILE="web-addrs.db"
DFLT_INFILE="$EMAIL_INFILE"
INFILE=
SEP=' ' # TAB character
DO_WEB=0
CLASS_SORT=0
BGC="#339999"
while [ "$#" != 0 ]; do
case "$1" in
-c)
CLASS_SORT=1
;;
-f)
shift
INFILE="$1"
;;
-w)
DO_WEB=1
DFLT_INFILE="$WEB_INFILE"
;;
*)
echo "$PROG: option syntax error." >&2
echo "$USAGE" >&2
exit 1
esac
shift
done
[ -z "$INFILE" ] && INFILE="$DFLT_INFILE"
#
# filter out the junk
#
nawk < $INFILE -F "$SEP" '{
# print and skip comments
if ($1 ~ /^#/) {
## print;
next;
}
# skip blank lines
if ($0 ~ /^$/) {
next;
}
# print and skip labels and blank lines
if (NF != 3) {
printf(">>> |%s| <<<\n",$0) > "/dev/tty";
next;
}
print;
}' |
#
# sort by year, and produce HTML
#
if [ "$CLASS_SORT" = 1 ]; then
#
# order by class (year) field
#
#sort -df -t' ' +2n -3 +0 |
sort -t' ' -k 3n -k 3 -k 1df -k 2df |
nawk -F "$SEP" 'BEGIN {
POS = 0;
DO_WEB = "'"$DO_WEB"'";
CLASS = "";
LAST_CLASS = "";
BQ = 0;
} {
CLASS = $3;
#
# when class changes, print a new NAME label
#
if (CLASS != LAST_CLASS) {
if (BQ == 1) {
print "";
BQ = 0;
}
CLASS_LABEL = (CLASS == "???") ? "unknown" : CLASS;
#printf("\n
\n- %s -\n
\n
\n\n",CLASS_LABEL,CLASS);
printf("\n\n",CLASS_LABEL,CLASS);
print "";
BQ = 1;
LAST_CLASS = CLASS;
}
#
# print the line for email addresses
#
if (DO_WEB == 0) {
printf("%s, %s
\n",$1,$2,$2);
next;
}
#
# print the line for web addresses
#
#printf("%s, %s
\n",$1,$2,$2);
printf("%s, %s
\n",$1,$2,$2);
}
END {
if (BQ == 1)
print "
";
}'
#
# sort by lastname, and produce HTML;
# if DO_WEB, produce links for web pages instead of email addresses,
# and don't bother with the NAME targets (because there aren't many
# of these yet);
#
else
#
# order alphabetically, which is how the DB is already ordered;
# for email addresses, add NAME labels to search by letter;
#
nawk -F "$SEP" 'BEGIN {
POS = 0;
# this is how we know what the next letter should be
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
letters = "abcdefghijklmnopqrstuvwxyz";
LETTER = "";
letter = "";
DO_WEB = "'"$DO_WEB"'";
BQ = 0;
} {
#
# for email addresses, when we hit a new letter, print a
# NAME label; do this even when a letter is entirely skipped;
#
if (DO_WEB == 0) {
NEW_LETTER = substr($0,1,1);
#DID = 0;
while (NEW_LETTER != LETTER && NEW_LETTER != letter) {
++POS;
if (POS+0 > 26)
break;
if (BQ == 1) {
print "";
BQ = 0;
}
LETTER = substr(LETTERS,POS,1);
letter = substr(letters,POS,1);
#printf("\n
\n- %s -
\n",LETTER,LETTER);
printf("\n\n",LETTER,LETTER);
print "";
BQ = 1;
#DID = 1;
}
#if (DID == 1)
# printf("%s\n","
");
printf("%s, %s, %s
\n",$1,$2,$2,$3);
next;
}
#
# for web, just print a blank when theres a letter change;
#
NEW_LETTER = substr($0,1,1);
DID = 0;
while (NEW_LETTER != LETTER && NEW_LETTER != letter) {
++POS;
if (POS+0 > 26)
break;
LETTER = substr(LETTERS,POS,1);
letter = substr(letters,POS,1);
DID = 1;
}
if (DID == 1)
printf("%s\n","
");
printf("%s, %s, %s
\n",$1,$2,$2,$3);
}
END {
if (BQ == 1)
print "
";
}'
fi
exit