#!/bin/sh # # @(#) mkmenus 1.17 95/05/23 01:10:26 # # mkmenus - Construct a set of "gn" menus for a directory tree. # # Edit at tabstops=4. # # Chip Rosenthal # Unicom Systems Development # # http://www.unicom.com/ # USAGE="usage: $0 [-rc] [-d start_dir] [-f footer_name] [-m menu_name] [-x exclude_file]" # # Site-specific definitions. # GN_ROOT=/pub/web # root of the web tree GN_LIB=/local/libexec/gn # where gn support files reside GN_MIMETYPES=$GN_LIB/gn_mime.types # map of document content-types MKCACHE=$GN_LIB/mkcache # path to "mkcache" command DFLT_XCLUDE='$menu_name $footer_name *~' # default exclusion list to "eval" # # Initialize. # Progname=`basename $0` menu_name="menu" # name of file to produce footer_name="menu.footer" # file to append as menu footer recurse=false # TRUE to recurse thru subdirs mkcache=false # TRUE to run "mkcache" when done xclude="" # list of glob patterns to exclude startdir= # where we are relative to GN_ROOT current_footer= # current footer file # # Crack the command line. # set X `getopt "cd:f:m:rx:" $*` if [ $? -ne 0 ] ; then echo "$USAGE" >&2 exit 1 fi shift while : ; do case "$1" in -c) mkcache=true ; shift ;; -d) startdir="$2" ; shift 2 ;; -f) footer_name="$2" ; shift 2 ;; -m) menu_name="$2" ; shift 2 ;; -r) recurse=true ; shift ;; -x) xclude="$xclude $2" ; shift 2 ;; --) shift ; break ;; *) echo "$USAGE" >&2 ; exit 1 ;; esac done if [ $# -ne 0 ] ; then echo "$USAGE" >&2 exit 1 fi # # Add in the standard things to the exclusion list. # eval xclude=\"\$xclude $DFLT_XCLUDE\" # # Build up a shell "case" command that we can "eval" to analyze the # selection specifed by "$item". This should "type" to the Gopher type # (or "SKIP" if the item should be excluded) and "content" to the MIME # Content-type for the item. # examine_item=" case \"\$item\" in `for x in $xclude ; do echo \"$x) type=SKIP content= ;;\" ; done` `awk ' BEGIN {n = 0} /^#/ {next} NF == 3 {x[$2] = sprintf(\"*.%s) type=%s content=%s ;;\", $2, $1, $3)} END {for (ext in x) print x[ext]} ' $GN_MIMETYPES` *) type=0 content= ;; esac " # # Figure out where we are relative to $GN_ROOT. This will *not* work # if there are any symlinks between us and $GN_ROOT, in which case # you'll need to use "-d" to specify the starting directory. # if [ "X$startdir" = "X" ] ; then startdir=`pwd` rootdir=`cd $GN_ROOT ; pwd` if [ "$startdir" = "$rootdir" ] ; then startdir="." else startdir=`expr "$startdir" : "${rootdir}/\\(.*\\)"` fi fi # # Build a banner to stuff at the top of the menu files. # u=${LOGNAME-${USER-"?unknown?"}} h=`( hostname || uuname -l || uname -n || echo "?oopsvax?" ) 2>/dev/null` banner="\ # Created by $u@$h on `date` # with the \"$0\" command. " mkmenu() { currdir=$1 echo "$Progname: building $currdir/$menu_name ..." 1>&2 echo "$banner" # # See if there is a footer file in this directory. # We want to load it in now in case we recurse into subdirectories. # test -f "$footer_name" && current_footer=$GN_ROOT/$currdir/$footer_name # # Process all the entries in this directory. # for item in * ; do # # Calculate the path to this item from GN_ROOT. # if [ "$currdir" = "." ] ; then p=$item else p=$currdir/$item fi # # Examine this item. # eval "$examine_item" test "$type" = "SKIP" && continue if [ -d "$item" ] ; then type=1 content= title="Directory $item" $recurse && ( cd $item && mkmenu $p >$menu_name ) else case "$item" in *.html) title=`sed -n -e '/<[Tt][Ii][Tt][Ll][Ee]>/{ s![ ][ ]*! !g s!.*<[Tt][Ii][Tt][Ll][Ee]> *!! s! *.*!! p q }' $item` ;; *) title= ;; esac fi # # Emit the menu entry for item. # if [ "X$title" != "X" ] ; then echo "Hname=$title" else echo "Name=$item" fi echo "Path=$type/$p" test "X$content" != "X" && echo "ContentType=$content" echo "" done # # See if there is a footer that should be added to the menu. # test -f "$current_footer" && cat $current_footer } mkmenu $startdir >$menu_name if $mkcache ; then if $recurse ; then $MKCACHE -m $menu_name -r else $MKCACHE -m $menu_name fi fi