#/bin/bash

echo "Welcome to the Discord-Linux Webmin Automated installer"
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/debian/notif && 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 -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"   install -y shared-mime-info  perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl apt-show-versions python unzip

#yes | DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"  --fix-broken install -f 
#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


notif "Finding a port and configuring webmin"

hostname=$(hostname)

echo "Grabbing a port from your container from our API"
port=$(curl -s  https://findport.ssh.surf/?id=$hostname)

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
echo "Setting Webmin Port"
sed -i "s/10000/port=${port}/g" /etc/webmin/miniserv.conf

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

service webmin start

sed -i "s/#webmin/service webmin start/g" /start.sh

notif "Your webmin installation has completed, you may access your installation at: http://ssh.surf:$port"

echo "Your installation has completed, you may access your installation at: http://ssh.surf:$port"