#!/bin/bash

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


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 Code-Server Automated installer for Discord-Linux ARM, We are preparing your installation..."

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

wget -q https://github.com/coder/code-server/releases/download/v3.12.0/code-server_3.12.0_arm64.deb

notif "Installing Code-Server 3.12 using DPKG"

dpkg -i code-server_3.12.0_arm64.deb

rm -f code-server_3.12.0_arm64.deb

notif "Downloading our configurations"

wget -q -O /var/tools/startCodeServer.json https://ssh.surf/install/code-server/startCodeServer.json

wget -q -O /var/tools/startCodeServer.sh https://ssh.surf/install/code-server/startCodeServer.sh

chmod +x /var/tools/startCodeServer.sh

mkdir -vp /root/.config/code-server/

wget -q -O /root/.config/code-server/config.yaml https://ssh.surf/install/code-server/config.yaml

PASS=$(openssl rand -hex 12)


sed -i "s/replaceme/${PASS}/g" ~/.config/code-server/config.yaml

sed -i "s/#code-server/cd \/var\/tools \&\& pm2 start startCodeServer.json \&\& relayPort 8080/g" /start.sh



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/127.0.0.1:8080/0.0.0.0:${port}/g" /root/.config/code-server/config.yaml

pm2 start /var/tools/startCodeServer.json


notif "The installation has completed. You may access your install at: http://ssh.surf:$port with the password $PASS"
