#!/bin/bash

echo "Welcome to the Discord-Linux HasteBin 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 "downloading the Notification Bot"
wget -q -O /usr/bin/notif https://ssh.surf/notif && chmod +x /usr/bin/notif
echo "Done"

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

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

cd /home

notif "Cloning into /home/haste-server"
git clone https://github.com/seejohnrun/haste-server.git

cd haste-server

npm install

npm update

notif "Requesting port from API Server"
hostname=$(hostname)

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

sed -i "s/7777/${port}/g" config.js


notif "Configuring HasteBin and starting it's server"
wget -O hasteBinStart.json https://ssh.surf/install/hastebin

cd /home/haste-server && pm2 start hasteBinStart.json

sed -i "s/#hastebin/cd \/home\/haste-server \&\& pm2 start hasteBinStart.json/g" /start.sh


echo "Your installation has completed, you may access your installation at: http://ssh.surf:$port"
echo ""
echo ""
echo "You may also create a subdomain for your installation by usoing the following commands in discord"
echo "+dns register yoursubdomainhere"
echo "+dns vhost $port"
echo "The installation will then be avalible over encrypted SSL at https://yoursubdomainhere.ssh.surf"

echo "Sending discord notification to your account:"
notif "Your hastebin installation has completed, you may access your installation at: http://ssh.surf:$port"


