#!/bin/bash

# To enable 'add-apt-repository'
apt-get --assume-yes install software-properties-common

# Add the repositories
wget https://archive.nic.cz/dists/cznic-archive-keyring.gpg
apt-key add ./cznic-archive-keyring.gpg
add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) universe multiverse"
add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc)-updates universe multiverse"
add-apt-repository "http://archive.nic.cz/ubuntu/"
apt-get update

# install postfix
debconf-set-selections <<< "postfix postfix/mailname string $(hostname)"
debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"
apt-get --assume-yes install postfix

# install fred
apt-get --assume-yes install fred

# Install the database scheme
su - postgres -c "/usr/sbin/fred-dbmanager install"

# adding system registrar without EPP access
/usr/sbin/fred-admin --registrar_add --handle=REG-SYSTEM --country=CZ --no_vat --system

# start services
systemctl start omniNames fred-rifd fred-adifd fred-pifd fred-logd fred-msgd fred-msgd fred-akmd fred-rsifd fred-pyfred fred-webadmin uwsgi apache2

# enable services for restart
systemctl enable postgresql omniNames fred-rifd fred-adifd fred-pifd fred-logd fred-msgd fred-msgd fred-akmd fred-rsifd fred-pyfred fred-webadmin uwsgi apache2

# Enable sites in UWSGI
ln -s /etc/uwsgi/apps-available/fred-rdap.ini /etc/uwsgi/apps-enabled/
ln -s /etc/uwsgi/apps-available/fred-webwhois.ini /etc/uwsgi/apps-enabled/
service uwsgi restart

# Enable sites in Apache
a2ensite 02-fred-mod-eppd-apache.conf
a2ensite 02-fred-mod-whoisd-apache.conf
a2enconf fred-rdap.conf
a2enconf fred-webwhois.conf

# mod-whoisd is not compatible with mpm-event apache scheme that is installed
# by default, so it should be disabled and replaced by mpm-prefork
a2dismod mpm_event
a2enmod mpm_prefork
service apache2 restart
