#! /bin/sh
#
# Copyright 2001,2002 by Dennis Schoen <ds@teuto.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA.

# Source debconf library
. /usr/share/debconf/confmodule

# Get ntop user

db_get ntop/user
USER=$RET
db_get ntop/createuser
CREATEUSER=$RET

# Creating ntop group if he isn't already there
if ! grep -q ^$USER: /etc/passwd; then
  if [ "$CREATEUSER" = "true" ]; then
    echo Adding system user: $USER.
    adduser --system --group --home /var/lib/ntop $USER
  fi
fi

# make status dir owned by user
if grep -q ^$USER: /etc/passwd; then
  chown $USER /var/lib/ntop
  chown $USER /var/log/ntop
else
  db_input high ntop/usernotice || true
  db_go
fi

# ask for initial admin password
db_input high ntop/adminpass || PASS_RETCODE=$? || true
db_go
db_get ntop/adminpass
PASS=$RET

# set the initial admin password
if [ ! "$PASS_RETCODE" = 30 ] ; then
  echo Setting initial admin password.
  ntop -t 0 -u ntop -P /var/lib/ntop/ --set-admin-password=$PASS
fi

# decide if we want to start the daemon on boot
db_get ntop/mode
MODE=$RET

# Record debconf configuration
# 1. Stuff needed for the init script

INITCFG=/etc/ntop/init.cfg
cat > $INITCFG <<EOF
# This file is automatically generated !
#
# YOU MAY MODIFY THIS FILE
# but we recommend to use dpkg-reconfigure ntop instead.
# It will be regenerated on each update.

EOF

db_get ntop/getopt
# Remove unsupported -N option (does not exist in this version)
GETOPT=`echo " $RET" | sed -e 's/,//g;s/[[:space:]]*-N[[:space:]]*//g;'`
db_get ntop/sslport
SSLPORT=$RET
db_get ntop/port
PORT=$RET
db_get ntop/interfaces
INTERFACES=$RET
db_get ntop/trace
TRACE=$RET

echo USER=\"$USER\" >> $INITCFG
echo GETOPT=\"$GETOPT\" >> $INITCFG
echo PORT=\"$PORT\" >> $INITCFG
echo SSLPORT=\"$SSLPORT\" >> $INITCFG
echo INTERFACES=\"$INTERFACES\" >> $INITCFG
echo TRACE=\"$TRACE\" >> $INITCFG

db_stop

if [ -x "/etc/init.d/ntop" ]; then
  if [ "$MODE" = "true" ]; then
    update-rc.d ntop defaults >/dev/null
    /etc/init.d/ntop start
  else
    update-rc.d -f ntop remove >/dev/null 2>&1
 fi
fi

#DEBHELPER#

