#!/sbin/sh
#Tag 0x00000f00
#
# Start DynaWeb server
#
# $Revision: 1.5 $
#

IS_ON=/sbin/chkconfig
WWWD_DIR=/usr/lib/Insight/dweb
WWWD=dynaweb

LIBRARY=/usr/share/Insight/library

config_dynaweb() {
    DWEB_ROOT=$WWWD_DIR/servroot
    HOSTNAME=`/usr/etc/configmail get mycanonname`

    # in case config_dynaweb gets called multiple times...
    X=""

    if [ -d $LIBRARY ]; then
	cd $LIBRARY
	for metashelf in *
	do
	    cd $metashelf
	    for bookshelf in *
	    do
		# if there are any books in the bookshelf
		if [ -n "`grep -i 'BOOK NAME' $LIBRARY/$metashelf/$bookshelf/booklist.txt 2> /dev/null`" ]; then
		    if [ "$metashelf" = "SGI_bookshelves" -a $bookshelf != "Help" ]; then
		        if [ ! -l $DWEB_ROOT/$bookshelf ]; then
			    ln -s $LIBRARY/$metashelf/$bookshelf $DWEB_ROOT/$bookshelf
		        fi
		        if [ "$X" = "" ]; then
			    X="@$bookshelf=$bookshelf"
		        else
			    X="$X;@$bookshelf=$bookshelf"
		        fi
		    elif [ "$metashelf" != "SGI_bookshelves" -a $bookshelf != "Help" ]; then
		        newshelf=`echo $metashelf | sed "s/bookshelves//"`
		        if [ ! -l $DWEB_ROOT/$newshelf$bookshelf ]; then
			    ln -s $LIBRARY/$metashelf/$bookshelf $DWEB_ROOT/$newshelf$bookshelf
		        fi
		        if [ "$X" = "" ]; then
			X="@$newshelf$bookshelf=$newshelf$bookshelf"
		        else
			    X="$X;@$newshelf$bookshelf=$newshelf$bookshelf"
		        fi
		    fi
		fi
	    done
	    cd ..
	done
    fi

    # EDIT LOCATION 1 of 3
    # The first line below sets up the config file for the default port.
    # Add following lines for additional ports by copying the first line
    # and (minimally) changing both occurrences of the string "dynaweb"
    # to "dynaweb_<port_number>".  For each additional port there must be
    # a /usr/lib/Insight/dweb/data/config/dynaweb_<port_number>.cfg.tmpl,
    # with the appropriate port specified in DWEB_PORT.  See the IRIS Insight
    # Administrator's  Guide for instructions on how to customize server 
    # port configuration.
    sed "s/%%LOCALHOST%%/$HOSTNAME/" $WWWD_DIR/data/config/dynaweb.cfg.tmpl | sed "s/%%BOOKSHELVES%%/$X/" > $WWWD_DIR/data/config/dynaweb.cfg
}

case "$1" in
  'start')
	if [ -x $WWWD_DIR/bin/$WWWD ]; then
	    if $IS_ON dynaweb; then

		# Before doing anything else, run insightAdmin to get 
		# bookshelves up to date.  Note that this will fail (with
		# errors going to /dev/null) if bookshelves are mounted 
		# rather than local

		echo "Verifying Insight bookshelves for DynaWeb..."
		for bookshelf in $LIBRARY/*/*
		do
		    echo $bookshelf
		    if [ `/sbin/stat -qc $bookshelf/booklist.txt 2> /dev/null` -lt `/sbin/stat -qc $bookshelf/books 2> /dev/null` ]; then
			insightAdmin -fa $bookshelf > /dev/null 2>&1
		    fi
		done

		# check whether we need to reconfigure the DynaWeb server
		# does the config file even exist?

		echo "Configuring and (re)starting DynaWeb server..."

		# EDIT LOCATION 2 of 3
		# List the config file for each port here, between the
		# double-quotes, separated by spaces.  Non-default
		# port configuration files must use the syntax: 
		# dynaweb_<port_number>.cfg.
		CONFIG_FILES="dynaweb.cfg"

                for CONFIG_FILE in $CONFIG_FILES
                do
                    if [ ! -f $WWWD_DIR/data/config/$CONFIG_FILE ]; then
                        config_dynaweb
                    else
                        CFG_TIME=`/sbin/stat -qc $WWWD_DIR/data/config/$CONFIG_FILE`
                        # older than the the template - ie new version installed
                        if [ $CFG_TIME -lt `/sbin/stat -qc $WWWD_DIR/data/config/$CONFIG_FILE.tmpl` ]; then
                            config_dynaweb
                        # is it older than the hostname
                        elif [ $CFG_TIME -lt `/sbin/stat -qc /etc/sys_id` ]; then
                            config_dynaweb
                        # is it older than the domain name (if there is one)
                        elif [ -f /var/yp/ypdomain ]; then
			    if [ $CFG_TIME -lt `/sbin/stat -qc /var/yp/ypdomain` ]; then
                                config_dynaweb
			    fi
                        # is it older than any bookshelves
                        else
                            for metabookshelf in /usr/share/Insight/library/*
                            do
                                # this  checks for removed bookshelves
                                STAT=`/sbin/stat -qc $metabookshelf`
                                if [ $CFG_TIME -lt $STAT ]; then
                                    config_dynaweb
                                    break
                                fi
                                for bookshelf in $metabookshelf/*
                                do
                                    # outermost conditional here tests for no 
                                    # bookshelves in metabookshelf; possible 
                                    # result of insightAdmin
                                    if [ -d  $bookshelf ]; then
                                        # this checks for added bookshelves
                                        STAT=`/sbin/stat -qc $bookshelf`
                                        if [ $CFG_TIME -lt $STAT ]; then
                                            config_dynaweb
                                            break
                                        fi
                                    fi
                                done
                            done
                        fi
                    fi
                done
		# do we need to add/replace any styles? 
		for styles in /usr/share/Insight/library/*/*/books/*/styles
		do
		    # see if it's an SGIDOC book
		    if [ -f $styles/bobindex -a -f $styles/media.tv ]; then

		        # this adds a fullhtml.v if there isn't one;
			# it's tuned for 3.3tools books 
		        if [ ! -f $styles/fullhtml.v ]; then
			    cp $WWWD_DIR/data/styles/fullhtml.v $styles/fullhtml.v > /dev/null 2>&1
			fi

                        # this replaces the fullhtml that shipped with
                        # insight_dev 1.0 with an updated version
                        if [ -n "`grep 'Revision: 1.7' $styles/fullhtml.v 2> /dev/null`" ]; then
                            cp $WWWD_DIR/data/styles/fullhtml.v $styles/fullhtml.v > /dev/null 2>&1
                        fi

			# this replaces the 3.3tools (1.33) toc.tv to fix
			# tm display
			if [ -n "`grep 'Revision: 1.33' $styles/toc.tv 2> /dev/null`" -a ! -f $styles/tochtml.tv ]; then
			    cp $WWWD_DIR/data/styles/toc.tv.33alt $styles/toc.tv > /dev/null 2>&1
			fi

                        # this replaces the insight_dev 1.0 (1.5) tochtml.tv
			# to fix tm display
                        if [ -n "`grep 'Revision: 1.5' $styles/tochtml.tv 2> /dev/null`" ]; then
                            cp $WWWD_DIR/data/styles/tochtml.tv $styles > /dev/null 2>&1
                        fi

		    fi
		done
		cd $WWWD_DIR/bin
		# EDIT LOCATION 3 of 3
		# List the ports for your server here, to turn them on. 
		# 88 is the default; list additional ports after 88.
		# You *must* use a *comma-separated* list (eg, 88,89)
		exec ./$WWWD -p 88
	    fi
	fi
        ;;

  'stop')
	/sbin/killall -k 10 -TERM $WWWD
        ;;

  *)
        echo "usage: /etc/init.d/dynaweb {start|stop}"
        ;;
esac
