#!/bin/sh
#####################################################################
# tirph - Generate TIR gophermap from file system
# Created 2010-09-02 by David Meyer <
[email protected]> +JMJ
#####################################################################
# Identification ####################################################
PROGRAM='tirph'
VERSION=0.0
COPYRIGHT='Copyright (C) 2010 David Meyer'
DESCRIPTION='Generate TIR gophermap from file system'
USAGE="Usage: $PROGRAM [-CPVh] [-n NUM] [-o OUTFILE] [-p PHLOGDIR] [DIR]"
CONTACT='David Meyer <
[email protected]>'
# Initialize environment ############################################
IFS="
"
unset PATH
BASENAME=/usr/bin/basename
CAT=/bin/cat
CHMOD=/bin/chmod
DIRNAME=/usr/bin/dirname
ECHO=/bin/echo
FIND=/usr/bin/find
FORTUNE=/usr/games/fortune
GREP=/usr/bin/grep
HEAD=/usr/bin/head
LS=/bin/ls
MV=/bin/mv
SED=/usr/bin/sed
TOUCH=/usr/bin/touch
SERVER=sdf.org
PORT=70
NEXUSSEL=/users/$USER
NEXUSPATH=/ftp/pub/users/$USER
#NEXUSPATH=/home/zaurus/cave/green
# Arguments #########################################################
while getopts CPn:o:p:Vh option; do
case $option in
C ) NOCONTENTS=1 ;;
P ) NOPHLOG=1 ;;
n ) NUM=$OPTARG ;;
o ) OUTFN=$OPTARG ;;
p ) PHLOGDIR=$OPTARG ;;
V ) $ECHO "$PROGRAM $VERSION"
$ECHO $COPYRIGHT
exit 0
;;
h ) $CAT << ENDHELP
$USAGE
$DESCRIPTION.
DIR ...
Options are as follows:
-p PHLOGDIR
-n NUM
-o OUTFN
-C Suppress output of directory contents section.
-P Suppress output of phlog section.
-V Display version number
-h Display this help message
Files:
DIR/.title
DIR/.descripton
DIR/.gophhead
DIR/.gophfoot
DIR/.exits
Report bugs to $CONTACT.
ENDHELP
exit 0
;;
* ) $ECHO $USAGE >&2
exit 1
;;
esac
done
shift $(( $OPTIND-1 ))
DIR=${1:-""}
: ${NUM:=10}
: ${OUTFN:=gophermap}
: ${PHLOGDIR:=""}
# Functions #########################################################
# ftitle() - Return title for file or subdirectory
ftitle() {
bn=$($BASENAME $1)
if [ -d $1 ]; then
if [ -f $1/.title ]; then
ftitle=$($CAT $1/.title)
else
ftitle=$bn
fi
else
fn=${bn%.*}
fext=${bn##*.}
case $fext in
txt | org )
ftitle=$($HEAD -n 1 $1)
;;
html | htm | shtml )
ftitle=$($GREP -i "\<h1\>" $1 | $HEAD -n 1 | $SED -e "s;</*[hH]1>;;g")
: ${ftitle:=$fn}
;;
* )
ftitle=$bn
;;
esac
fi
$ECHO $ftitle
}
# gtype() - Return Gopher item type
gtype() {
if [ -d $1 ]; then
gtype=1
else
fext=${1##*.}
case $fext in
txt | org ) gtype=0 ;;
html | htm | shtml ) gtype=h ;;
jpg | jpeg | png ) gtype=I ;;
gif ) gtype=g ;;
* ) gtype=0 ;;
esac
fi
$ECHO $gtype
}
# Main driver #######################################################
if [ "$DIR" ]; then
DIRPATH=$NEXUSPATH/$DIR
DIRSEL=$NEXUSSEL/$DIR
else
DIRPATH=$NEXUSPATH
DIRSEL=$NEXUSSEL
fi
OUTFILE=$DIRPATH/$OUTFN
# Backup output file ...
if [ -f $OUTFILE ]; then
$MV $OUTFILE $OUTFILE~
$TOUCH $OUTFILE
$CHMOD 750 $OUTFILE
fi
# Directory title ...
if [ -f $DIRPATH/.title ]; then
$CAT $DIRPATH/.title >>$OUTFILE
$ECHO "" >>$OUTFILE
fi
# Directory description ...
if [ -f $DIRPATH/.description ]; then
$CAT $DIRPATH/.description >>$OUTFILE
$ECHO "" >>$OUTFILE
fi
# Link to nexus directory (top) ...
if [ "$DIR" ]; then
$ECHO "1(to nexus) $NEXUSSEL $SERVER $PORT" >>$OUTFILE
$ECHO "" >>$OUTFILE
fi
# Gopher-specific header ...
if [ -f $DIRPATH/.gophhead ]; then
$CAT $DIRPATH/.gophhead >>$OUTFILE
fi
# Phlog ...
if [ -z "$NOPHLOG" ]; then
if [ "$PHLOGDIR" ]; then
PHLOGPATH=$NEXUSPATH/$PHLOGDIR
PHLOGSEL=$NEXUSSEL/$PHLOGDIR
else
PHLOGPATH=$NEXUSPATH
PHLOGSEL=$NEXUSSEL
fi
$LS -lt $($FIND $PHLOGPATH -type f -name "[^\.]*") | $GREP -Ev "~$|#.*#$|gophermap$" | $HEAD -n $NUM | while read perm lc own gr sz t1 t2 t3 fn; do
if [ -z "$phloginit" ]; then
$ECHO "" >>$OUTFILE
$ECHO "RECENT ACTIVITY:" >>$OUTFILE
phloginit=1
fi
ft=$(ftitle $fn)
loc=$($BASENAME $($DIRNAME $fn))
$ECHO "0$t1 $t2 $t3 $ft [$loc] $PHLOGSEL${fn#*/}" >>$OUTFILE
done
fi
# Directory contents ...
if [ -z "$NOCONTENTS" ]; then
$LS $DIRPATH | $GREP -Ev "~$|^#.*#$|^gophermap$" | while read fn; do
if [ -z "$continit" ]; then
$ECHO "" >>$OUTFILE
$ECHO "YOU SEE HERE:" >>$OUTFILE
continit=1
fi
type=$(gtype $DIRPATH/$fn)
title=$(ftitle $DIRPATH/$fn)
$ECHO "$type$title $DIRSEL/$fn $SERVER $PORT" >>$OUTFILE
done
fi
# Exits ...
if [ -f $DIRPATH/.exits ]; then
$CAT $DIRPATH/.exits | while read exit tag; do
if [ -z "$exitinit" ]; then
$ECHO "" >>$OUTFILE
$ECHO "OBVIOUS EXITS:" >>$OUTFILE
exitinit=1
fi
if [ "$exit" ]; then
title=$(ftitle $NEXUSPATH/$exit)
if [ "$tag" ]; then
title="($tag) "$title
fi
$ECHO "1$title $NEXUSSEL/$exit $SERVER $PORT" >>$OUTFILE
fi
done
fi
# Gopher-specific footer ...
if [ -f $DIRPATH/.gophfoot ]; then
$ECHO "" >>$OUTFILE
$CAT $DIRPATH/.gophfoot >>$OUTFILE
fi
# Link to nexus directory (bottom) ...
if [ "$DIR" ]; then
$ECHO "" >>$OUTFILE
$ECHO "1(to nexus) $NEXUSSEL $SERVER $PORT" >>$OUTFILE
fi
# Fortune cookie ...
if [ -x $FORTUNE ]; then
$ECHO "" >>$OUTFILE
$FORTUNE >>$OUTFILE
fi
exit 0