#!/bin/bash

echo "Welcome to the Discord-Linux Webmin Automated installer for dLinux64"
echo "This installer will automatically select a port on your container."
echo "I will let you know how to access your Webmin installation when installation is complete."
echo ""
echo ""
echo ""
echo ""

apt update
echo "downloading the Notification Bot"
wget -q -O /usr/bin/notif https://ssh.surf/notify-x64 && chmod +x /usr/bin/notif
echo "Done"

notif "Welcome to the Webmin Automated installer for Discord-Linux, We are preparing your installation..."


DEBIAN_FRONTEND=noninteractive apt upgrade -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"  -y

DEBIAN_FRONTEND=noninteractive apt-get -y install perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl  python unzip zip

#https://askubuntu.com/questions/916199/install-apt-show-versions-inside-an-ubuntu-docker-container
rm /etc/apt/apt.conf.d/docker-gzip-indexes
 apt-get -y  purge apt-show-versions
rm /var/lib/apt/lists/*lz4
 apt-get -o Acquire::GzipIndexes=false update

notif "We are building show versions, this may take a while..."

apt install -y  apt-show-versions

sh /var/tools/getsystemctl.sh

wget -O /tmp/webmin-current.deb http://www.webmin.com/download/deb/webmin-current.deb

dpkg -i /tmp/webmin-current.deb


echo "Disabling SSL in webmin for proxy support"
sed -i 's/ssl=1/ssl=0/g' /etc/webmin/miniserv.conf
echo "disabling refer checks to allow for vhost usage"
sed -i 's/referers_none=1/referers_none=0/g' /etc/webmin/config
sed -i 's/referer=1/referer=0/g' /etc/webmin/config


rm -f /tmp/webmin_1.984_all.deb 

service webmin start

relayPort 10000

sleep 5

url=$(grep -hPo "(?<=listening ).*" /root/.pm2/logs/Relay-10000-out.log  | head -n 1 | sed 's/$/.virt.fun/')
echo "referers=$url" >> /etc/webmin/config


sed -i "s/#webmin/service webmin start \&\& relayPort 10000/g" /start.sh

notif "Your webmin installation has completed, you may access your installation at: https://$url"

echo "Your installation has completed, you may access your installation at: https://$url"