#!/bin/bash


key_status1=$(env LANG=C pacman-key -f valeria@fadeeva.me 2>&1 | grep "unknown")

key_status2=$(env LANG=C pacman-key -f erik.dubois@gmail.com 2>&1 | grep "unknown")

if [[ -z $key_status1 ]] && [[ -z $key_status2 ]]; then
    exit
fi

if [[ "$UID" != 0 ]]; then
    counter=0

    for group in $(groups)
    do
        if [ "${group}" == "wheel" ] || [ "${group}" == "root" ]; then
            counter=$(( counter + 1 ))
            echo "The user '${USER}' is a member of the group ${group}"
            if [ ! -x "$0" ]; then
                chmod +x "$0"
            fi
            sudo /bin/sh -c "$0"
            exit
        fi
    done

    if [[ $counter -eq 0 ]]; then
        echo "The user '${USER}' does not have permission to run this program."
        echo "The user '${USER}' is not a member of the wheel or root group."
        echo ""
        echo -e "Add the user '${USER}' to the wheel group - COPY AND PASTE in new konsole:\n
                    su -c \"usermod -a -G wheel ${USER}\"; qdbus org.kde.LogoutPrompt /LogoutPrompt promptLogout; exit"
        echo ""
        echo "Or otherwise escalate privileges and run the program \"${HOME}/.config/autostart/$(basename ${0}).desktop\" again."
        echo ""
        echo -e "Help: https://wiki.archlinux.org/title/Sudo \nhttps://wiki.archlinux.org/title/Users_and_groups"
        echo ""
        echo "The program path is the ${HOME}/.local/bin/$(basename ${0})"
        echo "The shortcut path is the ${HOME}/.config/autostart/$(basename ${0}).desktop"
        echo ""
        echo "Waiting new konsole windows..."

        sleep 5

        /usr/bin/konsole
        exit
    fi
fi

# if [[ "$UID" != 0 ]]; then
#     echo "USER NOT ROOT"
#     sudo "$0"
#     exit
# else
#     echo "USER IS ROOT"
# fi

grep_text=$(ss -nlp | grep 9050)
if [[ "$?" == 0 ]]; then
    export {http,https,ftp}_proxy="socks5://127.0.0.1:9050"
fi

ping -c 1 1.1.1.1 > /dev/null
have_internet="$?"

cd "/root"

rm  -rf /var/lib/pacman/sync/*
rm -rf /etc/pacman.d/gnupg/*

mkdir -p /root/.gnupg 2>/dev/null
chmod 700 /root/.gnupg

haveged -w 1024
pacman-key --init
pkill haveged

pacman-key --add /usr/share/pacman/keyrings/melawy-linux.gpg
pacman-key --lsign-key BC8B600E8DDA1F4CB77B10D2BA803A261A5EE6B8
pacman-key --lsign-key B175815513971572FE7B1557CEC07AC8FF64DA4B

# if [ "$have_internet" -eq 0 ]; then
#     pacman-key --keyserver hkps://keys.openpgp.org --recv-keys BC8B600E8DDA1F4CB77B10D2BA803A261A5EE6B8
#     pacman-key --keyserver hkps://keyserver.ubuntu.com --recv-keys BC8B600E8DDA1F4CB77B10D2BA803A261A5EE6B8
#     pacman-key --lsign-key BC8B600E8DDA1F4CB77B10D2BA803A261A5EE6B8
#
#     pacman-key --keyserver hkps://keys.openpgp.org --recv-keys B175815513971572FE7B1557CEC07AC8FF64DA4B
#     pacman-key --keyserver hkps://keyserver.ubuntu.com --recv-keys B175815513971572FE7B1557CEC07AC8FF64DA4B
#     pacman-key --lsign-key B175815513971572FE7B1557CEC07AC8FF64DA4B
# fi

pacman-key --lsign-key melawy-linux

pacman-key --add /usr/share/pacman/keyrings/manjaro.gpg
pacman-key --lsign-key manjaro
pacman-key --add /usr/share/pacman/keyrings/cachyos.gpg
pacman-key --lsign-key cachyos
pacman-key --add /usr/share/pacman/keyrings/arcolinux.gpg
pacman-key --lsign-key arcolinux
pacman-key --add /usr/share/pacman/keyrings/chaotic.gpg
pacman-key --lsign-key chaotic
pacman-key --add /usr/share/pacman/keyrings/endeavouros.gpg
pacman-key --lsign-key endeavouros

pacman-key --populate

echo "
keyserver hkp://keyserver.ubuntu.com:80" | sudo tee --append /etc/pacman.d/gnupg/gpg.conf

pacman-key --updatedb

# if [ "$have_internet" -eq 0 ]; then
#     pacman -Sy
# fi

title="Successed!"
msg="$(date '+%Y.%m.%d %H:%M:%S')"

users=$(/usr/bin/users | sed 's| |\n|g' | sort | uniq)
for user in $users
do
    userid=$(/usr/bin/id -u "$user")

    if [ -S "/run/user/$userid/bus" ]; then
        cmd=(
            DISPLAY=:0
            DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/"$userid"/bus
            /usr/bin/notify-send
            --icon=info
            --urgency=low
            -t 5000
            -a "'Keys sign'"
            "'$title'"
            "'$msg'"
        )

        /usr/bin/su "$user" -c "${cmd[*]}"

        if [ "$user" != "root" ] && [ "$user" != "liveuser" ]; then
            if [ -f "/home/$user/.config/autostart/key-sign.sh.desktop" ]; then
            rm /home/"$user"/.config/autostart/key-sign.sh.desktop
            # rm /home/"$user"/.local/bin/key-sign.sh
            fi
        fi
    fi
done

echo "Finish!!!"
sleep 5
