#!/bin/bash

# picom launcher that adapts to the environment

if [ -e /opt/instantos/potato ] || iconf -i potato; then
    echo "no compositing for you, your pc is a potato"
    exit
fi

genconfig() {

    if [ -e ~/.compton.conf ]; then
        rm ~/.compton.conf
        echo "removed old compton file"
    fi

    mkdir -p ~/.config/picom
    echo '# instantos
# remove the above comment to disable auto settings' >.config/picom/picom.conf

    if ! grep -q 'hypervisor' /proc/cpuinfo; then
        echo 'fading = true
fade-in-step = 0.15
fade-out-step = 0.15' >>.config/picom/picom.conf

        echo "not a vm"
        if iconf -i blur; then

            # possibility 3:
            tee -a .config/picom/picom.conf >/dev/null <<EOT
backend = "glx"
blur-background = true;
blur-background-frame = true;
blur-background-fixed = true;

blur-kern = "3x3box";
blur-method = "dual_kawase";
blur-strength = 5;
blur-background-exclude = [ "class_g = 'slop'", "class_g = 'Peek'", "class_g = 'Rofi'", "class_g = 'kdeconnect.daemon'"]

EOT
        fi
    else
        echo "vm detected, disabling blur"
        if pgrep Xorg; then
            imenu -t 'blur causes massive issues on virtual machines.
Manually edit ~/.config/picom/picom.conf to force blur. You have been warned'
        fi
    fi

    if iconf -i vsync; then
        echo "vsync=true" >>.config/picom/picom.conf
    fi

}

cd || exit

if ! [ -e .config/picom/picom.conf ] ||
    grep -q 'instantos' .config/picom/picom.conf &&
    ! grep 'picom' ~/.instantrc | grep '0[^a-zA-Z]*$'; then
    genconfig
fi

picom "$@" &
