#!/bin/sh
# ==============================================================
#  Laptop HiDPI Setup Script
# --------------------------------------------------------------
#  Author   : Kris Yotam (aka. khr1st)
#  Contact  : krisyotam@protonmail.com
#  License  : GNU GPLv3
#  Date     : 2025-09-29
# --------------------------------------------------------------
#  Description:
#    - Set xrandr DPI for HiDPI displays (immediate effect).
#    - Update xprofile to persist DPI across reboots.
#    - Update ~/.Xresources for terminal fonts and DPI.
#    - Update dwm/st config.def.h files for git-safe persistence.
#    - Creates device flag file for permanent laptop designation.
#    - Backups are made before modifying files.
#    - Created for Macbook Pro 2015 13" Retina (Model A1502)
# ==============================================================

DPI_VALUE=150
FONT="Monospace:size=12"
DWM_FONT_SIZE=11
DWM_EMOJI_SIZE=16
ST_FONT_SIZE=14

# Device flag directory and file
DEVICE_FLAG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/lazykris"
DEVICE_FLAG_FILE="$DEVICE_FLAG_DIR/device-type"

# ==============================================================
# Create device flag for permanent laptop designation
# ==============================================================
mkdir -p "$DEVICE_FLAG_DIR"
echo "laptop" > "$DEVICE_FLAG_FILE"
echo "[ok] Device marked as laptop in $DEVICE_FLAG_FILE"

# ==============================================================
# Apply DPI change immediately
# ==============================================================
xrandr --dpi "$DPI_VALUE"

# ==============================================================
# Backup and patch xprofile for persistent DPI
# ==============================================================
XPROFILE_XDG="${XDG_CONFIG_HOME:-$HOME/.config}/x11/xprofile"
XPROFILE_HOME="$HOME/.xprofile"

if [ -f "$XPROFILE_XDG" ]; then
    XPROFILE="$XPROFILE_XDG"
elif [ -f "$XPROFILE_HOME" ]; then
    XPROFILE="$XPROFILE_HOME"
else
    XPROFILE="$XPROFILE_XDG"
    mkdir -p "$(dirname "$XPROFILE")"
fi

if [ -f "$XPROFILE" ]; then
    cp "$XPROFILE" "$XPROFILE.bak.$(date +%s)"
    if grep -q "^xrandr --dpi" "$XPROFILE"; then
        sed -i "s/^xrandr --dpi.*/xrandr --dpi $DPI_VALUE\t\t# Set DPI for laptop HiDPI/" "$XPROFILE"
    else
        if head -1 "$XPROFILE" | grep -q "^#!"; then
            sed -i "2i xrandr --dpi $DPI_VALUE\t\t# Set DPI for laptop HiDPI" "$XPROFILE"
        else
            sed -i "1i xrandr --dpi $DPI_VALUE\t\t# Set DPI for laptop HiDPI" "$XPROFILE"
        fi
    fi
else
    cat > "$XPROFILE" << EOF
#!/bin/sh
xrandr --dpi $DPI_VALUE		# Set DPI for laptop HiDPI
EOF
fi
echo "[ok] Updated $XPROFILE"

# ==============================================================
# Backup and patch ~/.Xresources
# ==============================================================
XRES="$HOME/.Xresources"
[ -f "$XRES" ] && cp "$XRES" "$XRES.bak.$(date +%s)"

# Ensure DPI line
if grep -q "^Xft.dpi:" "$XRES" 2>/dev/null; then
    sed -i "s/^Xft.dpi:.*/Xft.dpi: $DPI_VALUE/" "$XRES"
else
    echo "Xft.dpi: $DPI_VALUE" >> "$XRES"
fi

# Ensure terminal font line
if grep -q "^st.font:" "$XRES" 2>/dev/null; then
    sed -i "s/^st.font:.*/st.font: $FONT/" "$XRES"
else
    echo "st.font: $FONT" >> "$XRES"
fi

xrdb -merge "$XRES"
echo "[ok] Updated $XRES"

# ==============================================================
# Patch dwm config files (both config.h AND config.def.h)
# ==============================================================
DWM_DIR="$HOME/.local/src/dwm"
DWM_CONFIG="$DWM_DIR/config.h"
DWM_CONFIG_DEF="$DWM_DIR/config.def.h"

patch_dwm_config() {
    local config_file="$1"
    if [ -f "$config_file" ]; then
        cp "$config_file" "$config_file.bak.$(date +%s)"
        sed -i "s/monospace:size=[0-9]*/monospace:size=$DWM_FONT_SIZE/" "$config_file"
        sed -i "s/NotoColorEmoji:pixelsize=[0-9]*/NotoColorEmoji:pixelsize=$DWM_EMOJI_SIZE/" "$config_file"
        echo "[ok] Updated $config_file"
    fi
}

if [ -d "$DWM_DIR" ]; then
    patch_dwm_config "$DWM_CONFIG"
    patch_dwm_config "$DWM_CONFIG_DEF"
    echo "[..] Rebuilding dwm..."
    cd "$DWM_DIR" && sudo make clean install >/dev/null 2>&1
    echo "[ok] dwm rebuilt with bar font size=$DWM_FONT_SIZE"
fi

# ==============================================================
# Patch st config files (both config.h AND config.def.h)
# ==============================================================
ST_DIR="$HOME/.local/src/st"
ST_CONFIG="$ST_DIR/config.h"
ST_CONFIG_DEF="$ST_DIR/config.def.h"

patch_st_config() {
    local config_file="$1"
    if [ -f "$config_file" ]; then
        cp "$config_file" "$config_file.bak.$(date +%s)"
        # Update default font size in st
        sed -i "s/pixelsize=[0-9]*/pixelsize=$ST_FONT_SIZE/" "$config_file"
        echo "[ok] Updated $config_file"
    fi
}

if [ -d "$ST_DIR" ]; then
    patch_st_config "$ST_CONFIG"
    patch_st_config "$ST_CONFIG_DEF"
    echo "[..] Rebuilding st..."
    cd "$ST_DIR" && sudo make clean install >/dev/null 2>&1
    echo "[ok] st rebuilt with font pixelsize=$ST_FONT_SIZE"
fi

# ==============================================================
# Patch dwmblocks config if exists
# ==============================================================
DWMBLOCKS_DIR="$HOME/.local/src/dwmblocks"
if [ -d "$DWMBLOCKS_DIR" ]; then
    echo "[..] Rebuilding dwmblocks..."
    cd "$DWMBLOCKS_DIR" && sudo make clean install >/dev/null 2>&1
    echo "[ok] dwmblocks rebuilt"
    # Kill and restart dwmblocks with proper signal handling for clickability
    pkill -x dwmblocks
    sleep 0.5
    setsid -f dwmblocks
    echo "[ok] dwmblocks restarted with clickability"
fi

# ==============================================================
# Summary
# ==============================================================
echo ""
echo "=============================================================="
echo " Laptop mode enabled with persistent settings"
echo "=============================================================="
echo " DPI:            $DPI_VALUE"
echo " Terminal font:  $FONT"
echo " DWM bar font:   monospace:size=$DWM_FONT_SIZE"
echo " DWM emoji:      NotoColorEmoji:pixelsize=$DWM_EMOJI_SIZE"
echo " ST font:        pixelsize=$ST_FONT_SIZE"
echo " Device flag:    $DEVICE_FLAG_FILE"
echo ""
echo " Settings are now permanent and will persist across:"
echo "   - Reboots (via xprofile and Xresources)"
echo "   - Git pulls (config.def.h files updated)"
echo ""
echo " [!!] Restart dwm (Mod+Shift+Ctrl+q) for changes to take effect"
echo "=============================================================="
