#!/bin/sh

set -e

CONF="/etc/http-analyze.cfg"
CRON="/var/spool/cron/crontabs/"

LINE1="monthly"
echo "Please enter wether the mode of operation should be monthly "
echo "or daily. monthly is a lot better imho."
echo "[$LINE1]"
echo -n "> "
read MODE
if [ -z $MODE ]; then
  MODE=$LINE1
fi

LINE2="www.`dnsdomainname`"
echo "Please enter the name of your server."
echo "[$LINE2]"
echo -n "> "
read SERVNAME
if [ -z $SERVNAME ]; then
  SERVNAME=$LINE2
fi;

LINE3="/var/web/server/access.log"
echo "Please enter the name of the default logfile."
echo "[$LINE3]"
echo -n "> "
read $LOGFILE
if [ -z $LOGFILE ]; then
  LOGFILE=$LINE3
fi;

LINE4="index.html"
echo  "Please enter the name of the normal index page"
echo "[$LINE4]"
echo -n "> "
read $INDEX
if [ -z $INDEX ]; then
  INDEX=$LINE4
fi

LINE5="/var/web/webspace/stats"
echo "Please enter the complete Path to the directory where"
echo "the stats-files should be created."
echo "[$LINE5]"
echo -n "> "
read $HTMLDIR
if [ -z $HTMLDIR ]; then
  HTMLDIR=$LINE5
fi

LINE6="lists"
echo "Please enter the name of a _private_ directory where"
echo "the detailed site/URL lists should be created. Pathnames"
echo "not beginning with a '/' are relative to the previous"
echo "entry. [$LINE6]"
echo -n "> "
read $PRIVATEDIR
if [ -z $PRIVATEDIR ]; then
  PRIVATEDIR=$LINE6
fi

LINE7="10"
echo "Please enter the number of entries in the top"
echo "sites lists. [$LINE7]"
echo -n "> "
read $TOPSITES
if [ -z $TOPSITES ]; then
  TOPSITES=$LINE7
fi

LINE8="30"
echo  "Please enter the number of entries in the top"
echo "URLs lists. [$LINE8]"
echo -n "> "
read $TOPURL
if [ -z $TOPURL ]; then
  TOPURL=$LINE8
fi

LINE9="10"
echo "Please enter the number of entries in the last"
echo "URLs lists. [$LINE9]"
echo -n "> "
read $LASTURL
if [ -z $LASTURL ]; then
  LASTURL=$LINE9
fi                                          

LINE10="Access statistics for"
echo "Please enter the document title and header to display."
echo "[$LINE10]"
echo -n "> "
read $DOCTITLE
if [ -z $DOCTITLE ]; then
  DOCTITLE=$LINE10
fi

LINE11='<BODY BGCOLOR="#D6D6D6"><P><HR SIZE="8">'
echo "Please enter the suffix string to output after"
echo "the document header."
echo "[$LINE11]"
echo -n "> "
read $HEADSUFFIX
if [ -z $HEADSUFFIX ]; then
  HEADSUFFIX=$LINE11
fi

LINE12='<BR><FONT SIZE="-1"><A HREF="/">Back</A> to my homepage</FONT>'
echo "Please enter the trailer string to output at end of page."
echo "[$LINE12]"
echo -n "> "
read $DOCTRAILER
if [ -z $DOCTRAILER ]; then
  DOCTRAILER=$LINE12
fi

LINE13='www-data'
echo "Please enter the user who will run http-analyze"
echo "[$LINE13]"
echo -n "> "
read $CRONUSER
if [ -z $CRONUSER ]; then
  CRONUSER=$LINE13
fi


if [ -f $CONF ] ; then
  rm -r $CONF
fi

touch $CONF

echo -e "DefaultMode\t$MODE" > $CONF
echo -e "ServerName\t$SERVNAME" >> $CONF
echo -e "HTTPLogFile\t$LOGFILE" >> $CONF
echo -e "HomePage\t$INDEX" >> $CONF
echo -e "HTMLDir\t\t$HTMLDIR" >> $CONF
echo -e "PrivateDir\t$PRIVATEDIR" >> $CONF
echo -e "TopSites\t$TOPSITES" >> $CONF
echo -e "TopURLs\t\t$TOPURL" >> $CONF
echo -e "LastURLs\t$LASTURL" >> $CONF
echo -e "DocTitle\t$DOCTITLE" >> $CONF
echo -e "HeadPrefix\t$HEADPREFIX" >> $CONF
echo -e "HeadSuffix\t$HEADSUFFIX" >> $CONF
echo -e "DocTrailer\t$DOCTRAILER" >> $CONF
echo -e "HideURL\t\t*.map\t[All map files]" >> $CONF
echo -e "HideURL\t\t/robots.txt\t[Internal pages]" >> $CONF

CRON="${CRON}${CRONUSER}"
if [ -f $CRON ]; then
  echo -e "0 3 * * *\t\t/usr/bin/http-analyze -c /etc/http-analyze.cfg >/dev/null 2>/dev/null" >> $CRON
else
  echo -e "0 3 * * *\t\t/usr/bin/http-analyze -c /etc/http-analyze.cfg >/dev/null 2>/dev/null" > $CRON
fi

chmod 0644 $CONF
chmod 0600 $CRON

echo ""
echo ""
echo "Thank you. This configuration is now written to $CONF."
echo "If you want to change the configuration, just edit "
echo "this file. You will find a sample.conf in"
echo "/usr/doc/http-analyze/examples/. "
echo "Good day."


