#!/bin/sh
# Install script for secinfo-xmpp

# Installation directory - default to /usr/local/lib/secinfo-xmpp
INSTDIR=/usr/local/lib/secinfo-xmpp

# By default do NOT install as coordinator
install_coordinator=false

# 

# main program

if [ ! -d $INSTDIR ]; then
   mkdir $INSTDIR
   fi


cp secinfo-xmpp $INSTDIR/secinfo-xmpp
chmod +x $INSTDIR/secinfo-xmpp
cp secinfo-client $INSTDIR/secinfo-client
chmod +x $INSTDIR/secinfo-client

# make configuration directory only if it does not exist
if [ ! -d /etc/secinfo-xmpp ]; then
  mkdir /etc/secinfo-xmpp
  fi
  
# Do not overwrite the configurarion file if it exists
if [ ! -e /etc/secinfo-xmpp/config.ini ]; then
   cp config/config.ini /etc/secinfo-xmpp/config.ini
   chmod og-rwx /etc/secinfo-xmpp/config.ini
   echo "Edit /etc/secinfo-xmpp/config.ini to match your setup"
   fi
   
# Fail2ban

cp fail2ban/secinfo-xmpp.conf $INSTDIR/secinfo-xmpp.conf
cp fail2ban/secinfo-xmpp.sh $INSTDIR/secinfo-xmpp.sh
chmod +x $INSTDIR/secinfo-xmpp.sh

if [ -d /etc/fail2ban/action.d ]; then
   if [ -f /etc/fail2ban/action.d/secinfo-xmpp.conf ]; then
      echo "Removing old file /etc/fail2ban/action.d/secinfo-xmpp.conf - now link"
      rm /etc/fail2ban/action.d/secinfo-xmpp.conf
      fi
   if [ ! -L /etc/fail2ban/action.d/secinfo-xmpp.conf ]; then   
      ln -s $INSTDIR/secinfo-xmpp.conf /etc/fail2ban/action.d/secinfo-xmpp.conf
      fi
   fi


# do not overwrite an existing fail2ban sshd jail
if [ -e /etc/secinfo-xmpp.jail.d/sshd.local ]; then
 cp fail2ban/sshd.local /etc/fail2ban/jail.d/sshd.local
 fi

 # Systemd service file
 
 if [ ! -f /etc/systemd/system/secinfo-xmpp.service ]; then
   cp config/secinfo-xmpp.service /etc/systemd/system/secinfo-xmpp.service
 fi
 
 # Coordinator
 #
 if [ "$install_coordinator" = true ]; then
   cp secinfocoordinator.py $INSTDIR/secinfocoordinator.py
   if [ ! -d /var/local/lib/secinfo-xmpp/ ]; then
      mkdir -p /var/local/lib/secinfo-xmpp/
      fi
      
   sqlite3 /var/local/lib/secinfo-xmpp/secinfo.db  <<DBEND
CREATE TABLE admin(db_version int, starttime real);
INSERT INTO admin VALUES( 1, julianday('now'));
CREATE TABLE attack(time real, ip_addr text, jail text, sechost text);
CREATE TABLE attacker(ip_addr text, firstseen real, latest real, jails text, country text, hoster text);
CREATE TABLE country(country_code text, total int);
DBEND
fi


  
   
 
 
