#!/bin/bash
#set -e
##################################################################################################################
# Author    : Erik Dubois
# Website   : https://www.erikdubois.be
# Website   : https://www.alci.online
# Website   : https://www.arcolinux.info
# Website   : https://www.arcolinux.com
# Website   : https://www.arcolinuxd.com
# Website   : https://www.arcolinuxb.com
# Website   : https://www.arcolinuxiso.com
# Website   : https://www.arcolinuxforum.com
##################################################################################################################
#
#   DO NOT JUST RUN THIS. EXAMINE AND JUDGE. RUN AT YOUR OWN RISK.
#
##################################################################################################################
#tput setaf 0 = black 
#tput setaf 1 = red 
#tput setaf 2 = green
#tput setaf 3 = yellow 
#tput setaf 4 = dark blue 
#tput setaf 5 = purple
#tput setaf 6 = cyan 
#tput setaf 7 = gray 
#tput setaf 8 = light blue
##################################################################################################################

#[chaotic-aur]
#SigLevel = Required DatabaseOptional
#Include = /etc/pacman.d/chaotic-mirrorlist

# Set the file path
FILE="/etc/pacman.conf"

# Search for "chaotic-aur" in the specified file
if ! grep -q "chaotic-aur" "$FILE"; then
    echo "Adding chaotic-aur repository to $FILE"
    # Append the configuration for chaotic-aur to the file
    {
echo ""
echo "#[chaotic-aur]"
echo "#SigLevel = Required DatabaseOptional"
echo "#Include = /etc/pacman.d/chaotic-mirrorlist"
    } | sudo tee -a "$FILE"
fi

if grep -q "#\[chaotic-aur\]" /etc/pacman.conf; then
    echo "###############################################################################"
    echo "Activate the chaotic repo"
    echo "###############################################################################"
    sudo sed -i "s/#\[chaotic-aur\]/\[chaotic-aur\]/g" /etc/pacman.conf
    sudo sed -i "s/#SigLevel = Required DatabaseOptional/SigLevel = Required DatabaseOptional/g" /etc/pacman.conf
    sudo sed -i "s/#Include = \/etc\/pacman.d\/chaotic-mirrorlist/Include = \/etc\/pacman.d\/chaotic-mirrorlist/g" /etc/pacman.conf
else
    echo "###############################################################################"
    echo "Deactivate the chaotic repo"
    echo "###############################################################################"
    # Comment out only the relevant lines under chaotic-aur section
    sudo sed -i '0,/\[chaotic-aur\]/s/\[chaotic-aur\]/#\[chaotic-aur\]/' /etc/pacman.conf
    sudo sed -i '/\[chaotic-aur\]/,/#Include = \/etc\/pacman.d\/chaotic-mirrorlist/s/Include = \/etc\/pacman.d\/chaotic-mirrorlist/#Include = \/etc\/pacman.d\/chaotic-mirrorlist/' /etc/pacman.conf
    sudo sed -i '/\[chaotic-aur\]/,/#Include = \/etc\/pacman.d\/chaotic-mirrorlist/s/SigLevel = Required DatabaseOptional/#SigLevel = Required DatabaseOptional/' /etc/pacman.conf
fi

echo
echo "###############################################################################"
echo "###                DONE - YOU CAN CLOSE THIS WINDOW                        ####"
echo "###############################################################################"
