#!/usr/bin/env bash

set -e

. ${0%/*}/_functions.sh

echo ""
echo -e "\033[42m"
echo -e "\033[42m                                                          "
echo -e "\033[42m APP_ENV=${APP_ENV}                                       "
echo -e "\033[42m MODE=${MODE}                                       "
echo -e "                                                            \033[42m\033[m"
echo ""

ok "* starting installation..."

apt-get -qq update

apt-get -qq install -y dirmngr wget apt-transport-https && \
RELEASE="bullseye"
#RELEASE="buster"

wget -O - https://rspamd.com/apt-stable/gpg.key | apt-key add -
echo "deb http://rspamd.com/apt-stable/ $RELEASE main" > /etc/apt/sources.list.d/rspamd.list
echo "deb-src http://rspamd.com/apt-stable/ $RELEASE main" >> /etc/apt/sources.list.d/rspamd.list

wget -O - https://repo.dovecot.org/DOVECOT-REPO-GPG | apt-key add -
echo "deb https://repo.dovecot.org/ce-2.3-latest/debian/$RELEASE $RELEASE main" > /etc/apt/sources.list.d/dovecot.list
# we will use normal dovecot now

apt-get -qq update
apt-get -qq upgrade

########### base ###########
apt-get -qq -y --no-install-recommends install locales rsyslog nano tar clamav-daemon patch libarchive-tools

########### dovecot ###########
apt-get -qq -y --no-install-recommends install dovecot-core dovecot-imapd dovecot-pop3d dovecot-sqlite dovecot-sieve dovecot-managesieved dovecot-lmtpd msmtp msmtp-mta dovecot-solr

########### rspamd ###########
apt-get -qq -y --no-install-recommends install rspamd

########### sqlite ###########
apt-get -qq -y --no-install-recommends install sqlite3

########### admin ###########
apt-get -qq -y --no-install-recommends install dnsutils mpack unzip sudo psmisc net-tools
# TODO remove net-tools

########### redis ###########
apt-get -qq -y --no-install-recommends install redis-server

########### p0f ###########
apt-get -qq -y --no-install-recommends install p0f net-tools
# we need net-tools to get default input/output interface to detect OS

apt-get -qq -y --no-install-recommends install bind9-host wget curl netcat-openbsd

########### roundcube ###########
apt-get -qq -y --no-install-recommends install nginx-light php-common php-fpm php-intl php-sqlite3 php-cli php-xmlrpc php-curl php-mbstring php-zip php-xml php-bcmath

########### zpush ###########
apt-get -qq -y --no-install-recommends install php-soap php-imap

#rm /etc/apt/sources.list.d/testing.list
#apt-get -qq update

# fix #52 missing locale
echo "LANG=en_US.UTF-8" > /etc/default/locale && locale-gen en_US.UTF-8
sed -i 's/;?systemd_interval ?= ?\d+/systemd_interval = 0/' "etc/php/7.4/fpm/php-fpm.conf"
