#!/bin/bash
#
# Run the boa web server in its own environment for nightview server
#
# $Id: nightview-boa,v 1.11 2004-06-04 15:13:01 hroch Exp $

#set -x

#- BEGIN OF FINE TUNING PARAMETERS ----------------------------------------
# the server
BOA=/usr/sbin/boa

#
# root of nightview directory structure
NIGHT_ROOT=/var/tmp/nightview

# source directory with nightview.html and nightview.cgi
SRC=$(dirname $0)

# USER and GID for server
UNAME=www-data
GNAME=www-data

#- END OF TUNNIG ----------------------------------------------------------

# print help
if [ "$1" == "-h" -o "$1" == "--help" ]; then
    echo "NIGHTVIEW package utilities 0.3.0, (C) 2001-2 F. Hroch, Monte Boo, Brno, CZ"
    echo "nightview - boa Script to start BOA http server as nightview http server."
    echo "Usage: $0 [-h|--help] [path-to-nightview.cgi-and-nightview.html] [root directory]"
    echo "         [path-to-nightview.cgi-and-nightview.html] = $SRC"
    echo "         [root directory] = $NIGHT_ROOT"
    printf "\t-h print help"
    echo 
    echo "The leave the first parameter as blank leads to use of the current directory as source. Default for root is /tmp/nightview." 
    echo "This is a exmaple of the script to run http server. You can customize it as needs."
    exit 0
fi

# optionally set the source directory
if [ "$1" != "" ]; then
    SRC=$1
fi

# optionally set the target directory
if [ "$2" != "" ]; then
    NIGHT_ROOT=$2
fi

# create a new web server root
if [ ! -d $NIGHT_ROOT ]; then
   mkdir $NIGHT_ROOT
fi

# copy nightview.cgi and nightview.html to this directory
if [ "$SRC" = "/usr/sbin" ]; then
  # debian specific
  cp -f "/usr/share/nightview/html/nightview.html" "$NIGHT_ROOT"
  cp -f "/usr/lib/nightview/cgi-bin/nightview.cgi" "$NIGHT_ROOT"
  cp -f "/usr/share/nightview/html/index.boa.html" "$NIGHT_ROOT/index.html"
else
  # original tarball, SRC == "$(prefix)/sbin
  SRC1=${SRC%%/sbin}
  cp -f "${SRC1}/share/nightview/cgi-bin/nightview.cgi" "$NIGHT_ROOT"
  cp -f "${SRC1}/share/nightview/html/nightview.html" "$NIGHT_ROOT"
  cp -f "${SRC1}/share/nightview/html/index.boa.html" "$NIGHT_ROOT/index.html"
fi

# link to /tmp directory where the nightviewd create temp files
ln -sf /tmp $NIGHT_ROOT/tmp

# set gid and uid to default when root is running this script
if [ "$UID" != 0 ]; then
    UNAME=$USER
    GNAME=$(id -gn)
fi

# create the config file, if it doesn't exists
set -o noclobber
cat > $NIGHT_ROOT/boa.conf  <<EOF
#
# Configuration file for BOA running as nightview http server
#
# listen on port
Port 7666

# listen address, specify IP address (by numbers), not DNS name, not required
# See manual of BOA for deeper description.
#Listen 192.168.0.1

# UID and GID of BOA user, Debian policy is www-data. 
# Use nobody/nogroup when if server don't work.
User $UNAME
Group $GNAME

# mail to admin, currently not used
#ServerAdmin www-data@localhost

# Error log and access logs
ErrorLog boa_error.log
#AccessLog boa_access.log
# The access file is commented out to supprise a lot of messages.

# commented out uses UTC, uncommented localtime
#UseLocaltime

# uncomment to verbose cgi log
#VerboseCGILogs

# uncomment when you need different than DNS name
#ServerName www.porno.org

# uncomment for root of documents, it need for debugging
DocumentRoot $NIGHT_ROOT

# simple access to root directory
DirectoryIndex index.html

# Number of keep alive requests per seconds
KeepAliveMax 100

# Timeout for keepalive in seconds
KeepAliveTimeout 20

# Default mime types, don't comment out. Use /dev/null for no mime types.
#MimeTypes /etc/mime.types
MimeTypes /dev/null

# default type with unknown suffix
DefaultType text/html

# this directory is need for CGI (nightview) processing
AddType application/x-httpd-cgi cgi

# End of configuration. Consult the BOA server documentation
# on http://www.boa.org or in your local repository for special adjustments
# of the server.
EOF

# invoke of daemon
exec $BOA -c $NIGHT_ROOT

# this part of the file is passed when exec fail
exit 1
