#!/bin/sh
# postinst script for alienvault-dummy-sensor
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    configure)
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#


ossim_reconf="/etc/ossim/ossim_setup.conf"

if [ -f "$ossim_reconf" ]; then
	echo "$ossim_reconf found ."	
else

	# get first iface for admin
		admin_iface=`cat /proc/net/dev | grep -v face | grep -v Inter | grep -v lo | cut -d":" -f1 | head -1`
	# get hostname
		server_hostname=`cat /etc/hostname`

echo "interface=$admin_iface"            > $ossim_reconf
echo "language=en"              >> $ossim_reconf
echo "profile=Sensor"       >> $ossim_reconf
echo "version=1.2"              >> $ossim_reconf
echo "hostname=$server_hostname"               >> $ossim_reconf
echo "admin_ip=$iplocal"               >> $ossim_reconf
echo "first_init=no ">> $ossim_reconf
echo "email_notify=system@alienvault.com ">> $ossim_reconf

	if [ "$finalupdateapt" == "yes" ];then
		echo "upgrade=yes">> $ossim_reconf
	else
		echo "upgrade=no">> $ossim_reconf
	fi
echo ""                         >> $ossim_reconf
echo "[database]"               >> $ossim_reconf
echo "acl_db=ossim_acl"         >> $ossim_reconf
echo "db_ip=$db_host"           >> $ossim_reconf
echo "db_port=3306"             >> $ossim_reconf
echo "event_db=snort"           >> $ossim_reconf
echo "ocs_db=ocsweb"            >> $ossim_reconf
echo "ossim_db=ossim"           >> $ossim_reconf
echo "osvdb_db=osvdb"           >> $ossim_reconf
echo "pass=$db_pass"    >> $ossim_reconf
echo "type=mysql"               >> $ossim_reconf
echo "user=$ossim_user"                >> $ossim_reconf
echo "rebuild_database=yes"                >> $ossim_reconf
#echo "innodb=no"                >> $ossim_reconf
echo ""                         >> $ossim_reconf
echo "[expert]"                 >> $ossim_reconf
echo "profile=server"           >> $ossim_reconf
echo ""                         >> $ossim_reconf
echo "[sensor]"                 >> $ossim_reconf
#echo "detectors=aladdin,apache,arpwatch,avast,bind,bro-ids,cisco-ids,cisco-pix,cisco-router,cisco-vpn,clamav,clurgmgr,cyberguard,dhcp,iptables,nagios,osiris,p0f,pads,pam_unix,rrd,snare,snortunified,ssh,sudo" >> $ossim_reconf

echo "detectors=$detector_select" >> $ossim_reconf
echo "interfaces=$ifaces_promisc" >> $ossim_reconf
echo "ip="                      >> $ossim_reconf
echo "pci_express=yes"                      >> $ossim_reconf
#echo "monitors=nessus-monitor,nmap-monitor,opennms-monitor,ossim-monitor,ping-monitor,session-monitor,tcptrack-monitor,whois-monitor,wmi-monitor"                  >> $ossim_reconf
echo "monitors=$monitor_select"                  >> $ossim_reconf
echo "name=ossim"               >> $ossim_reconf
echo "networks=$cidr"   >> $ossim_reconf
echo "rsyslog_dnslookups_disable=yes"   >> $ossim_reconf
echo "netflow=yes"   >> $ossim_reconf
echo ""  >> $ossim_reconf
echo "[framework]"                 >> $ossim_reconf
echo "framework_ip=$framework_host"               >> $ossim_reconf
echo "framework_port=$framework_port"               >> $ossim_reconf
echo ""  >> $ossim_reconf
echo "[server]"                 >> $ossim_reconf
echo "server_ip=$server_host"               >> $ossim_reconf
echo "server_port=$server_port"               >> $ossim_reconf
echo "server_pro=$client"               >> $ossim_reconf
echo "server_license=$key"               >> $ossim_reconf
echo "server_plugins=osiris, pam_unix, ssh, snare, sudo"               >> $ossim_reconf
echo ""                         >> $ossim_reconf
echo "[snmp]"                 >> $ossim_reconf
echo "snmpd=yes"                 >> $ossim_reconf
echo "snmptrap=yes"                 >> $ossim_reconf
echo "community=public"                 >> $ossim_reconf
echo ""                         >> $ossim_reconf
echo "[firewall]"                 >> $ossim_reconf
echo "active=yes"                 >> $ossim_reconf
echo ""                         >> $ossim_reconf
echo "[vpn]"                 >> $ossim_reconf
echo "vpn_infraestructure=no"		  >> $ossim_reconf
echo "vpn_net=10.67.68"               >> $ossim_reconf
echo "vpn_port=33800"               >> $ossim_reconf

fi


if [ -x /usr/bin/ossim-reconfig ]; then
	if [ -x /usr/bin/postpone ]; then
		/usr/bin/postpone --verbose --lock /var/run/ossim-reconfig.lock --output /tmp/ossim-reconfig.XXXXXX --debian /usr/bin/ossim-reconfig -v -c --dpkg
	else
		/usr/bin/ossim-reconfig
	fi
fi

exit 0
