#!/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
  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 wp-icons 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 -f Wire-Py.desktop /usr/share/applications/ 

  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
  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 wp-icons 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 -f Wire-Py.desktop /usr/share/applications/ 
   
  fi 
  }

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 $"To close the Window press a button"
      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 $"To close the Window press a button"
        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
    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 wp-icons 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 -f Wire-Py.desktop /usr/share/applications/ 

    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
    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 zypper install python311-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 wp-icons 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 -f Wire-Py.desktop /usr/share/applications/     
            
    fi
    
else
    clear
    echo $"Your System could not be determined."
    echo
    read -n 1 -s -r -p $"To close the window press a button"
    clear
    exit 0

fi
#clear
read -n 1 -s -r -p $"To close the Window press a button"
clear