#!/bin/bash

# This script will install a GUI for the Wireguard VPN. The GUI runs
# as a binary file, fed by simple html, css, and javascript, with a
# few capabilities given by the NeutralinoJS open source project
# withint the operating system itself.
#
#
# As part of my project, I do use the ability to run terminal (bash)
# commands through NeutralinoJS, and being that those commands
# require super user (sudo) privileges, you need to modify a single
# configuration file in the project in order to use the project.
#
#
# please change the password in your file under the
#
# /opt/Vector-Client/app/assets
#
# directory with the file named config.js to have your
# sudo password.  Also, if you don't use wg0 as the interface name
# you should change that in this file as well.
#

# 1. Go get the project.

wget https://github.com/bmcgonag/Vector-Client/releases/download/0.6.0/Vector-Client.tar.gz

# 2. extract the file to our directory

sudo tar -xvzf Vector-Client.tar.gz -C /opt/

echo "Would you like to make a Desktop file to access the application (y/n) ?"
read makeDesktop

if [[ $makeDesktop == "Y" || $makeDesktop == "y" ]]; then

	# 3. Create a .desktop file to call the application from
	echo "#!/usr/bin/env xdg-open" >> $HOME/Desktop/Wireguard.desktop
	echo "[Desktop Entry]" >> $HOME/Desktop/Wireguard.desktop
	echo "Version=0.6.0" >> $HOME/Desktop/Wireguard.desktop
	echo "Type=Application" >> $HOME/Desktop/Wireguard.desktop
	echo "Terminal=false" >> $HOME/Desktop/Wireguard.desktop
	echo "Icon=/opt/Vector-Client/app/assets/Vector_Logo.png" >> $HOME/Desktop/Wireguard.desktop
	echo "Icon[en_US]=/opt/Vector-Client/app/assets/Vector_Logo.png " >> $HOME/Desktop/Wireguard.desktop
	echo "Exec=sh /opt/Vector-Client/wgvpn.sh " >> $HOME/Desktop/Wireguard.desktop
	echo "Name[en_US]=Vector-Clinet" >> $HOME/Desktop/Wireguard.desktop
	echo "Comment[en_US]=Wireguard GUI Client" >> $HOME/Desktop/Wireguard.desktop

	# 4. Make the desktop file executable

	chmod a+x $HOME/Desktop/Wireguard.desktop
fi

echo "Would you like to add a shortcut to your applications menu (y/n) ?"
read addMenuShortcut

if [[ $addMenuShortcut == "Y" || $addMenuShortcut == "y" ]]; then
	# 4b. Create a .desktop file for the file menu
	echo "#!/usr/bin/env xdg-open" >> $HOME/.local/share/applications/Wireguard.desktop
	echo "[Desktop Entry]" >> $HOME/.local/share/applications/Wireguard.desktop
	echo "Version=0.6.0" >> $HOME/.local/share/applications/Wireguard.desktop
	echo "Type=Application" >> $HOME/.local/share/applications/Wireguard.desktop
	echo "Terminal=false" >> $HOME/.local/share/applications/Wireguard.desktop
	echo "Icon=/opt/Vector-Client/app/assets/Vector_Logo.png" >> $HOME/.local/share/applications/Wireguard.desktop
	echo "Icon[en_US]=/opt/Vector-Client/app/assets/Vector_Logo.png " >> $HOME/.local/share/applications/Wireguard.desktop
	echo "Exec=sh /opt/Vector-Client/wgvpn.sh " >> $HOME/.local/share/applications/Wireguard.desktop
	echo "Name=Vector-Client" >> $HOME/.local/share/applications/Wireguard.desktop
	echo "Comment=Wireguard GUI Client" >> $HOME/.local/share/applications/Wireguard.desktop
fi

# 5. Set the user's sudo password in the config.js file
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo "You will now be asked to enter your sudo (super user) password."
echo "This password must be used to bring up and take down the Wireguard interface."
echo ""
echo ""
echo "*****     NOTE: Your password, if entered will be typed in plain text"
echo "*****     (viewable to anyone looking at your screen when you type"
echo "*****      it).  If you are uncomfortable with this, you can simply"
echo "*****     press the Enter / Return key to move on.  The password will"
echo "*****     be left blank in the configuration file.  If you leave"
echo "*****     it blank, please to to the file in"
echo ""
echo "          /opt/Vector-Client/app/assets/config.js"
echo ""
echo "*****     and enter your sudo password in that file in the indicated line."
echo ""
echo ""
echo ""
echo ""
sleep 5

echo "Please enter your super user (sudo) password:"
read userPass

sudo sed -i 's/sudo_user_pass: "you-password-here"/sudo_user_pass: "'"$userPass"'"/g' /opt/Vector-Client/app/assets/config.js

echo ""
echo ""

clear

echo ""
echo "Thank you! The Wireguard UI Client should now be installed an showing on your desktop.  You will want to import a Wireguard interface configuration if you don't already have an interface setup."

echo ""
echo ""
echo ""
echo ""
echo ""

