#!/bin/bash

# AKITA SERVER INSTALLATION
# Akita is slackware 15.0 laptop server that contains two lxc containers:
# corehub and orui.
#
# WARNING: All commands should be run as root. Check comments if it should be
# run as sudo user.

# NETWORKMANAGER CONFIGURATION
# In this slackware configuration I use NetworkManager instead DHCP.

nmcli device wifi list
nmcli device wifi connect "X" password "Y"

# Some slackware machines could have problem with hostname: after login and
# logout machine's hostname changes to localhost, for some reasons. To solve
# this problem complete nexct command.

echo "hostname-mode=none" >> /etc/NetworkManager/NetworkManager.conf

# VIM CONFIGURATION
# Here is my basic minimal settings for vim. `nobackup` and `noundofile` must be
# anyway.

echo "set shiftwidth=2" >> /root/.vimrc
echo "set tabstop=2" >> /root/.vimrc
echo "set expandtab" >> /root/.vimrc
echo "set nobackup" >> /root/.vimrc
echo "set noundofile" >> /root/.vimrc

echo "set shiftwidth=2" >> /home/master/.vimrc
echo "set tabstop=2" >> /home/master/.vimrc
echo "set expandtab" >> /home/master/.vimrc
echo "set nobackup" >> /home/master/.vimrc
echo "set noundofile" >> /home/master/.vimrc

chown master:users /home/master/.vimrc

# BASH PROFILE CONFIGURATION

echo "set -o vi" >> /root/.bash_profile
echo "set -o vi" >> /home/master/.bash_profile
chown master:users /home/master/.bash_profile

# USER CONFIGURATION

adduser master # with wheel group
vim /etc/sudoers # uncomment wheel group to execute any command

# Slackpkg Configuration and Packages Update
vim /etc/slackpkg/mirrors # uncomment only one mirror (I prefer https)
/usr/sbin/slackpkg update
/usr/sbin/slackpkg update gpg

# Keymaps Configuration
# I prefer dvorak keyboard layout and never use Caps_Lock.

vim /usr/share/kbd/keymaps/i386/dvorak/dvorak.map.gz # change Caps_Lock to
                                                     # Control

# REBOOT
# Just reboot to complete these changes, espacialy for keymaps.</p>

reboot

# Server Configuration
ntpdate time.nist.gov

chmod +x /etc/rc.d/rc.local

touch /etc/rc.d/rc.local_shutdown
chmod +x /etc/rc.d/rc.local_shutdown

# LXC Configuration
# I always make lxc containers unpreveliged and available for root.
# Then making two containers, setting bridge and make them auto start.

echo "root:100000:65536" >> /etc/subuid
echo "root:100000:65536" >> /etc/subgid
echo "lxc.idmap = u 0 100000 65536" >> /etc/lxc/default.conf
echo "lxc.idmap = g 0 100000 65536" >> /etc/lxc/default.conf

lxc-create -n apache -t download # (slackware 15.0 amd64)
lxc-create -n orui -t download # (slackware 15.0 amd64)

echo "USE_LXC_BRIDGE=\"true\"" >> /etc/default/lxc-net # file could not
                                                       # exist-just create it

/usr/libexec/lxc/lxc-net start

echo "lxc.net.0.type = veth" >> /var/lib/lxc/corehub/config
echo "lxc.net.0.flags = up" >> /var/lib/lxc/corehub/config
echo "lxc.net.0.link = lxcbr0" >> /var/lib/lxc/corehub/config
echo "lxc.start.auto = 1" >> /var/lib/lxc/corehub/config
echo "lxc.net.0.ipv4.address = 10.0.3.165/24" >> /var/lib/lxc/corehub/config
echo "lxc.net.0.ipv4.gateway = 10.0.3.1" >> /var/lib/lxc/corehub/config

echo "lxc.net.0.type = veth" >> /var/lib/lxc/orui/config
echo "lxc.net.0.flags = up" >> /var/lib/lxc/orui/config
echo "lxc.net.0.link = lxcbr0" >> /var/lib/lxc/orui/config
echo "lxc.start.auto = 1" >> /var/lib/lxc/orui/config
echo "lxc.net.0.ipv4.address = 10.0.3.37/24" >> /var/lib/lxc/orui/config
echo "lxc.net.0.ipv4.gateway = 10.0.3.1" >> /var/lib/lxc/orui/config

chmod +x /etc/rc.d/rc.lxc
echo """
# start lxc containers
if [ -x /etc/rc.d/rc.lxc ]; then
        /etc/rc.d/rc.lxc start
fi
""" >> /etc/rc.d/rc.local

echo """
# start lxc networking
if [ -x /usr/libexec/lxc/lxc-net ]; then
        /usr/libexec/lxc/lxc-net start
fi
""" >> /etc/rc.d/rc.local

# SSH Configuration
# I think it shouldn't be here, for some secure reasons.

ssh-keygen -t ecdsa -b 384 # as master

# NFT Configuration
touch /etc/rc.d/rc.firewall
chmod +x /etc/rc.d/rc.firewall

touch /etc/nftables.conf

echo """
table ip lxc-dnat {
        chain prerouting {
                        type nat hook prerouting priority -100;
                        policy accept;

                        iifname \"wlan0\" tcp dport 80 dnat to 10.0.3.165:80
                        }
        }
""" >> /etc/nftables.conf

echo """
#!/bin/bash

NFT=\"/usr/sbin/nft\"
CONF=\"/etc/nftables.conf\"

case \"$1\" in
        start)
                        $NFT -f \"$CONF\"
                        ;;

        stop)
                        $NFT delete table ip lxc-dnat 2>/dev/null
                        ;;

        restart)
                        $0 stop
                        $0 start
                        ;;

        *)
                        echo \"Usage: $0 {start|stop|restart}\"
                        ;;
esac
""" >> /etc/rc.d/rc.firewall

# REBOOT
# That's it. Just reboot and be happy.

reboot
      
# LXC Core Hub Installation
# Core Hub is the container that uses apache to host Core Hub website.
# WARNING: All commands should be run as root. Check comments if it should
# be run as sudo user, for expamle, as master.
#
# Slackpkg Update
# At first you could to update slackpkg packages and update gpg.

slackpkg update
slackpkg update gpg

# User Configuration
adduser master # with wheel group
vi /etc/sudoers # uncomment wheel group to execute any command

# Slackpkg Packages Installation and Some Upgrades
# Be sure to run next two commands as sudo user, for example, as master.

sudo /usr/sbin/slackpkg install vim libsodium httpd openssh apr-util sqlite \
  apr sasl mariadb icu4c lynx tmux # as master
sudo /usr/sbin/slackpkg upgrade pcre2 # as master

# Vim Configuration
# `nobackup` and `noundofile` must be anyway.

touch /home/master/.vimrc /root/.vimrc
chown master:users /home/master/.vimrc

echo "set shiftwidth=2" >> /root/.vimrc
echo "set tabstop=2" >> /root/.vimrc
echo "set expandtab" >> /root/.vimrc
echo "set nobackup" >> /root/.vimrc
echo "set noundofile" >> /root/.vimrc

echo "set shiftwidth=2" >> /home/master/.vimrc
echo "set tabstop=2" >> /home/master/.vimrc
echo "set expandtab" >> /home/master/.vimrc
echo "set nobackup" >> /home/master/.vimrc
echo "set noundofile" >> /home/master/.vimrc

# Apache Configuration
# I'm not sure is it a good idea to name the container the same as website's
# directory, well, whatever.
    
vim /etc/httpd/httpd.conf # change ServerName to corehub:80
                          # and "/srv/httpd/htdocs" to "/srv/www/corehub"
                          # for DocumentRoot and <Directory "...">
chmod +x /etc/rc.d/rc.httpd
apachectl start

# SSH Configuration
# Some basic configuration means to change Port to 2201, PermitRootLogin to
# no and MaxAuthTries to 3.

vim /etc/ssh/sshd_config # some basic configuration
chmod +x /etc/rc.d/rc.sshd
/etc/rc.d/rc.sshd start

# Some Stuff
scp -r tof@192.168.0.239:/Users/tof/Desktop/corehub /srv/www # WARNING: IP could
                                                             # be different
chown -R master:users /srv/www/corehub
