#!/bin/bash
set -e

sdir=/usr/share/omix
ldir=/var/lib/omix
vdir=/etc/omix

bak="--backup=numbered"
sed -e '/^#/d' -e '/^$/d'  /etc/dnsmasq.conf | grep . && mv $bak /etc/dnsmasq.conf /etc/dnsmasq.conf.dpkg-old
sed -i -e 's?^CONFIG_DIR=.*$?CONFIG_DIR=/etc/dnsmasq.d,\*.conf?' /etc/default/dnsmasq
[[ ! -e /etc/nftables.conf || "$(readlink /etc/nftables.conf)" == $rdir/nftables.conf ]] || \
  mv $bak /etc/nftables.conf /etc/nftables.conf.dpkg-old
[[ -e $ldir/nftables.conf ]] || cp $sdir/nftables.conf.sample $ldir/nftables.conf

touch $vdir/{dns.server,dns.rev}
/usr/share/omix/mkconf.gw

for f in dhcp.conf dnsmasq.conf servers; do
  [ -L /etc/dnsmasq.d/$f ] || ln -s $ldir/$f /etc/dnsmasq.d/$f
done
[ -L /etc/nftables.conf ] || ln -s $rdir/nftables.rt /etc/nftables.conf

systemctl restart dnsmasq
systemctl restart nftables
