# Sekidots Install Script
# Install base system and hyprland before running this script

# --- Collect all input upfront ---
echo "Select AUR helper:"
echo "1) paru (build from source)"
echo "2) paru-bin"
echo "3) yay (build from source)"
echo "4) yay-bin"
read -rp "Choice [1-4]: " AUR_CHOICE

read -rp "Setup personal data (passwords, music)? [y/N] " PERSONAL

if [[ "$PERSONAL" =~ ^[Yy]$ ]]; then
    read -rp "Enter path to GPG private key file: " GPG_KEY_PATH
fi
# ---------------------------------

# Update and Install Necessary Packages
echo "Installing necessary packages..."
sleep 5
cd $HOME
sudo pacman -Syu --needed --noconfirm \
    git \
    github-cli \
    base-devel

case $AUR_CHOICE in
    1) AUR_PKG="paru";      AUR="paru" ;;
    2) AUR_PKG="paru-bin";  AUR="paru" ;;
    3) AUR_PKG="yay";       AUR="yay"  ;;
    4) AUR_PKG="yay-bin";   AUR="yay"  ;;
    *) echo "Invalid choice, defaulting to yay-bin"; AUR_PKG="yay-bin"; AUR="yay" ;;
esac

git clone https://aur.archlinux.org/${AUR_PKG}.git /tmp/${AUR_PKG}
cd /tmp/${AUR_PKG}
makepkg -si --noconfirm
cd $HOME
rm -rf /tmp/${AUR_PKG}
echo "Done!"

echo "Setting up dotfiles repository..."
sleep 5
gh auth status &>/dev/null || gh auth login
gh repo clone sekiryl/sekidots ~/dotfiles -- --recurse-submodules
echo "Done!"

# 1. Installing the Shell
echo "Installing the shell..."
sleep 5
$AUR -S --needed --noconfirm \
    caelestia-shell-git \
    caelestia-cli-git
mkdir -p ~/.config/quickshell
git clone https://github.com/sekiryl/caelestia-shell.git ~/.config/quickshell/caelestia
cd ~/.config/quickshell/caelestia/
sh build_and_install.sh
cd $HOME
PYTHON_VER=$(python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
sudo cp -r ~/dotfiles/.config/caelestia/sekiratte /lib/python${PYTHON_VER}/site-packages/caelestia/data/schemes/
caelestia scheme set -n sekiratte -m dark
echo "Done!"

# 2a. Installing the Packages
echo "Installing packages..."
sleep 5

sudo pacman -S --needed --noconfirm \
    acpi \
    adw-gtk-theme \
    alsa-utils \
    alsa-plugins \
    android-file-transfer \
    archlinux-xdg-menu \
    audacity \
    base \
    base-devel \
    bat \
    bc \
    blueman \
    bluez \
    bluez-utils \
    btop \
    cava \
    cliphist \
    cups \
    deno \
    dolphin \
    efibootmgr \
    ethtool \
    exfat-utils \
    eza \
    fd \
    fdupes \
    feh \
    ffmpegthumbnailer \
    firefox \
    font-manager \
    fzf \
    gamemode \
    gamescope \
    gimp \
    git \
    glow \
    gnome-disk-utility \
    gnome-themes-extra \
    godot \
    gvfs-dnssd \
    gvfs-mtp \
    hostapd \
    html-xml-utils \
    htop \
    hypridle \
    hyprutils \
    hyprland \
    hyprpicker \
    hyprsunset \
    inetutils \
    intel-media-driver \
    intel-ucode \
    iw \
    jless \
    jq \
    jre21-openjdk \
    kdenlive \
    keyd \
    kid3 \
    kitty \
    lftp \
    libva-intel-driver \
    libva-utils \
    libwebp-utils \
    linux \
    linux-firmware \
    linux-headers \
    lzip \
    man-db \
    mangohud \
    meld \
    mold \
    mpc \
    mpv \
    nautilus \
    neovim \
    net-tools \
    nethogs \
    nmap \
    noto-fonts-cjk \
    nwg-look \
    openbsd-netcat \
    otf-font-awesome \
    rofi-calc \
    rofi-emoji \
    rofi-wayland \
    papirus-icon-theme \
    partitionmanager \
    pass \
    pass-otp \
    pavucontrol \
    perl-file-mimeinfo \
    perl-image-exiftool \
    pipewire \
    pipewire-alsa \
    pipewire-jack \
    pipewire-pulse \
    playerctl \
    plymouth \
    polkit-gnome \
    power-profiles-daemon \
    progress \
    pv \
    python-pip \
    python-pipx \
    qbittorrent \
    qt5-tools \
    qtcreator \
    reflector \
    rust \
    scapy \
    scrcpy \
    sddm \
    seahorse \
    stow \
    stress-ng \
    telegram-desktop \
    thunderbird \
    tidy \
    traceroute \
    translate-shell \
    tree-sitter-cli \
    ueberzugpp \
    unrar \
    usbutils \
    veracrypt \
    viu \
    vulkan-tools \
    wev \
    winetricks \
    wireguard-tools \
    wl-clipboard \
    xarchiver \
    xf86-video-intel \
    xorg-server-xvfb \
    xorg-xcursorgen \
    xorg-xhost \
    xprintidle \
    yarn \
    yazi \
    zathura-pdf-mupdf \
    zenity \
    zoxide \
    zram-generator \
    zsh

$AUR -S --needed --noconfirm --norebuild \
    app2unit-git \
    arch-update \
    bluetuith-bin \
    darkly-bin \
    gtk-engine-murrine \
    gtkmm \
    heroic-games-launcher-bin \
    jamesdsp-pipewire-bin \
    jbig2enc \
    localsend-bin \
    nitch \
    oxicord-bin \
    polymc-qt5-bin \
    qt5-styleplugins \
    qt5ct-kde \
    qt6ct-kde \
    rofi-pass \
    rofi-bluetooth-git \
    rtw88-dkms-git \
    surge-bin \
    tessen \
    ttf-rubik-vf \
    ventoy-bin \
    vesktop-bin

# 2b. Install termusic with pitch correction patch
curl -fsSL https://cdn.sekiryl.is-a.dev/scripts/install_termusic.sh | sh
echo "Done!"

# 3. Install Configurations
echo "Installing configurations..."
sleep 5
cd ~/dotfiles
rm -rf ~/.config/*
rm -rf ~/.*
stow .
cd $HOME
echo "Done!"

# 4. Setup keyd
echo "Setting up keyd..."
sleep 5
sudo mkdir -p /etc/keyd
sudo cp ~/dotfiles/keyd/default.conf /etc/keyd/default.conf
sudo systemctl enable --now keyd
echo "Done!"

# 5. Setup Pass
if [[ "$PERSONAL" =~ ^[Yy]$ ]]; then
echo "Setting up Pass..."
gpg --import "$GPG_KEY_PATH"
gh repo clone sekiryl/pwd-store ~/.password-store
echo "Done!"
fi

# 6. Setup Music Library
if [[ "$PERSONAL" =~ ^[Yy]$ ]]; then
echo "Setting up music library..."
sleep 5
rclone sync Music:Music ~/Music --checksum
echo "Done!"
fi

# 7. Install Papirus-Sekiratte
echo "Installing Papirus-Sekiratte..."
sleep 5
cd ~/dotfiles/utilities/PapirusSekiratteInstaller
python3 -m venv venv
~/dotfiles/utilities/PapirusSekiratteInstaller/venv/bin/pip install -r requirements.txt
sh install.sh
cd $HOME
echo "Done!"

# 8. Setup SDDM
echo "Installing SDDM theme..."
sleep 5
sudo mkdir -p /usr/share/sddm/themes/
sudo cp -r ~/dotfiles/sddm/sddm-astronaut-theme /usr/share/sddm/themes/
sudo cp -f ~/dotfiles/sddm/sddm.conf /etc/sddm.conf
echo "Done!"

# 9. Setup Plymouth
echo "Installing Plymouth theme..."
sleep 5
sudo mkdir -p /usr/share/plymouth/themes/
sudo cp -r ~/dotfiles/plymouth/circuit /usr/share/plymouth/themes/
sudo cp -f ~/dotfiles/plymouth/plymouthd.conf /etc/plymouth/plymouthd.conf
sudo plymouth-set-default-theme -R circuit
echo "Done!"

# 10. Changing default shell to zsh
chsh -s /bin/zsh $(whoami)

# 10. Reboot
echo "Installation complete! Rebooting in 10s..."
sleep 10
reboot
