#!/usr/bin/env bash

if [ "$(id -u)" -eq 0 ]; then 
    printf "Don't run this script as root!"
    exit 1
else 
    printf "not root"
fi

GREEN="$(tput setaf 2)[OK]$(tput sgr0)"
RED="$(tput setaf 1)[ERROR]$(tput sgr0)"
YELLOW="$(tput setaf 3)[NOTE]$(tput sgr0)"
CAT="$(tput setaf 6)[ACTION]$(tput sgr0)"
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
clear
LOG="install.log"
HYPRMOONARCH_DIR=$PWD

set -e

function pkg_inst () {
    log "Checking if $1 is installed..."
    if pacman -Qs $1; then
        log "Found $IS_INSTALLED ...skipping"
    else
        log "$1 not found. Installing..."
        paru -S $1 2>&1 | tee -a $LOG
    fi
}

function inst_paru {
    echo "-"
    echo "Installing AUR helper ${BOLD}paru${NORMAL}?"
    echo "${BOLD}paru${NORMAL} is mandatory."

    if command -v paru > /dev/null 2>&1; then
        echo "${BOLD}paru ${NORMAL}already installed. Skipping..."
    else
        echo -n "${BOLD}Continue?${NORMAL} (Y/N) "
        read confirm_paru
        if [[ $confirm_paru == "Y" ]]; 
        then
            sudo pacman -Syu --needed base-devel 2>&1 | tee -a $LOG
            sudo pacman -Syu rust 2>&1 | tee -a $LOG

            rm -rf $HYPRMOONARCH_DIR/.cache/paru-git
            git clone https://aur.archlinux.org/paru.git $HYPRMOONARCH_DIR/.cache/paru-git
            cd $HYPRMOONARCH_DIR/.cache/paru-git
            makepkg -si
            cd $HYPRMOONARCH_DIR

            git pull

            mkdir -p $HOME/.local/bin
            echo 'PATH="$HOME/.local/bin:$PATH"' | sudo tee -a /etc/environment >/dev/null

        else
            echo "exiting"
            exit 1
        fi
    fi
}

function inst_sddm {
    echo "-"
    echo "Installing SDDM - Display/Login Manager"

    declare -a pkg_list=(sddm qt6-5compat qt6-declarative qt6-svg sddm)
    for key in "${pkg_list[@]}"
    do
        pkg_inst $key
    done
    sddm --example-config > $HYPRMOONARCH_DIR/.cache/sddm.conf
    sudo mv $HYPRMOONARCH_DIR/.cache/sddm.conf /etc/sddm.conf

    echo "set default user image"
    sudo cp -f $HYPRMOONARCH_DIR/usr/share/sddm/faces/.face /usr/share/sddm/faces/.face
    cp -f $HYPRMOONARCH_DIR/usr/share/sddm/faces/.face $HOME/.face

    cd $HYPRMOONARCH_DIR/.cache
    # wget --unlink https://github.com/catppuccin/sddm/releases/download/v1.0.0/catppuccin-mocha.zip
    # sudo unzip catppuccin-mocha.zip -d /usr/share/sddm/themes/
    rm -rf moonarch-sddm
    git clone https://gitea.moonarch.de/nevaforget/moonarch-sddm.git
    sudo cp -Rf moonarch-sddm /usr/share/sddm/themes/
    sudo rm -rf /usr/share/sddm/themes/moonarch-sddm/.git
    sudo sed -i "s/^Current=.*/Current=moonarch-sddm/g" /etc/sddm.conf

    sudo systemctl disable display-manager
    sudo systemctl enable sddm

    cd $HYPRMOONARCH_DIR
}

function inst_pkgs {
    log "-"
    log "Installing ${BOLD}Arch packages.${NORMAL}"
    log "This includes all neccessary dependencies. You definitely want this."
    log "${BOLD}Continue?${NORMAL} (Y/N) "
    read confirm_basics
    log "$confirm_basics"
    if [[ $confirm_basics == "Y" ]]; 
    then

        declare -a pkg_list=(wayland stow jq network-manager-applet alarm-clock-applet xarchiver man-db hardinfo networkmanager-openvpn udisks2 plocate vim alacritty gettext-hostname lsd alsa-utils ponymix btop polkit polkit-gnome timeshift timeshift-autosnap pcmanfm-gtk3 flatery-icon-theme-git librewolf-bin fzf unzip hyprland hypridle hyprlock hyprcursor hyprkeys hyprpicker swww waypaper xdg-desktop-portal-hyprland wdisplays waybar-cava-git nwg-look cliphist wlogout hyprlock waybar-module-pacman-updates ufw zsh oh-my-zsh noise-suppression-for-voice grim satty-bin slurp dunst sweet-cursor-theme-git sweet-cursors-hyprcursor-git catppuccin-gtk-theme-mocha rofi-lbonn-wayland-git cmake meson cpio pkg-config)

        for key in "${pkg_list[@]}"
        do
            pkg_inst $key
        done

        sudo cp -Rf $HYPRMOONARCH_DIR/usr/local/bin/* /usr/local/bin/

        pkg_inst "informant"
        sudo informant read --all
        #$(getent group informant) ] || groupadd informant
        #sudo usermod -a -G informant $USER

    fi

    cd $HYPRMOONARCH_DIR
}

function inst_bluet {
    echo "-"
    echo "Installing ${BOLD}Bluetooth${NORMAL} Support"
    echo -n "${BOLD}Continue?${NORMAL} (Y/N) "
    read confirm_bluet
    if [[ $confirm_bluet == "Y" ]]; 
    then
        declare -a pkg_list=(bluez bluez-utils-compat blueberry)
        for key in "${pkg_list[@]}"
        do
            pkg_inst $key
        done
  
        sudo systemctl start bluetooth.service
        sudo systemctl enable bluetooth.service
    fi

    cd $HYPRMOONARCH_DIR
}

function inst_davfs {
    echo "-"
    echo "Installing ${BOLD}WebDAV / davfs2${NORMAL} support, including pcmanfm support"
    echo -n "${BOLD}Continue?${NORMAL} (Y/N) "
    read confirm_davfs
    if [[ $confirm_davfs == "Y" ]]; 
    then
        declare -a pkg_list=(davfs2 gvfs gvfs-dnssd)
        for key in "${pkg_list[@]}"
        do
            pkg_inst $key
        done

        if [ $(getent group admin) ]; then
          sudo usermod -a -G davfs2 $USER
        fi
    fi

    cd $HYPRMOONARCH_DIR
}

function inst_clamav {
    echo "-"
    echo "Installing ClamAV - Anti-Virus Scanner with freshclam and fangfrisch"
    echo -n "${BOLD}Continue?${NORMAL} (Y/N) "
    read confirm_clamav
    if [[ $confirm_clamav == "Y" ]]; 
    then
        declare -a pkg_list=(clamav python-fangfrisch)
        for key in "${pkg_list[@]}"
        do
            pkg_inst $key
        done

        sudo systemctl start clamav-daemon.service
        sudo systemctl enable clamav-daemon.service
        sudo freshclam
        sudo -u clamav /usr/bin/fangfrisch --conf /etc/fangfrisch/fangfrisch.conf initdb
        sudo systemctl enable fangfrisch.timer

        if command -v nemo &> /dev/null
        then
            cp -Rf $HYPRMOONARCH_DIR/usr/share/nemo/actions/clamscan.nemo_action /usr/share/nemo/actions
        fi

    fi

    cd $HYPRMOONARCH_DIR
}

function rebooter {
    echo "-"
    echo "I recommend to reboot at this point. But you should at least log out and in again."
    echo -n "${BOLD}Continue with Reboot?${NORMAL} (Y/N) "
    read confirm_rebot
    if [[ $confirm_rebot == "Y" ]]; 
    then
        sudo reboot
    fi
}

function setup {
    # ZSH
    log "Cloning ZSH Plugins..."
    sudo rm -rf ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
    sudo git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
    log "Done... zsh-autosuggestions"
    sudo rm -rf ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
    sudo git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
    log "Done... zsh-syntax-highlighting"
    sudo rm -rf ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
    sudo git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
    log "Done... powerlevel10k"
    chsh -s /usr/bin/zsh
    log "Done... Set zsh as default shell"

    # GUI
    log "Set up GUI Themes" 
    gsettings set org.gnome.desktop.interface gtk-theme "Catppuccin-Mocha-Standard-Lavender-Dark"
    log "Done... Window decorations"
    gsettings set org.gnome.desktop.interface color-scheme 'prefer-dark'
    log "Done... Set color-scheme to prefer-dark"
    gsettings set org.gnome.desktop.interface cursor-theme Sweet-cursors
    log "Done... Set a sweet cursor"

    hyprpm update
    hyprpm add https://github.com/DreamMaoMao/hycov
    hyprpm enable hycov

    # Extra Font
    sudo mkdir -p /usr/local/share/fonts/
    sudo install -C $HYPRMOONARCH_DIR/usr/local/share/fonts/MonarchOS.ttf /usr/local/share/fonts/MonarchOS.ttf
    sudo install -C $HYPRMOONARCH_DIR/usr/local/share/fonts/Icomoon-Feather.ttf /usr/local/share/fonts/Icomoon-Feather.ttf
    sudo install -C $HYPRMOONARCH_DIR/etc/fonts/local.conf /etc/fonts/local.conf
    fc-cache -f

    # Wallpaper
    WALLPAPER_DIR=$HOME/Pictures/Wallpaper
    if [ ! -d "$WALLPAPER_DIR" ]; then
        mkdir -p $WALLPAPER_DIR
    fi
    cp -Rf $HYPRMOONARCH_DIR/home/Pictures/Wallpaper/* $WALLPAPER_DIR/

    waypaper --wallpaper $WALLPAPER_DIR/1586853771_daniel-leone-v7datklzzaw-unsplash-modded.jpg

    # dotfiles
    log "Time stow things up"
    cp -R $HYPRMOONARCH_DIR/home/config/ $HOME/.hyprm/.config/
    cp -R $HYPRMOONARCH_DIR/home/.bashrc $HOME/.hyprm/.bashrc
    cp -R $HYPRMOONARCH_DIR/home/.zshrc $HOME/.hyprm/.zshrc
    cp -R $HYPRMOONARCH_DIR/home/.p10k.zsh $HOME/.hyprm/.p10k.zsh
    cd $HOME/.hyprm/
    stow --adopt .

    cd $HYPRMOONARCH_DIR
}

function log () {
    ts=$(date +"%H:%M:%S")

    printf "$ts $1\n" | tee -a $LOG
}

function init {

    rm -f $LOG

    mkdir -p $HYPRMOONARCH_DIR/.cache
    mkdir -p $HOME/.hyprm

    log "Installation start\n"

    inst_paru

    type paru >/dev/null 2>&1 || { echo >&2 "I require paru but it's not installed.  Aborting."; exit 1; }

    inst_pkgs
    inst_sddm

    setup

    inst_bluet
    inst_clamav
    inst_davfs

    rebooter
}

printf "#####################################\n"
printf "$(tput setaf 2)Welcome to MoonArch. Feel free.\n@version 1.0\n$(tput sgr0)"
printf "#####################################\n"
printf "\n"
printf "$(tput setaf 1)Don't run this script as ROOT!\n$(tput sgr0)"
printf "\n"
printf "${YELLOW} ${BOLD}This will clear and overwrite some config files\nand this can not be reverted. No Backups are created!\n " 
printf "\n"
echo -n "${BOLD}Continue?${NORMAL} (Y/N) "
read confirm

if [[ $confirm == "Y" ]]; 
then
    init
else
    echo "Goodbye sweet butterfly"
    exit 1
fi