#!/bin/bash

################################################################
# Script for manage your ProtonVPN connections (Dmenu VERSION) #
# Last change: 29-05-2022                                      #
# Author: q3aql                                                #
# Contact: q3aql@duck.com                                      #
# License: GPL v2.0                                            #
################################################################
VERSION="2.0"
M_DATE="290522"

# Variables
protonFiles="${HOME}/protonFiles"
url_download="https://account.protonvpn.com/downloads"
url_credentials="https://account.protonvpn.com/account#openvpn"
file_credentials="/opt/dmenu_protonvpn/proton-credentials.txt"
user_credentials="/opt/dmenu_protonvpn/proton-user.txt"
pass_credentials="/opt/dmenu_protonvpn/proton-pass.txt"
sed_file_credentials="\/opt\/dmenu_protonvpn\/proton-credentials.txt"
menu_files="/opt/dmenu_protonvpn/menu"

list_show_files() {
  echo "About"
  echo "Download ProtonVPN files"
  echo "Edit Credentials"
  if [ -d "${protonFiles}" ] ; then
    ls -1 "${protonFiles}/"
  fi
}

list_show_icons() {
  list_show_files | while read current_menu ; do
    if [ "${current_menu}" == "About" ] ; then
      echo "  ${current_menu}"
    elif [ "${current_menu}" == "Download ProtonVPN files" ] ; then
      echo "ﯲ  ${current_menu}"
    elif [ "${current_menu}" == "Edit Credentials" ] ; then
      echo "  ${current_menu}"
    else
      echo "  ${current_menu}"
    fi
  done
}

case "${TERM}" in
    xterm-color|*-256color) color_prompt=yes;;
esac
if [ "${color_prompt}" == "yes" ] ; then
  blue='\e[34m' ; red='\e[31m' ; yellow='\e[33m'
  purple='\e[35m' ; green='\e[32m' ; end='\e[0m'
else
  blue='' ; red='' ; yellow='' ; morado=''
  verde='' ; end=''
fi

if [ -f /usr/bin/kitty ] ; then
  termrun="kitty --title dmenu_proton -c=/opt/dmenu_protonvpn/config/kitty.conf -e" 
else
  xrdb -load /opt/dmenu_protonvpn/config/Xresources
  termrun="xterm -T dmenu_proton -e"  
fi

# Create proton-openvpn-files folder
mkdir -p ${protonFiles}

function openBrowser() {
  if [ -f /usr/bin/x-www-browser ] ; then
    /usr/bin/x-www-browser ${1} &
  else
    if [ -f /usr/bin/firefox ] ; then
      /usr/bin/firefox ${1}
    else
      if [ -f /usr/bin/brave-browser ] ; then
        /usr/bin/brave-browser ${1}
      else
        if [ -f /usr/bin/google-chrome ] ; then
          /usr/bin/google-chrome ${1} &
        fi
      fi
    fi
  fi
}

function insertCredentials() {
  cat ${user_credentials} > ${file_credentials}
  cat ${pass_credentials} >> ${file_credentials}
  if [ -f "${1}" ] ; then
    check_credentials=$(cat "${1}" | grep "auth-user-pass ${file_credentials}")
    if [ -z "${check_credentials}" ] ; then
     sed -i "s/auth-user-pass/auth-user-pass ${sed_file_credentials}/g" "${1}"  
    fi
  fi
  sed -i "s/up \/etc\/openvpn\/update-resolv-conf//g" "${1}"
  sed -i "s/down \/etc\/openvpn\/update-resolv-conf//g" "${1}"
}

function editCredentialsText() {
  if [ -f ${user_credentials} ] ; then
    current_user=$(cat ${user_credentials})
  else
    current_user=""
  fi
  if [ -f ${pass_credentials} ] ; then
    current_pass=$(cat ${pass_credentials})
  else
    current_pass=""
  fi
  echo "  EDIT CREDENTIALS:"
  echo ""
  echo "  Edit USER"
  echo "  Edit PASS"
  echo ""
  echo "  URL Credentials: ${url_credentials}"
  echo ""
  echo "  Configured user: ${current_user}"
  echo "  Configured pass: ${current_pass}"
  echo ""
}

function editCredentials() {
  select_output=$(editCredentialsText | /opt/dmenu_protonvpn/dmenu "$@" -p "嬨  dmenu_protonvpn v${VERSION}")
  if [ "${select_output}" == "  URL Credentials: ${url_credentials}" ] ; then                                                                                                                                                              
    openBrowser "${url_credentials}" &
    editCredentials
  elif [ "${select_output}" == "  Edit USER" ] ; then
    user_openvpn=$(echo > /dev/null | /opt/dmenu_protonvpn/dmenu "$@" -p "  User OpenVPN / IKEv2:")
    if [ -z "${user_openvpn}" ] ; then
      echo "# User canceled"
    else
      echo "${user_openvpn}" > ${user_credentials}
    fi
    cat ${user_credentials} > ${file_credentials}
    cat ${pass_credentials} >> ${file_credentials}
    editCredentials
  elif [ "${select_output}" == "  Edit PASS" ] ; then
    pass_openvpn=$(echo > /dev/null | /opt/dmenu_protonvpn/dmenu "$@" -p "  Password OpenVPN / IKEv2:")
    if [ -z "${pass_openvpn}" ] ; then
      echo "# Pass canceled"
    else
      echo "${pass_openvpn}" > ${pass_credentials}
    fi
    cat ${user_credentials} > ${file_credentials}
    cat ${pass_credentials} >> ${file_credentials}
    editCredentials
  else
    run_main
  fi 
}

function checkFileCredentials() {
  check_file=$(cat ${file_credentials} 2> /dev/null | wc -l)
  if [ ${check_file} -ne 2 ] ; then
    editCredentials
  fi
}

function aboutText() {
  echo "  ABOUT:"
  echo ""
  echo "  Software: dmenu_protonvpn ${VERSION}"
  echo "  Author: q3aql"
  echo "  Contact: q3aql@duck.com"
  echo "  License: GPL v2.0"
  echo ""
}

function showAbout() {
  aboutText | /opt/dmenu_protonvpn/dmenu "$@" -p "嬨  dmenu_protonvpn v${VERSION}"
  run_main
}

function downloadFilesText() {
  echo "ﯲ  DOWNLOAD PROTONVPN FILES:"
  echo ""
  echo "  Steps for download .ovpn files:"
  echo ""
  echo "   Open URL: ${url_download}"
  echo "   Login with your Proton account"
  echo "   Click to Downloads > OpenVPN configuration files"
  echo "   Download *.ovpn files and copy to ${HOME}/protonFiles"
  echo ""
  echo "   Press HERE for open URL with browser"
  echo ""
}

function downloadProtonVPNFiles() {
  select_output=$(downloadFilesText | /opt/dmenu_protonvpn/dmenu "$@" -p "嬨  dmenu_protonvpn v${VERSION}")
  if [ "${select_output}" == "   Press HERE for open URL with browser" ] ; then
    openBrowser "${url_download}" &
    run_main
  else
    run_main
  fi
}

function run_main() {
  echo "MAIN # Selected"
  list_output=$(list_show_icons | /opt/dmenu_protonvpn/dmenu "$@" -p "嬨  dmenu_protonvpn v${VERSION}")
  run_output=$(echo "${list_output}" | cut -c4-999)
  echo "${run_output} # Selected"
  if [ "${run_output}" == "  About" ] ; then
    showAbout
  elif [ "${run_output}" == "  Download ProtonVPN files" ] ; then
    downloadProtonVPNFiles
  elif [ "${run_output}" == "  Edit Credentials" ] ; then
    if [ -f /usr/bin/zenity ] ; then
      editCredentials
    else
      echo ${termrun} "${menu_files}/edit-credentials.sh"
      ${termrun} "${menu_files}/edit-credentials.sh"
    fi
  else
    if [ -z "${run_output}" ] ; then
      echo > /dev/null
    else
      checkFileCredentials
      if [ -f /usr/bin/sudo ] ; then
        run_output=$(echo ${run_output} | tr -s "  ")
        cp -rf ${protonFiles}/${run_output} /tmp/
        insertCredentials /tmp/${run_output}
        echo ""
        echo -e "${green}* Connecting using file ${end}${yellow}${protonFiles}/${run_output}${end}${purple}"
        ${termrun} sudo openvpn /tmp/${run_output}
        echo -e "${end}${red}* Connection closed${end}"
        echo -ne "${green}+ Press ${end}${purple}ENTER${end}${green} to return menu${end} " ; read return
      else
        run_output=$(echo ${run_output} | tr -s "  ")
        cp -rf ${protonFiles}/${run_output} /tmp/
        insertCredentials /tmp/${run_output}
        echo ""
        echo -e "${green}* Connecting using file ${end}${yellow}${protonFiles}/${run_output}${end}${purple}"
        ${termrun} su -c "openvpn /tmp/${run_output}"
        echo -e "${end}${red}* Connection closed${end}"
        echo -ne "${green}+ Press ${end}${purple}ENTER${end}${green} to return menu${end} " ; read return
      fi
    fi
  fi
}

# Exec main function
run_main

