#!/bin/bash

# apply instantOS dpi settings

DPI="$(iconf dpi)"

resetdpi() {
    echo "resetting dpi"
    sed -i '/^Xft\.dpi/d' ~/.Xresources
    sed -i '/^st.font/d' ~/.Xresources
}

appendpi() {
    echo "setting dpi to $DPI"
    echo "Xft.dpi: $DPI" >>~/.Xresources
    NEWDPI="$(echo "( $DPI / 96 ) * 15" | bc -l | grep -o '^[^\.]*')"
    sed -i '/^st.font/d' ~/.Xresources
    echo "st.font: Fira Code Nerd Font Mono:pixelsize=$NEWDPI:antialias=true:autohint=true" >>~/.Xresources
    echo "st.font2: JoyPixels:pixelsize=$NEWDPI:antialias=true:autohint=true" >>~/.Xresources
}

if [ -z "$DPI" ]; then
    resetdpi
    exit
fi

[ "$DPI" -eq "$DPI" ] || exit

# resetting
if [ "$DPI" = "96" ]; then
    resetdpi
    exit
fi

if ! [ -e .Xresources ]; then
    echo "initializing xresources"
    touch ~/.Xresources
    appendpi "$DPI"
else
    if grep -q '^Xft.dpi' ~/.Xresources; then
        resetdpi
    fi
    appendpi "$DPI"
fi
