#!/bin/bash

# this file goes into the ApolloVNCServer folder 
# => aka "Linux" folder in release

function show_title () {
    echo "******************************"
    echo ">>> $1"
    echo "******************************"
}
show_title "ApolloVNC installer"

echo "Save PWD"
mkdir $HOME/.avnc
echo "$PWD" > $HOME/.avnc/install_pwd

echo "Update system"
sudo apt-get update
sudo apt-get upgrade

echo "Install needed packages"
sudo apt-get install samba lhasa xclip libx11-dev xclip x11vnc cifs-utils caja-share yt-dlp

echo "Set a password vor x11vnc"
echo "For default, enter: c0de5a6a"
x11vnc -storepasswd

echo "Adding SAMBA entry to /etc/samba/smb.conf"
echo "Check if backup exists"
if [ -f smb.conf.bak ]; then
    echo "Backup for /etc/samba/smb.conf already done!"
else
    sudo cp /etc/samba/smb.conf $HOME/smb.conf.bak
    echo "Backup of /etc/samba/smb.conf written to smb.conf.bak"
fi
echo "Add a new share ApolloVNC ..."
cp smb.conf.bak smb.conf.new
echo "[ApolloVNC]" >> smb.conf.new 
echo "    comment = Share the Magic of ApolloVNC ;-)" >> smb.conf.new 
echo "    path = $HOME" >> smb.conf.new 
echo "    read only = no" >> smb.conf.new 
echo "    browsable = yes" >> smb.conf.new 
echo "    guest ok = no" >> smb.conf.new 
echo "    valid users = $USER" >> smb.conf.new 
echo "    create mask = 0775" >> smb.conf.new 
sudo cp smb.conf.new /etc/samba/smb.conf
rm smb.conf.new

echo "Set a SAMBA password..."
echo "For default, enter: c0de5a6a"
sudo smbpasswd -a $USER

echo "Restart SAMBA demon..."
sudo service smbd restart

echo "Update firewall rules for SAMBA."
sudo ufw allow samba

echo "Make exes and scripts executable..."
sudo chmod 777 avs
sudo chmod 777 ApolloVNCServer
sudo chmod 777 e4v
sudo chmod 777 ./tools/yt-dlp
echo "Update yt-dlp ..."
cd tools
./yt-dlp -U
cd ..

echo ""
echo "Information needed for installation on Vampire:"
echo ""
echo "Server-IP:"
hostname -I
echo "Username:"
echo $USER
echo "Password"
echo "(not shown)"
echo ""
echo "=> Use configure_avnc on your Vampire to write these values to the avc icon!"
