#!/bin/bash
NORMAL='\033[0m'
GREEN='\033[1;32m'
RED='\033[31;1;42m'
BLUE='\033[30;1;34m'

install_file_with(){
  clear
  mkdir -p ~/.config/wire_py && touch ~/.config/wire_py/keys && cp -u settings ~/.config/wire_py/ && \
  mkdir -p ~/.config/systemd/user && cp -u wg_start.service ~/.config/systemd/user/ && \
  systemctl --user enable wg_start.service >/dev/null 2>&1
  sudo cp -f org.sslcrypt.policy /usr/share/polkit-1/actions/ 
  if [ $? -ne 0 ]
    then 
      systemctl --user disable wg_start.service
      rm -r ~/.config/wire_py && rm -r ~/.config/systemd
      exit 0
  else 
    sudo apt install python3-tk && \
    sudo cp -fv wirepy.py start_wg.py cls_mth_fc.py ssl_encrypt.py ssl_decrypt.py /usr/local/bin/ && \
    sudo cp -uR lx-icons /usr/share/icons/ &&  sudo cp -uR TK-Themes /usr/share/ && \
    sudo cp -u languages/de/*.mo /usr/share/locale/de/LC_MESSAGES/ && \
    sudo cp -fv Wire-Py.desktop /usr/share/applications/ && \
    sudo ln -sf /usr/local/bin/wirepy.py /usr/local/bin/wirepy
    sudo mkdir -p /usr/local/etc/ssl
    if [ ! -f /usr/local/etc/ssl/pwgk.pem ]
      then
        sudo openssl genrsa -out /usr/local/etc/ssl/pwgk.pem 4096
    fi
  fi  
  }

install_arch_d(){
  clear
  mkdir -p ~/.config/wire_py && touch ~/.config/wire_py/keys && cp -u settings ~/.config/wire_py/ && \
  mkdir -p ~/.config/systemd/user && cp -u wg_start.service ~/.config/systemd/user/ && \
  systemctl --user enable wg_start.service >/dev/null 2>&1
  sudo cp -f org.sslcrypt.policy /usr/share/polkit-1/actions/
  if [ $? -ne 0 ]
    then 
        systemctl --user disable wg_start.service
        rm -r ~/.config/wire_py && rm -r ~/.config/systemd
        exit 0
  else 
    sudo pacman -S --noconfirm tk python3 python-requests && \
    sudo cp -fv wirepy.py start_wg.py cls_mth_fc.py ssl_encrypt.py ssl_decrypt.py /usr/local/bin/ && \
    sudo cp -uR lx-icons /usr/share/icons/ &&  sudo cp -uR TK-Themes /usr/share/ && \
    sudo cp -u languages/de/*.mo /usr/share/locale/de/LC_MESSAGES/ && \
    sudo cp -fv Wire-Py.desktop /usr/share/applications/ && \
    sudo ln -sf /usr/local/bin/wirepy.py /usr/local/bin/wirepy 
    sudo mkdir -p /usr/local/etc/ssl
    if [ ! -f /usr/local/etc/ssl/pwgk.pem ]
      then
        sudo openssl genrsa -out /usr/local/etc/ssl/pwgk.pem 4096
    fi   
   
  fi 
  }

install(){
  if grep -i 'debian' /etc/os-release > /dev/null 2>&1
    then
      groups > /tmp/isgroup
      if grep 'sudo' /tmp/isgroup
        then
          install_file_with
      else
        echo -e "$BLUE"The installer found that they are not in the group sudo.""
        echo -e "with "$RED"su -"$BLUE" "they can enter the root shell in which they then""
        echo -e "enter "$GREEN""usermod -aG sudo $USER.""$BLUE""
        echo -e ""after logging in from the system, they can then run Wire-Py install again." $NORMAL"
        read -n 1 -s -r -p $"Press Enter to exit"
        clear
        exit 0

      fi

  elif grep -i 'mint\|ubuntu\|pop|' /etc/os-release > /dev/null 2>&1
    then
        install_file_with

  elif grep -i 'arch' /etc/os-release > /dev/null 2>&1
    then
      groups > /tmp/isgroup
      clear
      if grep 'wheel' /tmp/isgroup
        then
            install_arch_d
      else
          echo "The installer found that they are not in the group sudo."
          echo "The sudoers file must be edited with"
          echo -e "$RED""su -""$NORMAL"
          echo -e "$GREEN"""EDITOR=nano visudo"""$NORMAL"
          echo "Find the line:"
          echo "## Uncomment to allow members of group wheel to execute any command"
          echo "remove '#' on  # %wheel ALL=(ALL) ALL and save the file"
          echo -e "then enter "$GREEN"gpasswd -a $USER wheel.""$NORMAL"
          echo "after logging in from the system, they can then run Wire-Py install again."
          read -n 1 -s -r -p $"Press Enter to exit"
          clear
          exit 0

      fi

  elif grep -i '|manjaro\|garuda\|endeavour|' /etc/os-release > /dev/null 2>&1
      then
          install_arch_d

  elif grep -i 'fedora' /etc/os-release > /dev/null 2>&1
    then
      clear
      mkdir -p ~/.config/wire_py && touch ~/.config/wire_py/keys && cp -u settings ~/.config/wire_py/ && \
      mkdir -p ~/.config/systemd/user && cp -u wg_start.service ~/.config/systemd/user/ && \
      systemctl --user enable wg_start.service >/dev/null 2>&1
      sudo cp -f org.sslcrypt.policy /usr/share/polkit-1/actions/
      if [ $? -ne 0 ]
        then 
            systemctl --user disable wg_start.service
            rm -r ~/.config/wire_py && rm -r ~/.config/systemd
            exit 0
      else
        sudo dnf install python3-tkinter -y
        sudo cp -fv wirepy.py start_wg.py cls_mth_fc.py ssl_encrypt.py ssl_decrypt.py /usr/local/bin/ && \
        sudo cp -uR lx-icons /usr/share/icons/ &&  sudo cp -uR TK-Themes /usr/share/ && \
        sudo cp -u languages/de/*.mo /usr/share/locale/de/LC_MESSAGES/ && \
        sudo cp -fv Wire-Py.desktop /usr/share/applications/  && \
        sudo ln -sf /usr/local/bin/wirepy.py /usr/local/bin/wirepy
        sudo mkdir -p /usr/local/etc/ssl
        if [ ! -f /usr/local/etc/ssl/pwgk.pem ]
          then
            sudo openssl genrsa -out /usr/local/etc/ssl/pwgk.pem 4096
        fi

      fi
  elif grep -i 'suse' /etc/os-release > /dev/null 2>&1
    then
      clear
      mkdir -p ~/.config/wire_py && touch ~/.config/wire_py/keys && cp -u settings ~/.config/wire_py/ && \
      mkdir -p ~/.config/systemd/user && cp -u wg_start.service ~/.config/systemd/user/ && \
      systemctl --user enable wg_start.service >/dev/null 2>&1
      sudo cp -f org.sslcrypt.policy /usr/share/polkit-1/actions/
      if [ $? -ne 0 ]
        then 
          systemctl --user disable wg_start.service
          rm -r ~/.config/wire_py && rm -r ~/.config/systemd
          exit 0
      else 
        sudo cp -fv wirepy.py start_wg.py cls_mth_fc.py ssl_encrypt.py ssl_decrypt.py /usr/local/bin/ && \
        sudo cp -uR lx-icons /usr/share/icons/ &&  sudo cp -uR TK-Themes /usr/share/ && \
        sudo cp -u languages/de/*.mo /usr/share/locale/de/LC_MESSAGES/ && \
        sudo cp -fv Wire-Py.desktop /usr/share/applications/ && \
        sudo ln -sf /usr/local/bin/wirepy.py /usr/local/bin/wirepy 
        sudo mkdir -p /usr/local/etc/ssl
        if [ ! -f /usr/local/etc/ssl/pwgk.pem ]
          then
            sudo openssl genrsa -out /usr/local/etc/ssl/pwgk.pem 4096
        fi
        if grep -i 'Tumbleweed' /etc/os-release > /dev/null 2>&1
          then
            sudo zypper install python313-tk
        else
          sudo zypper install python36-tk
        fi
              
      fi
      
  else
      clear
      echo $"Your System could not be determined."
      echo
      read -n 1 -s -r -p $"Press Enter to exit"
      clear
      exit 0

  fi
  #clear
  read -n 1 -s -r -p $"Press Enter to exit"
  clear

  }

remove(){
    sudo rm -f /usr/local/bin/wirepy /usr/local/bin/wirepy.py /usr/local/bin/start_wg.py \
    /usr/local/bin/cls_mth_fc.py /usr/local/bin/ssl_encrypt.py /usr/local/bin/ssl_decrypt.py 
    if [ $? -ne 0 ]
      then
        exit 0
    else 
      systemctl --user disable wg_start.service
      rm -r ~/.config/wire_py && rm -r ~/.config/systemd
      sudo rm /usr/share/applications/Wire-Py.desktop
      sudo rm /usr/share/locale/de/LC_MESSAGES/languages/de/wirepy.mo
      sudo rm -r /usr/local/etc/ssl
      which syncpy >/dev/null
      if [ $? -ne 0 ]
        then 
          sudo rm -r /usr/share/icons/lx-icons &&  sudo rm -r /usr/share/TK-Themes  

      fi

      echo
      read -p "Press Enter to exit..."

    fi

  }

which wirepy >/dev/null
if [ $? -eq 0 ]
    then
        echo "Do you want to update/reinstall or uninstall wirepy?"
        echo
        echo "Update/reinstall: press y, uninstall press r"
        echo
        read -n 1 -s -r -p "Cancel with any other key..."  result
        case $result in
            [y]* ) clear; install; exit;;
            [Y]* ) clear; install; exit;;
            [j]* ) clear; install; exit;;
            [J]* ) clear; install; exit;;
            [r]* ) clear; remove; exit;;
            [R]* ) clear; remove; exit;;
        esac
        clear
else
  install       
   
fi