#!/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'"]
EOT
        fi
    else
        echo "vm detected"
    fi

}

cd

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

if ! grep -q 'hypervisor' /proc/cpuinfo &&
    grep -q 'instantos' .config/picom/picom.conf &&
    iconf -i blur; then
    picom --experimental-backends &
else
    picom "$@" &
fi
