#!/bin/sh

set -e

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

CONFNEU="n"

if [ -s $CONF ] && [ -f $CONF ]; then
  LINE0="c"
  echo "It seems that you already have a http-analyze-configuration."
  echo "Do you want to Convert your existing configuration to the "
  echo "new format? Otherwise I will create a new one."
  echo "[$LINE0]"
  echo -n "> "
  read CONFNEU
  if [ -z $CONFNEU ]; then
    CONFNEU=$LINE0
  fi
fi

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

if [ ! -d $HTMLDIR ]; then
  mkdir -p $HTMLDIR 2>/dev/null
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                                          

LINE9a="2"
echo "Level of subdomains in hostnames to use in the Top N sites list as an"
echo "item, under which all hostnames from this domain are lumped together."
echo "Valid range: 1-5, Default: 2"
echo "[$LINE9a]"
echo -n "> "
read SHOWDOMAIN
if [ -z $SHOWDOMAIN ]; then
  SHOWDOMAIN=$LINE9a
fi

LINE10="Access statistics for $SERVNAME"
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 HEADPREFIX
if [ -z $HEADPREFIX ]; then
  HEADPREFIX=$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 [ $CONFNEU = "c" ]; then
  http-analyze -c $CONF -i ${CONF}.N
  mv $CONF ${CONF}.old
  mv ${CONF}.N ${CONF}
else
  rm -rf $CONF
  touch $CONF
  echo -e "DefaultMode\t$MODE" > $CONF
  echo -e "ServerName\t$SERVNAME" >> $CONF
  echo -e "LogFile\t\t${LOGFILE}" >> $CONF
  echo -e "HomePage\t$INDEX" >> $CONF
  echo -e "OutputDir\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 "ShowDomain\t$SHOWDOMAIN" >> $CONF
  echo -e "ReportTitle\t${DOCTITLE}" >> $CONF
  echo -e "HTMLPrefix\t${HEADPREFIX}" >> $CONF
#  echo -e "HTMLTrailer\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

fi

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."


