#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")")"

function error {
  echo -e "\\e[91m$1\\e[39m"
  exit 1
}
  clear='\033[0m'
  green='\033[1;32m'
  cyan='\033[1;36m'
  yellow='\033[0;33m'
  grey='\033[0;37m'
    echo -e "${green}           __          __   _            __ "
    echo -e "          / /   ____  / /__(_)___  ___  / /_"
    echo -e "         / /   / __ \/ //_/ / __ \/ _ \/ __/ "
    echo -e "        / /___/ /_/ / ,< / / / / /  __/ /_"
    echo -e "       /_____/\____/_/|_/_/_/ /_/\___/\__/ "
    echo -e "${yellow}Pi-Apps Installer by Minotaurware.net${grey}_______________  "
    echo -e "|___|___|___|___|___|___|___|___|___|___|___|___|___| "
    echo -e "|_|___|___|___|___|___|___|___|___|___|___|___|___|_| "
    echo -e "${clear}" 
echo "Install public key and add packages."
  
  sudo modprobe tun
  sudo curl -so /etc/apt/trusted.gpg.d/oxen.gpg https://deb.oxen.io/pub.gpg
  echo "deb https://deb.oxen.io $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/oxen.list
  
echo "Resync package repositories."

  sudo apt update

echo "Install Lokinet-GUI"

  sudo apt install lokinet-gui -y

echo "Enable tun module in systemd"

if [ ! -e /etc/systemd/system/lokinet.service.d/override.conf ]; then
echo "Apply systemd tun override"
sudo mkdir /etc/systemd/system/lokinet.service.d/
echo '[Service]' | sudo tee -a /etc/systemd/system/lokinet.service.d/override.conf >/dev/null
echo 'ExecStartPre=+modprobe tun' | sudo tee -a /etc/systemd/system/lokinet.service.d/override.conf >/dev/null
sudo systemctl daemon-reload
fi

echo "Install resolvconf and configure"

  sudo apt install resolvconf -y
  sudo resolvconf -u
  sudo systemctl restart lokinet
  #/usr/bin/lokinet-gui --notray >/dev/null 2>&1 &
 
echo "Installation complete!"