#!/bin/bash
# Start the container with libvirt and run this in the console to configure the
# system.

set -e

# jessie/contrib for winetricks
cat > /etc/apt/sources.list <<EOF
deb http://ftp.se.debian.org/debian/ jessie main contrib
EOF

# Optionally use apt-cacher-ng, if you have one
cat > /etc/apt/apt.conf.d/02proxy <<EOF
Acquire::http::proxy "http://aptcache:3142";
EOF

# wget is used to download the WineHQ release key over HTTPS next
apt-get update
apt-get install -y wget </dev/null

# Unstable wine repo for Wine 1.9
cat > /etc/apt/sources.list.d/wine.list <<EOF
deb http://dl.winehq.org/wine-builds/debian/ jessie main
EOF
wget https://dl.winehq.org/wine-builds/Release.key
apt-key add Release.key

# Enable 32-bit multiarch
dpkg --add-architecture i386
apt-get update

# xserver-common for MS VC runtime installer
apt-get install -y xserver-common </dev/null
apt-get install -y wine-staging wine-staging-amd64 winehq-staging </dev/null
# winetricks just for its dependencies. We will actually use the latest version from git.
# expect for unbuffer
# sudo for unprivileged control of the server systemd service
apt-get install -y winetricks expect sudo </dev/null

# Optionally install your favorite tools for managing the server's config files
apt-get install -y git vim less </dev/null

# Get the latest winetricks, since the repository version doesn't support wine 1.8
pushd /usr/local/bin/
wget https://raw.githubusercontent.com/Winetricks/winetricks/master/src/winetricks
chmod +x winetricks
popd

# Create the non-privilegied user account. Nobody likes /bin/sh (default shell)
useradd --system --create-home --shell /bin/bash kf2server

# Allow the unprivileged user to start/stop the server
cat >> /etc/sudoers <<EOF
kf2server ALL=NOPASSWD:/bin/systemctl status kf2server.service
kf2server ALL=NOPASSWD:/bin/systemctl start kf2server.service
kf2server ALL=NOPASSWD:/bin/systemctl stop kf2server.service
EOF

visudo --check
